在微控制器中读取内部闪存

时间:2018-01-03 05:39:27

标签: c flash microcontroller

我正在尝试从东芝编写一个TZ系列微控制器,通过I2C从传感器收集数据,并将数据存储到内部NOR闪存,每秒都有时间戳。我在读取闪存中的值时遇到问题,闪存是来自传感器的数据。我的代码如下,

   while(1)
   {
     if (input == 'a')
     {

         read_sens();
         sram_area[0] = (int)value;
         vTaskDelay(1000);

       rtn = drv_info->WriteData(WRITE_ADDRESS, sram_area, sizeof(sram_area));
       print_sp(PREF"DATA WRITE rtn= %d data=0x%02x\r\n",rtn,*sram_area); 
         print_sp(PREF "MEM VALUE  0x%08x\r\n", (ptr+=2));
     }

     if (input == 'b')
     {
        rtn = drv_info->ReadData(WRITE_ADDRESS, hts_measurement1 , 1);

       print_sp(PREF "DATA READ  rtn=%d  %d  \r\n",rtn, hts_measurement1[0]);

      //print_sp(PREF "MEM VALUE  0x%08x\r\n", (ptr+=1));
      vTaskDelay(1000);
     }
   }

变量sram_areahts_measurement1都是全局数组,类型为uint8_t。 varialble Value是简单的传感器数据,它每秒都会继续更新,并且是float类型。变量rtn表示函数WriteData和ReadData的返回值(0或1),类型为uint8_t。如果我尝试使用write同时读取数据,那么我可以正确读取该值。但我们的想法是先将数据写入闪存,然后在需要时读取。我请求一些人帮我解决这个问题。

我正在使用免费的RTOS和东芝系列微控制器。

先谢谢。

此致 尼丁

1 个答案:

答案 0 :(得分:0)

我尝试过可能的实施,但由于我没有董事会,所以我没有在船上编制或验证。

#define WRITE_ADDRESS            0x0007fff8     // NOR Flash address 
#define DATA_SIZE                0x02
#Define NO_OF_SAMPLES            10             // Change depending on your requirement and FLASH Size
#define DEBUG_NOR                1              // Enabled for debug purpose

float temperature = 0.0;
ARM_NOR_STATUS rtn;
uint8_t *ptr = (uint8_t *)WRITE_ADDRESS;
int32_t temp_i;
uint8_t sram_area[DATA_SIZE]={0x00,0x00};


void tmp006_demo_task(void *pvParameters)
{
    char input;
    uint32_t i;
    unsigned short Temp =0;

    #ifdef NOR_USE_SDMAC
        Driver_SDMAC.Initialize();
        Driver_SDMAC.PowerControl(ARM_POWER_FULL);
    #endif

    rtn = drv_info->Initialize(0);

    rtn = drv_info->PowerControl(ARM_POWER_FULL);

    input = prompt_console("select :");

    if ((input >= 'A') && (input <= 'Z'))
    {
        input = input + ('a' - 'A');
    }
    while(1)
    {
        if (input == 'a')
        {
            ptr = (uint8_t *)WRITE_ADDRESS;
            for (i=0; i < NO_OF_SAMPLES;i++)
            {
                #ifdef (DEBUG_NOR)
                    sram_area[0]  =  i*20;  //Some random number for only debugging purpose
                    sram_area[1]  =  i*15;
                #else
                    read_temp();            //Probable implementation  17.25 value is stored as 1725 
                    Temp = (unsigned short)(temperature * 100);
                    sram_area[0] = 0xFF & Temp;
                    sram_area[1] = 0xFF & (Temp >> 8);

                #endif

                vTaskDelay(1000);

                rtn = drv_info->WriteData(ptr, sram_area, 2);       //I'm hardcoding the size to 2
                if (rtn != ARM_NOR_OK)
                {
                    print_sp(PREF "Writing to NOR Flash Failed \r\n"); 
                    break;
                }
                print_sp(PREF "DATA WRITE        rtn=%d  data=0x%x 0x%x \r\n",  rtn, sram_area[0], sram_area[1]); 

                ptr = ptr+2; //Increment the pointer  
                             //Am Assuming atleast you have 20bytes in Nor flash that can be used                                

            }

            input =0 ;  //Making sure again it is not entering into the loop

        }

        if (input == 'b')
        {
            ptr = (uint8_t *)WRITE_ADDRESS;
            for (i=0; i < NO_OF_SAMPLES;i++)
            {
                vTaskDelay(1000);
                rtn = drv_info->ReadData(ptr, sram_area , 2);
                if (rtn != ARM_NOR_OK)
                {
                    print_sp(PREF "Reading to NOR Flash Failed \r\n"); 
                    break;
                }
                print_sp(PREF "DATA Read        rtn=%d  data=0x%x 0x%x \r\n",  rtn, sram_area[0], sram_area[1]); 

                ptr = ptr+2;

            }
            input = 0;          
        }
    }
}