device-wlan-led-control.md 3.9 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
## 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>

E
ester.zhou 已提交
9
1.  Complete the operations described in [Getting Started with Hi3861](../quick-start/quickstart-lite-overview.md).
W
wenjun 已提交
10

E
ester.zhou 已提交
11
    LED control examples are stored in the file **applications/sample/wifi-iot/app/iothardware/led\_example.c**.
W
wenjun 已提交
12

13
2.  Understand the cable connections by referring to the schematic diagram of the development board. You can learn that LED of hispark pegasus is connected to pin 9 of the chip.
N
NEEN 已提交
14 15 16 17

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

19 20
    >![](../public_sys-resources/icon-note.gif) **NOTE:**
    >
W
wenjun 已提交
21 22
    >For details about the schematic diagram of the development board, contact the Hi3861 customer service personnel.

23
3.  Initialize the GPIO pin, specify the pin usage, and create a task that turns on or off the LED periodically to implement LED blinking.
W
wenjun 已提交
24 25 26 27 28 29 30

    ```
    static void LedExampleEntry(void)
    {
        osThreadAttr_t attr;
    
        /* Initialize the GPIO pin. */
N
NEEN 已提交
31
        IoTGpioInit(LED_TEST_GPIO);
W
wenjun 已提交
32
        /* Set pin 9 as the output direction. */
N
NEEN 已提交
33
        IoTGpioSetDir(LED_TEST_GPIO, IOT_GPIO_DIR_OUT);
W
wenjun 已提交
34 35 36 37 38 39 40 41 42 43 44
    
        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 已提交
45
            printf("[LedExample] Failed to create LedTask!\n");
W
wenjun 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
        }
    }
    ```

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 已提交
59
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
W
wenjun 已提交
60 61 62
                    usleep(LED_INTERVAL_TIME_US);
                    break;
                case LED_OFF:
N
NEEN 已提交
63
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
W
wenjun 已提交
64 65 66
                    usleep(LED_INTERVAL_TIME_US);
                    break;
                case LED_SPARK:
N
NEEN 已提交
67
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 0);
W
wenjun 已提交
68
                    usleep(LED_INTERVAL_TIME_US);
N
NEEN 已提交
69
                    IoTGpioSetOutputVal(LED_TEST_GPIO, 1);
W
wenjun 已提交
70 71 72 73 74 75 76 77 78 79
                    usleep(LED_INTERVAL_TIME_US);
                    break;
                default:
                    usleep(LED_INTERVAL_TIME_US);
                    break;
            }
        }
    }
    ```

E
ester.zhou 已提交
80
5.  Call **SYS\_RUN\(\)** of OpenHarmony to start the service. \(**SYS\_RUN** is defined in the **ohos\_init.h** file.\)
W
wenjun 已提交
81 82 83 84 85

    ```
    SYS_RUN(LedExampleEntry);
    ```

86
6.  Add **led\_example.c** to the **applications/sample/wifi-iot/app/BUILD.gn** file for building.
W
wenjun 已提交
87 88 89 90 91 92 93 94 95 96 97

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


D
duangavin123 已提交
98 99
## Verification<a name="section1949121910344"></a>

E
ester.zhou 已提交
100
For details about the compilation and burning processes, see [Building](../quick-start/quickstart-lite-steps-hi3861-building.md) and [Burning](../quick-start/quickstart-lite-steps-hi3861-burn.md).
D
duangavin123 已提交
101

E
ester.zhou 已提交
102
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.
D
duangavin123 已提交
103

E
ester.zhou 已提交
104
**Figure  1** LED blinking<a name="fig20768175218527"></a>  
D
duangavin123 已提交
105
![](figures/led-blinking.gif "led-blinking")
D
duangavin123 已提交
106