generated from hulk/gd32e23x_template
	Compare commits
	
		
			3 Commits
		
	
	
		
			feature-ti
			...
			a546f7bf83
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a546f7bf83 | |||
| 39d800cb1b | |||
| a5c1c857a9 | 
@@ -6,13 +6,13 @@ set(PROJECT_NAME "XLSW_3DP_US-IR")
 | 
			
		||||
project(${PROJECT_NAME})
 | 
			
		||||
 | 
			
		||||
set(VERSION_MAJOR 0)
 | 
			
		||||
set(VERSION_MINOR 1)
 | 
			
		||||
set(VERSION_MINOR 2)
 | 
			
		||||
set(VERSION_PATCH 0)
 | 
			
		||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
 | 
			
		||||
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
 | 
			
		||||
 | 
			
		||||
# Options 1
 | 
			
		||||
set(OPT1 "_[24V]")
 | 
			
		||||
set(OPT1 "_[12V]")
 | 
			
		||||
#set(OPT1 "_[SW_IIC]")
 | 
			
		||||
 | 
			
		||||
# Options 2
 | 
			
		||||
 
 | 
			
		||||
@@ -1,79 +0,0 @@
 | 
			
		||||
# 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,9 +1,80 @@
 | 
			
		||||
# XLSW-3DP-Sensor-UltraSonic&IR
 | 
			
		||||
# gd32e23x_template
 | 
			
		||||
本项目为`GD32E230Fx`系列的基于Clion的CMake开发的工程模板。本人暂未入门,强行上强度,放弃keil,拥抱开源。遂尝试使用arm-none-eabi-gcc进行开发。
 | 
			
		||||
有幸寻得[@mo10 ](https://github.com/mo10)大佬的帮助,本项目的基础目录架构与CMakeLists.txt与toolchain.cmake均为大佬提供。
 | 
			
		||||
 | 
			
		||||
| **版本号** | **修改内容** |   **日期**   | **修改人** |
 | 
			
		||||
|:-------:|:--------:|:----------:|:-------:|
 | 
			
		||||
| v0.0.1  |  非稳定版本   | 2025-01-20 | Hulk    |
 | 
			
		||||
## 关于本项目
 | 
			
		||||
本项目默认的芯片型号为`GD32E230F4`,但是可以根据需要修改为其他型号,具体修改方法请参考下方`关于链接脚本`的说明。
 | 
			
		||||
### 版本号
 | 
			
		||||
默认版本号为`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`中添加了编译日期。
 | 
			
		||||
 | 
			
		||||
## US&IR传感器模块通信协议
 | 
			
		||||
### 关于led
 | 
			
		||||
本项目默认开启了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标准库生成的模板
 | 
			
		||||
 
 | 
			
		||||
@@ -91,15 +91,15 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in
 | 
			
		||||
 | 
			
		||||
# Conditional flags
 | 
			
		||||
# DEBUG
 | 
			
		||||
#set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O0 -g")
 | 
			
		||||
#set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
#set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O0 -g")
 | 
			
		||||
set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
#set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O2 -g")
 | 
			
		||||
#set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O2 -g")
 | 
			
		||||
#set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O2 -g")
 | 
			
		||||
set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -Os -g")
 | 
			
		||||
set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
#set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -Os -g")
 | 
			
		||||
#set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
#set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
 | 
			
		||||
# RELEASE
 | 
			
		||||
set(CMAKE_C_FLAGS_RELEASE        "-DNDEBUG -O3") #  -flto
 | 
			
		||||
 
 | 
			
		||||
@@ -9,23 +9,8 @@
 | 
			
		||||
 | 
			
		||||
// #define DEBUG_VERBOES
 | 
			
		||||
 | 
			
		||||
// #define POWER_SUPPLY_12V
 | 
			
		||||
#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
 | 
			
		||||
#define POWER_SUPPLY_12V
 | 
			
		||||
// #define POWER_SUPPLY_24V
 | 
			
		||||
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
@@ -41,6 +26,7 @@
 | 
			
		||||
    #error "Please define either POWER_SUPPLY_12V or POWER_SUPPLY_24V"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
#define I2C_GPIO_RCU         RCU_GPIOF
 | 
			
		||||
@@ -55,14 +41,13 @@
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
#define USART_GPIO_RCU       RCU_GPIOA
 | 
			
		||||
// #define USART_RCU            RCU_USART0
 | 
			
		||||
#define USART_RCU            RCU_USART1
 | 
			
		||||
#define USART_GPIO_PORT      GPIOA
 | 
			
		||||
#define USART_GPIO_AF        GPIO_AF_1
 | 
			
		||||
#define USART_TX_PIN         GPIO_PIN_2
 | 
			
		||||
#define USART_RX_PIN         GPIO_PIN_3
 | 
			
		||||
// #define USART_PHY            USART0
 | 
			
		||||
#define USART_PHY            USART1
 | 
			
		||||
#define USART_PHY_BAUDRATE   115200U
 | 
			
		||||
// #define USART_PHY_IRQ        USART0_IRQn
 | 
			
		||||
#define RS485_EN_PORT        GPIOA
 | 
			
		||||
#define RS485_EN_PIN         GPIO_PIN_4
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -13,17 +13,21 @@
 | 
			
		||||
#include "fwdgt.h"
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "ultrasonic_analog.h"
 | 
			
		||||
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
#define RX_BUFFER_SIZE            32
 | 
			
		||||
 | 
			
		||||
#define PROTOCOL_PACKAGE_HEADER   0xD5
 | 
			
		||||
#define PROTOCOL_BOARD_TYPE       0x04
 | 
			
		||||
#define PROTOCOL_PACKAGE_LENGTH   0x02
 | 
			
		||||
 | 
			
		||||
#define PACKET_START_BYTE         0xD5
 | 
			
		||||
 | 
			
		||||
#define MAX_SERIAL_CMD_SIZE       16
 | 
			
		||||
#define BUF_SIZE      8
 | 
			
		||||
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
typedef enum
 | 
			
		||||
@@ -35,9 +39,21 @@ typedef enum
 | 
			
		||||
    VALIDATION_LENGTH_ERROR = 8
 | 
			
		||||
} validation_result_t;
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
    PS_LEN,
 | 
			
		||||
    PS_TYPE,
 | 
			
		||||
    PS_PAYLOAD,
 | 
			
		||||
    PS_CRC,
 | 
			
		||||
    PS_NULL
 | 
			
		||||
}packet_state_t;
 | 
			
		||||
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
void process_command(uint8_t* cmd, size_t length);
 | 
			
		||||
