Mauricio Donatti / Mbed OS mbed-os-6-FlashIAP
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*********************************************************************
00002 *
00003 * FlashIAP (In-Application Programming) Test - MBED OS 6.2
00004 * 
00005 * Mauricio Martins Donatti
00006 * mauricio.donatti@lnls.br
00007 *
00008 * Electronics Instrumentation Group - GIE
00009 * Brazilian Synchrotron Light Laboratory (LNLS)
00010 * Brazilian Center for Research in Energy and Materials (CNPEM)
00011 *
00012 * August 2020
00013 *
00014 *
00015 *******************************************************************/
00016 
00017 #include "mbed.h"
00018 
00019 #define FLASH_DEBUG
00020 
00021 #ifdef FLASH_DEBUG
00022 #define FLASH_PRINTF(fmt, ...)          pc.printf("FLASH: " fmt "\r\n", ##__VA_ARGS__)
00023 #else
00024 #define FLASH_PRINTF(fmt, ...)          __NOP()
00025 #endif //Config enabled DEBUG
00026 
00027 // Time definitions
00028 #define SLEEP_DELAY         10
00029 #define HEARTBEAT_DELAY     1
00030 
00031 //LPC1768 has two sector sizes (4kB and 32kB)
00032 #define SIZE_4KB                  0x1000
00033 #define SIZE_32KB                 0x8000
00034 
00035 // 4kB Sectors
00036 #define BLOCK_0x00000_0x00FFF     0x00000
00037 #define BLOCK_0x01000_0x01FFF     0x01000
00038 #define BLOCK_0x02000_0x02FFF     0x02000
00039 #define BLOCK_0x03000_0x03FFF     0x03000
00040 #define BLOCK_0x04000_0x04FFF     0x04000
00041 #define BLOCK_0x05000_0x05FFF     0x05000
00042 #define BLOCK_0x06000_0x06FFF     0x06000
00043 #define BLOCK_0x07000_0x07FFF     0x07000
00044 #define BLOCK_0x08000_0x08FFF     0x08000
00045 #define BLOCK_0x09000_0x09FFF     0x09000
00046 #define BLOCK_0x0A000_0x0AFFF     0x0A000
00047 #define BLOCK_0x0B000_0x0BFFF     0x0B000
00048 #define BLOCK_0x0C000_0x0CFFF     0x0C000
00049 #define BLOCK_0x0D000_0x0DFFF     0x0D000
00050 #define BLOCK_0x0E000_0x0EFFF     0x0E000
00051 #define BLOCK_0x0F000_0x0FFFF     0x0F000
00052 
00053 //32kB Sectors
00054 #define BLOCK_0x10000_0x17FFF     0x10000
00055 #define BLOCK_0x18000_0x1FFFF     0x18000
00056 #define BLOCK_0x20000_0x27FFF     0x20000
00057 #define BLOCK_0x28000_0x2FFFF     0x28000
00058 #define BLOCK_0x30000_0x37FFF     0x30000
00059 #define BLOCK_0x38000_0x3FFFF     0x38000
00060 #define BLOCK_0x40000_0x47FFF     0x40000
00061 #define BLOCK_0x48000_0x4FFFF     0x48000
00062 #define BLOCK_0x50000_0x57FFF     0x50000
00063 #define BLOCK_0x58000_0x5FFFF     0x58000
00064 #define BLOCK_0x60000_0x67FFF     0x60000
00065 #define BLOCK_0x68000_0x6FFFF     0x68000
00066 #define BLOCK_0x70000_0x77FFF     0x70000
00067 #define BLOCK_0x78000_0x7FFFF     0x78000
00068 
00069 //Application Definitions
00070 #define DATA_LENGTH     1           //in bytes
00071 #define USED_BLOCKS     10          //number of used blocks
00072 #define BLOCK_SIZE      SIZE_32KB   //block size
00073 #define FIRST_BLOCK     BLOCK_0x10000_0x17FFF
00074 #define FLAG            1
00075 #define DEFAULT_DATA    'e'    
00076 
00077 Serial pc(USBTX, USBRX,115200);
00078 
00079 DigitalOut heart(LED1); //LED 1 - heart beat mbed
00080 DigitalOut msg_completed(LED2); //LED 1 - heart beat mbed
00081 
00082 Ticker heartbeat; //Define the digital output variable heartbeat as "Ticker" 
00083 int idx_buffer;
00084 char buffer[30];
00085 
00086 char last_value;
00087 
00088 void message_completed();
00089 // Heart beat function 
00090 void beat();
00091 
00092 FlashIAP flash;
00093 uint32_t page_size,sector_size,flash_size;
00094 uint32_t addr,active_addr;
00095 char erased_byte;
00096 int ret,active_block;
00097 
00098 void init_flash();
00099 void get_flash_data(char* buffer);
00100 void save_flash_data(char* buffer);
00101 char flash_buffer[DATA_LENGTH+1];
00102 
00103 int main() {
00104     heartbeat.attach(&beat,HEARTBEAT_DELAY); //Attaching the address of the function beat() and the interval HEATTBEAT_DELAY
00105     idx_buffer=0;
00106     
00107     //Testing if Flash IAP will break after sleep 
00108     ThisThread::sleep_for(SLEEP_DELAY);
00109     
00110     init_flash();
00111     get_flash_data(&last_value);    
00112     FLASH_PRINTF("Reading last value using Flash IAP: %c",last_value);
00113     
00114     while(1) {
00115         if(pc.readable())
00116         {
00117             buffer[idx_buffer] = pc.getc(); //Saving the character pressed in the rcv_buffer
00118             if(buffer[idx_buffer] == '\r'){ //If the key is "ENTER"
00119                 message_completed(); //Calling message treatment function
00120                 idx_buffer = 0;
00121             }
00122             else
00123                 idx_buffer++; //Increment the index variable
00124         }
00125         ThisThread::sleep_for(SLEEP_DELAY);
00126     }
00127 }
00128 
00129 void write_flash(char *buffer,int){
00130     
00131     
00132 }
00133 
00134 void init_flash(){
00135     char aux;
00136     
00137     flash.init();
00138     
00139     FLASH_PRINTF("\r\n");
00140     FLASH_PRINTF("--------------------Flash IAP Test - LPC1768 512kB--------------------");
00141     flash_size = flash.get_flash_size();
00142     FLASH_PRINTF("Flash Size: 0x%.8x",flash_size);
00143     
00144     addr = flash.get_flash_start();
00145     FLASH_PRINTF("Flash Start: 0x%.8x",addr);
00146     
00147     page_size = flash.get_page_size();
00148     FLASH_PRINTF("Page Size: 0x%.8x",page_size);
00149     
00150     FLASH_PRINTF("\r\n");
00151     FLASH_PRINTF("--------------------Flash Memory Mapping--------------------");
00152     FLASH_PRINTF("Start Address\tSector Size");
00153     do{
00154         sector_size = flash.get_sector_size(addr);
00155         FLASH_PRINTF("0x%.8x\t0x%.8x",addr,sector_size);
00156         addr = addr + sector_size;
00157         
00158     }while(addr < flash_size);
00159     
00160     FLASH_PRINTF("------------------------------------------------------------");
00161     FLASH_PRINTF("\r\n");
00162     
00163     erased_byte = flash.get_erase_value();
00164     FLASH_PRINTF("Erased Byte Value: 0x%.2X",erased_byte);
00165 }
00166 
00167 void get_flash_data(char* buffer){
00168     char aux;
00169     int i,j;
00170     active_addr=0;
00171     for(i=0;i<USED_BLOCKS && active_addr==0;i++){
00172         ret = flash.read(&aux,FIRST_BLOCK+i*BLOCK_SIZE,1);
00173         FLASH_PRINTF("Returned %d  - Reading 0x%0.8X: 0x%02X\t%c",ret,FIRST_BLOCK+i*BLOCK_SIZE,aux,aux);
00174         if(aux==erased_byte)
00175             FLASH_PRINTF("block %d is new",i);
00176         else{
00177            ret = flash.read(&aux,FIRST_BLOCK+(i+1)*BLOCK_SIZE-page_size,1); 
00178            if(aux==erased_byte){
00179               FLASH_PRINTF("block %d is not full",i);
00180               active_block = i;
00181               for(j=0;j<BLOCK_SIZE-page_size;j=j+page_size){
00182                  ret = flash.read(&aux,FIRST_BLOCK+i*BLOCK_SIZE+j,1);
00183                  FLASH_PRINTF("Read Address 0x%.8x: %.02X",FIRST_BLOCK+i*BLOCK_SIZE+j,aux);
00184                  if(aux==FLAG){
00185                     FLASH_PRINTF("Block %d Address: 0x%.8x is used",i,FIRST_BLOCK+i*BLOCK_SIZE+j);
00186                  } 
00187                  else{
00188                     active_block = i;
00189                     active_addr = FIRST_BLOCK+active_block*BLOCK_SIZE+j;
00190                     ret = flash.read(buffer,active_addr-page_size+1,DATA_LENGTH);
00191                     FLASH_PRINTF("Block %d Address: 0x%.8x Returned %d  - Reading last value using Flash IAP: 0x%02X\t%c",i,active_addr-page_size,ret,last_value,last_value);
00192                     break;
00193                 }   
00194               }
00195            } 
00196            else{
00197               FLASH_PRINTF("block %d is full",i);
00198             
00199            } 
00200         }
00201     }
00202     if(active_addr==0){
00203         active_block = 0;
00204         active_addr = FIRST_BLOCK+active_block*BLOCK_SIZE;
00205         FLASH_PRINTF("All Blocks are new: using block %d at address 0x%.8x",active_block,active_addr);        
00206     }  
00207 }
00208 
00209 void save_flash_data(char* buffer){
00210     char aux;
00211     int i;
00212     aux = FLAG;
00213     flash_buffer[0] = FLAG;
00214     for(i=0;i<DATA_LENGTH;i++)
00215         flash_buffer[i+1]=buffer[i];
00216         
00217     if(active_addr+page_size<FIRST_BLOCK+(active_block+1)*BLOCK_SIZE-1){ //If current block is not full
00218         ret = flash.program(flash_buffer,active_addr,DATA_LENGTH+1); 
00219         FLASH_PRINTF("Returned %d  - Saving Data to address 0x%0.8X",ret,active_addr+1); 
00220         active_addr = active_addr+page_size;
00221         FLASH_PRINTF("Saving new data to bank %d and address 0x%0.8X",active_block,active_addr);
00222     }
00223     else{ //If current block is full
00224         FLASH_PRINTF("block %d is full",active_block);
00225         int new_bank;
00226         new_bank = (active_block+1)%USED_BLOCKS;                                    //Next Flash Bank
00227         FLASH_PRINTF("erasing block %d",new_bank);
00228         ret = flash.erase(FIRST_BLOCK+new_bank*BLOCK_SIZE,sector_size);             //Erase new bank    
00229         ret = flash.program(flash_buffer,FIRST_BLOCK+new_bank*BLOCK_SIZE,DATA_LENGTH+1); 
00230         FLASH_PRINTF("Saving new data to bank %d and address 0x%0.8X",new_bank,FIRST_BLOCK+new_bank*BLOCK_SIZE);
00231         
00232         FLASH_PRINTF("Programming old block %d as obsolete",active_block);
00233         ret = flash.program(&aux,FIRST_BLOCK+(active_block+1)*BLOCK_SIZE-page_size,1); 
00234         FLASH_PRINTF("Returned %d  - Saving Data to address 0x%0.8X: 0x%02X",ret,FIRST_BLOCK+(active_block+1)*BLOCK_SIZE-page_size,aux); 
00235         active_addr = FIRST_BLOCK+new_bank*BLOCK_SIZE+page_size;  
00236         active_block = new_bank; 
00237     }  
00238 }
00239 
00240 
00241 void message_completed()
00242 {
00243     msg_completed = !msg_completed;
00244     ret = flash.read(&last_value,active_addr-page_size+1,DATA_LENGTH);
00245     FLASH_PRINTF("Returned %d  - Reading last value using Flash IAP: 0x%02X\t%c",ret,last_value,last_value);
00246     save_flash_data(buffer);
00247     FLASH_PRINTF("Saving %c using flash IAP",buffer[0]); 
00248     ret = flash.read(&last_value,active_addr-page_size+1,DATA_LENGTH);
00249     FLASH_PRINTF("Returned %d  - Reading last value using Flash IAP: 0x%02X\t%c",ret,last_value,last_value);
00250 }
00251 
00252 void beat()
00253 {
00254     heart = !heart; //Flip the LED 1
00255 }
00256