Contributed by WangChong, curated by Ai-Thinker
Recently I've been studying interrupts on the Xiao An Pai. Last night I wrote a demo to test it. What an interrupt is has already been explained very clearly. First, here are my references. The first is the interrupt article written by Ze Ge:
This one was written by me:
Since everyone understands the principle, here's the code directly:
#include "bflb_mtimer.h"
#include "board.h"
#include "bflb_gpio.h"
#define DBG_TAG "MAIN"
#include "log.h"
struct bflb_device_s *gpio;
struct bflb_device_s *led;
/**
* @brief interrupt callback function
*
*/
void interrupted_function()
{
// actually this check is unnecessary; if we're here, the interrupt was definitely triggered
bool intstatus = bflb_gpio_get_intstatus(gpio, GPIO_PIN_10);
if (intstatus) {
printf("Interrupt \r\n");
// clear the interrupt flag
bflb_gpio_int_clear(gpio, GPIO_PIN_10);
printf("clean successful \r\n");
}
}
void init_GPIO_interruption()
{
// high level trigger
bflb_gpio_int_init(gpio, GPIO_PIN_10, GPIO_INT_TRIG_MODE_SYNC_HIGH_LEVEL);
// whether to mask the interrupt
bflb_gpio_int_mask(gpio, GPIO_PIN_10, 0);
// bind the interrupt callback function
bflb_irq_attach(gpio->irq_num, interrupted_function, NULL);
}
int main(void)
{
board_init();
// initialize GPIO
gpio = bflb_device_get_by_name("gpio");
// initialize the interrupt
init_GPIO_interruption();
// enable the interrupt according to the GPIO interrupt number
bflb_irq_enable(gpio->irq_num);
while (1) {
// interrupt not triggered
printf("correct!");
};
}Expected result:After connecting with a serial tool, the console keeps printing "Interrupt" and "clean successful". If you connect a Dupont wire from IO10 to GND at this time, it will print "correct!" instead.
Tutorial section:
Two library files are mainly used here: #include "bflb_gpio.h" and bflb_irq.h
#define GPIO_INT_TRIG_MODE_SYNC_FALLING_EDGE 0
#define GPIO_INT_TRIG_MODE_SYNC_RISING_EDGE 1
#define GPIO_INT_TRIG_MODE_SYNC_LOW_LEVEL 2
#define GPIO_INT_TRIG_MODE_SYNC_HIGH_LEVEL 3
#if defined(BL602) || defined(BL702)
#define GPIO_INT_TRIG_MODE_ASYNC_FALLING_EDGE 4
#define GPIO_INT_TRIG_MODE_ASYNC_RISING_EDGE 5
#define GPIO_INT_TRIG_MODE_ASYNC_LOW_LEVEL 6
#define GPIO_INT_TRIG_MODE_ASYNC_HIGH_LEVEL 7
#else
#define GPIO_INT_TRIG_MODE_SYNC_FALLING_RISING_EDGE 4
#define GPIO_INT_TRIG_MODE_ASYNC_FALLING_EDGE 8
#define GPIO_INT_TRIG_MODE_ASYNC_RISING_EDGE 9
#define GPIO_INT_TRIG_MODE_ASYNC_LOW_LEVEL 10
#define GPIO_INT_TRIG_MODE_ASYNC_HIGH_LEVEL 11
#endifThe method is as follows:
/**
* @brief whether to mask the GPIO interrupt; if masked here, the interrupt cannot be entered
*
* @param [in] dev device handle
* @param [in] pin gpio pin, use @ref GPIO_PIN
* @param [in] mask true means disable, false means enable
*/
void bflb_gpio_int_mask(struct bflb_device_s *dev, uint8_t pin, bool mask);
/**
* @brief get the GPIO interrupt status
*
* @param [in] dev device handle
* @param [in] pin gpio pin, use @ref GPIO_PIN
* @return true means yes, false means no
*/
bool bflb_gpio_get_intstatus(struct bflb_device_s *dev, uint8_t pin);
/**
* @brief clear the GPIO interrupt flag
*
* @param [in] dev device handle
* @param [in] pin gpio pin, use @ref GPIO_PIN
*/
void bflb_gpio_int_clear(struct bflb_device_s *dev, uint8_t pin);
/**
* @brief bind the GPIO interrupt to a method
*
* @param [in] irq irq number
* @param [in] isr interrupt callback
* @param [in] this parameter seems to be miswritten by Ze Ge; it seems to be the formal parameter of the data callback function. Pass NULL if nothing needs to be passed
* @return A negated errno value on failure.
*/
int bflb_irq_attach(int irq, irq_callback isr, void *arg);Attachments: 
main.zip(773 Bytes, downloads: 2)
Uploaded 2023-10-21 17:37
Click the filename to download the attachment.
Have questions?
For other questions, please visit the unified discussion area: Ai-Thinker Discussions

