I have made below code. Task is to transmit data to/from two uarts.
One uart is at 9600 baud & other is at 115200 baud.
Whatever data is received at one uart is to be sent to other uart & vice versa.
MCU is 32 bit cortex M3 with two hardware uart's & core is running at 16Mhz.
Have implemented two circular buffers , each of size 27K. Data length could be upto 500KB in one go.
Data is always 1 strt,8-data & 1 stop.
1. Currently I have roughly taken 27K buffer. But how to calcualte how much buffer exactly should be required.
For example, in case of 9600 baud, I will rx 960 bytes of data in 1 sec & 11520 bytes in other uart.
2. Initillay only rx int of both uarts are enabled. Tx int are enabled only when there is data in buffer, & disabled again when head=tail.
One uart is at 9600 baud & other is at 115200 baud.
Whatever data is received at one uart is to be sent to other uart & vice versa.
MCU is 32 bit cortex M3 with two hardware uart's & core is running at 16Mhz.
Have implemented two circular buffers , each of size 27K. Data length could be upto 500KB in one go.
Data is always 1 strt,8-data & 1 stop.
1. Currently I have roughly taken 27K buffer. But how to calcualte how much buffer exactly should be required.
For example, in case of 9600 baud, I will rx 960 bytes of data in 1 sec & 11520 bytes in other uart.
2. Initillay only rx int of both uarts are enabled. Tx int are enabled only when there is data in buffer, & disabled again when head=tail.
C:
const uint32_t g_arr_size = 27648U;
volatile uint8_t g_9600_arr[g_arr_size];
volatile uint32_t g_9600_head;
volatile uint32_t g_9600_tail;
volatile uint8_t g_115200_arr[g_arr_size];
volatile uint32_t g_115200_head;
volatile uint32_t g_115200_tail;
void init_both_uarts(void)
{
uart2_9600_baud();
uart2_rx_int_enable();
uart2_tx_int_disable();
uart1_115200_baud();
uart1_rx_int_enable();
uart1_tx_int_disable();
}
void uart1_isr(void)
{
uint16_t data;
uint32_t next_head;
/* if data is rx */
if(uart1_data_rx_nt_full())
{
/* read data */
data = READ_UART1_DATA();
/* store data in array */
/* inc the head cnt */
next_head = g_115200_head++;
if(next_head >= g_arr_size)
{
next_head = 0U;
}
/* check if there is room */
if(next_head != g_115200_tail)
{
g_115200_arr[g_115200_head] = data;
g_115200_head = next_head;
/* enable other uart interrupt */
ENABLE_UART2_INT_TX();
}
else
{
/* diacrd the byte */
}
}
/* if data is tx */
if(uart1_data_tx_empty())
{
/* remove the byte from array */
if(g_9600_head != g_9600_tail)
{
while( !UART2_WHILE_TXE_COMPLETE );
WRITE_UART2_DR_REG( g_9600_arr[g_9600_tail] );
g_9600_tail++;
if(g_9600_tail >= g_arr_size)
{
g_9600_tail = 0U;
}
}
/* disable uart int tx */
else
{
DISABLE_UART1_INT_TX();
}
}
}
void uart2_isr(void)
{
uint16_t data;
uint32_t next_head;
/* if data is rx */
if(uart2_data_rx_nt_full())
{
/* read data */
data = READ_UART2_DATA();
/* store data in array */
/* inc the head cnt */
next_head = g_9600_head++;
if(next_head >= g_arr_size)
{
next_head = 0U;
}
/* check if there is room */
if(next_head != g_9600_tail)
{
g_9600_arr[g_9600_head] = data;
g_9600_head = next_head;
/* enable other uart interrupt */
ENABLE_UART1_INT_TX();
}
else
{
/* diacrd the byte */
}
}
/* if data is tx */
if(uart2_data_tx_empty())
{
/* remove the byte from array */
if(g_115200_head != g_115200_tail)
{
while( !UART1_WHILE_TXE_COMPLETE );
WRITE_UART1_DR_REG( g_115200_arr[g_115200_tail] );
g_115200_tail++;
if(g_115200_tail >= g_arr_size)
{
g_115200_tail = 0U;
}
}
/* disable uart int tx */
else
{
DISABLE_UART2_INT_TX();
}
}
}
Last edited by a moderator: