From 575abd58ff26dd0ddea3af8c86a5b19e05a9b502 Mon Sep 17 00:00:00 2001
From: flyingcys <flyingcys@163.com>
Date: Sat, 25 Mar 2023 19:05:33 +0800
Subject: [PATCH] [bsp]add bl808 uart2 driver && add bl60x/bl70x bsp to
 ci-action (#7097)

* update bl_mcu_sdk SConscript && add bl808 uart2 driver

* add bl60x/bl70x bsp to ci-action
---
 .github/workflows/action.yml                  |  2 +
 bsp/bouffalo_lab/bl60x/board/SConscript       |  2 +-
 bsp/bouffalo_lab/bl61x/board/SConscript       |  2 +-
 bsp/bouffalo_lab/bl70x/board/SConscript       |  2 +-
 bsp/bouffalo_lab/bl808/lp/board/SConscript    |  2 +-
 bsp/bouffalo_lab/bl808/m0/board/SConscript    |  2 +-
 bsp/bouffalo_lab/bl808/m0/board/board.c       | 10 ----
 .../bl_mcu_sdk/drivers/lhal/SConscript        |  3 +-
 .../drivers/lhal/src/bflb_ef_ctrl.c           |  2 +-
 .../bl_mcu_sdk/drivers/lhal/src/bflb_flash.c  |  2 +-
 .../drivers/lhal/src/flash/bflb_sf_ctrl.c     |  2 +-
 bsp/bouffalo_lab/libraries/rt_drivers/Kconfig | 36 +++++++++++++++
 .../libraries/rt_drivers/drv_uart.c           | 46 ++++++++++++++++++-
 13 files changed, 93 insertions(+), 20 deletions(-)

diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml
index 8c4940b131..3bf4d8bafc 100644
--- a/.github/workflows/action.yml
+++ b/.github/workflows/action.yml
@@ -246,6 +246,8 @@ jobs:
          - {RTT_BSP: "microchip/samc21", RTT_TOOL_CHAIN: "sourcery-arm"}
          - {RTT_BSP: "microchip/same54", RTT_TOOL_CHAIN: "sourcery-arm"}
          - {RTT_BSP: "microchip/same70", RTT_TOOL_CHAIN: "sourcery-arm"}
+         - {RTT_BSP: "bouffalo_lab/bl60x", RTT_TOOL_CHAIN: "sourcery-riscv64-unknown-elf"}
+         - {RTT_BSP: "bouffalo_lab/bl70x", RTT_TOOL_CHAIN: "sourcery-riscv64-unknown-elf"}
     steps:
       - uses: actions/checkout@v3
       - name: Set up Python
diff --git a/bsp/bouffalo_lab/bl60x/board/SConscript b/bsp/bouffalo_lab/bl60x/board/SConscript
index c0653a14cc..0907feabe2 100755
--- a/bsp/bouffalo_lab/bl60x/board/SConscript
+++ b/bsp/bouffalo_lab/bl60x/board/SConscript
@@ -4,6 +4,6 @@ cwd     = GetCurrentDir()
 src     = Glob('*.c') + Glob('*.cpp') + Glob('*.S')
 CPPPATH = [cwd]
 
-group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
+group = DefineGroup('Board', src, depend = [''], CPPPATH = CPPPATH)
 
 Return('group')
diff --git a/bsp/bouffalo_lab/bl61x/board/SConscript b/bsp/bouffalo_lab/bl61x/board/SConscript
index c0653a14cc..0907feabe2 100755
--- a/bsp/bouffalo_lab/bl61x/board/SConscript
+++ b/bsp/bouffalo_lab/bl61x/board/SConscript
@@ -4,6 +4,6 @@ cwd     = GetCurrentDir()
 src     = Glob('*.c') + Glob('*.cpp') + Glob('*.S')
 CPPPATH = [cwd]
 
-group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
+group = DefineGroup('Board', src, depend = [''], CPPPATH = CPPPATH)
 
 Return('group')
diff --git a/bsp/bouffalo_lab/bl70x/board/SConscript b/bsp/bouffalo_lab/bl70x/board/SConscript
index c0653a14cc..0907feabe2 100755
--- a/bsp/bouffalo_lab/bl70x/board/SConscript
+++ b/bsp/bouffalo_lab/bl70x/board/SConscript
@@ -4,6 +4,6 @@ cwd     = GetCurrentDir()
 src     = Glob('*.c') + Glob('*.cpp') + Glob('*.S')
 CPPPATH = [cwd]
 
-group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
+group = DefineGroup('Board', src, depend = [''], CPPPATH = CPPPATH)
 
 Return('group')
diff --git a/bsp/bouffalo_lab/bl808/lp/board/SConscript b/bsp/bouffalo_lab/bl808/lp/board/SConscript
index c0653a14cc..0907feabe2 100755
--- a/bsp/bouffalo_lab/bl808/lp/board/SConscript
+++ b/bsp/bouffalo_lab/bl808/lp/board/SConscript
@@ -4,6 +4,6 @@ cwd     = GetCurrentDir()
 src     = Glob('*.c') + Glob('*.cpp') + Glob('*.S')
 CPPPATH = [cwd]
 
-group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
+group = DefineGroup('Board', src, depend = [''], CPPPATH = CPPPATH)
 
 Return('group')
diff --git a/bsp/bouffalo_lab/bl808/m0/board/SConscript b/bsp/bouffalo_lab/bl808/m0/board/SConscript
index c0653a14cc..0907feabe2 100755
--- a/bsp/bouffalo_lab/bl808/m0/board/SConscript
+++ b/bsp/bouffalo_lab/bl808/m0/board/SConscript
@@ -4,6 +4,6 @@ cwd     = GetCurrentDir()
 src     = Glob('*.c') + Glob('*.cpp') + Glob('*.S')
 CPPPATH = [cwd]
 
-group = DefineGroup('Applications', src, depend = [''], CPPPATH = CPPPATH)
+group = DefineGroup('Board', src, depend = [''], CPPPATH = CPPPATH)
 
 Return('group')
diff --git a/bsp/bouffalo_lab/bl808/m0/board/board.c b/bsp/bouffalo_lab/bl808/m0/board/board.c
index 5494c69eaf..d8545b54e4 100644
--- a/bsp/bouffalo_lab/bl808/m0/board/board.c
+++ b/bsp/bouffalo_lab/bl808/m0/board/board.c
@@ -175,16 +175,6 @@ void rt_hw_board_init(void)
     }
 #endif
 
