Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Fix Raspberry Pi 2, Pi3 support #82

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions documentation/source/rpio_py.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion source/RPIO/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 12 additions & 2 deletions source/c_gpio/c_gpio.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#include <fcntl.h>
#include <sys/mman.h>
#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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
33 changes: 21 additions & 12 deletions source/c_gpio/cpuinfo.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions source/c_gpio/py_gpio.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 12 additions & 3 deletions source/c_pwm/mailbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdint.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include "mailbox.h"

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions source/c_pwm/mailbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions source/c_pwm/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
#include <sys/mman.h>
#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

Expand Down Expand Up @@ -321,7 +320,6 @@ shutdown(void)
}
}
_is_setup =0;
unlink(MBFILE);
}

// Terminate is triggered by signals
Expand Down Expand Up @@ -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");
Expand Down
16 changes: 8 additions & 8 deletions source/scripts/rpio
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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])
Expand Down
8 changes: 4 additions & 4 deletions source/scripts/rpio-curses
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions source/tests_gpio.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down