|
30 | 30 | #include <sys/mman.h> |
31 | 31 | #include <sys/stat.h> |
32 | 32 | #include <sys/time.h> |
| 33 | +#include <sys/utsname.h> |
33 | 34 | #include <linux/i2c-dev.h> |
| 35 | +#include <linux/version.h> |
34 | 36 |
|
35 | 37 | #include "common.h" |
36 | 38 | #include "gpio.h" |
|
48 | 50 | #define MAX_SIZE 64 |
49 | 51 | #define POLL_TIMEOUT |
50 | 52 |
|
51 | | -static volatile int base1, base2, _fd; |
52 | | -#define base1 220 |
53 | | -#define base2 253 |
54 | | -#define led_base 276 |
| 53 | +static volatile int _fd; |
55 | 54 | static mraa_gpio_context gpio; |
56 | 55 | static char* uart_name[MRAA_ROSCUBE_UARTCOUNT] = {"COM1", "COM2"}; |
57 | 56 | static char* uart_path[MRAA_ROSCUBE_UARTCOUNT] = {"/dev/ttyS0", "/dev/ttyS1"}; |
58 | 57 |
|
| 58 | +static void get_gpio_base(int *base1, int *base2, int *led_base) |
| 59 | +{ |
| 60 | + struct utsname buffer; |
| 61 | + char *p; |
| 62 | + long ver[16]; |
| 63 | + int i=0; |
| 64 | + |
| 65 | + errno = 0; |
| 66 | + if (uname(&buffer) != 0) { |
| 67 | + perror("uname"); |
| 68 | + exit(EXIT_FAILURE); |
| 69 | + } |
| 70 | + |
| 71 | + p = buffer.release; |
| 72 | + while (*p) { |
| 73 | + if (isdigit(*p)) { |
| 74 | + ver[i] = strtol(p, &p, 10); |
| 75 | + i++; |
| 76 | + } else { |
| 77 | + p++; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + long ver_hex = (ver[0] << 16) + (ver[1] << 8) + ver[2]; |
| 82 | + if (ver_hex < KERNEL_VERSION(5,15,0)) { |
| 83 | + *base1 = 220; |
| 84 | + *base2 = 253; |
| 85 | + *led_base = 276; |
| 86 | + } else { |
| 87 | + *base1 = 732; |
| 88 | + *base2 = 765; |
| 89 | + *led_base = 788; |
| 90 | + } |
| 91 | +} |
| 92 | + |
59 | 93 | // utility function to setup pin mapping of boards |
60 | 94 | static mraa_result_t mraa_roscube_set_pininfo(mraa_board_t* board, int mraa_index, char* name, |
61 | 95 | mraa_pincapabilities_t caps, int sysfs_pin) |
@@ -113,6 +147,8 @@ static mraa_result_t mraa_roscube_init_uart(mraa_board_t* board, int index) |
113 | 147 |
|
114 | 148 | int index_mapping(int index) |
115 | 149 | { |
| 150 | + int base1, base2, led_base; |
| 151 | + get_gpio_base(&base1, &base2, &led_base); |
116 | 152 | return index + led_base; |
117 | 153 | } |
118 | 154 |
|
@@ -258,6 +294,8 @@ mraa_board_t* mraa_roscube_i() |
258 | 294 | b->adv_func->led_set_close = rqi_led_set_close; |
259 | 295 | b->adv_func->led_check_bright = rqi_led_check_bright; |
260 | 296 |
|
| 297 | + int base1, base2, led_base; |
| 298 | + get_gpio_base(&base1, &base2, &led_base); |
261 | 299 | syslog(LOG_NOTICE, "ROSCubeI: base1 %d base2 %d\n", base1, base2); |
262 | 300 |
|
263 | 301 | mraa_roscube_set_pininfo(b, 1, "CN_DI0", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, base1 + 0); |
|
0 commit comments