]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/include/drv/digital_io.h
Remove the rest of the HAL layer
[pes-rpp/rpp-lib.git] / rpp / include / drv / digital_io.h
1 /**
2  * Hardware Abstraction Layer library interface file.
3  *
4  * @file hal.h
5  *
6  * @copyright Copyright (C) 2013, 2015 Czech Technical University in Prague
7  *
8  * @author Carlos Jenkins <carlos@jenkins.co.cr>
9  */
10
11 #ifndef __HAL_H
12 #define __HAL_H
13
14 #include "sys/sys.h"
15 #include "drv/digital_io_def.h"
16
17 typedef struct dio_pin_map_element {
18         const char *pin_name;          // Pin name
19         uint32_t pin_desc;              // Pin descriptor assigned to the pin name
20 } dio_pin_map_element_t;
21
22 enum dio_port_interface_type {
23         ADC = 1,
24         SPI,
25         GPIO
26 };
27
28 /**
29  * @brief Port descriptor
30  *
31  * The structure describes the port. Port here means set of IO pins on the board.
32  * The API is designed to provide an interface for setting and reading from ports connected to the MCU by SPI, GPIO or ADC.
33  */
34 typedef struct dio_port_desc_st {
35         uint32_t *config;       /**< Configuration of the port. An arry of values, which meaning differs for the interface type:
36                                          for SPI: address, chip-select
37                                          for GPIO: descriptors for pins (defined in gpio_tms570_def.h)
38                                          for ADC: ADC base address, ADC group number, Peripheral type (HOUTIFBK or ADC)
39                                  */
40         uint32_t numValues;     /**< Size of the data to be read from or written on the port. Meaning differs for the interface type:
41                                         for SPI: size of the command in bytes
42                                         for GPIO: number of pins on the port
43                                         for ADC: number of channels to be read from ADC.
44                                  */
45         enum dio_port_interface_type interfaceType;     /**< Type of the interface to which the port or its controller is connected.
46                                             Can be SPI, ADC or GPIO
47                                      */
48         uint32_t (*port_getfnc_ptr)(uint32_t *config, uint32_t num_val, uint32_t *values);   /**< Pointer to a getter function. If it is NULL, than port is write only.
49                                                                                                      All SPI ports are write only, because command has to be sent to obtain actual response.
50                                                                                                      It is allowed to read values from an output port.
51                                                                                               */
52         uint32_t (*port_setfnc_ptr)(uint32_t *config, uint32_t num_val, const uint32_t *values); /**< Pointer to a setter function. If it is NULL, than port is read only.
53                                                                                                      All ADC ports are read only.
54                                                                                                      It is not allowed to write values on an input port.
55                                                                                                   */
56 } dio_port_desc_t;
57
58 /**
59  * Maps port descriptor to the port name
60  */
61 typedef struct dio_port_def_st {
62         char *name;
63         dio_port_desc_t *desc;
64 } dio_port_def_t;
65
66 #define DIO_PORT_CONF_MASK      0xff000000
67
68 #define DIO_PORT_CONF_DIR_MASK  0x01000000
69 #define DIO_PORT_CONF_DIR_IN    (0x00000000 | DIO_PORT_CONF_SET_DIR)
70 #define DIO_PORT_CONF_DIR_OUT   (0x01000000 | DIO_PORT_CONF_SET_DIR)
71
72 #define DIO_PORT_CONF_INIT_MASK 0x02000000
73 #define DIO_PORT_CONF_INIT_LOW  0x00000000
74 #define DIO_PORT_CONF_INIT_HIGH 0x02000000
75
76 #define DIO_PORT_CONF_OD_MASK   0x04000000
77 #define DIO_PORT_CONF_OD_OFF    0x00000000
78 #define DIO_PORT_CONF_OD_ON     0x04000000
79
80 #define DIO_PORT_CONF_SET_DIR   0x08000000
81
82 #define DIO_PORT_CONF_MODE_MASK 0x30000000
83 #define DIO_PORT_CONF_MODE_PTYPE_MASK   0x10000000
84 #define DIO_PORT_CONF_MODE_PEN_MASK     0x20000000
85 #define DIO_PORT_CONF_MODE_PU   0x10000000      // Pull-up
86 #define DIO_PORT_CONF_MODE_PD   0x00000000      // Pull-down
87 #define DIO_PORT_CONF_MODE_PEN  0x00000000      // Pull resistor enable
88 #define DIO_PORT_CONF_MODE_PDIS 0x20000000      // Pull resistor disable
89
90 #define DIO_PORT_CONF_FNC_MASK  0xc0000000
91 #define DIO_PORT_CONF_FNC_GPIO  0x00000000
92 #define DIO_PORT_CONF_FNC_0     0x00000000
93 #define DIO_PORT_CONF_FNC_1     0x40000000
94 #define DIO_PORT_CONF_FNC_2     0x80000000
95 #define DIO_PORT_CONF_FNC_3     0xc0000000
96
97 /* Port indexes to portmap */
98 #define DIO_PORT_ID_DMM     0x0
99 #define DIO_PORT_ID_GIOA    0x1
100 #define DIO_PORT_ID_GIOB    0x2
101 #define DIO_PORT_ID_HET1    0x3
102 #define DIO_PORT_ID_HET2    0x4
103
104 uint8_t dio_gpio_get_pin_cnt();
105
106 dio_pin_map_element_t* dio_gpio_get_pin_map();
107
108 /**
109  * Get port base assigned to port number
110  * @param[in]   port_num    Port number <0;4>
111  * @return Pointer to port registers
112  */
113 gioPORT_t *dio_gpio_get_port_base(uint32_t port_num);
114
115 /**
116  * Get port number assigned to pin in its descriptor
117  * @param[in]   pin descriptor
118  * @return  Index of port
119  */
120 uint32_t dio_gpio_pin_get_port_num(uint32_t pin_dsc);
121
122 /**
123  * Get port base from pin descriptor
124  * Combines two upper defined functions
125  * @param[in]   pin_dcs Pin descriptor
126  * @return      Pointer to port registers
127  */
128 gioPORT_t *dio_gpio_pin_get_port_base(uint32_t pin_dsc);
129
130 /**
131  *  Get pin descriptor assigned to pin name.
132  *  @param[in]  pin_name    Pointer to string - the name of the pin.
133  *  @param[in]  len         Length of the name, if terminated by '/0', then len=-1
134  *  @return Pin descriptor or NULL if not found
135  */
136 uint32_t *dio_gpio_pin_get_dsc(const char *pin_name, int len);
137
138 /**
139  * Get value from GPIO pin
140  * @param[in]   pin_dsc pin descriptor
141  * @return  value read from specified gpio pin
142  */
143 uint32_t dio_gpio_pin_get_value(uint32_t pin_dsc);
144
145 /**
146  * Set value to gpio pin
147  * @param[in]   pin_dsc pin descriptor
148  * @param[in]   value   value to be assigned to the pin
149  */
150 void dio_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value);
151
152 /**
153  * Set pin direction to input
154  * @param[in] pin_dsc pin descriptor
155  * @return always 0
156  */
157 int dio_gpio_pin_set_dir_in(uint32_t pin_dsc);
158
159 /**
160  * Set pin direction to output
161  * @param[in] pin_dsc pin descriptor
162  * @return always 0
163  */
164 int dio_gpio_pin_set_dir_out(uint32_t pin_dsc, uint32_t value);
165
166 /**
167  * Get pin direction
168  * @param[in] pin_dsc pin descriptor
169  * @return 1 - output, 0 - input
170  */
171 int dio_gpio_pin_get_dir(uint32_t pin_dsc);
172
173 /**
174  * Set pin as pull down or pull up and pull resistor enabled or disabled.
175  * @param[in] pin_dsc Descriptor of the pin
176  * @param[in] Mode on which pin will be configured to.
177  *              PORT_CONF_MODE_PU - pull up
178  *              PORT_CONF_MODE_PD - pull down
179  *              must be | with
180  *              PORT_CONF_MODE_PEN - pull resistor enable
181  *              PORT_CONF_MODE_PDIS - pull resistor disable
182  * @return always 0
183  */
184 uint32_t dio_gpio_pin_set_mode(uint32_t pin_dsc, uint32_t mode);
185
186 /**
187  * Configure pin to be open drain or not
188  * @param[in] pin_dsc Descriptor of the pin
189  * @param[in] Mode on which pin will be configured to.
190  *              PORT_CONF_OD_OFF - open-drain disabled
191  *              PORT_CONF_OD_ON - open drain enabled
192  *
193  * @return always 0
194  */
195 uint32_t dio_gpio_pin_set_od(uint32_t pin_dsc, uint32_t od);
196
197 /**
198  * Configure pin
199  * @param[in] pin_dsc Descriptor of the pin
200  * @param[in] Mode on which pin will be configured to.
201  *              PORT_CONF_OD_OFF - open-drain disabled
202  *              PORT_CONF_OD_ON - open drain enabled
203  *
204  *              PORT_CONF_MODE_PU - pull up
205  *              PORT_CONF_MODE_PD - pull down
206  *
207  *              PORT_CONF_MODE_PEN - pull resistor enable
208  *              PORT_CONF_MODE_PDIS - pull resistor disable
209  *
210  *              PORT_CONF_DIR_IN - direction input
211  *              PORT_CONF_DIR_OUT - direction output
212  *
213  *              PORT_CONF_INIT_LOW - init value 0
214  *              PORT_CONF_INIT_HIGH - init value 1
215  *
216  *              PORT_CONF_FNC_GPIO - port function GPIO
217  *              PORT_CONF_FNC_FNCX - port alternate function X
218  *
219  * @return always 0
220  */
221 uint32_t dio_gpio_pin_set_config(uint32_t pin_dsc, uint32_t conf);
222
223 /**
224  * Do the initial pin configuration according values in pin descriptor
225  * @param[in]   pin_dsc pin descriptor
226  * @return always 0;
227  */
228 uint32_t dio_gpio_pin_configure(uint32_t pin_dsc);
229
230 /**
231  * Get values of all pins of given port.
232  * @param[in] config    Pointer to array of pin descriptors
233  * @param[in] num_val   Number of pins assigned to the port
234  * @param[out] values   Stored values of all pins of the port. 1st bit -> pin0, 2nd bit -> pin1...
235  * @return always 0
236  */
237 uint32_t dio_gpio_port_get_val(uint32_t *config, uint32_t num_val, uint32_t *values);
238
239 /**
240  * Set values to all pins of given port.
241  * @param[in] config    Pointer to array of pin descriptors
242  * @param[in] num_val   Number of pins assigned to the port
243  * @param[in] values    Stored values of all pins of the port. 1st bit -> pin0, 2nd bit -> pin1...
244  * @return always 0
245  */
246 uint32_t dio_gpio_port_set_val(uint32_t *config, uint32_t num_val, const uint32_t *values);
247
248 /**
249  *  Get port descriptor assigned to port name.
250  *  @param[in]  port_name   Pointer to string - the name of the port.
251  *  @param[in]  len         Length of the name, if terminated by '/0', then len=-1
252  *  @return Port descriptor or NULL if not found
253  */
254 const dio_port_def_t *dio_port_get_map();
255
256 /**
257  *  Get port descriptor assigned to port name.
258  *  @param[in]  port_name   Pointer to string - the name of the port.
259  *  @param[in]  len         Length of the name, if terminated by '/0', then len=-1
260  *  @return Port descriptor or NULL if not found
261  */
262 dio_port_desc_t *dio_port_get_dsc(const char *port_name, int len);
263
264 uint32_t dio_port_get_val_cnt(const dio_port_desc_t* port_desc);
265
266 /**
267  * Transfer command through the spi
268  * @param[in] config    Address of the SPI
269  * @param[in] num_bytes Number of bytes to be trasfered
270  * @param[in] commands  SPI command to be sent
271  * @return spi response
272  */
273 uint32_t dio_spi_port_transfer_command(uint32_t *config, uint32_t num_bytes, const uint32_t *commands);
274
275
276 #endif /* __HAL_H */