cancel
Showing results for 
Search instead for 
Did you mean: 

ı try to internal flash codding with STM32L476 ı van erase the banks and pages but HAL_FLASH_Program İS NOT WORKİNG ı get the SR errors(SIZERR,PGSERR) so how can ı fix it

yekme
Associate

my code is,

  

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

#include "main.h"

#include "stm32l4xx_hal.h"

#define FLASH_BSY   (1 << 16)

/* Fast programming not used => related errors not used*/

#define FLASH_PGSERR  (1 << 7) /* Programming sequence error */

#define FLASH_SIZERR  (1 << 6) /* Size error */

#define FLASH_PGAERR  (1 << 5) /* Programming alignment error */

#define FLASH_WRPERR  (1 << 4) /* Write protection error */

#define FLASH_PROGERR (1 << 3) /* Programming error */

#define FLASH_OPERR  (1 << 1) /* Operation error */

#define FLASH_EOP   (1 << 0) /* End of operation */

  

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

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

/* USER CODE BEGIN PV */

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

uint32_t data;

uint32_t data_1 = 0x11111111;

uint32_t data_2 = 0x22222222;

uint32_t FLASH_ADDRESS;

uint32_t Flash_Data;

uint32_t PageError;

uint32_t i;

HAL_StatusTypeDef status;

uint32_t * adres_data;

uint32_t * deneme_data_adres = (uint32_t *)0x0807F802;

/* USER CODE END PV */

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

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

/* USER CODE BEGIN PFP */

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

void Flash_Write(uint32_t FLASH_PROGRAM_ADDRESS, uint32_t Flash_Data);

uint32_t Flash_Read(uint32_t FLASH_PROGRAM_ADDRESS);

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

uint32_t Flash_Read(uint32_t FLASH_ADDRESS)

{

uint32_t Flash_Data;

Flash_Data = *(uint32_t*) FLASH_ADDRESS;

return Flash_Data;

  

}

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 *

 * @retval None

 */

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 */

 FLASH_EraseInitTypeDef FLASH_EraseInitStructure ;

FLASH_EraseInitStructure.Banks = FLASH_BANK_1;

FLASH_EraseInitStructure.Page = 255;

FLASH_EraseInitStructure.TypeErase =FLASH_TYPEERASE_PAGES;

FLASH_EraseInitStructure.NbPages =1;

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 /* USER CODE BEGIN 2 */

FLASH_ADDRESS = 0x0807F802;

(*adres_data) = 0x1234;

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

//

//Flash_Data = *(uint32_t*)0x0807F802;

//i++;

//Flash_Data = Flash_Read(0x0807F801);

//i++;

//Flash_Data = *(uint32_t*)0x0807F801;

//i++;

//Flash_Data = *(uint32_t*)0x0807F803;

//i++;

//Flash_Data = *(uint32_t*)0x0807F804;

//i++;

//Flash_Data = *(uint32_t*)0x0807F805;

 while (1)

 {

 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */

status = HAL_FLASH_Unlock();

deneme_data_adres[0] = data_1;

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGAERR | FLASH_FLAG_WRPERR | FLASH_FLAG_BSY | FLASH_FLAG_OPTVERR );   // Clear of every FLASH flag

 status = HAL_FLASHEx_Erase(&FLASH_EraseInitStructure,&PageError);

status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x0807F802,((uint64_t)data_1 << 32) | data_2);

HAL_FLASH_Lock();

 }

 /* USER CODE END 3 */

}

/**

 * @brief System Clock Configuration

 * @retval None

 */

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct;

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

  /**Initializes the CPU, AHB and APB busses clocks 

  */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = 16;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Initializes the CPU, AHB and APB busses clocks 

  */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure the main internal regulator output voltage 

  */

 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure the Systick interrupt time 

  */

 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  /**Configure the Systick 

  */

 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 /* SysTick_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/** Configure pins as 

    * Analog 

    * Input 

    * Output

    * EVENT_OUT

    * EXTI

*/

static void MX_GPIO_Init(void)

{

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOH_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

void Flash_Write(uint32_t FLASH_PROGRAM_ADDRESS, uint32_t Flash_Data)

{

//uint32_t err;

// HAL_FLASH_Unlock();

//HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&FLASH_EraseInitStructure, &err);

//if(err != 0){}

}

/* USER CODE END 4 */

/**

 * @brief This function is executed in case of error occurrence.

 * @param file: The file name as string.

 * @param line: The line in file as a number.

 * @retval None

 */

void _Error_Handler(char *file, int line)

{

 /* USER CODE BEGIN */

 /* User can add his own implementation to report the HAL error return state */

 while(1)

 {

 }

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *     where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(uint8_t* file, uint32_t line)

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

   tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/**

 * @}

 */

/**

 * @}

 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

2 REPLIES 2
Tomas DRESLER
Senior II

Don't you miscalculate page number? Btw. check return codes, on FLASH operations they are critical.

Try the demo in CubeMx repository:

STM32Cube\Repository\STM32Cube_FW_L4_V1.13.0RC3\Projects\NUCLEO-L476RG\Examples\FLASH\FLASH_EraseProgram\

status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,0x0807F802,((uint64_t)data_1 << 32) | data_2);

You can't write to unaligned addresses, writing 64-bit, be 8-byte aligned, for 32-bit be 4-byte aligned.

0x0807F802 clearly not suitable for 32-bit or 64-bit access

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