1 /*
2  * Copyright (c) 2022 Talkweb Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include "hal_gpio.h"
12 #include "uart_if.h"
13 #include "stm32f4xx_ll_bus.h"
14 #include "uart_core.h"
15 #include "hal_usart.h"
16 #include "osal_sem.h"
17 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
18 #include "hcs_macro.h"
19 #include "hdf_config_macro.h"
20 #else
21 #include "device_resource_if.h"
22 #endif
23 #include "hdf_log.h"
24 
25 #define HDF_UART_TMO 1000
26 #define HDF_LOG_TAG uartDev
27 #define GPIO_MAX_LENGTH 32
28 #define UART_FIFO_MAX_BUFFER 2048
29 #define UART_DMA_RING_BUFFER_SIZE 256 // mast be 2^n
30 
31 #define UART_IRQ_PRO 5
32 #define UART_DEV_SERVICE_NAME_PREFIX "HDF_PLATFORM_UART%d"
33 #define MAX_DEV_NAME_SIZE 32
34 typedef enum {
35     USART_NUM_1 = 1,
36     USART_NUM_2,
37     USART_NUM_3,
38     USART_NUM_4,
39     USART_NUM_5,
40     USART_NUM_6,
41 } USART_NUM;
42 
43 typedef enum {
44     USART_DATAWIDTH_8 = 0,
45     USART_DATAWIDTH_9,
46     USART_DATAWIDTH_MAX,
47 } USART_DATAWIDTH;
48 
49 typedef enum {
50     USART_STOP_BIT_0_5 = 0,
51     USART_STOP_BIT_1,
52     USART_STOP_BIT_1_5,
53     USART_STOP_BIT_2,
54     USART_STOP_BIT_MAX,
55 } USART_STOP_BIT;
56 
57 typedef enum {
58     USART_PARITY_CHECK_NONE = 0,
59     USART_PARITY_CHECK_EVENT,
60     USART_PARITY_CHECK_ODD,
61     USART_PARITY_CHECK_MAX,
62 } USART_PARITY_CHECK;
63 
64 typedef enum {
65     USART_TRANS_DIR_NONE = 0,
66     USART_TRANS_DIR_RX,
67     USART_TRANS_DIR_TX,
68     USART_TRANS_DIR_RX_TX,
69     USART_TRANS_DIR_MAX,
70 } USART_TRANS_DIR;
71 
72 typedef enum {
73     USART_FLOW_CTRL_NONE = 0,
74     USART_FLOW_CTRL_RTS,
75     USART_FLOW_CTRL_CTS,
76     USART_FLOW_CTRL_RTS_CTS,
77     USART_FLOW_CTRL_MAX,
78 } USART_FLOW_CTRL;
79 
80 typedef enum {
81     USART_OVER_SIMPLING_16 = 0,
82     USART_OVER_SIMPLING_8,
83     USART_OVER_SIMPLING_MAX,
84 } USART_OVER_SIMPLING;
85 
86 typedef enum {
87     USART_TRANS_BLOCK = 0, // block
88     USART_TRANS_NOBLOCK,
89     USART_TRANS_TX_DMA, // TX DMA RX NORMAL
90     USART_TRANS_RX_DMA, // TX NORMAL  RX DMA
91     USART_TRANS_TX_RX_DMA, // TX DMA  RX DMA
92     USART_TRANS_MODE_MAX,
93 } USART_TRANS_MODE;
94 
95 typedef enum {
96     USART_IDLE_IRQ_DISABLE = 0,
97     USART_IDLE_IRQ_ENABLE,
98     USART_IDLE_IRQ_MAX,
99 } USART_IDLE_IRQ;
100 
101 typedef enum {
102     USART_232 = 0,
103     USART_485,
104     USART_TYPE_MAX,
105 } USART_TYPE;
106 
107 typedef struct {
108     USART_NUM num;
109     uint32_t baudRate;
110     USART_DATAWIDTH dataWidth;
111     USART_STOP_BIT stopBit;
112     USART_PARITY_CHECK parity;
113     USART_TRANS_DIR transDir;
114     USART_FLOW_CTRL flowCtrl;
115     USART_OVER_SIMPLING overSimpling;
116     USART_TRANS_MODE transMode;
117     USART_TYPE uartType;
118     STM32_GPIO_PIN dePin;
119     STM32_GPIO_GROUP deGroup;
120 } UartResource;
121 
122 typedef enum {
123     UART_DEVICE_UNINITIALIZED = 0x0u,
124     UART_DEVICE_INITIALIZED = 0x1u,
125 } UartDeviceState;
126 
127 typedef enum {
128     UART_ATTR_HDF_TO_NIOBE = 0,
129     UART_ATTR_NIOBE_TO_HDF,
130 } UART_ATTR_TRAN_TYPE;
131 
132 typedef void (*HWI_UART_IDLE_IRQ)(void);
133 
134 typedef struct {
135     bool txDMA;
136     bool rxDMA;
137     bool isBlock;
138     uint8_t dePin;
139     uint8_t deGroup;
140     uint8_t uartType;
141 } UsartContextObj;
142 
143 typedef struct {
144     struct IDeviceIoService ioService;
145     UartResource resource;
146     LL_USART_InitTypeDef initTypedef;
147     uint32_t uartId;
148     bool initFlag;
149     USART_TRANS_MODE transMode;
150 } UartDevice;
151 
152 static const uint32_t g_dataWidthMap[USART_DATAWIDTH_MAX] = {
153     LL_USART_DATAWIDTH_8B,
154     LL_USART_DATAWIDTH_9B,
155 };
156 
157 static const uint32_t g_stopBitMap[USART_STOP_BIT_MAX] = {
158     LL_USART_STOPBITS_0_5,
159     LL_USART_STOPBITS_1,
160     LL_USART_STOPBITS_1_5,
161     LL_USART_STOPBITS_2,
162 };
163 
164 static const uint32_t g_flowControlMap[USART_FLOW_CTRL_MAX] = {
165     LL_USART_HWCONTROL_NONE,
166     LL_USART_HWCONTROL_RTS,
167     LL_USART_HWCONTROL_CTS,
168     LL_USART_HWCONTROL_RTS_CTS,
169 };
170 
171 static const uint32_t g_overSimplingMap[USART_OVER_SIMPLING_MAX] = {
172     LL_USART_OVERSAMPLING_16,
173     LL_USART_OVERSAMPLING_8,
174 };
175 
176 static const uint32_t g_transDirMap[USART_TRANS_DIR_MAX] = {
177     LL_USART_DIRECTION_NONE,
178     LL_USART_DIRECTION_RX,
179     LL_USART_DIRECTION_TX,
180     LL_USART_DIRECTION_TX_RX,
181 };
182 
183 static const uint32_t g_parityMap[USART_PARITY_CHECK_MAX] = {
184     LL_USART_PARITY_NONE,
185     LL_USART_PARITY_EVEN,
186     LL_USART_PARITY_ODD,
187 };
188 
189 static const USART_TypeDef* g_usartRegMap[USART_NUM_6] = {
190     USART1,
191     USART2,
192     USART3,
193     UART4,
194     UART5,
195     USART6,
196 };
197 static UsartContextObj g_uartCtx[USART_NUM_6] = {0};
198 
USARTTxMode(STM32_GPIO_PIN gpioPin,STM32_GPIO_GROUP group)199 static void USARTTxMode(STM32_GPIO_PIN gpioPin, STM32_GPIO_GROUP group)
200 {
201     uint32_t pinReg = LL_GET_HAL_PIN(gpioPin);
202     GPIO_TypeDef* gpiox = NULL;
203     gpiox = LL_GET_GPIOX(group);
204     if (gpiox == NULL) {
205         return;
206     }
207     LL_GPIO_SetOutputPin(gpiox, pinReg);
208 }
209 
USARTRxMode(STM32_GPIO_PIN gpioPin,STM32_GPIO_GROUP group)210 static void USARTRxMode(STM32_GPIO_PIN gpioPin, STM32_GPIO_GROUP group)
211 {
212     uint32_t pinReg = LL_GET_HAL_PIN(gpioPin);
213     GPIO_TypeDef* gpiox = NULL;
214     gpiox = LL_GET_GPIOX(group);
215     if (gpiox == NULL) {
216         return;
217     }
218     LL_GPIO_ResetOutputPin(gpiox, pinReg);
219 }
220 
221 static const uint32_t g_exitIrqnMap[USART_NUM_6] = {
222     USART1_IRQn,
223     USART2_IRQn,
224     USART3_IRQn,
225     UART4_IRQn,
226     UART5_IRQn,
227     USART6_IRQn,
228 };
229 
InitUsartClock(USART_NUM num)230 static void InitUsartClock(USART_NUM num)
231 {
232     switch (num) {
233         case USART_NUM_1:
234             LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
235             break;
236         case USART_NUM_2:
237             LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
238             break;
239         case USART_NUM_3:
240             LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3);
241             break;
242         case USART_NUM_4:
243             LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4);
244             break;
245         case USART_NUM_5:
246             LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART5);
247             break;
248         case USART_NUM_6:
249             LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART6);
250             break;
251         default:
252             break;
253     }
254 }
255 
InitContextTransMode(UsartContextObj * ctx,USART_TRANS_MODE mode)256 static void InitContextTransMode(UsartContextObj* ctx, USART_TRANS_MODE mode)
257 {
258     switch (mode) {
259         case USART_TRANS_BLOCK:
260             ctx->isBlock = true;
261             ctx->rxDMA = false;
262             ctx->txDMA = false;
263             break;
264         case USART_TRANS_NOBLOCK:
265             ctx->isBlock = false;
266             ctx->rxDMA = false;
267             ctx->txDMA = false;
268             break;
269         case  USART_TRANS_TX_DMA: // TX DMA RX NORMAL
270             ctx->isBlock = false;
271             ctx->rxDMA = false;
272             ctx->txDMA = true;
273             break;
274         case USART_TRANS_RX_DMA: // TX NORMAL  RX DMA
275             ctx->isBlock = false;
276             ctx->rxDMA = true;
277             ctx->txDMA = false;
278             break;
279         case USART_TRANS_TX_RX_DMA: // TX DMA  RX DMA
280             ctx->isBlock = false;
281             ctx->rxDMA = true;
282             ctx->txDMA = true;
283             break;
284         default:
285             break;
286     }
287 }
288 
289 static int32_t UartDriverBind(struct HdfDeviceObject *device);
290 static int32_t UartDriverInit(struct HdfDeviceObject *device);
291 static void UartDriverRelease(struct HdfDeviceObject *device);
292 
293 struct HdfDriverEntry g_UartDriverEntry = {
294     .moduleVersion = 1,
295     .moduleName = "ST_UART_MODULE_HDF",
296     .Bind = UartDriverBind,
297     .Init = UartDriverInit,
298     .Release = UartDriverRelease,
299 };
300 HDF_INIT(g_UartDriverEntry);
301 
302 static int32_t UartHostDevInit(struct UartHost *host);
303 static int32_t UartHostDevDeinit(struct UartHost *host);
304 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size);
305 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate);
306 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate);
307 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size);
308 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute);
309 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute);
310 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode);
311 
312 struct UartHostMethod g_uartHostMethod = {
313     .Init = UartHostDevInit,
314     .Deinit = UartHostDevDeinit,
315     .Read = UartHostDevRead,
316     .Write = UartHostDevWrite,
317     .SetBaud = UartHostDevSetBaud,
318     .GetBaud = UartHostDevGetBaud,
319     .SetAttribute = UartHostDevSetAttribute,
320     .GetAttribute = UartHostDevGetAttribute,
321     .SetTransMode = UartHostDevSetTransMode,
322 };
323 
InitUartDevice(struct UartHost * host)324 static int InitUartDevice(struct UartHost *host)
325 {
326     uint8_t initRet;
327     UartDevice *uartDevice = NULL;
328     LL_USART_InitTypeDef *initTypedef = NULL;
329     UartResource *resource = NULL;
330 
331     if (host == NULL || host->priv == NULL) {
332         HDF_LOGE("%s: invalid parameter", __func__);
333         return HDF_ERR_INVALID_PARAM;
334     }
335 
336     uartDevice = (UartDevice *)host->priv;
337     resource = &uartDevice->resource;
338     if (resource == NULL) {
339         HDF_LOGE("%s: INVALID OBJECT", __func__);
340         return HDF_ERR_INVALID_OBJECT;
341     }
342     initTypedef = &uartDevice->initTypedef;
343     if (initTypedef == NULL) {
344         HDF_LOGE("%s: INVALID OBJECT", __func__);
345         return HDF_ERR_INVALID_OBJECT;
346     }
347     InitContextTransMode(&g_uartCtx[uartDevice->uartId - 1], resource->transMode);
348     LL_USART_Disable(g_usartRegMap[uartDevice->uartId - 1]);
349     LL_USART_DeInit(g_usartRegMap[uartDevice->uartId - 1]);
350     InitUsartClock(uartDevice->uartId);
351 
352     if (!uartDevice->initFlag) {
353         initRet = LL_USART_Init(g_usartRegMap[uartDevice->uartId - 1], initTypedef);
354         if (initRet) {
355             HDF_LOGE("uart %ld device init failed\r\n", uartDevice->uartId);
356             return HDF_FAILURE;
357         }
358         LL_USART_ConfigAsyncMode(g_usartRegMap[uartDevice->uartId - 1]);
359         LL_USART_Enable(g_usartRegMap[uartDevice->uartId - 1]);
360         UART_IRQ_INIT(g_usartRegMap[uartDevice->uartId - 1], uartDevice->uartId,
361             g_exitIrqnMap[uartDevice->uartId - 1], g_uartCtx[uartDevice->uartId - 1].isBlock);
362 
363         uartDevice->initFlag = true;
364     }
365 
366     return HDF_SUCCESS;
367 }
368 
369 #ifndef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
GetUartHcs(const struct DeviceResourceIface * dri,const struct DeviceResourceNode * resourceNode,UartResource * resource)370 static int32_t GetUartHcs(const struct DeviceResourceIface *dri,
371     const struct DeviceResourceNode *resourceNode, UartResource *resource)
372 {
373     if (dri->GetUint8(resourceNode, "num", &resource->num, 0) != HDF_SUCCESS) {
374         return HDF_FAILURE;
375     }
376 
377     if (dri->GetUint32(resourceNode, "baudRate", &resource->baudRate, 0) != HDF_SUCCESS) {
378         return HDF_FAILURE;
379     }
380 
381     if (dri->GetUint8(resourceNode, "dataWidth", &resource->dataWidth, 0) != HDF_SUCCESS) {
382         return HDF_FAILURE;
383     }
384 
385     if (dri->GetUint8(resourceNode, "stopBit", &resource->stopBit, 0) != HDF_SUCCESS) {
386         return HDF_FAILURE;
387     }
388 
389     if (dri->GetUint8(resourceNode, "parity", &resource->parity, 0) != HDF_SUCCESS) {
390         return HDF_FAILURE;
391     }
392 
393     if (dri->GetUint8(resourceNode, "transDir", &resource->transDir, 0) != HDF_SUCCESS) {
394         return HDF_FAILURE;
395     }
396 
397     if (dri->GetUint8(resourceNode, "flowCtrl", &resource->flowCtrl, 0) != HDF_SUCCESS) {
398         return HDF_FAILURE;
399     }
400 
401     if (dri->GetUint8(resourceNode, "overSimpling", &resource->overSimpling, 0) != HDF_SUCCESS) {
402         return HDF_FAILURE;
403     }
404 
405     if (dri->GetUint8(resourceNode, "transMode", &resource->transMode, 0) != HDF_SUCCESS) {
406         return HDF_FAILURE;
407     }
408 
409     if (dri->GetUint8(resourceNode, "uartType", &resource->uartType, 0) != HDF_SUCCESS) {
410         return HDF_FAILURE;
411     }
412 
413     if (dri->GetUint8(resourceNode, "uartDePin", &resource->dePin, 0) != HDF_SUCCESS) {
414         return HDF_FAILURE;
415     }
416 
417     if (dri->GetUint8(resourceNode, "uartDeGroup", &resource->deGroup, 0) != HDF_SUCCESS) {
418         return HDF_FAILURE;
419     }
420 
421     return HDF_SUCCESS;
422 }
423 #endif
424 
425 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
426 #define UART_FIND_CONFIG(node, name, resource) \
427     do { \
428         if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \
429             resource->num = HCS_PROP(node, num); \
430             resource->baudRate = HCS_PROP(node, baudRate); \
431             resource->dataWidth = HCS_PROP(node, dataWidth); \
432             resource->stopBit = HCS_PROP(node, stopBit); \
433             resource->parity = HCS_PROP(node, parity); \
434             resource->transDir = HCS_PROP(node, transDir); \
435             resource->flowCtrl = HCS_PROP(node, flowCtrl); \
436             resource->overSimpling = HCS_PROP(node, overSimpling); \
437             resource->transMode = HCS_PROP(node, transMode); \
438             resource->uartType = HCS_PROP(node, uartType); \
439             resource->dePin = HCS_PROP(node, uartDePin); \
440             resource->deGroup = HCS_PROP(node, uartDeGroup); \
441             result = HDF_SUCCESS; \
442         } \
443     } while (0)
444 #define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform)
445 #define PLATFORM_UART_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), uart_config)
GetUartDeviceResource(UartDevice * device,const char * deviceMatchAttr)446 static uint32_t GetUartDeviceResource(UartDevice *device, const char *deviceMatchAttr)
447 {
448     UartResource *resource = NULL;
449     int32_t result = HDF_FAILURE;
450     if (device == NULL || deviceMatchAttr == NULL) {
451         HDF_LOGE("device or deviceMatchAttr is NULL\r\n");
452         return HDF_ERR_INVALID_PARAM;
453     }
454     resource = &device->resource;
455 #if HCS_NODE_HAS_PROP(PLATFORM_CONFIG, uart_config)
456     HCS_FOREACH_CHILD_VARGS(PLATFORM_UART_CONFIG, UART_FIND_CONFIG, deviceMatchAttr, resource);
457 #endif
458     if (result != HDF_SUCCESS) {
459         HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr);
460         return result;
461     }
462     device->uartId = resource->num;
463     g_uartCtx[device->uartId - 1].dePin = resource->dePin;
464     g_uartCtx[device->uartId - 1].deGroup = resource->deGroup;
465     g_uartCtx[device->uartId - 1].uartType = resource->uartType;
466     device->initFlag = false;
467     device->initTypedef.BaudRate = resource->baudRate;
468     device->initTypedef.DataWidth = g_dataWidthMap[resource->dataWidth];
469     device->initTypedef.HardwareFlowControl = g_flowControlMap[resource->flowCtrl];
470     device->initTypedef.StopBits = g_stopBitMap[resource->stopBit];
471     device->initTypedef.OverSampling = g_overSimplingMap[resource->overSimpling];
472     device->initTypedef.TransferDirection = g_transDirMap[resource->transDir];
473     device->initTypedef.Parity = g_parityMap[resource->parity];
474     return HDF_SUCCESS;
475 }
476 #else
GetUartDeviceResource(UartDevice * device,const struct DeviceResourceNode * resourceNode)477 static int32_t GetUartDeviceResource(UartDevice *device, const struct DeviceResourceNode *resourceNode)
478 {
479     struct DeviceResourceIface *dri = NULL;
480     UartResource *resource = NULL;
481     if (device == NULL || resourceNode == NULL) {
482         HDF_LOGE("%s: INVALID PARAM", __func__);
483         return HDF_ERR_INVALID_PARAM;
484     }
485     resource = &device->resource;
486     if (resource == NULL) {
487         HDF_LOGE("%s: INVALID OBJECT", __func__);
488         return HDF_ERR_INVALID_OBJECT;
489     }
490 
491     dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
492     if (dri == NULL || dri->GetUint32 == NULL || dri->GetUint8 == NULL) {
493         HDF_LOGE("DeviceResourceIface is invalid");
494         return HDF_ERR_INVALID_PARAM;
495     }
496 
497     if (GetUartHcs(dri, resourceNode, resource) != HDF_SUCCESS) {
498         HDF_LOGE("Get uart hcs failed\n");
499         return HDF_ERR_INVALID_PARAM;
500     }
501 
502     device->uartId = resource->num;
503     g_uartCtx[device->uartId - 1].dePin = resource->dePin;
504     g_uartCtx[device->uartId - 1].deGroup = resource->deGroup;
505     g_uartCtx[device->uartId - 1].uartType = resource->uartType;
506     device->initFlag = false;
507     device->initTypedef.BaudRate = resource->baudRate;
508     device->initTypedef.DataWidth = g_dataWidthMap[resource->dataWidth];
509     device->initTypedef.HardwareFlowControl = g_flowControlMap[resource->flowCtrl];
510     device->initTypedef.StopBits = g_stopBitMap[resource->stopBit];
511     device->initTypedef.OverSampling = g_overSimplingMap[resource->overSimpling];
512     device->initTypedef.TransferDirection = g_transDirMap[resource->transDir];
513     device->initTypedef.Parity = g_parityMap[resource->parity];
514 
515     return HDF_SUCCESS;
516 }
517 #endif
518 
AttachUartDevice(struct UartHost * uartHost,struct HdfDeviceObject * device)519 static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device)
520 {
521     int32_t ret;
522     UartDevice *uartDevice = NULL;
523 
524 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
525     if (device == NULL || uartHost == NULL) {
526 #else
527     if (uartHost == NULL || device == NULL || device->property == NULL) {
528 #endif
529         HDF_LOGE("%s: property is NULL", __func__);
530         return HDF_ERR_INVALID_PARAM;
531     }
532 
533     uartDevice = (UartDevice *)OsalMemAlloc(sizeof(UartDevice));
534     if (uartDevice == NULL) {
535         HDF_LOGE("%s: OsalMemCalloc uartDevice error", __func__);
536         return HDF_ERR_MALLOC_FAIL;
537     }
538 #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO
539     ret = GetUartDeviceResource(uartDevice, device->deviceMatchAttr);
540 #else
541     ret = GetUartDeviceResource(uartDevice, device->property);
542 #endif
543     if (ret != HDF_SUCCESS) {
544         (void)OsalMemFree(uartDevice);
545         return HDF_FAILURE;
546     }
547 
548     uartHost->priv = uartDevice;
549 
550     return HDF_SUCCESS;
551 }
552 
553 static int32_t UartDriverBind(struct HdfDeviceObject *device)
554 {
555     if (device == NULL) {
556         HDF_LOGE("%s: invalid parameter", __func__);
557         return HDF_ERR_INVALID_PARAM;
558     }
559 
560     return (UartHostCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS;
561 }
562 
563 static void UartDriverRelease(struct HdfDeviceObject *device)
564 {
565     HDF_LOGI("Enter %s:", __func__);
566     uint32_t uartId;
567     struct UartHost *host = NULL;
568     UartDevice *uartDevice = NULL;
569 
570     if (device == NULL) {
571         HDF_LOGE("%s: device is NULL", __func__);
572         return;
573     }
574 
575     host = UartHostFromDevice(device);
576     if (host == NULL || host->priv == NULL) {
577         HDF_LOGE("%s: host is NULL", __func__);
578         return;
579     }
580 
581     uartDevice = (UartDevice *)host->priv;
582     uartId = uartDevice->uartId;
583     host->method = NULL;
584 
585     OsalMemFree(uartDevice);
586 }
587 
588 static int32_t UartDriverInit(struct HdfDeviceObject *device)
589 {
590     HDF_LOGI("Enter %s:", __func__);
591     int32_t ret;
592     struct UartHost *host = NULL;
593 
594     if (device == NULL) {
595         HDF_LOGE("%s: device is NULL", __func__);
596         return HDF_ERR_INVALID_OBJECT;
597     }
598 
599     host = UartHostFromDevice(device);
600     if (host == NULL) {
601         HDF_LOGE("%s: host is NULL", __func__);
602         return HDF_ERR_INVALID_OBJECT;
603     }
604 
605     ret = AttachUartDevice(host, device);
606     if (ret != HDF_SUCCESS) {
607         HDF_LOGE("%s: attach error", __func__);
608         return HDF_FAILURE;
609     }
610 
611     host->method = &g_uartHostMethod;
612 
613     return ret;
614 }
615 
616 /* UartHostMethod implementations */
617 static int32_t UartHostDevInit(struct UartHost *host)
618 {
619     HDF_LOGI("%s: Enter\r\n", __func__);
620     if (host == NULL) {
621         HDF_LOGE("%s: invalid parameter", __func__);
622         return HDF_ERR_INVALID_PARAM;
623     }
624 
625     return InitUartDevice(host);
626 }
627 
628 static int32_t UartHostDevDeinit(struct UartHost *host)
629 {
630     HDF_LOGI("%s: Enter", __func__);
631     uint32_t uartId;
632     UartDevice *uartDevice = NULL;
633 
634     if (host == NULL || host->priv == NULL) {
635         HDF_LOGE("%s: invalid parameter", __func__);
636         return HDF_ERR_INVALID_PARAM;
637     }
638 
639     uartDevice = (UartDevice *)host->priv;
640     uartId = uartDevice->uartId;
641     uartDevice->initFlag = false;
642     UART_IRQ_DEINIT(g_usartRegMap[uartDevice->uartId - 1], g_exitIrqnMap[uartId -1]);
643     LL_USART_Disable(g_usartRegMap[uartDevice->uartId - 1]);
644 
645     return LL_USART_DeInit(g_usartRegMap[uartId - 1]);
646 }
647 
648 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size)
649 {
650     UartDevice *device = NULL;
651     uint32_t portId;
652 
653     if (host == NULL || data == NULL || size == 0 || host->priv == NULL) {
654         HDF_LOGE("%s: invalid parameter", __func__);
655         return HDF_ERR_INVALID_PARAM;
656     }
657 
658     device = (UartDevice*)host->priv;
659     portId = device->uartId;
660     USART_TypeDef* usartx = g_usartRegMap[portId - 1];
661     if (g_uartCtx[portId - 1].uartType == USART_485) {
662         USARTTxMode(g_uartCtx[portId - 1].dePin, g_uartCtx[portId - 1].deGroup);
663     }
664     if (g_uartCtx[portId - 1].txDMA) {
665         HDF_LOGE("unsupport dma now\n");
666         return HDF_ERR_INVALID_PARAM;
667     } else {
668         USART_TxData(usartx, data, size);
669     }
670     if (g_uartCtx[portId - 1].uartType == USART_485) {
671         USARTRxMode(g_uartCtx[portId - 1].dePin, g_uartCtx[portId - 1].deGroup);
672     }
673 
674     return HDF_SUCCESS;
675 }
676 
677 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size)
678 {
679     int32_t ret;
680     uint32_t uartId;
681     UartDevice *uartDevice = NULL;
682 
683     if (host == NULL || data == NULL || host->priv == NULL) {
684         HDF_LOGE("%s: invalid parameter", __func__);
685         return HDF_ERR_INVALID_PARAM;
686     }
687 
688     uartDevice = (UartDevice *)host->priv;
689     uartId = uartDevice->uartId;
690     USART_TypeDef* usartx = g_usartRegMap[uartId - 1];
691     if (g_uartCtx[uartId - 1].uartType == USART_485) {
692         USARTRxMode(g_uartCtx[uartId - 1].dePin, g_uartCtx[uartId - 1].deGroup);
693     }
694 
695     if (g_uartCtx[uartId - 1].rxDMA) {
696         HDF_LOGE("unsupport dma now\n");
697         return HDF_ERR_INVALID_PARAM;
698     } else {
699         ret = USART_RxData(uartId, data, size, g_uartCtx[uartId - 1].isBlock);
700     }
701 
702     return ret;
703 }
704 
705 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate)
706 {
707     HDF_LOGI("%s: Enter", __func__);
708     UartDevice *uartDevice = NULL;
709     LL_USART_InitTypeDef *uartInit = NULL;
710     uint32_t uartId;
711 
712     if (host == NULL || host->priv == NULL) {
713         HDF_LOGE("%s: invalid parameter", __func__);
714         return HDF_ERR_INVALID_PARAM;
715     }
716 
717     uartDevice = (UartDevice *)host->priv;
718     uartId = uartDevice->uartId;
719     uartInit = &uartDevice->initTypedef;
720     if (uartInit == NULL) {
721         HDF_LOGE("%s: device config is NULL", __func__);
722         return HDF_ERR_INVALID_OBJECT;
723     }
724 
725     uartInit->BaudRate = baudRate;
726     UartHostDevDeinit(host);
727     UartHostDevInit(host);
728 
729     return HDF_SUCCESS;
730 }
731 
732 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate)
733 {
734     HDF_LOGI("%s: Enter", __func__);
735     UartDevice *uartDevice = NULL;
736     LL_USART_InitTypeDef *uartInit = NULL;
737     uint32_t uartId;
738 
739     if (host == NULL || baudRate == NULL || host->priv == NULL) {
740         HDF_LOGE("%s: invalid parameter", __func__);
741         return HDF_ERR_INVALID_PARAM;
742     }
743     uartDevice = (UartDevice *)host->priv;
744     uartId = uartDevice->uartId;
745     uartInit = &uartDevice->initTypedef;
746     if (uartInit == NULL) {
747         HDF_LOGE("%s: device is NULL", __func__);
748         return HDF_ERR_INVALID_OBJECT;
749     }
750     *baudRate = uartInit->BaudRate;
751 
752     return HDF_SUCCESS;
753 }
754 static void TransDataWidth(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type)
755 {
756     if (uartInit == NULL || attribute == NULL) {
757         HDF_LOGE("null ptr\r\n");
758         return;
759     }
760 
761     if (type == UART_ATTR_NIOBE_TO_HDF) {
762         switch (uartInit->DataWidth) { // only support 9bit and 8 bit in niobe407
763             case LL_USART_DATAWIDTH_8B:
764                 attribute->dataBits = UART_ATTR_DATABIT_8;
765                 break;
766             default:
767                 attribute->dataBits = UART_ATTR_DATABIT_8;
768                 break;
769         }
770     } else {
771         switch (attribute->dataBits) {
772             case UART_ATTR_DATABIT_8:
773                 uartInit->DataWidth = LL_USART_DATAWIDTH_8B;
774                 break;
775             default:
776                 uartInit->DataWidth = LL_USART_DATAWIDTH_8B;
777                 break;
778         }
779     }
780 
781     return;
782 }
783 
784 static void TransParity(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type)
785 {
786     if (uartInit == NULL || attribute == NULL) {
787         HDF_LOGE("null ptr\r\n");
788         return;
789     }
790 
791     if (type == UART_ATTR_NIOBE_TO_HDF) {
792         switch (uartInit->Parity) {
793             case LL_USART_PARITY_NONE:
794                 attribute->parity = UART_ATTR_PARITY_NONE;
795                 break;
796             case LL_USART_PARITY_EVEN:
797                 attribute->parity = UART_ATTR_PARITY_EVEN;
798                 break;
799             case LL_USART_PARITY_ODD:
800                 attribute->parity = UART_ATTR_PARITY_ODD;
801                 break;
802             default:
803                 attribute->parity = UART_ATTR_PARITY_NONE;
804                 break;
805         }
806     } else {
807         switch (attribute->parity) {
808             case UART_ATTR_PARITY_NONE:
809                 uartInit->Parity = LL_USART_PARITY_NONE;
810                 break;
811             case UART_ATTR_PARITY_EVEN:
812                 uartInit->Parity = LL_USART_PARITY_EVEN;
813                 break;
814             case UART_ATTR_PARITY_ODD:
815                 uartInit->Parity = LL_USART_PARITY_ODD;
816                 break;
817             default:
818                 uartInit->Parity = LL_USART_PARITY_NONE;
819                 break;
820         }
821     }
822 
823     return;
824 }
825 
826 static void TransStopbit(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type)
827 {
828     if (uartInit == NULL || attribute == NULL) {
829         HDF_LOGE("null ptr\r\n");
830         return;
831     }
832 
833     if (type == UART_ATTR_NIOBE_TO_HDF) {
834         switch (uartInit->StopBits) {
835             case LL_USART_STOPBITS_1:
836                 attribute->stopBits = UART_ATTR_STOPBIT_1;
837                 break;
838             case LL_USART_STOPBITS_1_5:
839                 attribute->stopBits = UART_ATTR_STOPBIT_1P5;
840                 break;
841             case LL_USART_STOPBITS_2:
842                 attribute->stopBits = UART_ATTR_STOPBIT_2;
843                 break;
844             default:
845                 attribute->stopBits = UART_ATTR_PARITY_NONE;
846                 break;
847         }
848     } else {
849         switch (attribute->stopBits) {
850             case UART_ATTR_STOPBIT_1:
851                 uartInit->StopBits = LL_USART_STOPBITS_1;
852                 break;
853             case UART_ATTR_STOPBIT_1P5:
854                 uartInit->StopBits = LL_USART_STOPBITS_1_5;
855                 break;
856             case UART_ATTR_STOPBIT_2:
857                 uartInit->StopBits = LL_USART_STOPBITS_2;
858                 break;
859             default:
860                 uartInit->StopBits = LL_USART_STOPBITS_1;
861                 break;
862         }
863     }
864 
865     return;
866 }
867 
868 static void TransFlowCtrl(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type)
869 {
870     if (uartInit == NULL || attribute == NULL) {
871         HDF_LOGE("null ptr\r\n");
872         return;
873     }
874 
875     if (type == UART_ATTR_NIOBE_TO_HDF) {
876         switch (uartInit->HardwareFlowControl) {
877             case LL_USART_HWCONTROL_NONE:
878                 attribute->rts = 0;
879                 attribute->cts = 0;
880                 break;
881             case LL_USART_HWCONTROL_CTS:
882                 attribute->rts = 0;
883                 attribute->cts = 1;
884                 break;
885             case LL_USART_HWCONTROL_RTS:
886                 attribute->rts = 1;
887                 attribute->cts = 0;
888                 break;
889             case LL_USART_HWCONTROL_RTS_CTS:
890                 attribute->rts = 1;
891                 attribute->cts = 1;
892                 break;
893             default:
894                 attribute->rts = 0;
895                 attribute->cts = 0;
896                 break;
897         }
898     } else {
899         if (attribute->rts && attribute->cts) {
900             uartInit->HardwareFlowControl = LL_USART_HWCONTROL_RTS_CTS;
901         } else if (attribute->rts && !attribute->cts) {
902             uartInit->HardwareFlowControl = LL_USART_HWCONTROL_RTS;
903         } else if (!attribute->rts && attribute->cts) {
904             uartInit->HardwareFlowControl = LL_USART_HWCONTROL_CTS;
905         } else {
906             uartInit->HardwareFlowControl = LL_USART_HWCONTROL_NONE;
907         }
908     }
909 
910     return;
911 }
912 
913 static void TransDir(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type)
914 {
915     if (uartInit == NULL || attribute == NULL) {
916         HDF_LOGE("null ptr\r\n");
917         return;
918     }
919     if (type == UART_ATTR_NIOBE_TO_HDF) {
920         switch (uartInit->TransferDirection) {
921             case LL_USART_DIRECTION_NONE:
922                 attribute->fifoRxEn = 0;
923                 attribute->fifoTxEn = 0;
924                 break;
925             case LL_USART_DIRECTION_RX:
926                 attribute->fifoRxEn = 1;
927                 attribute->fifoTxEn = 0;
928                 break;
929             case LL_USART_DIRECTION_TX:
930                 attribute->fifoRxEn = 0;
931                 attribute->fifoTxEn = 1;
932                 break;
933             case LL_USART_DIRECTION_TX_RX:
934                 attribute->fifoRxEn = 1;
935                 attribute->fifoTxEn = 1;
936                 break;
937             default:
938                 attribute->fifoRxEn = 1;
939                 attribute->fifoTxEn = 1;
940                 break;
941         }
942     } else {
943         if (attribute->fifoRxEn && attribute->fifoTxEn) {
944             uartInit->TransferDirection = LL_USART_DIRECTION_TX_RX;
945         } else if (attribute->fifoRxEn && !attribute->fifoTxEn) {
946             uartInit->TransferDirection = LL_USART_DIRECTION_RX;
947         } else if (!attribute->fifoRxEn && attribute->fifoTxEn) {
948             uartInit->TransferDirection = LL_USART_DIRECTION_TX;
949         } else {
950             uartInit->TransferDirection = LL_USART_DIRECTION_NONE;
951         }
952     }
953 
954     return;
955 }
956 
957 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute)
958 {
959     HDF_LOGI("%s: Enter", __func__);
960     UartDevice *uartDevice = NULL;
961     LL_USART_InitTypeDef *uartInit = NULL;
962     uint32_t uartId;
963 
964     if (host == NULL || attribute == NULL || host->priv == NULL) {
965         HDF_LOGE("%s: invalid parameter", __func__);
966         return HDF_ERR_INVALID_PARAM;
967     }
968 
969     uartDevice = (UartDevice *)host->priv;
970     uartId = uartDevice->uartId;
971     uartInit = &uartDevice->initTypedef;
972     if (uartInit == NULL) {
973         HDF_LOGE("%s: config is NULL", __func__);
974         return HDF_ERR_INVALID_OBJECT;
975     }
976 
977     TransDataWidth(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE);
978     TransParity(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE);
979     TransStopbit(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE);
980     TransFlowCtrl(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE);
981     TransDir(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE);
982 
983     UartHostDevDeinit(host);
984     UartHostDevInit(host);
985 
986     return HDF_SUCCESS;
987 }
988 
989 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute)
990 {
991     HDF_LOGI("%s: Enter", __func__);
992     UartDevice *uartDevice = NULL;
993     LL_USART_InitTypeDef *uartInit = NULL;
994 
995     if (host == NULL || attribute == NULL || host->priv == NULL) {
996         HDF_LOGE("%s: invalid parameter", __func__);
997         return HDF_ERR_INVALID_PARAM;
998     }
999 
1000     uartDevice = (UartDevice *)host->priv;
1001     uartInit = &uartDevice->initTypedef;
1002     if (uartInit == NULL) {
1003         HDF_LOGE("%s: uartInit is NULL", __func__);
1004         return HDF_ERR_INVALID_OBJECT;
1005     }
1006 
1007     TransDataWidth(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF);
1008     TransParity(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF);
1009     TransStopbit(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF);
1010     TransFlowCtrl(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF);
1011     TransDir(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF);
1012 
1013     return HDF_SUCCESS;
1014 }
1015 
1016 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode)
1017 {
1018     HDF_LOGI("%s: Enter", __func__);
1019     UartDevice *uartDevice = NULL;
1020     uint32_t uartId;
1021 
1022     if (host == NULL || host->priv == NULL) {
1023         HDF_LOGE("%s: invalid parameter", __func__);
1024         return HDF_ERR_INVALID_PARAM;
1025     }
1026 
1027     uartDevice = (UartDevice *)host->priv;
1028     uartId = uartDevice->uartId;
1029 
1030     switch (mode) {
1031         case UART_MODE_RD_BLOCK:
1032             g_uartCtx[uartId - 1].isBlock = true;
1033             break;
1034         case UART_MODE_RD_NONBLOCK:
1035             g_uartCtx[uartId - 1].isBlock = false;
1036             break;
1037         case UART_MODE_DMA_RX_EN:
1038             g_uartCtx[uartId - 1].rxDMA = true;
1039             break;
1040         case UART_MODE_DMA_RX_DIS:
1041             g_uartCtx[uartId - 1].rxDMA = false;
1042             break;
1043         case UART_MODE_DMA_TX_EN:
1044             g_uartCtx[uartId - 1].txDMA = true;
1045             break;
1046         case UART_MODE_DMA_TX_DIS:
1047             g_uartCtx[uartId - 1].txDMA = false;
1048             break;
1049         default:
1050             HDF_LOGE("%s: UartTransMode(%d) invalid", __func__, mode);
1051             break;
1052     }
1053 
1054     return HDF_SUCCESS;
1055 }