int eink_getdata(int buflen) { unsigned long int i,len,dumplen=0; unsigned long int startaddress = ImageAddr; //图像在flash中的起始地址. unsigned char buf[DATA_BUFFER_LEN]={0}; unsigned char *pdata = buf; len = DATA_BUFFER_LEN; dumplen = IMAGE_LENGTH -buflen; if(buflen > len) { flash_read_data(startaddress+dumplen,buf,len); for(i=0;i<len;i++) { exchange_buff[i]=buf[i]; } return len; }else{ flash_read_data(startaddress+dumplen,buf,len); for(i=0;i<buflen;i++) { exchange_buff[i]=buf[i]; } return buflen; } }
/**************************************************************************//** * @brief Reads data from flash and returns the the last occupied location * * @param data is the pointer to to store the read data * @param length is the length of the data to read * * @return \b flash_write_count is the last occupied location ******************************************************************************/ unsigned int flash_read_wear_level(unsigned int *data, unsigned int length) { unsigned int ii, index; unsigned int flash_write_count; // check to see if there is any data in the array before starting the search flash_read_data(&flash_write_count, 0 ,1); if (flash_write_count == 0xFFFF) { return 0; } // search for the end last block to be occupied for(ii=0;ii<SIZE_OF_STORAGE_ARRAY;ii=ii+length+1) { flash_read_data(&flash_write_count, ii ,1); if(flash_write_count == 0xFFFF) { flash_read_data(&flash_write_count, 0 ,1); index = ii-(length+1); ii = SIZE_OF_STORAGE_ARRAY; } } // read the data from the last occupied location and return flash_read_data(&flash_write_count, index ,1); flash_read_data(data, index+1 ,length); return flash_write_count; }
/**************************************************************************//** * @brief Writes data after the last occupied location and returns the updated * value for the last occupied location * * @param data is the pointer to the data that will be written to the flash * @param length is the length of the data that will be written to the flash * * @return \b flash_write_count is the last occupied location ******************************************************************************/ unsigned int flash_write_wear_level(unsigned int *data, unsigned int length) { unsigned int ii, index; unsigned int flash_write_count; flash_read_data(&flash_write_count, 0 ,1); switch(flash_write_count){ // the case of a un-initialized array, brandnew device case 0xFFFF: flash_write_count = 1; flash_write_data(&flash_write_count, 0 ,1); flash_write_data(data, 1 ,length); break; // the case of having reached 50000 writes, reset the counter to 1 case 0xC350: flash_write_count = 1; flash_erase_segment(0, SIZE_OF_STORAGE_ARRAY); flash_write_data(&flash_write_count, 0 ,1); flash_write_data(data, 1 ,length); break; // Normal operation, find the end of the current array, store new data default: // search the array for the end of the flash storage array, look for 0xFFFF for(ii=0;ii<SIZE_OF_STORAGE_ARRAY;ii=ii+length+1) { flash_read_data(&flash_write_count, ii ,1); if(flash_write_count == 0xFFFF) { flash_read_data(&flash_write_count, 0 ,1); index = ii; ii = SIZE_OF_STORAGE_ARRAY; } } // before writing to segment, check to see if we are out of bound if(index+length+1 > SIZE_OF_STORAGE_ARRAY ) { // if we have reached the end, delete the entire array, increment counter and restart flash_read_data(&flash_write_count, 0 ,1); flash_erase_segment(0, SIZE_OF_STORAGE_ARRAY); flash_write_count++; flash_write_data(&flash_write_count, 0 ,1); flash_write_data(data, 1 ,length); } else { // Normal write to Flash array flash_write_data(&flash_write_count, index ,1); flash_write_data(data, index+1 ,length); } break; } return flash_write_count; }
void read_cmdset(void) { unsigned char buf[16] = {0}; unsigned long int startaddress = CMDSET_START_ADDR; spi_lcm_exchange(BUS_TO_SPI); flash_read_data(startaddress, buf, 16); spi_lcm_exchange(BUS_TO_LCM); startaddress = CMDSET_START_ADDR + CMDSET_LENGTH - 16; spi_lcm_exchange(BUS_TO_SPI); flash_read_data(startaddress, buf, 16); spi_lcm_exchange(BUS_TO_LCM); APP_TRACE("\nbuf[0]= %02x\n", buf[0]); }
int main(void) { u32 result = 0; u8 str_send[SEND_BUFFER_SIZE], str_verify[SEND_BUFFER_SIZE]; init_system(); while(1) { usart_send_string(USART1, (u8*)"Please enter string to write into Flash memory:\n\r", SEND_BUFFER_SIZE); usart_get_string(USART1, str_send, SEND_BUFFER_SIZE); result = flash_program_data(FLASH_OPERATION_ADDRESS, str_send, SEND_BUFFER_SIZE); switch(result) { case RESULT_OK: /*everything ok*/ usart_send_string(USART1, (u8*)"Verification of written data: ", SEND_BUFFER_SIZE); flash_read_data(FLASH_OPERATION_ADDRESS, SEND_BUFFER_SIZE, str_verify); usart_send_string(USART1, str_verify, SEND_BUFFER_SIZE); break; case FLASH_WRONG_DATA_WRITTEN: /*data read from Flash is different than written data*/ usart_send_string(USART1, (u8*)"Wrong data written into flash memory", SEND_BUFFER_SIZE); break; default: /*wrong flags' values in Flash Status Register (FLASH_SR)*/ usart_send_string(USART1, (u8*)"Wrong value of FLASH_SR: ", SEND_BUFFER_SIZE); local_ltoa_hex(result, str_send); usart_send_string(USART1, str_send, SEND_BUFFER_SIZE); break; } /*send end_of_line*/ usart_send_string(USART1, (u8*)"\r\n", 3); } return 0; }
void write_baseinfo(void) { unsigned int startaddress = FIRMWARE_OFFSET; unsigned int data; unsigned char info[16]; unsigned char i; unsigned int buf[4] = {CMDSET_LENGTH,CMDSET_START_ADDR, WAVEFORM_LENGTH, FIRMWARE_OFFSET }; unsigned char *Pcmd = (unsigned char *)buf; unsigned char temp; spi_lcm_exchange(BUS_TO_SPI); for(i=0; i < 16; i++) { flash_write_data(startaddress + i, Pcmd + i, 1); } for(i=0; i < 16; i++) { flash_read_data(startaddress + i,&temp, 1); if(temp != Pcmd[i]) { APP_TRACE("write_baseinfo [%08x] = %02x -> %02xerror\n",startaddress + i, temp,Pcmd[i] ); } } spi_lcm_exchange(BUS_TO_LCM); }
void write_cmdset(void) { unsigned long int i = 0; unsigned long int startaddress = CMDSET_START_ADDR; unsigned char *Pcmd = cmdset; unsigned char temp; total_len = 0; error_len = 0; spi_lcm_exchange(BUS_TO_SPI); for(i=0; i < CMDSET_LENGTH; i++) { flash_write_data(startaddress + i, Pcmd + i, 1); } total_len = CMDSET_LENGTH; for(i=0; i < CMDSET_LENGTH; i++) { flash_read_data(startaddress + i,&temp, 1); if(temp != Pcmd[i]) { APP_TRACE("write_cmdset [%08x] = %02x -> %02x error\n",startaddress + i, temp,Pcmd[i] ); error_len ++; } } spi_lcm_exchange(BUS_TO_LCM); APP_TRACE("write_cmdset is OK! %d, %d \r\n", total_len, error_len); }
void read_baseinfo(void) { unsigned long int startaddress = FIRMWARE_OFFSET; unsigned char buf[16] = {0}; spi_lcm_exchange(BUS_TO_SPI); flash_read_data(startaddress, buf, 16); spi_lcm_exchange(BUS_TO_LCM); }
void read_spi_falsh(unsigned long int address, unsigned char *buf, unsigned long int size) { spi_lcm_exchange(BUS_TO_SPI); flash_read_data(address, buf, size); spi_lcm_exchange(BUS_TO_LCM); //APP_TRACE("read_spi_flash is ok! \r\n"); }
void read_wav(void) { unsigned char buf[16] = {0}; unsigned long int startaddress = WAVFORM_START_ADDR; spi_lcm_exchange(BUS_TO_SPI); flash_read_data(startaddress, buf, 16); spi_lcm_exchange(BUS_TO_LCM); APP_TRACE("\nbuf[0]= %02x\n", buf[0]); }
void set_global_defaults(void) { flash_read_data((uint32_t*)configDataBlock.data, configData.data,sizeof(configData.config)); if (configData.config.validBlock == VALID_BLOCK) return; /* Set default communications control variables */ configData.config.measurementSend = true; configData.config.debugMessageSend = false; configData.config.enableSend = true; /* Set default recording control variables */ configData.config.recording = false; /* Set default measurement variables */ configData.config.measurementInterval = 1000; /* 1 second intervals */ configData.config.numberConversions = 6; /* number of interfaces plus temperature */ configData.config.numberSamples = 16; /* burst of samples for averaging */ }
void flash_write_test() { int i; int j; uint8_t buf[256]; uint8_t rbuf[256]; for(i = 0; i < 256; i++) { buf[i] = i; } flash_write_data(0x000000, buf, 256); flash_read_data(0x000000, rbuf, 256); for(i = 0; i < 256; i++) { if(i == rbuf[i]) { continue; } } }
void get_cmdset_waveform(unsigned long int *cmdlen, unsigned long int *wavlen) { unsigned long int startaddress = FIRMWARE_OFFSET; unsigned char buf[16] = {0}; unsigned long int len; spi_lcm_exchange(BUS_TO_SPI); flash_read_data(startaddress, buf, 16); spi_lcm_exchange(BUS_TO_LCM); len = buf[0]; len += buf[1] << 8; len += buf[2] << 16; len += buf[3] << 24; *cmdlen = len; len = buf[8]; len += buf[9] << 8; len += buf[10] << 16; len += buf[11] << 24; *wavlen = len; }
void write_image(void) { unsigned long int i = 0; unsigned long int len; unsigned long int startaddress = 0; unsigned char *Pcmd = gImage_image; unsigned char temp; unsigned char times; len = 0; error_len = 0; if(image_p == 0xff) return; startaddress = IMAGE_START_ADD + 300*16*(image_p -1); spi_lcm_exchange(BUS_TO_SPI); if(image_p< 2) { len = 300 * 16; } else { len = IMAGE_LENGTH - 300*16 * (image_p -1); } total_len = len; for(i=0; i < len; i++) { times = 0; flash_write_data(startaddress + i, Pcmd + i, 1); do{ flash_read_data(startaddress + i,&temp, 1); if(temp != Pcmd[i]) { flash_write_data(startaddress + i, Pcmd + i, 1); APP_TRACE("error addr: %08x\n", startaddress + i); } }while(times++ < 0); } for(i=0; i < len; i++) { flash_read_data(startaddress + i,&temp, 1); if(temp != Pcmd[i]) { APP_TRACE("\r\nwrite_wav [%08x] = %02x -> %02xerror\r\n",startaddress + i, temp,Pcmd[i] ); error_len++; } } spi_lcm_exchange(BUS_TO_LCM); APP_TRACE("write_image %d is OK! %d, %d \r\n",image_p, total_len, error_len); }
void write_wav(void) { unsigned long int i = 0; unsigned long int len; unsigned long int startaddress = 0; unsigned char *Pcmd = (unsigned char *)wfm_set; unsigned char temp; unsigned char times; error_len = 0; if(waveform_p == 0xff) return; startaddress = WAVFORM_START_ADDR + 200*16*(waveform_p -1); spi_lcm_exchange(BUS_TO_SPI); if(waveform_p < 23){ len = 200 * 16; } else { len = WAVEFORM_LENGTH - 200*16 * (waveform_p -1); } total_len = len; for(i=0; i < len; i++) { times = 0; flash_write_data(startaddress + i, Pcmd + i, 1); do{ flash_read_data(startaddress + i,&temp, 1); if(temp != Pcmd[i]) { flash_write_data(startaddress + i, Pcmd + i, 1); APP_TRACE("error addr: %08x\n", startaddress + i); } }while(times++ < 0); } for(i=0; i < len; i++) { flash_read_data(startaddress + i,&temp, 1); if(temp != Pcmd[i]) { //APP_TRACE("\r\nwrite_wav [%08x] = %02x -> %02xerror\r\n",startaddress + i, temp,Pcmd[i] ); error_len++; } } spi_lcm_exchange(BUS_TO_LCM); APP_TRACE("write_wav is OK! %d, %d \r\n", total_len, error_len); }