index.md: add edison link
[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().
21
22 In libmraa, all code is split into 7 modules, src/{i2c, spi, gpio, uart, pwm,
23 aio and common}. These should be fairly self explanatory in goals/purpose but a
24 few work in different ways. Public APIs are stored in api/ and internal headers
25 are in include/
26
27 ### Logging ###
28
29 Logging is now done purely in syslog(). Note that on platforms running systemd
30 journald will intercept syslog(3) calls and log to the journal instead. You can
31 set the log mask by using mraa_set_log_level(). Doing a DEBUG build of libmraa
32 will also cause the DEBUG macro to be defined which will cause the syslog mask
33 to be unset.
34
35 ### Contexts ###
36
37 libmraa uses contexts to store all information, this context cannot be accessed
38 by the user and so it's layout can and may be changed without warning to users.
39 If an init() function fails it will return NULL and further calls with this
40 context will lead to undefined behaviour.
41
42 ### Pinmapper ###
43
44 The mraa_board_t is defined in mraa/common.h. It's a mostly static structure
45 initialised during mraa_init(). The pinmap file in
46 src/{manufacturer}_{boardname}_{revision}.c then fills this array. It's also
47 where platform hooks can be defined, functions that will be run at various
48 'hook' points in the code.
49
50 The mraa_pininfo_t structure needs to be set for the board pincount (set in a
51 macro in the platform configuration header. Every pin will have a
52 mraa_pincapabilities_t which will define what it can do. The doxygen doc
53 explains how this works but it's essentially a bitfield which needs to be set
54 for every capability the pin can have. Gpios can have multiple muxes which will
55 be set at the gpio init before it can be toggled.
56
57 ### i2c ###
58
59 I2c from userspace in GNU/Linux is handled by character devices handled by the
60 kernel driver i2c-dev. For more details the i2c/dev-interface documentation
61 file in the kernel is the place to go.
62
63 In libmraa, we re-use part of a library - libi2c from RoadNarrows -
64 i2c/smbus.c. This library simply makes it easier for us to handle the error
65 conditions that can arrise when writing on i2c buses. Essentially the API is
66 fairly simple consisting of writes & reads.
67
68 Careful - on alot of platforms i2cdetect will often crash, for finding your i2c
69 addresses please look at your sensors datasheet!
70
71 ### spi ###
72
73 ### gpio ###
74
75 GPIO is probably the most complicated and odd module in libmraa. It is based on
76 the gpiolib kernel driver framework which uses sysfs. There is a lot of good
77 documentation in gpio/sysfs.txt in the kernel docs.
78
79 The main issue is that gpios on hobbyist boards typically come with a number of
80 muxers or level shifters and are often mapped in crazy ways. libmraa's goal is
81 to make the label on your board match the API :) We hope that pleases you.
82
83 Because boards are very different we use alot of platform hooks (@ref hooks) to
84 make the initialisation work on all platforms. The hope is that simple
85 platforms with no level shifters or expanders will work with just the pinmap
86 definition.
87
88 GPIOs are typically interfaced via sysfs because that's easier for us but we
89 can also work with fast gpio. This is typically preffered to do mmap gpio
90 access. This is however trickier and typically relies on lots of platform
91 hooks. We do support by default to go hit /dev/mem or another device at
92 specific addresses to toggle gpios which is how mmap access works on some
93 boards.
94
95 ### uart ###
96
97 libmraa does not support UART/serial as there are many good libraries that do
98 this already. In the future we may wrap or use one. However the class exists to
99 set the pinmapper correctly for uart to work on some platforms.
100
101 ### pwm ###
102
103 ### aio ###
104
105 ### Initialisation ###
106
107 mraa_init() needs to be called in order to initialise the platform files or
108 'pinmap'. Because calling this is tedious libmraa uses a C constructor to run
109 mraa_init on library load. This means that it is not possible to stop this
110 running and all functino calls like mraa_set_log_level() will not work during
111 mraa_init(). This feature is supported by most sane compilers & libcs but you
112 can turn off CTORS in uclibc, though I've yet to find a configuration with
113 someone doing that. mraa_init() can be called multiple times if you feel like
114 being 'safe'.
115
116 In the SWIG modulse mraa_init() is called during the %init stage of the module
117 loading. This is simply to avoid mraa_init() running 'too' early, though I've
118 never seen an issue in running it in a CTOR.
119
120 ### SWIG ###
121
122 At the time when libmraa was created the only - working - API/wrapper
123 generation tool that supported nodejs was SWIG. For more general information on
124 swig please see the swig documentation.
125
126 The src/{javascript, python} & src/mraa.i folders contain all the files for the
127 swig generation. The C++ headers in api/mraa/ are given as input sources to
128 SWIG. SWIG modules do not link to libmraa (although maybe that would be a good
129 idea...)
130