generated from hulk/gd32e23x_template
Compare commits
No commits in common. "fa7f364bdbe1b62508649c75a9be837f9bff97e7" and "4dc9054544172482d7babef453ce061c1191b446" have entirely different histories.
fa7f364bdb
...
4dc9054544
@ -8,7 +8,6 @@
|
|||||||
#include "gd32e23x_it.h"
|
#include "gd32e23x_it.h"
|
||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "main.h"
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -35,6 +35,4 @@ OF SUCH DAMAGE.
|
|||||||
#ifndef MAIN_H
|
#ifndef MAIN_H
|
||||||
#define MAIN_H
|
#define MAIN_H
|
||||||
|
|
||||||
// #define DEBUG_VERBOES
|
|
||||||
|
|
||||||
#endif /* MAIN_H */
|
#endif /* MAIN_H */
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
#define RS485_EN_PIN GPIO_PIN_1
|
#define RS485_EN_PIN GPIO_PIN_1
|
||||||
|
|
||||||
#define RX_BUFFER_SIZE 32
|
#define RX_BUFFER_SIZE 64
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VALIDATION_SUCCESS = 0,
|
VALIDATION_SUCCESS = 0,
|
||||||
@ -32,17 +32,11 @@ typedef enum {
|
|||||||
} validation_result_t;
|
} validation_result_t;
|
||||||
|
|
||||||
void rs485_config(void);
|
void rs485_config(void);
|
||||||
|
|
||||||
void process_command(uint8_t *cmd, size_t length);
|
void process_command(uint8_t *cmd, size_t length);
|
||||||
|
|
||||||
uint8_t calculate_crc(uint8_t data[], size_t data_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_header(uint8_t *data);
|
||||||
|
|
||||||
validation_result_t validate_package_type(uint8_t *data);
|
validation_result_t validate_package_type(uint8_t *data);
|
||||||
|
|
||||||
void eddy_current_value_report(void);
|
void eddy_current_value_report(void);
|
||||||
|
|
||||||
void tempture_value_report(void);
|
void tempture_value_report(void);
|
||||||
|
|
||||||
#endif //RS485_H
|
#endif //RS485_H
|
@ -121,7 +121,7 @@ void USART0_IRQHandler(void) {
|
|||||||
static uint8_t rx_index = 0;
|
static uint8_t rx_index = 0;
|
||||||
static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
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);
|
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);
|
||||||
|
|
||||||
@ -129,23 +129,21 @@ void USART0_IRQHandler(void) {
|
|||||||
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 (usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE) != RESET) {
|
// 检查是否接收到换行符,表示指令结束
|
||||||
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
|
if (received_data == '\n') {
|
||||||
// printf("%c%c%c%c%c%c", 0xB5, 0xF0, 0x02, 0x6F, 0x6B, 0xCC);
|
if (calculate_crc(rx_buffer, rx_index - 2) == rx_buffer[rx_index - 2]) {
|
||||||
// printf("%d", rx_index);
|
// printf("CRC check success\r\n");
|
||||||
// printf("%c", rx_buffer[0]);
|
// rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
||||||
// uint8_t crc = calculate_crc(rx_buffer, rx_index - 2);
|
process_command(rx_buffer, rx_index); // 处理指令
|
||||||
// printf("%d", crc);
|
rx_index = 0; // 重置缓冲区索引
|
||||||
// if (calculate_crc(rx_buffer, rx_index - 1) == rx_buffer[rx_index - 1])
|
} else {
|
||||||
// // printf("CRC check success");
|
// printf("CRC check failed\r\n");
|
||||||
// // rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
printf("%c%c%c%c\r\n", 0xB5, 0xF2, 0x00, 0xF2);
|
||||||
process_command(rx_buffer, rx_index); // 处理指令
|
rx_index = 0; // 重置缓冲区索引
|
||||||
// } else {
|
return;
|
||||||
// // printf("CRC check failed");
|
}
|
||||||
// printf("%c%c%c%c", 0xB5, 0xF2, 0x00, 0xF2);
|
|
||||||
// // fflush(stdout);
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
src/i2c.c
34
src/i2c.c
@ -158,9 +158,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c bus is busy in WRITE BYTE!\n");
|
printf("i2c bus is busy in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_SEND_ADDRESS:
|
case I2C_SEND_ADDRESS:
|
||||||
@ -175,9 +173,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends start signal timeout in WRITE BYTE!\n");
|
printf("i2c master sends start signal timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
case I2C_CLEAR_ADDRESS_FLAG:
|
||||||
@ -192,9 +188,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
|
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_TRANSMIT_DATA:
|
case I2C_TRANSMIT_DATA:
|
||||||
@ -209,9 +203,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
@ -225,9 +217,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends MSB data timeout in WRITE BYTE!\n");
|
printf("i2c master sends MSB data timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
@ -242,9 +232,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends LSB data timeout in WRITE BYTE!\n");
|
printf("i2c master sends LSB data timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
@ -257,9 +245,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
printf("i2c master sends data timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_STOP:
|
case I2C_STOP:
|
||||||
@ -276,18 +262,14 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
|
|||||||
} else {
|
} else {
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
|
printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
i2c_timeout_flag = I2C_OK;
|
i2c_timeout_flag = I2C_OK;
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends start signal in WRITE BYTE.\n");
|
printf("i2c master sends start signal in WRITE BYTE.\n");
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -319,9 +301,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
// i2c_bus_reset();
|
// i2c_bus_reset();
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c bus is busy in READ!\n");
|
printf("i2c bus is busy in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* send the start signal */
|
/* send the start signal */
|
||||||
@ -348,9 +328,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
read_cycle = RESET;
|
read_cycle = RESET;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends start signal timeout in READ!\n");
|
printf("i2c master sends start signal timeout in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_CLEAR_ADDRESS_FLAG:
|
case I2C_CLEAR_ADDRESS_FLAG:
|
||||||
@ -370,9 +348,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
read_cycle = RESET;
|
read_cycle = RESET;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master clears address flag timeout in READ!\n");
|
printf("i2c master clears address flag timeout in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case I2C_TRANSMIT_DATA:
|
case I2C_TRANSMIT_DATA:
|
||||||
@ -389,9 +365,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
read_cycle = RESET;
|
read_cycle = RESET;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master wait data buffer is empty timeout in READ!\n");
|
printf("i2c master wait data buffer is empty timeout in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
/* wait until BTC bit is set */
|
/* wait until BTC bit is set */
|
||||||
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
while ((!i2c_flag_get(I2C0, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
|
||||||
@ -405,9 +379,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
read_cycle = RESET;
|
read_cycle = RESET;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends register address timeout in READ!\n");
|
printf("i2c master sends register address timeout in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while (number_of_byte) {
|
while (number_of_byte) {
|
||||||
@ -432,9 +404,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
read_cycle = 0;
|
read_cycle = 0;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends data timeout in READ!\n");
|
printf("i2c master sends data timeout in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
@ -454,9 +424,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
timeout = 0;
|
timeout = 0;
|
||||||
state = I2C_START;
|
state = I2C_START;
|
||||||
read_cycle = 0;
|
read_cycle = 0;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends stop signal timeout in READ!\n");
|
printf("i2c master sends stop signal timeout in READ!\n");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -464,9 +432,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
|
|||||||
read_cycle = 0;
|
read_cycle = 0;
|
||||||
i2c_timeout_flag = I2C_OK;
|
i2c_timeout_flag = I2C_OK;
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
#ifdef DEBUG_VERBOES
|
|
||||||
printf("i2c master sends start signal in READ.\n");
|
printf("i2c master sends start signal in READ.\n");
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,10 +143,10 @@ void ldc1612_iic_get_sensor_infomation(void) {
|
|||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
// ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data);
|
// ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data);
|
||||||
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
|
i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data);
|
||||||
printf("\tManufacturer: 0x%x", (data[0] << 8) | data[1]);
|
printf("\tManufacturer: 0x%x\r\n", (data[0] << 8) | data[1]);
|
||||||
// ldc1612_iic_read_16bits(READ_DEVICE_ID, data);
|
// ldc1612_iic_read_16bits(READ_DEVICE_ID, data);
|
||||||
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
|
i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data);
|
||||||
printf("\tDevice: 0x%x", (data[0] << 8) | data[1]);
|
printf("\tDevice: 0x%x\r\n", (data[0] << 8) | data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t ldc1612_get_manufacturer_id(void) {
|
uint16_t ldc1612_get_manufacturer_id(void) {
|
||||||
|
@ -25,7 +25,6 @@ uint32_t g_eddy_current_value_uint32;
|
|||||||
\retval none
|
\retval none
|
||||||
*/
|
*/
|
||||||
int main(void) {
|
int main(void) {
|
||||||
setbuf(stdout, NULL);
|
|
||||||
/* configure systick */
|
/* configure systick */
|
||||||
systick_config();
|
systick_config();
|
||||||
/* configure USART */
|
/* configure USART */
|
||||||
@ -36,7 +35,7 @@ int main(void) {
|
|||||||
i2c_gpio_config();
|
i2c_gpio_config();
|
||||||
i2c_config();
|
i2c_config();
|
||||||
|
|
||||||
// ldc1612_iic_get_sensor_infomation();
|
ldc1612_iic_get_sensor_infomation();
|
||||||
/* configure LDC1612 */
|
/* configure LDC1612 */
|
||||||
ldc1612_single_ch0_config();
|
ldc1612_single_ch0_config();
|
||||||
|
|
||||||
@ -46,7 +45,6 @@ int main(void) {
|
|||||||
g_temperature_uint32 = 0;
|
g_temperature_uint32 = 0;
|
||||||
g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
|
g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
|
||||||
g_temperature_uint32 = tmp112a_get_raw_channel_result();
|
g_temperature_uint32 = tmp112a_get_raw_channel_result();
|
||||||
// printf("1");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
69
src/rs485.c
69
src/rs485.c
@ -33,69 +33,52 @@ void rs485_config(void) {
|
|||||||
|
|
||||||
usart_enable(RS485_PHY);
|
usart_enable(RS485_PHY);
|
||||||
|
|
||||||
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
|
||||||
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
|
||||||
|
|
||||||
nvic_irq_enable(USART0_IRQn, 0);
|
nvic_irq_enable(USART0_IRQn, 0);
|
||||||
|
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_command(uint8_t *cmd, size_t length) {
|
void process_command(uint8_t *cmd, size_t length) {
|
||||||
char combined_str[3];
|
char combined_str[3];
|
||||||
|
|
||||||
// printf("%d", length);
|
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) {
|
||||||
|
|
||||||
// validate_package_crc(cmd, length);
|
eddy_current_value_report();
|
||||||
// if (calculate_crc(cmd, length - 1) != cmd[length - 1]) {
|
} else if (strcmp(combined_str, "M2") == 0) {
|
||||||
// printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3);
|
tempture_value_report();
|
||||||
// }
|
} else {
|
||||||
|
printf("%c%c%c%c%c%c%c\r\n", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||||
// if (cmd[0] == 0xD5) {
|
return;
|
||||||
// if (cmd[1] == 0x03) {
|
}
|
||||||
// if (cmd[2] == 0x02) {
|
} else {
|
||||||
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
printf("%c%c%c%c\r\n", 0xB5, 0xF1, 0x00, 0xF1);
|
||||||
if (strcmp(combined_str, "M1") == 0) {
|
}
|
||||||
eddy_current_value_report();
|
} else {
|
||||||
} else if (strcmp(combined_str, "M2") == 0) {
|
printf("%c%c%c%c\r\n", 0xB5, 0xF5, 0x00, 0xF5);
|
||||||
tempture_value_report();
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
// return;
|
|
||||||
}
|
}
|
||||||
// } 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[], size_t data_length) {
|
||||||
uint8_t crc = 0;
|
uint8_t crc = 0;
|
||||||
|
|
||||||
for (size_t i = 1; i < data_length - 1; i++) {
|
for (size_t i = 1; i < data_length; i++) {
|
||||||
crc += data[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]) {
|
|
||||||
return VALIDATION_SUCCESS;
|
|
||||||
} else {
|
|
||||||
printf("%c%c%c%c", 0xB5, 0xF3, 0x01, 0xF3);
|
|
||||||
return VALIDATION_CRC_ERROR;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validation_result_t validate_package_header(uint8_t *data) {
|
validation_result_t validate_package_header(uint8_t *data) {
|
||||||
if (data[0] == 0xD5) {
|
if (data[0] == 0xD5) {
|
||||||
return VALIDATION_SUCCESS;
|
return VALIDATION_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
printf("%c%c%c%c", 0xB5, 0xF3, 0x02, 0xF3);
|
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
return VALIDATION_CRC_ERROR;
|
return VALIDATION_CRC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,7 +87,7 @@ validation_result_t validate_package_type(uint8_t *data) {
|
|||||||
if (data[1] == 0x03) {
|
if (data[1] == 0x03) {
|
||||||
return VALIDATION_SUCCESS;
|
return VALIDATION_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
printf("%c%c%c%c", 0xB5, 0xF3, 0x03, 0xF3);
|
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
return VALIDATION_CRC_ERROR;
|
return VALIDATION_CRC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,7 +107,7 @@ void eddy_current_value_report(void) {
|
|||||||
// printf("EddyCurrent: %x\r\n", g_eddy_current_value_uint32);
|
// 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", 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%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
|
||||||
printf("%c", calculate_crc(combined_data, 7));
|
printf("%c\r\n", calculate_crc(combined_data, 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
void tempture_value_report(void) {
|
void tempture_value_report(void) {
|
||||||
@ -142,5 +125,5 @@ void tempture_value_report(void) {
|
|||||||
// printf("Temperature: %x\r\n", g_temperature_uint32);
|
// 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", 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%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
|
||||||
printf("%c", calculate_crc(combined_data, 7));
|
printf("%c\r\n", calculate_crc(combined_data, 7));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user