Compare commits

..

No commits in common. "eca38a53b870efd8bf8a1e1feb17e768e67f24a0" and "6702703031188db8ed1a4cf01ccc842ad6d0ee96" have entirely different histories.

6 changed files with 76 additions and 86 deletions

View File

@ -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>

View File

@ -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 */

View File

@ -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;
} }
} }

View File

@ -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) {

View File

@ -46,7 +46,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");
} }
} }

View File

@ -10,7 +10,8 @@ extern uint32_t g_eddy_current_value_uint32;
uint8_t package_header[3] = {0xB5, 0xF0, 0x04}; uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
uint8_t package_data[4] = {0}; 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_GPIO_RCU);
rcu_periph_clock_enable(RS485_RCU); rcu_periph_clock_enable(RS485_RCU);
@ -38,7 +39,8 @@ void rs485_config(void) {
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE); 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]; char combined_str[3];
validation_result_t validate = VALIDATION_SUCCESS; validation_result_t validate = VALIDATION_SUCCESS;
@ -47,15 +49,21 @@ void process_command(uint8_t *cmd, size_t length) {
validate_data_length(cmd) | validate_data_length(cmd) |
validate_package_crc(cmd, length)); validate_package_crc(cmd, length));
switch (validate) { switch (validate)
{
case VALIDATION_SUCCESS: case VALIDATION_SUCCESS:
// printf("%d", length); // printf("%d", length);
sprintf(combined_str, "%c%c", cmd[3], cmd[4]); sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
if (strcmp(combined_str, "M1") == 0) { if (strcmp(combined_str, "M1") == 0)
{
eddy_current_value_report(); eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0) { }
else if (strcmp(combined_str, "M2") == 0)
{
tempture_value_report(); 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%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
return; return;
} }
@ -77,49 +85,68 @@ void process_command(uint8_t *cmd, size_t length) {
} }
} }
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; 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]; 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) { 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) { {
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1)
{
return VALIDATION_SUCCESS; return VALIDATION_SUCCESS;
} else { }
else
{
return VALIDATION_CRC_ERROR; 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] == LDC1612_PACKAGE_HEADER) { {
if (data[0] == LDC1612_PACKAGE_HEADER)
{
return VALIDATION_SUCCESS; return VALIDATION_SUCCESS;
} else { }
else
{
return VALIDATION_HEADER_ERROR; return VALIDATION_HEADER_ERROR;
} }
} }
validation_result_t validate_package_type(uint8_t *data) { validation_result_t validate_package_type(uint8_t* data)
if (data[1] == LDC1612_BOARD_TYPE) { {
if (data[1] == LDC1612_BOARD_TYPE)
{
return VALIDATION_SUCCESS; return VALIDATION_SUCCESS;
} else { }
else
{
return VALIDATION_TYPE_ERROR; return VALIDATION_TYPE_ERROR;
} }
} }
validation_result_t validate_data_length(uint8_t *data) { validation_result_t validate_data_length(uint8_t* data)
if (data[2] == LDC1612_PACKAGE_LENGTH) { {
if (data[2] == LDC1612_PACKAGE_LENGTH)
{
return VALIDATION_SUCCESS; return VALIDATION_SUCCESS;
} else { }
else
{
return VALIDATION_LENGTH_ERROR; 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[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF;
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF; package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
package_data[2] = (g_eddy_current_value_uint32 >> 8) & 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)); 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[0] = (g_temperature_uint32 >> 24) & 0xFF;
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF; package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF; package_data[2] = (g_temperature_uint32 >> 8) & 0xFF;