I want to change the baudrate on UARTN for a moment and back again to allow to set the proper baudrate on the module that is connected to it.
I cannot figure it out.
Thanks in advance
Rens
Hello Rens,
The user manual says that you need to be sure that you are not receiving any data because you will disable the UART so you won't be able to receive or transmit any data until you enable the UART again once you change the baudrate. So the step 1 (Make sure the USART is not currently sending or receiving data) is just to be sure that you won't loss any information. Here you have two options:
The routine you Chip_UARTN_SetBaud doesn't disable the UART, because this function was made planning that you will use it at the beginning of the program when the UART is disable. However, you can make the change with this function too, you just have to disable the USART then call the function and finally enable it again.
Here's the code:
/*Function to change the baudrate of the UART*/
static void setupBaudrate(uint32_t baudrate)
{
/*Deactivate the UART*/
LPC_USART1->CFG &= 0xFFFFFE;
/*Setting the new baudrate*/
Chip_UARTN_SetBaud(LPC_USART1, baudrate);
/*Enable the UART again*/
LPC_USART1->CFG |= 0x1;
}
/*Check if the USART is not transmitting or receiving data*/
if((LPC_USART1->STAT & 1 << 1) && (LPC_USART1->STAT & 1 << 3))
{
/*Call the function to set a new baudrate*/
setupBaudrate(9600);
/*Test that the new baudrate was set correctly*/
putLineUART("BAUDRATE CHANGED 9600\r\n");
}
Please let me know if you have any more doubts.
Have a great day,
Victor.
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello Victor,
Thanks for your reply.
I had (still have) the flu, so everything goes a bit slow.
I still have some questions:
The remark on page 212 of the user manual says:
Remark: If software needs to change the baud rate, the following sequence should be
used: 1) Make sure the USART is not currently sending or receiving data. 2) Disable the
USART by writing a 0 to the Enable bit (0 may be written to the entire registers). 3) Write
the new BRGVAL. 4) Write to the CFG register to set the Enable bit to 1.
How can you be sure, in a "living" system that the UART is not receiving data, as data arrives asynchrousnously?
Would disable UART be sufficient?
I also found the routine:
void Chip_UARTN_SetBaud(LPC_USARTN_T *pUART, uint32_t baudrate)
{
uint32_t baudRateGenerator;
baudRateGenerator = Chip_Clock_GetUSARTNBaseClockRate() / (16 * baudrate);
pUART->BRG = baudRateGenerator - 1; /* baud rate */
}
in uart_n_11u6x.c.
Shouldn't this do the job as well?
Why doesn't this routine NOT disable the UART? Any idea?
I have to adapt the lpcopen example to my board to test it, but I am confident that,
except the "active" receiver, it will work.
Thanks again for your reply.
Best regards, Rens
Hello Rens,
Which UART are you using? Please notice that you need to use UART1, UART2, UART3 or UART4 in order to change the baudrate on the go.
In the corresponding user manual of the LPC (UM10732) page 212 section Remark you will find the steps to change the baudrate on the go.
Here is an example of a function to re declare the baudrate:
static void setupBaudrate(uint32_t baudrate)
{
LPC_USART1->CFG = 0x00;
UART_CONFIG_T cfg =
{
0, /* U_PCLK frequency in Hz */
baudrate, /* Baud Rate in Hz */
1, /* 8N1 */
0, /* Asynchronous Mode */
NO_ERR_EN /* Enable No Errors */
};
cfg.sys_clk_in_hz = Chip_Clock_GetSystemClockRate();
/* Initialize the UART with the configuration parameters */
(void)LPC_UARTND_API->uart_init(uartHandle, &cfg);
}
I tried this function along with the example periph_uart_n_rom_polling provided in the LPCOpen and it worked correctly.
Please let me know if you have any more doubts.
Have a great day,
Victor.
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------