Compare commits

...

4 Commits

14 changed files with 305 additions and 9 deletions

View File

@ -12,7 +12,7 @@ set(VERSION "V${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
string(TIMESTAMP CURRENT_DATE "%Y-%m-%d")
# Options 1
set(OPT1 "")
set(OPT1 "_[HW_IIC]")
#set(OPT1 "_[SW_IIC]")
# Options 2
@ -39,6 +39,8 @@ set(TARGET_C_SRC
${CMAKE_SOURCE_DIR}/src/soft_i2c.c
${CMAKE_SOURCE_DIR}/src/i2c.c
${CMAKE_SOURCE_DIR}/src/fwdgt.c
${CMAKE_SOURCE_DIR}/src/rs485_protocol.c
${CMAKE_SOURCE_DIR}/src/gd60914.c
)
add_executable(${PROJECT_NAME} ${TARGET_C_SRC})

View File

@ -91,9 +91,15 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in
# Conditional flags
# DEBUG
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
#set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
#set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
#set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -O0 -g")
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -O2 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -O2 -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -O2 -g")
#set(CMAKE_C_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
#set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
#set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG=0 -Os -g")
# RELEASE
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O3") # -flto

View File

@ -31,7 +31,7 @@
#define USART_PHY USART0
#define USART_PHY_BAUDRATE 115200U
#define RS485_EN_PORT GPIOA
#define RS485_EN_PIN GPIO_PIN_1
#define RS485_EN_PIN GPIO_PIN_4
/******************************************************************************/

View File

@ -39,6 +39,9 @@ OF SUCH DAMAGE.
#include "main.h"
#include "systick.h"
#include "board_config.h"
#include "usart.h"
#include "fwdgt.h"
#include "rs485_protocol.h"
/* function declarations */
/* this function handles NMI exception */

34
inc/gd60914.h Normal file
View File

@ -0,0 +1,34 @@
//
// Created by dell on 25-1-7.
//
#ifndef GD60914_H
#define GD60914_H
#include "gd32e23x_it.h"
#include "gd32e23x.h"
#include "board_config.h"
#ifdef SOFTWARE_IIC
#include "soft_i2c.h"
#else
#include "i2c.h"
#endif
/******************************************************************************/
#define GD60914_ADDR (0x18 << 1)
/******************************************************************************/
#define GD60914_HUM_TEMP 0x1A
#define GD60914_OBJ_TEMP 0x1F
#define GD60914_AMB_TEMP 0x1E
#define GD60914_TEMP_REG 0x1C
void gd60914_get_object_tempture(void);
void gd60914_read_temp(void);
#endif //GD60914_H

View File

@ -19,7 +19,7 @@
/******************************************************************************/
#define I2C_SPEED 20000
#define I2C_SPEED 100000
#define I2C_TIME_OUT (uint16_t)(5000)
#define I2C_OK 1

View File

@ -43,6 +43,7 @@ OF SUCH DAMAGE.
#include "usart.h"
#include "fwdgt.h"
#include "board_config.h"
#include "gd60914.h"
#ifdef SOFTWARE_IIC
#include "soft_i2c.h"

53
inc/rs485_protocol.h Normal file
View File

@ -0,0 +1,53 @@
//
// Created by yelv1 on 24-12-31.
//
#ifndef RS485_PROTOCOL_H
#define RS485_PROTOCOL_H
#include "gd32e23x.h"
#include "systick.h"
#include "gd32e23x_libopt.h"
#include "board_config.h"
#include "usart.h"
#include "fwdgt.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/******************************************************************************/
#define RX_BUFFER_SIZE 32
#define PROTOCOL_PACKAGE_HEADER 0xD5
#define PROTOCOL_BOARD_TYPE 0x04
#define PROTOCOL_PACKAGE_LENGTH 0x02
/******************************************************************************/
typedef enum
{
VALIDATION_SUCCESS = 0,
VALIDATION_CRC_ERROR = 1,
VALIDATION_HEADER_ERROR = 2,
VALIDATION_TYPE_ERROR = 4,
VALIDATION_LENGTH_ERROR = 8
} validation_result_t;
/******************************************************************************/
void process_command(uint8_t* cmd, size_t length);
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length);
validation_result_t validate_package_header(uint8_t* data);
validation_result_t validate_package_type(uint8_t* data);
validation_result_t validate_data_length(uint8_t* data);
void gd60914_tempture_report(void);
#endif //RS485_PROTOCOL_H

View File

@ -120,4 +120,28 @@ void TIMER16_IRQHandler(void)
}
led_status = !led_status;
}
}
void USART0_IRQHandler(void) {
static uint8_t rx_index = 0;
static uint8_t rx_buffer[RX_BUFFER_SIZE];
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);
// 将接收到的数据存储到缓冲区
if (rx_index < RX_BUFFER_SIZE - 1) {
rx_buffer[rx_index++] = received_data;
}
}
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
process_command(rx_buffer, rx_index); // 处理指令
rx_index = 0; // 重置缓冲区索引
return;
}
}

