From 6e68b14cc9499381f8e12ff92a0e4fd8cea3c25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 2 Aug 2014 15:47:37 -0700 Subject: [PATCH] return success of save_code --- pic/MDR/MDR.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/pic/MDR/MDR.c b/pic/MDR/MDR.c index 3124ec9..37d2038 100644 --- a/pic/MDR/MDR.c +++ b/pic/MDR/MDR.c @@ -186,8 +186,13 @@ void write_code(uint16_t address) } /* look in the memory for the code (global variable) - * if not present, write it a the next free memory space */ -void save_code() + * if not present, write it a the next free memory space + * 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; uint8_t stored[3]; @@ -195,21 +200,21 @@ void save_code() send_start(); if (send_byte(0xa0)) { /* write address to eeprom at 0xA0/0x50 */ send_stop(); - return; + return 2; } if (send_byte(0x00)) { /* go to address 0x0000 */ send_stop(); - return; + return 2; } if (send_byte(0x00)) { /* go to address 0x0000 */ send_stop(); - return; + return 2; } /* start reading */ send_start(); if (send_byte(0xa1)) { /* read eeprom at 0xA0/0x50 */ send_stop(); - return; + return 2; } /* go through memory */ for (address=0; address<0x7FFF-2; address+=3) { @@ -221,13 +226,14 @@ void save_code() read_byte(0); /* send a NACK to stop reading */ send_stop(); /* finish transaction */ 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 */ read_byte(0); /* send a NACK to stop reading */ send_stop(); /* finish transaction */ - return; + return 0; } } + return 3; } void clear_memory(void) @@ -254,9 +260,7 @@ void clear_memory(void) send_byte(0x00); /* clear byte */ if ((address%0x40)==0x3f) { /* end of page */ send_stop(); /* finish transaction */ - for (wait=0; wait<1024; wait++) { /* wait for eeprom to be writen */ - wait; - } + for (wait=0; wait<1024; wait++); /* wait for eeprom to be writen */ } } } @@ -300,6 +304,7 @@ void main (void) uint8_t rx = 0; /* are we receiving a pulse */ uint8_t bit = 0; /* the current received bit */ 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 */ @@ -335,7 +340,7 @@ void main (void) } if (bit==24) { /* received all 24 bits */ led_on(); - save_code(); + save_code(); /* save code in external EEPROM */ led_off(); } }