UART驱动API描述
UART驱动的API主要如下:
| API类别 | API名称 | 描述 |
|---|---|---|
| 初始化 | hal_uart_init() | 初始化UART外设,配置时钟分频系数等参数。 |
| hal_uart_deinit() | 反初始化UART外设。 | |
| hal_uart_msp_init() | 初始化UART外设所使用的GPIO引脚复用、NVIC中断、DMA通道。 | |
| hal_uart_msp_deinit() | 反初始化UART外设所使用的GPIO引脚复用、NVIC中断、DMA通道。 | |
| IO操作 | hal_uart_transmit() | 发送数据,轮询方式。 |
| hal_uart_receive() | 接收数据,轮询方式。 | |
| hal_uart_transmit_it() | 发送数据,中断方式。 | |
| hal_uart_receive_it() | 接收数据,中断方式。 | |
| hal_uart_transmit_dma() | 发送数据,DMA方式。 | |
| hal_uart_receive_dma() | 接收数据,DMA方式。 | |
| hal_uart_dma_pause() | 暂停DMA传输。 | |
| hal_uart_dma_resume() | 恢复DMA传输。 | |
| hal_uart_dma_stop() | 停止DMA传输。 | |
| hal_uart_abort() | 中止中断及DMA方式下的数据收发,轮询方式。 | |
| hal_uart_abort_transmit() | 中止中断及DMA方式下的数据发送,轮询方式。 | |
| hal_uart_abort_receive() | 中止中断及DMA方式下的数据接收,轮询方式。 | |
| hal_uart_abort_it() | 中止中断及DMA方式下的数据收发,中断方式。 | |
| hal_uart_abort_transmit_it() | 中止中断及DMA方式下的数据发送,中断方式。 | |
| hal_uart_abort_receive_it() | 中止中断及DMA方式下的数据接收,中断方式。 | |
| 中断处理及回调函数 | hal_uart_irq_handler() | 中断处理函数。 |
| hal_uart_tx_cplt_callback() | 发送完成中断回调函数。 | |
| hal_uart_rx_cplt_callback() | 接收完成中断回调函数。 | |
| hal_uart_error_callback() | 错误中断回调函数。 | |
| hal_uart_abort_cplt_callback() | 收发中止完成中断回调函数。 | |
| hal_uart_abort_transmit_cplt_callback() | 发送中止完成中断回调函数。 | |
| hal_uart_abort_receive_cplt_callback() | 接收中止完成中断回调函数。 | |
| 状态及错误 | hal_uart_get_state() | 获取驱动运行状态。 |
| hal_uart_get_error() | 获取错误码。 | |
| 睡眠相关 | hal_uart_suspend_reg() | 睡眠之前挂起和UART配置相关的寄存器。 |
| hal_uart_resume_reg() | 唤醒时恢复和UART配置相关的寄存器。 |
下面章节将对各API进行详细描述。
hal_uart_init
| 函数原型 | hal_status_t hal_uart_init(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 根据uart_init_t中的指定参数初始化UART模式,并初始化相关句柄。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_deinit
| 函数原型 | hal_status_t hal_uart_deinit(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 反初始化UART外设。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_msp_init
| 函数原型 | void hal_uart_msp_init(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 初始化UART所使用的GPIO引脚复用、NVIC中断、DMA通道等配置。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该API为weak类型的空函数,开发者需要重写该API完成GPIO引脚复用、NVIC中断、DMA通道的初始化。 |
hal_uart_msp_deinit
| 函数原型 | void hal_uart_msp_deinit(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 反初始化UART所使用的GPIO引脚复用、NVIC中断、DMA通道等配置。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该API为weak类型的空函数,开发者需重写该API完成GPIO引脚复用、NVIC中断、DMA通道的反初始化。 |
hal_uart_transmit
| 函数原型 | hal_status_t hal_uart_transmit(uart_handle_t *p_uart, uint8_t *p_data, uint16_t size, uint32_t timeout) |
|---|---|
| 功能说明 | 发送大量数据,轮询方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 p_data:指向数据缓冲区的指针。 size:待发送数据的长度。 timout:超时时间。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_receive
| 函数原型 | hal_status_t hal_uart_receive(uart_handle_t *p_uart, uint8_t *p_data, uint16_t size, uint32_t timeout) |
|---|---|
| 功能说明 | 接收大量数据,轮询方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 p_data:指向数据缓冲区的指针。 size:待接收数据的长度。 timout:超时时间。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_transmit_it
| 函数原型 | hal_status_t hal_uart_transmit_it(uart_handle_t *p_uart, uint8_t *p_data, uint16_t size) |
|---|---|
| 功能说明 | 发送大量数据,中断方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 p_data:指向数据缓冲区的指针。 size:待发送数据的长度。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_receive_it
| 函数原型 | hal_status_t hal_uart_receive_it(uart_handle_t *p_uart, uint8_t *p_data, uint16_t size) |
|---|---|
| 功能说明 | 接收大量数据,中断方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 p_data:指向数据缓冲区的指针。 size:待接收数据的长度。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_transmit_dma
| 函数原型 | hal_status_t hal_uart_transmit_dma(uart_handle_t *p_uart, uint8_t *p_data, uint16_t size) |
|---|---|
| 功能说明 | 发送大量数据,DMA方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 p_data:指向数据缓冲区的指针。 size:待发送数据的长度。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_receive_dma
| 函数原型 | hal_status_t hal_uart_receive_dma(uart_handle_t *p_uart, uint8_t *p_data, uint16_t size) |
|---|---|
| 功能说明 | 接收大量数据,DMA方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 p_data:指向数据缓冲区的指针。 size:待接收数据的长度。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_dma_pause
| 函数原型 | hal_status_t hal_uart_dma_pause(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 暂停UART DMA传输。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_dma_resume
| 函数原型 | hal_status_t hal_uart_dma_resume(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 恢复UART DMA传输。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_dma_stop
| 函数原型 | hal_status_t hal_uart_dma_stop(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 停止UART DMA传输。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
hal_uart_abort
| 函数原型 | hal_status_t hal_uart_abort(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 中止中断及DMA方式下的数据收发,轮询方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
该函数可用于中止中断方式或DMA方式的数据收发,该函数执行如下操作:
|
hal_uart_abort_transmit
| 函数原型 | hal_status_t hal_uart_abort_transmit(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 中止中断及DMA方式下的数据发送,轮询方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
该函数可用于中止中断方式或DMA方式的数据发送,该函数执行如下操作:
|
hal_uart_abort_receive
| 函数原型 | hal_status_t hal_uart_abort_receive(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 中止中断及DMA方式下的数据接收,轮询方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
该函数可用于中止中断方式或DMA方式的数据接收,该函数执行如下操作:
|
hal_uart_abort_it
| 函数原型 | hal_status_t hal_uart_abort_it(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 中止中断及DMA方式下的数据收发,中断方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
该函数可用于中止中断方式或DMA方式的数据收发,该函数执行如下操作:
中止完成后,hal_uart_abort_cplt_callback()会被调用。 |
hal_uart_abort_transmit_it
| 函数原型 | hal_status_t hal_uart_abort_transmit_it(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 中止中断及DMA方式下的数据发送,中断方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
该函数可用于中止中断方式或DMA方式的数据发送,该函数执行如下操作:
中止完成后,hal_uart_abort_transmit_cplt_callback()会被调用。 |
hal_uart_abort_receive_it
| 函数原型 | hal_status_t hal_uart_abort_receive_it(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 中止中断及DMA方式下的数据接收,中断方式。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
| 备注 |
该函数可用于中止中断方式或DMA方式的数据接收,该函数执行如下操作:
中止完成后,hal_uart_abort_receive_cplt_callback()会被调用。 |
hal_uart_irq_handler
| 函数原型 | void hal_uart_irq_handler(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 处理UART中断请求。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 |
hal_uart_tx_cplt_callback
| 函数原型 | void hal_uart_tx_cplt_callback(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 发送完成中断回调函数。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该函数为weak类型的空函数,当开发者需要使用该回调函数时,可重写该API。 |
hal_uart_rx_cplt_callback
| 函数原型 | void hal_uart_rx_cplt_callback(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 接收完成中断回调函数。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该函数为weak类型的空函数,当开发者需要使用该回调函数时,可重写该API。 |
hal_uart_error_callback
| 函数原型 | void hal_uart_error_callback(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | UART错误中断回调函数。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该函数为weak类型的空函数,当开发者需要使用该回调函数时,可重写该API。 |
hal_uart_abort_cplt_callback
| 函数原型 | void hal_uart_abort_cplt_callback(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | UART中止完成中断回调函数。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该函数为weak类型的空函数,当开发者需要使用该回调函数时,可重写该API。 |
hal_uart_abort_tx_cplt_callback
| 函数原型 | void hal_uart_abort_tx_cplt_callback(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | UART中止发送完成中断回调函数。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 | 该函数为weak类型的空函数,当开发者需要使用该回调函数时,可重写该API。 |
hal_uart_abort_rx_cplt_callback
| 函数原型 | void hal_uart_abort_rx_cplt_callback(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | UART中止接收完成中断回调函数。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | 无 |
| 备注 |
hal_uart_get_state
| 函数原型 | hal_uart_state_t hal_uart_get_state(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 获取UART运行状态。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 |
UART运行状态,值可以是下面中的任意一个:
|
| 备注 |
hal_uart_get_error
| 函数原型 | uint32_t hal_uart_get_error(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 获取UART错误码。 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 |
UART错误码,值可以是下面中的任意一个:
|
hal_uart_suspend_reg
| 函数原型 | hal_status_t hal_uart_suspend_reg(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 睡眠之前挂起和UART配置相关的寄存器 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态。 |
hal_uart_resume_reg
| 函数原型 | hal_status_t hal_uart_resume_reg(uart_handle_t *p_uart) |
|---|---|
| 功能说明 | 唤醒时恢复和UART配置相关的寄存器 |
| 输入参数 | p_uart:指向uart_handle_t结构体变量的指针,该结构体变量包含指定的UART的配置信息。 |
| 返回值 | HAL状态 |
| 备注 |