• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdlib.h>
17 #include <string.h>
18 #include "esp_err.h"
19 #include "uart.h"
20 #include "gpio_types.h"
21 #include "uart_types.h"
22 #include "uart_if.h"
23 #include "uart_core.h"
24 #include "osal_sem.h"
25 #include "hcs_macro.h"
26 #include "hdf_config_macro.h"
27 #include "hdf_log.h"
28 
29 #ifdef GPIO_NUM_MAX
30 #undef GPIO_NUM_MAX
31 #endif
32 
33 #define HDF_UART_TMO 1000
34 #define HDF_LOG_TAG uartDev
35 #define GPIO_MAX_LENGTH 32
36 #define UART_FIFO_MAX_BUFFER 2048
37 #define UART_DMA_RING_BUFFER_SIZE 256 // mast be 2^n
38 #define MAX_DEV_NAME_SIZE 32
39 
40 #define UART_PIN_NUNS 4 // 串口引脚配置个数
41 #define MAX_UART_NUMS 3 // 最大串口个数
42 #define UART_RX_BUFF_SIZE 512
43 #define UART_TX_BUFF_SIZE 512
44 #define OH_ESP_DATABITS_EX(data_bits) (3 - (data_bits))
45 #define OH2ESP_STOPBITS(oh_stop_bits) (1 + (oh_stop_bits))
46 #define ESP2OH_STOPBITS(esp_stop_bits) ((esp_stop_bits)-1)
47 
48 #define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform)
49 #define PLATFORM_UART_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), uart_config)
50 #define HDF_GPIO_FIND_SOURCE(node, uartHost, device)                                                 \
51     do {                                                                                             \
52         if (strcmp(HCS_PROP(node, match_attr), (device)->deviceMatchAttr) == 0) {                    \
53             int cur_uart_gpio_pin[] = HCS_ARRAYS(HCS_PROP(node, uart_gpio_pin));                     \
54             struct UartAttrSourceStr cur_uart_attr = HCS_ARRAYS(HCS_PROP(node, uart_attr));          \
55             UartAttr *uart_config = (UartAttr *)OsalMemAlloc(sizeof(UartAttr));                      \
56             if (uart_config == NULL) {                                                               \
57                 HDF_LOGE("%s: OsalMemCalloc uart_config error", __func__);                           \
58                 return HDF_ERR_MALLOC_FAIL;                                                          \
59             }                                                                                        \
60             uart_config->uart_port = HCS_PROP(node, uart_port);                                      \
61             uart_config->baudrate = HCS_PROP(node, baudrate);                                        \
62             uart_config->uart_pin[0] = cur_uart_gpio_pin[0];                                         \
63             uart_config->uart_pin[1] = cur_uart_gpio_pin[1];                                         \
64             uart_config->uart_pin[2] = cur_uart_gpio_pin[2];                                         \
65             uart_config->uart_pin[3] = cur_uart_gpio_pin[3];                                         \
66             uart_config->data_bits = GetWordLengthFromStr(cur_uart_attr.data_bits);                  \
67             uart_config->parity = GetParityFromStr(cur_uart_attr.parity);                            \
68             uart_config->stop_bits = GetStopBitsFromStr(cur_uart_attr.stop_bits);                    \
69             uart_config->hw_flowcontrol = GetHwFlowControlFromStr(cur_uart_attr.hw_flowcontrol);     \
70             HDF_LOGE("----- UART%d Config -----", uart_config->uart_port);                           \
71             HDF_LOGE("baudrate = %d", uart_config->baudrate);                                        \
72             HDF_LOGE("Uart_Pin = [%d,%d,%d,%d]", uart_config->uart_pin[0], uart_config->uart_pin[1], \
73                      uart_config->uart_pin[2], uart_config->uart_pin[3]);                            \
74             HDF_LOGE("data_bits = %s[%d]", cur_uart_attr.data_bits, uart_config->data_bits);         \
75             HDF_LOGE("parity = %s[%d]", cur_uart_attr.parity, uart_config->parity);                  \
76             HDF_LOGE("stop_bits = %s[%d]", cur_uart_attr.stop_bits, uart_config->stop_bits);         \
77             HDF_LOGE("hw_flowcontrol = %s[%d]", cur_uart_attr.hw_flowcontrol,                        \
78                      uart_config->hw_flowcontrol);                                                   \
79             uart_config->block_time = 0;                                                             \
80             (uartHost)->priv = uart_config;                                                          \
81             (uartHost)->num = uart_config->uart_port;                                                \
82         }                                                                                            \
83     } while (0)
84 
85 static int32_t UartDriverBind(struct HdfDeviceObject *device);
86 static int32_t UartDriverInit(struct HdfDeviceObject *device);
87 static void UartDriverRelease(struct HdfDeviceObject *device);
88 
89 struct HdfDriverEntry g_UartDriverEntry = {
90     .moduleVersion = 1,
91     .moduleName = "ESP32U4_HDF_UART",
92     .Bind = UartDriverBind,
93     .Init = UartDriverInit,
94     .Release = UartDriverRelease,
95 };
96 
97 HDF_INIT(g_UartDriverEntry);
98 
99 static int32_t UartHostDevInit(struct UartHost *host);
100 static int32_t UartHostDevDeinit(struct UartHost *host);
101 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size);
102 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate);
103 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate);
104 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size);
105 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute);
106 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute);
107 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode);
108 
109 struct UartHostMethod g_uartHostMethod = {
110     .Init = UartHostDevInit,
111     .Deinit = UartHostDevDeinit,
112     .Read = UartHostDevRead,
113     .Write = UartHostDevWrite,
114     .SetBaud = UartHostDevSetBaud,
115     .GetBaud = UartHostDevGetBaud,
116     .SetAttribute = UartHostDevSetAttribute,
117     .GetAttribute = UartHostDevGetAttribute,
118     .SetTransMode = UartHostDevSetTransMode,
119 };
120 
121 struct UartAttrSourceStr {
122     char *data_bits;
123     char *parity;
124     char *stop_bits;
125     char *hw_flowcontrol;
126 };
127 
128 typedef struct _UartAttr {
129     int uart_port;
130     int baudrate;
131     int uart_pin[UART_PIN_NUNS];
132     int data_bits;
133     int parity;
134     int stop_bits;
135     int hw_flowcontrol;
136     unsigned int block_time;
137 } UartAttr;
138 
GetWordLengthFromStr(const char * str)139 static int GetWordLengthFromStr(const char *str)
140 {
141     if (!strcmp(str, "UART_DATA_5_BITS")) {
142         return UART_DATA_5_BITS;
143     } else if (!strcmp(str, "UART_DATA_6_BITS")) {
144         return UART_DATA_6_BITS;
145     } else if (!strcmp(str, "UART_DATA_7_BITS")) {
146         return UART_DATA_7_BITS;
147     }
148     return UART_DATA_8_BITS;
149 }
150 
GetParityFromStr(const char * str)151 static int GetParityFromStr(const char *str)
152 {
153     if (!strcmp(str, "UART_PARITY_EVEN")) {
154         return UART_PARITY_EVEN;
155     } else if (!strcmp(str, "UART_PARITY_ODD")) {
156         return UART_PARITY_ODD;
157     }
158     return UART_PARITY_DISABLE;
159 }
160 
GetStopBitsFromStr(const char * str)161 static int GetStopBitsFromStr(const char *str)
162 {
163     if (!strcmp(str, "UART_STOP_BITS_1_5")) {
164         return UART_STOP_BITS_1_5;
165     } else if (!strcmp(str, "UART_STOP_BITS_2")) {
166         return UART_STOP_BITS_2;
167     }
168     return UART_STOP_BITS_1;
169 }
170 
GetHwFlowControlFromStr(const char * str)171 static int GetHwFlowControlFromStr(const char *str)
172 {
173     if (!strcmp(str, "UART_HW_FLOWCTRL_RTS")) {
174         return UART_HW_FLOWCTRL_RTS;
175     } else if (!strcmp(str, "UART_HW_FLOWCTRL_CTS")) {
176         return UART_HW_FLOWCTRL_CTS;
177     } else if (!strcmp(str, "UART_HW_FLOWCTRL_CTS_RTS")) {
178         return UART_HW_FLOWCTRL_CTS_RTS;
179     }
180     return UART_HW_FLOWCTRL_DISABLE;
181 }
182 
AttachUartDevice(struct UartHost * uartHost,struct HdfDeviceObject * device)183 static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device)
184 {
185     int32_t ret;
186     if (device == NULL || uartHost == NULL) {
187         HDF_LOGE("%s: uartHost or device is NULL", __func__);
188         return HDF_ERR_INVALID_PARAM;
189     }
190 
191     HCS_FOREACH_CHILD_VARGS(PLATFORM_UART_CONFIG, HDF_GPIO_FIND_SOURCE, uartHost, device);
192 
193     return HDF_SUCCESS;
194 }
195 
UartDriverBind(struct HdfDeviceObject * device)196 static int32_t UartDriverBind(struct HdfDeviceObject *device)
197 {
198     if (device == NULL) {
199         HDF_LOGE("%s: invalid parameter", __func__);
200         return HDF_ERR_INVALID_PARAM;
201     }
202     struct UartHost *devService = UartHostCreate(device);
203     if (devService == NULL) {
204         HDF_LOGE("%s: UartHostCreate fail!", __func__);
205         return HDF_FAILURE;
206     }
207     HDF_LOGI("%s: UartHostCreate success!", __func__);
208     return HDF_SUCCESS;
209 }
210 
UartDriverRelease(struct HdfDeviceObject * device)211 static void UartDriverRelease(struct HdfDeviceObject *device)
212 {
213     struct UartHost *host = NULL;
214     if (device == NULL) {
215         HDF_LOGE("%s: device is NULL!", __func__);
216         return;
217     }
218 
219     host = UartHostFromDevice(device);
220     if (host == NULL) {
221         HDF_LOGE("%s: host is null", __func__);
222         return;
223     }
224 
225     if (host->priv != NULL) {
226         OsalMemFree(host->priv);
227         host->priv = NULL;
228     }
229 }
230 
UartDriverInit(struct HdfDeviceObject * device)231 static int32_t UartDriverInit(struct HdfDeviceObject *device)
232 {
233     HDF_LOGI("Enter %s:", __func__);
234     int32_t ret;
235     struct UartHost *host = NULL;
236 
237     if (device == NULL) {
238         HDF_LOGE("%s: device is NULL", __func__);
239         return HDF_ERR_INVALID_OBJECT;
240     }
241 
242     host = UartHostFromDevice(device);
243     if (host == NULL) {
244         HDF_LOGE("%s: host is NULL", __func__);
245         return HDF_ERR_INVALID_OBJECT;
246     }
247 
248     ret = AttachUartDevice(host, device);
249     if (ret != HDF_SUCCESS) {
250         HDF_LOGE("%s: attach error", __func__);
251         return HDF_FAILURE;
252     }
253     host->method = &g_uartHostMethod;
254     return ret;
255 }
256 
257 #define DEFAULT_RX_FLOW_CTRL_THRESH 122
258 #define INDEX_2 2
259 #define INDEX_3 3
260 /* UartHostMethod implementations */
UartHostDevInit(struct UartHost * host)261 static int32_t UartHostDevInit(struct UartHost *host)
262 {
263     int ret;
264     if (host == NULL) {
265         HDF_LOGE("%s: invalid parameter", __func__);
266         return HDF_ERR_INVALID_PARAM;
267     }
268 
269     UartAttr *uart_config = (UartAttr *)host->priv;
270     if (uart_config == NULL) {
271         HDF_LOGE("%s: parse uart config fail", __func__);
272         return HDF_FAILURE;
273     }
274     uart_config_t uartConfig = {0};
275     uartConfig.baud_rate = uart_config->baudrate;
276     uartConfig.data_bits = uart_config->data_bits;
277     uartConfig.parity = uart_config->parity;
278     uartConfig.stop_bits = uart_config->stop_bits;
279     uartConfig.flow_ctrl = uart_config->hw_flowcontrol;
280     if (0x01 & uartConfig.flow_ctrl) {
281         uartConfig.rx_flow_ctrl_thresh = DEFAULT_RX_FLOW_CTRL_THRESH;
282     }
283     uart_param_config(uart_config->uart_port, &uartConfig);
284 
285     // 检查pin脚是否合法
286     for (int i = 0; i < UART_PIN_NUNS; i++) {
287         if (uart_config->uart_pin[i] >= GPIO_NUM_MAX || uart_config->uart_pin[i] < 0) {
288             uart_config->uart_pin[i] = -1;
289         }
290     }
291 
292     ret = uart_set_pin(uart_config->uart_port, uart_config->uart_pin[0], uart_config->uart_pin[1],
293                        uart_config->uart_pin[INDEX_2], uart_config->uart_pin[INDEX_3]);
294     if (ret != 0) {
295         HDF_LOGE("uart_set_pin failed ret=0x%X\n", ret);
296         return HDF_FAILURE;
297     }
298 
299     ret = uart_driver_install(uart_config->uart_port, UART_RX_BUFF_SIZE, UART_TX_BUFF_SIZE, 0, NULL, 0);
300     if (ret != 0) {
301         HDF_LOGE("uart_driver_install failed ret=0x%X\n", ret);
302         return HDF_FAILURE;
303     }
304 
305     return HDF_SUCCESS;
306 }
307 
UartHostDevDeinit(struct UartHost * host)308 static int32_t UartHostDevDeinit(struct UartHost *host)
309 {
310     int ret = HDF_FAILURE;
311     ret = uart_driver_delete(host->num);
312     if (ret != ESP_OK) {
313         HDF_LOGE("%s: uart%d_driver_delete failed\n", host->num, __func__);
314         return HDF_FAILURE;
315     }
316     return HDF_SUCCESS;
317 }
318 
UartHostDevWrite(struct UartHost * host,uint8_t * data,uint32_t size)319 static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size)
320 {
321     int ret = HDF_FAILURE;
322     ret = uart_write_bytes(host->num, (const uint8_t *)data, size);
323     if (ret < 0) {
324         HDF_LOGE("uart_write_bytes failed!");
325         return HDF_FAILURE;
326     }
327     return ret;
328 }
329 
UartHostDevRead(struct UartHost * host,uint8_t * data,uint32_t size)330 static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size)
331 {
332     int length = 0;
333     int rcv_bytes = 0;
334     if (host == NULL) {
335         HDF_LOGE("UartHost is NULL!");
336         return HDF_ERR_INVALID_PARAM;
337     }
338 
339     UartAttr *uart_config = (UartAttr *)host->priv;
340     if (uart_config == NULL) {
341         HDF_LOGE("%s: parse uart config fail", __func__);
342         return HDF_FAILURE;
343     }
344     int ret = uart_read_bytes(host->num, (uint8_t *)data, 1, uart_config->block_time);
345     if (ret < 0) {
346         HDF_LOGE("uart_read_bytes failed!");
347         return HDF_FAILURE;
348     }
349     rcv_bytes += ret;
350     uart_get_buffered_data_len(host->num, &length); // 获取buffer长度
351     if (length > 0) {
352         ret = uart_read_bytes(host->num, (uint8_t *)(&data[1]), size - 1, 0);
353         if (ret < 0) {
354             HDF_LOGE("uart_read_bytes failed!");
355             return HDF_FAILURE;
356         }
357         rcv_bytes += ret;
358     }
359     return rcv_bytes;
360 }
361 
UartHostDevSetBaud(struct UartHost * host,uint32_t baudRate)362 static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate)
363 {
364     if (host == NULL) {
365         HDF_LOGE("UartHost is NULL!");
366         return HDF_ERR_INVALID_PARAM;
367     }
368 
369     UartAttr *uart_config = (UartAttr *)host->priv;
370     if (uart_config == NULL) {
371         HDF_LOGE("%s: parse uart config fail", __func__);
372         return HDF_FAILURE;
373     }
374     uart_config->baudrate = baudRate;
375     return HDF_SUCCESS;
376 }
377 
UartHostDevGetBaud(struct UartHost * host,uint32_t * baudRate)378 static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate)
379 {
380     if (host == NULL) {
381         HDF_LOGE("UartHost is NULL!");
382         return HDF_ERR_INVALID_PARAM;
383     }
384     UartAttr *uart_config = (UartAttr *)host->priv;
385     if (uart_config == NULL) {
386         HDF_LOGE("%s: parse uart config fail", __func__);
387         return HDF_FAILURE;
388     }
389     *baudRate = uart_config->baudrate;
390     return HDF_SUCCESS;
391 }
392 
UartHostDevSetAttribute(struct UartHost * host,struct UartAttribute * attribute)393 static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute)
394 {
395     if (host == NULL) {
396         HDF_LOGE("UartHost is NULL!");
397         return HDF_ERR_INVALID_PARAM;
398     }
399     UartAttr *uart_config = (UartAttr *)host->priv;
400     if (uart_config == NULL) {
401         HDF_LOGE("%s: parse uart config fail", __func__);
402         return HDF_FAILURE;
403     }
404 
405     uart_config->data_bits = OH_ESP_DATABITS_EX(attribute->dataBits);
406     if (attribute->parity == UART_ATTR_PARITY_NONE) {
407         uart_config->parity = UART_PARITY_DISABLE;
408     } else if (attribute->parity == UART_ATTR_PARITY_EVEN) {
409         uart_config->parity = UART_PARITY_EVEN;
410     } else if (attribute->parity == UART_ATTR_PARITY_ODD) {
411         uart_config->parity = UART_PARITY_ODD;
412     } else {
413         HDF_LOGE("%s: Not support set uart attribute->parity = %d", __func__, attribute->parity);
414         return HDF_FAILURE;
415     }
416     uart_config->stop_bits = OH2ESP_STOPBITS(attribute->stopBits);
417     return HDF_SUCCESS;
418 }
419 
UartHostDevGetAttribute(struct UartHost * host,struct UartAttribute * attribute)420 static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute)
421 {
422     if (host == NULL) {
423         HDF_LOGE("UartHost is NULL!");
424         return HDF_ERR_INVALID_PARAM;
425     }
426     UartAttr *uart_config = (UartAttr *)host->priv;
427     if (uart_config == NULL) {
428         HDF_LOGE("%s: parse uart config fail", __func__);
429         return HDF_FAILURE;
430     }
431 
432     attribute->dataBits = OH_ESP_DATABITS_EX(uart_config->data_bits);
433 
434     if (uart_config->parity == UART_PARITY_EVEN) {
435         attribute->parity = UART_ATTR_PARITY_EVEN;
436     } else if (uart_config->parity == UART_PARITY_ODD) {
437         attribute->parity = UART_ATTR_PARITY_ODD;
438     } else {
439         attribute->parity = UART_ATTR_PARITY_NONE;
440     }
441     attribute->stopBits = ESP2OH_STOPBITS(uart_config->stop_bits);
442     return HDF_SUCCESS;
443 }
444 
UartHostDevSetTransMode(struct UartHost * host,enum UartTransMode mode)445 static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode)
446 {
447     UartAttr *uart_config = (UartAttr *)host->priv;
448     if (uart_config == NULL) {
449         HDF_LOGE("%s: parse uart config fail", __func__);
450         return HDF_FAILURE;
451     }
452     if (mode == UART_MODE_RD_BLOCK) {
453         uart_config->block_time = 0xFFFFFFFF;
454     } else if (mode == UART_MODE_RD_NONBLOCK) {
455         uart_config->block_time = 0;
456     } else {
457         HDF_LOGE("%s: Not support mode = %d", __func__, mode);
458     }
459     return HDF_SUCCESS;
460 }