fix commit
This commit is contained in:
118
src/gd32e23x_it.c
Normal file
118
src/gd32e23x_it.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/*!
|
||||
\file gd32e23x_it.c
|
||||
\brief interrupt service routines
|
||||
|
||||
\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_it.h"
|
||||
#include "main.h"
|
||||
#include "systick.h"
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
/* if NMI exception occurs, go to infinite loop */
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles HardFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* if Hard Fault exception occurs, go to infinite loop */
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles SVC exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
/* if SVC exception occurs, go to infinite loop */
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles PendSV exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
/* if PendSV exception occurs, go to infinite loop */
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles SysTick exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
void TIMER13_IRQHandler(void) {
|
||||
if (timer_interrupt_flag_get(TIMER13, TIMER_INT_FLAG_UP) == SET)
|
||||
{
|
||||
timer_interrupt_flag_clear(TIMER13, 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(GPIOA, GPIO_PIN_9, RESET);
|
||||
timer_autoreload_value_config(TIMER13, 19200);
|
||||
} else {
|
||||
//! turn off led & reconfig timer13 period to 1000(100ms)
|
||||
gpio_bit_write(GPIOA, GPIO_PIN_9, SET);
|
||||
timer_autoreload_value_config(TIMER13, 800);
|
||||
}
|
||||
led_status = !led_status;
|
||||
}
|
||||
}
|
61
src/main.c
Normal file
61
src/main.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*!
|
||||
\file main.c
|
||||
\brief led spark with systick, USART print and key example
|
||||
|
||||
\version 2024-02-22, V2.1.0, firmware for GD32E23x
|
||||
*/
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include "gd32e23x_libopt.h"
|
||||
|
||||
#include "usonic_driver.h"
|
||||
|
||||
#define ULTRASONIC_CYCLES 0x05U
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* configure systick */
|
||||
systick_config();
|
||||
/* configure ultrasonic board hardware */
|
||||
ultrasonic_config();
|
||||
|
||||
|
||||
/* ---------- debug start ---------- */
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* ---------- debug end ---------- */
|
||||
|
||||
printf("\r\n");
|
||||
printf("START!\r\n");
|
||||
printf("XLSW-3DP-UltraSonic Analog 300K!\r\n");
|
||||
printf("\r\n");
|
||||
|
||||
while(1)
|
||||
{
|
||||
delay_ms(50);
|
||||
ultrasonic_pwm_out_cycles(ULTRASONIC_CYCLES);
|
||||
// printf("Send ultra sonic driver signal!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* retarget the C library printf function to the USART */
|
||||
int _write (int fd, char *pBuffer, int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
usart_data_transmit(USART0, (uint8_t)pBuffer[i]);
|
||||
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||||
}
|
||||
return size;
|
||||
}
|
63
src/peripheral.c
Normal file
63
src/peripheral.c
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// Created by yelv1 on 24-9-22.
|
||||
//
|
||||
#include "peripheral.h"
|
||||
#include "gd32e23x.h"
|
||||
|
||||
void usart_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_USART0);
|
||||
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_3);
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_3);
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
|
||||
|
||||
usart_deinit(USART0);
|
||||
usart_baudrate_set(USART0, 115200U);
|
||||
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART0);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
|
||||
|
||||
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);
|
||||
}
|
101
src/systick.c
Normal file
101
src/systick.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* ************************************************************************
|
||||
*
|
||||
* @file systick.c
|
||||
* @author GD32
|
||||
* @brief 通过 SysTick 定时器进行微秒级别和毫秒级别的延时函数
|
||||
*
|
||||
* ************************************************************************
|
||||
* @copyright Copyright (c) 2024 GD32
|
||||
* ************************************************************************
|
||||
*/
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
|
||||
volatile static float count_1us = 0;
|
||||
volatile static float count_1ms = 0;
|
||||
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @brief 配置 SysTick 定时器
|
||||
*
|
||||
*
|
||||
* ************************************************************************
|
||||
*/
|
||||
void systick_config(void)
|
||||
{
|
||||
//设置了 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_us 微秒延时函数
|
||||
*
|
||||
* @param[in] count 微秒值
|
||||
*
|
||||
* ************************************************************************
|
||||
*/
|
||||
void delay_us(uint32_t 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @brief delay_ms 毫秒延时函数
|
||||
*
|
||||
* @param[in] count 毫秒值
|
||||
*
|
||||
* ************************************************************************
|
||||
*/
|
||||
void delay_ms(uint32_t count) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @brief delay_nop 5个空指令延迟
|
||||
*
|
||||
* @param[in] none
|
||||
*
|
||||
* ************************************************************************
|
||||
*/
|
||||
void delay_nop(void) {
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
}
|
130
src/ultrasonic_driver.c
Normal file
130
src/ultrasonic_driver.c
Normal file
@@ -0,0 +1,130 @@
|
||||
//
|
||||
// Created by dell on 24-9-23.
|
||||
//
|
||||
|
||||
#include "usonic_driver.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
|
||||
uint8_t speed_pwm = 30; //bldc default pwm is 30
|
||||
|
||||
void led_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
|
||||
gpio_bit_write(GPIOA, GPIO_PIN_9, 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);
|
||||
}
|
||||
|
||||
void usart_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_USART0);
|
||||
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
|
||||
|
||||
/* configure USART Tx as alternate function push-pull */
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_3);
|
||||
|
||||
/* USART configure */
|
||||
usart_deinit(USART0);
|
||||
usart_baudrate_set(USART0, 115200U);
|
||||
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART0);
|
||||
}
|
||||
|
||||
void ultrasonic_gpio_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOB);
|
||||
|
||||
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
|
||||
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
||||
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_1);
|
||||
}
|
||||
|
||||
void ultrasonic_timer_config(void)
|
||||
{
|
||||
timer_oc_parameter_struct timer_ocinitpara;
|
||||
timer_parameter_struct timer_initpara;
|
||||
|
||||
rcu_periph_clock_enable(RCU_TIMER2);
|
||||
timer_deinit(TIMER2);
|
||||
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler = 0;
|
||||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||
timer_initpara.period = 239;
|
||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER2, &timer_initpara);
|
||||
|
||||
timer_channel_output_struct_para_init(&timer_ocinitpara);
|
||||
timer_ocinitpara.outputstate =TIMER_CCX_ENABLE;
|
||||
timer_ocinitpara.outputnstate =TIMER_CCXN_DISABLE;
|
||||
timer_ocinitpara.ocpolarity =TIMER_OC_POLARITY_HIGH;
|
||||
timer_ocinitpara.ocnpolarity =TIMER_OCN_POLARITY_HIGH;
|
||||
timer_ocinitpara.ocidlestate =TIMER_OC_IDLE_STATE_LOW;
|
||||
timer_ocinitpara.ocnidlestate =TIMER_OCN_IDLE_STATE_LOW;
|
||||
timer_channel_output_config(TIMER2, TIMER_CH_3, &timer_ocinitpara);
|
||||
|
||||
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, 120);
|
||||
timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM0);
|
||||
timer_auto_reload_shadow_enable(TIMER2);
|
||||
|
||||
timer_interrupt_enable(TIMER2, TIMER_INT_UP);
|
||||
// nvic_irq_enable(TIMER2_IRQn, 1);
|
||||
// timer_enable(TIMER2);
|
||||
}
|
||||
|
||||
void ultrasonic_config(void) {
|
||||
led_config();
|
||||
usart_config();
|
||||
ultrasonic_gpio_config();
|
||||
ultrasonic_timer_config();
|
||||
}
|
||||
|
||||
void ultrasonic_pwm_out_cycles(uint8_t cycles) {
|
||||
uint8_t current_cycle = 0;
|
||||
|
||||
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, 120);
|
||||
timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM1);
|
||||
timer_enable(TIMER2);
|
||||
|
||||
while (current_cycle < cycles)
|
||||
{
|
||||
while (!timer_interrupt_flag_get(TIMER2, TIMER_INT_FLAG_UP));
|
||||
timer_interrupt_flag_clear(TIMER2, TIMER_INT_FLAG_UP);
|
||||
current_cycle ++;
|
||||
}
|
||||
// delay_nop();
|
||||
timer_disable(TIMER2);
|
||||
// if(gpio_output_bit_get(GPIOB, GPIO_PIN_1) == SET)
|
||||
// {
|
||||
// gpio_bit_reset(GPIOB, GPIO_PIN_1);
|
||||
// }
|
||||
}
|
||||
|
Reference in New Issue
Block a user