1 #include <stdint.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <hal/hal.h>
5 #include <k_api.h>
6 #include <aos/hal/uart.h>
7 #include <aos/kernel.h>
8 
9 #define GENIE_MCU_UART_PORT (1)
10 #define GENIE_MCU_UART_BAUDRATE (9600)
11 #define FRAME_BUFFER_SIZE (256)
12 #define SEND_BUFFER_SIZE (64)
13 #define FRAME_TIEMOUT (50) //unit ms
14 
15 static uart_dev_t mcu_uart, *p_mcu_uart = NULL;
16 static unsigned char frame_buff_index = 0;
17 static unsigned char frame_buff[FRAME_BUFFER_SIZE];
18 static aos_timer_t uart_frame_end_timer;
19 
20 extern int genie_at_cmd_parser(char data[]);
21 extern int genie_bin_cmds_handle(uint8_t *p_data, uint8_t data_len);
22 
uart_frame_end_timer_cb(void * arg1,void * arg2)23 static void uart_frame_end_timer_cb(void *arg1, void *arg2)
24 {
25      if (frame_buff_index > 0) //received some datas
26      {
27 #ifdef CONIFG_GENIE_MESH_BINARY_CMD
28           genie_bin_cmds_handle(frame_buff, frame_buff_index);
29 #endif
30 #ifdef CONFIG_GENIE_MESH_AT_CMD
31           genie_at_cmd_parser((char *)frame_buff);
32 #endif
33 
34           frame_buff_index = 0; //reset this index
35           memset(frame_buff, 0, FRAME_BUFFER_SIZE);
36      }
37 }
38 
genie_sal_uart_init(void)39 int genie_sal_uart_init(void)
40 {
41      memset(frame_buff, 0, FRAME_BUFFER_SIZE);
42 
43      mcu_uart.port = GENIE_MCU_UART_PORT;
44      mcu_uart.config.baud_rate = GENIE_MCU_UART_BAUDRATE;
45      mcu_uart.config.mode = MODE_TX_RX;
46      mcu_uart.config.flow_control = FLOW_CONTROL_DISABLED;
47      mcu_uart.config.stop_bits = STOP_BITS_1;
48      mcu_uart.config.parity = NO_PARITY;
49      mcu_uart.config.data_width = DATA_WIDTH_8BIT;
50 
51      if (0 != hal_uart_init(&mcu_uart))
52      {
53           printf("uart(%d) init fail\r\n", GENIE_MCU_UART_PORT);
54           return -1;
55      }
56      else
57      {
58           p_mcu_uart = &mcu_uart;
59      }
60 
61      return aos_timer_new(&uart_frame_end_timer, uart_frame_end_timer_cb, NULL, FRAME_TIEMOUT, 0);
62 }
63 
genie_sal_uart_send_one_byte(uint8_t byte)64 int32_t genie_sal_uart_send_one_byte(uint8_t byte)
65 {
66      uint8_t send_data = 0;
67 
68      if (p_mcu_uart == NULL)
69      {
70           printf("uart(%d) not init\r\n", GENIE_MCU_UART_PORT);
71           return -1;
72      }
73 
74      send_data = byte;
75 
76      return hal_uart_send(p_mcu_uart, &send_data, 1, 0);
77 }
78 
genie_sal_uart_send_str(const char * fmt,...)79 int32_t genie_sal_uart_send_str(const char *fmt, ...)
80 {
81      int ret = 0;
82      va_list params;
83      int8_t send_buff[SEND_BUFFER_SIZE];
84 
85      if (p_mcu_uart == NULL)
86      {
87           printf("uart(%d) not init\r\n", GENIE_MCU_UART_PORT);
88           return -1;
89      }
90 
91      va_start(params, fmt);
92      memset(send_buff, 0, SEND_BUFFER_SIZE);
93      ret = vsnprintf((char *)send_buff, SEND_BUFFER_SIZE - 1, fmt, params);
94      va_end(params);
95 
96      if (ret <= 0)
97      {
98           printf("uart(%d) send fail,no mem\r\n", GENIE_MCU_UART_PORT);
99           return -1;
100      }
101 
102      if (0 != hal_uart_send(p_mcu_uart, (void *)send_buff, (uint32_t)strlen((const char *)send_buff), HAL_WAIT_FOREVER))
103      {
104           printf("uart(%d) send fail\r\n", GENIE_MCU_UART_PORT);
105           return -1;
106      }
107 
108      return 0;
109 }
110 
genie_sal_uart_read(uint32_t timeout)111 int genie_sal_uart_read(uint32_t timeout)
112 {
113      int8_t read_buff;
114      int32_t ret = 0;
115      uint32_t recv_size = 0;
116 
117      if (p_mcu_uart == NULL)
118      {
119           printf("uart(%d) not init\r\n", GENIE_MCU_UART_PORT);
120           return -1;
121      }
122 
123      ret = hal_uart_recv_II(p_mcu_uart, &read_buff, 1, &recv_size, timeout);
124      if ((ret == 0) && (recv_size == 1))
125      {
126           if (frame_buff_index > FRAME_BUFFER_SIZE)
127           {
128                frame_buff_index = 0;
129           }
130 
131           frame_buff[frame_buff_index++] = read_buff;
132           aos_timer_stop(&uart_frame_end_timer);
133           aos_timer_start(&uart_frame_end_timer);
134      }
135 
136      return 0;
137 }
138