/* * Calls SD_read until SD_read doesn't fail * because of the random errors. */ int safe_read(int sector, void* buf) { int success = SD_read(sector, buf); if (success == -1 && sderrno == E_READING_FILE) success = safe_read(sector, buf); return success; }
char SD_read(unsigned char bytes) { SD_read(sd_sector, sd_pos, fat16_buffer, bytes); sd_pos+=(unsigned short)bytes; if(sd_pos == 512) { sd_pos = 0; sd_sector++; } return bytes; }
// find the start address in sdcard for this new logging session // and set the common log index for eeprom and sdcard static void LOG_find_sdcard_start(u8 eeprom_index) { u8 buf[2]; // scan the whole eeprom while (1) { // read the 2 first octets of the log at LOG.addr SD_read(LOG.sdcard_addr, buf, sizeof(buf)); while ( !SD_is_fini() ) ; // if the read octets are erased eeprom if ( (buf[0] == 0xff) && (buf[1] == 0xff) ) { // the new log session start address is found (here) // // the previous index is known from the previous read (see below) // so increment it LOG.index++; // index found during eeprom scanning is higher if ( eeprom_index > LOG.index ) { // use the eeprom index LOG.index = eeprom_index; } // finally the scan is over return; } // extract index LOG.index = buf[0]; // check the next log block LOG.sdcard_addr += sizeof(log_t); // if address is out of range if ( LOG.sdcard_addr >= SDCARD_END_ADDR ) { // sdcard is full // so give up // the log thread protection will prevent overwriting LOG.index = 0xff; } } }