-    /* GPIO driver initialization is open by default */
-#ifdef BSP_USING_PIN
-    rt_hw_pin_init();
-#endif
-
-    /* I2C driver initialization is open by default */
-#ifdef BSP_USING_I2C
-    rt_hw_i2c_init();
-#endif
-
     /* Set the shell console output device */
 #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
     rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
diff --git a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/SConscript b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/SConscript
index 9eacf7f39f..f9dc4f5d67 100755
--- a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/SConscript
+++ b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/SConscript
@@ -4,7 +4,8 @@ from building import *
 
 cwd     = GetCurrentDir()
 
-CPPPATH = [cwd + r'/include',
+CPPPATH = [cwd,
+        cwd + r'/include',
         cwd + r'/include/arch',
         cwd + r'/include/arch/risc-v/t-head',
         cwd + r'/include/arch/risc-v/t-head/Core/Include',
diff --git a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_ef_ctrl.c b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_ef_ctrl.c
index 3e858abcdd..05689ec501 100755
--- a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_ef_ctrl.c
+++ b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_ef_ctrl.c
@@ -1,5 +1,5 @@
 #include "bflb_ef_ctrl.h"
-#include "hardware/ef_ctrl_reg.h"
+#include "include/hardware/ef_ctrl_reg.h"
 
 #if defined(BL602) || defined(BL702) || defined(BL702L)
 #define BFLB_EF_CTRL_BASE ((uint32_t)0x40007000)
diff --git a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_flash.c b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_flash.c
index 6229cfeb9a..4a4ba6338b 100755
--- a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_flash.c
+++ b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/bflb_flash.c
@@ -25,7 +25,7 @@
 #include "bflb_xip_sflash.h"
 #include "bflb_sf_cfg.h"
 #include "bflb_flash.h"
-#include "hardware/sf_ctrl_reg.h"
+#include "include/hardware/sf_ctrl_reg.h"
 
 #if defined(BL616)
 static uint32_t flash1_size = 4 * 1024 * 1024;
diff --git a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/flash/bflb_sf_ctrl.c b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/flash/bflb_sf_ctrl.c
index 2996a65695..cc2fdcee38 100755
--- a/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/flash/bflb_sf_ctrl.c
+++ b/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/src/flash/bflb_sf_ctrl.c
@@ -35,7 +35,7 @@
   */
 
 #include "bflb_sf_ctrl.h"
-#include "hardware/sf_ctrl_reg.h"
+#include "include/hardware/sf_ctrl_reg.h"
 #if defined(BL602) || defined(BL702) || defined(BL702L)
 #include <risc-v/e24/clic.h>
 #else
diff --git a/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig b/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig
index aae14d8fa4..1019c82cba 100755
--- a/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig
+++ b/bsp/bouffalo_lab/libraries/rt_drivers/Kconfig
@@ -83,6 +83,42 @@ menu "General Drivers Configuration"
             endchoice
         endif
 
+    if BSP_USING_BL808
+        menuconfig BSP_USING_UART2
+            bool "Enable UART2"
+            default n
+            if BSP_USING_UART2
+                choice
+                    prompt "UART2 TX PIN"
+                    default UART2_TX_USING_GPIO20
+
+                    config UART2_TX_USING_GPIO4
+                        bool "GPIO_4"
+                    config UART2_TX_USING_GPIO16
+                        bool "GPIO_16"
+                    config UART2_TX_USING_GPIO18
+                        bool "GPIO_18"
+                    config UART2_TX_USING_GPIO20
+                        bool "GPIO_20"
+                endchoice
+                choice
+                    prompt "UART2 RX PIN"
+                    default UART2_RX_USING_GPIO21
+
+                    config UART2_RX_USING_GPIO3
+                        bool "GPIO_3"
+                    config UART2_RX_USING_GPIO5
+                        bool "GPIO_5"
+                    config UART2_RX_USING_GPIO17
+                        bool "GPIO_17"
+                    config UART2_RX_USING_GPIO19
+                        bool "GPIO_19"
+                    config UART2_RX_USING_GPIO21
+                        bool "GPIO_21"
+                endchoice
+            endif
+        endif
+
     endmenu
 
 endmenu
diff --git a/bsp/bouffalo_lab/libraries/rt_drivers/drv_uart.c b/bsp/bouffalo_lab/libraries/rt_drivers/drv_uart.c
index 5a4d8e958f..4bd3c1cb24 100755
--- a/bsp/bouffalo_lab/libraries/rt_drivers/drv_uart.c
+++ b/bsp/bouffalo_lab/libraries/rt_drivers/drv_uart.c
@@ -63,7 +63,28 @@
 #define UART1_GPIO_RX           GPIO_PIN_27
 #endif
 
-static struct bflb_device_s    *gpio;
+// uart2
+#ifdef UART2_TX_USING_GPIO4
+#define UART2_GPIO_TX           GPIO_PIN_4
+#elif defined(UART2_TX_USING_GPIO16)
+#define UART2_GPIO_TX           GPIO_PIN_16
+#elif defined(UART2_TX_USING_GPIO18)
+#define UART2_GPIO_TX           GPIO_PIN_18
+#elif defined(UART2_TX_USING_GPIO20)
+#define UART2_GPIO_TX           GPIO_PIN_20
+#endif
+
+#ifdef UART2_RX_USING_GPIO3
+#define UART2_GPIO_RX           GPIO_PIN_3
+#elif defined(UART2_RX_USING_GPIO5)
+#define UART2_GPIO_RX           GPIO_PIN_5
+#elif defined(UART2_RX_USING_GPIO17)
+#define UART2_GPIO_RX           GPIO_PIN_17
+#elif defined(UART2_RX_USING_GPIO19)
+#define UART2_GPIO_RX           GPIO_PIN_19
+#elif defined(UART2_RX_USING_GPIO21)
+#define UART2_GPIO_RX           GPIO_PIN_21
+#endif
 
 struct device_uart
 {
@@ -250,6 +271,7 @@ static const struct rt_uart_ops _uart_ops =
 int rt_hw_uart_init(void)
 {
     rt_err_t result = 0;
+    struct bflb_device_s    *gpio;
 
     struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
     struct rt_serial_device *serial;
@@ -301,5 +323,27 @@ int rt_hw_uart_init(void)
                                     uart);
     RT_ASSERT(result == RT_EOK);
 #endif
+
+#ifdef BSP_USING_UART2
+    static struct device_uart bl_uart2;
+
+    serial  = &bl_uart2.serial;
+    uart    = &bl_uart2;
+
+    serial->ops              = &_uart_ops;
+    serial->config           = config;
+    serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
+
+    uart->bflb_device = bflb_device_get_by_name("uart2");
+    bflb_gpio_uart_init(gpio, UART2_GPIO_TX, GPIO_UART_FUNC_UART2_TX);
+    bflb_gpio_uart_init(gpio, UART2_GPIO_RX, GPIO_UART_FUNC_UART2_RX);
+
+    /* register USART device */
+    result = rt_hw_serial_register(serial,
+                                    "uart2",
+                                    RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
+                                    uart);
+    RT_ASSERT(result == RT_EOK);
+#endif
     return 0;
 }