电子产业一站式赋能平台

PCB联盟网

搜索
查看: 833|回复: 0
收起左侧

鸿蒙OS 注册UART驱动入口

[复制链接]

2607

主题

2607

帖子

7472

积分

高级会员

Rank: 5Rank: 5

积分
7472
发表于 2020-9-16 15:56:46 | 显示全部楼层 |阅读模式
鸿蒙OS 注册UART驱动入口, 注册UART驱动入口。 基于HDF框架注册UART驱动的入口HdfDriverEntry,代码如下: // 绑定UART驱动接口到HDF框架 static int32_t HdfUartSampleBind(structHdfDeviceObject *device) {    IF (device == NULL) {        return HDF_ERR_INVALID_OBJECT;     }    HDF_LOGI(“Enter %s:“, __func__);    return (UartHostCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS; } // 从UART驱动的HCS中获取配置信息 static uint32_t UartDeviceGetResource(    struct UartDevice *device, const struct DeviceResourceNode*resourceNode) {     struct UartResource *resource =&device->resource;    struct DeviceResourceIface *dri = NULL;    dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);    if (dri == NULL || dri->GetUint32 == NULL) {        HDF_LOGE(“DeviceResourceIface is invalid“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “num“,&resource->num, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readnum fail“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “base“, &resource->base,0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readbase fail“);        return HDF_FAILURE;     }    resource->physBase = (unsigned long) OsalIoRemap(resource->base,0x48);    if (resource->physBase == 0) {        HDF_LOGE(“uart config fail to remap physBase“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “irqNum“,&resource->irqNum, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config read irqNum fail“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “baudrate“,&resource->baudrate, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readbaudrate fail“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “wlen“,&resource->wlen, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readwlen fail“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “parity“,&resource->parity, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readparity fail“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “stopBit“,&resource->stopBit, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readstopBit fail“);        return HDF_FAILURE;     }    if (dri->GetUint32(resourceNode, “uartClk“,&resource->uartClk, 0) != HDF_SUCCESS) {        HDF_LOGE(“uart config  readuartClk fail“);        return HDF_FAILURE;     }    return HDF_SUCCESS; } // 将UART驱动的配置和接口附加到HDF驱动框架 static int32_t SampleAttach(struct UartHost*host, struct HdfDeviceObject *device) {    int32_t ret;    struct UartDevice *uartDevice = NULL;    if (device->property == NULL) {        HDF_LOGE(“%s: property is NULL“, __func__);        return HDF_FAILURE;     }    uartDevice = (struct UartDevice *) OsalMemcalloc(sizeof(struct UartDevice));    if (uartDevice == NULL) {        HDF_LOGE(“%s: OsalMemCalloc uartDevice error“, __func__);        return HDF_ERR_MALLOC_FAIL;     }    ret = UartDeviceGetResource(uartDevice, device->property);    if (ret != HDF_SUCCESS) {        (void) OsalMemFree(uartDevice);        return HDF_FAILURE;     }    host->num = uartDevice->resource.num;    host->priv = uartDevice;    UartSampleAddDev(host); // 添加用户态UART设备节点,具体实现见源码uart_dev_sample    return UartDeviceInit(uartDevice); // 初始化UART PL011,具体实现见源码uart_pl011_sample } // 初始化UART驱动 static int32_t HdfUartSampleInit(structHdfDeviceObject *device) {    int32_t ret;    struct UartHost *host = NULL;    if (device == NULL) {        HDF_LOGE(“%s: device is NULL“, __func__);        return HDF_ERR_INVALID_OBJECT;     }    HDF_LOGI(“Enter %s:“, __func__);    host = UartHostFromDevice(device);    if (host == NULL) {        HDF_LOGE(“%s: host is NULL“, __func__);        return HDF_FAILURE;     }    ret = SampleAttach(host, device);    if (ret != HDF_SUCCESS) {        HDF_LOGE(“%s: attach error“, __func__);        return HDF_FAILURE;     }    host->method = &g_uartSampleHostMethod;    return ret; } static void UartDeviceDeinit(structUartDevice *device) {    struct UartRegisterMap *regMap = (struct UartRegisterMap *)device->resource.physBase;    /* wait for uart enter idle. */    while (UartPl011IsBusy(regMap));    UartPl011ResetRegisters(regMap);    uart_clk_cfg(0, false);    OsalIoUnmap((void *) device->resource.physBase);    device->state = UART_DEVICE_UNINITIALIZED; } // 解绑并释放UART驱动 static void SampLEDetach(struct UartHost*host) {    struct UartDevice *uartDevice = NULL;    if (host->priv == NULL) {        HDF_LOGE(“%s: invalid parameter“, __func__);        return;     }    uartDevice = host->priv;    UartDeviceDeinit(uartDevice);    (void) OsalMemFree(uartDevice);    host->priv = NULL; } // 释放UART驱动 static void HdfUartSampleRelease(structHdfDeviceObject *device) {    struct UartHost *host = NULL;    HDF_LOGI(“Enter %s:“, __func__);    if (device == NULL) {        HDF_LOGE(“%s: device is null“, __func__);        return;     }    host = UartHostFromDevice(device);    if (host == NULL) {        HDF_LOGE(“%s: host is null“, __func__);        return;     }    if (host->priv != NULL) {        SampleDetach(host);     }    UartHostDestroy(host); } struct HdfDriverEntry g_hdfUartSample = {    .moduleVersion = 1,    .moduleName = “UART_SAMPLE“,    .Bind = HdfUartSampleBind,    .Init = HdfUartSampleInit,    .Release = HdfUartSampleRelease, }; HDF_INIT(g_hdfUartSample);
作者:疯壳 注:文档和视频中所有的图片及代码截图皆为示意图,具体以HarmonyOS官网发布内容为准。
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


联系客服 关注微信 下载APP 返回顶部 返回列表