`
对于liteos_m有了一定认识之后,就不多说废话了,直接开始。首先需要在某个目录新建一个一个文件夹存放业务逻辑代码。
目录路径:./applications/sample/wifi-iot/app,在该路径下新建一个文件夹led,其中led.c,BUILD.gn为编译脚本,具体目录结构如下:
新建并编写led.c的业务逻辑代码以及编译脚本
新建./applications/sample/wifi-iot/app/led下的led.c文件,在led.c中新建业务入口函数led,并实现led的GPIO初始化。调用HarmonyOS启动恢复模块接口SYS_RUN()启动业务。
1、BUILD.gn为编译脚本
2、led.c为led代码
led.c
- #include <stdio.h>
- #include <unistd.h>
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include "wifiiot_gpio.h"
- #include "wifiiot_gpio_ex.h"
- #define LED_INTERVAL_TIME_US300000
- #define LED_TASK_STACK_SIZE512
- #define LED_TASK_PRIO 25
- enum LedState
- {
- LED_ON = 0,
- LED_OFF,
- LED_SPARK,
- };
- enum LedState g_ledState = LED_SPARK;
- static void *LedTask(constchar *arg)
- {
- (void)arg;
- while (1)
- {
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
- switch (g_ledState)
- {
- case LED_ON:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 1);
- usleep(LED_INTERVAL_TIME_US);
- break;
- case LED_OFF:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
- usleep(LED_INTERVAL_TIME_US);
- break;
- case LED_SPARK:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
- usleep(LED_INTERVAL_TIME_US);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 1);
- usleep(LED_INTERVAL_TIME_US);
- break;
- default:
- usleep(LED_INTERVAL_TIME_US);
- break;
- }
- }
- return NULL;
- }
- static voidLedExampleEntry(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
- 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;
- printf("[LedExample] try to createLedTask!
- ");
- if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL)
- {
- printf("[LedExample] Falied tocreate LedTask!
- ");
- }
- }
- SYS_RUN(LedExampleEntry);
复制代码同级目录下的BUILD.gn
- static_library("led_example"){
- sources = [
- "led.c"
- include_dirs = [
- "//utils/native/lite/include",
- "//kernel/liteos_m/components/cmsis/2.0",
- "//base/iot_hardware/interfaces/kits/wifiiot_lite",
- }
复制代码上一级目录的BUILD.gn
- import("//build/lite/config/component/lite_component.gni")
-
- lite_component("app") {
- features = [
- "led:Wiking",
- ]
- }
复制代码在虚拟机上执行python build.py wifiiot进行编译,当出现下图时,编译成功即可烧入。
烧入操作可以借鉴上一帖。
`