某商城花了70块顺便淘了块GD32VF103C-START
的板子, RISC-V用来水几篇博客美滋滋.
板子长这个样子:
右边是GD-Link, 插上Win10联网会自动安装驱动, 安装时候显示的其实就是 DAP-Link, 用JLink方式调试运行, 没有虚拟串口!!! 所以你看到的, 上面又弄了个CH340转成了USB, 为了对称, 下面又弄一个MCU自己引出的USB, 一个板子怼了3个USB口我也是…而且上面的USB转串口的那个不支持供电, 板子要供电, 就得插上GD-Link的USB. Arduino的排座没有焊, 排针还是排座自己应付吧. 70块钱带调试器其实还算国产良心了! 废话有点多, 开撸了.
如下:
参考 Nuclei Studio IDE快速上手手册:
打开Application/main.c, 看到里面是用 gd32vf103v_eval 的板子来点灯, 我们是start板子, 就一个灯, main.c里面的代码可以全删了, 改成下面的:
#include "gd32vf103.h"
#include "systick.h"
#define LED_PIN GPIO_PIN_7
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_CLK RCU_GPIOA
int main(void)
{
rcu_periph_clock_enable(LED_GPIO_CLK); //enable the peripherals clock
gpio_init(LED_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_PIN); //GPIO output with push-pull
GPIO_BC(LED_GPIO_PORT) = LED_PIN; //bit clear
while(1){
GPIO_BOP(LED_GPIO_PORT) = LED_PIN; //1
delay_1ms(1000);
GPIO_BC(LED_GPIO_PORT) = LED_PIN; //0
delay_1ms(1000);
}
}
Run -> Debug Configuration, GDB OpenOCD Debugging选工程名开头的那个:
其他不用变, 然后点击Debug, 就开始保存编译调试了, 点击下图按钮, 就可以看到板子的灯间隔1s亮灭了:
如果修改了代码想重新调试, 需要先点击红色方块停止, 再重新进入调试.
板子还有一个Wakeup的按键, 连到PA0, 默认低电平, 按键按下是高电平:
注意按键相邻有一个Reset按键, 是MCU复位用的, 看丝印可以分清, 不要弄混.
下面代码中ECLIC是 Enhancement Core-Local Interrupt Controller 的缩写.
我们设置按键的下降沿中断, 每次按键释放, LED更改一次状态:
#include "gd32vf103.h"
#include "systick.h"
#define LED_PIN GPIO_PIN_7
#define LED_GPIO_PORT GPIOA
#define LED_GPIO_CLK RCU_GPIOA
#define KEY_PIN GPIO_PIN_0
#define KEY_GPIO_PORT GPIOA
#define KEY_GPIO_CLK RCU_GPIOA
#define KEY_EXTI EXTI0_IRQn
#define KEY_PORT_SOURCE GPIO_PORT_SOURCE_GPIOA
#define KEY_PIN_SOURCE GPIO_PIN_SOURCE_0
#define KEY_LINE EXTI_0
int main(void)
{
//LED
rcu_periph_clock_enable(LED_GPIO_CLK); //enable the peripherals clock
gpio_init(LED_GPIO_PORT, GPIO_MODE_OUT_PP,
GPIO_OSPEED_50MHZ, LED_PIN); //GPIO output with push-pull
GPIO_BC(LED_GPIO_PORT) = LED_PIN; //bit clear
//KEY
eclic_global_interrupt_enable(); //enable the global interrupt
eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1); //set the priority group
rcu_periph_clock_enable(KEY_GPIO_CLK); //enable the peripherals clock
rcu_periph_clock_enable(RCU_AF); //enable alternate function clock
gpio_init(KEY_GPIO_PORT, GPIO_MODE_IN_FLOATING,
GPIO_OSPEED_50MHZ, KEY_PIN); //configure button pin as input
eclic_irq_enable(KEY_EXTI, 1, 1); //enable the EXTI0 interrupt @priority(1, 1)
gpio_exti_source_select(KEY_PORT_SOURCE,
KEY_PIN_SOURCE); //select GPIO pin exti sources
exti_init(KEY_LINE, EXTI_INTERRUPT, EXTI_TRIG_FALLING); //configure key EXTI line
exti_interrupt_flag_clear(KEY_LINE); //clear EXTI lines pending flag
while(1){
//GPIO_BOP(LED_GPIO_PORT) = LED_PIN; //1
//delay_1ms(1000);
//GPIO_BC(LED_GPIO_PORT) = LED_PIN; //0
//delay_1ms(1000);
}
}
void Toggle_LED(void) {
gpio_bit_write(LED_GPIO_PORT, LED_PIN,
(bit_status)(1-gpio_input_bit_get(LED_GPIO_PORT, LED_PIN)));
}
void EXTI0_IRQHandler(void)
{
if(RESET != exti_interrupt_flag_get(KEY_LINE)){
exti_interrupt_flag_clear(KEY_LINE);
Toggle_LED();
}
}
按键设置下降沿中断的代码真的是又臭又长, 如果有自动代码生成工具就好了. 调试运行, 按一下Wakeup按键, 灯亮, 再按一下, 灯灭.
clean project后再压缩, 会小一点.
https://download.csdn.net/download/weifengdq/11935057