1
2 #include "ameba_soc.h"
3 #include "k_api.h"
4
5 #include "board.h"
6
7
8 #include "device.h"
9 #include "gpio_api.h"
10 #include "aos/hal/uart.h"
11 #include "device.h"
12 #include "serial_api.h"
13 #include "gpio_api.h"
14 #include "diag.h"
15
16
17
18 #define RX_BUFFER_LENGTH 128
19
20
21 typedef struct{
22 u8 id;
23 PinName tx;
24 PinName rx;
25 PinName rts;
26 PinName cts;
27 }uart_s;
28
29 typedef struct{
30 u32 length;
31 u8 *rxbuffer;
32 }uart_buffer;
33
34 static uart_buffer uart_rxbuffer[5];
35
36
37
38 /*the pin used for uart, you can change the pin as pinmux table, 0 means not use*/
39 static uart_s uart_obj[5]={
40 {2, PA_7, PA_8, 0, 0}, //log uart
41 {0, PA_18, PA_19, PA_17, PA_18}, //km4 uart
42 {1, PA_2, PA_4, PA_0, PB_31}, //bt uart
43 {3, PA_12, PA_13, PA_14, PA_15}, //km0 uart
44 {3, PB_1, PB_2, 0, 0}
45 };
46
47
48 #define UART_TX PB_1
49 #define UART_RX PB_2
50
51 // serial_t uart_obj_t;
52
53 #define EVENT_RX_READY ((uint32_t)1 << 0)
54
55 #if (RHINO_CONFIG_CPU_NUM > 1)
56 static aos_spinlock_t uart0_lock = { KRHINO_SPINLOCK_FREE_VAL, };
57 #else
58 static aos_spinlock_t uart0_lock;
59 #endif
60 static aos_event_t uart0_event;
61 static uint8_t uart0_rx_buf[RX_BUFFER_LENGTH];
62 static size_t uart0_rx_buf_head = 0;
63 static size_t uart0_rx_buf_tail = 0;
64
65 #define uart0_rx_buf_count() \
66 ((RX_BUFFER_LENGTH + uart0_rx_buf_head - uart0_rx_buf_tail) & \
67 (RX_BUFFER_LENGTH - 1))
68 #define uart0_rx_buf_space() \
69 (RX_BUFFER_LENGTH - 1 - uart0_rx_buf_count())
70
uart0_rx_buffer_produce(const void * buf,size_t count)71 size_t uart0_rx_buffer_produce(const void *buf, size_t count)
72 {
73 size_t c = 0;
74 size_t old_count;
75 aos_irqsave_t flags;
76
77 flags = aos_spin_lock_irqsave(&uart0_lock);
78 old_count = uart0_rx_buf_count();
79
80 while (c < count && uart0_rx_buf_space() > 0) {
81 uart0_rx_buf[uart0_rx_buf_head++] = ((const uint8_t *)buf)[c++];
82 uart0_rx_buf_head &= RX_BUFFER_LENGTH - 1;
83 }
84
85 if (old_count == 0 && c > 0)
86 aos_event_set(&uart0_event, EVENT_RX_READY, AOS_EVENT_OR);
87
88 aos_spin_unlock_irqrestore(&uart0_lock, flags);
89
90 return c;
91 }
92
uart0_rx_buffer_consume(void * buf,size_t count)93 static size_t uart0_rx_buffer_consume(void *buf, size_t count)
94 {
95 size_t c = 0;
96 aos_irqsave_t flags;
97
98 flags = aos_spin_lock_irqsave(&uart0_lock);
99
100 while (c < count && uart0_rx_buf_count() > 0) {
101 ((uint8_t *)buf)[c++] = uart0_rx_buf[uart0_rx_buf_tail++];
102 uart0_rx_buf_tail &= RX_BUFFER_LENGTH - 1;
103 }
104
105 if (uart0_rx_buf_count() == 0)
106 aos_event_set(&uart0_event, ~EVENT_RX_READY, AOS_EVENT_AND);
107
108 aos_spin_unlock_irqrestore(&uart0_lock, flags);
109
110 return c;
111 }
112
113 int g_uart_init = 0;
114
115
116
uart_irq(void * data)117 static int32_t uart_irq(void *data)
118 {
119 volatile u8 reg_iir;
120 u8 int_id;
121 u32 reg_val;
122 uint8_t rx_char;
123 UART_TypeDef * uart_dev;
124
125 uart_dev_t* uart = (uart_dev_t*) data;
126 u8 num = uart_obj[uart->port].id;
127 uart_dev = UART_DEV_TABLE[num].UARTx;
128
129
130
131 reg_iir = UART_IntStatus(uart_dev);
132 if ((reg_iir & RUART_IIR_INT_PEND) != 0)
133 {
134 /* No pending IRQ */
135 return 0;
136 }
137
138 int_id = (reg_iir & RUART_IIR_INT_ID) >> 1;
139
140 switch (int_id)
141 {
142 case RUART_RECEIVER_DATA_AVAILABLE:
143 case RUART_TIME_OUT_INDICATION:
144 {
145 if (UART_Readable(uart_dev))
146 {
147 if(uart_rxbuffer[uart->port].length<RX_BUFFER_LENGTH)
148 {
149 UART_CharGet(uart_dev, (uart_rxbuffer[uart->port].rxbuffer + uart_rxbuffer[uart->port].length));
150 uart_rxbuffer[uart->port].length++;
151 }
152 else
153 {
154 DBG_8195A("UART BUFFER SIZE OVERFLOW!!!!!!!\n");
155 }
156 }
157 }
158 break;
159
160 case RUART_RECEIVE_LINE_STATUS:
161 reg_val = (UART_LineStatusGet(uart_dev));
162 DBG_8195A("data_uart_irq: LSR %08x interrupt\n", reg_val);
163 break;
164
165 default:
166 DBG_8195A("data_uart_irq: Unknown interrupt type %d\n", int_id);
167 break;
168 }
169
170 return 0;
171 }
172
173 /**
174 * Initialises a UART interface
175 *
176 *
177 * @param[in] uart the interface which should be initialised
178 *
179 * @return 0 : on success, EIO : if an error occurred with any step
180 */
hal_uart_init(uart_dev_t * uart)181 int32_t hal_uart_init(uart_dev_t *uart)
182 {
183 if(uart->port != 0){
184 u8 num = uart_obj[uart->port].id;
185 UART_InitTypeDef UARTStruct;
186 UART_TypeDef *uart_dev;
187 uart_dev = UART_DEV_TABLE[num].UARTx;
188
189 uart_rxbuffer[uart->port].length=0;
190
191 uart_rxbuffer[uart->port].rxbuffer = aos_malloc(RX_BUFFER_LENGTH);
192
193 /*set pin*/
194 if(uart_obj[uart->port].tx){
195 Pinmux_Config(uart_obj[uart->port].tx, PINMUX_FUNCTION_UART);
196 PAD_PullCtrl(uart_obj[uart->port].tx, GPIO_PuPd_UP);
197 }
198 if(uart_obj[uart->port].rx){
199 Pinmux_Config(uart_obj[uart->port].rx, PINMUX_FUNCTION_UART);
200 PAD_PullCtrl(uart_obj[uart->port].rx, GPIO_PuPd_UP);
201 }
202 if(uart->config.flow_control == FLOW_CONTROL_CTS){
203 if(uart_obj[uart->port].cts){
204 Pinmux_Config(uart_obj[uart->port].cts, PINMUX_FUNCTION_UART_RTSCTS);
205 }
206 }else if(uart->config.flow_control == FLOW_CONTROL_RTS){
207 if(uart_obj[uart->port].rts){
208 Pinmux_Config(uart_obj[uart->port].rts, PINMUX_FUNCTION_UART_RTSCTS);
209 }
210 }else if(uart->config.flow_control == FLOW_CONTROL_CTS_RTS){
211 if(uart_obj[uart->port].cts){
212 Pinmux_Config(uart_obj[uart->port].cts, PINMUX_FUNCTION_UART_RTSCTS);
213 }
214 if(uart_obj[uart->port].rts){
215 Pinmux_Config(uart_obj[uart->port].rts, PINMUX_FUNCTION_UART_RTSCTS);
216 }
217 }
218
219 UART_StructInit(&UARTStruct);
220 if(uart->config.data_width == DATA_WIDTH_7BIT)
221 UARTStruct.WordLen = RUART_WLS_7BITS;
222 else if(uart->config.data_width == DATA_WIDTH_8BIT)
223 UARTStruct.WordLen = RUART_WLS_8BITS;
224 else
225 DBG_8195A("UNSUPPORTED DATA LENGTH\n");
226
227 if(uart->config.stop_bits == STOP_BITS_1)
228 UARTStruct.StopBit = RUART_STOP_BIT_1;
229 else if(uart->config.stop_bits == STOP_BITS_2)
230 UARTStruct.StopBit = RUART_STOP_BIT_2;
231 else
232 DBG_8195A("UNSUPPORTED STOP BIT\n");
233
234 if(uart->config.parity ==NO_PARITY){
235 UARTStruct.Parity = RUART_PARITY_DISABLE;
236 }else if(uart->config.parity == ODD_PARITY){
237 UARTStruct.Parity = RUART_PARITY_ENABLE;
238 UARTStruct.ParityType = RUART_ODD_PARITY;
239 }else if(uart->config.parity == EVEN_PARITY){
240 UARTStruct.Parity = RUART_PARITY_ENABLE;
241 UARTStruct.ParityType = RUART_EVEN_PARITY;
242 }else{
243 DBG_8195A("UNSUPPORTED PARITY TYPE\n");
244 }
245
246 if(uart->config.flow_control == FLOW_CONTROL_DISABLED){
247 UARTStruct.FlowControl = DISABLE;
248 }else{
249 UARTStruct.FlowControl = ENABLE;
250 }
251
252 UART_Init(uart_dev, &UARTStruct);
253 UART_SetBaud(uart_dev, uart->config.baud_rate);
254
255 InterruptRegister((IRQ_FUN)uart_irq, UART_DEV_TABLE[num].IrqNum, (uint32_t)uart, 5);
256 InterruptEn( UART_DEV_TABLE[num].IrqNum, 5);
257
258 UART_INTConfig(uart_dev, RUART_IER_ERBI | RUART_IER_ETOI | RUART_IER_ELSI, ENABLE);
259
260 UART_RxCmd(uart_dev, ENABLE);
261
262 return 0;
263 }else{
264 /*
265 serial_init(&uart_obj_t, UART_TX, UART_RX);
266 serial_baud(&uart_obj_t, 115200);
267 serial_format(&uart_obj_t, 8, ParityNone, 1);
268 serial_irq_handler(&uart_obj_t, uart_irq, (uint32_t)&uart_obj_t);
269
270 serial_irq_set(&uart_obj_t, RxIrq, 1);*/
271
272 g_uart_init = 1;
273 aos_event_new(&uart0_event, 0);
274
275 #ifdef AMEBAD_TODO
276 LOGUART_SetBaud_FromFlash();
277 #endif
278
279 shell_init_rom(0, 0);
280
281 shell_init_ram();
282 ipc_table_init();
283
284 /* Register Log Uart Callback function */
285 extern VOID shell_uart_irq_rom(VOID * Data);
286 InterruptRegister((IRQ_FUN) shell_uart_irq_rom, UART_LOG_IRQ, (u32)NULL, 10);
287 InterruptEn(UART_LOG_IRQ_LP,10);
288
289 return 0;
290
291 }
292 }
293
294 /**
295 * Transmit data on a UART interface
296 *
297 * @param[in] uart the UART interface
298 * @param[in] data pointer to the start of data
299 * @param[in] size number of bytes to transmit
300 * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
301 * if you want to wait forever
302 *
303 * @return 0 : on success, EIO : if an error occurred with any step
304 */
hal_uart_send(uart_dev_t * uart,const void * data,uint32_t size,uint32_t timeout)305 int32_t hal_uart_send(uart_dev_t *uart, const void *data, uint32_t size, uint32_t timeout)
306 {
307 if(uart->port != 0){
308 u8 num = uart_obj[uart->port].id;
309 UART_TypeDef *uart_dev;
310 uart_dev = UART_DEV_TABLE[num].UARTx;
311
312 UART_SendDataTO(uart_dev, (uint8_t *)data, size, 1000);
313
314 return 0;
315 }else{
316 int i;
317 char * cdata = (char *)data;
318
319 for(i = 0; i < size; i++)
320 {
321 DiagPutChar(*cdata++);
322 }
323
324 return 0;
325 }
326 }
327
328
329 /**
330 * Receive data on a UART interface
331 *
332 * @param[in] uart the UART interface
333 * @param[out] data pointer to the buffer which will store incoming data
334 * @param[in] expect_size number of bytes to receive
335 * @param[out] recv_size number of bytes received
336 * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
337 * if you want to wait forever
338 *
339 * @return 0 : on success, EIO : if an error occurred with any step
340 */
hal_uart_recv_II(uart_dev_t * uart,void * data,uint32_t expect_size,uint32_t * recv_size,uint32_t timeout)341 int32_t hal_uart_recv_II(uart_dev_t *uart, void *data, uint32_t expect_size,
342 uint32_t *recv_size, uint32_t timeout)
343 {
344 if(uart->port != 0){
345 u8 num = uart_obj[uart->port].id;
346 UART_TypeDef *uart_dev;
347 uart_dev = UART_DEV_TABLE[num].UARTx;
348
349 if((uart_rxbuffer[uart->port].length) <= expect_size){
350 _memcpy(data, uart_rxbuffer[uart->port].rxbuffer, uart_rxbuffer[uart->port].length);
351 *recv_size = uart_rxbuffer[uart->port].length;
352 uart_rxbuffer[uart->port].length = 0;
353 }else{
354 _memcpy(data, uart_rxbuffer[uart->port].rxbuffer, expect_size);
355 _memcpy(uart_rxbuffer[uart->port].rxbuffer,uart_rxbuffer[uart->port].rxbuffer+expect_size, uart_rxbuffer[uart->port].length-expect_size);
356 *recv_size = expect_size;
357 uart_rxbuffer[uart->port].length -= expect_size;
358 }
359 return 0;
360 }else{
361 uint32_t c = 0;
362
363 if (data == NULL || expect_size == 0) {
364 return -1;
365 }
366
367 while (1) {
368 aos_status_t r;
369 uint32_t val;
370
371 c += uart0_rx_buffer_consume(&((uint8_t *)data)[c], expect_size - c);
372 if (c > 0)
373 break;
374
375 if (timeout == 0)
376 break;
377
378 r = aos_event_get(&uart0_event, EVENT_RX_READY, AOS_EVENT_OR,
379 &val, timeout);
380 if (r)
381 break;
382 }
383
384 if (recv_size) {
385 *recv_size = c;
386 }
387
388 return c > 0 ? 0 : -1;
389
390 }
391
392 }
393
394 /**
395 * Deinitialises a UART interface
396 *
397 * @param[in] uart the interface which should be deinitialised
398 *
399 * @return 0 : on success, EIO : if an error occurred with any step
400 */
hal_uart_finalize(uart_dev_t * uart)401 int32_t hal_uart_finalize(uart_dev_t *uart)
402 {
403 if(uart->port != 0){
404 u8 num = uart_obj[uart->port].id;
405 UART_DeInit( UART_DEV_TABLE[num].UARTx);
406 InterruptDis(UART_DEV_TABLE[num].IrqNum);
407 InterruptUnRegister(UART_DEV_TABLE[num].IrqNum);
408
409 aos_free(uart_rxbuffer[uart->port].rxbuffer);
410
411 return 0;
412 }else{
413 if(g_uart_init == 0)
414 {
415 return 0;
416 }
417
418 uart0_rx_buf_head = 0;
419 uart0_rx_buf_tail = 0;
420 aos_event_free(&uart0_event);
421 g_uart_init = 0;
422
423 return 0;
424 }
425 }
426
hal_uart_rx_sem_take(int uart_id,int timeout)427 int hal_uart_rx_sem_take(int uart_id, int timeout)
428 {
429 uint32_t val;
430
431 if (uart_id != 0)
432 return -EINVAL;
433
434 return aos_event_get(&uart0_event, EVENT_RX_READY, AOS_EVENT_OR, &val, timeout);
435 }
436
hal_uart_rx_sem_give(int uart_id)437 int hal_uart_rx_sem_give(int uart_id)
438 {
439 if (uart_id != 0)
440 return -EINVAL;
441
442 aos_event_set(&uart0_event, EVENT_RX_READY, AOS_EVENT_OR);
443
444 return 0;
445 }
446