Add README.md files in ble, can, ethernet, and rs485 in ch32v208rbt6

This commit is contained in:
huoyujia081 2024-06-13 17:28:26 +08:00
parent 700ce53b50
commit be85a2a903
46 changed files with 385 additions and 44 deletions

View File

@ -1,58 +1,181 @@
# CH32V307VCT6-board README # 1. 简介
## 1. 简介 | 硬件 | 描述 |
| --------- | ------------------------------------------------------------ |
| 芯片型号 | CH32V208RBT6 |
| CPU | 单核RISC-V4C |
| 主频 | 144MHz |
| 片内SRAM | 可配32KB、48KB、64KB |
| 片内FLASH | 可配160KB、144KB、128KB |
| 外设 | 2Mbps低功耗蓝牙BLE通讯模块、10M以太网MAC+PHY模块、USB2.0全速设备+主机/设备接口、CAN控制器等 |
| 硬件 | 描述 | # 2. 克隆代码
| -- | -- |
|芯片型号| ch32v307 |
|CPU| 单核RV32GC |
|主频| 144MHz |
|片内SRAM| 可配最大128KB |
| 外设 | |
| | GPIO、UART|
## 2. 编译说明 将XiUOS的源代码克隆下来
编译环境Ubuntu18.04 ```bash
git clone https://gitlink.org.cn/xuos/xiuos.git
编译工具链riscv-none-embed-gcc ( xPack GNU RISC-V Embedded GCC)
**xPack RISC-V Embedded GCC** 工具链可以使用[xpm](https://www.npmjs.com/package/xpm)工具进行安装:
```console
$ xpm install --global @xpack-dev-tools/riscv-none-embed-gcc@8.2.0-3.1.1
``` ```
编译步骤: # 3. 下载编译工具链
>1.将编译工具链的路径添加到board/ch32v307vct6/config.mk文件当中例如将xpack-riscv-none-elf-gcc解压到/opt/下时添加: 编译环境Ubuntu 20.04.6 LTS
```
export CROSS_COMPILE ?=/opt/riscv-embedded-gcc/bin/riscv-none-embed- 编译工具链riscv-none-elf-gccxpack-riscv-none-elf-gcc-11.3.0-1
```
>2.在代码根目录下执行以下命令,生成配置文件 编译工具链可到Github进行下载https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases
```
cd ./Ubiquitous/XiZi ![image-20240509155858504](imgs/image-20240509155858504.png)
make BOARD=ch32v307vct6 distclean
make BOARD=ch32v307vct6 menuconfig 下载完成后将其移动到`/opt`目录下,并进行解压:
```
>3.在menuconfig界面配置需要关闭和开启的功能按回车键进入下级菜单按Y键选中需要开启的功能按N键选中需要关闭的功能配置结束后选择Exit保存并退出 ```bash
>4.继续执行以下命令,进行编译 sudo tar -xvzf xpack-riscv-none-elf-gcc-11.3.0-1-linux-x64.tar.gz
```
make BOARD=ch32v307vct6
```
>5.如果编译正确无误会产生XiZi-ch32v307vct6.elf、XiZi-ch32v307vct6.bin文件。其中XiZi-ch32v307vct6.bin需要烧写到设备中进行运行。
>注:最后可以执行以下命令,清除配置文件和编译生成的文件
```
make BOARD=ch32v307vct6 distclean
``` ```
## 3. 烧写及执行 # 4. 编译
开发板可使用数据线通过USB接口与电脑连接将开发板的Boot0接口通电连接VCC后按动ReSet按钮即可使用烧录工具[WCHISPTool](https://www.wch.cn/download/WCHISPTool_Setup_exe.html)进行烧录。![board_settng](img/board.jpg)烧录工具的配置如下图所示。![WCHISPTool配置](img/WCHIPSTool_setting.PNG) 1. 设置`CROSS_COMPILE`环境变量,以下为将编译工具链解压到`/opt`目录下时的命令:
烧录完成后可通过串口连接PC电脑与开发版TX(PA9)接串口RX、RX(PA10)接串口TX。![](img/serial.jpg) ```bash
export CROSS_COMPILE=/opt/xpack-riscv-none-elf-gcc-11.3.0-1/bin/riscv-none-elf-
```
### 3.1 运行结果 2. 在`./Ubiquitous/XiZi_IIoT`目录下执行命令:
如果编译 & 烧写无误将开发板BOOT引脚与VCC引脚断开后重新上电或将RESET引脚短接一下将会在串口终端上看到信息打印输出。 ```bash
make BOARD=ch32v208rbt6 distclean # 将之前的编译生成文件清空
make BOARD=ch32v208rbt6 menuconfig # 进行编译设置
```
![terminal](img/terminal.PNG) 如果`make BOARD=ch32v208rbt6 menuconfig`显示【无法找到`kconfig-mconf`】,需要先安装`ncurses-devel`和`kconfig-mconf`,如下:
```bash
sudo apt install libncurses5-dev kconfig-frontends
```
3. 在`menuconfig`配置界面选择要增加的功能。按回车键进入下级菜单按Y键选中需要开启的功能按N键选中需要关闭的功能配置结束后选择Exit保存并退出完成后会生成`.config`文件。
4. 进行编译:
```bash
make BOARD=ch32v208rbt6
```
5. 编译完成后会生成`XiZi-ch32v208rbt6.bin`文件将该文件拷贝至Windows侧待下一步进行烧录。
# 5. 端口连接
`ch32v208rbt6`需要使用`WCH-LINK`模块进行串口连接,各个端口的连接如下:
| 接口组别 | ch32v208rbt6 | WCH-LINK |
| -------------- | ------------ | -------- |
| J2编程接口 | GND | GND |
| J2编程接口 | DIO | SWDIO |
| J2编程接口 | CLK | SWCLK |
| J2编程接口 | 3V3 | 3V3 |
| J3通信接口 | GND | GND |
| J3通信接口 | TX | RX |
| J3通信接口 | RX | TX |
连接后如下:
![cb4d033a1ed826e9cee7acbdc225fab5](imgs/cb4d033a1ed826e9cee7acbdc225fab5.jpeg)
# 6. WCH-LINK模块固件更新可选
如WCH-LINK模块蓝灯闪烁代表其固件缺失需要进行固件烧录或者更新。
可以使用[MounRiver Studio](http://www.mounriver.com/download)在线更新。更新步骤如下:
1. 将WCH-LINK连接至电脑。
2. 打开MounRiver Studio点击`download`图标右边的向下箭头,点击`configuration`,如下所示:
![image-20240509163525255](imgs/image-20240509163525255.png)
3. 显示窗口后,点击任意`query`按钮即可进行固件更新,如下:
<img src="imgs/image-20240509163753634.png" alt="image-20240509163753634" style="zoom:67%;" />
4. 待WCH-LINK模块红灯亮起表示固件更新完成。
# 7. 烧录
1. 下载`WCH-LinkUtility`工具https://www.wch.cn/downloads/WCH-LinkUtility_ZIP.html
2. ch32v208rbt6连接电源线如果电源线只有两根保证`IN`和其中一个`GND`接地即可),如下:
<img src="imgs/5B75D386B81B5235B2A6274534359E34.png" alt="5B75D386B81B5235B2A6274534359E34" style="zoom:67%;" />
3. 将WCH-LINK连接至电脑设备管理器显示`WCH-LinkRV`表示连接成功,如下:
<img src="imgs/image-20240509164821442.png" alt="image-20240509164821442" style="zoom:67%;" />
4. 打开烧录软件WCH-LinkUtility如下
<img src="imgs/image-20240509165100764.png" alt="image-20240509165100764" style="zoom:67%;" />
1连接 WCH-Link。
2选择芯片信息`MCU Core`选择`RISC-V``Series`选择`CH32V20X`。
3添加固件即[2. 编译](# 2. 编译)中编译好的`XiZi-ch32v208rbt6.bin`文件。
4设置配置若芯片为读保护需解除芯片读保护。
5执行。
## 7.1 指定ROM和RAM大小
> 参考资料:
>
> [1]https://www.cnblogs.com/gscw/p/17286307.html
> [!caution]
>
> WCH-LinkUtility中需要指定ROM和RAM大小如果大小与源文件`link.ld`中的ROM和RAM大小不一致可能会导致XiZi启动失败。
在WCH_LinkUtility中在以下位置指定ROM和RAM大小
<img src="imgs/image-20240613144008865.png" alt="image-20240613144008865" style="zoom:50%;" />
其中set按钮用于设置CH32V208的ROM和RAM大小get按钮用于获取CH32V208的当前ROM和RAM大小。
在`link.ld`中在以下代码位置配置ROM和RAM大小
```c
/* CH32V20x_D8 - CH32V203RB
CH32V20x_D8W - CH32V208x
FLASH + RAM supports the following configuration
FLASH-128K + RAM-64K
FLASH-144K + RAM-48K
FLASH-160K + RAM-32K
*/
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 448K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
```
以下是测试成功的RAM和ROM大小配置如果编译时显示RAM或者ROM不足则需要调整RAM或者ROM。推荐使用下面的慢速FLASH配置。
1. 仅使用快速FLASH
| link.ld | WCH-LinkUtility |
| ------------------------- | ------------------ |
| 128KFLASH+ 64KRAM | 128K ROM + 64K RAM |
| 144KFLASH+ 48KRAM | 144K ROM + 48K RAM |
| 160KFLASH+ 32KRAM | 160K ROM + 32K RAM |
2. 使用慢速FLASH
| link.ld | WCH-LinkUtility |
| ------------------------- | ------------------ |
| 448KFLASH+ 64KRAM | 128K ROM + 64K RAM |
# 8. 启动
烧录完成后并且将WCH-LINK连接至电脑串口。
按下ch32v208rbt6板上的`RST`按钮,即可看到操作系统启动的信息,如下:
![image-20240509165901360](imgs/image-20240509165901360.png)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

View File

@ -0,0 +1,52 @@
# 1. 特殊配置
CH32V208支持2Mbps低功耗蓝牙BLE通讯模块。
在进行正常程序编译烧录的过程中,需要进行以下特殊配置,其他步骤保持一致。
## 1.1 menuconfig
在menuconfig配置界面中根据以下路径选择
`ch32v208rbt6 feature`->勾选`Using BLE`,如下所示:
![image-20240613140451994](imgs/image-20240613140451994.png)
## 1.2 烧录
在首次将包含蓝牙的程序烧录进CH32V208时在WCH-LinkUtility烧录工具中需要勾选`Erase All`选项,如下所示,此后烧录则无需勾选该选项:
![image-20240613140951925](imgs/image-20240613140951925.png)
## 1.3 ROM和RAM大小设置
由于蓝牙源文件中有静态库源文件的ROM大小可能会超过128K因此最好启用慢速FLASH。详见`Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/README.md`中的`7. 烧录`一节。
# 2. 连接蓝牙
启动系统完成后,使用`test_ble`命令启动蓝牙。如果在[1.2 烧录](#1.2 烧录)中是首次进行烧录,那么会看到以下内容:
![image-20240613141557338](imgs/image-20240613141557338.png)
此时蓝牙并没有启动成功需要按下CH32V208上的复位按钮待系统启动完成后再次输入`test_ble`命令,此时蓝牙才可以启动成功,如下所示:
![image-20240613141821767](imgs/image-20240613141821767.png)
> [!note]
>
> 此后烧录,如果在`WCH-LinkUtility`中没有勾选`Erase All`,那么可以一次启动蓝牙成功,无需复位。
此时,在手机任意蓝牙调试程序中,即可看到`wch_ble_uart`的蓝牙设备,如下:
<img src="imgs/image-20240613142109901.png" alt="image-20240613142109901" style="zoom:50%;" />
连接之后发送信息即可在串口调试中看到所发送的信息表示CH32V208成功收到蓝牙信息如下图所示
![image-20240613142439953](imgs/image-20240613142439953.png)
# 3. 断开蓝牙
目前,测试程序无法自行断开蓝牙,需要在手机上主动断开蓝牙。断开之后,系统程序回到命令行,如下:
![image-20240613142940868](imgs/image-20240613142940868.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@ -0,0 +1,55 @@
# 1. 特殊配置
## 1.1 CAN波特率配置
> 参考资料:
>
> [1]https://www.cnblogs.com/wchmcu/p/17546797.html
如果CAN波特率配置不正确导致CAN上位机无法识别波特率那么会导致CAN发送信息失败。
在源程序中CAN的计算公式为$CANbps=tpclk1/((TS1[3:0]+1+TS2[2:0]+1 +1)*(BPR[9:0]+1))$
源文件配置CAN波特率的位置为`Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/can/test/can_test.c`的`static int init_can(u8 tsjw, u8 tbs2, u8 tbs1, u16 brp, u8 mode)`方法。
初始波特率配置为:
```c
init_can(CAN_SJW_1tq, CAN_BS2_5tq, CAN_BS1_6tq, 20, CAN_Mode_Normal);
```
即$60M/((4+1+5+1+1))*(19+1))=250Kbps$
> [!note]
>
> 主频在`Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/User/system_ch32v20x.c`中配置需要注意tpclk1为主频的1/2因此主频如果设置为120MHz那么对应的tpclk1为60MHz。
## 1.2 menuconfig
在menuconfig配置界面中根据以下路径选择
`ch32v208rbt6 feature`->勾选`Using CAN device`,如下所示:
![image-20240613152126116](imgs/image-20240613152126116.png)
# 2. 连接USBCAN分析仪
此处测试使用的是广成USBCAN-II Pro+分析仪。
将CH32V208的CAN_H端口与分析仪CAN1的H端口相连将CH32V208的CAN_L端口与分析仪CAN1的L端口相连如下图所示
<img src="imgs/image-20240613153049516.png" alt="image-20240613153049516" style="zoom: 25%;" />
# 3. CAN总线发送信息
USBCAN上位机注意选择波特率为250K默认。注意关闭帧id过滤功能。
![image-20240613153336507](imgs/image-20240613153336507.png)
打开上位机之后在XiZi命令行输入`test_can`命令此时程序自动往CAN总线发送信息。如果发送信息成功命令行会显示以下内容
![image-20240613153729116](imgs/image-20240613153729116.png)
同时在USBCAN上位机显示接受CAN帧成功如下所示
![image-20240613153847722](imgs/image-20240613153847722.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

View File

@ -0,0 +1,79 @@
# 1. 特殊配置
## 1.1 IP地址配置
目前IP地址配置内容在代码中写死所在的源文件为`Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/ethernet/connect_ether.c`。
其内容可以自行修改:
```c
uint8_t IPAddr[4] = { 192, 168, 1, 10 }; // IP address
uint8_t GWIPAddr[4] = { 192, 168, 1, 1 }; // Gateway IP address
uint8_t IPMask[4] = { 255, 255, 255, 0 }; // subnet mask
uint8_t DESIP[4] = { 192, 168, 1, 100 }; // destination IP address
```
以上默认配置表示:
- CH32V208的IP地址为192.168.1.10
- 网关地址为192.168.1.1
- 子网掩码为255.255.255.0
- 目的地址为192.168.1.100`test_tcp_client`命令连接的服务器地址)
将CH32V208与电脑连接好网线并启动XiZi之后电脑的IP地址配置如下所示
<img src="imgs/image-20240613155912432.png" alt="image-20240613155912432" style="zoom: 67%;" />
## 1.2 ROM和RAM大小设置
由于以太网源文件中有静态库源文件的ROM大小可能会超过128K因此最好启用慢速FLASH。详见`Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/README.md`中的`7. 烧录`一节。
## 1.3 Menuconfig
在menuconfig配置界面中根据以下路径选择
`ch32v208rbt6 feature`->勾选`Using Ethernet`,如下所示:
![image-20240613172343662](imgs/image-20240613172343662.png)
# 2. ping
使用网线连接CH32V208和电脑并且启动XiZi操作系统后在命令行输入`ping <ip地址>`。如果CH32V208与电脑网络畅通会显示以下内容
![image-20240613161310960](imgs/image-20240613161310960.png)
如果CH32V208与电脑网络不畅通会显示以下内容使用错误IP地址测试
![image-20240613161426964](imgs/image-20240613161426964.png)
# 3. test_tcp_client
`test_tcp_client`命令测试CH32V208作为客户端连接服务器。
使用`TcpIpDebug`调试工具模拟服务器下载地址https://www.wch.cn/downloads/TcpIpDebug_exe.html
## 3.1 创建服务器
在电脑中打开TcpIpDebug工具后创建服务器IP地址和端口配置如下
![image-20240613162119335](imgs/image-20240613162119335.png)
## 3.2 启动服务器
右键点击刚刚创建的服务器,点击启动服务器,如下所示:
<img src="imgs/image-20240613162253358.png" alt="image-20240613162253358" style="zoom:50%;" />
## 3.3 连接服务器
在XiZi命令行中键入命令`test_tcp_client`,显示以下内容表示连接服务器成功:
![image-20240613162645796](imgs/image-20240613162645796.png)
在TcpIpDebug中可以模拟服务器向客户端发送信息发送之后在XiZi命令行会显示接收信息的长度并且向服务器会送同样的内容如下
![image-20240613163035203](imgs/image-20240613163035203.png)
![image-20240613163159837](imgs/image-20240613163159837.png)
测试源程序中没有主动断开连接的代码因此需要服务器主动断开之后源程序回到XiZi命令行输入。

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -0,0 +1,32 @@
RS-485只是电气信号接口有许多通信协议使用RS-485准位的电气信号但RS-485规格书本身没有提到通信速度、格式以及资料传输的通信协议。因此使用UART在RS485总线上传输数据。
# 1. 特殊配置
## 1.1 Menuconfig
在menuconfig配置界面中根据以下路径选择
`ch32v208rbt6 feature`->勾选`Using RS485`,如下所示:
![image-20240613172635753](imgs/image-20240613172635753.png)
# 2. 连接USB转RS485转换器
连接CH32V208上的`RS485_A`端口到USB转RS485转换器上的`A`端口连接CH32V208上的`RS485_B`端口到USB转RS485转换器上的`B`端口,如下图所示:
<img src="imgs/image-20240613165104624.png" alt="image-20240613165104624" style="zoom: 25%;" />
# 3. test_rs485
打开串口调试上位机并且启动XiZi操作系统后键入命令`test_rs485`此时在上位机会看到以下信息代表CH32V208在RS485总线上发送信息成功
![image-20240613170435850](imgs/image-20240613170435850.png)
同时在上位机中向RS485总线发送数据CH32V208也可以收到如下所示
![image-20240613170704386](imgs/image-20240613170704386.png)
接收达到10行信息后源程序会退出该命令回到命令行正常输入模式如下图所示
![image-20240613170902704](imgs/image-20240613170902704.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB