Compare commits

..

No commits in common. "fa7f364bdbe1b62508649c75a9be837f9bff97e7" and "4dc9054544172482d7babef453ce061c1191b446" have entirely different histories.

8 changed files with 47 additions and 111 deletions

View File

@ -8,7 +8,6 @@
#include "gd32e23x_it.h"
#include "gd32e23x.h"
#include "systick.h"
#include "main.h"
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

View File

@ -35,6 +35,4 @@ OF SUCH DAMAGE.
#ifndef MAIN_H
#define MAIN_H
// #define DEBUG_VERBOES
#endif /* MAIN_H */

View File

@ -24,7 +24,7 @@
#define RS485_EN_PIN GPIO_PIN_1
#define RX_BUFFER_SIZE 32
#define RX_BUFFER_SIZE 64
typedef enum {
VALIDATION_SUCCESS = 0,
@ -32,17 +32,11 @@ typedef enum {
} 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 eddy_current_value_report(void);
void tempture_value_report(void);
#endif //RS485_H

View File

@ -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);
@ -129,23 +129,21 @@ void USART0_IRQHandler(void) {
if (rx_index < RX_BUFFER_SIZE - 1) {
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);
// 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);
// }
// 检查是否接收到换行符,表示指令结束
if (received_data == '\n') {
if (calculate_crc(rx_buffer, rx_index - 2) == rx_buffer[rx_index - 2]) {
// printf("CRC check success\r\n");
// 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;
}
}
}
}

View File

@ -158,9 +158,7 @@ uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2])
} else {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c bus is busy in WRITE BYTE!\n");
#endif
}
break;
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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal timeout in WRITE BYTE!\n");
#endif
}
break;
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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master clears address flag timeout in WRITE BYTE!\n");
#endif
}
break;
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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends data timeout in WRITE BYTE!\n");
#endif
}
/* 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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends MSB data timeout in WRITE BYTE!\n");
#endif
}
/* 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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends LSB data timeout in WRITE BYTE!\n");
#endif
}
/* 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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends data timeout in WRITE BYTE!\n");
#endif
}
break;
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 {
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
#endif
}
break;
default:
state = I2C_START;
i2c_timeout_flag = I2C_OK;
timeout = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal in WRITE BYTE.\n");
#endif
break;
}
}
@ -319,9 +301,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
// i2c_bus_reset();
timeout = 0;
state = I2C_START;
#ifdef DEBUG_VERBOES
printf("i2c bus is busy in READ!\n");
#endif
}
}
/* 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;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal timeout in READ!\n");
#endif
}
break;
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;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master clears address flag timeout in READ!\n");
#endif
}
break;
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;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master wait data buffer is empty timeout in READ!\n");
#endif
}
/* wait until BTC bit is set */
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;
state = I2C_START;
read_cycle = RESET;
#ifdef DEBUG_VERBOES
printf("i2c master sends register address timeout in READ!\n");
#endif
}
} else {
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;
state = I2C_START;
read_cycle = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends data timeout in READ!\n");
#endif
}
}
timeout = 0;
@ -454,9 +424,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
timeout = 0;
state = I2C_START;
read_cycle = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends stop signal timeout in READ!\n");
#endif
}
break;
default:
@ -464,9 +432,7 @@ uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) {
read_cycle = 0;
i2c_timeout_flag = I2C_OK;
timeout = 0;
#ifdef DEBUG_VERBOES
printf("i2c master sends start signal in READ.\n");
#endif
break;
}
}

View File

@ -143,10 +143,10 @@ void ldc1612_iic_get_sensor_infomation(void) {
uint8_t data[2] = {0};
// ldc1612_iic_read_16bits(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);
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) {

View File

@ -25,7 +25,6 @@ uint32_t g_eddy_current_value_uint32;
\retval none
*/
int main(void) {
setbuf(stdout, NULL);
/* configure systick */
systick_config();
/* configure USART */
@ -36,7 +35,7 @@ int main(void) {
i2c_gpio_config();
i2c_config();
// ldc1612_iic_get_sensor_infomation();
ldc1612_iic_get_sensor_infomation();
/* configure LDC1612 */
ldc1612_single_ch0_config();
@ -46,7 +45,6 @@ int main(void) {
g_temperature_uint32 = 0;
g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0);
g_temperature_uint32 = tmp112a_get_raw_channel_result();
// printf("1");
}
}

View File

@ -33,69 +33,52 @@ void rs485_config(void) {
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);
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
}
void process_command(uint8_t *cmd, size_t length) {
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);
// 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();
eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0) {
tempture_value_report();
} else {
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 {
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
// return;
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
}
// } 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 crc = 0;
for (size_t i = 1; i < data_length - 1; i++) {
for (size_t i = 1; i < data_length; i++) {
crc += data[i];
}
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;
}
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", 0xB5, 0xF3, 0x02, 0xF3);
printf("%c%c%c%c\r\n", 0xB5, 0xF3, 0x00, 0xF3);
return VALIDATION_CRC_ERROR;
}
}
@ -104,7 +87,7 @@ validation_result_t validate_package_type(uint8_t *data) {
if (data[1] == 0x03) {
return VALIDATION_SUCCESS;
} 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;
}
}
@ -124,7 +107,7 @@ void eddy_current_value_report(void) {
// 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));
printf("%c\r\n", calculate_crc(combined_data, 7));
}
void tempture_value_report(void) {
@ -142,5 +125,5 @@ void tempture_value_report(void) {
// 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));
printf("%c\r\n", calculate_crc(combined_data, 7));
}