2020-08-20 07:19 AM
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 */
}
2020-08-20 07:27 AM
Writing to FLASH operates independently of the source of the data
2020-08-20 07:45 AM
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.
2020-08-20 08:26 AM
Use a blocking UART receive call and ensure it returns a valid value.
2020-08-20 10:03 AM
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.
2020-08-21 12:34 AM
Yes I did. And you are correct. It couldn't convert char to uint32 correctly. Do you know char to uint32 converting method?
2020-08-21 12:39 AM
Thank you for your reply Clive.
I did not add writing and reading codes. Problem is not there.
I found the problem. It can not convert char to uint32 correctly.
Do you know the method of it?
2020-08-21 06:49 AM
Did you try to search?
https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c
2020-08-21 12:41 PM
And, in addition to what others said, UART reception doesn't give you a zero-terminated C string.
2020-08-21 01:06 PM
sscanf() and atoi() should work provided you have determined that the data is ready.
HAL_UART_Receive_IT() returns immediately, prior to data arrival.
Once the data has been received the HAL calls your callback routine with valid data in the buffer.
The callback is called under interrupt context, so really shouldn't dwell too long, or block.