HaaS EDU K1 equipment resource interruption

1. Interruption concept

During program operation, if an emergency occurs outside the system, inside the system, or the current program itself, the CPU suspends the current program, automatically transfers to the pre-set interrupt service program, and returns to the original program to continue running after processing. , This whole process is called interruption.


According to whether the interrupt can be masked, the interrupt can be divided into two types: maskable interrupt and non-maskable interrupt. The interrupt that can be controlled by the program to issue an interrupt signal is called a maskable interrupt. When the interrupt is masked, the CPU will not receive this interrupt. Conversely, for interrupts whose shielding state cannot be controlled by the program, as long as the interrupt is generated, the interrupt that the CPU must process immediately is called a non-maskable interrupt. Non-maskable interrupts are mainly used for situations that must be dealt with immediately, such as power failures and power failures.
According to the source of the interrupt signal, the interrupt is divided into hardware interrupt and software interrupt. A hardware interrupt is a signal sent when the actual hardware needs to notify the CPU for special event processing. Hardware interrupts such as Timer, UART, GPIO, etc. can all issue hardware interrupts; while a software interrupt is a signal triggered by software (a CPU instruction), which can be used in various operations. In the system, the realization of the system call is generally realized by triggering a software interrupt.

Interrupts are used for many purposes. The system clock tick is calculated based on the Timer interrupt; you can also set the UART configuration to issue an interrupt when the amount of data buffered by the hardware reaches a certain threshold when receiving data; external to the CPU The interrupt of the module is generally reported by the interrupt event by connecting to the IO port with interrupt function on the CPU.

Next, we mainly introduce the GPIO interrupt in HaaS EDK as an example.

 

2. Interrupt related API

API name

Features

hal_gpio_enable_irq

Enable the interrupt mode of the specified GPIO and mount the interrupt service function

hal_gpio_disable_irq

Clear the interrupt status of the specified GPIO

hal_gpio_clear_irq

Clear the interrupt status of the specified GPIO

Before using hal_gpio_xxx_irq to operate the interrupt corresponding to the specified GPIO, you need to call hal_gpio_init to set the GPIO to interrupt mode (IRQ_MODE).

 

2.1、hal_gpio_enable_irq

Enable the interrupt mode of the specified GPIO and mount the interrupt service function.

  • Function prototype and parameter description

int32_t hal_gpio_enable_irq(gpio_dev_t *gpio, gpio_irq_trigger_t trigger,gpio_irq_handler_t handler, void *arg)

API name

Features

gpio

GPIO device description

trigger

Trigger mode of interrupt, trigger on rising edge, falling edge or both

handler

Interrupt service function pointer, the function pointed to will be executed after the interrupt is triggered

arg

Input parameters of interrupt service function

  • Return Value
    Type: int Returns success or failure, returns 0 to enable interrupt success, non-zero indicates failure.

 

2.2、hal_gpio_disable_irq

Turn off the interrupt of the specified GPIO

  • Function prototype and parameter description

int32_t hal_gpio_disable_irq(gpio_dev_t *gpio)

API name

Features

gpio

GPIO device description

  • Return Value
    Type: int Returns success or failure, returns 0 to indicate successful interrupt disabling, non-zero indicates failure.

 

2.3、hal_gpio_clear_irq

Clear the interrupt of the specified GPIO

  • Function prototype and parameter description

int32_t hal_gpio_clear_irq(gpio_dev_t *gpio)

API name

Features

gpio

GPIO device description

  • Return Value
    Type: int Returns success or failure, returns 0 to indicate successful interrupt disabling, non-zero indicates failure.

 

3. Interrupt the experiment

Set the K1 button on the HaaS EDK to interrupt mode, detect the pressed action within 150ms after the button is pressed, and print the "k1 is pressed" log.

 

3.1, hardware


Among them, KEY1 is directly connected to P2_7 of the CPU, and the ID macro in the program code is defined as: HAL_IOMUX_PIN_P2_7

 

3.2, software implementation

#include <aos/hal/gpio.h>


#define EDK_BOARD_KEY1 HAL_IOMUX_PIN_P2_7


/* define dev */

gpio_dev_t k1;


/* pressed flag */

/* K1按键按下的标志位 */

int k1_pressed = 0;


/* 中断处理程序中将k1_pressed设置为1,代表检测到K1按下的事件 */

void k1_handler(void *arg)

{

  k1_pressed = 1;

}



int application_start(int argc, char *argv[])

{

  int ret = -1;


  /* IRQ pin config */

  /* 中断GPIO配置 */

  k1.port = EDK_BOARD_KEY1;


  /* set as interrupt mode */

  /* 设置为中断模式 */

  k1.config = IRQ_MODE;


    /* configure GPIO with the given settings */

    /* 呼叫hal_gpio_init对K1进行初始化 */

    ret = hal_gpio_init(&k1);

    if (ret != 0) {

        printf("k1 init error !\n");

    }


    /* enable interrupt on K1 */

    /* 开启K1对应的中断 */

    ret = hal_gpio_enable_irq(&k1, IRQ_TRIGGER_BOTH_EDGES,

                              k1_handler, NULL);

    if (ret != 0) {

    printf("gpio irq enable error !\n");

    }


    /* if k1 is pressed, print "k1 is pressed !" */

     /* 检测到k1_pressed标志位1, 打印 "k1 is pressed !" */

    while(1) {

        if (k1_pressed == 1) {

            k1_pressed = 0;

            printf("k1 is pressed !\n");

        }


        /* sleep 100ms, must be shorter than 150ms according to experiment's target */

         /* 休眠100ms, 必须要短于150ms,确保在k1_pressed在中断处理程序中被设置成1后能够在150ms内检测到此K1被按下的事件 */

        aos_msleep(100);

    };

}

After this program runs, when the button K1 is pressed, the serial port will print "k1 is pressed!"

 

Developer technical support

If you need more technical support, you can join the DingTalk developer group, or follow the WeChat public account

For more technology and solution introduction, please visit the Aliyun AIoT homepage https://iot.aliyun.com/

Guess you like

Origin blog.csdn.net/HaaSTech/article/details/114788746