cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 file transfer via bluetooth

carlogulliani
Associate III
Posted on November 19, 2015 at 12:09

Hi there, I'm trying to receive file which has been sending from my laptop to my stm32f4discovery using usart via bluetooth HC-

If I send just text data it works fine, but when I try to send some file (it's about 40kB) I see nothing My handler for receiving anything from usart looks like this:

void
HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
__HAL_UART_FLUSH_DRREGISTER(&huart3); 
// Clear the buffer to prevent overrun
if
(rxBuffer == 
'\n'
|| rxBuffer == 
'\r'
) 
// rxBuffer is uint8_t rxBuffer = 0;
{
char
buf[40000];
char
msg[100];
sprintf
(buf, 
''%s\n''
, restring); 
// rxString is uint8_t rxString[45000]; 45kB for data
print(but); 
// my own function to print in serial port
}
}

I guess the problem could be in detection of file end. How to detect EOF? #stm32f3 #usart #stm32
7 REPLIES 7
Posted on November 19, 2015 at 12:54

I some how doubt you have 40K of stack space available, so why don't you just send restring? Instead of going through the unnecessary circus of sprintf()?

What's but in this context?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
carlogulliani
Associate III
Posted on November 19, 2015 at 13:09

Autocorrect changed my variable, I meant

sprintf

(buf,

''%s
''

, rxString);

So, what is better way? To send my file by parts? And I set breakpoint on the line where I check does the receiving char equal ' ', my transmitter program showed me that all file was sent, but I didn't catch breakpoint in my IDE for stm.. How to recognize that I get the end of file, I guess for does not have a symbol ' ' And one more question. I use hc-06 bluetooth, it sends using RFCOMM profile, is it good to send file using this profile? I can't find some info does hc-06 support OBEX profile
Posted on November 19, 2015 at 13:18

If the data is all ASCII, a common EOF character is 0x1A (26), but you'd have to add that.

For files over serial I'd tend to use something robust like XModem1K

Like I said don't put it on the stack (ie a local variable allocated from said stack), if you have all the data in restring sprintf()ing it to buf is a pointless exercise. Send restring directly rather than make a copy of it, unless you expect it to change.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 19, 2015 at 13:22

String functions expect you to NUL terminate the data strings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
carlogulliani
Associate III
Posted on November 19, 2015 at 21:33

Thanks for your detailed description, yes it could be send directly without sprintf, I used sprintf before to merge some data and didn't remove it.

So according my question and your answer, my file is the firmware for stm32 in .bin format. I'm trying to send it...

The main idea is to make possibility to update firmware by air, and I follow the next steps: 

1. send file via bluetooth

2. micro controller receives the file and save it on sd card, when my program finished send it, I call the function to write fw into ROM.

Posted on November 19, 2015 at 22:32

I've use HTTP for FOTA applications in the cellular space.

For upgrades over a serial connection, I've used XMODEM, either staged to SD Card, unused portions of FLASH, or directly from the boot loader to the application space. The XMODEM family are good from an integrity and self-pacing perspective. It's also supported natively by a lot of terminal applications, so less tools to write.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
carlogulliani
Associate III
Posted on November 20, 2015 at 10:52

Thank you! Indeed xmodem protocol has many useful options such as checksum and xmodem 1k has one important option, it can increase buffer size if there's no some error while transferring. 

Could you tell me or share some links where can I read how to use this way on stm32? I found out that I should init usart, init xmodem protocol and add 2 methods: receive and transmit