|
鴻蒙OS 注冊UART驅(qū)動接口, 注冊UART驅(qū)動接口。 HDF框架提供了UART驅(qū)動接口的模板方法UartHostMethod,實現(xiàn)UART驅(qū)動接口的代碼如下: 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中寫入數(shù)據(jù) 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. // 設(shè)置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驅(qū)動模塊,代碼如下: 1. LITEOS_BASELIB+= -lhdf_uart_sample 2. LIB_SUBDIRS +=$(VENDOR_HDF_DRIVERS_ROOT)/sample/platform/uart
作者:瘋殼 注:文檔和視頻中所有的圖片及代碼截圖皆為示意圖,具體以HarmonyOS官網(wǎng)發(fā)布內(nèi)容為準。 |
|