diff --git a/documentation/source/rpio_py.rst b/documentation/source/rpio_py.rst old mode 100644 new mode 100755 index bdcbc45..6baa772 --- a/documentation/source/rpio_py.rst +++ b/documentation/source/rpio_py.rst @@ -218,8 +218,8 @@ Additions to RPi.GPIO Additional Constants -* ``RPIO.RPI_REVISION`` - the current board's revision (either ``1`` or ``2``) -* ``RPIO.RPI_REVISION_HEX`` - the cpu hex revision code (``0002`` .. ``000f``) +* ``RPIO.RPI_REVISION`` - the current board's revision and CPU model (either ``1``, ``2``, ``3`` or ``0x103``, with 0x103 identifying BCM2709 chips on revision 3 boards) +* ``RPIO.RPI_REVISION_HEX`` - the cpu hex revision code (``0002`` .. ``000f``) for Pi 2 (``a01041``..``a21041``) for Pi 3 (``a02082``..``a22082``) Additional Methods diff --git a/source/RPIO/__init__.py b/source/RPIO/__init__.py old mode 100644 new mode 100755 index fae1705..ee7ed59 --- a/source/RPIO/__init__.py +++ b/source/RPIO/__init__.py @@ -159,7 +159,19 @@ def socket_callback(socket, val): 'd': ('B', '2.0', 512, 'Egoman'), 'e': ('B', '2.0', 512, 'Sony'), 'f': ('B', '2.0', 512, 'Qisda'), - '10': ('B+', '1.0', 512, 'Sony') + '10': ('B+', '1.0', 512, 'Sony'), + '11': ('CM', '1.0', 512, 'Sony'), + '12': ('A+', '1.1', 256, 'Sony'), + '13': ('B+', '1.2', 512, '?'), + '14': ('CM', '1.0', 512, 'EMBEST'), + '15': ('A+', '1.1', 256, 'EMBEST'), + 'a01040': ('2B', '1.0', 1024, '?'), + 'a01041': ('2B', '1.0', 1024, 'Sony'), + 'a21041': ('2B', '1.0', 1024, 'EMBEST'), + '900092': ('Zero', '1.2', 512, 'Sony'), + '900093': ('Zero', '1.3', 512, 'Sony'), + 'a02082': ('3B', '1.0', 1024, 'Sony'), + 'a22082': ('3B', '1.0', 1024, '?') } # List of valid bcm gpio ids for raspberry rev1, rev2 and rev3. Used for inspect-all. diff --git a/source/c_gpio/c_gpio.c b/source/c_gpio/c_gpio.c old mode 100644 new mode 100755 index 25a04ca..3ed5355 --- a/source/c_gpio/c_gpio.c +++ b/source/c_gpio/c_gpio.c @@ -31,9 +31,11 @@ #include #include #include "c_gpio.h" +#include "cpuinfo.h" #define BCM2708_PERI_BASE 0x20000000 -#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) +#define BCM2709_PERI_BASE 0x3f000000 +#define GPIO_BASE (peri_base + 0x200000) #define OFFSET_FSEL 0 // 0x0000 #define OFFSET_SET 7 // 0x001c / 4 #define OFFSET_CLR 10 // 0x0028 / 4 @@ -52,6 +54,7 @@ #define BLOCK_SIZE (4*1024) static volatile uint32_t *gpio_map; +static uint32_t peri_base; // `short_wait` waits 150 cycles void @@ -67,7 +70,8 @@ short_wait(void) int setup(void) { - int mem_fd; + int mem_fd, type; + char revision_hex[1024]; uint8_t *gpio_mem; if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) @@ -79,6 +83,12 @@ setup(void) if ((uint32_t)gpio_mem % PAGE_SIZE) gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE); + type = get_cpuinfo_revision(revision_hex); + if ((type & 0x100) == 0) + peri_base = BCM2708_PERI_BASE; + else + peri_base = BCM2709_PERI_BASE; + gpio_map = (uint32_t *)mmap( (caddr_t)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, GPIO_BASE); if ((uint32_t)gpio_map < 0) diff --git a/source/c_gpio/cpuinfo.c b/source/c_gpio/cpuinfo.c old mode 100644 new mode 100755 index 922fc5f..289df54 --- a/source/c_gpio/cpuinfo.c +++ b/source/c_gpio/cpuinfo.c @@ -42,6 +42,8 @@ get_cpuinfo_revision(char *revision_hex) char buffer[1024]; char hardware[1024]; int rpi_found = 0; + int bcm2709 = 0; + int board_rev = 3; if ((fp = fopen("/proc/cpuinfo", "r")) == NULL) return -1; @@ -52,9 +54,11 @@ get_cpuinfo_revision(char *revision_hex) if (strcmp(hardware, "BCM2708") == 0|| strcmp(hardware, "BCM2709") == 0|| strcmp(hardware, "BCM2835") == 0|| - strcmp(hardware, "BCM2836") == 0 - ) + strcmp(hardware, "BCM2836") == 0) rpi_found = 1; + + if (strcmp(hardware, "BCM2709") == 0) + bcm2709 = 1; sscanf(buffer, "Revision : %s", revision_hex); } fclose(fp); @@ -72,15 +76,20 @@ get_cpuinfo_revision(char *revision_hex) // Returns revision if ((strcmp(revision_hex, "0002") == 0) || - (strcmp(revision_hex, "0003") == 0)) { - return 1; - } else if ((strcmp(revision_hex, "0010") == 0)) { - // We'll call Model B+ (0010) rev3 - return 3; - } else { - // assume rev 2 (0004 0005 0006 ...) - return 2; - } + (strcmp(revision_hex, "0003") == 0)) + board_rev = 1; + else if ((strcmp(revision_hex, "0004") == 0) + || (strcmp(revision_hex, "0005") == 0) + || (strcmp(revision_hex, "0006") == 0) + || (strcmp(revision_hex, "0007") == 0) + || (strcmp(revision_hex, "0008") == 0) + || (strcmp(revision_hex, "0009") == 0) + || (strcmp(revision_hex, "000d") == 0) + || (strcmp(revision_hex, "000e") == 0) + || (strcmp(revision_hex, "000f") == 0)) + board_rev = 2; + else // We'll call Model A+, B+, Pi2, Pi3 and Zero rev3 + board_rev = 3; - return -1; + return (bcm2709 << 8) | board_rev; } diff --git a/source/c_gpio/py_gpio.c b/source/c_gpio/py_gpio.c old mode 100644 new mode 100755 index 2e8ab7b..5a34dc6 --- a/source/c_gpio/py_gpio.c +++ b/source/c_gpio/py_gpio.c @@ -54,7 +54,7 @@ static PyObject *version; // eg. gpio_id = *(*pin_to_gpio_rev2 + board_pin_id); static const int pin_to_gpio_rev1[41] = {-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; static const int pin_to_gpio_rev2[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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; -static const int pin_to_gpio_rev3[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 24, 11, 7, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21}; +static const int pin_to_gpio_rev3[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}; static const int (*pin_to_gpio)[41]; // Board header info is shifted left 8 bits (leaves space for up to 255 channel ids per header) @@ -519,7 +519,7 @@ PyMODINIT_FUNC init_GPIO(void) // detect board revision and set up accordingly cache_rpi_revision(); - switch (revision_int) { + switch (revision_int & 0xff) { case 1: pin_to_gpio = &pin_to_gpio_rev1; gpio_to_pin = &gpio_to_pin_rev1; diff --git a/source/c_pwm/mailbox.c b/source/c_pwm/mailbox.c index 139482a..3202a4b 100755 --- a/source/c_pwm/mailbox.c +++ b/source/c_pwm/mailbox.c @@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "mailbox.h" @@ -249,9 +250,17 @@ int mbox_open() { // open a char device file used for communicating with kernel mbox driver file_desc = open(DEVICE_FILE_NAME, 0); if (file_desc < 0) { - printf("Can't open device file: %s\n", DEVICE_FILE_NAME); - printf("Try creating a device file with: sudo mknod %s c %d 0\n", DEVICE_FILE_NAME, MAJOR_NUM); - exit(-1); + unlink(DEVICE_FILE_NAME); + if (mknod(DEVICE_FILE_NAME, S_IFCHR|0600, makedev(MAJOR_NUM, 0)) < 0) { + printf("Failed to create mailbox device\n"); + exit(-1); + } + file_desc = open(DEVICE_FILE_NAME, 0); + if (file_desc < 0) { + printf("Can't open device file: %s\n", DEVICE_FILE_NAME); + printf("Try creating a device file with: sudo mknod %s c %d 0\n", DEVICE_FILE_NAME, MAJOR_NUM); + exit(-1); + } } return file_desc; } diff --git a/source/c_pwm/mailbox.h b/source/c_pwm/mailbox.h index ffd5b77..07ca361 100755 --- a/source/c_pwm/mailbox.h +++ b/source/c_pwm/mailbox.h @@ -29,9 +29,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define MAJOR_NUM 100 #define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *) -#define DEVICE_FILE_NAME "/dev/vcio-mb" +#define DEVICE_FILE_NAME "/dev/vcio" -int mbox_open(); +int mbox_open(void); void mbox_close(int file_desc); unsigned get_version(int file_desc); diff --git a/source/c_pwm/pwm.c b/source/c_pwm/pwm.c index 08fa381..c1c9d70 100644 --- a/source/c_pwm/pwm.c +++ b/source/c_pwm/pwm.c @@ -86,7 +86,6 @@ #include #include "pwm.h" #include "mailbox.h" -#define MBFILE DEVICE_FILE_NAME /* From mailbox.h */ // 15 DMA channels are usable on the RPi (0..14) #define DMA_CHANNELS 15 @@ -321,7 +320,6 @@ shutdown(void) } } _is_setup =0; - unlink(MBFILE); } // Terminate is triggered by signals @@ -757,8 +755,6 @@ setup(int pw_incr_us, int hw) init_hardware(); /* Use the mailbox interface to the VC to ask for physical memory */ - if (mknod(MBFILE, S_IFCHR|0600, makedev(249, 0)) < 0) - return fatal("Failed to create mailbox device\n"); mbox.handle = mbox_open(); if (mbox.handle < 0) return fatal("Failed to open mailbox\n"); diff --git a/source/scripts/rpio b/source/scripts/rpio old mode 100644 new mode 100755 index 7637136..bcf7fcb --- a/source/scripts/rpio +++ b/source/scripts/rpio @@ -148,12 +148,12 @@ def main(): RPIO.setwarnings(False) def is_valid_gpio_id(gpio_id): - if RPIO.sysinfo()[1] == 'B+': - return gpio_id in RPIO.GPIO_LIST_R3 - elif RPIO.RPI_REVISION == 1: + if RPIO.RPI_REVISION == 1: return gpio_id in RPIO.GPIO_LIST_R1 - else: + elif RPIO.RPI_REVISION == 2: return gpio_id in RPIO.GPIO_LIST_R2 + else: + return gpio_id in RPIO.GPIO_LIST_R3 def validate_gpio_id_or_die(gpio_id): if not is_valid_gpio_id(gpio_id): @@ -205,12 +205,12 @@ def main(): show_help = False ids = options.show if options.inspect_all: - if RPIO.sysinfo()[1] == 'B+': - pins = RPIO.GPIO_LIST_R3 - elif RPIO.RPI_REVISION == 1: + if RPIO.RPI_REVISION == 1: pins = RPIO.GPIO_LIST_R1 - else: + elif RPIO.RPI_REVISION == 2: pins = RPIO.GPIO_LIST_R2 + else: + pins = RPIO.GPIO_LIST_R3 id_list = list(pins) id_list.sort() ids = ",".join([str(gpio) for gpio in id_list if gpio >= 0]) diff --git a/source/scripts/rpio-curses b/source/scripts/rpio-curses old mode 100644 new mode 100755 index fc549ec..6512eb8 --- a/source/scripts/rpio-curses +++ b/source/scripts/rpio-curses @@ -75,12 +75,12 @@ if "no_rpio" not in CMD_OPTIONS: RPIO.setwarnings(False) RPIO_VERSION = RPIO.VERSION def get_gpiolist(): - if RPIO.sysinfo()[1] == 'B+': - pins = RPIO.GPIO_LIST_R3 - elif RPIO.RPI_REVISION == 1: + if RPIO.RPI_REVISION == 1: pins = RPIO.GPIO_LIST_R1 - else: + elif RPIO.RPI_REVISION == 2: pins = RPIO.GPIO_LIST_R2 + else: + pins = RPIO.GPIO_LIST_R3 id_list = list(pins) id_list.sort() diff --git a/source/tests_gpio.py b/source/tests_gpio.py old mode 100644 new mode 100755 index 395c811..e865a18 --- a/source/tests_gpio.py +++ b/source/tests_gpio.py @@ -118,12 +118,12 @@ def test5_board_pin_numbers(self): logging.info("=== BCM AND BOARD NUMBERING TESTS ===") RPIO.setmode(RPIO.BCM) - if RPIO.sysinfo()[1] == 'B+': - pins = RPIO.GPIO_LIST_R3 - elif RPIO.RPI_REVISION == 1: + if RPIO.RPI_REVISION == 1: pins = RPIO.GPIO_LIST_R1 - else: + elif RPIO.RPI_REVISION == 2: pins = RPIO.GPIO_LIST_R2 + else: + pins = RPIO.GPIO_LIST_R3 logging.info("testing bcm gpio numbering: %s", pins) for pin in pins: gpio_id = RPIO.channel_to_gpio(pin)