cancel
Showing results for 
Search instead for 
Did you mean: 

How to do a byte program on my STM32L4R5 platform ?

fema
Associate III
Posted on April 20, 2018 at 05:19

I didn't find a way to do a byte program on my STM32L4 , there is a doubleword program exist in the stm32l4xx_hal_flash.c file.

How can i do that , read the doubleword out first and modify one byte and do a doubleword program .

Is it correct ?

thanks 

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on April 20, 2018 at 13:54

>>So the flash is not suitable for the data storage purpose.

Well not low bandwidth byte streams. There you'd need to record 64-bit for each byte you want to store. Here you could use two blocks one where you record the bytes, and when you fill that with the sparse data you pack into the second using 1/8th the space.

The more practical method for configuration data is to have a structure, cache that to RAM, and then update as a whole to FLASH as required.

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

View solution in original post

5 REPLIES 5
fema
Associate III
Posted on April 20, 2018 at 05:35

I add two more program cases word and byte as below :

if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)

{

/* Program double-word (64-bit) at a specified address */

FLASH_Program_DoubleWord(Address, Data);

prog_bit = FLASH_CR_PG;

}

if(TypeProgram == FLASH_TYPEPROGRAM_WORD) //  i add

{

/* Program word (32-bit) at a specified address */

FLASH_Program_Word(Address, Data);

prog_bit = FLASH_CR_PG;

}

if(TypeProgram == FLASH_TYPEPROGRAM_BYTE) //  i add

{

/* Program byte (8-bit) at a specified address */

FLASH_Program_Byte(Address, Data);

prog_bit = FLASH_CR_PG;

}

functions like as below :

static void FLASH_Program_Word(uint32_t Address, uint32_t Data)

{

/* Check the parameters */

assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));

/* Set PG bit */

SET_BIT(FLASH->CR, FLASH_CR_PG);

/* Program the word */

*(__IO uint32_t*)Address = (uint32_t)Data;

}

static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)

{

/* Check the parameters */

assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));

/* Set PG bit */

SET_BIT(FLASH->CR, FLASH_CR_PG);

/* Program the byte */

*(__IO uint32_t*)Address = (uint8_t)Data;

}

Is it correct ?

thanks 

fema
Associate III
Posted on April 20, 2018 at 08:31

Using the code i post ,  I found the byte or word program does not work .

Only the

doubleword program work , Does the STM32L4 does not support the other program ?

thanks 

AvaTar
Lead
Posted on April 20, 2018 at 08:46

Set the unused bytes, i.e. the bytes of the word you do not want to program, to 0xFF.

No Cortex M platform I know supports byte-wise programming for the internal Flash.

fema
Associate III
Posted on April 20, 2018 at 13:36

Oh !! No .

I got a program error return. Since the data before i program is not 0xFF .

According to user manual written , the Flash status register bit 'PROGERR' which mean as below :

Set by hardware when a double-word address to be programmed contains a value different from 0xFFFF FFFF before program.

It looks like the data i program need to follow the below two conditions:

1. Must be program in double word alignment .

2. Data can't contain other value than 0xFFFF FFFF before i program.

So the flash is not suitable for the data storage purpose.

I think i need to read/write the data to SRAM and find a good timing to store them to the flash before device turn off.

Posted on April 20, 2018 at 13:54

>>So the flash is not suitable for the data storage purpose.

Well not low bandwidth byte streams. There you'd need to record 64-bit for each byte you want to store. Here you could use two blocks one where you record the bytes, and when you fill that with the sparse data you pack into the second using 1/8th the space.

The more practical method for configuration data is to have a structure, cache that to RAM, and then update as a whole to FLASH as required.

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