KEY of HaaS EDU K1 equipment resources

1. Background introduction


HaaS EDU K1 is the abbreviation of HaaS Education Kit1. It is an IoT educational development board based on the quad-core high-performance MCU-HaaS1000 chip that integrates beauty and connotation.

For the overall hardware introduction, if you are interested, please refer to HaaS EDU K1 hardware introduction .

For the overall introduction of equipment resources, if you are interested, please refer to the overall introduction of HaaS EDU K1 equipment resources .

 

In this article, we will use the 3 buttons on the board to learn how to implement the buttons. Through the study of this article, you will learn how to use IO port as input port and GPIO interrupt.

The GPIO interface has been introduced in detail in the GPIO chapter, please refer to the GPIO of HaaS EDU K1 equipment resources

HaaS EDU K1 has four buttons on board, as shown in the figure below. They are K1, K2, K3, K4.

                                                          Figure 1 HaaS EDU K1 floor plan

The GPIOs corresponding to the four buttons are:

Key name

Corresponding GPIO mapping

GPIO mode

K1

GPIO_P2_7

Input mode

K2

GPIO_P2_4

Input mode

K3

GPIO_P2_5

Input mode

K4

GPIO_P3_2

Input mode

 

2. Example introduction

The IO port has already been introduced in more detail in the previous chapter, so we won't talk about it here. When the IO port is used as input, there are two ways, one is to directly read the level value of the IO port, this is called Polling; the other is through interruption, for example, when the IO port has an input, it will trigger one Interrupted.

Knowing this, we can start our code writing.

We will learn through the 3 buttons on the board (K1, K2 and K3).

 

2.1, hardware implementation

The hardware used in this chapter has KEY respectively. The circuit is already connected on the development board by default, so there is no need to move anything on the hardware. The connection schematic diagram is as follows:

   

                            Figure 2 Schematic diagram of the button part

 

2.2, software design

2.2.1, operation effect

Press the four buttons separately, and each will have a response. The results are as follows:

key 1 press

key 1 press

key 2 press

key 2 press

key 2 press

key 3 press

key 4 press

 

2.2.2, application code

The code files used in the key test are as follows:

application/example/edu_demo/mfg_test/key_test.c

key_test.c finishes printing the relevant key information when the key value is received. The function key_test calls key_init to initialize the key function, and sets a callback function key_event_handle. When there is a key event, it will call back key_event_handle, and then display related information in key_event_handle.

static void key_event_handle(key_code_t key_code)

{

     switch (key_code)

     {

        case EDK_KEY_1:

            printf("key 1 press\n");

            led_test_flag = 0;

            break;

        case EDK_KEY_2:

            printf("key 2 press\n");

            led_test_flag = 0;

            break;

        case EDK_KEY_3:

            printf("key 3 press\n");

            led_test_flag = 0;

            break;

        case EDK_KEY_4:

            printf("key 4 press\n");

            led_test_flag = 0;

            break;

        case EDK_KEY_1 | EDK_KEY_2:

            printf("Enter auto_factory_test mode\n");

            auto_test_flag = 1;

        break;   

     }

}



void key_test(void)

{

    key_init(key_event_handle);

}

 

2.2.3, key detection code

The code for detecting the key is in key.c, and the path is as follows:

platform/board/haaseduk1/drivers/key.c

There are four buttons in EDK . The button initialization is in key_init, here is mainly to set a unified callback function, this callback function has a parameter key_code, which indicates which button or which keys (combination) are pressed.

 

key_init initializes the 4 Pins connected to the button, here it is initialized to IRQ_MODE by hal_gpio_init, which is the interrupt mode. Enable the interrupt by hal_gpio_enable, the interrupt processing function is key_falling_edge_handle, that is, when the button is not pressed, the PIN pin is high, and when the button is pressed, it will be pulled to low level, so the start of the button is a falling edge, through key_falling_edge_handle Function processing, on the contrary, release the button to trigger a rising edge, which is processed by the key_rising_edge_handle function.

 

At the same time, a timer is started here to execute key_poll regularly. This function polls four keys and is mainly used to detect the pressing of a combination key.

int key_init(key_code_cb key_func)

{

    int32_t ret = 0;

    uint32_t i = 0;

    printf("enter %s:%d\n", __func__,__LINE__);



    if (key_func) {

        notify_key_code_cb = key_func;

    } else {

        return -1;

    }



    for (i = 0; i < KEY_NUM; i++) {

        ret = hal_gpio_init(&key_dev_input[i].gpio_dev);

        if (ret) {

            LOGE(TAG, "di %d pin %d init fail ret", i, key_dev_input[i].gpio_dev.port, ret);

            return -1;

        }



        ret = hal_gpio_enable_irq(&key_dev_input[i].gpio_dev, IRQ_TRIGGER_FALLING_EDGE,

                            key_falling_edge_handle, &key_dev_input[i].gpio_dev);

        if (ret) {

            LOGE(TAG, "di %d pin %d fail enable irq ret %d", i, key_dev_input[i].gpio_dev.port, ret);

            return -1;

        }

        key_dev_input[i].installed = 1;

    }



        /*init the gpio check timer, check gpio value every 30ms */

    ret = aos_timer_new_ext(&key_poll_timer, key_poll, NULL, TIMER_CHECK_INTERVAL, 1, 1);

    if (ret) {

        LOGE(TAG, "Fail to new gpio value check timer ret 0x%x", ret);

        for (i = 0; i < KEY_NUM; i++) {

            hal_gpio_disable_irq(&key_dev_input[i].gpio_dev);

            hal_gpio_finalize(&key_dev_input[i].gpio_dev);

            key_dev_input[i].installed = 0;

        }

        return -1;

    }



    return 0;

}

 

2.3. Compile and download

2.3.1, code preparation

Turn on the production test switch of edu_demo

application/example/edu_demo/Config.in

Modify the compilation options in this file and turn on the EDK_DEMO_FACTORY_TEST_ENABLIE switch.

config EDK_DEMO_FACTORY_TEST_ENABLIE

    bool "enable factory test function"

    default y

 

Add Demo to the startup code

application/example/edu_demo/app_entry.c

In the function application_start, comment out menu_init(); and add key_test();

        //menu_init();

        key_test();

2.3.2, compile

Compile from the command line

aos make distclean

aos make edu_demo@haaseduk1 -c config

aos make

 

2.3.3, burn

  • Command line
aos upload
  • Graphical interface

For details, please refer to chapter 4.3.3 of haaS EDU K1 Quick Start- Burning with GUI Tool.

 

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/114581085