AnsweredAssumed Answered

Hard Fault Error - STM32F100C8

Question asked by shankar_k.gowri on Jun 27, 2011
Latest reply on Jun 28, 2011 by Clive One
I am trying to implement EEPROM emulation in the flash memory. I am able to write and read the data from flash. When trying to write the second data it is going to HARDFAULT Error. I m not sure why this happens.

Below is the code....

void Write_Data(uint32_t Address,uint16_t *Data,uint16_t Len)
{
    uint8_t Temp_RdLen=0;
    uint16_t i=0;
    uint16_t RdLen=0;
       
    FLASH_UnlockBank1();
    Flash Bank1 Program Erase controller */

    NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR)   
                                 /FLASH_PAGE_SIZE;  

    FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR |
                                          FLASH_FLAG_WRPRTERR);

        for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus ==
                                                      FLASH_COMPLETE); EraseCounter++)
        {
          FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR +
                                                               (FLASH_PAGE_SIZE * EraseCounter));
        } 
       
    while(Address < BANK1_WRITE_END_ADDR&& MemoryProgramStatus !=
                                                                                                         FAILED)
    {
        RdLen = (*(__IO uint32_t*)Address);
        Temp_RdLen=(uint8_t)(RdLen>>8);
        break;
    }
   
    if(Temp_RdLen == 0xFF)
    {
        while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus ==
                                                               FLASH_COMPLETE))
        {
            FLASHStatus = FLASH_ProgramHalfWord(Address, Len);
            Address+=2;
            break;
        }
       
        while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus ==
                            FLASH_COMPLETE)&& i<Len)           
        {
            FLASHStatus = FLASH_ProgramHalfWord(Address, *Data);
            Address+=2;
            Data+=1;
            if(Len-i>=2)
                i+=2;
            else if(Len-i==1)
                i+=1;
            else if(Len-i==0)
                break;                       
        }
    }
    else
    {

    }
    FLASH_LockBank1();
}

1. Before writing the data i m checking whether the data in the address 0x0800F000 is 0xFF. If so then i proceed to next step.

2. At this address - 0x0800F000 the string length is written and from 0x0800F002 the original data is written.

3. I m able to read back the data from this address also.

4. Next i m checking that whether  the data in the address 0x0800F025  is 0xFF.

5. If yes, then i m trying to write the next string length.

6. But at this point, the control goes to HARDFAULT_ERROR.

I m not sure where is the original trouble is..

Outcomes