Skip to content

Instantly share code, notes, and snippets.

@OrsoEric

OrsoEric/main.cpp

Created Jul 4, 2020
Embed
What would you like to do?
Longan Nano GD32VF103 Polling Interrupt Flags
#include <gd32vf103.h>
void init()
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOC);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2);
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
gpio_bit_set(GPIOC,GPIO_PIN_13);
gpio_bit_set(GPIOA,GPIO_PIN_1);
gpio_bit_set(GPIOA,GPIO_PIN_2);
rcu_periph_clock_enable(RCU_AF);
eclic_init(ECLIC_NUM_INTERRUPTS);
eclic_priority_group_set(ECLIC_PRIGROUP_LEVEL3_PRIO1);
eclic_irq_enable(EXTI5_9_IRQn, 1, 1);
exti_deinit();
gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_8);
exti_init(EXTI_8, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
//Clear interrupt flags
exti_interrupt_flag_clear(EXTI_8);
eclic_clear_pending(EXTI5_9_IRQn);
eclic_global_interrupt_enable();
return;
}
void EXTI5_9_IRQHandler()
{
}
void delay_us(unsigned int us)
{
uint64_t start_mtime, delta_mtime;
// Don't start measuring until we see an mtime tick
uint64_t tmp = get_timer_value();
do
{
start_mtime = get_timer_value();
}
while (start_mtime == tmp);
do
{
delta_mtime = get_timer_value() - start_mtime;
}
while(delta_mtime <(SystemCoreClock/4000000.0 *us ));
return;
}
int main()
{
init();
while (true)
{
gpio_bit_write(GPIOC, GPIO_PIN_13, (bit_status)(1-gpio_input_bit_get(GPIOC, GPIO_PIN_13)));
delay_us(250000);
//This correctly detects the EXTI8. Only a single acquisition at beginning
if (exti_interrupt_flag_get(EXTI_8) != RESET)
{
gpio_bit_write(GPIOA, GPIO_PIN_1, (bit_status)(1-gpio_input_bit_get(GPIOA, GPIO_PIN_1)));
exti_interrupt_flag_clear(EXTI_8);
eclic_clear_pending(EXTI5_9_IRQn);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.