generated from hulk/gd32e23x_template
Compare commits
7 Commits
6702703031
...
eca38a53b8
Author | SHA1 | Date | |
---|---|---|---|
eca38a53b8 | |||
fca25e1981 | |||
15730d7234 | |||
fa7f364bdb | |||
2d273efef5 | |||
8fa7ed32d8 | |||
05fb90c727 |
@ -8,6 +8,7 @@
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include "main.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
@ -35,4 +35,6 @@ OF SUCH DAMAGE.
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
// #define DEBUG_VERBOES
|
||||
|
||||
#endif /* MAIN_H */
|
||||
|
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 {
|
||||
timeout = 0;
|
||||
state = I2C_START;
|
||||
#ifdef DEBUG_VERBOES
|
||||
printf("i2c bus is busy in WRITE BYTE!\n");
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
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 {
|
||||
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:
|
||||
@ -188,7 +192,9 @@ 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:
|
||||
@ -203,7 +209,9 @@ 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 */
|
||||
@ -217,7 +225,9 @@ 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 */
|
||||
@ -232,7 +242,9 @@ 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 */
|
||||
@ -245,7 +257,9 @@ 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:
|
||||
@ -262,14 +276,18 @@ 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;
|
||||
}
|
||||
}
|
||||
@ -301,7 +319,9 @@ 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 */
|
||||
@ -328,7 +348,9 @@ 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:
|
||||
@ -348,7 +370,9 @@ 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:
|
||||
@ -365,7 +389,9 @@ 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)) {
|
||||
@ -379,7 +405,9 @@ 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) {
|
||||
@ -404,7 +432,9 @@ 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;
|
||||
@ -424,7 +454,9 @@ 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:
|
||||
@ -432,7 +464,9 @@ 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\r\n", (data[0] << 8) | data[1]);
|
||||
printf("\tManufacturer: 0x%x", (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\r\n", (data[0] << 8) | data[1]);
|
||||
printf("\tDevice: 0x%x", (data[0] << 8) | data[1]);
|
||||
}
|
||||
|
||||
uint16_t ldc1612_get_manufacturer_id(void) {
|
||||
|
@ -46,6 +46,7 @@ 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,8 +10,7 @@ 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);
|
||||
|
||||
@ -39,8 +38,7 @@ 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;
|
||||
|
||||
@ -49,104 +47,79 @@ 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;
|
||||
@ -161,8 +134,7 @@ 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