generated from hulk/gd32e23x_template
Merge remote-tracking branch 'origin/feature-rs485' into dev
# Conflicts: # inc/rs485.h # src/gd32e23x_it.c # src/rs485.c
This commit is contained in:
commit
fca25e1981
@ -5,7 +5,7 @@ project(xlsw_3dp_LDC1612)
|
||||
|
||||
set(VERSION_MAJOR 0)
|
||||
set(VERSION_MINOR 1)
|
||||
set(VERSION_PATCH 0)
|
||||
set(VERSION_PATCH 1)
|
||||
set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
|
||||
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
|
||||
|
||||
|
@ -15,6 +15,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_SPEED 20000
|
||||
#define RCU_GPIO_I2C RCU_GPIOF
|
||||
#define RCU_I2C RCU_I2C0
|
||||
@ -24,11 +26,15 @@
|
||||
#define I2C_SDA_PIN GPIO_PIN_0
|
||||
#define I2C_GPIO_AF GPIO_AF_1
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_TIME_OUT (uint16_t)(10000)
|
||||
#define I2C_OK 1
|
||||
#define I2C_FAIL 0
|
||||
#define I2C_END 1
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum {
|
||||
I2C_START = 0,
|
||||
I2C_SEND_ADDRESS,
|
||||
@ -37,6 +43,8 @@ typedef enum {
|
||||
I2C_STOP
|
||||
} i2c_process_enum;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void i2c_gpio_config(void);
|
||||
|
||||
void i2c_config(void);
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
/*Register Rddr*/
|
||||
/***************************************************************************/
|
||||
|
||||
#define CONVERTION_RESULT_REG_START 0X00
|
||||
#define SET_CONVERSION_TIME_REG_START 0X08
|
||||
#define SET_CONVERSION_OFFSET_REG_START 0X0C
|
||||
|
@ -14,6 +14,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_7
|
||||
#define LED_RCU RCU_GPIOA
|
||||
@ -21,6 +23,8 @@
|
||||
#define LED_TIMER TIMER16
|
||||
#define LED_IRQ TIMER16_IRQn
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void led_config(void);
|
||||
|
||||
#endif //LED_H
|
||||
|
39
inc/rs485.h
39
inc/rs485.h
@ -14,6 +14,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RS485_RCU RCU_USART0
|
||||
#define RS485_GPIO_RCU RCU_GPIOA
|
||||
#define RS485_GPIO_PORT GPIOA
|
||||
@ -21,28 +23,37 @@
|
||||
#define RS485_RX_PIN GPIO_PIN_3
|
||||
#define RS485_PHY USART0
|
||||
#define RS485_BAUDRATE 115200U
|
||||
|
||||
#define RS485_EN_PIN GPIO_PIN_1
|
||||
|
||||
#define RX_BUFFER_SIZE 32
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum {
|
||||
#define RX_BUFFER_SIZE 32
|
||||
|
||||
#define LDC1612_PACKAGE_HEADER 0xD5
|
||||
#define LDC1612_BOARD_TYPE 0x03
|
||||
#define LDC1612_PACKAGE_LENGTH 0x02
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
VALIDATION_SUCCESS = 0,
|
||||
VALIDATION_CRC_ERROR = 1
|
||||
VALIDATION_CRC_ERROR = 1,
|
||||
VALIDATION_HEADER_ERROR = 2,
|
||||
VALIDATION_TYPE_ERROR = 4,
|
||||
VALIDATION_LENGTH_ERROR = 8
|
||||
} validation_result_t;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void rs485_config(void);
|
||||
|
||||
void process_command(uint8_t *cmd, size_t length);
|
||||
|
||||
uint8_t calculate_crc(uint8_t data[], size_t data_length);
|
||||
validation_result_t validate_package_crc(uint8_t *data, size_t data_length);
|
||||
validation_result_t validate_package_header(uint8_t *data);
|
||||
|
||||
validation_result_t validate_package_type(uint8_t *data);
|
||||
|
||||
void process_command(uint8_t* cmd, size_t length);
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
|
||||
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length);
|
||||
validation_result_t validate_package_header(uint8_t* data);
|
||||
validation_result_t validate_package_type(uint8_t* data);
|
||||
validation_result_t validate_data_length(uint8_t* data);
|
||||
void eddy_current_value_report(void);
|
||||
|
||||
void tempture_value_report(void);
|
||||
|
||||
#endif //RS485_H
|
||||
|
@ -15,8 +15,12 @@
|
||||
#include <math.h>
|
||||
#include "i2c.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define TMP112A_ADDR (0x49 << 1)
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
uint32_t tmp112a_get_raw_channel_result(void);
|
||||
|
||||
#endif //TMP112_H
|
||||
|
@ -121,7 +121,7 @@ void USART0_IRQHandler(void) {
|
||||
static uint8_t rx_index = 0;
|
||||
static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
||||
|
||||
if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) {
|
||||
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);
|
||||
|
||||
@ -131,22 +131,12 @@ void USART0_IRQHandler(void) {
|
||||
}
|
||||
}
|
||||
|
||||
if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE) != RESET) {
|
||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
|
||||
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
|
||||
// printf("%c%c%c%c%c%c", 0xB5, 0xF0, 0x02, 0x6F, 0x6B, 0xCC);
|
||||
// printf("%d", rx_index);
|
||||
// printf("%c", rx_buffer[0]);
|
||||
// uint8_t crc = calculate_crc(rx_buffer, rx_index - 2);
|
||||
// printf("%d", crc);
|
||||
// if (calculate_crc(rx_buffer, rx_index - 1) == rx_buffer[rx_index - 1])
|
||||
// // printf("CRC check success");
|
||||
// // rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
||||
|
||||
process_command(rx_buffer, rx_index); // 处理指令
|
||||
// } else {
|
||||
// // printf("CRC check failed");
|
||||
// printf("%c%c%c%c", 0xB5, 0xF2, 0x00, 0xF2);
|
||||
// // fflush(stdout);
|
||||
// }
|
||||
rx_index = 0;
|
||||
|
||||
rx_index = 0; // 重置缓冲区索引
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ int main(void) {
|
||||
ldc1612_single_ch0_config();
|
||||
|
||||
while (1) {
|
||||
delay_ms(500);
|
||||
delay_ms(100);
|
||||
g_eddy_current_value_uint32 = 0;
|
||||
g_temperature_uint32 = 0;
|
||||
g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
|
||||
|
@ -1,63 +0,0 @@
|
||||
//
|
||||
// Created by yelv1 on 24-9-22.
|
||||
//
|
||||
#include "peripheral.h"
|
||||
#include "gd32e23x.h"
|
||||
|
||||
void usart_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_USART0);
|
||||
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_3);
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_3);
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
|
||||
|
||||
usart_deinit(USART0);
|
||||
usart_baudrate_set(USART0, 115200U);
|
||||
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART0);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
|
||||
|
||||
gpio_bit_write(GPIOA, GPIO_PIN_4, SET);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief led blink configuration
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void led_blink_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOB);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
|
||||
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
||||
gpio_bit_write(GPIOB, GPIO_PIN_1, SET);
|
||||
|
||||
rcu_periph_clock_enable(RCU_TIMER13);
|
||||
timer_deinit(RCU_TIMER13);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler =7199;
|
||||
timer_initpara.alignedmode =TIMER_COUNTER_EDGE;
|
||||
timer_initpara.counterdirection =TIMER_COUNTER_UP;
|
||||
timer_initpara.period =999;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER13, &timer_initpara);
|
||||
|
||||
timer_auto_reload_shadow_enable(TIMER13);
|
||||
timer_interrupt_enable(TIMER13, TIMER_INT_UP);
|
||||
nvic_irq_enable(TIMER13_IRQn, 0);
|
||||
timer_enable(TIMER13);
|
||||
}
|
140
src/rs485.c
140
src/rs485.c
@ -10,7 +10,8 @@ extern uint32_t g_eddy_current_value_uint32;
|
||||
uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
|
||||
uint8_t package_data[4] = {0};
|
||||
|
||||
void rs485_config(void) {
|
||||
void rs485_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
||||
rcu_periph_clock_enable(RS485_RCU);
|
||||
|
||||
@ -33,85 +34,119 @@ void rs485_config(void) {
|
||||
|
||||
usart_enable(RS485_PHY);
|
||||
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
||||
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
||||
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
}
|
||||
|
||||
void process_command(uint8_t *cmd, size_t length) {
|
||||
void process_command(uint8_t* cmd, size_t length)
|
||||
{
|
||||
char combined_str[3];
|
||||
validation_result_t validate = VALIDATION_SUCCESS;
|
||||
|
||||
printf("%d", length);
|
||||
validate = (validate_package_header(cmd) |
|
||||
validate_package_type(cmd) |
|
||||
validate_data_length(cmd) |
|
||||
validate_package_crc(cmd, length));
|
||||
|
||||
// validate_package_crc(cmd, length);
|
||||
// if (calculate_crc(cmd, length - 1) != cmd[length - 1]) {
|
||||
// printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3);
|
||||
// }
|
||||
|
||||
// if (cmd[0] == 0xD5) {
|
||||
// if (cmd[1] == 0x03) {
|
||||
// if (cmd[2] == 0x02) {
|
||||
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||
if (strcmp(combined_str, "M1") == 0) {
|
||||
eddy_current_value_report();
|
||||
} else if (strcmp(combined_str, "M2") == 0) {
|
||||
tempture_value_report();
|
||||
} else {
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||
// return;
|
||||
switch (validate)
|
||||
{
|
||||
case VALIDATION_SUCCESS:
|
||||
// printf("%d", length);
|
||||
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||
if (strcmp(combined_str, "M1") == 0)
|
||||
{
|
||||
eddy_current_value_report();
|
||||
}
|
||||
else if (strcmp(combined_str, "M2") == 0)
|
||||
{
|
||||
tempture_value_report();
|
||||
}
|
||||
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, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||
break;
|
||||
case VALIDATION_HEADER_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||
break;
|
||||
case VALIDATION_TYPE_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||
break;
|
||||
case VALIDATION_LENGTH_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// } else {
|
||||
// printf("%c%c%c%c", 0xB5, 0xF1, 0x00, 0xF1);
|
||||
// }
|
||||
// } else {
|
||||
// printf("%c%c%c%c", 0xB5, 0xF5, 0x00, 0xF5);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
uint8_t calculate_crc(uint8_t data[], size_t data_length) {
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
|
||||
for (size_t i = 1; i < data_length - 1; i++) {
|
||||
for (uint8_t i = 1; i < data_length - 1; i++)
|
||||
{
|
||||
crc += data[i];
|
||||
}
|
||||
|
||||
return (uint8_t) (crc & 0xFF);
|
||||
return (uint8_t)(crc & 0xFF);
|
||||
}
|
||||
|
||||
validation_result_t validate_package_crc(uint8_t *data, size_t data_length) {
|
||||
uint8_t crc = calculate_crc(data, data_length - 2);
|
||||
|
||||
if (crc == data[data_length]) {
|
||||
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length)
|
||||
{
|
||||
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
} else {
|
||||
printf("%c%c%c%c", 0xB5, 0xF3, 0x01, 0xF3);
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_CRC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
validation_result_t validate_package_header(uint8_t *data) {
|
||||
if (data[0] == 0xD5) {
|
||||
validation_result_t validate_package_header(uint8_t* data)
|
||||
{
|
||||
if (data[0] == LDC1612_PACKAGE_HEADER)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
} else {
|
||||
printf("%c%c%c%c", 0xB5, 0xF3, 0x02, 0xF3);
|
||||
return VALIDATION_CRC_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_HEADER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
validation_result_t validate_package_type(uint8_t *data) {
|
||||
if (data[1] == 0x03) {
|
||||
validation_result_t validate_package_type(uint8_t* data)
|
||||
{
|
||||
if (data[1] == LDC1612_BOARD_TYPE)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
} else {
|
||||
printf("%c%c%c%c", 0xB5, 0xF3, 0x03, 0xF3);
|
||||
return VALIDATION_CRC_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_TYPE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
void eddy_current_value_report(void) {
|
||||
// package_header[1] = 0xF0; //eddy current
|
||||
validation_result_t validate_data_length(uint8_t* data)
|
||||
{
|
||||
if (data[2] == LDC1612_PACKAGE_LENGTH)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_LENGTH_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
void eddy_current_value_report(void)
|
||||
{
|
||||
package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF;
|
||||
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
|
||||
package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF;
|
||||
@ -121,15 +156,13 @@ void eddy_current_value_report(void) {
|
||||
memcpy(combined_data, package_header, 3);
|
||||
memcpy(combined_data + 3, package_data, 4);
|
||||
|
||||
// printf("EddyCurrent: %x\r\n", g_eddy_current_value_uint32);
|
||||
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, 7));
|
||||
}
|
||||
|
||||
void tempture_value_report(void) {
|
||||
// package_header[1] = 0xF1; //temperature
|
||||
|
||||
void tempture_value_report(void)
|
||||
{
|
||||
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF;
|
||||
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
|
||||
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF;
|
||||
@ -139,7 +172,6 @@ void tempture_value_report(void) {
|
||||
memcpy(combined_data, package_header, 3);
|
||||
memcpy(combined_data + 3, package_data, 4);
|
||||
|
||||
// printf("Temperature: %x\r\n", g_temperature_uint32);
|
||||
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, 7));
|
||||
|
Loading…
x
Reference in New Issue
Block a user