// void process_command(uint8_t* cmd, size_t length);
 | 
			
		||||
 | 
			
		||||
bool code_seen(char code);
 | 
			
		||||
 | 
			
		||||
float code_value(void);
 | 
			
		||||
 | 
			
		||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
 | 
			
		||||
 | 
			
		||||
@@ -53,4 +69,10 @@ void gd60914_tempture_report(void);
 | 
			
		||||
 | 
			
		||||
void ultrasonic_distance_report(void);
 | 
			
		||||
 | 
			
		||||
void start_communication(void);
 | 
			
		||||
 | 
			
		||||
void get_command(void);
 | 
			
		||||
 | 
			
		||||
void prcess_command(void);
 | 
			
		||||
 | 
			
		||||
#endif //RS485_PROTOCOL_H
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										17
									
								
								inc/usart.h
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								inc/usart.h
									
									
									
									
									
								
							@@ -8,8 +8,25 @@
 | 
			
		||||
#include "gd32e23x.h"
 | 
			
		||||
#include "board_config.h"
 | 
			
		||||
 | 
			
		||||
#define RX_BUFFER_SIZE            64
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
    unsigned char buffer[RX_BUFFER_SIZE];
 | 
			
		||||
    volatile unsigned int head;
 | 
			
		||||
    volatile unsigned int tail;
 | 
			
		||||
}ring_buffer_t;
 | 
			
		||||
 | 
			
		||||
static ring_buffer_t rx_buffer = {{0}, 0, 0}; // ring buffer for USART0
 | 
			
		||||
 | 
			
		||||
void usart_config(void);
 | 
			
		||||
 | 
			
		||||
void rs485_config(void);
 | 
			
		||||
 | 
			
		||||
void store_char(unsigned char data, ring_buffer_t *rx_buf);
 | 
			
		||||
 | 
			
		||||
