1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include "amp_boot.h"
6 #include "board_config.h"
7 
8 static uart_dev_t g_boot_uart = { 0 };
9 
pyamp_boot_uart_send_byte(unsigned char c)10 void pyamp_boot_uart_send_byte(unsigned char c)
11 {
12     if ((aos_hal_uart_send(&g_boot_uart, &c, 1, osWaitForever)) != 0) {
13         // aos_printf("pyamp_boot_uart_send_byte error\n");
14     }
15 }
16 
pyamp_boot_uart_send_str(char * str)17 void pyamp_boot_uart_send_str(char *str)
18 {
19     if ((aos_hal_uart_send(&g_boot_uart, str, strlen(str), osWaitForever)) != 0) {
20         // aos_printf("pyamp_boot_uart_send_str %s error\n", str);
21     }
22 }
23 
pyamp_boot_uart_recv_byte(unsigned char * c)24 unsigned char pyamp_boot_uart_recv_byte(unsigned char *c)
25 {
26     int read_byte = 0;
27     if (aos_hal_uart_recv_poll(&g_boot_uart, c, 1, &read_byte) != 0) {
28         return 0;
29     }
30     return read_byte;
31 }
32 
pyamp_boot_uart_recv_line(unsigned char * str_line,int lens,int timeout_ms)33 int pyamp_boot_uart_recv_line(unsigned char *str_line, int lens, int timeout_ms)
34 {
35     uint64_t begin_time = (uint64_t)aos_now_ms();
36     int32_t read_byte = 0;
37     int32_t str_num = 0;
38     char c = 0;
39 
40     while (1) {
41         c = 0;
42         aos_hal_uart_recv_poll(&g_boot_uart, &c, 1, &read_byte);
43         if (read_byte == 1) {
44             str_line[str_num] = c;
45             if (c == '\n') {
46                 if ((str_num > 0) && (str_line[str_num - 1] == '\r')) {
47                     str_num--;
48                 }
49                 return str_num;
50             }
51             if (str_num >= lens) {
52                 return 0;
53             }
54             str_num++;
55         }
56 
57         if ((timeout_ms != osWaitForever) && (begin_time + timeout_ms < aos_now_ms())) {
58             return 0;
59         }
60     }
61 
62     return 0;
63 }
64 
pyamp_boot_uart_init(void)65 void pyamp_boot_uart_init(void)
66 {
67     g_boot_uart.port = MP_RECOVERY_UART_PORT;
68     g_boot_uart.config.baud_rate = MP_RECOVERY_UART_PORT_BAUDRATE;
69     g_boot_uart.config.data_width = DATA_WIDTH_8BIT;
70     g_boot_uart.config.flow_control = FLOW_CONTROL_DISABLED;
71     g_boot_uart.config.mode = MODE_TX_RX;
72     g_boot_uart.config.parity = NO_PARITY;
73     g_boot_uart.config.stop_bits = STOP_BITS_1;
74     int ret = aos_hal_uart_init(&g_boot_uart);
75     if (ret) {
76         return;
77     }
78 }
79