Skip to main content
John3
Associate II
November 23, 2018
Question

stm32l4xx internal flash + data for the single byte program

  • November 23, 2018
  • 5 replies
  • 2055 views

Hi all,

I am using stm32l486 MCU and trying to use stmcube internal flash example code. As i see we have API "HAL_FLASH_Program" to program word or double word. Is it possible to write byte data by doing type casting and increasing the address by 1?

Could anyone help on this.

Thanks,

John

This topic has been closed for replies.

5 replies

AvaTar
Senior III
November 23, 2018

Flash usually has an unprogrammed value of 0xFF (per byte). So setting bytes of a word you do not want to program to 0xFF will do the job. The hardware supports only word or double word write, so type casting will not work.

If a Flash cell contains 0xFF, you can "program" it to 0xFF as often as you want. Once the value of a bit is 0, it can not be changed until you erase (usuall the whole block).

There <<are>> vendors that return 0x00 for unprogrammed internal Flash, and programming will set bits to 1. So reading datasheets is a good practice.

John3
John3Author
Associate II
November 27, 2018

Hi @Community member​ :

Thanks for the reply.

Can I combined the bytes and write as a word or double word as below:

data32 = wr_buff[3] | (wr_buff[2]<<8) | (wr_buff[1]<<16) | (wr_buff[0]<<24);

HAL_FLASH_Program(TYPEPROGRAM_WORD, Address, data32);

Thanks,

John

John3
John3Author
Associate II
November 27, 2018

Hi,

If I am not wrong I think stm32l4 series supports only double-word (64-bit) program. So can i combine as below:

data64 = wr_buff[7] | (wr_buff[6]<<8) | (wr_buff[5]<<16) | (wr_buff[4]<<24) | (wr_buff[3]<<32) | (wr_buff[2]<<40) | (wr_buff[1]<<48) | (wr_buff[0]<<56);

but the shift count is too large.

 HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, data64);

Tesla DeLorean
Guru
November 27, 2018

You have the byte order backward and would need to cast values to uint64_t before shifting.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
John3
John3Author
Associate II
November 27, 2018

Many thanks @Community member​ 

AvaTar
Senior III
November 27, 2018

I didn't work with the L4 yet.

Shifting is one way to combine the data. Not sure what you mean with "too large".

Another option would be a union, laying a 46 bit variable and a byte array "on top" of each other.

Tesla DeLorean
Guru
November 27, 2018

The way the ECC works it does not permit a single byte write, or write to unaligned memory addresses.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..