/* Copyright (c) 2012-2016 Ben Croston Copyright (c) 2018 PHYTEC America, LLC. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "board.h" #include "common.h" #include "c_gpio.h" int gpio_mode = MODE_UNKNOWN; int gpio_warnings = 1; int setup_error = 0; int module_setup = 0; /* * rpi_pin_to_bcm gives Broadcom processor GPIO pin numbers indexed by * Raspberry Pi GPIO header pin numbers. This is used to translate RPi GPIO * header pin requests to BCM processor pin requests which are then used to * make platform-specific GPIO requests using board_pins[] and BOARD_VALID_PINS_MAP. * * Note: Only supporting the RPi A+/B+ and later layout */ const int rpi_pin_to_bcm[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21}; /* * board_pins[] stores GPIO chip and pin pairs that are associated * externally with Broadcom processor GPIO pin numbers. */ const struct board_pin board_pins[BOARD_VALID_PINS] = BOARD_VALID_PINS_MAP; // returns the bcm pin number on success, -1 on failure int channel_to_bcm_pin(int channel) { if (gpio_mode == BCM) { if (channel < 0 || channel > 53) goto err; return channel; } else if (gpio_mode == BOARD) { if (channel < 1 || channel > 40 || rpi_pin_to_bcm[channel] == -1) goto err; return rpi_pin_to_bcm[channel]; } err: return -1; } // returns 0 on success, -1 on failure (and sets *board_pin_ptr to NULL) int bcm_to_board_pin(int bcm_pin, const struct board_pin **board_pin_ptr) { if (bcm_pin < 0 || bcm_pin >= BOARD_VALID_PINS || board_pins[bcm_pin].bank == 0) { *board_pin_ptr = NULL; return -1; } if (board_pin_ptr) *board_pin_ptr = &board_pins[bcm_pin]; return 0; }