注册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官网发布内容为准。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