bool check_crc (uint16_t* length) { uint32_t ii; uint16_t crc = 0, crcexpected = 0; uint8_t byte; *length = 0; // get length of the application eep_read(MOD_eExtEEPAddr_AppSize, MOD_LEN_APPSIZE, (uint8_t*)length); if (*length > EEPROM_SIZE) return false; // read expected CRC value eep_read(MOD_eExtEEPAddr_AppCrc, MOD_LEN_APPCRC, (uint8_t*)&crcexpected); // calculate CRC of EEProm content crc = CRC_START_VALUE; for (ii=0; ii<(*length); ii++) { eep_read(MOD_eExtEEPAddr_AppStart + ii, 1, &byte); crc = crc_16_next_byte(crc, byte); } if (crc != crcexpected) return false; return true; }
void program_flash (uint16_t address, uint16_t length) { uint16_t ii, ww; uint8_t temp; // wait until eventually running write cycles of internal eeprom are // finished. eeprom_busy_wait (); do { // erase flash page and wait until the memory is erased. boot_page_erase (address); boot_spm_busy_wait (); // only copy full 16bit words to flash memory if (length % 2) length--; for (ii=0; ii<SPM_PAGESIZE; ii+=2) { // read a word and convert to litte endian eep_read(MOD_eExtEEPAddr_AppStart + address + ii, 1, &temp); ww = temp; eep_read(MOD_eExtEEPAddr_AppStart + address + ii + 1, 1, &temp); ww |= temp << 8; boot_page_fill (address + ii, ww); length -= 2; if (length == 0) break; } // store buffer in flash page and wait until the memory is written. boot_page_write (address); boot_spm_busy_wait(); // one flash page has been written address += SPM_PAGESIZE; } while (length > 0); }
int eep_self_test(uint32_t begin, uint32_t end) { int i,j; uint8_t buf[PAGE_SIZE]; for(i=0; i<(end - begin)/PAGE_SIZE; i++) { for(j=0; j<sizeof(buf);j++) { buf[j] = j&0xFF; } printf("eep write 0x%X...", i*PAGE_SIZE); eep_write_page(i*PAGE_SIZE, buf, 8); memset(buf, 0, sizeof(buf)); eep_read(i*PAGE_SIZE, buf, sizeof(buf)); printf("varify..."); for(j=0; j<sizeof(buf); j++) { if(buf[j] != j%0xFF) { //printf("index:%d 0x%X err\r\n", j, buf[j]); return EEP_ERROR; } } printf("ok!\r\n"); } return EEP_OK; }
bool check_controller_id (void) { uint8_t sigbyte, eepcontent = 0; eep_read(BLD_eExtEEPAddr_CtrlID + 0, 1, &eepcontent); sigbyte = boot_signature_byte_get(ADDR_SIGNATURE_BYTE0); if (sigbyte != eepcontent) return false; eep_read(BLD_eExtEEPAddr_CtrlID + 1, 1, &eepcontent); sigbyte = boot_signature_byte_get(ADDR_SIGNATURE_BYTE1); if (sigbyte != eepcontent) return false; eep_read(BLD_eExtEEPAddr_CtrlID + 2, 1, &eepcontent); sigbyte = boot_signature_byte_get(ADDR_SIGNATURE_BYTE2); if (sigbyte != eepcontent) return false; return true; }
/********************************************************************** functionName:void pcm_read(void) description:从flash区读取上位机参数 **********************************************************************/ void pcm_read(void) { #if defined USE_PCM_SD_SAVE pcm_read_sd(); #elif defined USE_PCM_INSIDE_FLASH_SAVE eep_read(pcm_ram,PCM_DATA_BASE,PCM_MEM_SIZE); #elif defined USE_PCM_OUTSIDE_FLASH_SAVE sf_ReadBuffer(pcm_ram,PCM_DATA_BASE,PCM_MEM_SIZE); #endif // eep_read(pcm_ram,PCM_DATA_BASE,PCM_MEM_SIZE); // pcm_read_sd(); // sf_ReadBuffer(pcm_ram,PCM_DATA_BASE,PCM_MEM_SIZE); }
/******************************************************************************* * @函数名称 Init_Timer_Cnt * @函数说明 给全局计数器清零 * @输入参数 无 * @输出参数 无 * @返回参数 无 *******************************************************************************/ void Init_Timer_Cnt(void) { Pile_State.Open_Flag = 1; Pile_State.Close_Flag = true; Pcak_Pile_State_All_Flag = 0; Can1_Rev_Flag = false; debug = 0; log_w = 0; Stitic_Time_Cnt = 0; Queue_Create(&Q_dir,Q_DIR); //Flash_Read_Inside(PROG_DATA_ADDR,Only_ID,12); //static uint8_t buf[20] = {'0','1','2','3','4','5','6','7','8','9','10','11'}; //at24cxx_write(0, buf, 12); eep_read(0, Only_ID, 12); /* read only ID */ /* RTC 判断时间是否合法 */ if(!RTC_IsTimeValid()) { RTC_DateTime_Type td = {0}; td.year = 2011; td.month = 11; td.day = 11; td.hour = 11; td.minute = 11; td.second = 11; RTC_SetTime(&td); } #if DEBUG RTC_DateTime_Type td = {0}; RTC_GetTime(&td); printf("first:%d-%d-%d %d:%d:%d\r\n", td.year, td.month, td.day, td.hour, td.minute, td.second); /* 设置闹钟在当前3秒后 */ /* RTC_GetTime(&td); td.second += 3; RTC_SetAlarm(&td); */ #endif }
/********************************************************************** * Function: void load_param(void) * PreCondition: None * Input: None * Output: None * Side Effects: * Overview: Load parameters from EEPROM ***********************************************************************/ void load_param(void) { unsigned char adr; unsigned int signature, nb_essai = 3; adr = 0; while (--nb_essai && signature != EEP_SIGNATURE) { eep_read(adr, (unsigned char *)&signature, sizeof(signature)); } adr += sizeof(signature); if (signature == EEP_SIGNATURE) { eep_adr_config_byte = adr; eep_read(adr, (unsigned char *)&config_byte.byte, sizeof(config_byte.byte)); adr += sizeof(config_byte.byte); eep_adr_stepper_delay_manual = adr; eep_read(adr, (unsigned char *)&stepper_delay_manual, sizeof(stepper_delay_manual)); adr += sizeof(stepper_delay_manual); eep_adr_stepper_delay_auto = adr; eep_read(adr, (unsigned char *)&stepper_delay_auto, sizeof(stepper_delay_auto)); adr += sizeof(stepper_delay_auto); eep_adr_stepper_pos_max = adr; eep_read(adr, (unsigned char *)&stepper_pos_max, sizeof(stepper_pos_max)); adr += sizeof(stepper_pos_max); eep_adr_stepper_pos_min = adr; eep_read(adr, (unsigned char *)&stepper_pos_min, sizeof(stepper_pos_min)); adr += sizeof(stepper_pos_min); eep_adr_stepper_position = adr; eep_read(adr, (unsigned char *)&stepper_position, sizeof(stepper_position)); adr += sizeof(stepper_position); eep_adr_stepper_backlash = adr; eep_read(adr, (unsigned char *)&stepper_backlash, sizeof(stepper_backlash)); adr += sizeof(stepper_backlash); eep_adr_temperature_coef = adr; eep_read(adr, (unsigned char *)&temperature_coef, sizeof(temperature_coef)); adr += sizeof(temperature_coef); eep_adr_p_step_sequencer = adr; eep_read(adr, (unsigned char *)&p_step_sequencer, sizeof(p_step_sequencer)); adr += sizeof(p_step_sequencer); eep_adr_pwr_threshold = adr; eep_read(adr, (unsigned char *)&pwr_threshold, sizeof(pwr_threshold)); adr += sizeof(pwr_threshold); } else { // bad signature, writing default values eep_adr_config_byte = adr; eep_write(adr, (unsigned char *)&config_byte.byte, sizeof(config_byte.byte)); adr += sizeof(config_byte.byte); eep_adr_stepper_delay_manual = adr; eep_write(adr, (unsigned char *)&stepper_delay_manual, sizeof(stepper_delay_manual)); adr += sizeof(stepper_delay_manual); eep_adr_stepper_delay_auto = adr; eep_write(adr, (unsigned char *)&stepper_delay_auto, sizeof(stepper_delay_auto)); adr += sizeof(stepper_delay_auto); eep_adr_stepper_pos_max = adr; eep_write(adr, (unsigned char *)&stepper_pos_max, sizeof(stepper_pos_max)); adr += sizeof(stepper_pos_max); eep_adr_stepper_pos_min = adr; eep_write(adr, (unsigned char *)&stepper_pos_min, sizeof(stepper_pos_min)); adr += sizeof(stepper_pos_min); eep_adr_stepper_position = adr; eep_write(adr, (unsigned char *)&stepper_position, sizeof(stepper_position)); adr += sizeof(stepper_position); eep_adr_stepper_backlash = adr; eep_write(adr, (unsigned char *)&stepper_backlash, sizeof(stepper_backlash)); adr += sizeof(stepper_backlash); eep_adr_temperature_coef = adr; eep_write(adr, (unsigned char *)&temperature_coef, sizeof(temperature_coef)); adr += sizeof(temperature_coef); eep_adr_p_step_sequencer = adr; eep_write(adr, (unsigned char *)&p_step_sequencer, sizeof(p_step_sequencer)); adr += sizeof(p_step_sequencer); eep_adr_pwr_threshold = adr; eep_write(adr, (unsigned char *)&pwr_threshold, sizeof(pwr_threshold)); adr += sizeof(pwr_threshold); signature = EEP_SIGNATURE; eep_write(0, (unsigned char *)&signature, sizeof(signature)); } }