internals.md: add more internals documentation
[contrib/mraa.git] / docs / internals.md
1 libmraa Internals                       {#internals}
2 =================
3
4 For building see @ref building. This will describe the general internal build
5 of libmraa and will be useful to developers who'd like to understand more of
6 how libmraa works or who'd like to add additional platforms. The internals will
7 deal with the C API as that is the low level API which libmraa is built around.
8 Note that C++ is simply a header only wrapper of the C API.
9
10 libmraa has the philosophy that the board mapping is what we typically use in
11 the API with the execption of i2c/spi bus numbering as they are typically not
12 labelled on boards and so we use the kernel numbering scheme. Whilst this can
13 confuse some, it's typically not an issue as platforms rarely expose more than
14 one of these for user use and so when this is the case, libmraa will always use
15 the bus in the pinmapper. For example edison uses i2c #6 but since there is
16 only one, libmraa will try to be helpful and everything is treated as 6 when
17 doing a mraa_i2c_init and so when this is the case, libmraa will always use the
18 bus in the pinmapper. For example edison uses i2c #6 but since there is only
19 one, libmraa will try to be helpful and everything is treated as 6 when doing a
20 mraa_i2c_init(). The _raw functions will override the pinmapper and can be
21 accessed without a valid board configuration. This can be helpful either in
22 development of platform configurations for mraa or when modifying kernels
23 etc... The mechanism is used heavily internaly.
24
25 In libmraa, all code is split into 7 modules, src/{i2c, spi, gpio, uart, pwm,
26 aio and common}. These should be fairly self explanatory in goals/purpose but a
27 few work in different ways. Public APIs are stored in api/ and internal headers
28 are in include/
29
30 ### Logging ###
31
32 Logging is now done purely in syslog(). Note that on platforms running systemd
33 journald will intercept syslog(3) calls and log to the journal instead. You can
34 set the log mask by using mraa_set_log_level(). Doing a DEBUG build of libmraa
35 will also cause the DEBUG macro to be defined which will cause the syslog mask
36 to be unset.
37
38 ### Contexts ###
39
40 libmraa uses contexts to store all information, this context cannot be accessed
41 by the user and so it's layout can and may be changed without warning to users.
42 If an init() function fails it will return NULL and further calls with this
43 context will lead to undefined behaviour.
44
45 ### Pinmapper ###
46
47 The mraa_board_t is defined in mraa/common.h. It's a mostly static structure
48 initialised during mraa_init(). The pinmap file in
49 src/{arch}/{manufacturer}_{boardname}_{revision}.c then fills this array. It's
50 also where platform hooks can be defined, functions that will be run at various
51 'hook' points in the code.
52
53 The mraa_pininfo_t structure needs to be set for the board pincount (set in a
54 macro in the platform configuration header. Every pin will have a
55 mraa_pincapabilities_t which will define what it can do. The doxygen doc
56 explains how this works but it's essentially a bitfield which needs to be set
57 for every capability the pin can have. Gpios can have multiple muxes which will
58 be set at the gpio init before it can be toggled.
59
60 ### i2c ###
61
62 I2c from userspace in GNU/Linux is handled by character devices handled by the
63 kernel driver i2c-dev. For more details the i2c/dev-interface documentation
64 file in the kernel is the place to go.
65
66 In libmraa, we re-use part of a library - libi2c from RoadNarrows -
67 i2c/smbus.c. This library simply makes it easier for us to handle the error
68 conditions that can arrise when writing on i2c buses. Essentially the API is
69 fairly simple consisting of writes & reads.
70
71 Careful - on alot of platforms i2cdetect will often crash, for finding your i2c
72 addresses please look at your sensors datasheet!
73
74 ### spi ###
75
76 Mraa deals exclusively with spidev, so when we say bus we really mean bus +
77 chip select from spidev. Spi(0) could lead to spidev5.1 and Spi(1) to
78 spidev5.2. Typically on a micro using a random gpio as a chip select works
79 well, and on some platforms if one is careful with threads this can work well
80 with mraa. However when a kernel module shares the same bus as spidev (but on a
81 different CS) this behaviour is *very* dangerous. Platforms such as galileo
82 gen2 & edison + arduino breakout work this way. Mraa will not help you in using
83 a non HW chip select, do so at your own peril!
84
85 ### gpio ###
86
87 GPIO is probably the most complicated and odd module in libmraa. It is based on
88 the gpiolib kernel driver framework which uses sysfs. There is a lot of good
89 documentation in gpio/sysfs.txt in the kernel docs.
90
91 The main issue is that gpios on hobbyist boards typically come with a number of
92 muxers or level shifters and are often mapped in crazy ways. libmraa's goal is
93 to make the label on your board match the API :) We hope that pleases you.
94
95 Because boards are very different we use alot of platform hooks (@ref hooks) to
96 make the initialisation work on all platforms. The hope is that simple
97 platforms with no level shifters or expanders will work with just the pinmap
98 definition.
99
100 GPIOs are typically interfaced via sysfs because that's easier for us but we
101 can also work with fast gpio. This is typically preffered to do mmap gpio
102 access. This is however trickier and typically relies on lots of platform
103 hooks. We do support by default to go hit /dev/mem or another device at
104 specific addresses to toggle gpios which is how mmap access works on some
105 boards.
106
107 Note that in Linux gpios are numbered from ARCH_NR_GPIOS down. This means that
108 if ARCH_NR_GPIOS is changed, the gpio numbering will change. In 3.18+ the
109 default changed from 256 to 512, sadly the value cannot be viewed from
110 userspace so we rely on the kernel version to extrapolate the likely value.
111
112 ### uart ###
113
114 libmraa does not support UART/serial as there are many good libraries that do
115 this already. In the future we may wrap or use one. However the class exists to
116 set the pinmapper correctly for uart to work on some platforms.
117
118 ### pwm ###
119
120 ### aio ###
121
122 AIO pins are numbered after GPIO pins. This means that on arduino style boards
123 pin 14 is A0. Typically mraa will only support an ADC if a platform ships with
124 one and has a good kernel module for it. extra i2c/spi ADCs can be supported
125 via something like UPM but are unlikely to receive support in mraa at the moment.
126
127 Note that giving mraa_aio_init(0) will literally query the pinmapper for
128 board->gpio_count + 0 so you must place your aio pins after gpio_count. This is
129 the default behaviour but can of course be overriden by advance function
130 pointers. Whilst maybe not the sanest of defaults, most of the hobbyist boards
131 we deal with follow a similar naming pattern to arduino or have no ADC so for
132 now we have considered this sensible.
133
134 ### Initialisation ###
135
136 mraa_init() needs to be called in order to initialise the platform files or
137 'pinmap'. Because calling this is tedious libmraa uses a C constructor to run
138 mraa_init on library load. This means that it is not possible to stop this
139 running and all functino calls like mraa_set_log_level() will not work during
140 mraa_init(). This feature is supported by most sane compilers & libcs but you
141 can turn off CTORS in uclibc, though I've yet to find a configuration with
142 someone doing that. mraa_init() can be called multiple times if you feel like
143 being 'safe'.
144
145 In the SWIG modulse mraa_init() is called during the %init stage of the module
146 loading. This is simply to avoid mraa_init() running 'too' early, though I've
147 never seen an issue in running it in a CTOR.
148
149 ### SWIG ###
150
151 At the time when libmraa was created (still the case?) the only - working -
152 API/wrapper generation tool that supported nodejs was SWIG. For more general
153 information on swig please see the swig documentation.
154
155 The src/{javascript, python} & src/mraa.i folders contain all the files for the
156 swig generation. The C++ headers in api/mraa/ are given as input sources to
157 SWIG. SWIG modules do not link to libmraa (although maybe that would be a good
158 idea...)
159
160 Typemaps are used heavily to map uint8_t* pointers to bytearrays and
161 node_buffers. These are native python & nodejs types that represent uint8_t
162 data the best and are very well supported in both languages. Argument
163 conversions and memory allocations are performed so the performance of using
164 these functions compared to the C/C++ equivalent will likely be a little lower,
165 however it is much more natural than using carrays.i typemap library.
166
167 ### NPM ###
168
169 mraa is published on NPM, there is a target to prebuild a mraa src tarball that
170 can be built with node-gyp. The way this works is to use the mraa_LIB_SRCS
171 array to generate a binding.gyp file from the skeleton binding.gyp.cmake in
172 src/javascript. Because we don't expect most NPM users to have SWIG we
173 precompile the src/mraajsJAVASCRIPT_wrap.cxx. The src/version.c is already
174 known since this is a static tarball so we write that too. These files are
175 placed not in a build/ dir but in the main mraa dir. You can then tar the dir
176 up and send it to NPM. This is done automatically on every commit by our
177 automated build system.
178