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 <k_api.h>
10 #include "ulog/ulog.h"
11 #include "aos/hal/uart.h"
12 
13 /* RS485 Macro definition */
14 #define HAAS_RS485_UART_IDX  1
15 #define HAAS_RS485_BD_DFT    19200
16 
17 /* static variable definition */
18 static uart_dev_t uart_rs485 = {0};
19 
haas_rs485_init(uart_dev_t * uart_dev)20 int32_t haas_rs485_init(uart_dev_t *uart_dev)
21 {
22     int32_t ret = 0;
23 
24     if(uart_dev == NULL)
25     {
26         uart_rs485.port = HAAS_RS485_UART_IDX;
27         uart_rs485.config.baud_rate = HAAS_RS485_BD_DFT;
28         uart_rs485.config.data_width = DATA_WIDTH_8BIT;
29         uart_rs485.config.flow_control = FLOW_CONTROL_DISABLED;
30         uart_rs485.config.mode = MODE_TX_RX;
31         uart_rs485.config.parity = NO_PARITY;
32         uart_rs485.config.stop_bits = STOP_BITS_1;
33     }
34     else
35     {
36         memset(&uart_rs485, 0, sizeof(uart_dev_t));
37         memcpy(&uart_rs485, uart_dev, sizeof(uart_dev_t));
38     }
39 
40     ret = hal_uart_init(&uart_rs485);
41     if (ret) {
42         printf("=====rs485: init fail =====\r\n");
43         return ret;
44     }
45 
46     return ret;
47 }
48 
haas_rs485_send(const void * data,uint32_t size,uint32_t timeout)49 int32_t haas_rs485_send(const void *data, uint32_t size, uint32_t timeout)
50 {
51     int32_t ret;
52 
53     ret = hal_uart_send(&uart_rs485, data, size, timeout);
54     if (ret) {
55         printf("=====rs485: fail to send test data======\r\n");
56         return -1;
57     }
58 
59 }
60 
haas_rs485_recv(void * data,uint32_t expect_size,uint32_t * recv_size,uint32_t timeout)61 int32_t haas_rs485_recv(void *data, uint32_t expect_size,
62                          uint32_t *recv_size, uint32_t timeout)
63 {
64     int32_t ret = 0;
65 
66     ret = hal_uart_recv_II(&uart_rs485, data, expect_size, recv_size, timeout);
67     if (ret)
68     {
69         printf("=====rs485: recv data fail=====\r\n");
70     }
71 }
72 
haas_rs485_deinit(uart_dev_t * uart)73 int32_t haas_rs485_deinit(uart_dev_t *uart)
74 {
75     int32_t ret = 0;
76 
77     ret = hal_uart_finalize(&uart_rs485);
78 
79     return ret;
80 }
81