Remote Access Control


memory.c

Go to the documentation of this file.
00001 // This file has been prepared for Doxygen automatic documentation generation.
00048 #include "memory.h"
00049 #include "common.h"
00050 #include "config.h"
00051 
00052 
00053 
00054 /* --- EEPROM memory contents --- */
00055 
00056 // Variables at absolute addresses are defined in the header file only.
00057 
00058 
00059 
00060 /* --- SRAM memory contents --- */
00061 
00062 byte cryptoBlock[ BLOCK_SIZE + 1 ];
00063 union SharedBlock sharedBlock;
00064 
00065 
00066 
00067 /* --- Associated functions --- */
00068 
00069 void initializeMessage(void)
00070 {
00071         // The preamble is used as a synch-and-start indicator for
00072         // the RF receiver.
00073         sharedBlock.messagePayload.preamble = 0xfe; // 11111110
00074         sharedBlock.messagePayload.serialNo = serialNo;
00075 }
00076 
00077 
00078 
00079 void createMessage( COMMAND_CODE_TYPE commandCode )
00080 {
00081         sharedBlock.messagePayload.commandCode = commandCode;
00082         sharedBlock.messagePayload.counterValue = nextCounterValue;
00083 }
00084 
00085 
00086 
00088 static volatile byte counterBytesLeft = 0;
00089 
00090 
00091 
00092 void startCounterUpdate()
00093 {
00094         // Make sure EEPROM is ready.
00095         do {} while( EECR & (1<<EEWE) );
00096         // Prepare control variables.
00097         EEDR = 0xff; // 0xff indicates that we just started.
00098         EEAR = (uint16_t) &nextCounterValue;
00099         counterBytesLeft = SEQ_COUNTER_BYTES;
00100         // Enable EEPROM Ready Interrupt.
00101         EECR |= (1<<EERIE);
00102 }
00103 
00104 
00105 
00106 void waitForCounterUpdate()
00107 {
00108         while( counterBytesLeft ) { __sleep(); }
00109 }
00110 
00111 
00112 
00113 /* --- ISR and variables for updating counter value --- */
00114 
00116 #pragma vector = EE_RDY_vect
00117 __interrupt void eepromReadyHandler(void)
00118 {
00119         // If previous byte wrapped to 0x00, increase address.
00120         if( EEDR == 0x00 ) {
00121                 ++EEAR;
00122         }
00123 
00124         // Read old value.
00125         EECR |= (1<<EERE);
00126         // Increase.
00127         ++EEDR;
00128         // If no overflow, no bytes left to update.
00129         if( EEDR != 0x00 ) {
00130                 counterBytesLeft = 0;
00131                 EECR &= ~(1<<EERIE); // Disable further interrupts.
00132         } else {
00133                 --counterBytesLeft;
00134         }
00135         
00136         // Start writing.
00137         EECR |= (1<<EEMWE);
00138         EECR |= (1<<EEWE);
00139 }
00140 
@DOC_TITLE@
Generated on Fri Aug 8 11:03:16 2008 for AVR411 Secure Rolling Code Algorithm (Transmitter) by doxygen 1.4.7