From 0233fffa4d32c08ede01087ff9882b8104cb3277 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Mon, 30 Dec 2024 22:39:45 +0800 Subject: [PATCH] add led --- CMakeLists.txt | 1 + inc/board_config.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ inc/peripheral.h | 2 +- src/gd32e23x_it.c | 15 ++++++++------- src/led.c | 37 +++++++++++++++++++++++++++++++++++++ src/led.h | 13 +++++++++++++ src/peripheral.c | 30 ------------------------------ 7 files changed, 104 insertions(+), 38 deletions(-) create mode 100644 inc/board_config.h create mode 100644 src/led.c create mode 100644 src/led.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 30de3dc..c6f6b6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c ${CMAKE_SOURCE_DIR}/src/systick.c ${CMAKE_SOURCE_DIR}/src/peripheral.c + ${CMAKE_SOURCE_DIR}/src/led.c ) add_executable(gd32e23x_template ${TARGET_C_SRC}) diff --git a/inc/board_config.h b/inc/board_config.h new file mode 100644 index 0000000..c8a2284 --- /dev/null +++ b/inc/board_config.h @@ -0,0 +1,44 @@ +// +// Created by dell on 24-12-28. +// + +#ifndef BOARD_CONFIG_H +#define BOARD_CONFIG_H + +#define SOFTWARE_IIC + +// #define DEBUG_VERBOES + +/******************************************************************************/ + +#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 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 + +/******************************************************************************/ + +#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/peripheral.h b/inc/peripheral.h index 31004a4..c74f98a 100644 --- a/inc/peripheral.h +++ b/inc/peripheral.h @@ -6,6 +6,6 @@ #define PERIPHERAL_H void usart_config(void); -void led_blink_config(void); + #endif //PERIPHERAL_H diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 0c77955..e7fcd90 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -35,6 +35,7 @@ OF SUCH DAMAGE. #include "gd32e23x_it.h" #include "main.h" #include "systick.h" +#include "board_config.h" /*! \brief this function handles NMI exception @@ -98,21 +99,21 @@ void SysTick_Handler(void) { } -void TIMER13_IRQHandler(void) +void TIMER16_IRQHandler(void) { - if (timer_interrupt_flag_get(TIMER13, TIMER_INT_FLAG_UP) == SET) + if (timer_interrupt_flag_get(LED_BLINK_TIMER, TIMER_INT_FLAG_UP) == SET) { - timer_interrupt_flag_clear(TIMER13, TIMER_INT_FLAG_UP); + timer_interrupt_flag_clear(LED_BLINK_TIMER, TIMER_INT_FLAG_UP); static uint8_t led_status = 0; if (led_status) { //! turn on led & reconfig timer13 period to 19000(1900ms) - gpio_bit_write(GPIOB, GPIO_PIN_1, RESET); - timer_autoreload_value_config(TIMER13, 19200); + gpio_bit_write(LED_PORT, LED_PIN, RESET); + timer_autoreload_value_config(LED_BLINK_TIMER, 19200); } else { //! turn off led & reconfig timer13 period to 1000(100ms) - gpio_bit_write(GPIOB, GPIO_PIN_1, SET); - timer_autoreload_value_config(TIMER13, 800); + gpio_bit_write(LED_PORT, LED_PIN, SET); + timer_autoreload_value_config(LED_BLINK_TIMER, 800); } led_status = !led_status; } diff --git a/src/led.c b/src/led.c new file mode 100644 index 0000000..87e98d5 --- /dev/null +++ b/src/led.c @@ -0,0 +1,37 @@ +// +// Created by yelv1 on 24-12-30. +// + +#include "led.h" + +/*! + \brief led blink configuration + \param[in] none + \param[out] none + \retval none +*/ +void led_blink_config(void) +{ + rcu_periph_clock_enable(LED_RCU); + + gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); + gpio_output_options_set(LED_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, LED_PIN); + gpio_bit_write(LED_PORT, LED_PIN, RESET); + + rcu_periph_clock_enable(LED_BLINK_TIMER_RCU); + timer_deinit(LED_BLINK_TIMER); + + timer_parameter_struct timer_initpara; + timer_struct_para_init(&timer_initpara); + timer_initpara.prescaler =7199; + timer_initpara.alignedmode =TIMER_COUNTER_EDGE; + timer_initpara.counterdirection =TIMER_COUNTER_UP; + timer_initpara.period =9999; + timer_initpara.clockdivision =TIMER_CKDIV_DIV1; + timer_init(LED_BLINK_TIMER, &timer_initpara); + + timer_auto_reload_shadow_enable(LED_BLINK_TIMER); + timer_interrupt_enable(LED_BLINK_TIMER, TIMER_INT_UP); + nvic_irq_enable(LED_BLINK_IRQ, 0); + timer_enable(LED_BLINK_TIMER); +} \ No newline at end of file diff --git a/src/led.h b/src/led.h new file mode 100644 index 0000000..d270385 --- /dev/null +++ b/src/led.h @@ -0,0 +1,13 @@ +// +// Created by yelv1 on 24-12-30. +// + +#ifndef LED_H +#define LED_H + +#include "gd32e23x.h" +#include "board_config.h" + +void led_blink_config(void); + +#endif //LED_H diff --git a/src/peripheral.c b/src/peripheral.c index 9e8d282..463ec58 100644 --- a/src/peripheral.c +++ b/src/peripheral.c @@ -30,34 +30,4 @@ void usart_config(void) gpio_bit_write(GPIOA, GPIO_PIN_4, SET); } -/*! - \brief led blink configuration - \param[in] none - \param[out] none - \retval none -*/ -void led_blink_config(void) -{ - rcu_periph_clock_enable(RCU_GPIOB); - gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1); - gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1); - gpio_bit_write(GPIOB, GPIO_PIN_1, SET); - - rcu_periph_clock_enable(RCU_TIMER13); - timer_deinit(RCU_TIMER13); - - timer_parameter_struct timer_initpara; - timer_struct_para_init(&timer_initpara); - timer_initpara.prescaler =7199; - timer_initpara.alignedmode =TIMER_COUNTER_EDGE; - timer_initpara.counterdirection =TIMER_COUNTER_UP; - timer_initpara.period =999; - timer_initpara.clockdivision =TIMER_CKDIV_DIV1; - timer_init(TIMER13, &timer_initpara); - - timer_auto_reload_shadow_enable(TIMER13); - timer_interrupt_enable(TIMER13, TIMER_INT_UP); - nvic_irq_enable(TIMER13_IRQn, 0); - timer_enable(TIMER13); -}