return success of save_code

This commit is contained in:
King Kévin 2014-08-02 15:47:37 -07:00
parent 2ef68f1a1c
commit 6e68b14cc9
1 changed files with 17 additions and 12 deletions

View File

@ -186,8 +186,13 @@ void write_code(uint16_t address)
} }
/* look in the memory for the code (global variable) /* look in the memory for the code (global variable)
* if not present, write it a the next free memory space */ * if not present, write it a the next free memory space
void save_code() * return 0 if the code is already in EEPROM
* return 1 if the code is new and saved
* return 2 if error occured
* return 3 if no space in memory
*/
uint8_t save_code(void)
{ {
uint16_t address; uint16_t address;
uint8_t stored[3]; uint8_t stored[3];
@ -195,21 +200,21 @@ void save_code()
send_start(); send_start();
if (send_byte(0xa0)) { /* write address to eeprom at 0xA0/0x50 */ if (send_byte(0xa0)) { /* write address to eeprom at 0xA0/0x50 */
send_stop(); send_stop();
return; return 2;
} }
if (send_byte(0x00)) { /* go to address 0x0000 */ if (send_byte(0x00)) { /* go to address 0x0000 */
send_stop(); send_stop();
return; return 2;
} }
if (send_byte(0x00)) { /* go to address 0x0000 */ if (send_byte(0x00)) { /* go to address 0x0000 */
send_stop(); send_stop();
return; return 2;
} }
/* start reading */ /* start reading */
send_start(); send_start();
if (send_byte(0xa1)) { /* read eeprom at 0xA0/0x50 */ if (send_byte(0xa1)) { /* read eeprom at 0xA0/0x50 */
send_stop(); send_stop();
return; return 2;
} }
/* go through memory */ /* go through memory */
for (address=0; address<0x7FFF-2; address+=3) { for (address=0; address<0x7FFF-2; address+=3) {
@ -221,13 +226,14 @@ void save_code()
read_byte(0); /* send a NACK to stop reading */ read_byte(0); /* send a NACK to stop reading */
send_stop(); /* finish transaction */ send_stop(); /* finish transaction */
write_code(address); /* write code at this space */ write_code(address); /* write code at this space */
return; return 1;
} else if (stored[0]==code[0] && stored[1]==code[1] && stored[2]==code[2]) { /* code already stored */ } else if (stored[0]==code[0] && stored[1]==code[1] && stored[2]==code[2]) { /* code already stored */
read_byte(0); /* send a NACK to stop reading */ read_byte(0); /* send a NACK to stop reading */
send_stop(); /* finish transaction */ send_stop(); /* finish transaction */
return; return 0;
} }
} }
return 3;
} }
void clear_memory(void) void clear_memory(void)
@ -254,9 +260,7 @@ void clear_memory(void)
send_byte(0x00); /* clear byte */ send_byte(0x00); /* clear byte */
if ((address%0x40)==0x3f) { /* end of page */ if ((address%0x40)==0x3f) { /* end of page */
send_stop(); /* finish transaction */ send_stop(); /* finish transaction */
for (wait=0; wait<1024; wait++) { /* wait for eeprom to be writen */ for (wait=0; wait<1024; wait++); /* wait for eeprom to be writen */
wait;
}
} }
} }
} }
@ -300,6 +304,7 @@ void main (void)
uint8_t rx = 0; /* are we receiving a pulse */ uint8_t rx = 0; /* are we receiving a pulse */
uint8_t bit = 0; /* the current received bit */ uint8_t bit = 0; /* the current received bit */
uint8_t time = 0; /* time since previous bitframe start, in 0.256ms steps */ uint8_t time = 0; /* time since previous bitframe start, in 0.256ms steps */
uint16_t new = 0; /* is the code new */
init(); /* configure micro-controller */ init(); /* configure micro-controller */
@ -335,7 +340,7 @@ void main (void)
} }
if (bit==24) { /* received all 24 bits */ if (bit==24) { /* received all 24 bits */
led_on(); led_on();
save_code(); save_code(); /* save code in external EEPROM */
led_off(); led_off();
} }
} }