generated from hulk/gd32e23x_template
Compare commits
6 Commits
dev
...
feature-pr
Author | SHA1 | Date | |
---|---|---|---|
c11db2c9bf | |||
11cf546438 | |||
1dacace57a | |||
a546f7bf83 | |||
39d800cb1b | |||
a5c1c857a9 |
@@ -12,7 +12,7 @@ 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,61 +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. 读取数据时间间隔
|
||||
|
||||
- 推荐数据时间间隔至少为50ms
|
93
README.md
93
README.md
@@ -1,31 +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 |
|
||||
| v0.2.0 | 硬件修改RS485接口芯片为SP3485(非自动换向) | 2025-06-03 | 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设置,目前能用。
|
||||
|
||||
## 300K超声换能头工作流程
|
||||
### **1. 超声波发送**
|
||||
通过TIMER13定时器,配置为输出300KHz的PWM信号,驱动超声换能头发射超声波。TIMER13的输出模式为PWM1模式,同时用TIMER13的更新中断触发次数进行计数。
|
||||
最终使驱动引脚发送300KHz 50%占空比 5个周期的PWM信号。同时开启计时。
|
||||
## 添加源文件与头文件
|
||||
在`ProjectDir/CMakeLists.txt`中21行左右,添加对应源文件即可。
|
||||
|
||||
### **2. 超声波计时**
|
||||
通过TIMER15定时器,配置为us计时器,并传入预先测得的超声消隐延时值(例如:230us)。该定时器用于在发送超声波后延时一段时间,确保接收通道不会受到发射信号的干扰。但是暂不开启该定时器。
|
||||
同时在TIMER15的更新中断中断服务函数中,开启外部输入中断,用以检测超声回波,并开启TIMER16定时器。
|
||||
TIMER16定时器输入捕获,用于计算超声回波的时间。
|
||||
通过前面的消隐时间与回波时间的和值,计算出实际的距离值。
|
||||
```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`
|
||||
|
||||
### **3. 距离值输出**
|
||||
| 芯片型号 | FLASH | RAM |
|
||||
|------------|-------|-----|
|
||||
| GD32E230F4 | 16K | 4K |
|
||||
| GD32E230F8 | 64K | 8K |
|
||||
|
||||
\[
|
||||
\frac{\frac{(\verb|TIME_CORRECTION_US| + \verb|us_value|)\times 340\,\text{m/s}}{10^{3} \ast 10^{3}} }{2}
|
||||
\]
|
||||
```linkerscript
|
||||
/* memory map */
|
||||
MEMORY
|
||||
{
|
||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
|
||||
}
|
||||
```
|
||||
|
||||
其中,`TIME_CORRECTION_US`为超声消隐延时定时器的初始值,`us_value`为外部输入中断计数值。
|
||||
## 关于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,10 +9,8 @@
|
||||
|
||||
// #define DEBUG_VERBOES
|
||||
|
||||
// #define POWER_SUPPLY_12V
|
||||
#define POWER_SUPPLY_24V
|
||||
|
||||
// #define DEBUG_VOFA_TOOL
|
||||
#define POWER_SUPPLY_12V
|
||||
// #define POWER_SUPPLY_24V
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
@@ -43,15 +41,16 @@
|
||||
/******************************************************************************/
|
||||
|
||||
#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 USART1_IRQn
|
||||
#define RS485_EN_PORT GPIOA
|
||||
#define RS485_EN_PIN GPIO_PIN_1
|
||||
#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
|
||||
|
19
inc/usart.h
19
inc/usart.h
@@ -8,6 +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,8 +1,8 @@
|
||||
/* memory map */
|
||||
MEMORY
|
||||
{
|
||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
|
||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
||||
}
|
||||
|
||||
ENTRY(Reset_Handler)
|
||||
|
@@ -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,26 +158,28 @@ void EXTI0_1_IRQHandler(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void USART0_IRQHandler(void) {
|
||||
static uint8_t rx_index = 0;
|
||||
static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
||||
void USART1_IRQHandler(void) {
|
||||
// static uint8_t rx_index = 0;
|
||||
// static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
||||
|
||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
|
||||
uint8_t received_data = (uint8_t) usart_data_receive(USART0);
|
||||
if (RESET != usart_interrupt_flag_get(USART_PHY, USART_INT_FLAG_RBNE)) {
|
||||
// usart_interrupt_flag_clear(USART_PHY, USART_INT_FLAG_RBNE);
|
||||
uint8_t received_data = (uint8_t) usart_data_receive(USART_PHY);
|
||||
|
||||
// printf("%c", received_data);
|
||||
// 将接收到的数据存储到缓冲区
|
||||
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;
|
||||
// }
|
||||
}
|
@@ -35,5 +35,5 @@ void led_blink_config(void)
|
||||
|
||||
timer_enable(LED_BLINK_TIMER);
|
||||
|
||||
nvic_irq_enable(LED_BLINK_IRQ, 3U);
|
||||
nvic_irq_enable(LED_BLINK_IRQ, 2);
|
||||
}
|
15
src/main.c
15
src/main.c
@@ -6,6 +6,8 @@
|
||||
*/
|
||||
#include "main.h"
|
||||
|
||||
#include "newlib.h"
|
||||
|
||||
volatile uint8_t g_temperature_uint8[2] = {0};
|
||||
volatile uint16_t g_capture_value;
|
||||
|
||||
@@ -35,13 +37,16 @@ int main(void)
|
||||
|
||||
printf("system start!\r\n");
|
||||
|
||||
ultrasonic_config();
|
||||
// ultrasonic_config();
|
||||
|
||||
while(1){
|
||||
// gd60914_get_object_tempture();
|
||||
|
||||
delay_ms(50);
|
||||
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
delay_ms(500);
|
||||
start_communication();
|
||||
printf("hello world!\r\n");
|
||||
|
||||
// ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
|
||||
// watchdog_reload();
|
||||
}
|
||||
@@ -50,8 +55,8 @@ int main(void)
|
||||
/* 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,81 @@
|
||||
|
||||
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;
|
||||
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
|
||||
// }
|
||||
|
||||
bool code_seen(char code) {
|
||||
strchr_pointer = NULL;
|
||||
for (int i = 0; cmd_buffer[buf_index_r][i] != '\0'; i++) {
|
||||
if (cmd_buffer[buf_index_r][i] == code) {
|
||||
strchr_pointer = &cmd_buffer[buf_index_r][i];
|
||||
break;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
|
||||
@@ -110,9 +142,6 @@ void ultrasonic_distance_report(void) {
|
||||
|
||||
distance_uint16 = ultrasonic_calc_distance();
|
||||
|
||||
#ifdef DEBUG_VOFA_TOOL
|
||||
printf("%d\n", distance_uint16);
|
||||
#else
|
||||
package_data[0] = (distance_uint16 >> 8) & 0xFF;
|
||||
package_data[1] = distance_uint16 & 0xFF;
|
||||
|
||||
@@ -123,5 +152,92 @@ void ultrasonic_distance_report(void) {
|
||||
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
||||
printf("%c%c", package_data[0], package_data[1]);
|
||||
printf("%c", calculate_crc(combined_data, 6));
|
||||
#endif
|
||||
}
|
||||
|
||||
void start_communication(void) {
|
||||
if (buf_length < (BUF_SIZE - 1)) {
|
||||
get_command();
|
||||
}
|
||||
|
||||
if (buf_length) {
|
||||
printf("DONE!\r\n");
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -140,7 +140,7 @@ void ultrasonic_config(void) {
|
||||
}
|
||||
|
||||
uint16_t ultrasonic_calc_distance(void) {
|
||||
// while (!ultrasonicMeasurementDone);
|
||||
while (!ultrasonicMeasurementDone);
|
||||
// uint32_t us_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH);
|
||||
uint16_t distance = (TIME_CORRECTION_US + g_capture_value) * 17;
|
||||
/*
|
||||
|
89
src/usart.c
89
src/usart.c
@@ -4,6 +4,35 @@
|
||||
|
||||
#include "usart.h"
|
||||
|
||||
/**
|
||||
* @brief configure the USART
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void usart_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(USART_GPIO_RCU);
|
||||
rcu_periph_clock_enable(USART_RCU);
|
||||
|
||||
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_RX_PIN);
|
||||
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_TX_PIN);
|
||||
|
||||
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_RX_PIN);
|
||||
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_RX_PIN);
|
||||
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_TX_PIN);
|
||||
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_TX_PIN);
|
||||
|
||||
usart_deinit(USART_PHY);
|
||||
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_interrupt_enable(USART_PHY, USART_INT_RBNE);
|
||||
// usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
|
||||
usart_enable(USART_PHY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief configure the RS485(MAX13487) driver
|
||||
* @param none
|
||||
@@ -11,39 +40,33 @@
|
||||
*/
|
||||
void rs485_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(USART_GPIO_RCU);
|
||||
rcu_periph_clock_enable(USART_RCU);
|
||||
usart_config();
|
||||
|
||||
gpio_af_set(USART_GPIO_PORT, GPIO_AF_1, USART_TX_PIN | USART_RX_PIN | RS485_EN_PIN);
|
||||
gpio_mode_set(RS485_EN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN);
|
||||
gpio_output_options_set(RS485_EN_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
|
||||
|
||||
/* configure USART Tx&Rx as alternate function push-pull */
|
||||
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, USART_TX_PIN | USART_RX_PIN);
|
||||
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, USART_TX_PIN | USART_RX_PIN);
|
||||
|
||||
/* configure RS485 EN Pin */
|
||||
gpio_mode_set(USART_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN);
|
||||
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
|
||||
// gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET);
|
||||
|
||||
/* USART configure */
|
||||
usart_deinit(USART_PHY);
|
||||
usart_baudrate_set(USART_PHY, USART_PHY_BAUDRATE);
|
||||
// usart_parity_config(RS485_PHY, USART_PM_NONE);
|
||||
// usart_word_length_set(RS485_PHY, USART_WL_8BIT);
|
||||
// usart_stop_bit_set(RS485_PHY, USART_STB_1BIT);
|
||||
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_driver_assertime_config(USART_PHY, 0x01);
|
||||
usart_driver_deassertime_config(USART_PHY, 0x01);
|
||||
|
||||
// usart_depolarity_config(USART0, USART_DEP_HIGH);
|
||||
// usart_depolarity_config(USART0, USART_DEP_LOW);
|
||||
|
||||
usart_rs485_driver_enable(USART_PHY);
|
||||
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_IDLE);
|
||||
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