Skip to main content
Explorer
September 18, 2024
Question

unable to write in flash memory.

  • September 18, 2024
  • 3 replies
  • 996 views

Hi,

i am using STM32G0C1RET6 MCU board, i am unable to write in flash memory. i am using an 8 byte structure to put data at that location. but I don't know what is going wrong. please, help... I have given some part of the code below,

 

static FLASH_EraseInitTypeDef EraseInitStruct;

uint32_t PAGError;

/* Unlock the Flash to enable the flash control register access *************/

HAL_FLASH_Unlock();

/* Erase the user Flash area */



/* Get the number of pages to erase */



//uint32_t StartPage = StartPageAddress;

//uint32_t EndPageAddress = 0x0801FFFF;

//uint8_t NbPages = 1; /*!< Number of pages to be erased.



/* Fill EraseInit structure*/

EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

EraseInitStruct.Banks = FLASH_CR_MER2;

EraseInitStruct.Page = 383;

EraseInitStruct.NbPages = 1;



if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGError) != HAL_OK) {

DEBUG_PR("PAGE ERASE FAILED\r\n");

return HAL_FLASH_GetError ();

} else {

DEBUG_PR("PAGE ERASED\r\n");

}



if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, StartPageAddress, gt_s.pan_motor) == HAL_OK) {

DEBUG_PR("DATA WROTE in FLASH MEMORY");

} else {

DEBUG_PR("FAILED TO WRITE IN FLASH\r\n");

return HAL_FLASH_GetError ();

}

HAL_FLASH_Lock();

return 0;
    This topic has been closed for replies.

    3 replies

    Technical Moderator
    September 18, 2024

    Hello @Rohit007 ,

    Please kindly use </> button to paste your code. I'm editing your post then ..

    Please review our recommendations on posting in this community.

    Thank you for your understanding.

    Graduate II
    September 18, 2024

    Does it report an error?

    You have it instrumented what does it output?

    Does it erase the memory?

    What's the address?

    In presenting the problem clearly you might be able to understand the issue yourself.

    Break it down explain your observations..

    Rohit007Author
    Explorer
    September 18, 2024

    @Tesla DeLorean  yes it reports error on writing in flash memory, it is successfully erasing the page an address is of last page.

     

    Graduate II
    September 18, 2024

    What error does the HAL function actually report.

    What IS the address?

    Is it correctly aligned with the expectation of the FLASH array and write line widths?

    Be specific, not unduly vague, it's the details that matter

     

    Rohit007Author
    Explorer
    September 24, 2024

    @Tesla DeLorean i am getting HAL_error from FLASH_WaitForLastOperation function.