iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_state.c Source File

u8g_state.c

00001 /*
00002 
00003   u8g_state.c
00004   
00005   backup and restore hardware state
00006 
00007   Universal 8bit Graphics Library
00008   
00009   Copyright (c) 2011, olikraus@gmail.com
00010   All rights reserved.
00011 
00012   Redistribution and use in source and binary forms, with or without modification, 
00013   are permitted provided that the following conditions are met:
00014 
00015   * Redistributions of source code must retain the above copyright notice, this list 
00016     of conditions and the following disclaimer.
00017     
00018   * Redistributions in binary form must reproduce the above copyright notice, this 
00019     list of conditions and the following disclaimer in the documentation and/or other 
00020     materials provided with the distribution.
00021 
00022   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
00023   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
00024   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00025   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00027   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00028   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
00029   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00030   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00031   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00032   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00033   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00034   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00035   
00036 
00037   state callback: backup env U8G_STATE_MSG_BACKUP_ENV
00038   device callback: DEV_MSG_INIT
00039   state callback: backup u8g U8G_STATE_MSG_BACKUP_U8G
00040   state callback: restore env U8G_STATE_MSG_RESTORE_ENV
00041 
00042   state callback: backup env U8G_STATE_MSG_BACKUP_ENV
00043   state callback: retore u8g U8G_STATE_MSG_RESTORE_U8G
00044   DEV_MSG_PAGE_FIRST or DEV_MSG_PAGE_NEXT
00045   state callback: restore env U8G_STATE_MSG_RESTORE_ENV
00046 
00047 */
00048 
00049 #include <stddef.h>
00050 #include "u8g.h"
00051 
00052 void u8g_state_dummy_cb(uint8_t msg)
00053 {
00054   /* the dummy procedure does nothing */
00055 }
00056 
00057 void u8g_SetHardwareBackup(u8g_t *u8g, u8g_state_cb backup_cb)
00058 {
00059   u8g->state_cb = backup_cb;
00060   /* in most cases the init message was already sent, so this will backup the */
00061   /* current u8g state */
00062   backup_cb(U8G_STATE_MSG_BACKUP_U8G);
00063 }
00064 
00065 
00066 /*===============================================================*/
00067 /* register variable for restoring interrupt state */
00068 
00069 #if defined(__AVR__)
00070 uint8_t global_SREG_backup;
00071 #endif
00072 
00073 
00074 
00075 /*===============================================================*/
00076 /* AVR */
00077 
00078 #if defined(__AVR__)
00079 #define U8G_ATMEGA_HW_SPI
00080 
00081 /* remove the definition for attiny */
00082 #if __AVR_ARCH__ == 2
00083 #undef U8G_ATMEGA_HW_SPI
00084 #endif
00085 #if __AVR_ARCH__ == 25
00086 #undef U8G_ATMEGA_HW_SPI
00087 #endif
00088 #endif
00089 
00090 #if defined(U8G_ATMEGA_HW_SPI)
00091 #include <avr/interrupt.h>
00092 static uint8_t u8g_state_avr_spi_memory[2];
00093 
00094 void u8g_backup_spi(uint8_t msg)
00095 {
00096   if ( U8G_STATE_MSG_IS_BACKUP(msg) )
00097   {
00098     u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)] = SPCR;
00099   }
00100   else
00101   {
00102     uint8_t tmp = SREG;
00103     cli();
00104     SPCR = 0;
00105     SPCR = u8g_state_avr_spi_memory[U8G_STATE_MSG_GET_IDX(msg)];
00106     SREG = tmp;
00107   }
00108 }
00109 
00110 #elif defined(ARDUINO) && defined(__arm__)      // Arduino Due, maybe we should better check for __SAM3X8E__
00111 
00112 #include "sam.h"
00113 
00114 struct sam_backup_struct
00115 {
00116   uint32_t mr;
00117   uint32_t sr;
00118   uint32_t csr[4];  
00119 } sam_backup[2];
00120 
00121 void u8g_backup_spi(uint8_t msg)
00122 {
00123   uint8_t idx = U8G_STATE_MSG_GET_IDX(msg);
00124   if ( U8G_STATE_MSG_IS_BACKUP(msg) )
00125   {
00126     sam_backup[idx].mr = SPI0->SPI_MR;
00127     sam_backup[idx].sr = SPI0->SPI_SR;
00128     sam_backup[idx].csr[0] = SPI0->SPI_CSR[0];
00129     sam_backup[idx].csr[1] = SPI0->SPI_CSR[1];
00130     sam_backup[idx].csr[2] = SPI0->SPI_CSR[2];
00131     sam_backup[idx].csr[3] = SPI0->SPI_CSR[3];
00132   }
00133   else
00134   {
00135     SPI0->SPI_MR = sam_backup[idx].mr;
00136     SPI0->SPI_CSR[0] = sam_backup[idx].csr[0];
00137     SPI0->SPI_CSR[1] = sam_backup[idx].csr[1];
00138     SPI0->SPI_CSR[2] = sam_backup[idx].csr[2];
00139     SPI0->SPI_CSR[3] = sam_backup[idx].csr[3];
00140   }
00141 }
00142 
00143 #else
00144 
00145 void u8g_backup_spi(uint8_t msg)
00146 {
00147 }
00148 
00149 #endif
00150 
00151