device-wlan-led-outcontrol.md 4.2 KB
Newer Older
D
duangavin123 已提交
1
# LED Peripheral Control<a name="EN-US_TOPIC_0000001054530966"></a>
W
wenjun 已提交
2

D
duangavin123 已提交
3 4 5 6 7 8 9 10 11 12
-   [Overview](#section14639174516337)
-   [Development](#section13857170163412)
-   [Verification](#section1949121910344)

## Overview<a name="section14639174516337"></a>

Based on the Hi3861 platform, the OpenHarmony WLAN module provides abundant peripheral operation capabilities, including the I2C, I2S, ADC, UART, SPI, SDIO, GPIO, PWM, and flash memory. This document describes how to control GPIO pins by calling the OpenHarmony native development kit \(NDK\) interface to implement LED blinking. For details about how to control other IoT peripherals, see the API guide.

## Development<a name="section13857170163412"></a>

D
duangavin123 已提交
13
1.  Complete the operations described in  [Getting Started with Hi3861](../quick-start/oem_minitinier_des_3861.md#section19352114194115).
W
wenjun 已提交
14 15 16

    LED control examples are stored in the file  **applications/sample/wifi-iot/app/iothardware/led\_example.c**.

D
duangavin123 已提交
17
2.  Understand the cable connections by referring to the schematic diagram. You can learn that LED of hispark pegasus is connected to pin 9 of the chip.
N
NEEN 已提交
18 19 20 21

    ```
    #define LED_TEST_GPIO 9
    ```
W
wenjun 已提交
22

D
duangavin123 已提交
23
    >![](../public_sys-resources/icon-note.gif) **NOTE:** 
W
wenjun 已提交
24 25 26 27 28 29 30 31 32 33
    >For details about the schematic diagram of the development board, contact the Hi3861 customer service personnel.

3.  Initialize the GPIO pin, specify the pin usage, and create a task to turn on or off the LED periodically so that the LED blinks.

    ```
    static void LedExampleEntry(void)
    {
        osThreadAttr_t attr;
    
        /* Initialize the GPIO pin. */
N
NEEN 已提交
34
        IoTGpioInit(LED_TEST_GPIO);
W
wenjun 已提交
35
        /* Set pin 9 as the output direction. */
N
NEEN 已提交
36
        IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
W
wenjun 已提交
37 38 39 40 41 42 43 44 45 46 47
    
        attr.name = "LedTask";
        attr.attr_bits = 0U;
        attr.cb_mem = NULL;
        attr.cb_size = 0U;
        attr.stack_mem = NULL;
        attr.stack_size = LED_TASK_STACK_SIZE;
        attr.priority = LED_TASK_PRIO;
    
        /* Start the task. */
        if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
N
NEEN 已提交
48
            printf("[LedExample] Failed to create LedTask!\n");
W
wenjun 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
        }
    }
    ```

4.  Use a cyclic task in which the LED periodically turns on and off to implement LED blinking.

    ```
    static void *LedTask(const char *arg)
    {
        (void)arg;
        while (1) {
            switch (g_ledState) {
                case LED_ON:
N
NEEN 已提交
62
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
W
wenjun 已提交
63 64 65
                    usleep(LED_INTERVAL_TIME_US);
                    break;
                case LED_OFF:
N
NEEN 已提交
66
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
W
wenjun 已提交
67 68 69
                    usleep(LED_INTERVAL_TIME_US);
                    break;
                case LED_SPARK:
N
NEEN 已提交
70
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
W
wenjun 已提交
71
                    usleep(LED_INTERVAL_TIME_US);
N
NEEN 已提交
72
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
W
wenjun 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                    usleep(LED_INTERVAL_TIME_US);
                    break;
                default:
                    usleep(LED_INTERVAL_TIME_US);
                    break;
            }
        }
        return NULL;
    }
    ```

5.  Call  **SYS\_RUN\(\)**  of OpenHarmony to start the service. \(**SYS\_RUN**  is defined in the  **ohos\_init.h**  file.\)

    ```
    SYS_RUN(LedExampleEntry);
    ```

6.  Change the  **applications/sample/wifi-iot/app/BUILD.gn**  file to enable  **led\_example.c**  to participate in compilation.

    ```
    import("//build/lite/config/component/lite_component.gni")
    lite_component("app") {
        features = [
            "iothardware:led_example"
        ]
    }
    ```


D
duangavin123 已提交
102 103
## Verification<a name="section1949121910344"></a>

104
For details about the compilation and burning processes, see  [Building Source Code](../quick-start/quickstart-lite-steps-hi3861-connection.md#section191121332125319)  and  [Burning Images](../quick-start/quickstart-lite-steps-hi3861-connection.md#section3288165814218)  in the  _Getting Started with Hi3861_.
D
duangavin123 已提交
105 106 107 108

After the preceding two steps are complete, press the  **RST**  button to reset the module. If the LED blinks periodically as expected, the verification is passed.

**Figure  1**  LED blinking<a name="fig20768175218527"></a>  
D
duangavin123 已提交
109
![](figures/led-blinking.gif "led-blinking")
D
duangavin123 已提交
110