generated from hulk/gd32e23x_template
Compare commits
6 Commits
21a5a2e783
...
4dc9054544
Author | SHA1 | Date | |
---|---|---|---|
4dc9054544 | |||
5838f18742 | |||
dc8bff918d | |||
f550619217 | |||
b307160ba0 | |||
8e2cdd1ac5 |
12
inc/rs485.h
12
inc/rs485.h
@ -26,7 +26,17 @@
|
|||||||
|
|
||||||
#define RX_BUFFER_SIZE 64
|
#define RX_BUFFER_SIZE 64
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
VALIDATION_SUCCESS = 0,
|
||||||
|
VALIDATION_CRC_ERROR = 1
|
||||||
|
} validation_result_t;
|
||||||
|
|
||||||
void rs485_config(void);
|
void rs485_config(void);
|
||||||
void process_command(char *cmd);
|
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_header(uint8_t *data);
|
||||||
|
validation_result_t validate_package_type(uint8_t *data);
|
||||||
|
void eddy_current_value_report(void);
|
||||||
|
void tempture_value_report(void);
|
||||||
|
|
||||||
#endif //RS485_H
|
#endif //RS485_H
|
@ -39,19 +39,15 @@ OF SUCH DAMAGE.
|
|||||||
#include "rs485.h"
|
#include "rs485.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
|
||||||
char rx_buffer[RX_BUFFER_SIZE];
|
|
||||||
uint8_t rx_index = 0;
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief this function handles NMI exception
|
\brief this function handles NMI exception
|
||||||
\param[in] none
|
\param[in] none
|
||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void NMI_Handler(void)
|
void NMI_Handler(void) {
|
||||||
{
|
|
||||||
/* if NMI exception occurs, go to infinite loop */
|
/* if NMI exception occurs, go to infinite loop */
|
||||||
while(1) {
|
while (1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,10 +57,9 @@ void NMI_Handler(void)
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void HardFault_Handler(void)
|
void HardFault_Handler(void) {
|
||||||
{
|
|
||||||
/* if Hard Fault exception occurs, go to infinite loop */
|
/* if Hard Fault exception occurs, go to infinite loop */
|
||||||
while(1) {
|
while (1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,10 +69,9 @@ void HardFault_Handler(void)
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void SVC_Handler(void)
|
void SVC_Handler(void) {
|
||||||
{
|
|
||||||
/* if SVC exception occurs, go to infinite loop */
|
/* if SVC exception occurs, go to infinite loop */
|
||||||
while(1) {
|
while (1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,10 +81,9 @@ void SVC_Handler(void)
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void PendSV_Handler(void)
|
void PendSV_Handler(void) {
|
||||||
{
|
|
||||||
/* if PendSV exception occurs, go to infinite loop */
|
/* if PendSV exception occurs, go to infinite loop */
|
||||||
while(1) {
|
while (1) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +93,7 @@ void PendSV_Handler(void)
|
|||||||
\param[out] none
|
\param[out] none
|
||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
void SysTick_Handler(void)
|
void SysTick_Handler(void) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,12 +103,10 @@ void SysTick_Handler(void)
|
|||||||
* @retval None
|
* @retval None
|
||||||
*/
|
*/
|
||||||
void TIMER16_IRQHandler(void) {
|
void TIMER16_IRQHandler(void) {
|
||||||
if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET)
|
if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) {
|
||||||
{
|
|
||||||
timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP);
|
timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP);
|
||||||
static uint8_t led_status = 0;
|
static uint8_t led_status = 0;
|
||||||
if (led_status)
|
if (led_status) {
|
||||||
{
|
|
||||||
gpio_bit_write(LED_PORT, LED_PIN, RESET);
|
gpio_bit_write(LED_PORT, LED_PIN, RESET);
|
||||||
timer_autoreload_value_config(LED_TIMER, 19200);
|
timer_autoreload_value_config(LED_TIMER, 19200);
|
||||||
} else {
|
} else {
|
||||||
@ -128,21 +118,32 @@ void TIMER16_IRQHandler(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void USART0_IRQHandler(void) {
|
void USART0_IRQHandler(void) {
|
||||||
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
|
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);
|
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
|
||||||
uint8_t received_data = (uint8_t)usart_data_receive(USART0);
|
uint8_t received_data = (uint8_t) usart_data_receive(USART0);
|
||||||
|
|
||||||
// 将接收到的数据存储到缓冲区
|
// 将接收到的数据存储到缓冲区
|
||||||
if(rx_index < RX_BUFFER_SIZE - 1) {
|
if (rx_index < RX_BUFFER_SIZE - 1) {
|
||||||
rx_buffer[rx_index++] = received_data;
|
rx_buffer[rx_index++] = received_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否接收到换行符,表示指令结束
|
// 检查是否接收到换行符,表示指令结束
|
||||||
if(received_data == '\n') {
|
if (received_data == '\n') {
|
||||||
rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
if (calculate_crc(rx_buffer, rx_index - 2) == rx_buffer[rx_index - 2]) {
|
||||||
process_command(rx_buffer); // 处理指令
|
// printf("CRC check success\r\n");
|
||||||
rx_index = 0; // 重置缓冲区索引
|
// rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
||||||
|
process_command(rx_buffer, rx_index); // 处理指令
|
||||||
|
rx_index = 0; // 重置缓冲区索引
|
||||||
|
} else {
|
||||||
|
// printf("CRC check failed\r\n");
|
||||||
|
printf("%c%c%c%c\r\n", 0xB5, 0xF2, 0x00, 0xF2);
|
||||||
|
rx_index = 0; // 重置缓冲区索引
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ void ldc1612_set_conversion_time(uint8_t channel, uint16_t result) {
|
|||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
data[0] = (result >> 8) & 0xFF;
|
data[0] = (result >> 8) & 0xFF;
|
||||||
data[1] = result & 0xFF;
|
data[1] = result & 0xFF;
|
||||||
i2c_read_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
|
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief set conversion offset.
|
/** @brief set conversion offset.
|
||||||
@ -23,7 +23,7 @@ void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result) {
|
|||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
data[0] = (result >> 8) & 0xFF;
|
data[0] = (result >> 8) & 0xFF;
|
||||||
data[1] = result & 0xFF;
|
data[1] = result & 0xFF;
|
||||||
i2c_read_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
|
i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief Before conversion,wait LC sensor stabilize for a short time.
|
/** @brief Before conversion,wait LC sensor stabilize for a short time.
|
||||||
@ -34,7 +34,7 @@ void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result) {
|
|||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
data[0] = (result >> 8) & 0xFF;
|
data[0] = (result >> 8) & 0xFF;
|
||||||
data[1] = result & 0xFF;
|
data[1] = result & 0xFF;
|
||||||
i2c_read_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
|
i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief set input frequency divide and fref divide.
|
/** @brief set input frequency divide and fref divide.
|
||||||
@ -66,7 +66,7 @@ void ldc1612_set_freq_divide(uint8_t channel) {
|
|||||||
data[1] = value & 0xFF;
|
data[1] = value & 0xFF;
|
||||||
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div);
|
// printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div);
|
||||||
|
|
||||||
i2c_read_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
|
i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief Error output config.
|
/** @brief Error output config.
|
||||||
@ -77,7 +77,7 @@ void ldc1612_set_error_config(uint16_t value) {
|
|||||||
data[0] = (value >> 8) & 0xFF;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
data[1] = value & 0xFF;
|
data[1] = value & 0xFF;
|
||||||
|
|
||||||
i2c_read_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
|
i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief mux config.
|
/** @brief mux config.
|
||||||
@ -88,7 +88,7 @@ void ldc1612_set_mux_config(uint16_t value) {
|
|||||||
data[0] = (value >> 8) & 0xFF;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
data[1] = value & 0xFF;
|
data[1] = value & 0xFF;
|
||||||
|
|
||||||
i2c_read_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
|
i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief reset sensor.
|
/** @brief reset sensor.
|
||||||
@ -98,7 +98,7 @@ void ldc1612_reset_sensor(void) {
|
|||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
data[0] = 0x80;
|
data[0] = 0x80;
|
||||||
data[1] = 0x00;
|
data[1] = 0x00;
|
||||||
i2c_read_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
|
i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief set drive current of sensor.
|
/** @brief set drive current of sensor.
|
||||||
@ -109,7 +109,7 @@ void ldc1612_set_drive_current(uint8_t channel, uint16_t value) {
|
|||||||
data[0] = (value >> 8) & 0xFF;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
data[1] = value & 0xFF;
|
data[1] = value & 0xFF;
|
||||||
|
|
||||||
i2c_read_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
|
i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable ..
|
/** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable ..
|
||||||
@ -120,7 +120,7 @@ void ldc1612_set_sensor_config(uint16_t value) {
|
|||||||
data[0] = (value >> 8) & 0xFF;
|
data[0] = (value >> 8) & 0xFF;
|
||||||
data[1] = value & 0xFF;
|
data[1] = value & 0xFF;
|
||||||
|
|
||||||
i2c_read_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
|
i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ldc1612_single_ch0_config(void) {
|
void ldc1612_single_ch0_config(void) {
|
||||||
|
107
src/rs485.c
107
src/rs485.c
@ -7,6 +7,9 @@
|
|||||||
extern uint32_t g_temperature_uint32;
|
extern uint32_t g_temperature_uint32;
|
||||||
extern uint32_t g_eddy_current_value_uint32;
|
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_GPIO_RCU);
|
||||||
rcu_periph_clock_enable(RS485_RCU);
|
rcu_periph_clock_enable(RS485_RCU);
|
||||||
@ -34,37 +37,93 @@ void rs485_config(void) {
|
|||||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_command(char *cmd) {
|
void process_command(uint8_t *cmd, size_t length) {
|
||||||
if (strncmp(cmd, "M1", 2) == 0) {
|
char combined_str[3];
|
||||||
// printf("M1 -=-=- OK!\r\n");
|
|
||||||
printf("EddyCurrent: %lu\r\n", g_eddy_current_value_uint32);
|
if (cmd[0] == 0xD5) {
|
||||||
} else if (strncmp(cmd, "M2", 2) == 0) {
|
if (cmd[1] == 0x03) {
|
||||||
// printf("M2 -=-=- OK!\r\n");
|
if (cmd[2] == 0x02) {
|
||||||
printf("Temperature: %lu\r\n", g_temperature_uint32);
|
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||||
// } else if (strncmp(cmd, "M3", 2) == 0) {
|
if (strcmp(combined_str, "M1") == 0) {
|
||||||
// char *param_str = cmd + 2; // Skip "M3"
|
|
||||||
// int param = atoi(param_str + 1); // Skip "S" and convert to integer
|
eddy_current_value_report();
|
||||||
// if (param >= 0 && param <= 100) {
|
} else if (strcmp(combined_str, "M2") == 0) {
|
||||||
// printf("M3 with parameter %d -=-=- OK!\r\n", param);
|
tempture_value_report();
|
||||||
// } else {
|
} else {
|
||||||
// printf("Invalid parameter for M3 command!\r\n");
|
printf("%c%c%c%c%c%c%c\r\n", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||||
// }
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("%c%c%c%c\r\n", 0xB5, 0xF1, 0x00, 0xF1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("%c%c%c%c\r\n", 0xB5, 0xF5, 0x00, 0xF5);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Invalid Command!\r\n");
|
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t calculate_crc(uint8_t package_header[3], uint8_t package_data[], size_t data_length) {
|
uint8_t calculate_crc(uint8_t data[], size_t data_length) {
|
||||||
uint8_t crc = 0;
|
uint8_t crc = 0;
|
||||||
|
|
||||||
/* Calculate CRC for header */
|
for (size_t i = 1; i < data_length; i++) {
|
||||||
for (int i = 1; i < 3; i++) {
|
crc += data[i];
|
||||||
crc += package_header[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < data_length; i++) {
|
|
||||||
crc += package_data[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (uint8_t)(crc & 0xFF);
|
return (uint8_t)(crc & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validation_result_t validate_package_header(uint8_t *data) {
|
||||||
|
if (data[0] == 0xD5) {
|
||||||
|
return VALIDATION_SUCCESS;
|
||||||
|
} else {
|
||||||
|
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
|
return VALIDATION_CRC_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
validation_result_t validate_package_type(uint8_t *data) {
|
||||||
|
if (data[1] == 0x03) {
|
||||||
|
return VALIDATION_SUCCESS;
|
||||||
|
} else {
|
||||||
|
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
|
return VALIDATION_CRC_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void eddy_current_value_report(void) {
|
||||||
|
// package_header[1] = 0xF0; //eddy current
|
||||||
|
|
||||||
|
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;
|
||||||
|
package_data[3] = g_eddy_current_value_uint32 & 0xFF;
|
||||||
|
|
||||||
|
uint8_t combined_data[7];
|
||||||
|
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\r\n", calculate_crc(combined_data, 7));
|
||||||
|
}
|
||||||
|
|
||||||
|
void tempture_value_report(void) {
|
||||||
|
// package_header[1] = 0xF1; //temperature
|
||||||
|
|
||||||
|
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF;
|
||||||
|
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
|
||||||
|
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF;
|
||||||
|
package_data[3] = g_temperature_uint32 & 0xFF;
|
||||||
|
|
||||||
|
uint8_t combined_data[7];
|
||||||
|
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\r\n", calculate_crc(combined_data, 7));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user