1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #ifndef BE_BOARD_MGR_H
6 #define BE_BOARD_MGR_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include <stdint.h>
13 #include "aos_hal_i2c.h"
14 
15 #define I2C_SLAVE_MAX   (16)
16 
17 /* app.json configuration */
18 #define APP_CONFIG_PAGES   "pages"
19 #define APP_CONFIG_IO      "io"
20 #define APP_CONFIG_NET     "net"
21 #define APP_CONFIG_DEBUG   "debugLevel"
22 #define APP_CONFIG_REPL    "repl"
23 
24 #define MARKER_ID          "type"
25 #define MARKER_PORT        "port"
26 
27 /* GPIO */
28 #define MARKER_GPIO        "GPIO"
29 #define GPIO_DIR           "dir"
30 #define GPIO_PULL          "pull"
31 #define GPIO_INTMODE       "intMode"
32 
33 #define GPIO_DIR_OUTPUT    "output"
34 #define GPIO_DIR_INPUT     "input"
35 #define GPIO_DIR_IRQ       "irq"
36 #define GPIO_DIR_ANALOG    "analog"
37 
38 #define GPIO_PULL_DOWN     "pulldown"
39 #define GPIO_PULL_UP       "pullup"
40 #define GPIO_PULL_OPEN     "opendrain"
41 
42 #define GPIO_INT_RISING    "rising"
43 #define GPIO_INT_FALLING   "falling"
44 #define GPIO_INT_BOTH      "both"
45 #define GPIO_INT_HIGH_LEVEL  "high"
46 #define GPIO_INT_LOW_LEVEL   "low"
47 
48 /* UART */
49 #define MARKER_UART        "UART"
50 #define UART_DATA_WIDTH    "dataWidth"
51 #define UART_BAUD_RATE     "baudRate"
52 #define UART_STOP_BITS     "stopBits"
53 #define UART_FLOW_CONTROL  "flowControl"
54 #define UART_PARITY_CONFIG "parity"
55 #define UART_MODE          "mode"
56 
57 #define UART_FC_DISABLE    "disable"
58 #define UART_FC_CTS        "cts"
59 #define UART_FC_RTS        "rts"
60 #define UART_FC_RTSCTS     "rtscts"
61 
62 #define UART_PARITY_NONE   "none"
63 #define UART_PARITY_ODD    "odd"
64 #define UART_PARITY_EVEN   "even"
65 
66 /* I2C */
67 #define MARKER_I2C         "I2C"
68 #define I2C_ADDR_WIDTH     "addrWidth"
69 #define I2C_FREQ           "freq"
70 #define I2C_MODE           "mode"
71 #define I2C_TIMEOUT        "timeout"
72 #define I2C_ADDR_DEV       "devAddr"
73 #define I2C_SLAVE_TREE     "slaveTree"
74 
75 #define I2C_MASTER         "master"
76 #define I2C_SLAVE          "slave"
77 
78 /* SPI */
79 #define MARKER_SPI         "SPI"
80 #define SPI_MODE           "mode"
81 #define SPI_FREQ           "freq"
82 #define SPI_CPOL_CPHA      "polPha"
83 
84 #define SPI_MODE_MASTER    "master"
85 #define SPI_MODE_SLAVE     "slave"
86 
87 /* ADC */
88 #define MARKER_ADC         "ADC"
89 #define ADC_SAMPLING       "sampling"
90 
91 /* DAC */
92 #define MARKER_DAC         "DAC"
93 
94 /* CAN */
95 #define MARKER_CAN         "CAN"
96 #define CAN_BAUD_RATE      "baudRate"
97 #define CAN_IDE            "ide"
98 #define CAN_AUTO_BUS_OFF   "auto_bus_off"
99 #define CAN_RETRY_TRANSMIT "retry_transmit"
100 
101 #define IDE_NORMAL_CAN     "normal"
102 #define IDE_EXTEND_CAN     "extend"
103 
104 #define CAN_DISABLE        "disable"
105 #define CAN_ENABLE         "enable"
106 
107 /* PWM */
108 #define MARKER_PWM         "PWM"
109 
110 /* TIMER */
111 #define MARKER_TIMER       "TIMER"
112 typedef enum addon_module {
113     MODULE_GPIO = 0x1324,
114     MODULE_UART,
115     MODULE_I2C,
116     MODULE_PWM,
117     MODULE_ADC,
118     MODULE_DAC,
119     MODULE_SPI,
120     MODULE_TIMER,
121     MODULE_RTC,
122     MODULE_BT,
123     MODULE_IR,
124     MODULE_I2S,
125     MODULE_CAN,
126     MODULE_SDIO,
127     MODULE_USB,
128     MODULE_I2C_GPIO,
129     MODULE_NUMS,
130 } addon_module_m;
131 
132 typedef enum board_json_err {
133     BOARD_ERR_NONE = 0,
134     BOARD_ERR_JOSN_NOT_EXIST = -1,
135     BOARD_ERR_JOSN_EMPTY = -2,
136     BOARD_ERR_JOSN_PARSER_FAILED = -3,
137     BOARD_ERR_NODE_NOT_EXIST = -4,
138     BOARD_ERR_HANDLE_NULL = -5,
139     BOARD_ERR_SIZE_INVALID = -6,
140     BOARD_ERR_NO_MEM = -7,
141     BOARD_ERR_JSON_PARSE = -8,
142     BOARD_ERR_INVALID_ARG = -9,
143 } board_json_err;
144 
145 typedef struct item_handle {
146     void *handle;
147 } item_handle_t;
148 
149 typedef struct {
150     int irq_mode;
151     int js_cb_ref;
152     void *reserved;
153 } gpio_params_t;
154 
155 typedef struct {
156     uint8_t* name;
157     uint16_t addr;
158 } i2c_slave_t;
159 
160 typedef struct {
161     i2c_dev_t dev;
162     uint32_t timeout;
163     i2c_slave_t salves[I2C_SLAVE_MAX];
164 } i2c_devicetree_t;
165 
166 /**
167  * initialize mgr system
168  *
169  * @param[in]  json_path  the path of boar-mgr file
170  * @return  the operation status, 0 is OK, others is error
171  */
172 int32_t py_board_mgr_init();
173 
174 /**
175  * attach a  driver resource
176  *
177  * @param[in]  module   the module type of a driver
178  * @param[in]  name_id  the name of a driver
179  * @param[out] name_id  the resource of a driver
180  * @return  the operation status, 0 is OK, others is error
181  */
182 int8_t py_board_attach_item(addon_module_m module, const char *name_id,
183                             item_handle_t *out);
184 
185 /**
186  * release a  driver resource
187  *
188  * @param[in]  module   the module type of a driver
189  * @param[in]  handle  the resource of a driver
190  * @return  the operation status, 0 is OK, others is error
191  */
192 int8_t py_board_disattach_item(addon_module_m module, item_handle_t *handle);
193 
194 /**
195  * the attach status of the driver and resource
196  *
197  * @param[in]  module   the module type of a driver
198  * @param[in]  handle  the resource of a driver
199  * @return  the attach status, 1 is attach, others is dis-attach
200  */
201 int8_t py_board_check_attach_status(addon_module_m module,
202                                     item_handle_t *handle);
203 
204 /**
205  * get the resource of a driver by name
206  *
207  * @param[in]  module   the module type of a driver
208  * @param[in]  name_id  the name of a driver
209  * @return  driver resource,  null if not exist,otherwise it's right
210  */
211 void *py_board_get_node_by_name(addon_module_m module, const char *name_id);
212 
213 /**
214  * get the resource of a driver by the handle of a driver resource
215  *
216  * @param[in]  module   the module type of a driver
217  * @param[in]  handle  the resource of a driver
218  * @return  driver resource,  null if not exist,otherwise it's right
219  */
220 void *py_board_get_node_by_handle(addon_module_m module, item_handle_t *handle);
221 
222 #ifdef __cplusplus
223 }
224 #endif
225 
226 #endif /* BE_BOARD_MGR_H */
227