|
鴻蒙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); // 添加用戶態(tài)UART設備節(jié)點,具體實現(xiàn)見源碼uart_dev_sample return UartDeviceInit(uartDevice); // 初始化UART PL011,具體實現(xiàn)見源碼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官網(wǎng)發(fā)布內容為準。 |
|