Azure / azure-sphere-samples

Samples for Azure Sphere
Other
222 stars 200 forks source link

How to change UART baud rate on Azure after DFU #266

Closed nishithvpoojary closed 1 year ago

nishithvpoojary commented 1 year ago

Hi. We use Azure Iot Sample to connect device to cloud with the default baud rate of 460800. Using c2d message the external firmware already stored in azure has to be sent to external MCU with the baud rate of 115200. I was able to close the default baud rate of 460800 and open new Fd with 115200 baud rate and successfully do the external firmware update.

Then I opened a new Uart Fd of baud rate 460800. Now i face issue here. Since i opened the new Fd in cloud.c I can only do function included in cloud.c ..... All the c2d messages are working, but i dont receive any message from external device since the Uart event handler is declared in main.c.

How can i have a new Fd with 460800 baud rate which can be used by all event handlers. Thank you.

jamesadevine commented 1 year ago

Hiya,

Unless I am misunderstanding the question, this seems to be a general programming question than a problem with any with Azure Sphere APIs.

You can use file descriptors across different c files so long as the variable is declared with appropriate linkage. (see: https://stackoverflow.com/questions/1045501/how-do-i-share-variables-between-different-c-files)

main.c:

int my_variable_fd = -1;

... 

my_variable_fd = Uart_Open(...)

cloud.c:

extern int my_variable_fd;

...

write(my_variable_fd, "hello");

Global variables are generally bad practice. You should instead add new .c/.h files with functions to manipulate the existing file descriptor e.g.

uart.c:

static int uart_fd = -1;

void uart_open() {
...
}

int uart_write(char* bytes, int len) {
...
}

int uart_read(char* buf, int len) {
...
}

int uart_set_baud(uint32_t baud) {
...
}

uart.h:

#pragma once
void uart_open();
int uart_write(char* bytes, int len);
int uart_read(char* buf, int len);
int uart_set_baud(uint32_t baud);