1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * Description: Provides iot_uart driver source \n
16 *
17 * History: \n
18 * 2023-10-18, Create file. \n
19 */
20 #include "securec.h"
21 #include "iot_errno.h"
22 #include "uart.h"
23 #include "iot_uart.h"
24 #if defined(CONFIG_UART_SUPPORT_DMA)
25 #include "dma.h"
26 #endif
27
28 #define IOT_UART_RX_BUFFER_SIZE 1024
29 #define IOT_UART_SET_WR_TIMEOUT 0
30 #define IOT_UART_MIN_DATABITS 5
31 #define IOT_UART_MAX_DATABITS 8
32 #define IOT_UART_LIMIT_STOPBITS 6
33 #define IOT_UART_MIN_STOPBITS 1
34 #define IOT_UART_MAX_STOPBITS 2
35
36 #if defined(CONFIG_UART_SUPPORT_DMA)
37 #define UART_DMA_WIDTH 0
38 #define UART_DMA_BURST_LENGTH 2
39 #define UART_DMA_PRIORITY 0
40 #define UART_DMA_TX_ENABLE 1
41 #define UART_DMA_RX_ENABLE 1
42 #endif
43
44 uint8_t g_iot_uart_rx_buff[IOT_UART_RX_BUFFER_SIZE] = { 0 };
45
IoTUartInit(unsigned int id,const IotUartAttribute * param)46 unsigned int IoTUartInit(unsigned int id, const IotUartAttribute *param)
47 {
48 if (param == NULL || param->dataBits < IOT_UART_MIN_DATABITS || param->dataBits > IOT_UART_MAX_DATABITS ||
49 param->stopBits < IOT_UART_MIN_STOPBITS || param->stopBits > IOT_UART_MAX_STOPBITS) {
50 return IOT_FAILURE;
51 }
52 uint8_t databits = 0;
53 uint8_t stopbits = 0;
54 uart_pin_config_t pin_config = {
55 .tx_pin = S_MGPIO0,
56 .rx_pin = S_MGPIO1,
57 .cts_pin = PIN_NONE,
58 .rts_pin = PIN_NONE
59 };
60
61 if (param->dataBits >= IOT_UART_LIMIT_STOPBITS) {
62 databits = param->dataBits - IOT_UART_MIN_DATABITS;
63 }
64
65 if (param->stopBits == IOT_UART_MAX_STOPBITS) {
66 stopbits = 1;
67 }
68
69 uart_attr_t attr = {
70 .baud_rate = param->baudRate,
71 .data_bits = databits,
72 .stop_bits = stopbits,
73 .parity = param->parity
74 };
75 uart_buffer_config_t uart_buffer_config = {
76 .rx_buffer = g_iot_uart_rx_buff,
77 .rx_buffer_size = IOT_UART_RX_BUFFER_SIZE
78 };
79
80 #if defined(CONFIG_UART_SUPPORT_DMA)
81 uart_extra_attr_t extra_attr = {
82 .tx_dma_enable = UART_DMA_TX_ENABLE,
83 .tx_int_threshold = 0,
84 .rx_dma_enable = UART_DMA_RX_ENABLE,
85 .rx_int_threshold = 0
86 };
87 uapi_dma_init();
88 uapi_dma_open();
89 if (uapi_uart_init((uart_bus_t)id, &pin_config, &attr, &extra_attr, &uart_buffer_config) != ERRCODE_SUCC) {
90 return IOT_FAILURE;
91 }
92 #else
93 if (uapi_uart_init((uart_bus_t)id, &pin_config, &attr, NULL, &uart_buffer_config) != ERRCODE_SUCC) {
94 return IOT_FAILURE;
95 }
96 #endif
97 return IOT_SUCCESS;
98 }
99
IoTUartDeinit(unsigned int id)100 unsigned int IoTUartDeinit(unsigned int id)
101 {
102 if (uapi_uart_deinit((uart_bus_t)id) != ERRCODE_SUCC) {
103 return IOT_FAILURE;
104 }
105 return IOT_SUCCESS;
106 }
107
IoTUartWrite(unsigned int id,const unsigned char * data,unsigned int dataLen)108 int IoTUartWrite(unsigned int id, const unsigned char *data, unsigned int dataLen)
109 {
110 if (data == NULL) {
111 return IOT_FAILURE;
112 }
113 #if defined(CONFIG_UART_SUPPORT_DMA)
114 uart_write_dma_config_t dma_cfg = {
115 .src_width = UART_DMA_WIDTH,
116 .dest_width = UART_DMA_WIDTH,
117 .burst_length = UART_DMA_BURST_LENGTH,
118 .priority = UART_DMA_PRIORITY
119 };
120 if (uapi_uart_write_by_dma((uart_bus_t)id, (const uint8_t *)data, (uint32_t)dataLen,
121 &dma_cfg) != (int32_t)dataLen) {
122 return IOT_FAILURE;
123 }
124 #else
125 if (uapi_uart_write((uart_bus_t)id, (const uint8_t *)data, (uint32_t)dataLen,
126 IOT_UART_SET_WR_TIMEOUT) != (int32_t)dataLen) {
127 return IOT_FAILURE;
128 }
129 #endif
130 return (int32_t)dataLen;
131 }
132
IoTUartRead(unsigned int id,unsigned char * data,unsigned int dataLen)133 int IoTUartRead(unsigned int id, unsigned char *data, unsigned int dataLen)
134 {
135 if (data == NULL) {
136 return IOT_FAILURE;
137 }
138 #if defined(CONFIG_UART_SUPPORT_DMA)
139 uart_write_dma_config_t dma_cfg = {
140 .src_width = UART_DMA_WIDTH,
141 .dest_width = UART_DMA_WIDTH,
142 .burst_length = UART_DMA_BURST_LENGTH,
143 .priority = UART_DMA_PRIORITY
144 };
145 if (uapi_uart_read_by_dma((uart_bus_t)id, g_iot_uart_rx_buff, dataLen, &dma_cfg) != (int32_t)dataLen) {
146 return IOT_FAILURE;
147 }
148 #else
149 if (uapi_uart_read((uart_bus_t)id, g_iot_uart_rx_buff, dataLen, IOT_UART_SET_WR_TIMEOUT) != (int32_t)dataLen) {
150 return IOT_FAILURE;
151 }
152 #endif
153 if (memcpy_s(data, dataLen, g_iot_uart_rx_buff, dataLen) != EOK) {
154 return IOT_FAILURE;
155 }
156 return (int32_t)dataLen;
157 }
158
IoTUartSetFlowCtrl(unsigned int id,IotFlowCtrl flowCtrl)159 unsigned int IoTUartSetFlowCtrl(unsigned int id, IotFlowCtrl flowCtrl)
160 {
161 unused(id);
162 unused(flowCtrl);
163 return IOT_SUCCESS;
164 }
165