针对stm32的drv_usart驱动库,有以下初始化内容
int rt_hw_usart_init(void)
{
rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct stm32_uart);
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t result = 0;
stm32_uart_get_dma_config();
for (int i = 0; i < obj_num; i++)
{
uart_obj[i].config = &uart_config[i];
uart_obj[i].serial.ops = &stm32_uart_ops;
uart_obj[i].serial.config = config;
/* register UART device */
result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
RT_DEVICE_FLAG_RDWR
| RT_DEVICE_FLAG_INT_RX
| RT_DEVICE_FLAG_INT_TX
| uart_obj[i].uart_dma_flag
, NULL);
RT_ASSERT(result == RT_EOK);
}
return result;
}
其中以下默认配置,包含了串口的接收缓冲区应申请的buffer大小,不知道RTT哪一部分代码进行了内存申请,赋值到了串口对应rt_serial_device结构体的void *serial_rx指针
/* Default config for serial_configure structure /
#define RT_SERIAL_CONFIG_DEFAULT
{
BAUD_RATE_115200, / 115200 bits/s /
DATA_BITS_8, / 8 databits /
STOP_BITS_1, / 1 stopbit /
PARITY_NONE, / No parity /
BIT_ORDER_LSB, / LSB first sent /
NRZ_NORMAL, / Normal mode /
RT_SERIAL_RB_BUFSZ, / Buffer size /
RT_SERIAL_FLOWCONTROL_NONE, / Off flowcontrol */
0
}
struct rt_serial_device
{
struct rt_device parent;
const struct rt_uart_ops *ops;
struct serial_configure config;
void *serial_rx;
void *serial_tx;
};
在串口中断处理函数里面,使用到了serial_rx指向的串口接收缓存区地址,猜测是哪里有代码动态申请了一个内存。
rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
希望有大佬指点下,RTT哪一部分代码对这个指针赋值了地址,谢谢!
/* ISR for serial interrupt /
void rt_hw_serial_isr(struct rt_serial_device serial, int event)
{
switch (event & 0xff)
{
case RT_SERIAL_EVENT_RX_IND:
{
int ch = -1;
rt_base_t level;
struct rt_serial_rx_fifo rx_fifo;
/ interrupt mode receive /
rx_fifo = (struct rt_serial_rx_fifo)serial->serial_rx;
RT_ASSERT(rx_fifo != RT_NULL);
while (1)
{
ch = serial->ops->getc(serial);
if (ch == -1) break;
/* disable interrupt /
level = rt_hw_interrupt_disable();
rx_fifo->buffer[rx_fifo->put_index] = ch;
rx_fifo->put_index += 1;
if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
/ if the next position is read index, discard this 'read char' /
if (rx_fifo->put_index == rx_fifo->get_index)
{
rx_fifo->get_index += 1;
rx_fifo->is_full = RT_TRUE;
if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
_serial_check_buffer_size();
}
/ enable interrupt /
rt_hw_interrupt_enable(level);
}
/ invoke callback /
if (serial->parent.rx_indicate != RT_NULL)
{
rt_size_t rx_length;
/ get rx length /
level = rt_hw_interrupt_disable();
rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
(serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
rt_hw_interrupt_enable(level);
if (rx_length)
{
serial->parent.rx_indicate(&serial->parent, rx_length);
}
}
break;
}
case RT_SERIAL_EVENT_TX_DONE:
{
struct rt_serial_tx_fifo tx_fifo;
tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
rt_completion_done(&(tx_fifo->completion));
break;
}
}
}