Browse Source

完善CH32V307的串口BSP (#6359)

* Update drv_usart.h

CH32V307的串口1外设是对接在APB2桥上,其他串口均对接在APB1桥上

已完全测试,并发现一个问题,已修正。
self-confident neko 2 years ago
parent
commit
6da3b2b4b6

+ 80 - 11
bsp/wch/risc-v/Libraries/ch32_drivers/drv_usart.c

@@ -59,7 +59,7 @@ 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,
+        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,
@@ -68,25 +68,81 @@ static struct ch32_uart_hw_config uart_hw_config[] =
     },
 #endif
 #ifdef BSP_USING_UART2
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOA, GPIO_Pin_2, /* Tx */GPIOA, GPIO_Pin_3, /* 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_UART3
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOB,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOB, GPIO_Pin_10, /* Tx */GPIOB, GPIO_Pin_11, /* 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_UART4
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOC, GPIO_Pin_10, /* Tx */GPIOC, GPIO_Pin_11, /* 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_UART5
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOC, GPIO_Pin_12, /* Tx */GPIOD, GPIO_Pin_2, /* 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_UART6
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_UART6, RCC_APB2Periph_GPIOC,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOC, GPIO_Pin_0, /* Tx */GPIOC, GPIO_Pin_1, /* 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_UART7
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_UART7, RCC_APB2Periph_GPIOC,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOC, GPIO_Pin_2, /* Tx */GPIOC, GPIO_Pin_3, /* 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_UART8
-    {},
+    {
+        /* clock configuration, please refer to ch32v30x_rcc.h */
+        RCC_APB1Periph_UART8, RCC_APB2Periph_GPIOC,
+        /* GPIO  configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
+        GPIOC, GPIO_Pin_4, /* Tx */GPIOC, GPIO_Pin_5, /* 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
 };
 
@@ -208,7 +264,15 @@ static rt_err_t ch32_configure(struct rt_serial_device *serial, struct serial_co
     }
 
     /* UART hardware configuration, including clock and GPIO, etc. */
-    RCC_APB2PeriphClockCmd(uart->hw_config->periph_clock, ENABLE);
+    RCC_APB2PeriphClockCmd(uart->hw_config->gpio_periph_clock, ENABLE);
+    if(uart->config->Instance == USART1)
+    {
+        RCC_APB2PeriphClockCmd(uart->hw_config->uart_periph_clock, ENABLE);
+    }
+    else
+    {
+        RCC_APB1PeriphClockCmd(uart->hw_config->uart_periph_clock, ENABLE);
+    }
 
     if(uart->hw_config->remap != GPIO_Remap_NONE)
     {
@@ -415,9 +479,14 @@ int rt_hw_usart_init(void)
         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)
+        if(uart_obj[i].hw_config->gpio_periph_clock == 0)
+        {
+            LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
+            continue;
+        }
+        if(uart_obj[i].hw_config->uart_periph_clock == 0)
         {
-            LOG_E("You did not perform hardware initialization for %s", uart->config->name);
+            LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
             continue;
         }
         /* register UART device */

+ 2 - 1
bsp/wch/risc-v/Libraries/ch32_drivers/drv_usart.h

@@ -20,7 +20,8 @@
 /* ch32 hardware config class */
 struct ch32_uart_hw_config
 {
-    rt_uint32_t periph_clock;
+    rt_uint32_t uart_periph_clock;
+    rt_uint32_t gpio_periph_clock;
     GPIO_TypeDef *tx_gpio_port;
     rt_uint16_t tx_gpio_pin;
     GPIO_TypeDef *rx_gpio_port;