Skip to main content
UUrcan
Associate II
August 20, 2020
Question

WRITING TO FLASH WITH UART

  • August 20, 2020
  • 3 replies
  • 3055 views

Hi everyone,

I can write to flash of STM32F103, max value of uint32 variable 4294967294.

It is possible to write when you write it to flash directly.

But I want to write it to flash with uart receiving. But when I put the rows (which is writed Bold in codes) for receiving uart then it gives different value.

I am sharing my codes below. Could you please check it where or what is wrong with my codes?

Thanks and regards

uint32_t RxData;

uint32_t TxData;

char UartRxData[10]="4294967294";

char data[10];

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 /* USER CODE BEGIN 2 */

 Flash_Write( 0x08007C00, 4294967294);

 HAL_Delay(50);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 HAL_UART_Receive_IT(&huart2, (uint8_t*)&UartRxData, strlen(UartRxData)); //activate UART receive interrupt every time

 

unsigned long val = atoi(UartRxData);

 Flash_Write(0x08007C00, val);

 RxData = Flash_Read(0x08007C00);

 sprintf(data, " %lu", RxData);

 HAL_UART_Transmit(&huart2, (uint8_t*)&data, strlen(data), 1000);

 HAL_Delay(50);

 }

 /* USER CODE END 3 */

}

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
August 20, 2020

Writing to FLASH operates independently of the source of the data

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
UUrcan
UUrcanAuthor
Associate II
August 20, 2020

Sure. Writing operation is done independently. I do not have any problem with writing operation. I want to get variable value from UART and also want it to write to the flash.

Tesla DeLorean
Guru
August 20, 2020

Your initial post was devoid of any content.

You can only write to a Flash location once after you erase it.

Your HAL_UART_Receive_IT() code falls-through, unhelpfully in this case.

The Flash_Read() and Flash_Write() code are not provided.

I'd focus on getting the reading from UART half of this working properly first.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
August 20, 2020

Use a blocking UART receive call and ensure it returns a valid value.

"If you feel a post has answered your question, please click ""Accept as Solution""."
UUrcan
UUrcanAuthor
Associate II
August 21, 2020

Yes I did. And you are correct. It couldn't convert char to uint32 correctly. Do you know char to uint32 converting method?

TDK
August 21, 2020
"If you feel a post has answered your question, please click ""Accept as Solution""."
Piranha
Principal III
August 21, 2020

https://wiki.sei.cmu.edu/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number

And, in addition to what others said, UART reception doesn't give you a zero-terminated C string.