[文章]鸿蒙OS 注册UART驱动接口

阅读量0
0
0
注册UART驱动接口。
HDF框架提供了UART驱动接口的模板方法UartHostMethod,实现UART驱动接口的代码如下:
1.  static int32_tSampleInit(struct UartHost *host)
2.  {
3.      HDF_LOGI("%s: Enter", __func__);
4.      if (host == NULL) {
5.          HDF_LOGE("%s: invalidparameter", __func__);
6.          return HDF_ERR_INVALID_PARAM;
7.      }
8.      return HDF_SUCCESS;
9.  }
10.     
11.    static int32_t SampleDeinit(struct UartHost*host)
12.    {
13.        HDF_LOGI("%s: Enter", __func__);
14.        if (host == NULL) {
15.            HDF_LOGE("%s: invalidparameter", __func__);
16.            return HDF_ERR_INVALID_PARAM;
17.        }
18.        return HDF_SUCCESS;
19.    }
20.     
21.    // 向UART中写入数据
22.    static int32_t SampleWrite(struct UartHost*host, uint8_t *data, uint32_t size)
23.    {
24.        HDF_LOGI("%s: Enter", __func__);
25.        uint32_t idx;
26.        struct UartRegisterMap *regMap = NULL;
27.        struct UartDevice *device = NULL;
28.     
29.        if (host == NULL || data == NULL || size ==0) {
30.            HDF_LOGE("%s: invalidparameter", __func__);
31.            return HDF_ERR_INVALID_PARAM;
32.        }
33.        device = (struct UartDevice *)host->priv;
34.        if (device == NULL) {
35.            HDF_LOGE("%s: device isNULL", __func__);
36.            return HDF_ERR_INVALID_PARAM;
37.        }
38.        regMap = (struct UartRegisterMap *)device->resource.physBase;
39.        for (idx = 0; idx < size; idx++) {
40.            while (UartPl011IsBusy(regMap));
41.            UartPl011Write(regMap, data[idx]);
42.        }
43.        return HDF_SUCCESS;
44.    }
45.     
46.    // 设置UART的波特率
47.    static int32_t SampleSetBaud(structUartHost *host, uint32_t baudRate)
48.    {
49.        HDF_LOGI("%s: Enter", __func__);
50.        struct UartDevice *device = NULL;
51.        struct UartRegisterMap *regMap = NULL;
52.        UartPl011Error err;
53.     
54.        if (host == NULL) {
55.            HDF_LOGE("%s: invalidparameter", __func__);
56.            return HDF_ERR_INVALID_PARAM;
57.        }
58.        device = (struct UartDevice *)host->priv;
59.        if (device == NULL) {
60.            HDF_LOGE("%s: device isNULL", __func__);
61.            return HDF_ERR_INVALID_PARAM;
62.        }
63.        regMap = (struct UartRegisterMap *)device->resource.physBase;
64.        if (device->state !=UART_DEVICE_INITIALIZED) {
65.            return UART_PL011_ERR_NOT_INIT;
66.        }
67.        if (baudRate == 0) {
68.            return UART_PL011_ERR_INVALID_BAUD;
69.        }
70.        err = UartPl011SetBaudrate(regMap,device->uartClk, baudRate);
71.        if (err == UART_PL011_ERR_NONE) {
72.            device->baudrate = baudRate;
73.        }
74.        return err;
75.    }
76.     
77.    // 获取UART的波特率
78.    static int32_t SampleGetBaud(structUartHost *host, uint32_t *baudRate)
79.    {
80.        HDF_LOGI("%s: Enter", __func__);
81.        struct UartDevice *device = NULL;
82.     
83.        if (host == NULL) {
84.            HDF_LOGE("%s: invalidparameter", __func__);
85.            return HDF_ERR_INVALID_PARAM;
86.        }
87.        device = (struct UartDevice *)host->priv;
88.        if (device == NULL) {
89.            HDF_LOGE("%s: device isNULL", __func__);
90.            return HDF_ERR_INVALID_PARAM;
91.        }
92.        *baudRate = device->baudrate;
93.        return HDF_SUCCESS;
94.    }
95.     
96.    // 在HdfUartSampleInit方法中绑定
97.    struct UartHostMethodg_uartSampleHostMethod = {
98.        .Init = SampleInit,
99.        .Deinit = SampleDeinit,
100.      .Read = NULL,
101.      .Write = SampleWrite,
102.      .SetBaud = SampleSetBaud,
103.      .GetBaud = SampleGetBaud,
104.      .SetAttribute = NULL,
105.      .GetAttribute = NULL,
106.      .SetTransMode = NULL,
107.  };
在vendor/huawei/hdf/hdf_vendor.mk编译脚本中增加示例UART驱动模块,代码如下:
1.  LITEOS_BASELIB+= -lhdf_uart_sample
2.  LIB_SUBDIRS    +=$(VENDOR_HDF_DRIVERS_ROOT)/sample/platform/uart

作者:疯壳
注:文档和视频中所有的图片及代码截图皆为示意图,具体以HarmonyOS官网发布内容为准。

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友