generated from hulk/gd32e23x_template
Compare commits
No commits in common. "eca38a53b870efd8bf8a1e1feb17e768e67f24a0" and "6702703031188db8ed1a4cf01ccc842ad6d0ee96" have entirely different histories.
eca38a53b8
...
6702703031
@ -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>
|
||||
|
@ -35,6 +35,4 @@ OF SUCH DAMAGE.
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
// #define DEBUG_VERBOES
|
||||
|
||||
#endif /* MAIN_H */
|
||||
|
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 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -46,7 +46,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");
|
||||
}
|
||||
}
|
||||
|
||||
|
120
src/rs485.c
120
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);
|
||||
|
||||
@ -38,7 +39,8 @@ void rs485_config(void) {
|
||||
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@ -47,79 +49,104 @@ void process_command(uint8_t *cmd, size_t length) {
|
||||
validate_data_length(cmd) |
|
||||
validate_package_crc(cmd, length));
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
|
||||
for (uint8_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, uint8_t data_length) {
|
||||
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1) {
|
||||
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 {
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_CRC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
validation_result_t validate_package_header(uint8_t *data) {
|
||||
if (data[0] == LDC1612_PACKAGE_HEADER) {
|
||||
validation_result_t validate_package_header(uint8_t* data)
|
||||
{
|
||||
if (data[0] == LDC1612_PACKAGE_HEADER)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_HEADER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
validation_result_t validate_package_type(uint8_t *data) {
|
||||
if (data[1] == LDC1612_BOARD_TYPE) {
|
||||
validation_result_t validate_package_type(uint8_t* data)
|
||||
{
|
||||
if (data[1] == LDC1612_BOARD_TYPE)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_TYPE_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
validation_result_t validate_data_length(uint8_t *data) {
|
||||
if (data[2] == LDC1612_PACKAGE_LENGTH) {
|
||||
validation_result_t validate_data_length(uint8_t* data)
|
||||
{
|
||||
if (data[2] == LDC1612_PACKAGE_LENGTH)
|
||||
{
|
||||
return VALIDATION_SUCCESS;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return VALIDATION_LENGTH_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
void eddy_current_value_report(void) {
|
||||
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;
|
||||
@ -134,7 +161,8 @@ void eddy_current_value_report(void) {
|
||||
printf("%c", calculate_crc(combined_data, 7));
|
||||
}
|
||||
|
||||
void tempture_value_report(void) {
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user