cancel
Showing results for 
Search instead for 
Did you mean: 

stm32l4xx internal flash + data for the single byte program

John3
Associate II

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

7 REPLIES 7
AvaTar
Lead

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
Associate II

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
Associate II

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);

AvaTar
Lead

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.

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

Many thanks @Community member​