電子產(chǎn)業(yè)一站式賦能平臺

PCB聯(lián)盟網(wǎng)

搜索
查看: 741|回復(fù): 0
收起左側(cè)

鴻蒙OS 注冊UART驅(qū)動入口

[復(fù)制鏈接]

2607

主題

2607

帖子

7472

積分

高級會員

Rank: 5Rank: 5

積分
7472
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2020-9-16 15:56:46 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
鴻蒙OS 注冊UART驅(qū)動入口, 注冊UART驅(qū)動入口。 基于HDF框架注冊UART驅(qū)動的入口HdfDriverEntry,代碼如下: // 綁定UART驅(qū)動接口到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驅(qū)動的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驅(qū)動的配置和接口附加到HDF驅(qū)動框架 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); // 添加用戶態(tài)UART設(shè)備節(jié)點,具體實現(xiàn)見源碼uart_dev_sample    return UartDeviceInit(uartDevice); // 初始化UART PL011,具體實現(xiàn)見源碼uart_pl011_sample } // 初始化UART驅(qū)動 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驅(qū)動 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驅(qū)動 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官網(wǎng)發(fā)布內(nèi)容為準(zhǔn)。
回復(fù)

使用道具 舉報

發(fā)表回復(fù)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則


聯(lián)系客服 關(guān)注微信 下載APP 返回頂部 返回列表