cancel
Showing results for 
Search instead for 
Did you mean: 

WRITING TO FLASH WITH UART

UUrcan
Associate II

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

}

9 REPLIES 9

Writing to FLASH operates independently of the source of the data

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

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.

TDK
Guru

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".

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
Up vote any posts that you find helpful, it shows what's working..
UUrcan
Associate II

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

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?

Did you try to search?

https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c

If you feel a post has answered your question, please click "Accept as Solution".
Piranha
Chief II

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.

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.

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