b5a495c19e65d061595c6d970edb6626a250edb9
[contrib/mraa.git] / include / mraa_internal_types.h
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
4  * Copyright (c) 2014 Intel Corporation.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #pragma once
27
28 #include "common.h"
29 #include "mraa.h"
30 #include "mraa_func.h"
31 #include "mraa_adv_func.h"
32
33 // Bionic does not implement pthread cancellation API
34 #ifndef __BIONIC__
35 #define HAVE_PTHREAD_CANCEL
36 #endif
37
38 // general status failures for internal functions
39 #define MRAA_PLATFORM_NO_INIT -3
40 #define MRAA_IO_SETUP_FAILURE -2
41 #define MRAA_NO_SUCH_IO -1
42
43 /**
44  * A structure representing a gpio pin.
45  */
46 struct _gpio {
47     /*@{*/
48     int pin; /**< the pin number, as known to the os. */
49     int phy_pin; /**< pin passed to clean init. -1 none and raw*/
50     int value_fp; /**< the file pointer to the value of the gpio */
51     void (* isr)(void *); /**< the interupt service request */
52     void *isr_args; /**< args return when interupt service request triggered */
53     pthread_t thread_id; /**< the isr handler thread id */
54     int isr_value_fp; /**< the isr file pointer on the value */
55 #ifndef HAVE_PTHREAD_CANCEL
56     int isr_control_pipe[2]; /**< a pipe used to interrupt the isr from polling the value fd*/
57 #endif
58     mraa_boolean_t isr_thread_terminating; /**< is the isr thread being terminated? */
59     mraa_boolean_t owner; /**< If this context originally exported the pin */
60     mraa_result_t (*mmap_write) (mraa_gpio_context dev, int value);
61     int (*mmap_read) (mraa_gpio_context dev);
62     mraa_adv_func_t* advance_func; /**< override function table */
63     /*@}*/
64 };
65
66 /**
67  * A structure representing a I2C bus
68  */
69 struct _i2c {
70     /*@{*/
71     int busnum; /**< the bus number of the /dev/i2c-* device */
72     int fh; /**< the file handle to the /dev/i2c-* device */
73     int addr; /**< the address of the i2c slave */
74     unsigned long funcs; /**< /dev/i2c-* device capabilities as per https://www.kernel.org/doc/Documentation/i2c/functionality */
75     void *handle; /**< generic handle for non-standard drivers that don't use file descriptors  */
76     mraa_adv_func_t* advance_func; /**< override function table */
77     /*@}*/
78 };
79
80 /**
81  * A structure representing the SPI device
82  */
83 struct _spi {
84     /*@{*/
85     int devfd;          /**< File descriptor to SPI Device */
86     uint32_t mode;      /**< Spi mode see spidev.h */
87     int clock;          /**< clock to run transactions at */
88     mraa_boolean_t lsb; /**< least significant bit mode */
89     unsigned int bpw;   /**< Bits per word */
90     mraa_adv_func_t* advance_func; /**< override function table */
91     /*@}*/
92 };
93
94 /**
95  * A structure representing a PWM pin
96  */
97 struct _pwm {
98     /*@{*/
99     int pin; /**< the pin number, as known to the os. */
100     int chipid; /**< the chip id, which the pwm resides */
101     int duty_fp; /**< File pointer to duty file */
102     int period;  /**< Cache the period to speed up setting duty */
103     mraa_boolean_t owner; /**< Owner of pwm context*/
104     mraa_adv_func_t* advance_func; /**< override function table */
105     /*@}*/
106 };
107
108 /**
109  * A structure representing a Analog Input Channel
110  */
111 struct _aio {
112     /*@{*/
113     unsigned int channel; /**< the channel as on board and ADC module */
114     int adc_in_fp; /**< File Pointer to raw sysfs */
115     int value_bit; /**< 10 bits by default. Can be increased if board */
116     mraa_adv_func_t* advance_func; /**< override function table */
117     /*@}*/
118 };
119
120 /**
121  * A structure representing a UART device
122  */
123 struct _uart {
124     /*@{*/
125     int index; /**< the uart index, as known to the os. */
126     const char* path; /**< the uart device path. */
127     int fd; /**< file descriptor for device. */
128     mraa_adv_func_t* advance_func; /**< override function table */
129     /*@}*/
130 };
131
132 /**
133  * A structure reprensting an IIO device
134  */
135 struct _iio {
136     int num; /**< IIO device number */
137     char* name; /**< IIO device name */
138 };
139
140 /**
141  * A bitfield representing the capabilities of a pin.
142  */
143 typedef struct {
144     /*@{*/
145     mraa_boolean_t valid:1;     /**< Is the pin valid at all */
146     mraa_boolean_t gpio:1;      /**< Is the pin gpio capable */
147     mraa_boolean_t pwm:1;       /**< Is the pin pwm capable */
148     mraa_boolean_t fast_gpio:1; /**< Is the pin fast gpio capable */
149     mraa_boolean_t spi:1;       /**< Is the pin spi capable */
150     mraa_boolean_t i2c:1;       /**< Is the pin i2c capable */
151     mraa_boolean_t aio:1;       /**< Is the pin analog input capable */
152     mraa_boolean_t uart:1;       /**< Is the pin uart capable */
153     /*@}*/
154 } mraa_pincapabilities_t;
155
156 /**
157  * A Structure representing a multiplexer and the required value
158  */
159 typedef struct {
160     /*@{*/
161     unsigned int pin;   /**< Raw GPIO pin id */
162     unsigned int value; /**< Raw GPIO value */
163     /*@}*/
164 } mraa_mux_t;
165
166 typedef struct {
167     mraa_boolean_t complex_pin:1;
168     mraa_boolean_t output_en:1;
169     mraa_boolean_t output_en_high:1;
170     mraa_boolean_t pullup_en:1;
171     mraa_boolean_t pullup_en_hiz:1;
172 } mraa_pin_cap_complex_t;
173
174 typedef struct {
175     /*@{*/
176     unsigned int pinmap; /**< sysfs pin */
177     unsigned int parent_id; /** parent chip id */
178     unsigned int mux_total; /** Numfer of muxes needed for operation of pin */
179     mraa_mux_t mux[6]; /** Array holding information about mux */
180     unsigned int output_enable; /** Output Enable GPIO, for level shifting */
181     unsigned int pullup_enable; /** Pull-Up enable GPIO, inputs */
182     mraa_pin_cap_complex_t complex_cap;
183     /*@}*/
184 } mraa_pin_t;
185
186 typedef struct {
187     /*@{*/
188     char mem_dev[32]; /**< Memory device to use /dev/uio0 etc */
189     unsigned int mem_sz; /** Size of memory to map */
190     unsigned int bit_pos; /** Position of value bit */
191     mraa_pin_t gpio; /** GPio context containing none mmap info */
192     /*@}*/
193 } mraa_mmap_pin_t;
194
195 /**
196  * A Structure representing a physical Pin.
197  */
198 typedef struct {
199     /*@{*/
200     char name[MRAA_PIN_NAME_SIZE]; /**< Pin's real world name */
201     mraa_pincapabilities_t capabilites; /**< Pin Capabiliites */
202     mraa_pin_t gpio; /**< GPIO structure */
203     mraa_pin_t pwm;  /**< PWM structure */
204     mraa_pin_t aio;  /**< Anaglog Pin */
205     mraa_mmap_pin_t mmap; /**< GPIO through memory */
206     mraa_pin_t i2c;  /**< i2c bus/pin */
207     mraa_pin_t spi;  /**< spi bus/pin */
208     mraa_pin_t uart;  /**< uart module/pin */
209     /*@}*/
210 } mraa_pininfo_t;
211
212 /**
213  * A Structure representing the physical properties of a i2c bus.
214  */
215 typedef struct {
216     /*@{*/
217     unsigned int bus_id; /**< ID as exposed in the system */
218     unsigned int scl; /**< i2c SCL */
219     unsigned int sda; /**< i2c SDA */
220     // mraa_drv_api_t drv_type; /**< Driver type */
221     /*@}*/
222 } mraa_i2c_bus_t;
223
224 /**
225  * A Structure representing the physical properties of a spi bus.
226  */
227 typedef struct {
228     /*@{*/
229     unsigned int bus_id; /**< The Bus ID as exposed to the system. */
230     unsigned int slave_s; /**< Slave select */
231     mraa_boolean_t three_wire; /**< Is the bus only a three wire system */
232     unsigned int sclk; /**< Serial Clock */
233     unsigned int mosi; /**< Master Out, Slave In. */
234     unsigned int miso; /**< Master In, Slave Out. */
235     unsigned int cs; /**< Chip Select, used when the board is a spi slave */
236     /*@}*/
237 } mraa_spi_bus_t;
238
239 /**
240  * A Structure representing a uart device.
241  */
242 typedef struct {
243     /*@{*/
244     unsigned int index; /**< ID as exposed in the system */
245     int rx; /**< uart rx */
246     int tx; /**< uart tx */
247     const char* device_path; /**< To store "/dev/ttyS1" for example */
248     /*@}*/
249 } mraa_uart_dev_t;
250
251 /**
252  * A Structure representing a platform/board.
253  */
254
255 typedef struct _board_t {
256     /*@{*/
257     unsigned int phy_pin_count; /**< The Total IO pins on board */
258     unsigned int gpio_count; /**< GPIO Count */
259     unsigned int aio_count;  /**< Analog side Count */
260     unsigned int i2c_bus_count; /**< Usable i2c Count */
261     mraa_i2c_bus_t  i2c_bus[12]; /**< Array of i2c */
262     unsigned int def_i2c_bus; /**< Position in array of default i2c bus */
263     unsigned int spi_bus_count; /**< Usable spi Count */
264     mraa_spi_bus_t spi_bus[12];       /**< Array of spi */
265     unsigned int def_spi_bus; /**< Position in array of defult spi bus */
266     unsigned int adc_raw; /**< ADC raw bit value */
267     unsigned int adc_supported; /**< ADC supported bit value */
268     unsigned int def_uart_dev; /**< Position in array of defult uart */
269     unsigned int uart_dev_count; /**< Usable spi Count */
270     mraa_uart_dev_t uart_dev[6]; /**< Array of UARTs */
271     mraa_boolean_t no_bus_mux; /**< i2c/spi/adc/pwm/uart bus muxing setup not required */
272     int pwm_default_period; /**< The default PWM period is US */
273     int pwm_max_period; /**< Maximum period in us */
274     int pwm_min_period; /**< Minimum period in us */
275     mraa_platform_t platform_type; /**< Platform type */
276     const char* platform_name; /**< Platform Name pointer */
277     mraa_pininfo_t* pins;     /**< Pointer to pin array */
278     mraa_adv_func_t* adv_func;    /**< Pointer to advanced function disptach table */
279     struct _board_t* sub_platform;     /**< Pointer to sub platform */
280     struct _iio* iio_devices; /**< Pointer to IIO devices */
281     uint8_t iio_device_count; /**< IIO device count */
282     /*@}*/
283 } mraa_board_t;
284
285