generated from hulk/gd32e23x_template
Compare commits
8 Commits
feature-ul
...
feature-ti
Author | SHA1 | Date | |
---|---|---|---|
e5f3bf900a | |||
3f75e99553 | |||
f67baed8c5 | |||
ac74c30923 | |||
9f615ba3a3 | |||
705cb68764 | |||
cc4d898114 | |||
f8a41af344 |
@@ -6,13 +6,13 @@ set(PROJECT_NAME "XLSW_3DP_US-IR")
|
|||||||
project(${PROJECT_NAME})
|
project(${PROJECT_NAME})
|
||||||
|
|
||||||
set(VERSION_MAJOR 0)
|
set(VERSION_MAJOR 0)
|
||||||
set(VERSION_MINOR 2)
|
set(VERSION_MINOR 1)
|
||||||
set(VERSION_PATCH 0)
|
set(VERSION_PATCH 0)
|
||||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
||||||
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
|
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
|
||||||
|
|
||||||
# Options 1
|
# Options 1
|
||||||
set(OPT1 "_[12V]")
|
set(OPT1 "_[24V]")
|
||||||
#set(OPT1 "_[SW_IIC]")
|
#set(OPT1 "_[SW_IIC]")
|
||||||
|
|
||||||
# Options 2
|
# Options 2
|
||||||
|
79
CommunicationProtocol.md
Normal file
79
CommunicationProtocol.md
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
# US&IR传感器模块通信协议
|
||||||
|
|
||||||
|
## US&IR传感器模块通信协议
|
||||||
|
|
||||||
|
| **序号** | **修改内容** | **版本** | **日期** | **修改人** |
|
||||||
|
|:------:|:--------:|:------:|:----------:|:-------:|
|
||||||
|
| 1 | 初版 | V0.1 | 2025-01-20 | Hulk |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
| | | | | |
|
||||||
|
|
||||||
|
### 发包格式
|
||||||
|
|
||||||
|
| **包头** | **类型** | **数据长度** | **数据** | **校验** |
|
||||||
|
|:------:|:------:|:-----------:|:------:|:------:|
|
||||||
|
| D5 | 0x04 | Data Length | Data | CRC |
|
||||||
|
|
||||||
|
- 数据长度只包含数据部分,不包含包头、类型、数据长度、校验
|
||||||
|
- CRC求和校验,包含类型、数据长度、数据
|
||||||
|
- 数据部分为ascii码
|
||||||
|
|
||||||
|
### 回包格式
|
||||||
|
|
||||||
|
| **包头** | **状态码** | **数据长度** | **数据** | **校验** |
|
||||||
|
|:------:|:----------:|:-----------:|:------:|:------:|
|
||||||
|
| B5 | 0xF0 正常包 | Data Length | Data | CRC |
|
||||||
|
| B5 | 0xF1 CRC错误 | Data Length | Data | CRC |
|
||||||
|
| B5 | 0xF2 包头错误 | Data Length | Data | CRC |
|
||||||
|
| B5 | 0xF3 类型错误 | Data Length | Data | CRC |
|
||||||
|
| B5 | 0xF4 包长度错误 | Data Length | Data | CRC |
|
||||||
|
|
||||||
|
- 数据长度只包含数据部分,不包含包头、类型、数据长度、校验
|
||||||
|
- CRC求和校验,包含状态码、数据长度、数据
|
||||||
|
- 有效数据部分为uint32_t,高字节在前
|
||||||
|
- 包错误和指令错误时,数据部分为ascii码 `err`
|
||||||
|
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
## US&IR传感器模块功能
|
||||||
|
|
||||||
|
### 1. 读取液面的超声距离
|
||||||
|
|
||||||
|
- 发送M1指令,读取距离液面的超声距离。
|
||||||
|
- `D5 04 02 4D 31 84`
|
||||||
|
- 电涡流传感器模块涡流回复数据
|
||||||
|
- `B5 F0 02 1D C0 CF`, 有效数据为 `0x1DC0`,转换为`7616`,距离为76.16mm
|
||||||
|
- `B5 F0 02 16 A5 AD`, 有效数据为 `0x16A5`,转换为`5797`,距离为57.97mm
|
||||||
|
- 错误命令(M3指令)回包
|
||||||
|
- `B5 F0 03 65 72 72 3C`,有效数据为 `err`
|
||||||
|
- CRC错误回包
|
||||||
|
- `B5 F1 03 65 72 72 3D`, 有效数据为 `err`
|
||||||
|
- 包头错误回包
|
||||||
|
- `B5 F2 03 65 72 72 3E`, 有效数据为 `err`
|
||||||
|
- 类型错误回包
|
||||||
|
- `B5 F3 03 65 72 72 3F`, 有效数据为 `err`
|
||||||
|
- 数据长度错误回包
|
||||||
|
- `B5 F4 03 65 72 72 40`, 有效数据为 `err`
|
||||||
|
|
||||||
|
### 2. 读取液面红外温度传感器数据
|
||||||
|
|
||||||
|
- 发送M2指令,读取液面红外温度传感器数据。
|
||||||
|
- `D5 04 02 4D 32 85`
|
||||||
|
- 电涡流传感器模块温度补偿回复数据
|
||||||
|
- `B5 F0 02 00 BD AF`, 有效数据为 `0x00BD`,转换为`189`(单位为摄氏度*10)温度为18.9℃
|
||||||
|
- `B5 F0 02 01 0C FF`, 有效数据为 `0x010C`,转换为`268`(单位为摄氏度*10)温度为26.8℃
|
||||||
|
- 错误命令(M3指令)回包
|
||||||
|
- `B5 F0 03 65 72 72 3C`,有效数据为 `err`
|
||||||
|
- CRC错误回包
|
||||||
|
- `B5 F1 03 65 72 72 3D`, 有效数据为 `err`
|
||||||
|
- 包头错误回包
|
||||||
|
- `B5 F2 03 65 72 72 3E`, 有效数据为 `err`
|
||||||
|
- 类型错误回包
|
||||||
|
- `B5 F3 03 65 72 72 3F`, 有效数据为 `err`
|
||||||
|
- 数据长度错误回包
|
||||||
|
- `B5 F4 03 65 72 72 40`, 有效数据为 `err`
|
||||||
|
|
||||||
|
### 3. 读取数据时间间隔
|
||||||
|
|
||||||
|
- 推荐数据时间间隔至少为1s
|
83
README.md
83
README.md
@@ -1,80 +1,9 @@
|
|||||||
# gd32e23x_template
|
# XLSW-3DP-Sensor-UltraSonic&IR
|
||||||
本项目为`GD32E230Fx`系列的基于Clion的CMake开发的工程模板。本人暂未入门,强行上强度,放弃keil,拥抱开源。遂尝试使用arm-none-eabi-gcc进行开发。
|
|
||||||
有幸寻得[@mo10 ](https://github.com/mo10)大佬的帮助,本项目的基础目录架构与CMakeLists.txt与toolchain.cmake均为大佬提供。
|
|
||||||
|
|
||||||
## 关于本项目
|
| **版本号** | **修改内容** | **日期** | **修改人** |
|
||||||
本项目默认的芯片型号为`GD32E230F4`,但是可以根据需要修改为其他型号,具体修改方法请参考下方`关于链接脚本`的说明。
|
|:-------:|:--------:|:----------:|:-------:|
|
||||||
### 版本号
|
| v0.0.1 | 非稳定版本 | 2025-01-20 | Hulk |
|
||||||
默认版本号为`0.0.1`,在`CMakeLists.txt`中修改`PROJECT_VERSION`即可。
|
|
||||||
### 项目名称
|
|
||||||
默认项目名称为`gd32e23x_template`,在`CMakeLists.txt`中修改`PROJECT_NAME`即可。请先修改项目名称再配置编译环境。
|
|
||||||
### 软件IIC与硬件IIC
|
|
||||||
本项目中提供了软件IIC与硬件IIC的驱动,但是默认使用硬件IIC,如果需要使用软件IIC,请在`board_config.h`中取消注释`// #define SOFTWARE_IIC`(line 8)。
|
|
||||||
### 编译选项
|
|
||||||
本项目预留了两个编译选项,`OPT1`与`OPT2`,默认均为空,请根据需要自行修改,例如`OPT1`为`_[HW_IIC]`,`OPT2`为`_[NO_LED]`。
|
|
||||||
`OPT1`与`OPT2`均在`CMakeLists.txt`中可修改。
|
|
||||||
### 关于编译日期
|
|
||||||
本项目在`CMakeLists.txt`中添加了编译日期。
|
|
||||||
|
|
||||||
### 关于led
|
## US&IR传感器模块通信协议
|
||||||
本项目默认开启了LED闪烁,并使用TIMER16进行定时。
|
|
||||||
|
|
||||||
|
通信协议:[US&IR通信协议](CommunicationProtocol.md)
|
||||||
## 关于C标准库的printf的重写
|
|
||||||
在Keil开发中,ARMClang有自己的microLIB,所以直接调用,然后重写fputc函数即可,但在gcc中需要重写`_write`函数,本项目模板中已经在`main.c`中完成重写。
|
|
||||||
同时需要添加`--spaces=nano.spaces`编译参数。
|
|
||||||
但是printf本身占用flash比较大,建议谨慎使用,尤其是本项目搭建时候采用的型号为`GD32E230F4V6`内存非常有限,重写后加上spaces设置,目前能用。
|
|
||||||
|
|
||||||
## 添加源文件与头文件
|
|
||||||
在`ProjectDir/CMakeLists.txt`中21行左右,添加对应源文件即可。
|
|
||||||
|
|
||||||
```cmake
|
|
||||||
set(TARGET_C_SRC
|
|
||||||
${CMAKE_SOURCE_DIR}/src/main.c
|
|
||||||
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c
|
|
||||||
${CMAKE_SOURCE_DIR}/src/systick.c
|
|
||||||
)
|
|
||||||
```
|
|
||||||
## 关于链接脚本
|
|
||||||
注意芯片选型, 不同型号的芯片 FLASH 和 RAM 大小不同。需要修改链接脚本`ld/gd32e23x_gcc.ld`
|
|
||||||
|
|
||||||
| 芯片型号 | FLASH | RAM |
|
|
||||||
|------------|-------|-----|
|
|
||||||
| GD32E230F4 | 16K | 4K |
|
|
||||||
| GD32E230F8 | 64K | 8K |
|
|
||||||
|
|
||||||
```linkerscript
|
|
||||||
/* memory map */
|
|
||||||
MEMORY
|
|
||||||
{
|
|
||||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
|
||||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 关于Startup文件
|
|
||||||
|
|
||||||
[@mo10 ](https://github.com/mo10)大佬提供的一些想法,具体如下。但是我目前还没测试到实际的影响和作用范围,所以暂未同步进来,仅作为备忘内容
|
|
||||||
```asm
|
|
||||||
Reset_Handler:
|
|
||||||
ldr r0, =_sp
|
|
||||||
mov sp, r0
|
|
||||||
ldr r0, =_end
|
|
||||||
msr msplim, r0
|
|
||||||
/* copy the data segment into ram */
|
|
||||||
movs r1, #0
|
|
||||||
b LoopCopyDataInit
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Ref
|
|
||||||
|
|
||||||
1. 参考LD/Startup
|
|
||||||
|
|
||||||
[https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/linker.ld](https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/linker.ld)
|
|
||||||
|
|
||||||
[https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/startup.s](https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/startup.s)
|
|
||||||
|
|
||||||
2. 官方LD/Startup
|
|
||||||
|
|
||||||
使用Embedded Builder工具生成的C标准库生成的模板
|
|
@@ -9,8 +9,23 @@
|
|||||||
|
|
||||||
// #define DEBUG_VERBOES
|
// #define DEBUG_VERBOES
|
||||||
|
|
||||||
#define POWER_SUPPLY_12V
|
// #define POWER_SUPPLY_12V
|
||||||
// #define POWER_SUPPLY_24V
|
#define POWER_SUPPLY_24V
|
||||||
|
|
||||||
|
// #define GD32E230F4
|
||||||
|
// #define GD32E230F8
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#ifdef GD32E230F8
|
||||||
|
#define USART_RCU RCU_USART1
|
||||||
|
#define USART_PHY USART1
|
||||||
|
#define USART_PHY_IRQ USART1_IRQn
|
||||||
|
#else
|
||||||
|
#define USART_RCU RCU_USART0
|
||||||
|
#define USART_PHY USART0
|
||||||
|
#define USART_PHY_IRQ USART0_IRQn
|
||||||
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
@@ -26,7 +41,6 @@
|
|||||||
#error "Please define either POWER_SUPPLY_12V or POWER_SUPPLY_24V"
|
#error "Please define either POWER_SUPPLY_12V or POWER_SUPPLY_24V"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_GPIO_RCU RCU_GPIOF
|
#define I2C_GPIO_RCU RCU_GPIOF
|
||||||
@@ -41,13 +55,14 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
#define USART_GPIO_RCU RCU_GPIOA
|
#define USART_GPIO_RCU RCU_GPIOA
|
||||||
#define USART_RCU RCU_USART0
|
// #define USART_RCU RCU_USART0
|
||||||
#define USART_GPIO_PORT GPIOA
|
#define USART_GPIO_PORT GPIOA
|
||||||
#define USART_GPIO_AF GPIO_AF_1
|
#define USART_GPIO_AF GPIO_AF_1
|
||||||
#define USART_TX_PIN GPIO_PIN_2
|
#define USART_TX_PIN GPIO_PIN_2
|
||||||
#define USART_RX_PIN GPIO_PIN_3
|
#define USART_RX_PIN GPIO_PIN_3
|
||||||
#define USART_PHY USART0
|
// #define USART_PHY USART0
|
||||||
#define USART_PHY_BAUDRATE 115200U
|
#define USART_PHY_BAUDRATE 115200U
|
||||||
|
// #define USART_PHY_IRQ USART0_IRQn
|
||||||
#define RS485_EN_PORT GPIOA
|
#define RS485_EN_PORT GPIOA
|
||||||
#define RS485_EN_PIN GPIO_PIN_4
|
#define RS485_EN_PIN GPIO_PIN_4
|
||||||
|
|
||||||
|
@@ -44,7 +44,6 @@ OF SUCH DAMAGE.
|
|||||||
#include "fwdgt.h"
|
#include "fwdgt.h"
|
||||||
#include "board_config.h"
|
#include "board_config.h"
|
||||||
#include "gd60914.h"
|
#include "gd60914.h"
|
||||||
#include "ultrasonic_analog.h"
|
|
||||||
|
|
||||||
#ifdef SOFTWARE_IIC
|
#ifdef SOFTWARE_IIC
|
||||||
#include "soft_i2c.h"
|
#include "soft_i2c.h"
|
||||||
|
@@ -15,7 +15,7 @@ void watchdog_init(void) {
|
|||||||
rcu_osci_stab_wait(RCU_IRC40K);
|
rcu_osci_stab_wait(RCU_IRC40K);
|
||||||
|
|
||||||
/* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
|
/* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
|
||||||
fwdgt_config(625, FWDGT_PSC_DIV256); // Set timeout to 1 seconds (625 / 0.625 KHz)
|
fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
|
||||||
|
|
||||||
/* Enable FWDGT */
|
/* Enable FWDGT */
|
||||||
fwdgt_enable();
|
fwdgt_enable();
|
||||||
|
20
src/main.c
20
src/main.c
@@ -27,29 +27,23 @@ int main(void)
|
|||||||
/* configure FWDGT */
|
/* configure FWDGT */
|
||||||
watchdog_init();
|
watchdog_init();
|
||||||
|
|
||||||
// #ifdef SOFTWARE_IIC
|
#ifdef SOFTWARE_IIC
|
||||||
// soft_i2c_config();
|
soft_i2c_config();
|
||||||
// #else
|
#else
|
||||||
// i2c_config();
|
i2c_config();
|
||||||
// #endif
|
#endif
|
||||||
|
|
||||||
printf("system start!\r\n");
|
printf("system start!\r\n");
|
||||||
|
|
||||||
ultrasonic_config();
|
ultrasonic_config();
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
// gd60914_get_object_tempture();
|
gd60914_get_object_tempture();
|
||||||
|
|
||||||
delay_ms(500);
|
delay_ms(50);
|
||||||
|
|
||||||
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||||
|
|
||||||
delay_ms(10);
|
|
||||||
|
|
||||||
uint16_t distance = ultrasonic_calc_distance();
|
|
||||||
|
|
||||||
printf("%u\r\n", distance);
|
|
||||||
|
|
||||||
watchdog_reload();
|
watchdog_reload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,12 +26,10 @@ void usart_config(void)
|
|||||||
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
|
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
|
||||||
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
||||||
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
||||||
|
nvic_irq_enable(USART_PHY_IRQ, 0);
|
||||||
usart_enable(USART_PHY);
|
|
||||||
|
|
||||||
nvic_irq_enable(USART0_IRQn, 0);
|
|
||||||
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
|
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
|
||||||
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
|
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
|
||||||
|
usart_enable(USART_PHY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user