FRTOS crashes when writing to internal flash

hudson2009 wrote on Monday, February 23, 2009:

Using the internal flash of sam7x as to log data.

When running in debugging mode with Jtag connected, the code Run (alternately -> didn’t run 1st time and after reset it will run and redet again it will stop running and so on).

Also it will not run in release mode(no debugger attached).

following are my Flash command code and it is in .fast section.

Any one can give me some hint how to get the Internal flash working properly in FreeRTOS.

Many Thanks!

//----------------------------------------------------------------------------

e_FLASH_Result Flash_Command(e_FLASH_Cmd cmd, const int page)
{

    e_FLASH_Result ret_val = FLASH_FAIL;
    unsigned int fcr_val = 0x0; //Value to be placed in MC_FCR.
    unsigned int fsr_val; //FSR reg content.
    AT91S_MC *pEfc; //pointer to EFC.
   
    pEfc = AT91C_BASE_MC; // Base address of EFC0 registers.

portDISABLE_INTERRUPTS();
    switch(cmd)
    {
        case FLASH_LOCK_PAGE:    // Lock a page.
       
          fcr_val = (0x5A << 24) | (page << 8) | 0x2;
          pEfc->MC_FMR = (48 << 16) | AT91C_MC_FWS_3FWS;
          break;

        case FLASH_UNLOCK_PAGE:    // Unlock a page.

          fcr_val = (0x5A << 24) | (page << 8) | 0x4;
          pEfc->MC_FMR = (48 << 16) | AT91C_MC_FWS_3FWS;
          break;

        case FLASH_ERASE_PAGE:    // Erase a page of data - write all zeroes (not 0xff)!           
        case FLASH_WRITE_PAGE:    // Write data to a page (which should be unlocked already).
                               
          fcr_val = (0x5A << 24) | (page << 8) | 0x1;
          pEfc->MC_FMR = (72 << 16) | AT91C_MC_FWS_3FWS;
          break;
       
        default:
               
          return ret_val;
          break;
    }

    // Wait for flash to be ready.
    do
    {
        fsr_val = pEfc->MC_FSR;    
    }
    while ((fsr_val & AT91C_MC_FRDY) == 0);
   
    // Now send command.   
    pEfc->MC_FCR = fcr_val;
   
    // Wait for flash to be ready.
    do
    {
        fsr_val = pEfc->MC_FSR;
    }
    while ((fsr_val & AT91C_MC_FRDY) == 0);
   
    //Check for errors and return.
    if((fsr_val = pEfc->MC_FSR) & (AT91C_MC_PROGE | AT91C_MC_LOCKE))
    {
        return ret_val; // Error shown in PROGE or LOCKE bits.
    }   

portENABLE_INTERRUPTS();
    return FLASH_OK;
}
//---------------------------------------------------------------------------

bkastel1 wrote on Monday, February 23, 2009:

Hello,

i had a simillar problem with LPC2148 and the problem was becasue i didn’t disabled the IRQ’s - while writing to the FLASH i had to disable the IRQ’s because the IAP functions messed with the stack. Try this.

Best of luck,

Borut

mikej42 wrote on Tuesday, February 24, 2009:

Hello

I am using the swam7s family, which has a single plane of flash memory, and you cannot write to the flash while attempting to read it. The actual write must be run from RAM.

I think the 7x family has multiple planes, but you cannot write to the plane which you are readinh from I think.

The IAR toolset has the __ram_func qualifier which will carry out actual write function:

static __ramfunc unsigned int
writeCommand (unsigned int page)
{
   unsigned int status;
   while (!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY))
       /*EMPTY*/;

   AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;        // probably unecessary!

   AT91C_BASE_MC->MC_FCR = page | AT91C_MC_CORRECT_KEY;
   do
   {
      status = AT91C_BASE_MC->MC_FSR;
   }
   while (!(status & AT91C_MC_FRDY))
    /*EMPTY*/;
   return status;
}

Hope this helps

Mike N

hudson2009 wrote on Wednesday, February 25, 2009:

Hi Mike and Borut,

Thanks for the reply. However still no getting much Joy.

The Interrupt was disable and the WriteFlash function was in the RAM (.fast using Rowley CrossWorks).

It always crash in "dabort_handler:" 1st time round running with debugger and it will Run successfully if i resetting the debugging after crash(tried many time, the problem seem to be constant). but it never run without debugger.

Anyone have similar problem before or any help.

Thanks!

HS

hudson2009 wrote on Friday, February 27, 2009:

problem seem to be disappear by having the flash function in with thing above + in ARM mode.