Howto saving Data in Flash in Cortex M3/M4

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Location: South Korea
Has thanked: 3 times
Been thanked: 3 times

Re: Howto saving Data in Flash in Cortex M3/M4

Postby DeusExMachina » Wed Feb 27, 2013 12:02 pm

Hello,
I successfully used MABL code to write data to a flash page (Chibios 2.4.3)
But then I try to use this code in multithread app or when exist ISRs from peripheral devices - I get "unhandled exception". How can I save data to flash in multithread application with timer and UART ISRs?
Thanks

Code: Select all

   
        gptStart(&GPTD2, &gpt2cfg);
   gptPolledDelay(&GPTD2, 10); /* Small delay.*/
   gptStart(&GPTD3, &gpt3cfg);
   gptPolledDelay(&GPTD3, 10); /* Small delay.*/

//   Save2Flash(); // <- here data is stored to flash without exception

   gptStartContinuous(&GPTD2, 1000);
   gptStartContinuous(&GPTD3, 100);

   Save2Flash(); // <-unhandled exception here. Data is stored to flash, but MCU hangs


Save2flash() use MABL library for P107 bootloader. How can I save data "on the fly" to flash?

theShed
Posts: 50
Joined: Tue Feb 26, 2013 3:43 pm
Location: The flatlands of East Anglia

Re: Howto saving Data in Flash in Cortex M3/M4

Postby theShed » Wed Feb 27, 2013 1:59 pm

We'll start with the simple answer - It is *bad* practice to write/erase flash with interrupts enabled.

A flash array is not able to be read when it is being programmed or erased, that is *no* part of the array can be read, not just the word/sector in question.
In the old days, when external flash was the norm, this meant that the flash programming code had to be executed from RAM since the flash device would output a status word whilst in a programming or erase state.
Parts like the STM32 with an internal flash array and the flash controller attached to a syncronous bus do not have this restriction, instead the bus can be stalled during a program or erase operation. Of course this does mean that no instructions can be executed during this period so interrupts might as well be disabled as they cannot be serviced.

Erasing a flash block is also a very slow operation, IIRC the array used in the STM32F1xx takes about 40mS to erase a page. Given this large stall I suspect that the problem you are seeing is an overflow of some sort due to interrupts not being processed in a timely manner.

Probably not the answer you wanted but I hope it explains why it is happening.

As an aside, I don't know your application, how much data you want to write or how frequently it need to change but the flash core in the STM32 has two separate arrays: the main program array and a small info block. Part of the small info block is available for user data storage. You do need to be very careful how you manage this block though as it contains the 'read-protect' flag and inadvertant set/clear of this flag can trigger a mass erase of the main flash area.


--
mike

User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Location: South Korea
Has thanked: 3 times
Been thanked: 3 times

Re: Howto saving Data in Flash in Cortex M3/M4

Postby DeusExMachina » Wed Feb 27, 2013 3:05 pm

theShed wrote:We'll start with the simple answer - It is *bad* practice to write/erase flash with interrupts enabled.
...
mike

Thank you for the advance. I understand this issues. I have to save 12 16bit values. More distinct question is: how can I stop/pause chibios threads and disable interrupts for a writing time? Is it possible?

theShed
Posts: 50
Joined: Tue Feb 26, 2013 3:43 pm
Location: The flatlands of East Anglia

Re: Howto saving Data in Flash in Cortex M3/M4

Postby theShed » Wed Feb 27, 2013 3:37 pm

I would use chSysDisable() / chSysEnable().
I'm sure Giovanni will correct me if I'm wrong here.....

--
mike

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Howto saving Data in Flash in Cortex M3/M4

Postby Giovanni » Wed Feb 27, 2013 5:07 pm

You can disable interrupts like Mike suggested, of course you will get a hit on response time while you do so, the system is able to recover just fine but make sure you don't have critical tasks ongoing. Flash operations are in the order of several milliseconds, the RTOS normally has response times in the order of microseconds.

Giovanni

User avatar
DeusExMachina
Posts: 223
Joined: Tue Apr 03, 2012 5:08 am
Location: South Korea
Has thanked: 3 times
Been thanked: 3 times

Re: Howto saving Data in Flash in Cortex M3/M4

Postby DeusExMachina » Thu Feb 28, 2013 4:19 am

I successfully saved my variables to flash. I used the following sequence:

Code: Select all

if (i_have_to_do_this)
      {
//stopping devices
         uartStop(&UARTD1);
         gptStop(&GPTD2);
         gptStop(&GPTD3);
// terminating threads
         chThdTerminate(t1);     /* Requesting termination.                  */
         chThdTerminate(t2);     /* Requesting termination.                  */
         chThdTerminate(t3);     /* Requesting termination.                  */
//disabling interrupts
         chSysDisable();
         /* Now the main function is again a normal function, no more a
                OS thread.*/
         Save2Flash();
         chSysHalt(); // waiting for watchdog here...
      }


Return to “General Support”

Who is online

Users browsing this forum: Google [Bot] and 47 guests