generated from hulk/gd32e23x_template
Compare commits
20 Commits
997637efcf
...
feature-pr
Author | SHA1 | Date | |
---|---|---|---|
c11db2c9bf | |||
11cf546438 | |||
1dacace57a | |||
a546f7bf83 | |||
39d800cb1b | |||
a5c1c857a9 | |||
7ffb8c0612 | |||
7075fd16ea | |||
f7ef4b5af7 | |||
e940c69d49 | |||
b4901f5f37 | |||
c9c1e00d6a | |||
6dd053e217 | |||
8a7baaf87a | |||
1af7a98a5f | |||
aee082068e | |||
a0ba0c6bd1 | |||
389d480da7 | |||
c12273fd46 | |||
e86f8b0dd3 |
@@ -6,8 +6,8 @@ set(PROJECT_NAME "XLSW_3DP_US-IR")
|
||||
project(${PROJECT_NAME})
|
||||
|
||||
set(VERSION_MAJOR 0)
|
||||
set(VERSION_MINOR 0)
|
||||
set(VERSION_PATCH 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")
|
||||
|
||||
@@ -16,7 +16,7 @@ set(OPT1 "_[12V]")
|
||||
#set(OPT1 "_[SW_IIC]")
|
||||
|
||||
# Options 2
|
||||
set(OPT2 "")
|
||||
set(OPT2 "_[HW_IIC]")
|
||||
#set(OPT2 "_[NO_LED]")
|
||||
|
||||
enable_language(C)
|
||||
@@ -41,6 +41,7 @@ set(TARGET_C_SRC
|
||||
${CMAKE_SOURCE_DIR}/src/fwdgt.c
|
||||
${CMAKE_SOURCE_DIR}/src/rs485_protocol.c
|
||||
${CMAKE_SOURCE_DIR}/src/ultrasonic_analog.c
|
||||
${CMAKE_SOURCE_DIR}/src/gd60914.c
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${TARGET_C_SRC})
|
||||
|
@@ -94,12 +94,12 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in
|
||||
#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 -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")
|
||||
|
||||
# RELEASE
|
||||
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O3") # -flto
|
||||
|
@@ -41,13 +41,14 @@
|
||||
/******************************************************************************/
|
||||
|
||||
#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_4
|
||||
|
||||
|
34
inc/gd60914.h
Normal file
34
inc/gd60914.h
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by dell on 25-1-7.
|
||||
//
|
||||
|
||||
#ifndef GD60914_H
|
||||
#define GD60914_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
|
||||
#include "board_config.h"
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
#include "soft_i2c.h"
|
||||
#else
|
||||
#include "i2c.h"
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define GD60914_ADDR (0x18 << 1)
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define GD60914_HUM_TEMP 0x1A
|
||||
#define GD60914_OBJ_TEMP 0x1F
|
||||
#define GD60914_AMB_TEMP 0x1E
|
||||
#define GD60914_TEMP_REG 0x1C
|
||||
|
||||
void gd60914_get_object_tempture(void);
|
||||
|
||||
void gd60914_read_temp(void);
|
||||
|
||||
#endif //GD60914_H
|
@@ -19,7 +19,7 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_SPEED 20000
|
||||
#define I2C_SPEED 100000
|
||||
|
||||
#define I2C_TIME_OUT (uint16_t)(5000)
|
||||
#define I2C_OK 1
|
||||
|
@@ -43,7 +43,7 @@ OF SUCH DAMAGE.
|
||||
#include "usart.h"
|
||||
#include "fwdgt.h"
|
||||
#include "board_config.h"
|
||||
#include "ultrasonic_analog.h"
|
||||
#include "gd60914.h"
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
#include "soft_i2c.h"
|
||||
|
@@ -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 0x03
|
||||
#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);
|
||||
|
||||
@@ -49,8 +65,14 @@ validation_result_t validate_package_type(uint8_t* data);
|
||||
|
||||
validation_result_t validate_data_length(uint8_t* data);
|
||||
|
||||
|
||||
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
|
||||
|
@@ -37,7 +37,7 @@ void ultrasonic_echo_timer_config(void);
|
||||
|
||||
void ultrasonic_config(void);
|
||||
|
||||
uint32_t ultrasonic_calc_distance(void);
|
||||
uint16_t ultrasonic_calc_distance(void);
|
||||
|
||||
|
||||
#endif //ULTRASONIC_ANALOG_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,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)
|
||||
|
@@ -34,7 +34,8 @@ OF SUCH DAMAGE.
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
|
||||
__IO uint32_t g_capture_value;
|
||||
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;
|
||||
// }
|
||||
}
|
51
src/gd60914.c
Normal file
51
src/gd60914.c
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by dell on 25-1-7.
|
||||
//
|
||||
|
||||
#include "gd60914.h"
|
||||
|
||||
void gd60914_get_object_tempture(void) {
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_config();
|
||||
#else
|
||||
i2c_config();
|
||||
#endif
|
||||
|
||||
static uint8_t sensor_validation_data[2];
|
||||
extern uint8_t g_temperature_uint8[2];
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data);
|
||||
#else
|
||||
i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, sensor_validation_data);
|
||||
#endif
|
||||
|
||||
if (sensor_validation_data[0] != 0xAA || sensor_validation_data[1] != 0x55) {
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("sensor error\r\n");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
delay_ms(350);
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8);
|
||||
#else
|
||||
i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8);
|
||||
#endif
|
||||
|
||||
// printf("%d\r\n", g_temperature_uint8[1] << 8 | g_temperature_uint8[0]);
|
||||
}
|
||||
|
||||
void gd60914_read_temp(void) {
|
||||
|
||||
uint8_t value[2] = {0};
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, value);
|
||||
#else
|
||||
i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, value);
|
||||
#endif
|
||||
|
||||
printf("%x %x\r\n", value[1], value[0]);
|
||||
}
|
34
src/main.c
34
src/main.c
@@ -6,8 +6,10 @@
|
||||
*/
|
||||
#include "main.h"
|
||||
|
||||
extern uint32_t g_capture_value;
|
||||
uint16_t g_distance_uint16;
|
||||
#include "newlib.h"
|
||||
|
||||
volatile uint8_t g_temperature_uint8[2] = {0};
|
||||
volatile uint16_t g_capture_value;
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
@@ -25,32 +27,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
|
||||
|
||||
printf("system start!\r\n");
|
||||
|
||||
ultrasonic_config();
|
||||
// ultrasonic_config();
|
||||
|
||||
// gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, RESET);
|
||||
while(1){
|
||||
// printf("hello world!\r\n");
|
||||
delay_ms(50);
|
||||
// gd60914_get_object_tempture();
|
||||
|
||||
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
// delay_ms(2);
|
||||
delay_ms(500);
|
||||
start_communication();
|
||||
printf("hello world!\r\n");
|
||||
|
||||
// ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
|
||||
// g_distance_uint16 = ultrasonic_calc_distance();
|
||||
// printf("Distance: %d cm\r\n", g_distance_uint16);
|
||||
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;
|
||||
}
|
||||
|
@@ -4,51 +4,83 @@
|
||||
|
||||
#include "rs485_protocol.h"
|
||||
|
||||
void process_command(uint8_t *cmd, size_t length) {
|
||||
char combined_str[3];
|
||||
validation_result_t validate = VALIDATION_SUCCESS;
|
||||
extern uint8_t g_temperature_uint8[2];
|
||||
|
||||
validate = (validate_package_header(cmd) |
|
||||
validate_package_type(cmd) |
|
||||
validate_data_length(cmd) |
|
||||
validate_package_crc(cmd, length));
|
||||
static int buf_index_r = 0;
|
||||
static int buf_index_w = 0;
|
||||
static int buf_length = 0;
|
||||
|
||||
switch (validate) {
|
||||
case VALIDATION_SUCCESS:
|
||||
// printf("%d", length);
|
||||
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||
if (strcmp(combined_str, "M1") == 0) {
|
||||
static char cmd_buffer[BUF_SIZE][MAX_SERIAL_CMD_SIZE];
|
||||
static char serial_char = 0x00;
|
||||
|
||||
ultrasonic_distance_report();
|
||||
} else if (strcmp(combined_str, "M2") == 0) {
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0x6B, 0xCC);
|
||||
// tempture_value_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;
|
||||
|
||||
@@ -91,42 +123,121 @@ validation_result_t validate_data_length(uint8_t *data) {
|
||||
}
|
||||
}
|
||||
|
||||
// void eddy_current_value_report(void) {
|
||||
// static uint32_t eddy_current_value_uint32 = 0;
|
||||
//
|
||||
// eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
|
||||
//
|
||||
// package_data[0] = (eddy_current_value_uint32 >> 24) & 0xFF;
|
||||
// package_data[1] = (eddy_current_value_uint32 >> 16) & 0xFF;
|
||||
// package_data[2] = (eddy_current_value_uint32 >> 8) & 0xFF;
|
||||
// package_data[3] = eddy_current_value_uint32 & 0xFF;
|
||||
//
|
||||
// uint8_t combined_data[7];
|
||||
// memcpy(combined_data, package_header, 3);
|
||||
// memcpy(combined_data + 3, package_data, 4);
|
||||
//
|
||||
// printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
||||
// printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
|
||||
// printf("%c", calculate_crc(combined_data, 8));
|
||||
// }
|
||||
void gd60914_tempture_report(void) {
|
||||
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
|
||||
|
||||
uint8_t combined_data[5];
|
||||
memcpy(combined_data, package_header, 3);
|
||||
memcpy(combined_data + 3, g_temperature_uint8, 2);
|
||||
|
||||
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
||||
printf("%c%c", g_temperature_uint8[1], g_temperature_uint8[0]);
|
||||
printf("%c", calculate_crc(combined_data, 6));
|
||||
}
|
||||
|
||||
void ultrasonic_distance_report(void) {
|
||||
static uint32_t distance_uint32 = 0;
|
||||
static uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
|
||||
static uint16_t distance_uint16 = 0;
|
||||
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
|
||||
static uint8_t package_data[4] = {0};
|
||||
|
||||
distance_uint32 = ultrasonic_calc_distance();
|
||||
distance_uint16 = ultrasonic_calc_distance();
|
||||
|
||||
package_data[0] = (distance_uint32 >> 24) & 0xFF;
|
||||
package_data[1] = (distance_uint32 >> 16) & 0xFF;
|
||||
package_data[2] = (distance_uint32 >> 8) & 0xFF;
|
||||
package_data[3] = distance_uint32 & 0xFF;
|
||||
package_data[0] = (distance_uint16 >> 8) & 0xFF;
|
||||
package_data[1] = distance_uint16 & 0xFF;
|
||||
|
||||
uint8_t combined_data[7];
|
||||
memcpy(combined_data, package_header, 3);
|
||||
memcpy(combined_data + 3, package_data, 4);
|
||||
memcpy(combined_data + 3, package_data, 2);
|
||||
|
||||
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
||||
printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
|
||||
printf("%c", calculate_crc(combined_data, 8));
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@
|
||||
\retval none
|
||||
*/
|
||||
void soft_i2c_delay(void) {
|
||||
delay_us(20); // Adjust delay as needed
|
||||
delay_us(1); // Adjust delay as needed
|
||||
/* delay to freq
|
||||
* 15KHz: delay_us(20);
|
||||
* 65KHz: delay_us(1);
|
||||
|
@@ -139,10 +139,10 @@ void ultrasonic_config(void) {
|
||||
ultrasonic_echo_timer_config();
|
||||
}
|
||||
|
||||
uint32_t ultrasonic_calc_distance(void) {
|
||||
uint16_t ultrasonic_calc_distance(void) {
|
||||
while (!ultrasonicMeasurementDone);
|
||||
// uint32_t us_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH);
|
||||
uint32_t distance = (TIME_CORRECTION_US + g_capture_value) * 17;
|
||||
uint16_t distance = (TIME_CORRECTION_US + g_capture_value) * 17;
|
||||
/*
|
||||
* (TIME_CORRECTION_US + us_value) * 340 m/s
|
||||
* -----------------------------------------
|
||||
|
30
src/usart.c
30
src/usart.c
@@ -27,11 +27,10 @@ void usart_config(void)
|
||||
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART_PHY);
|
||||
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
nvic_irq_enable(USART_PHY_IRQ, 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,3 +47,26 @@ void rs485_config(void)
|
||||
|
||||
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