2022-08-19 01:44 AM
Hello,
I am working on a project in which i have to save my sensor readings in STM32G030F6's internal flash memory with time stamps. For that i am using STM's internal RTC to get the time and date. But problem is that i can't write data into flash if i am using RTC. It works fine if don't read the RTC date, time.
2022-08-19 04:07 AM
Then you are either incorrectly read RTC date time, or incorrectly write to FLASH.
My answer just reflects the amount of information you've given.
JW
2022-08-19 04:28 AM
void Flash_Write_Data (void)
{
HAL_FLASH_Unlock();
cData1[0] = gTime.Hours;
cData1[1] = gTime.Minutes;
cData1[2] = gDate.Date;
cData1[3] = gDate.Month;
DateTime = ((uint64_t)cData1[0])<<48 |((uint64_t)cData1[1])<<32 |(cData1[2] << 16) | (cData1[3] & 0xffff); //To combine the four 16 bit integers
if (Address < ENDAddress)
{
/* increment to next double word*/
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, Total_Stack_Voltage) == HAL_OK)
{
Address = Address + 8; /* increment to next double word*/
counter = counter+8;
}
}
HAL_FLASH_Lock();
}
int main(void)
{
/* USER CODE BEGIN 1 */
Address = StartAddress;
Flash_erase();
/* USER CODE END 1 */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN);
HAL_Delay(3000);
Flash_Write_Data();
}
/* USER CODE END 3 */
}
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x10;
sTime.Minutes = 0x30;
sTime.Seconds = 0x0;
sTime.SubSeconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_FRIDAY;
sDate.Month = RTC_MONTH_AUGUST;
sDate.Date = 0x19;
sDate.Year = 0x22;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
Hi,
Thanks, I have attached the Code snippets for Flash Write, RTC initialization and main function above. My code is very simple. For demo i used some hard coded values and used STM's HAL libraries for RTC and Writing data into flash. MCU that i am using is STM32G030f6P6.
2022-08-19 04:40 AM
OK and what are the symptoms and how are they different from the expectations?
Is the FLASH area into which you are writing, erased previously?
JW
2022-08-19 04:56 AM
My task is to get the sensor data and RTC data and then save them into internal flash of STM32 MCU. For that i started with sensor data only. I started from the Page 9 of MCU's flash memory and erased the memory before writing. And i have no problem in that.
Then i also included the RTC part in my original code. And now i am first getting the time and date from RTC and then writing it to Flash. but it doesn't work.
Now the problem is that Flash_Write() function works only when the RTC is not running. If i includes RTC, Flash_Write() function executes only once and then stops.
2022-08-19 05:04 AM
> Flash_Write() function executes only once and then stops.
What does it mean that "then stops"? How do you observe that?
JW
2022-08-19 05:10 AM
In this code i am writing data into flash in every three second and in one time it only writes 8 byte data and increase the flash address by 8. After 3 seconds it again writes 8 byte data and increase the address and so on. but when i include the RTC part it writes a 8 byte data and increases the address and then it stops. Other parts of the code keeps executing.
2022-08-19 05:15 AM
> when i include the RTC part it writes a 8 byte data and increases the address and then it stops.
How does it stop? Where does it stop? What does stop? How do you know "it stops"?
> Other parts of the code keeps executing.
What parts of code? How do they execute if the FLASH write stops?
JW
2022-08-19 09:52 PM
if (Address < ENDAddress)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address,
Total_Stack_Voltage) == HAL_OK)
{
Address = Address + 8; /* increment to next double word*/
}
while (1)
{
Flash_Write_Data();
HAL_Delay(3000);
HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
I am talking about this part of code. Ideally it should write the data in flash then read the time and date date and finally toggle the LED. The data is written into flash in 8 byte packets and after writing the data it should add 8 to current memory address (Address = Address + 8).
But in my case when i run the code, For the first time it works properly like it writes the data in flash, increase the address by 8 then read the date, time and toggle the LED. but after that it only updates the date, time and toggles the LED and doesn't write anything in flash, and doesn't increase the address by 8.
2022-08-20 02:10 AM
Step through HAL_FLASH_Program() and check the error flags after FLASH programming.
Make sure FLASH is erased before running the program, and that the addresses are properly aligned (i.e. integer multiple of 8).
JW