RT-Thread中的串口DMA分析

這裏分析一下RT-Thread中串口DMA方式的實現,以供做新處理器串口支持時的參考。

背景

在如今的芯片性能和外設強大功能的情況下,串口不實現DMA/中斷方式操作,我認爲在實際項目中基本是不可接受的,但遺憾的是,rt-thread現有支持的實現中,基本上沒有支持串口的DMA,文檔也沒有關於串口DMA支持相關的說明,這裏以STM32實現爲背景,梳理一下串口DMA的實現流程,以供新處理器實現時以作參考。

DMA接收準備

啓用DMA接收,需要在打開設備的時候做一些處理,入口函數爲rt_device_open()。主體實現是:

rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
    ......
    result = device_init(dev);
    ......
    result = device_open(dev, oflag);
    ......
}

device_init()就是rt_serial_init()函數,其主要是調用configure()函數,

static rt_err_t rt_serial_init(struct rt_device *dev)
{
    ......
    if (serial->ops->configure)
        result = serial->ops->configure(serial, &serial->config);
    ......
}

在stm32下,其configure()函數是stm32_configure(),其根據設備打開參數,配置STM32外設的寄存器。包括波特率、校驗等串口工作參數。

device_open()函數就是rt_serial_open()函數,其主要實現是:

static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
{
    ......

#ifdef RT_SERIAL_USING_DMA        
    else if (oflag & RT_DEVICE_FLAG_DMA_RX)
    {
        if (serial->config.bufsz == 0) {
            struct rt_serial_rx_dma* rx_dma;

            rx_dma = (struct rt_serial_rx_dma*) rt_malloc (sizeof(struct rt_serial_rx_dma));
            RT_ASSERT(rx_dma != RT_NULL);
            rx_dma->activated = RT_FALSE;

            serial->serial_rx = rx_dma;
        } else {
            struct rt_serial_rx_fifo* rx_fifo;

            rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
                serial->config.bufsz);
            RT_ASSERT(rx_fifo != RT_NULL);
            rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
            rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
            rx_fifo->put_index = 0;
            rx_fifo->get_index = 0;
            rx_fifo->is_full = RT_FALSE;
            serial->serial_rx = rx_fifo;
            /* configure fifo address and length to low level device */
            serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
        }
        dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
    }
#endif /* RT_SERIAL_USING_DMA */
	......
#ifdef RT_SERIAL_USING_DMA
    else if (oflag & RT_DEVICE_FLAG_DMA_TX)
    {
        struct rt_serial_tx_dma* tx_dma;

        tx_dma = (struct rt_serial_tx_dma*) rt_malloc (sizeof(struct rt_serial_tx_dma));
        RT_ASSERT(tx_dma != RT_NULL);
        tx_dma->activated = RT_FALSE;

        rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
        serial->serial_tx = tx_dma;

        dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
        /* configure low level device */
        serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *)RT_DEVICE_FLAG_DMA_TX);
    }
#endif /* RT_SERIAL_USING_DMA */
    ......
}

可見,其主要工作是爲DMA接收準備FIFO緩衝區;爲DMA發送準備發送數據緩衝隊列,但是好像STM32中斷並沒有用到發送數據緩衝。

DMA配置數據來源是rt_hw_usart_init()函數,缺省的配置參數由宏RT_SERIAL_CONFIG_DEFAULT決定, 這裏決定了缺省的接收緩衝區參數是64字節,通訊缺省參數是:115200,8N1。

#define RT_SERIAL_RB_BUFSZ              64

DMA接收

DMA接收我們從DMA中斷開始分析,DMA接收中斷服務函數爲UARTn_DMA_RX_IRQHandler(),其調用HAL庫的DMA處理函數HAL_DMA_IRQHandler(),該函數調用回調函數HAL_UART_RxCpltCallback()或HAL_UART_RxHalfCpltCallback(),這兩個函數進入真正的中斷服務處理函數dma_isr(struct rt_serial_device *),主體代碼如下:

