diff --git a/CMakeLists.txt b/CMakeLists.txt index 402aeb4..f34035c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/led.c ${CMAKE_SOURCE_DIR}/src/i2c.c ${CMAKE_SOURCE_DIR}/src/soft_i2c.c + ${CMAKE_SOURCE_DIR}/src/fwdgt.c ) add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC}) diff --git a/inc/board_config.h b/inc/board_config.h index 5a8314c..c8a2284 100644 --- a/inc/board_config.h +++ b/inc/board_config.h @@ -11,14 +11,13 @@ /******************************************************************************/ -#define I2C_SPEED 20000 -#define RCU_GPIO_I2C RCU_GPIOF -#define RCU_I2C RCU_I2C0 -#define I2C_SCL_PORT GPIOF -#define I2C_SCL_PIN GPIO_PIN_1 -#define I2C_SDA_PORT GPIOF -#define I2C_SDA_PIN GPIO_PIN_0 -#define I2C_GPIO_AF GPIO_AF_1 +#define RCU_GPIO_I2C RCU_GPIOF +#define RCU_I2C RCU_I2C0 +#define I2C_SCL_PORT GPIOF +#define I2C_SCL_PIN GPIO_PIN_1 +#define I2C_SDA_PORT GPIOF +#define I2C_SDA_PIN GPIO_PIN_0 +#define I2C_GPIO_AF GPIO_AF_1 /******************************************************************************/ @@ -33,4 +32,13 @@ /******************************************************************************/ +#define LED_PORT GPIOA +#define LED_PIN GPIO_PIN_7 +#define LED_RCU RCU_GPIOA +#define LED_BLINK_TIMER_RCU RCU_TIMER16 +#define LED_BLINK_TIMER TIMER16 +#define LED_BLINK_IRQ TIMER16_IRQn + +/******************************************************************************/ + #endif //BOARD_CONFIG_H diff --git a/inc/fwdgt.h b/inc/fwdgt.h new file mode 100644 index 0000000..28da74c --- /dev/null +++ b/inc/fwdgt.h @@ -0,0 +1,15 @@ +// +// Created by yelv1 on 24-12-29. +// + +#ifndef FWDGT_H +#define FWDGT_H + +#include "gd32e23x.h" +#include "board_config.h" + +void watchdog_init(void); + +void fwdgt_reset_mcu(void); + +#endif //FWDGT_H diff --git a/inc/i2c.h b/inc/i2c.h index 67645b2..ea09aa7 100644 --- a/inc/i2c.h +++ b/inc/i2c.h @@ -19,13 +19,7 @@ /******************************************************************************/ -#define I2C_SCL_HIGH() gpio_bit_set(I2C_SCL_PORT, I2C_SCL_PIN) -#define I2C_SCL_LOW() gpio_bit_reset(I2C_SCL_PORT, I2C_SCL_PIN) -#define I2C_SDA_HIGH() gpio_bit_set(I2C_SDA_PORT, I2C_SDA_PIN) -#define I2C_SDA_LOW() gpio_bit_reset(I2C_SDA_PORT, I2C_SDA_PIN) -#define I2C_SDA_READ() gpio_input_bit_get(I2C_SDA_PORT, I2C_SDA_PIN) - -/******************************************************************************/ +#define I2C_SPEED 20000 #define I2C_TIME_OUT (uint16_t)(5000) #define I2C_OK 1 diff --git a/inc/led.h b/inc/led.h index b3fb0c9..b02d4ee 100644 --- a/inc/led.h +++ b/inc/led.h @@ -6,24 +6,7 @@ #define LED_H #include "gd32e23x_it.h" -#include "gd32e23x.h" -#include "systick.h" -#include -#include -#include -#include -#include - -/******************************************************************************/ - -#define LED_PORT GPIOA -#define LED_PIN GPIO_PIN_7 -#define LED_RCU RCU_GPIOA -#define LED_TIMER_RCU RCU_TIMER16 -#define LED_TIMER TIMER16 -#define LED_IRQ TIMER16_IRQn - -/******************************************************************************/ +#include "board_config.h" void led_config(void); diff --git a/inc/main.h b/inc/main.h index 88b766c..8c8dca8 100644 --- a/inc/main.h +++ b/inc/main.h @@ -35,10 +35,21 @@ OF SUCH DAMAGE. #ifndef MAIN_H #define MAIN_H -// #define DEBUG_VERBOES +#include +#include "gd32e23x.h" +#include "systick.h" +#include "gd32e23x_libopt.h" +#include "rs485.h" +#include "led.h" +#include "i2c.h" +#include "ldc1612.h" +#include "fwdgt.h" +#include "board_config.h" -void watchdog_init(void); - -void reset_mcu(void); +#ifdef SOFTWARE_IIC +#include "soft_i2c.h" +#else +#include "i2c.h" +#endif #endif /* MAIN_H */ diff --git a/inc/rs485.h b/inc/rs485.h index dbc5b94..2f7b139 100644 --- a/inc/rs485.h +++ b/inc/rs485.h @@ -8,25 +8,11 @@ #include "gd32e23x_it.h" #include "gd32e23x.h" #include "systick.h" -#include -#include #include -#include -#include -#include "i2c.h" #include "ldc1612.h" #include "tmp112.h" - -/******************************************************************************/ - -#define RS485_RCU RCU_USART0 -#define RS485_GPIO_RCU RCU_GPIOA -#define RS485_GPIO_PORT GPIOA -#define RS485_TX_PIN GPIO_PIN_2 -#define RS485_RX_PIN GPIO_PIN_3 -#define RS485_PHY USART0 -#define RS485_BAUDRATE 115200U -#define RS485_EN_PIN GPIO_PIN_1 +#include "fwdgt.h" +#include "board_config.h" /******************************************************************************/ diff --git a/inc/tmp112.h b/inc/tmp112.h index 29b2436..226013a 100644 --- a/inc/tmp112.h +++ b/inc/tmp112.h @@ -7,12 +7,6 @@ #include "gd32e23x_it.h" #include "gd32e23x.h" -#include "systick.h" -#include -#include -#include -#include -#include #include "board_config.h" @@ -28,6 +22,8 @@ /******************************************************************************/ +#define TMP112A_TEMP_REG 0x00 + uint32_t tmp112a_get_raw_channel_result(void); #endif //TMP112_H diff --git a/src/fwdgt.c b/src/fwdgt.c new file mode 100644 index 0000000..b933237 --- /dev/null +++ b/src/fwdgt.c @@ -0,0 +1,28 @@ +// +// Created by yelv1 on 24-12-29. +// + +#include "fwdgt.h" + +void watchdog_init(void) { + /* Enable the LSI clock */ + rcu_osci_on(RCU_IRC40K); + rcu_osci_stab_wait(RCU_IRC40K); + + /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ + fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz) + + /* Enable FWDGT */ + fwdgt_enable(); +} + +void fwdgt_reset_mcu(void) { + /* Enable the write access to the FWDGT_CTL register */ + FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE; + + /* Configure FWDGT to trigger a system reset */ + fwdgt_config(50, FWDGT_PSC_DIV4); + + /* Reload the counter to trigger the reset */ + fwdgt_counter_reload(); +} \ No newline at end of file diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 44c7960..6893a93 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -103,15 +103,15 @@ void SysTick_Handler(void) { * @retval None */ void TIMER16_IRQHandler(void) { - if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) { - timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP); + if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET) { + timer_interrupt_flag_clear(LED_BLINK_TIMER, TIMER_INT_FLAG_UP); static uint8_t led_status = 0; if (led_status) { gpio_bit_write(LED_PORT, LED_PIN, RESET); - timer_autoreload_value_config(LED_TIMER, 19200); + timer_autoreload_value_config(LED_BLINK_TIMER, 19200); } else { gpio_bit_write(LED_PORT, LED_PIN, SET); - timer_autoreload_value_config(LED_TIMER, 800); + timer_autoreload_value_config(LED_BLINK_TIMER, 800); } led_status = !led_status; } diff --git a/src/led.c b/src/led.c index f203941..31df075 100644 --- a/src/led.c +++ b/src/led.c @@ -11,8 +11,8 @@ void led_config(void) { gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN); gpio_bit_write(LED_PORT, LED_PIN, SET); - rcu_periph_clock_enable(LED_TIMER_RCU); - timer_deinit(LED_TIMER); + rcu_periph_clock_enable(LED_BLINK_TIMER_RCU); + timer_deinit(LED_BLINK_TIMER); timer_parameter_struct timer_initpara; timer_struct_para_init(&timer_initpara); @@ -21,12 +21,12 @@ void led_config(void) { timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 9999; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; - timer_init(LED_TIMER, &timer_initpara); + timer_init(LED_BLINK_TIMER, &timer_initpara); - timer_auto_reload_shadow_enable(LED_TIMER); - timer_interrupt_enable(LED_TIMER, TIMER_INT_UP); + timer_auto_reload_shadow_enable(LED_BLINK_TIMER); + timer_interrupt_enable(LED_BLINK_TIMER, TIMER_INT_UP); - timer_enable(LED_TIMER); + timer_enable(LED_BLINK_TIMER); - nvic_irq_enable(LED_IRQ, 2); + nvic_irq_enable(LED_BLINK_IRQ, 2); } diff --git a/src/main.c b/src/main.c index fd4f284..85babea 100644 --- a/src/main.c +++ b/src/main.c @@ -5,44 +5,6 @@ \version 2024-02-22, V2.1.0, firmware for GD32E23x */ #include "main.h" -#include -#include "gd32e23x.h" -#include "systick.h" -#include "gd32e23x_libopt.h" -#include "rs485.h" -#include "led.h" -#include "i2c.h" -#include "ldc1612.h" -#include "board_config.h" - -#ifdef SOFTWARE_IIC -#include "soft_i2c.h" -#else -#include "i2c.h" -#endif - -void watchdog_init(void) { - /* Enable the LSI clock */ - rcu_osci_on(RCU_IRC40K); - rcu_osci_stab_wait(RCU_IRC40K); - - /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ - fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz) - - /* Enable FWDGT */ - fwdgt_enable(); -} - -void reset_mcu(void) { - /* Enable the write access to the FWDGT_CTL register */ - FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE; - - /* Configure FWDGT to trigger a system reset */ - fwdgt_config(50, FWDGT_PSC_DIV4); - - /* Reload the counter to trigger the reset */ - fwdgt_counter_reload(); -} /*! \brief main function diff --git a/src/rs485.c b/src/rs485.c index c471eca..ce911d1 100644 --- a/src/rs485.c +++ b/src/rs485.c @@ -56,7 +56,7 @@ void process_command(uint8_t *cmd, size_t length) { } else if (strcmp(combined_str, "M3") == 0) { printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC); - reset_mcu(); + fwdgt_reset_mcu(); } else { printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); return; diff --git a/src/soft_i2c.c b/src/soft_i2c.c index a985aef..69e0240 100644 --- a/src/soft_i2c.c +++ b/src/soft_i2c.c @@ -13,7 +13,7 @@ void soft_i2c_delay(void) { delay_us(20); // Adjust delay as needed /* delay to freq - * 20KHz: delay_us(20); + * 15KHz: delay_us(20); * 65KHz: delay_us(1); */ } @@ -44,15 +44,12 @@ void soft_i2c_config(void) { \retval none */ void soft_i2c_start(void) { - // sda_out(); I2C_SDA_HIGH(); I2C_SCL_HIGH(); soft_i2c_delay(); I2C_SDA_LOW(); soft_i2c_delay(); I2C_SCL_LOW(); - // soft_i2c_delay(); - // 从全高开始,SCL为高期间,SDA下降沿表示start信号,再拉低SCL } /*! @@ -69,7 +66,6 @@ void soft_i2c_stop(void) { I2C_SCL_HIGH(); soft_i2c_delay(); I2C_SDA_HIGH(); - // 从全低开始,SCL为高期间,SDA上升沿表示stop } /*! @@ -87,7 +83,6 @@ void soft_i2c_send_ack(void) { I2C_SCL_LOW(); soft_i2c_delay(); I2C_SDA_HIGH(); - // SCL产生一个正常的时钟周期,其间SDA始终为低电平,表示ACK } /*! @@ -97,7 +92,6 @@ void soft_i2c_send_ack(void) { \retval none */ void soft_i2c_send_nack(void) { - // sda_out(); I2C_SDA_HIGH(); soft_i2c_delay(); I2C_SCL_HIGH(); @@ -105,7 +99,6 @@ void soft_i2c_send_nack(void) { I2C_SCL_LOW(); soft_i2c_delay(); I2C_SDA_HIGH(); - // SCL产生一个正常的时钟周期,其间SDA始终为高电平,表示NACK } /*! @@ -115,13 +108,11 @@ void soft_i2c_send_nack(void) { \retval 0: ACK received, 1: ACK not received */ uint8_t soft_i2c_wait_ack(void) { - // sda_in(); I2C_SDA_HIGH(); soft_i2c_delay(); I2C_SCL_HIGH(); soft_i2c_delay(); uint8_t ack = !I2C_SDA_READ(); - //ACK信号是第九个时钟期间,SDA被从机在SCL高期间,拉低并保持低电平。此处判断SDA是否被拉低,被拉低则返回0,取反为1,表示收到ACK I2C_SCL_LOW(); return ack; } @@ -135,19 +126,18 @@ uint8_t soft_i2c_wait_ack(void) { void soft_i2c_send_byte(uint8_t byte) { // sda_out(); for (int i = 0; i < 8; i++) { - if (byte & 0x80) { //通过&操作获取第一位是1还是0 - I2C_SDA_HIGH(); //SCL低电平期间,SDA高电平表示1 + if (byte & 0x80) { + I2C_SDA_HIGH(); } else { - I2C_SDA_LOW(); //SCL低电平期间,SDA低电平表示0 + I2C_SDA_LOW(); } - byte <<= 1; //左移一位,把原本第二位的数据移到第一位,再判断高低电平 + byte <<= 1; soft_i2c_delay(); - I2C_SCL_HIGH(); //SCL拉高电平,SDA电平状态保持不变 + I2C_SCL_HIGH(); + soft_i2c_delay(); + I2C_SCL_LOW(); soft_i2c_delay(); - I2C_SCL_LOW(); //SCL拉低电平 - delay_us(5); } - // i2c_wait_ack(); } /*! @@ -157,9 +147,8 @@ void soft_i2c_send_byte(uint8_t byte) { \retval received byte */ uint8_t soft_i2c_receive_byte(uint8_t ack) { - // sda_in(); uint8_t byte = 0; - I2C_SDA_HIGH(); //从高开始 + I2C_SDA_HIGH(); for (int i = 0; i < 8; i++) { byte <<= 1; I2C_SCL_HIGH(); diff --git a/src/tmp112.c b/src/tmp112.c index d28ce94..27b97f9 100644 --- a/src/tmp112.c +++ b/src/tmp112.c @@ -11,9 +11,9 @@ uint32_t tmp112a_get_raw_channel_result(void) { // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value); #ifdef SOFTWARE_IIC - soft_i2c_read_16bits(TMP112A_ADDR, 0x00, value); + soft_i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value); #else - i2c_read_16bits(TMP112A_ADDR, 0x00, value); + i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value); #endif raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4));