2017-06-25 07:00 PM
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 specifiedand 8bytes?
in other series like STM32F4XX we can find 8bytes support.
if
(TypeProgram == )00190 {00191/*Program byte (8-bit) at a specified address.*/
00192 (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.2017-06-26 01:28 AM
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
2017-06-26 09:49 PM
thank you, great difference. now half size and efficiency in every sector.
2017-06-27 01:44 AM
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
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?