Skip to main content
SDhka.1
Associate II
March 17, 2023
Question

can any one tell me why its only showing 23 in memory address and rest of its not showing 24,25,25

  • March 17, 2023
  • 3 replies
  • 1426 views

* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32l5xx_hal.h"

#define FLASH_SECTOR2_BASE_ADDRESS 0x08040000

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

uint32_t addressflash =0x08040000;

//uint8_t Data=0x24;

uint32_t Banks =FLASH_BANK_2;

uint8_t Data[4]={0x23,0x24,0x25,0x25};

uint8_t i;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void){

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 /* USER CODE BEGIN 2 */

// HAL_FLASH_Unlock();

// FLASH_PageErase(addressflash,Banks);

//

// HAL_FLASH_Lock();

  HAL_FLASH_Unlock();

for(i=0;i<=3;i++)

 

  HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addressflash,Data[i]);

addressflash++;

HAL_FLASH_Lock();

 

 

 

 

 

 

 

 

 

 /* USER CODE END 2 */

This topic has been closed for replies.

3 replies

Bubbles
ST Employee
March 20, 2023

Hi @SDhka.1​,

you are calling doubleword programming method. The data parameter must be 64bit, but in one go, not in loop by 8 bits. Look at the FLASH_EraseProgram example from intoduction package repository to see how you can pass whole 64 in single call.

BR,

J

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Tesla DeLorean
Guru
March 20, 2023

64-bit double words will require you advance the address variable by 8-bytes, not 1

The width of the FLASH line is 64-bit, you get a single shot to write it.

You could cast a 64-bit data pointer to you Data array, but you'd end up writing those 4 bytes, followed by 4 bytes beyond that in memory.

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

You'd want to use a compound statement

for(i=0;i<=3;i++)
{
 HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addressflash,Data[i]);
 addressflash += 8; // 64-bit advanced, and aligned address
}

Or

HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,addressflash, *((uint64_t *)Data) );

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
SDhka.1
SDhka.1Author
Associate II
March 21, 2023

But i want read write erase again and again . in the above program its not happing. He Tesla DeLorean can you help me in this program... how did i do read write erase again and again. , what should i do change in my code.

Tesla DeLorean
Guru
March 21, 2023

STM32Cube_FW_L5_V1.2.0\Projects\NUCLEO-L552ZE-Q\Examples\FLASH\FLASH_EraseProgram

Perhaps something materially similar to this, which erases and writes a block of data

https://community.st.com/s/question/0D53W00002Bfv6BSAR/writing-to-internal-flash-stm32l422kbt6

Constantly erasing and writing a small amount of content is not recommended. Might want to look at journalling the write across the entire page before erasing.

Perhaps having a checksum/crc on critical data, and the ability to pick usable default values in the initial case, or when data is corrupt / unreadable.

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