diff --git a/openocd_gd32e230f4.cfg b/openocd_gd32e230f4.cfg new file mode 100644 index 0000000..8484dd8 --- /dev/null +++ b/openocd_gd32e230f4.cfg @@ -0,0 +1,9 @@ +# 连接cmsis-dap(喝粥) +; interface cmsis-dap +source [find interface/cmsis-dap.cfg] + +# 选择SWD +transport select swd + +# 加载gd32e23x.cfg +source [find target/gd32e23x.cfg] diff --git a/src/main.c b/src/main.c index 9be1f91..9c50f6b 100644 --- a/src/main.c +++ b/src/main.c @@ -5,33 +5,6 @@ \version 2024-02-22, V2.1.0, firmware for GD32E23x */ -/* - Copyright (c) 2024, GigaDevice Semiconductor Inc. - - Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - 3. Neither the name of the copyright holder nor the names of its contributors - may be used to endorse or promote products derived from this software without - specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, -INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY -OF SUCH DAMAGE. -*/ - #include "gd32e23x.h" #include "systick.h" #include @@ -39,6 +12,12 @@ OF SUCH DAMAGE. #include "gd32e23x_libopt.h" +/*! + \brief LED blink function with tmer13 + \param[in] none + \param[out] none + \retval none +*/ void led_config(void) { rcu_periph_clock_enable(RCU_GPIOB); @@ -76,26 +55,9 @@ int main(void) { /* configure systick */ systick_config(); - /* initilize the LEDs, USART and key */ - // gd_eval_led_init(LED1); - // gd_eval_led_init(LED2); - // gd_eval_led_init(LED3); - // gd_eval_com_init(EVAL_COM); - // gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO); - - /* print out the clock frequency of system, AHB, APB1 and APB2 */ - // printf("\r\nCK_SYS is %lu", rcu_clock_freq_get(CK_SYS)); - // printf("\r\nCK_AHB is %lu", rcu_clock_freq_get(CK_AHB)); - // printf("\r\nCK_APB1 is %lu", rcu_clock_freq_get(CK_APB1)); - // printf("\r\nCK_APB2 is %lu", rcu_clock_freq_get(CK_APB2)); led_config(); - // 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, RESET); while(1){ } @@ -106,6 +68,5 @@ int fputc(int ch, FILE *f) { usart_data_transmit(USART0, (uint8_t)ch); while(RESET == usart_flag_get(USART0, USART_FLAG_TBE)); - return ch; } diff --git a/src/systick.c b/src/systick.c index 6d05a62..2056a57 100644 --- a/src/systick.c +++ b/src/systick.c @@ -1,83 +1,117 @@ /*! \file systick.c \brief the systick configuration file - + \version 2024-02-22, V2.1.0, firmware for GD32E23x */ /* - Copyright (c) 2024, GigaDevice Semiconductor Inc. + Copyright (c) 2024, GigaDevice Semiconductor inc. - Redistribution and use in source and binary forms, with or without modification, + Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, this + 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holder nor the names of its contributors - may be used to endorse or promote products derived from this software without + 3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without specific prior written permission. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, -INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "gd32e23x.h" #include "systick.h" -volatile static uint32_t delay; +volatile static float count_1us = 0; +volatile static float count_1ms = 0; -/*! - \brief configure systick - \param[in] none - \param[out] none - \retval none -*/ +/** + * ************************************************************************ + * @brief 配置 SysTick 定时器 + * + * + * ************************************************************************ + */ void systick_config(void) { - /* setup systick timer for 1000Hz interrupts */ - if(SysTick_Config(SystemCoreClock / 1000U)){ - /* capture error */ - while(1){ - } - } - /* configure the systick handler priority */ - NVIC_SetPriority(SysTick_IRQn, 0x00U); + //设置了 SysTick 定时器的时钟源为 HCLK/8 + systick_clksource_set(SYSTICK_CLKSOURCE_HCLK_DIV8); + //计算了每微秒所需的 SysTick 计数值 + count_1us = (float)SystemCoreClock/8000000; + //计算了每毫秒所需的 SysTick 计数值 + count_1ms = (float)count_1us * 1000; } -/*! - \brief delay a time in milliseconds - \param[in] count: count in milliseconds - \param[out] none - \retval none -*/ -void delay_1ms(uint32_t count) + +/** + * ************************************************************************ + * @brief delay_us 微秒延时函数 + * + * @param[in] count 微秒值 + * + * ************************************************************************ + */ +void delay_us(uint32_t count) { - delay = count; + uint32_t ctl; + + //设置 SysTick 计数器的装载值 + SysTick->LOAD = (uint32_t)(count * count_1us); + //清零 SysTick 计数器,以确保计数器从零开始计数 + SysTick->VAL = 0x0000U; + //使能 SysTick 定时器,开始进行计数 + SysTick->CTRL = SysTick_CTRL_ENABLE_Msk; + //等待 SysTick 计数器的计数值达到装载值时退出 + do + { + ctl = SysTick->CTRL; //读取 CTRL 寄存器的值 + }while((ctl & SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk)); + //循环退出,禁用 SysTick 定时器 + SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; + //将 SysTick 计数器的当前值清零,以便下次使用 + SysTick->VAL = 0x0000U; - while(0U != delay){ - } } -/*! - \brief delay decrement - \param[in] none - \param[out] none - \retval none -*/ -void delay_decrement(void) + +/** + * ************************************************************************ + * @brief delay_ms 毫秒延时函数 + * + * @param[in] count 毫秒值 + * + * ************************************************************************ + */ +void delay_ms(uint32_t count) { - if(0U != delay){ - delay--; - } + uint32_t ctl; + + //设置 SysTick 计数器的装载值 + SysTick->LOAD = (uint32_t)(count * count_1ms); + //清零 SysTick 计数器,以确保计数器从零开始计数 + SysTick->VAL = 0x0000U; + //使能 SysTick 定时器,开始进行计数 + SysTick->CTRL = SysTick_CTRL_ENABLE_Msk; + //等待 SysTick 计数器的计数值达到装载值时退出 + do + { + ctl = SysTick->CTRL; //读取 CTRL 寄存器的值 + }while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk)); + //循环退出,禁用 SysTick 定时器 + SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; + //将 SysTick 计数器的当前值清零,以便下次使用 + SysTick->VAL = 0x0000U; }