cancel
Showing results for 
Search instead for 
Did you mean: 

i can't erase flash memory of STM32C0116DK

KenLee
Associate III

i'm using stm32c011f4u6 mcu's DK(stm32c0116dk), and i generate the file as mdk-arm and it's running with keil5.

i tried erase to test the flash memory, but it doesn't work. i even used the SDK example file, and i get the same error.

 

Below is the main.c file.

 

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file FLASH/FLASH_EraseProgram/Src/main.c
* @author MCD Application Team
* @brief This example provides a description of how to erase and program the
* STM32C0xx FLASH.
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* 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 */
#define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_5 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR (ADDR_FLASH_PAGE_1 + FLASH_PAGE_SIZE - 1) /* End @ of user Flash area */

#define DATA_32 ((uint32_t)0x12345678)
#define DATA_64 ((uint64_t)0x1234567812345678)

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

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

/* USER CODE BEGIN PV */
uint32_t FirstPage = 0, NbOfPages = 0;
uint32_t Address = 0, PageError = 0;
__IO uint32_t MemoryProgramStatus = 0;
__IO uint32_t data32 = 0;

/*Variable used for Erase procedure*/
static FLASH_EraseInitTypeDef EraseInitStruct;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
static uint32_t GetPage(uint32_t Address);

/* 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 */
/* STM32C0xx HAL library initialization:
- Configure the Flash prefetch
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Low Level Initialization
*/

/* USER CODE END 1 */

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

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */
/* Configure the system clock to 48 MHz */
/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
/* USER CODE BEGIN 2 */
/* Initialize LED3 */
BSP_LED_Init(LED3);
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();

/* Clear OPTVERR bit set on virgin samples */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

/* Erase the user Flash area
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

/* Get the 1st page to erase */
FirstPage = GetPage(FLASH_USER_START_ADDR);

/* Get the number of pages to erase from 1st page */
NbOfPages = GetPage(FLASH_USER_END_ADDR) - FirstPage + 1;

/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Page = FirstPage;
EraseInitStruct.NbPages = NbOfPages;

/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
you have to make sure that these data are rewritten before they are accessed during code
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
DCRST and ICRST bits in the FLASH_CR register. */
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) (error ocurred function)
{
/*
Error occurred while page erase.
User can add here some code to deal with this error.
PageError will contain the faulty page and then to know the code error on this page,
user can call function 'HAL_FLASH_GetError()'
*/
/* Infinite loop */
while (1)
{
/* Make LED3 blink (100ms on, 2s off) to indicate error in Erase operation */
BSP_LED_On(LED3);
HAL_Delay(100);
BSP_LED_Off(LED3);
HAL_Delay(2000);
}
}

/* Program the user Flash area word by word
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

Address = FLASH_USER_START_ADDR;

while (Address < FLASH_USER_END_ADDR)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
{
Address = Address + 8; /* increment to next double word*/
}
else
{
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
while (1)
{
/* Make LED3 blink (100ms on, 2s off) to indicate error in Write operation */
BSP_LED_On(LED3);
HAL_Delay(100);
BSP_LED_Off(LED3);
HAL_Delay(2000);
}
}
}

/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();

/* Check if the programmed data is OK
MemoryProgramStatus = 0: data programmed correctly
MemoryProgramStatus != 0: number of words not programmed correctly ******/
Address = FLASH_USER_START_ADDR;
MemoryProgramStatus = 0x0;

while (Address < FLASH_USER_END_ADDR)
{
data32 = *(__IO uint32_t *)Address;

if (data32 != DATA_32)
{
MemoryProgramStatus++;
}
Address = Address + 4;
}

/*Check if there is an issue to program data*/
if (MemoryProgramStatus == 0)
{
/* No error detected. Switch on LED3*/
BSP_LED_On(LED3);
}
else
{
/* Error detected. LED3 will blink with 1s period */
while (1)
{
BSP_LED_On(LED3);
HAL_Delay(500);
BSP_LED_Off(LED3);
HAL_Delay(500);
}
}

/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */
}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}

/* USER CODE BEGIN 4 */

 

/**
* @brief Gets the page of a given address
* @PAram Addr: Address of the FLASH Memory
* @retval The page of a given address
*/
static uint32_t GetPage(uint32_t Addr)
{
return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;;
}


/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* 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,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

capture.JPG

An error occurs in the function including breakpoint in the picture above. if i run the program, i'll be stuck in that infinite loop.

Why is this problem happening?

 
1 ACCEPTED SOLUTION

Accepted Solutions
KDJEM.1
ST Employee

Hi @KenLee ,

Thank you for updating post.

I think that the problem is more likely related to the software environment or configuration on the specific PC rather than the hardware, because the issue is not consistent across all PCs and all toolchain like STM32CubeIDE.

I suggest you to compare the configurations and software versions between the PCs to identify any discrepancies.

Could you please try to reinstall keil and to use FLASH_EraseProgram example of STM32CubeC0 V1.1.0.

Note that, this example is developed with MDK-ARM toolchain V5.36 as mentioned in the release note.

KDJEM1_0-1713858185029.png

Thank you.

Kaouthar 

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.

View solution in original post

4 REPLIES 4
KDJEM.1
ST Employee

Hello @KenLee and welcome to the community :),

Do you encounter the same issue when using STM32CubeIDE toolchain?

I think that this issue of  Flash Erase in Keil is due to breakpoints. 

Could you please try to disable this behavior by following the instructions provided  here.

Also, I recommend you to take a look at this discussion may help you.

Please let me know if the issue is solved.

Thank you.

Kaouthar

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.

Thank you for Reply.

As you said, it is works well in CubeIDE. But, in Keil5, Unlike the contents of the link you sent me, an error occurs even though there is no breakpoint when debuging.

Also, errors do not appear on other PCs that use the same code and compiler. then, it is hardware problem? i'm really confused.

KDJEM.1
ST Employee

Hi @KenLee ,

Thank you for updating post.

I think that the problem is more likely related to the software environment or configuration on the specific PC rather than the hardware, because the issue is not consistent across all PCs and all toolchain like STM32CubeIDE.

I suggest you to compare the configurations and software versions between the PCs to identify any discrepancies.

Could you please try to reinstall keil and to use FLASH_EraseProgram example of STM32CubeC0 V1.1.0.

Note that, this example is developed with MDK-ARM toolchain V5.36 as mentioned in the release note.

KDJEM1_0-1713858185029.png

Thank you.

Kaouthar 

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.

Thank you for your response.

As your advice, My MDK-ARM toolchain version was not V5.36.

I checked the version when I installed the SDK, but I guess i was mistaken for something.

Anyway, After changing the version to V5.36 and trying again, the program works very well now. Thank you!