2 * Hardware Abstraction Layer library interface file.
6 * @copyright Copyright (C) 2013, 2015 Czech Technical University in Prague
8 * @author Carlos Jenkins <carlos@jenkins.co.cr>
15 #include "drv/digital_io_def.h"
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;
22 enum dio_port_interface_type {
29 * @brief Port descriptor
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.
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)
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.
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
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.
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.
59 * Maps port descriptor to the port name
61 typedef struct dio_port_def_st {
63 dio_port_desc_t *desc;
66 #define DIO_PORT_CONF_MASK 0xff000000
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)
72 #define DIO_PORT_CONF_INIT_MASK 0x02000000
73 #define DIO_PORT_CONF_INIT_LOW 0x00000000
74 #define DIO_PORT_CONF_INIT_HIGH 0x02000000
76 #define DIO_PORT_CONF_OD_MASK 0x04000000
77 #define DIO_PORT_CONF_OD_OFF 0x00000000
78 #define DIO_PORT_CONF_OD_ON 0x04000000
80 #define DIO_PORT_CONF_SET_DIR 0x08000000
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
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
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
104 uint8_t dio_gpio_get_pin_cnt();
106 dio_pin_map_element_t* dio_gpio_get_pin_map();
109 * Get port base assigned to port number
110 * @param[in] port_num Port number <0;4>
111 * @return Pointer to port registers
113 gioPORT_t *dio_gpio_get_port_base(uint32_t port_num);
116 * Get port number assigned to pin in its descriptor
117 * @param[in] pin descriptor
118 * @return Index of port
120 uint32_t dio_gpio_pin_get_port_num(uint32_t pin_dsc);
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
128 gioPORT_t *dio_gpio_pin_get_port_base(uint32_t pin_dsc);
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
136 uint32_t *dio_gpio_pin_get_dsc(const char *pin_name, int len);
139 * Get value from GPIO pin
140 * @param[in] pin_dsc pin descriptor
141 * @return value read from specified gpio pin
143 uint32_t dio_gpio_pin_get_value(uint32_t pin_dsc);
146 * Set value to gpio pin
147 * @param[in] pin_dsc pin descriptor
148 * @param[in] value value to be assigned to the pin
150 void dio_gpio_pin_set_value(uint32_t pin_dsc, uint32_t value);
153 * Set pin direction to input
154 * @param[in] pin_dsc pin descriptor
157 int dio_gpio_pin_set_dir_in(uint32_t pin_dsc);
160 * Set pin direction to output
161 * @param[in] pin_dsc pin descriptor
164 int dio_gpio_pin_set_dir_out(uint32_t pin_dsc, uint32_t value);
168 * @param[in] pin_dsc pin descriptor
169 * @return 1 - output, 0 - input
171 int dio_gpio_pin_get_dir(uint32_t pin_dsc);
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
180 * PORT_CONF_MODE_PEN - pull resistor enable
181 * PORT_CONF_MODE_PDIS - pull resistor disable
184 uint32_t dio_gpio_pin_set_mode(uint32_t pin_dsc, uint32_t mode);
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
195 uint32_t dio_gpio_pin_set_od(uint32_t pin_dsc, uint32_t od);
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
204 * PORT_CONF_MODE_PU - pull up
205 * PORT_CONF_MODE_PD - pull down
207 * PORT_CONF_MODE_PEN - pull resistor enable
208 * PORT_CONF_MODE_PDIS - pull resistor disable
210 * PORT_CONF_DIR_IN - direction input
211 * PORT_CONF_DIR_OUT - direction output
213 * PORT_CONF_INIT_LOW - init value 0
214 * PORT_CONF_INIT_HIGH - init value 1
216 * PORT_CONF_FNC_GPIO - port function GPIO
217 * PORT_CONF_FNC_FNCX - port alternate function X
221 uint32_t dio_gpio_pin_set_config(uint32_t pin_dsc, uint32_t conf);
224 * Do the initial pin configuration according values in pin descriptor
225 * @param[in] pin_dsc pin descriptor
228 uint32_t dio_gpio_pin_configure(uint32_t pin_dsc);
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...
237 uint32_t dio_gpio_port_get_val(uint32_t *config, uint32_t num_val, uint32_t *values);
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...
246 uint32_t dio_gpio_port_set_val(uint32_t *config, uint32_t num_val, const uint32_t *values);
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
254 const dio_port_def_t *dio_port_get_map();
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
262 dio_port_desc_t *dio_port_get_dsc(const char *port_name, int len);
264 uint32_t dio_port_get_val_cnt(const dio_port_desc_t* port_desc);
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
273 uint32_t dio_spi_port_transfer_command(uint32_t *config, uint32_t num_bytes, const uint32_t *commands);