static void dma_isr(struct rt_serial_device *serial)
{
    ......
    /* 如果是DMA-RX中斷 */
    if ((__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_TC) != RESET) ||
            (__HAL_DMA_GET_IT_SOURCE(&(uart->dma_rx.handle), DMA_IT_HT) != RESET))
    {
        level = rt_hw_interrupt_disable();
        /* 得到本次接收到的數據量 */
        recv_total_index = serial->config.bufsz - __HAL_DMA_GET_COUNTER(&(uart->dma_rx.handle));
        if (recv_total_index == 0)
        {
            /* 這一句代碼,是什麼意思? */
            recv_len = serial->config.bufsz - uart->dma_rx.last_index;
        }
        else
        {
            /* 減去以前接收到的數據量,得到本次接收到的數據數量 */
            recv_len = recv_total_index - uart->dma_rx.last_index;
        }
        /* 更新接收歷史數據量 */
        uart->dma_rx.last_index = recv_total_index;
        rt_hw_interrupt_enable(level);

        if (recv_len)
        {
            /* 如果有新數據,調用serial設備模塊的通用處理 */
            rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
        }
    }
}

在serial模塊的函數rt_hw_serial_isr()中,主體代碼是:

void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
{
    ......
    case RT_SERIAL_EVENT_RX_DMADONE:
    {
        int length;
        rt_base_t level;

        /* get DMA rx length */
        length = (event & (~0xff)) >> 8;

        if (serial->config.bufsz == 0)
        {
            /* 這個case的處理邏輯不知道怎麼應用,看起來STM32實現並沒有處理這個case */
            struct rt_serial_rx_dma* rx_dma;

            rx_dma = (struct rt_serial_rx_dma*) serial->serial_rx;
            RT_ASSERT(rx_dma != RT_NULL);

            RT_ASSERT(serial->parent.rx_indicate != RT_NULL);
            serial->parent.rx_indicate(&(serial->parent), length);
            rx_dma->activated = RT_FALSE;
        }
        else
        {
            /* disable interrupt */
            level = rt_hw_interrupt_disable();
            /* update fifo put index, 將數據放入接收緩衝區 */
            rt_dma_recv_update_put_index(serial, length);
            /* calculate received total length, 更新緩衝區信息 */
            length = rt_dma_calc_recved_len(serial);
            /* enable interrupt */
            rt_hw_interrupt_enable(level);
            /* invoke callback, 通知上層,有新數據到達 */
            if (serial->parent.rx_indicate != RT_NULL)
            {
                serial->parent.rx_indicate(&(serial->parent), length);
            }
        }
        break;
    }
    ......
}

上層接到通知後,讀取函數最終調用驅動讀函數rt_serial_read()函數,在DMA的條件下,調用_serial_dma_rx()從緩衝區讀取數據。其代碼爲:

static rt_size_t rt_serial_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
    ......
    else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
    {
        return _serial_dma_rx(serial, (rt_uint8_t *)buffer, size);
    }
    ......
}

DMA發送

DMA發送從驅動寫函數rt_serial_write()開始,在DMA的條件下,調用_serial_dma_tx(),_serial_dma_tx()再調用操作的DMA發送函數發送數據,代碼爲:

static rt_size_t rt_serial_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    ......
    else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
    {
        return _serial_dma_tx(serial, (const rt_uint8_t *)buffer, size);
    }
    ......
}
rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
{
    ......
    /* make a DMA transfer */
    serial->ops->dma_transmit(serial, (rt_uint8_t *)data, length, RT_SERIAL_DMA_TX);
    ......
}

STM32的dma_transmit()實現函數是stm32_dma_transmit(),其實現就是簡單調用HAL_UART_Transmit_DMA(),代碼爲:

static rt_size_t stm32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
    ......
    if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) == HAL_OK)
    ......
}

實現非常簡單。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章