uint16_t uart_available(void);
 | 
			
		||||
 | 
			
		||||
int uart_read(void);
 | 
			
		||||
 | 
			
		||||
#endif //USART_H
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
/* memory map */
 | 
			
		||||
MEMORY
 | 
			
		||||
{
 | 
			
		||||
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 16K
 | 
			
		||||
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 64K
 | 
			
		||||
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 4K
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ void watchdog_init(void) {
 | 
			
		||||
    rcu_osci_stab_wait(RCU_IRC40K);
 | 
			
		||||
 | 
			
		||||
    /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
 | 
			
		||||
    fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
 | 
			
		||||
    fwdgt_config(6250, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
 | 
			
		||||
 | 
			
		||||
    /* Enable FWDGT */
 | 
			
		||||
    fwdgt_enable();
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,7 @@ OF SUCH DAMAGE.
 | 
			
		||||
#include "gd32e23x_it.h"
 | 
			
		||||
 | 
			
		||||
extern uint16_t g_capture_value;
 | 
			
		||||
extern ring_buffer_t rx_buffer;
 | 
			
		||||
 | 
			
		||||
/*!
 | 
			
		||||
    \brief      this function handles NMI exception
 | 
			
		||||
@@ -117,7 +118,7 @@ void TIMER5_IRQHandler(void) {
 | 
			
		||||
        } else {
 | 
			
		||||
            //! turn off led & reconfig timer13 period to 1000(100ms)
 | 
			
		||||
            gpio_bit_write(LED_PORT, LED_PIN, SET);
 | 
			
		||||
            timer_autoreload_value_config(LED_BLINK_TIMER, 800);
 | 
			
		||||
            timer_autoreload_value_config(LED_BLINK_TIMER, 8000);
 | 
			
		||||
        }
 | 
			
		||||
        led_status = !led_status;
 | 
			
		||||
    }
 | 
			
		||||
@@ -157,7 +158,7 @@ void EXTI0_1_IRQHandler(void) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void USART0_IRQHandler(void) {
 | 
			
		||||
void USART1_IRQHandler(void) {
 | 
			
		||||
    static uint8_t rx_index = 0;
 | 
			
		||||
    static uint8_t rx_buffer[RX_BUFFER_SIZE];
 | 
			
		||||
 | 
			
		||||
@@ -165,18 +166,19 @@ void USART0_IRQHandler(void) {
 | 
			
		||||
        usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
 | 
			
		||||
        uint8_t received_data = (uint8_t) usart_data_receive(USART0);
 | 
			
		||||
 | 
			
		||||
        // 将接收到的数据存储到缓冲区
 | 
			
		||||
        if (rx_index < RX_BUFFER_SIZE - 1) {
 | 
			
		||||
            rx_buffer[rx_index++] = received_data;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
 | 
			
		||||
        usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
 | 
			
		||||
 | 
			
		||||
        process_command(rx_buffer, rx_index); // 处理指令
 | 
			
		||||
 | 
			
		||||
        rx_index = 0; // 重置缓冲区索引
 | 
			
		||||
        return;
 | 
			
		||||
        // // 将接收到的数据存储到缓冲区
 | 
			
		||||
        // if (rx_index < RX_BUFFER_SIZE - 1) {
 | 
			
		||||
        //     rx_buffer[rx_index++] = received_data;
 | 
			
		||||
        // }
 | 
			
		||||
        store_char(received_data, &rx_buffer);
 | 
			
		||||
    }
 | 
			
		||||
    //
 | 
			
		||||
    // if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
 | 
			
		||||
    //     usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
 | 
			
		||||
    //
 | 
			
		||||
    //     process_command(rx_buffer, rx_index); // 处理指令
 | 
			
		||||
    //
 | 
			
		||||
    //     rx_index = 0; // 重置缓冲区索引
 | 
			
		||||
    //     return;
 | 
			
		||||
    // }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/main.c
									
									
									
									
									
								
							@@ -25,34 +25,36 @@ int main(void)
 | 
			
		||||
    /* configure LED */
 | 
			
		||||
    led_blink_config();
 | 
			
		||||
    /* configure FWDGT */
 | 
			
		||||
    watchdog_init();
 | 
			
		||||
    // watchdog_init();
 | 
			
		||||
 | 
			
		||||
#ifdef SOFTWARE_IIC
 | 
			
		||||
    soft_i2c_config();
 | 
			
		||||
#else
 | 
			
		||||
    i2c_config();
 | 
			
		||||
#endif
 | 
			
		||||
// #ifdef SOFTWARE_IIC
 | 
			
		||||
//     soft_i2c_config();
 | 
			
		||||
// #else
 | 
			
		||||
//     i2c_config();
 | 
			
		||||
// #endif
 | 
			
		||||
 | 
			
		||||
    printf("system start!\r\n");
 | 
			
		||||
 | 
			
		||||
    ultrasonic_config();
 | 
			
		||||
    // ultrasonic_config();
 | 
			
		||||
 | 
			
		||||
    while(1){
 | 
			
		||||
        gd60914_get_object_tempture();
 | 
			
		||||
        // gd60914_get_object_tempture();
 | 
			
		||||
 | 
			
		||||
        delay_ms(50);
 | 
			
		||||
        delay_ms(500);
 | 
			
		||||
        start_communication();
 | 
			
		||||
        printf("hello world!\r\n");
 | 
			
		||||
 | 
			
		||||
        ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
 | 
			
		||||
        // ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
 | 
			
		||||
 | 
			
		||||
        watchdog_reload();
 | 
			
		||||
        // watchdog_reload();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* retarget the C library printf function to the USART */
 | 
			
		||||
int _write(int fd, char *pBuffer, int size) {
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        usart_data_transmit(USART0, (uint8_t) pBuffer[i]);
 | 
			
		||||
        while (RESET == usart_flag_get(USART0, USART_FLAG_TBE));
 | 
			
		||||
        usart_data_transmit(USART1, (uint8_t) pBuffer[i]);
 | 
			
		||||
        while (RESET == usart_flag_get(USART1, USART_FLAG_TBE));
 | 
			
		||||
    }
 | 
			
		||||
    return size;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,49 +6,70 @@
 | 
			
		||||
 | 
			
		||||
extern uint8_t g_temperature_uint8[2];
 | 
			
		||||
 | 
			
		||||
void process_command(uint8_t *cmd, size_t length) {
 | 
			
		||||
    char combined_str[3];
 | 
			
		||||
    validation_result_t validate = VALIDATION_SUCCESS;
 | 
			
		||||
static int buf_index_r = 0;
 | 
			
		||||
static int buf_index_w = 0;
 | 
			
		||||
static int buf_length = 0;
 | 
			
		||||
 | 
			
		||||
    validate = (validate_package_header(cmd) |
 | 
			
		||||
                validate_package_type(cmd) |
 | 
			
		||||
                validate_data_length(cmd) |
 | 
			
		||||
                validate_package_crc(cmd, length));
 | 
			
		||||
static char cmd_buffer[BUF_SIZE][MAX_SERIAL_CMD_SIZE];
 | 
			
		||||
static char serial_char = 0x00;
 | 
			
		||||
 | 
			
		||||
    switch (validate) {
 | 
			
		||||
    case VALIDATION_SUCCESS:
 | 
			
		||||
        // printf("%d", length);
 | 
			
		||||
            sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
 | 
			
		||||
        if (strcmp(combined_str, "M1") == 0) {
 | 
			
		||||
            ultrasonic_distance_report();
 | 
			
		||||
        } else if (strcmp(combined_str, "M2") == 0) {
 | 
			
		||||
            gd60914_tempture_report();
 | 
			
		||||
        } else if (strcmp(combined_str, "M3") == 0)
 | 
			
		||||
        {
 | 
			
		||||
            printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
 | 
			
		||||
            fwdgt_reset_mcu();
 | 
			
		||||
        } else {
 | 
			
		||||
            printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case VALIDATION_CRC_ERROR:
 | 
			
		||||
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
 | 
			
		||||
        break;
 | 
			
		||||
    case VALIDATION_HEADER_ERROR:
 | 
			
		||||
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
 | 
			
		||||
        break;
 | 
			
		||||
    case VALIDATION_TYPE_ERROR:
 | 
			
		||||
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
 | 
			
		||||
        break;
 | 
			
		||||
    case VALIDATION_LENGTH_ERROR:
 | 
			
		||||
        printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
static int serial_count = 0;
 | 
			
		||||
static char *strchr_pointer = NULL;
 | 
			
		||||
 | 
			
		||||
static packet_state_t packet_state = PS_LEN;
 | 
			
		||||
 | 
			
		||||
bool code_seen(char code) {
 | 
			
		||||
    strchr_pointer = strchr(cmd_buffer[buf_index_r], code);
 | 
			
		||||
    return (strchr_pointer != NULL);  //Return True if a character was found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float code_value(void) {
 | 
			
		||||
    return (strtod(&cmd_buffer[buf_index_r][strchr_pointer - cmd_buffer[buf_index_r] + 1], NULL));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// void process_command(uint8_t *cmd, size_t length) {
 | 
			
		||||
//     char combined_str[3];
 | 
			
		||||
//     validation_result_t validate = VALIDATION_SUCCESS;
 | 
			
		||||
//
 | 
			
		||||
//     validate = (validate_package_header(cmd) |
 | 
			
		||||
//                 validate_package_type(cmd) |
 | 
			
		||||
//                 validate_data_length(cmd) |
 | 
			
		||||
//                 validate_package_crc(cmd, length));
 | 
			
		||||
//
 | 
			
		||||
//     switch (validate) {
 | 
			
		||||
//     case VALIDATION_SUCCESS:
 | 
			
		||||
//         // printf("%d", length);
 | 
			
		||||
//             sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
 | 
			
		||||
//         if (strcmp(combined_str, "M1") == 0) {
 | 
			
		||||
//             ultrasonic_distance_report();
 | 
			
		||||
//         } else if (strcmp(combined_str, "M2") == 0) {
 | 
			
		||||
//             gd60914_tempture_report();
 | 
			
		||||
//         } else if (strcmp(combined_str, "M3") == 0)
 | 
			
		||||
//         {
 | 
			
		||||
//             printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
 | 
			
		||||
//             fwdgt_reset_mcu();
 | 
			
		||||
//         } else {
 | 
			
		||||
//             printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
 | 
			
		||||
//             return;
 | 
			
		||||
//         }
 | 
			
		||||
//         break;
 | 
			
		||||
//     case VALIDATION_CRC_ERROR:
 | 
			
		||||
//         printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
 | 
			
		||||
//         break;
 | 
			
		||||
//     case VALIDATION_HEADER_ERROR:
 | 
			
		||||
//         printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
 | 
			
		||||
//         break;
 | 
			
		||||
//     case VALIDATION_TYPE_ERROR:
 | 
			
		||||
//         printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
 | 
			
		||||
//         break;
 | 
			
		||||
//     case VALIDATION_LENGTH_ERROR:
 | 
			
		||||
//         printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
 | 
			
		||||
//         break;
 | 
			
		||||
//     default:
 | 
			
		||||
//         break;
 | 
			
		||||
//     }
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
 | 
			
		||||
    uint8_t crc = 0;
 | 
			
		||||
 | 
			
		||||
@@ -121,3 +142,88 @@ void ultrasonic_distance_report(void) {
 | 
			
		||||
    printf("%c%c", package_data[0], package_data[1]);
 | 
			
		||||
    printf("%c", calculate_crc(combined_data, 6));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void start_communication(void) {
 | 
			
		||||
    if (buf_length < (BUF_SIZE - 1)) {
 | 
			
		||||
        get_command();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (buf_length) {
 | 
			
		||||
        prcess_command();
 | 
			
		||||
        buf_length--;
 | 
			
		||||
        buf_index_r = (buf_index_r + 1) % BUF_SIZE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void get_command(void) {
 | 
			
		||||
    static uint8_t check_sum = 0;
 | 
			
		||||
    static uint8_t packet_len ;
 | 
			
		||||
    static uint8_t packet_type;
 | 
			
		||||
    packet_state = PS_NULL;
 | 
			
		||||
    serial_count = 0;
 | 
			
		||||
    serial_char=0;
 | 
			
		||||
 | 
			
		||||
    if (uart_available() > 0)
 | 
			
		||||
        delay_ms(5);
 | 
			
		||||
    while (uart_available() > 0 && buf_length < BUF_SIZE) {
 | 
			
		||||
        delay_us(100);
 | 
			
		||||
        serial_char = uart_read();
 | 
			
		||||
        switch (packet_state) {
 | 
			
		||||
            case PS_NULL:
 | 
			
		||||
                if (serial_char == PACKET_START_BYTE) {
 | 
			
		||||
                    packet_state = PS_TYPE;
 | 
			
		||||
                    serial_count = 0;
 | 
			
		||||
                    check_sum = 0;
 | 
			
		||||
                } else {
 | 
			
		||||
                    serial_count = 0;
 | 
			
		||||
                }
 | 
			
		||||
            break;
 | 
			
		||||
            case PS_TYPE:
 | 
			
		||||
                packet_type = serial_char;
 | 
			
		||||
            check_sum += serial_char;
 | 
			
		||||
            packet_state = PS_LEN;
 | 
			
		||||
            break;
 | 
			
		||||
            case PS_LEN:
 | 
			
		||||
                check_sum += serial_char;
 | 
			
		||||
            packet_len = serial_char;
 | 
			
		||||
            packet_state = PS_PAYLOAD;
 | 
			
		||||
            break;
 | 
			
		||||
            case PS_PAYLOAD:
 | 
			
		||||
                check_sum += serial_char;
 | 
			
		||||
            cmd_buffer[buf_index_w][serial_count++] = serial_char;
 | 
			
		||||
            if (serial_count >= packet_len) {
 | 
			
		||||
                packet_state = PS_CRC;
 | 
			
		||||
            }
 | 
			
		||||
            break;
 | 
			
		||||
            case PS_CRC:
 | 
			
		||||
                packet_state = PS_NULL;
 | 
			
		||||
            if (!serial_count || check_sum != serial_char) {
 | 
			
		||||
                serial_count = 0;
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
            cmd_buffer[buf_index_w][serial_count] = 0;
 | 
			
		||||
            buf_index_w = (buf_index_w + 1) % BUF_SIZE;
 | 
			
		||||
            buf_length++;
 | 
			
		||||
            serial_count = 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void prcess_command(void) {
 | 
			
		||||
    if (code_seen('M')) {
 | 
			
		||||
        switch ((int)code_value()) {
 | 
			
		||||
            case 1:
 | 
			
		||||
                // ultrasonic_distance_report();
 | 
			
		||||
                    printf("M1");
 | 
			
		||||
                break;
 | 
			
		||||
            case 2:
 | 
			
		||||
                // gd60914_tempture_report();
 | 
			
		||||
                    printf("M2");
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								src/usart.c
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								src/usart.c
									
									
									
									
									
								
							@@ -26,9 +26,14 @@ void usart_config(void)
 | 
			
		||||
    usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
 | 
			
		||||
    usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
 | 
			
		||||
    usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
 | 
			
		||||
    nvic_irq_enable(USART_PHY_IRQ, 0);
 | 
			
		||||
 | 
			
		||||
    usart_deinit(USART_PHY);
 | 
			
		||||
    usart_baudrate_set(USART_PHY, 115200U);
 | 
			
		||||
    usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
 | 
			
		||||
    usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
 | 
			
		||||
    nvic_irq_enable(USART1_IRQn, 0);
 | 
			
		||||
    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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -45,4 +50,27 @@ void rs485_config(void)
 | 
			
		||||
    gpio_output_options_set(RS485_EN_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
 | 
			
		||||
 | 
			
		||||
    gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, SET); //auto dircetion control
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void store_char(unsigned char data, ring_buffer_t *rx_buf) {
 | 
			
		||||
    uint16_t i = (unsigned int)(rx_buf->head + 1) % RX_BUFFER_SIZE;
 | 
			
		||||
 | 
			
		||||
    if (i != rx_buf->tail) {
 | 
			
		||||
        rx_buf->buffer[rx_buf->head] = data;
 | 
			
		||||
        rx_buf->head = i;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t uart_available(void) {
 | 
			
		||||
    return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int uart_read(void) {
 | 
			
		||||
    if (rx_buffer.head == rx_buffer.tail) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    } else {
 | 
			
		||||
        unsigned char c = rx_buffer.buffer[rx_buffer.tail];
 | 
			
		||||
        rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
 | 
			
		||||
        return c;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user