[drv_usart]完善ch32的串口驱动。 (#6336)

This commit is contained in:
liYang~ 2022-08-27 12:34:27 +08:00 committed by GitHub
parent ae66e67dee
commit ef8ae7963c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 331 additions and 117 deletions

View File

@ -5,94 +5,227 @@
*
* Change Logs:
* Date Author Notes
* 2009-01-05 Bernard the first version
* 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
* 2013-05-13 aozima update for kehong-lingtai.
* 2015-01-31 armink make sure the serial transmit complete in putc()
* 2016-05-13 armink add DMA Rx mode
* 2017-01-19 aubr.cool add interrupt Tx mode
* 2017-04-13 aubr.cool correct Rx parity err
* 2017-10-20 ZYH porting to HAL Libraries(with out DMA)
* 2017-11-15 ZYH update to 3.0.0
* 2022-08-27 liYony the first version
*/
#include "board.h"
#include <rtdevice.h>
#include <drv_usart.h>
/* uart driver */
struct ch32_uart
#ifdef RT_USING_SERIAL
//#define DRV_DEBUG
#define LOG_TAG "drv.uart"
#include <drv_log.h>
#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && \
!defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
#error "Please define at least one BSP_USING_UARTx"
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
#endif
enum
{
USART_InitTypeDef huart;
USART_TypeDef *USARTx;
IRQn_Type irq;
#ifdef BSP_USING_UART1
UART1_INDEX,
#endif
#ifdef BSP_USING_UART2
UART2_INDEX,
#endif
#ifdef BSP_USING_UART3
UART3_INDEX,
#endif
#ifdef BSP_USING_UART4
UART4_INDEX,
#endif
#ifdef BSP_USING_UART5
UART5_INDEX,
#endif
#ifdef BSP_USING_UART6
UART6_INDEX,
#endif
#ifdef BSP_USING_UART7
UART7_INDEX,
#endif
#ifdef BSP_USING_UART8
UART8_INDEX,
#endif
};
/* If you want to use other serial ports, please follow UART1 to complete other
serial ports. For clock configuration, */
static struct ch32_uart_hw_config uart_hw_config[] =
{
#ifdef BSP_USING_UART1
{
/* clock configuration, please refer to ch32v30x_rcc.h */
RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,
/* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
GPIOA, GPIO_Pin_9, /* Tx */GPIOA, GPIO_Pin_10, /* Rx */
/* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
GPIO_Remap_NONE,
},
#endif
#ifdef BSP_USING_UART2
{},
#endif
#ifdef BSP_USING_UART3
{},
#endif
#ifdef BSP_USING_UART4
{},
#endif
#ifdef BSP_USING_UART5
{},
#endif
#ifdef BSP_USING_UART6
{},
#endif
#ifdef BSP_USING_UART7
{},
#endif
#ifdef BSP_USING_UART8
{},
#endif
};
static struct ch32_uart_config uart_config[] =
{
#ifdef BSP_USING_UART1
{
"uart1",
USART1,
USART1_IRQn,
},
#endif
#ifdef BSP_USING_UART2
{
"uart2",
USART2,
USART2_IRQn,
},
#endif
#ifdef BSP_USING_UART3
{
"uart3",
USART3,
USART3_IRQn,
},
#endif
#ifdef BSP_USING_UART4
{
"uart4",
UART4,
UART4_IRQn,
},
#endif
#ifdef BSP_USING_UART5
{
"uart5",
UART5,
UART5_IRQn,
},
#endif
#ifdef BSP_USING_UART6
{
"uart6",
UART6,
UART6_IRQn,
},
#endif
#ifdef BSP_USING_UART7
{
"uart7",
UART7,
UART7_IRQn,
},
#endif
#ifdef BSP_USING_UART8
{
"uart8",
UART8,
UART8_IRQn,
},
#endif
};
static struct ch32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
static rt_err_t ch32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct ch32_uart *uart;
GPIO_InitTypeDef GPIO_InitStructure={0};
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = (struct ch32_uart *)serial->parent.user_data;
uart->huart.USART_BaudRate = cfg->baud_rate;
uart->huart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart->huart.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
uart = (struct ch32_uart *) serial->parent.user_data;
uart->Init.USART_BaudRate = cfg->baud_rate;
uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart->Init.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
switch (cfg->data_bits)
{
case DATA_BITS_8:
uart->huart.USART_WordLength = USART_WordLength_8b;
uart->Init.USART_WordLength = USART_WordLength_8b;
break;
case DATA_BITS_9:
uart->huart.USART_WordLength = USART_WordLength_9b;
uart->Init.USART_WordLength = USART_WordLength_9b;
break;
default:
uart->huart.USART_WordLength = USART_WordLength_8b;
uart->Init.USART_WordLength = USART_WordLength_8b;
break;
}
switch (cfg->stop_bits)
{
case STOP_BITS_1:
uart->huart.USART_StopBits = USART_StopBits_1;
uart->Init.USART_StopBits = USART_StopBits_1;
break;
case STOP_BITS_2:
uart->huart.USART_StopBits = USART_StopBits_2;
uart->Init.USART_StopBits = USART_StopBits_2;
break;
default:
uart->huart.USART_StopBits = USART_StopBits_1;
uart->Init.USART_StopBits = USART_StopBits_1;
break;
}
switch (cfg->parity)
{
case PARITY_NONE:
uart->huart.USART_Parity = USART_Parity_No;
uart->Init.USART_Parity = USART_Parity_No;
break;
case PARITY_ODD:
uart->huart.USART_Parity = USART_Parity_Odd;
uart->Init.USART_Parity = USART_Parity_Odd;
break;
case PARITY_EVEN:
uart->huart.USART_Parity = USART_Parity_Even;
uart->Init.USART_Parity = USART_Parity_Even;
break;
default:
uart->huart.USART_Parity = USART_Parity_No;
uart->Init.USART_Parity = USART_Parity_No;
break;
}
if(uart->USARTx == USART1)
/* UART hardware configuration, including clock and GPIO, etc. */
RCC_APB2PeriphClockCmd(uart->hw_config->periph_clock, ENABLE);
if(uart->hw_config->remap != GPIO_Remap_NONE)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_Init(uart->USARTx,&uart->huart);
USART_Cmd(uart->USARTx, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(uart->hw_config->remap, ENABLE);
}
GPIO_InitStructure.GPIO_Pin = uart->hw_config->tx_gpio_pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(uart->hw_config->tx_gpio_port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = uart->hw_config->rx_gpio_pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(uart->hw_config->rx_gpio_port, &GPIO_InitStructure);
USART_Init(uart->config->Instance,&uart->Init);
USART_Cmd(uart->config->Instance, ENABLE);
return RT_EOK;
}
@ -107,16 +240,16 @@ static rt_err_t ch32_control(struct rt_serial_device *serial, int cmd, void *arg
/* disable interrupt */
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
NVIC_DisableIRQ(uart->irq);
NVIC_DisableIRQ(uart->config->irq_type);
/* disable interrupt */
USART_ITConfig(uart->USARTx,USART_IT_RXNE,DISABLE);
USART_ITConfig(uart->config->Instance,USART_IT_RXNE,DISABLE);
break;
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
NVIC_EnableIRQ(uart->irq);
NVIC_EnableIRQ(uart->config->irq_type);
/* enable interrupt */
USART_ITConfig(uart->USARTx, USART_IT_RXNE,ENABLE);
USART_ITConfig(uart->config->Instance, USART_IT_RXNE,ENABLE);
break;
}
return RT_EOK;
@ -127,8 +260,8 @@ static int ch32_putc(struct rt_serial_device *serial, char c)
struct ch32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct ch32_uart *)serial->parent.user_data;
while (USART_GetFlagStatus(uart->USARTx, USART_FLAG_TC) == RESET);
uart->USARTx->DATAR = c;
while (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TC) == RESET);
uart->config->Instance->DATAR = c;
return 1;
}
@ -139,33 +272,26 @@ static int ch32_getc(struct rt_serial_device *serial)
RT_ASSERT(serial != RT_NULL);
uart = (struct ch32_uart *)serial->parent.user_data;
ch = -1;
if (USART_GetFlagStatus(uart->USARTx, USART_FLAG_RXNE) != RESET)
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE) != RESET)
{
ch = uart->USARTx->DATAR & 0xff;
ch = uart->config->Instance->DATAR & 0xff;
}
return ch;
}
rt_size_t ch32dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
return RT_EOK;
}
/**
* Uart common interrupt process. This need add to uart ISR.
*
* @param serial serial device
*/
static void uart_isr(struct rt_serial_device *serial)
{
struct ch32_uart *uart = (struct ch32_uart *) serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
if (USART_GetITStatus(uart->USARTx, USART_IT_RXNE) != RESET)
if (USART_GetITStatus(uart->config->Instance, USART_IT_RXNE) != RESET)
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
USART_ClearITPendingBit(uart->USARTx, USART_IT_RXNE);
USART_ClearITPendingBit(uart->config->Instance, USART_IT_RXNE);
}
}
@ -178,76 +304,131 @@ static const struct rt_uart_ops ch32_uart_ops =
ch32dma_transmit
};
#if defined(BSP_USING_UART1)
/* UART1 device driver structure */
struct ch32_uart uart1;
struct rt_serial_device serial1;
void USART1_IRQHandler(void) __attribute__((interrupt()));
#ifdef BSP_USING_UART1
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART1_IRQHandler(void)
{
GET_INT_SP();
/* enter interrupt */
rt_interrupt_enter();
uart_isr(&serial1);
/* leave interrupt */
uart_isr(&(uart_obj[UART1_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif /* BSP_USING_UART1 */
#endif
#ifdef BSP_USING_UART2
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART2_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART2_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
#ifdef BSP_USING_UART3
void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void USART3_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART3_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
#ifdef BSP_USING_UART4
void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART4_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART4_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
#ifdef BSP_USING_UART5
void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART5_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART5_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
#ifdef BSP_USING_UART6
void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART6_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART6_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
#ifdef BSP_USING_UART7
void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART7_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART7_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
#ifdef BSP_USING_UART8
void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void UART8_IRQHandler(void)
{
GET_INT_SP();
rt_interrupt_enter();
uart_isr(&(uart_obj[UART8_INDEX].serial));
rt_interrupt_leave();
FREE_INT_SP();
}
#endif
int rt_hw_usart_init(void)
{
struct ch32_uart *uart;
rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ch32_uart);
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t result = 0;
#if defined(BSP_USING_UART1)
uart=&uart1;
uart->irq=USART1_IRQn;
uart->USARTx=USART1;
config.baud_rate = BAUD_RATE_115200;
serial1.ops = &ch32_uart_ops;
serial1.config = config;
uart->huart.USART_BaudRate = 115200;
uart->huart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart->huart.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
uart->huart.USART_WordLength = USART_WordLength_8b;
uart->huart.USART_StopBits = USART_StopBits_1;
uart->huart.USART_Parity = USART_Parity_No;
if(uart->USARTx == USART1)
for (int i = 0; i < obj_num; i++)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_Init(uart->USARTx,&uart->huart);
USART_Cmd(uart->USARTx, ENABLE);
}
/* init UART object */
uart_obj[i].config = &uart_config[i];
uart_obj[i].hw_config = &uart_hw_config[i];
uart_obj[i].serial.ops = &ch32_uart_ops;
uart_obj[i].serial.config = config;
/* Hardware initialization is required, otherwise it
will not be registered into the device framework */
if(uart_obj[i].hw_config->periph_clock == 0)
{
LOG_E("You did not perform hardware initialization for %s", uart->config->name);
continue;
}
/* 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
, &uart_obj[i]);
RT_ASSERT(result == RT_EOK);
}
/* register UART1 device */
rt_hw_serial_register(&serial1, "uart1",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
#endif /* BSP_USING_UART1 */
return 0;
return result;
}
INIT_BOARD_EXPORT(rt_hw_usart_init);
#endif /* RT_USING_SERIAL */

View File

@ -5,12 +5,45 @@
*
* Change Logs:
* Date Author Notes
* 2009-01-05 Bernard the first version
* 2022-08-27 liYony the first version
*/
#ifndef __USART_H__
#define __USART_H__
#include "rthw.h"
#include "rtthread.h"
#ifndef __DRV_USART_H__
#define __DRV_USART_H__
#include <rtthread.h>
#include "rtdevice.h"
#include <rthw.h>
/* Do not use GPIO_Remap*/
#define GPIO_Remap_NONE 0
/* ch32 hardware config class */
struct ch32_uart_hw_config
{
rt_uint32_t periph_clock;
GPIO_TypeDef *tx_gpio_port;
rt_uint16_t tx_gpio_pin;
GPIO_TypeDef *rx_gpio_port;
rt_uint16_t rx_gpio_pin;
rt_uint32_t remap;
};
/* ch32 config class */
struct ch32_uart_config
{
const char *name;
USART_TypeDef *Instance;
IRQn_Type irq_type;
};
/* ch32 uart dirver class */
struct ch32_uart
{
struct ch32_uart_hw_config *hw_config;
struct ch32_uart_config *config;
USART_InitTypeDef Init;
struct rt_serial_device serial;
};
int rt_hw_usart_init(void);
#endif