1

I'm trying to write some bytes of data at the specific region in the flash using HAL_FLASH_Program (uint32_t TypeProgram, uint32_t Address, uint32_t Data). But my current understanding of from HAL user manual at 19.2.7 UM1749 "Program word at a specified address" is unclear what does it mean? how can I write bytes e.g.: char* demo="nucleo" into memory.

   char* demo="nucleo";
   int main(){
   .
   .
   ScratchWrite(FLASH_SCRATCH_START, demo);
   .
   .
   }

    uint32_t ScratchWrite(uint32_t destination, uint32_t *p_source)
   {
   
     HAL_FLASH_Unlock();
   
       /* Write the buffer to the memory */
      HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,destination, p_source );
   
     HAL_FLASH_Lock();
   
     return status;
   }
1
  • that would be two words, 8 bytes so sure, do two writes. Commented Mar 29, 2018 at 20:05

1 Answer 1

5

HAL_FLASH_Program allows you to write given number of bytes under specified address. First function argument uint32_t TypeProgram controls how large the chunk you want to write is. The possibilites are:

#define FLASH_TYPEPROGRAM_BYTE        ((uint32_t)0x00U)  /*!< Program byte (8-bit) at a specified address           */
#define FLASH_TYPEPROGRAM_HALFWORD    ((uint32_t)0x01U)  /*!< Program a half-word (16-bit) at a specified address   */
#define FLASH_TYPEPROGRAM_WORD        ((uint32_t)0x02U)  /*!< Program a word (32-bit) at a specified address        */
#define FLASH_TYPEPROGRAM_DOUBLEWORD  ((uint32_t)0x03U)  /*!< Program a double word (64-bit) at a specified address */

So in case of your question, FLASH_TYPEPROGRAM_WORD writes 32-bit (4 byte) value under specified address. For example:

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x8010000, *(uint32_t*)demo);

will write "nucl" under address 0x8010000.

How large the chunks you want to program with HAL_FLASH_Program are is totally up to you and will affect performance if you choose small chunks (FLASH_TYPEPROGRAM_BYTE for example) when programming large portion of flash.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.