clang-format: run clang-format on C/C++ code
[contrib/mraa.git] / src / arm / raspberry_pi.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Author: Michael Ring <mail@michael-ring.org>
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 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #include <mraa/common.h>
30
31 #include "common.h"
32 #include "arm/raspberry_pi.h"
33
34 #define PLATFORM_NAME_RASPBERRY_PI_B_REV_1 "Raspberry Pi Model B Rev 1"
35 #define PLATFORM_NAME_RASPBERRY_PI_A_REV_2 "Raspberry Pi Model A Rev 2"
36 #define PLATFORM_NAME_RASPBERRY_PI_B_REV_2 "Raspberry Pi Model B Rev 2"
37 #define PLATFORM_NAME_RASPBERRY_PI_B_PLUS_REV_1 "Raspberry Pi Model B+ Rev 1"
38 #define PLATFORM_NAME_RASPBERRY_PI_COMPUTE_MODULE_REV_1 "Raspberry Pi Compute Module Rev 1"
39 #define PLATFORM_NAME_RASPBERRY_PI_A_PLUS_REV_1 "Raspberry Pi Model A+ Rev 1"
40 #define PLATFORM_NAME_RASPBERRY_PI2_B_REV_1 "Raspberry Pi 2 Model B Rev 1"
41 #define PLATFORM_RASPBERRY_PI_B_REV_1 1
42 #define PLATFORM_RASPBERRY_PI_A_REV_2 2
43 #define PLATFORM_RASPBERRY_PI_B_REV_2 3
44 #define PLATFORM_RASPBERRY_PI_B_PLUS_REV_1 4
45 #define PLATFORM_RASPBERRY_PI_COMPUTE_MODULE_REV_1 5
46 #define PLATFORM_RASPBERRY_PI_A_PLUS_REV_1 6
47 #define PLATFORM_RASPBERRY_PI2_B_REV_1 7
48 #define MMAP_PATH "/dev/mem"
49 #define BCM2835_PERI_BASE 0x20000000
50 #define BCM2835_GPIO_BASE (BCM2835_PERI_BASE + 0x200000)
51 #define BCM2836_PERI_BASE 0x3f000000
52 #define BCM2836_GPIO_BASE (BCM2836_PERI_BASE + 0x200000)
53 #define BCM2835_BLOCK_SIZE (4 * 1024)
54 #define BCM2836_BLOCK_SIZE (4 * 1024)
55 #define BCM283X_GPSET0 0x001c
56 #define BCM283X_GPCLR0 0x0028
57 #define BCM2835_GPLEV0 0x0034
58 #define MAX_SIZE 64
59
60 // MMAP
61 static uint8_t* mmap_reg = NULL;
62 static int mmap_fd = 0;
63 static int mmap_size;
64 static unsigned int mmap_count = 0;
65 static int platform_detected = 0;
66
67 mraa_result_t
68 mraa_raspberry_pi_spi_init_pre(int index)
69 {
70     char devpath[MAX_SIZE];
71     sprintf(devpath, "/dev/spidev%u.0", plat->spi_bus[index].bus_id);
72     if (!mraa_file_exist(devpath)) {
73         syslog(LOG_ERR, "spi: Device not initialized");
74         syslog(LOG_ERR, "spi: If you run a kernel >=3.18 then you will have to add dtparam=spi=on "
75                         "to /boot/config.txt and reboot");
76         syslog(LOG_INFO, "spi: trying modprobe for spi-bcm2708");
77         system("modprobe spi-bcm2708 >/dev/null 2>&1");
78         system("modprobe spi_bcm2708 >/dev/null 2>&1");
79         if (!mraa_file_exist(devpath)) {
80             return MRAA_ERROR_NO_RESOURCES;
81         }
82     }
83     return MRAA_SUCCESS;
84 }
85
86 mraa_result_t
87 mraa_raspberry_pi_i2c_init_pre(unsigned int bus)
88 {
89     char devpath[MAX_SIZE];
90     sprintf(devpath, "/dev/i2c-%u", bus);
91     if (!mraa_file_exist(devpath)) {
92         syslog(LOG_INFO, "spi: trying modprobe for i2c-bcm2708 & i2c-dev");
93         system("modprobe i2c-bcm2708 >/dev/null 2>&1");
94         system("modprobe i2c-dev >/dev/null 2>&1");
95         system("modprobe i2c_bcm2708 >/dev/null 2>&1");
96         system("modprobe i2c_dev >/dev/null 2>&1");
97     }
98     if (!mraa_file_exist(devpath)) {
99         syslog(LOG_ERR, "i2c: Device not initialized");
100         if (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_1) {
101             syslog(LOG_ERR, "i2c: If you run a kernel >=3.18 then you will have to add "
102                             "dtparam=i2c0=on to /boot/config.txt and reboot");
103         } else {
104             syslog(LOG_ERR, "i2c: If you run a kernel >=3.18 then you will have to add "
105                             "dtparam=i2c1=on to /boot/config.txt and reboot");
106         }
107         return MRAA_ERROR_NO_RESOURCES;
108     }
109     return MRAA_SUCCESS;
110 }
111
112 mraa_result_t
113 mraa_raspberry_pi_mmap_write(mraa_gpio_context dev, int value)
114 {
115     volatile uint32_t* addr;
116     if (value) {
117         *(volatile uint32_t*) (mmap_reg + BCM283X_GPSET0 + (dev->pin / 32) * 4) =
118         (uint32_t)(1 << (dev->pin % 32));
119     } else {
120         *(volatile uint32_t*) (mmap_reg + BCM283X_GPCLR0 + (dev->pin / 32) * 4) =
121         (uint32_t)(1 << (dev->pin % 32));
122     }
123     return MRAA_SUCCESS;
124 }
125
126 static mraa_result_t
127 mraa_raspberry_pi_mmap_unsetup()
128 {
129     if (mmap_reg == NULL) {
130         syslog(LOG_ERR, "raspberry mmap: null register cant unsetup");
131         return MRAA_ERROR_INVALID_RESOURCE;
132     }
133     munmap(mmap_reg, mmap_size);
134     mmap_reg = NULL;
135     if (close(mmap_fd) != 0) {
136         return MRAA_ERROR_INVALID_RESOURCE;
137     }
138     return MRAA_SUCCESS;
139 }
140
141 int
142 mraa_raspberry_pi_mmap_read(mraa_gpio_context dev)
143 {
144     uint32_t value = *(volatile uint32_t*) (mmap_reg + BCM2835_GPLEV0 + (dev->pin / 32) * 4);
145     if (value & (uint32_t)(1 << (dev->pin % 32))) {
146         return 1;
147     }
148     return 0;
149 }
150
151 mraa_result_t
152 mraa_raspberry_pi_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
153 {
154     if (dev == NULL) {
155         syslog(LOG_ERR, "raspberry mmap: context not valid");
156         return MRAA_ERROR_INVALID_HANDLE;
157     }
158
159     if (en == 0) {
160         if (dev->mmap_write == NULL && dev->mmap_read == NULL) {
161             syslog(LOG_ERR, "raspberry mmap: can't disable disabled mmap gpio");
162             return MRAA_ERROR_INVALID_PARAMETER;
163         }
164         dev->mmap_write = NULL;
165         dev->mmap_read = NULL;
166         mmap_count--;
167         if (mmap_count == 0) {
168             return mraa_raspberry_pi_mmap_unsetup();
169         }
170         return MRAA_SUCCESS;
171     }
172
173     if (dev->mmap_write != NULL && dev->mmap_read != NULL) {
174         syslog(LOG_ERR, "raspberry mmap: can't enable enabled mmap gpio");
175         return MRAA_ERROR_INVALID_PARAMETER;
176     }
177
178     // Might need to make some elements of this thread safe.
179     // For example only allow one thread to enter the following block
180     // to prevent mmap'ing twice.
181     if (mmap_reg == NULL) {
182         if ((mmap_fd = open(MMAP_PATH, O_RDWR)) < 0) {
183             syslog(LOG_ERR, "raspberry map: unable to open resource0 file");
184             return MRAA_ERROR_INVALID_HANDLE;
185         }
186
187         if (platform_detected == PLATFORM_RASPBERRY_PI2_B_REV_1) {
188             mmap_reg = (uint8_t*) mmap(NULL, BCM2836_BLOCK_SIZE, PROT_READ | PROT_WRITE,
189                                        MAP_FILE | MAP_SHARED, mmap_fd, BCM2836_GPIO_BASE);
190         } else {
191             mmap_reg = (uint8_t*) mmap(NULL, BCM2835_BLOCK_SIZE, PROT_READ | PROT_WRITE,
192                                        MAP_FILE | MAP_SHARED, mmap_fd, BCM2835_GPIO_BASE);
193         }
194         if (mmap_reg == MAP_FAILED) {
195             syslog(LOG_ERR, "raspberry mmap: failed to mmap");
196             mmap_reg = NULL;
197             close(mmap_fd);
198             return MRAA_ERROR_NO_RESOURCES;
199         }
200     }
201     dev->mmap_write = &mraa_raspberry_pi_mmap_write;
202     dev->mmap_read = &mraa_raspberry_pi_mmap_read;
203     mmap_count++;
204
205     return MRAA_SUCCESS;
206 }
207
208 mraa_board_t*
209 mraa_raspberry_pi()
210 {
211     mraa_board_t* b = (mraa_board_t*) malloc(sizeof(mraa_board_t));
212     if (b == NULL) {
213         return NULL;
214     }
215
216     size_t len = 100;
217     char* line = malloc(len);
218
219     FILE* fh = fopen("/proc/cpuinfo", "r");
220     if (fh != NULL) {
221         while (getline(&line, &len, fh) != -1) {
222             if (strncmp(line, "Revision", 8) == 0) {
223                 if (strstr(line, "0002") || strstr(line, "0003")) {
224                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_1;
225                     platform_detected = PLATFORM_RASPBERRY_PI_B_REV_1;
226                     b->phy_pin_count = MRAA_RASPBERRY_PI_B_REV_1_PINCOUNT;
227                 } else if (strstr(line, "0004") || strstr(line, "0005") || strstr(line, "0006") ||
228                            strstr(line, "000d") || strstr(line, "000e") || strstr(line, "000f")) {
229                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_2;
230                     platform_detected = PLATFORM_RASPBERRY_PI_B_REV_2;
231                     b->phy_pin_count = MRAA_RASPBERRY_PI_AB_REV_2_PINCOUNT;
232                 } else if (strstr(line, "0007") || strstr(line, "0008") || strstr(line, "0009")) {
233                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_A_REV_2;
234                     platform_detected = PLATFORM_RASPBERRY_PI_A_REV_2;
235                     b->phy_pin_count = MRAA_RASPBERRY_PI_AB_REV_2_PINCOUNT;
236                 } else if (strstr(line, "0010")) {
237                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_PLUS_REV_1;
238                     platform_detected = PLATFORM_RASPBERRY_PI_B_PLUS_REV_1;
239                     b->phy_pin_count = MRAA_RASPBERRY_PI_AB_PLUS_PINCOUNT;
240                 } else if (strstr(line, "0011")) {
241                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_COMPUTE_MODULE_REV_1;
242                     platform_detected = PLATFORM_RASPBERRY_PI_COMPUTE_MODULE_REV_1;
243                     b->phy_pin_count = MRAA_RASPBERRY_PI_COMPUTE_MODULE_PINCOUNT;
244                 } else if (strstr(line, "0012")) {
245                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_A_PLUS_REV_1;
246                     platform_detected = PLATFORM_RASPBERRY_PI_A_PLUS_REV_1;
247                     b->phy_pin_count = MRAA_RASPBERRY_PI_AB_PLUS_PINCOUNT;
248                 } else if (strstr(line, "a01041")) {
249                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI2_B_REV_1;
250                     platform_detected = PLATFORM_RASPBERRY_PI2_B_REV_1;
251                     b->phy_pin_count = MRAA_RASPBERRY_PI2_B_REV_1_PINCOUNT;
252                 } else {
253                     b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_1;
254                     platform_detected = PLATFORM_RASPBERRY_PI_B_REV_1;
255                     b->phy_pin_count = MRAA_RASPBERRY_PI_B_REV_1_PINCOUNT;
256                 }
257             }
258         }
259         fclose(fh);
260     }
261     free(line);
262
263     b->aio_count = 0;
264     b->adc_raw = 0;
265     b->adc_supported = 0;
266     b->pwm_default_period = 500;
267     b->pwm_max_period = 2147483;
268     b->pwm_min_period = 1;
269
270     b->pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * b->phy_pin_count);
271
272     advance_func->spi_init_pre = &mraa_raspberry_pi_spi_init_pre;
273     advance_func->i2c_init_pre = &mraa_raspberry_pi_i2c_init_pre;
274     advance_func->gpio_mmap_setup = &mraa_raspberry_pi_mmap_setup;
275
276     strncpy(b->pins[0].name, "INVALID", 8);
277     b->pins[0].capabilites = (mraa_pincapabilities_t){ 0, 0, 0, 0, 0, 0, 0, 0 };
278
279     strncpy(b->pins[1].name, "3V3", 8);
280     b->pins[1].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
281
282     strncpy(b->pins[2].name, "5V", 8);
283     b->pins[2].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
284
285     strncpy(b->pins[3].name, "SDA0", 8);
286     b->pins[3].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 1, 0, 0 };
287     b->pins[3].gpio.pinmap = 2;
288     b->pins[3].gpio.mux_total = 0;
289     b->pins[3].i2c.pinmap = 0;
290     b->pins[3].i2c.mux_total = 0;
291
292     strncpy(b->pins[4].name, "5V", 8);
293     b->pins[4].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
294
295     strncpy(b->pins[5].name, "SCL0", 8);
296     b->pins[5].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 1, 0, 0 };
297     b->pins[5].gpio.pinmap = 3;
298     b->pins[5].gpio.mux_total = 0;
299     b->pins[5].i2c.pinmap = 0;
300     b->pins[5].i2c.mux_total = 0;
301
302     strncpy(b->pins[6].name, "GND", 8);
303     b->pins[6].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
304
305     strncpy(b->pins[7].name, "GPIO4", 8);
306     b->pins[7].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
307     b->pins[7].gpio.pinmap = 4;
308     b->pins[7].gpio.mux_total = 0;
309
310     strncpy(b->pins[8].name, "UART_TX", 8);
311     b->pins[8].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 1 };
312     b->pins[8].gpio.pinmap = 14;
313     b->pins[8].gpio.mux_total = 0;
314     b->pins[8].uart.parent_id = 0;
315     b->pins[8].uart.mux_total = 0;
316
317     strncpy(b->pins[9].name, "GND", 8);
318     b->pins[9].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
319
320     strncpy(b->pins[10].name, "UART_RX", 8);
321     b->pins[10].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 1 };
322     b->pins[10].gpio.pinmap = 15;
323     b->pins[10].gpio.mux_total = 0;
324     b->pins[10].uart.parent_id = 0;
325     b->pins[10].uart.mux_total = 0;
326
327     strncpy(b->pins[11].name, "GPIO17", 8);
328     b->pins[11].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
329     b->pins[11].gpio.pinmap = 17;
330     b->pins[11].gpio.mux_total = 0;
331
332     strncpy(b->pins[12].name, "GPIO18", 8);
333     b->pins[12].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
334     b->pins[12].gpio.pinmap = 18;
335     b->pins[12].gpio.mux_total = 0;
336
337     if (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_1) {
338         strncpy(b->pins[13].name, "GPIO21", 8);
339         b->pins[13].gpio.pinmap = 21;
340     } else {
341         strncpy(b->pins[13].name, "GPIO27", 8);
342         b->pins[13].gpio.pinmap = 27;
343     }
344     b->pins[13].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
345     b->pins[13].gpio.mux_total = 0;
346
347     strncpy(b->pins[14].name, "GND", 8);
348     b->pins[14].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
349
350     strncpy(b->pins[15].name, "GPIO22", 8);
351     b->pins[15].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
352     b->pins[15].gpio.pinmap = 22;
353     b->pins[15].gpio.mux_total = 0;
354
355     strncpy(b->pins[16].name, "GPIO23", 8);
356     b->pins[16].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
357     b->pins[16].gpio.pinmap = 23;
358     b->pins[16].gpio.mux_total = 0;
359
360     strncpy(b->pins[17].name, "3V3", 8);
361     b->pins[17].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
362
363     strncpy(b->pins[18].name, "GPIO24", 8);
364     b->pins[18].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
365     b->pins[18].gpio.pinmap = 24;
366     b->pins[18].gpio.mux_total = 0;
367
368     strncpy(b->pins[19].name, "SPI_MOSI", 8);
369     b->pins[19].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 1, 0, 0, 0 };
370     b->pins[19].gpio.pinmap = 10;
371     b->pins[19].gpio.mux_total = 0;
372     b->pins[19].spi.pinmap = 0;
373     b->pins[19].spi.mux_total = 0;
374
375     strncpy(b->pins[20].name, "GND", 8);
376     b->pins[20].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
377
378     strncpy(b->pins[21].name, "SPI_MISO", 8);
379     b->pins[21].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 1, 0, 0, 0 };
380     b->pins[21].gpio.pinmap = 9;
381     b->pins[21].gpio.mux_total = 0;
382     b->pins[21].spi.pinmap = 0;
383     b->pins[21].spi.mux_total = 0;
384
385     strncpy(b->pins[22].name, "GPIO25", 8);
386     b->pins[22].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
387     b->pins[22].gpio.pinmap = 25;
388     b->pins[22].gpio.mux_total = 0;
389
390     strncpy(b->pins[23].name, "SPI_CLK", 8);
391     b->pins[23].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 1, 0, 0, 0 };
392     b->pins[23].gpio.pinmap = 11;
393     b->pins[23].gpio.mux_total = 0;
394     b->pins[23].spi.pinmap = 0;
395     b->pins[23].spi.mux_total = 0;
396
397     strncpy(b->pins[24].name, "SPI_CS0", 8);
398     b->pins[24].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 1, 0, 0, 0 };
399     b->pins[24].gpio.pinmap = 8;
400     b->pins[24].gpio.mux_total = 0;
401     b->pins[24].spi.pinmap = 0;
402     b->pins[24].spi.mux_total = 0;
403
404     strncpy(b->pins[25].name, "GND", 8);
405     b->pins[25].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
406
407     strncpy(b->pins[26].name, "SPI_CS1", 8);
408     b->pins[26].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 1, 0, 0, 0 };
409     b->pins[26].gpio.pinmap = 7;
410     b->pins[26].gpio.mux_total = 0;
411     b->pins[26].spi.pinmap = 0;
412     b->pins[26].spi.mux_total = 0;
413
414     if ((platform_detected == PLATFORM_RASPBERRY_PI_A_REV_2) ||
415         (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_2)) {
416         strncpy(b->pins[27].name, "5V", 8);
417         b->pins[27].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
418
419         strncpy(b->pins[28].name, "3V3", 8);
420         b->pins[28].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
421
422         strncpy(b->pins[29].name, "GPIO8", 8);
423         b->pins[29].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
424         b->pins[29].gpio.pinmap = 8;
425         b->pins[29].gpio.mux_total = 0;
426
427         strncpy(b->pins[30].name, "GPIO9", 8);
428         b->pins[30].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
429         b->pins[30].gpio.pinmap = 9;
430         b->pins[30].gpio.mux_total = 0;
431
432         strncpy(b->pins[31].name, "GPIO10", 8);
433         b->pins[31].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
434         b->pins[31].gpio.pinmap = 10;
435         b->pins[31].gpio.mux_total = 0;
436
437         strncpy(b->pins[32].name, "GPIO11", 8);
438         b->pins[32].gpio.pinmap = 11;
439         b->pins[32].gpio.mux_total = 0;
440         b->pins[32].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
441
442         strncpy(b->pins[33].name, "GND", 8);
443         b->pins[33].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
444
445         strncpy(b->pins[34].name, "GND", 8);
446         b->pins[34].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
447     }
448
449     // BUS DEFINITIONS
450     b->i2c_bus_count = 1;
451     b->def_i2c_bus = 0;
452     if (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_1)
453         b->i2c_bus[0].bus_id = 0;
454     else
455         b->i2c_bus[0].bus_id = 1;
456     b->i2c_bus[0].sda = 3;
457     b->i2c_bus[0].scl = 5;
458
459     b->spi_bus_count = 1;
460     b->def_spi_bus = 0;
461     b->spi_bus[0].bus_id = 0;
462     b->spi_bus[0].slave_s = 0;
463     b->spi_bus[0].cs = 24;
464     b->spi_bus[0].mosi = 19;
465     b->spi_bus[0].miso = 21;
466     b->spi_bus[0].sclk = 23;
467
468     b->uart_dev_count = 1;
469     b->def_uart_dev = 0;
470     b->uart_dev[0].rx = 10;
471     b->uart_dev[0].tx = 8;
472
473     if ((platform_detected == PLATFORM_RASPBERRY_PI_A_PLUS_REV_1) ||
474         (platform_detected == PLATFORM_RASPBERRY_PI_B_PLUS_REV_1) ||
475         (platform_detected == PLATFORM_RASPBERRY_PI2_B_REV_1)) {
476
477         strncpy(b->pins[27].name, "ID_SD", 8);
478         b->pins[27].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
479
480         strncpy(b->pins[28].name, "ID_SC", 8);
481         b->pins[28].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
482
483         strncpy(b->pins[29].name, "GPIO05", 8);
484         b->pins[29].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
485         b->pins[29].gpio.pinmap = 5;
486         b->pins[29].gpio.mux_total = 0;
487
488         strncpy(b->pins[30].name, "GND", 8);
489         b->pins[30].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
490
491         strncpy(b->pins[31].name, "GPIO06", 8);
492         b->pins[31].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
493         b->pins[31].gpio.pinmap = 6;
494         b->pins[31].gpio.mux_total = 0;
495
496         strncpy(b->pins[32].name, "GPIO12", 8);
497         b->pins[32].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
498         b->pins[32].gpio.pinmap = 12;
499         b->pins[32].gpio.mux_total = 0;
500
501         strncpy(b->pins[33].name, "GPIO13", 8);
502         b->pins[33].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
503         b->pins[33].gpio.pinmap = 13;
504         b->pins[33].gpio.mux_total = 0;
505
506         strncpy(b->pins[34].name, "GND", 8);
507         b->pins[34].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
508
509         strncpy(b->pins[35].name, "GPIO19", 8);
510         b->pins[35].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
511         b->pins[35].gpio.pinmap = 19;
512         b->pins[35].gpio.mux_total = 0;
513
514         strncpy(b->pins[36].name, "GPIO16", 8);
515         b->pins[36].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
516         b->pins[36].gpio.pinmap = 16;
517         b->pins[36].gpio.mux_total = 0;
518
519         strncpy(b->pins[37].name, "GPIO26", 8);
520         b->pins[37].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
521         b->pins[37].gpio.pinmap = 26;
522         b->pins[37].gpio.mux_total = 0;
523
524         strncpy(b->pins[38].name, "GPIO20", 8);
525         b->pins[38].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
526         b->pins[38].gpio.pinmap = 20;
527         b->pins[38].gpio.mux_total = 0;
528
529         strncpy(b->pins[39].name, "GND", 8);
530         b->pins[39].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
531
532         strncpy(b->pins[40].name, "GPIO21", 8);
533         b->pins[40].capabilites = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
534         b->pins[40].gpio.pinmap = 21;
535         b->pins[40].gpio.mux_total = 0;
536     }
537
538     b->gpio_count = 0;
539     int i;
540     for (i = 0; i < b->phy_pin_count; i++) {
541         if (b->pins[i].capabilites.gpio) {
542             b->gpio_count++;
543         }
544     }
545
546     return b;
547 }