51
src/gd60914.c Normal file
View File

@ -0,0 +1,51 @@
//
// Created by dell on 25-1-7.
//
#include "gd60914.h"
void gd60914_get_object_tempture(void) {
#ifdef SOFTWARE_IIC
soft_i2c_config();
#else
i2c_config();
#endif
static uint8_t sensor_validation_data[2];
extern uint8_t g_temperature_uint8[2];
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data);
#else
i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, sensor_validation_data);
#endif
if (sensor_validation_data[0] != 0xAA || sensor_validation_data[1] != 0x55) {
#ifdef DEBUG_VERBOES
printf("sensor error\r\n");
#endif
return;
}
delay_ms(350);
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8);
#else
i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8);
#endif
// printf("%d\r\n", g_temperature_uint8[1] << 8 | g_temperature_uint8[0]);
}
void gd60914_read_temp(void) {
uint8_t value[2] = {0};
#ifdef SOFTWARE_IIC
soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, value);
#else
i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, value);
#endif
printf("%x %x\r\n", value[1], value[0]);
}

View File

@ -6,6 +6,8 @@
*/
#include "main.h"
volatile uint8_t g_temperature_uint8[2] = {0};
/*!
\brief main function
\param[in] none
@ -14,6 +16,7 @@
*/
int main(void)
{
setbuf(stdout, NULL);
/* configure systick */
systick_config();
/* configure USART */
@ -23,11 +26,20 @@ int main(void)
/* configure FWDGT */
watchdog_init();
#ifdef SOFTWARE_IIC
soft_i2c_config();
#else
i2c_config();
#endif
printf("system start!\r\n");
while(1){
printf("hello world!\r\n");
delay_ms(500);
// printf("hello world!\r\n");
// delay_ms(500);
gd60914_get_object_tempture();
// delay_ms(100);
// printf("%d\r\n", g_temperature_uint8[1] << 8 | g_temperature_uint8[0]);
watchdog_reload();
}

106
src/rs485_protocol.c Normal file
View File

@ -0,0 +1,106 @@
//
// Created by yelv1 on 24-12-31.
//
#include "rs485_protocol.h"
extern uint8_t g_temperature_uint8[2];
void process_command(uint8_t *cmd, size_t length) {
char combined_str[3];
validation_result_t validate = VALIDATION_SUCCESS;
validate = (validate_package_header(cmd) |
validate_package_type(cmd) |
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) {
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
// eddy_current_value_report();
} else if (strcmp(combined_str, "M2") == 0)
{
gd60914_tempture_report();
} else if (strcmp(combined_str, "M3") == 0)
{
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
fwdgt_reset_mcu();
} 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, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
break;
case VALIDATION_HEADER_ERROR:
printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
break;
case VALIDATION_TYPE_ERROR:
printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
break;
case VALIDATION_LENGTH_ERROR:
printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
break;
default:
break;
}
}
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++) {
crc += data[i];
}
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) {
return VALIDATION_SUCCESS;
} else {
return VALIDATION_CRC_ERROR;
}
}
validation_result_t validate_package_header(uint8_t *data) {
if (data[0] == PROTOCOL_PACKAGE_HEADER) {
return VALIDATION_SUCCESS;
} else {
return VALIDATION_HEADER_ERROR;
}
}
validation_result_t validate_package_type(uint8_t *data) {
if (data[1] == PROTOCOL_BOARD_TYPE) {
return VALIDATION_SUCCESS;
} else {
return VALIDATION_TYPE_ERROR;
}
}
validation_result_t validate_data_length(uint8_t *data) {
if (data[2] == PROTOCOL_PACKAGE_LENGTH) {
return VALIDATION_SUCCESS;
} else {
return VALIDATION_LENGTH_ERROR;
}
}
void gd60914_tempture_report(void) {
static uint8_t package_header[3] = {0xB5, 0xF0, 0x02};
uint8_t combined_data[5];
memcpy(combined_data, package_header, 3);
memcpy(combined_data + 3, g_temperature_uint8, 2);
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
printf("%c%c", g_temperature_uint8[1], g_temperature_uint8[0]);
printf("%c", calculate_crc(combined_data, 6));
}

View File

@ -11,7 +11,7 @@
\retval none
*/
void soft_i2c_delay(void) {
delay_us(20); // Adjust delay as needed
delay_us(1); // Adjust delay as needed
/* delay to freq
* 15KHz: delay_us(20);
* 65KHz: delay_us(1);

View File

@ -28,6 +28,10 @@ void usart_config(void)
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
usart_enable(USART_PHY);
nvic_irq_enable(USART0_IRQn, 0);
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
}
/**