1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include "aos/init.h"
10 #include "board.h"
11 #include <k_api.h>
12 #include "aos/hal/uart.h"
13 #include "k_trace.h"
14 #include "SEGGER_SYSVIEW.h"
15 #include "SEGGER_RTT.h"
16 
17 #define SYSVIEW_SINGLE_TX 1024
18 #define UART_TX_TIMEOUT 100
19 
20 #define TASK_SENDDATA_NAME      "senddata"
21 #define TASK_SENDDATA_STACKSIZE 4096
22 #define TASK_SENDDATA_PRI       50
23 
24 #define TASK_RECVDATA_NAME      "revdata"
25 #define TASK_RECVDATA_STACKSIZE 1024
26 #define TASK_RECVDATA_PRI       50
27 
28 #define _SERVER_HELLO_SIZE        (4)
29 #define _TARGET_HELLO_SIZE        (4)
30 
31 static const U8 _abHelloMsg[_TARGET_HELLO_SIZE] = { 'S', 'V', (SEGGER_SYSVIEW_VERSION / 10000), (SEGGER_SYSVIEW_VERSION / 1000) % 10 };
32 // "Hello" message expected by SysView: [ 'S', 'V', <PROTOCOL_MAJOR>, <PROTOCOL_MINOR> ]
33 
34 static uart_dev_t uart_demo;
35 
36 /* task entry */
task_senddata_entry(void * arg)37 static void task_senddata_entry(void *arg)
38 {
39   int ret = 0;
40   int channel_id = SEGGER_SYSVIEW_GetChannelID();
41   uint32_t num = 0;
42   uint8_t  bytes = 0;
43   uint8_t tx_buf[SYSVIEW_SINGLE_TX];
44   int length = 0;
45   int len_read = 0;
46 
47   SEGGER_SYSVIEW_Start();
48   ret = hal_uart_send(&uart_demo, _abHelloMsg, _SERVER_HELLO_SIZE, UART_TX_TIMEOUT);
49   if (ret != 0) {
50     printf("uart data send error!\n");
51   }
52 
53   while (1)
54   {
55     bytes = SEGGER_RTT_GetBytesInBuffer(channel_id);
56     if(bytes >= SYSVIEW_SINGLE_TX) {
57       printf("bytes:%d!\n", bytes);
58     }
59     if(bytes == 0) {
60       printf("bytes is 0!\n");
61       aos_msleep(1);
62       break;
63     }
64 
65     num = SEGGER_RTT_ReadUpBufferNoLock(channel_id, tx_buf, bytes);
66 
67     hal_trace_output_block(tx_buf, num);
68     usleep(1);
69   }
70 }
71 
task_recvdata_entry(void * arg)72 static void task_recvdata_entry(void *arg)
73 {
74     int  i;
75     int  ret;
76     int  rev_length;
77     char rev_buf[1] = {0};
78     int channel_id = SEGGER_SYSVIEW_GetChannelID();
79 
80     while (1) {
81         ret = hal_uart_recv_II(&uart_demo, rev_buf, sizeof(rev_buf), &rev_length, HAL_WAIT_FOREVER);
82         if (ret == 0) {
83           SEGGER_RTT_WriteDownBuffer(channel_id, rev_buf, 1);
84         }
85     }
86 }
87 
uart_port_init(void)88 int uart_port_init(void)
89 {
90   int ret = 0;
91 	/* task handle */
92 	aos_task_t task_recvdata;
93   aos_task_t task_senddata;
94 
95   uart_demo.port                = 0;
96   uart_demo.config.baud_rate    = 1500000;
97   uart_demo.config.mode         = MODE_TX_RX;
98   uart_demo.config.flow_control = FLOW_CONTROL_DISABLED;
99   uart_demo.config.stop_bits    = STOP_BITS_1;
100   uart_demo.config.parity       = NO_PARITY;
101   uart_demo.config.data_width   = DATA_WIDTH_8BIT;
102 
103   ret = hal_uart_init(&uart_demo);
104   if(ret != 0) {
105       printf("init uart error\r\n");
106       return -1;
107   }
108   /* Create the task to receive data */
109   ret = aos_task_new_ext(&task_recvdata, TASK_RECVDATA_NAME, task_recvdata_entry, NULL,
110                           TASK_RECVDATA_STACKSIZE, TASK_RECVDATA_PRI);
111   if (ret != 0) {
112       hal_uart_finalize(&uart_demo);
113       printf("create uart data recv task error\r\n");
114       return;
115   }
116   /* Create the task to send data */
117   ret = aos_task_new_ext(&task_senddata, TASK_SENDDATA_NAME, task_senddata_entry, NULL,
118                           TASK_SENDDATA_STACKSIZE, TASK_SENDDATA_PRI);
119   if (ret != 0) {
120       hal_uart_finalize(&uart_demo);
121       printf("create uart data recv task error\r\n");
122       return;
123   }
124 
125   return ret;
126 }
127