cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F1XX HAL_FLASH_Program dont support 8bytes? or go 16bytes space waste.

Nicolas Felipe
Associate II
Posted on June 26, 2017 at 04:00

Hi, im trying to program (8byte) 500 lenght buffer into Internal an STM32F103xx microcontroller, i know how to write but

char receive_data[500]; / Char Belongs to 8bytes

but in STM32F1xx_hal_flash.h

#define FLASH_TYPEPROGRAM_HALFWORD ((uint32_t)0x01) /*!<Program a half-word (16-bit) at a specified address.*/

#define FLASH_TYPEPROGRAM_WORD ((uint32_t)0x02) /*!<Program a word (32-bit) at a specified address.*/

#define FLASH_TYPEPROGRAM_DOUBLEWORD ((uint32_t)0x03) /*!<Program a double word (64-bit) at a specified 

and 8bytes?

in other series like STM32F4XX we can find 8bytes support.

if

(TypeProgram ==

http://www.disca.upv.es/aperles/arm_cortex_m3/llibre/st/STM32F439xx_User_Manual/group__flash__type__program.html#gac975d7139325057ed0069c6b55e4faed

)00190     {00191      

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

00192      

http://www.disca.upv.es/aperles/arm_cortex_m3/llibre/st/STM32F439xx_User_Manual/group__flash__exported__functions.html#ga1cf2729cad60da1faeba294904235b02

(Address, (uint8_t) Data);00193     }

and we have the FLASH_TYPEPROGRAM_BYTE.

i was tempted to addit to this STM32F1XX function but dont look any 8byte friendly with the 

nbiterations var.

if(status == HAL_OK)

{

if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)

{

/* Program halfword (16-bit) at a specified address. */

nbiterations = 1;

}

else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)

{

/* Program word (32-bit = 2*16-bit) at a specified address. */

nbiterations = 2;

}

else

{

/* Program double word (64-bit = 4*16-bit) at a specified address. */

nbiterations = 4;

}

for (index = 0; index < nbiterations; index++)

{

FLASH_Program_HalfWord((Address + (2*index)), (uint16_t)(Data >> (16*index)));

#if defined(FLASH_BANK2_END)

if(Address <= FLASH_BANK1_END)

{

#endif /* FLASH_BANK2_END */

/* Wait for last operation to be completed */

status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

/* If the program operation is completed, disable the PG Bit */

CLEAR_BIT(FLASH->CR, FLASH_CR_PG);

#if defined(FLASH_BANK2_END)

}

for example im gonna program : 

char strJsonExtracted[] = '{\'set\':\'mf\',\'d\':\'2\',\'m\':\'22\',\'a\':\'17\',\'h\':\'18\',\'s\':\'45\'}';

(8  bytes buffer).

if the 

FLASH_TYPEPROGRAM_HALFWORD is used i guess lot of space will get wasted because will se 16bytes when needed is just 8bytes and

, am i wrong? or this would be the best option? thanks.
3 REPLIES 3
misagh
Associate III
Posted on June 26, 2017 at 10:28

Hi  Nicolas,

I think you have a simple choice to use your memory more efficiently: making your data to 16-bit data, which contains two cells of your array, and write it to the flash memory. This is a simple example:

char strJsonExtracted[] = '{\'set\':\'mf\',\'d\':\'2\',\'m\':\'22\',\'a\':\'17\',\'h\':\'18\',\'s\':\'45\'}';

uint16_t* dataPointer;

dataPointer = (uint16_t*)strJsonExtracted;

for(index = 0; index < sizeof(strJsonExtracted)/2; index++){

   FLASH_Program_HalfWord((Address + (2*index)), *dataPointer);

}

Hope it would be help.

Bests,

Misagh

Posted on June 27, 2017 at 04:49

thank you, great difference. now half size and efficiency in every sector.

Posted on June 27, 2017 at 08:44

In the case where you have even number of bytes your approach works ok, but when you have odd number of bytes you may get into trouble with this approach because odd number divided by 2 is not integer and then you loose 1 written byte.

I would rewrite your code to:

uint16_t* dataPointer;
size_t length;
dataPointer = (uint16_t *)strJsonExtracted;
length = sizeof(strJsonExtracted)/2;
//Write all bytes as 16-bit
for(index = 0; index < length; index++){
 FLASH_Program_HalfWord((Address + (2 * index)), *dataPointer);
}
//Write last byte too
if (sizeof(strJsonExtracted) % 2) {
 uint16_t value = strJsonExtracted[sizeof(strJsonExtracted) - 1]; //Get last byte value
 FLASH_Program_HalfWord((Address + (2 * index)), &value); //Write this last value to flash
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?