更新升级 专属应用 系统故障 硬件故障 电脑汽车 鸿蒙刷机 鸿蒙开发Linux教程 鸿蒙开发Linux命令
当前位置:HMXT之家 > 应用开发 > 鸿蒙系统在启动中初始化了硬件驱动,但还在应用层的代码里重新初始化

鸿蒙系统在启动中初始化了硬件驱动,但还在应用层的代码里重新初始化

更新时间:2022-04-03 09:38:43浏览次数:564+次

关于鸿蒙设备开发问题:明明系统在启动过程中已经初始化了硬件驱动,为什么现在很多的教程还在应用层的代码里重新初始化?当前的情况是:系统在启动过程中已经初始化了UART驱动、Flash驱动、看门狗、NV等,用户在开发过程中,请勿重复初始化这些模块,否则引起系统错误。

\

解答

对于该描述,可以理解为系统启动时如果已经对相应的模块就行了驱动的初始化,是不能再次进行初始化的,其实这些模块的驱动的初始化函数也是需要在用户的应用代码中调用的,否则无法使用相应的功能,但需要重点注意的是:不能多次重复初始化,比如下面hi3861的demoapp的代码:

static void LedExampleEntry(void)

{

osThreadAttr_t attr;

IoTGpioInit(LED_TEST_GPIO);

IoTGpioSetDir(LED_TEST_GPIO, 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;

if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {

printf("[LedExample] Falied to create LedTask!\n");

}

}

IoTGpioInit(LED_TEST_GPIO)同样需要在用户的应用中调用。

以下是openharmony项目的led_example.c源代码:

/*

 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

#include <stdio.h>

#include <unistd.h>

#include "ohos_init.h"

#include "cmsis_os2.h"

#include "iot_gpio.h"

#define LED_INTERVAL_TIME_US 300000

#define LED_TASK_STACK_SIZE 512

#define LED_TASK_PRIO 25

#define LED_TEST_GPIO 9 // for hispark_pegasus

static long long g_iState = 0;

enum LedState {

    LED_ON = 0,

    LED_OFF,

    LED_SPARK,

};

enum LedState g_ledState = LED_SPARK;

static void *LedTask(const char *arg)

{

    (void)arg;

    while (1) {

        switch (g_ledState) {

            case LED_ON:

                IoTGpioSetOutputVal(LED_TEST_GPIO, 1);

                usleep(LED_INTERVAL_TIME_US);

                g_iState++;

                break;

            case LED_OFF:

                IoTGpioSetOutputVal(LED_TEST_GPIO, 0);

                usleep(LED_INTERVAL_TIME_US);

                g_iState++;

                break;

            case LED_SPARK:

                IoTGpioSetOutputVal(LED_TEST_GPIO, 0);

                usleep(LED_INTERVAL_TIME_US);

                IoTGpioSetOutputVal(LED_TEST_GPIO, 1);

                usleep(LED_INTERVAL_TIME_US);

                g_iState++;

                break;

            default:

                usleep(LED_INTERVAL_TIME_US);

                break;

        }

        if (g_iState == 0xffffffff) {

            g_iState = 0;

            break;

        }

    }

    return NULL;

}

static void LedExampleEntry(void)

{

    osThreadAttr_t attr;

    IoTGpioInit(LED_TEST_GPIO);

    IoTGpioSetDir(LED_TEST_GPIO, 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;

    if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {

        printf("[LedExample] Falied to create LedTask!\n");

    }

}

SYS_RUN(LedExampleEntry);

相关参考:OpenHarmony开源版鸿蒙可不可以使用java语言开发