当前位置:网站首页>RT thread console device initialization

RT thread console device initialization

2022-06-13 05:02:00 Snow * sleet * snow

Today I am studying RT-Thread Log module ulog When I finally found log It is output through the console device , The console device is also a serial port device in nature , So I studied the serial port equipment .
So let's see log Output :

rt_device_t dev = rt_console_get_device();

rt_uint16_t old_flag = dev->open_flag;

dev->open_flag |= RT_DEVICE_FLAG_STREAM;
rt_device_write(dev, 0, log, len);
dev->open_flag = old_flag;

log Get before output console equipment , And then call rt_device_write Output . Then take a look at console Device initialization process .

#ifdef RT_USING_CONSOLE
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif

The above code is in rt_hw_board_init() In the middle of . among RT_CONSOLE_DEVICE_NAME The definition is as follows :

#define RT_CONSOLE_DEVICE_NAME “uart1”

It can be seen that it is a serial port device , The specific initialization is as follows :

rt_device_t rt_console_set_device(const char *name)
{
    
    rt_device_t new_device, old_device;

    /* save old device */
    old_device = _console_device;

    /* find new console device */
    new_device = rt_device_find(name);

    /* check whether it's a same device */
    if (new_device == old_device) return RT_NULL;

    if (new_device != RT_NULL)
    {
    
        if (_console_device != RT_NULL)
        {
    
            /* close old console device */
            rt_device_close(_console_device);
        }

        /* set new console device */
        rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
        _console_device = new_device;
    }

    return old_device;
}
RTM_EXPORT(rt_console_set_device);

From here, you can see that the console device is a serial port device .
Look back rt_hw_board_init() function

    /* USART driver initialization is open by default */
#ifdef RT_USING_SERIAL
    rt_hw_usart_init();
#endif
    
    /* Set the shell console output device */
#ifdef RT_USING_CONSOLE
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif

You can see that the serial port driver is initialized before initializing the console device . The specific code is as follows

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++)
    {
    
        /* init UART object */
        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;
}

Mainly look at this place uart_obj[i].config->name, Let's look for this name Where to define .
“uart_obj[i].config = &uart_config[i]”, From this line of code, we can see that the serial port configuration is in uart_config In this array . His definition is as follows :

tatic struct stm32_uart_config uart_config[] =
{
    
#ifdef BSP_USING_UART1
    UART1_CONFIG,
#endif

#ifdef BSP_USING_UART2
    UART2_CONFIG,
#endif

#ifdef BSP_USING_UART3
    UART3_CONFIG,
#endif

#ifdef BSP_USING_UART4
    UART4_CONFIG,
#endif
#ifdef BSP_USING_UART5
    UART5_CONFIG,
#endif
#ifdef BSP_USING_UART6
    UART6_CONFIG,
#endif
#ifdef BSP_USING_UART7
    UART7_CONFIG,
#endif
#ifdef BSP_USING_UART8
    UART8_CONFIG,
#endif
#ifdef BSP_USING_LPUART1
    LPUART1_CONFIG,
#endif
};

We're going to use uart1, Then take a look at uart1 The definition of :

#define UART1_CONFIG \ {
       \ .name = "uart1", \ .Instance = USART1, \ .irq_type = USART1_IRQn, \ }

The name is uart1, Look back rt_console_set_device(RT_CONSOLE_DEVICE_NAME), It is used to initialize the console device uart1 This serial port device .

原网站

版权声明
本文为[Snow * sleet * snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280516196325.html