generated from hulk/gd32e23x_template
Compare commits
4 Commits
4dc9054544
...
fa7f364bdb
Author | SHA1 | Date | |
---|---|---|---|
fa7f364bdb | |||
2d273efef5 | |||
8fa7ed32d8 | |||
05fb90c727 |
@ -8,6 +8,7 @@
|
|||||||
#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,4 +35,6 @@ OF SUCH DAMAGE.
|
|||||||
#ifndef MAIN_H
|
#ifndef MAIN_H
|
||||||
#define MAIN_H
|
#define MAIN_H
|
||||||
|
|
||||||
|
// #define DEBUG_VERBOES
|
||||||
|
|
||||||
#endif /* MAIN_H */
|
#endif /* MAIN_H */
|
||||||
|
10
inc/rs485.h
10
inc/rs485.h
@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
#define RS485_EN_PIN GPIO_PIN_1
|
#define RS485_EN_PIN GPIO_PIN_1
|
||||||
|
|
||||||
#define RX_BUFFER_SIZE 64
|
#define RX_BUFFER_SIZE 32
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VALIDATION_SUCCESS = 0,
|
VALIDATION_SUCCESS = 0,
|
||||||
@ -32,11 +32,17 @@ 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 (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) {
|
||||||
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,21 +129,23 @@ 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) {
|
||||||
if (received_data == '\n') {
|
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
|
||||||
if (calculate_crc(rx_buffer, rx_index - 2) == rx_buffer[rx_index - 2]) {
|
// printf("%c%c%c%c%c%c", 0xB5, 0xF0, 0x02, 0x6F, 0x6B, 0xCC);
|
||||||
// printf("CRC check success\r\n");
|
// printf("%d", rx_index);
|
||||||
// rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
// printf("%c", rx_buffer[0]);
|
||||||
process_command(rx_buffer, rx_index); // 处理指令
|
// uint8_t crc = calculate_crc(rx_buffer, rx_index - 2);
|
||||||
rx_index = 0; // 重置缓冲区索引
|
// printf("%d", crc);
|
||||||
} else {
|
// if (calculate_crc(rx_buffer, rx_index - 1) == rx_buffer[rx_index - 1])
|
||||||
// printf("CRC check failed\r\n");
|
// // printf("CRC check success");
|
||||||
printf("%c%c%c%c\r\n", 0xB5, 0xF2, 0x00, 0xF2);
|
// // rx_buffer[rx_index] = '\0'; // 添加字符串结束符
|
||||||
rx_index = 0; // 重置缓冲区索引
|
process_command(rx_buffer, rx_index); // 处理指令
|
||||||
return;
|
// } else {
|
||||||
}
|
// // printf("CRC check failed");
|
||||||
|
// printf("%c%c%c%c", 0xB5, 0xF2, 0x00, 0xF2);
|
||||||
}
|
// // fflush(stdout);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
src/i2c.c
34
src/i2c.c
@ -158,7 +158,9 @@ 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:
|
||||||
@ -173,7 +175,9 @@ 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:
|
||||||
@ -188,7 +192,9 @@ 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:
|
||||||
@ -203,7 +209,9 @@ 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 */
|
||||||
@ -217,7 +225,9 @@ 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 */
|
||||||
@ -232,7 +242,9 @@ 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 */
|
||||||
@ -245,7 +257,9 @@ 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:
|
||||||
@ -262,14 +276,18 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -301,7 +319,9 @@ 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 */
|
||||||
@ -328,7 +348,9 @@ 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:
|
||||||
@ -348,7 +370,9 @@ 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:
|
||||||
@ -365,7 +389,9 @@ 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)) {
|
||||||
@ -379,7 +405,9 @@ 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) {
|
||||||
@ -404,7 +432,9 @@ 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;
|
||||||
@ -424,7 +454,9 @@ 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:
|
||||||
@ -432,7 +464,9 @@ 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\r\n", (data[0] << 8) | data[1]);
|
printf("\tManufacturer: 0x%x", (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\r\n", (data[0] << 8) | data[1]);
|
printf("\tDevice: 0x%x", (data[0] << 8) | data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t ldc1612_get_manufacturer_id(void) {
|
uint16_t ldc1612_get_manufacturer_id(void) {
|
||||||
|
@ -25,6 +25,7 @@ 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 */
|
||||||
@ -35,7 +36,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();
|
||||||
|
|
||||||
@ -45,6 +46,7 @@ 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,52 +33,69 @@ void rs485_config(void) {
|
|||||||
|
|
||||||
usart_enable(RS485_PHY);
|
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_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];
|
char combined_str[3];
|
||||||
|
|
||||||
if (cmd[0] == 0xD5) {
|
// printf("%d", length);
|
||||||
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();
|
// validate_package_crc(cmd, length);
|
||||||
} else if (strcmp(combined_str, "M2") == 0) {
|
// if (calculate_crc(cmd, length - 1) != cmd[length - 1]) {
|
||||||
tempture_value_report();
|
// printf("%c%c%c%c", 0xB5, 0xF3, 0x00, 0xF3);
|
||||||
} else {
|
// }
|
||||||
printf("%c%c%c%c%c%c%c\r\n", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
|
||||||
return;
|
// if (cmd[0] == 0xD5) {
|
||||||
}
|
// if (cmd[1] == 0x03) {
|
||||||
} else {
|
// if (cmd[2] == 0x02) {
|
||||||
printf("%c%c%c%c\r\n", 0xB5, 0xF1, 0x00, 0xF1);
|
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||||
}
|
if (strcmp(combined_str, "M1") == 0) {
|
||||||
} else {
|
eddy_current_value_report();
|
||||||
printf("%c%c%c%c\r\n", 0xB5, 0xF5, 0x00, 0xF5);
|
} else if (strcmp(combined_str, "M2") == 0) {
|
||||||
}
|
tempture_value_report();
|
||||||
} else {
|
} else {
|
||||||
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||||
|
// 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; i++) {
|
for (size_t i = 1; i < data_length - 1; 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\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
printf("%c%c%c%c", 0xB5, 0xF3, 0x02, 0xF3);
|
||||||
return VALIDATION_CRC_ERROR;
|
return VALIDATION_CRC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +104,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\r\n", 0xB5, 0xF3, 0x00, 0xF3);
|
printf("%c%c%c%c", 0xB5, 0xF3, 0x03, 0xF3);
|
||||||
return VALIDATION_CRC_ERROR;
|
return VALIDATION_CRC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,7 +124,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\r\n", calculate_crc(combined_data, 7));
|
printf("%c", calculate_crc(combined_data, 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
void tempture_value_report(void) {
|
void tempture_value_report(void) {
|
||||||
@ -125,5 +142,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\r\n", calculate_crc(combined_data, 7));
|
printf("%c", calculate_crc(combined_data, 7));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user