1 // Copyright (C) 2022 Beken Corporation
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 #include "iot_errno.h"
15 #include "iot_uart.h"
16
17 #include <driver/uart.h>
18 #include <os/os.h>
19
20 //SOC_UART_ID_NUM_PER_UNIT
21 #define BK_UART_MAX 2
22
23 struct uart_ctrl {
24 unsigned int inited;
25 };
26
27 static struct uart_ctrl uart_ctrls[BK_UART_MAX] = {
28 [ 0 ... (BK_UART_MAX-1) ] = {0},
29 };
30
convert_uart_config(uart_config_t * dst,const IotUartAttribute * src)31 static int convert_uart_config(uart_config_t *dst, const IotUartAttribute *src)
32 {
33 dst->baud_rate = src->baudRate;
34
35 if (src->dataBits == IOT_UART_DATA_BIT_5)
36 dst->data_bits = UART_DATA_5_BITS;
37 else if (src->dataBits == IOT_UART_DATA_BIT_6)
38 dst->data_bits = UART_DATA_6_BITS;
39 else if (src->dataBits == IOT_UART_DATA_BIT_7)
40 dst->data_bits = UART_DATA_7_BITS;
41 else if (src->dataBits == IOT_UART_DATA_BIT_8)
42 dst->data_bits = UART_DATA_8_BITS;
43 else
44 return -1;
45
46 dst->parity = src->parity ;
47
48 if (src->stopBits == IOT_UART_STOP_BIT_1)
49 dst->stop_bits = UART_STOP_BITS_1;
50 else if (src->stopBits == IOT_UART_STOP_BIT_2)
51 dst->stop_bits = UART_STOP_BITS_2;
52 else
53 return -1;
54
55 dst->flow_ctrl = UART_FLOWCTRL_DISABLE;
56 dst->src_clk = UART_SCLK_XTAL_26M;
57
58 return 0;
59 }
60
IoTUartInit(unsigned int id,const IotUartAttribute * param)61 unsigned int IoTUartInit(unsigned int id, const IotUartAttribute *param)
62 {
63 unsigned int ret;
64 uart_config_t config;
65
66 if (id >= BK_UART_MAX)
67 return IOT_FAILURE;
68
69 if (uart_ctrls[id].inited)
70 return IOT_FAILURE;
71
72 ret = convert_uart_config(&config, param);
73 if (ret)
74 return IOT_FAILURE;
75
76 ret = bk_uart_init(id, &config);
77 if (ret)
78 return IOT_FAILURE;
79
80 uart_ctrls[id].inited = 1;
81 return IOT_SUCCESS;
82 }
83
IoTUartDeinit(unsigned int id)84 unsigned int IoTUartDeinit(unsigned int id)
85 {
86 if (id >= BK_UART_MAX)
87 return IOT_FAILURE;
88
89 bk_uart_deinit(id);
90 uart_ctrls[id].inited = 0;
91 return IOT_SUCCESS;
92 }
93
IoTUartRead(unsigned int id,unsigned char * data,unsigned int dataLen)94 int IoTUartRead(unsigned int id, unsigned char *data, unsigned int dataLen)
95 {
96 unsigned int ret;
97
98 if (id >= BK_UART_MAX)
99 return IOT_FAILURE;
100 if ((data == NULL) || (dataLen == 0))
101 return IOT_FAILURE;
102
103 if (!uart_ctrls[id].inited)
104 return IOT_FAILURE;
105
106 ret = bk_uart_read_bytes(id, data, dataLen, BEKEN_NO_WAIT);
107 if (ret)
108 return IOT_FAILURE;
109
110 return dataLen;
111 }
112
IoTUartWrite(unsigned int id,const unsigned char * data,unsigned int dataLen)113 int IoTUartWrite(unsigned int id, const unsigned char *data, unsigned int dataLen)
114 {
115 unsigned int ret;
116
117 if (id >= BK_UART_MAX)
118 return IOT_FAILURE;
119 if ((data == NULL) || (dataLen == 0))
120 return IOT_FAILURE;
121
122 if (!uart_ctrls[id].inited)
123 return IOT_FAILURE;
124
125 ret = bk_uart_write_bytes(id, data, dataLen);
126 if (ret)
127 return IOT_FAILURE;
128
129 return dataLen;
130 }
131
IoTUartSetFlowCtrl(unsigned int id,IotFlowCtrl flowCtrl)132 unsigned int IoTUartSetFlowCtrl(unsigned int id, IotFlowCtrl flowCtrl)
133 {
134 return IOT_FAILURE; //TODO
135 }
136
137