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

Commit

Permalink
Fix Raspberry Pi2, Pi3 support
Browse files Browse the repository at this point in the history
. Fix Raspberry Pi model detection
. Add model detection in GPIO module
. Try to fix mailbox char device detection on various kernels
. Fix compiler warnings
  • Loading branch information
Yackou committed Aug 17, 2016
1 parent 07c776f commit 0b56f11
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion documentation/source/rpio_py.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ 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_HEX`` - the cpu hex revision code (``0002`` .. ``000f``) for Pi 2 (``a01041``..``a21041``) for Pi 3 (``a02082``)

Additional Methods

Expand Down
5 changes: 4 additions & 1 deletion source/RPIO/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ 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'),
'a01041': ('2B', '1.0', 1024, 'Sony'),
'a21041': ('2B', '1.0', 1024, 'EMBEST'),
'a02082': ('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
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 == 1)
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
6 changes: 5 additions & 1 deletion source/c_gpio/cpuinfo.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ get_cpuinfo_revision(char *revision_hex)
(strcmp(revision_hex, "0003") == 0)) {
return 1;
} else if ((strcmp(revision_hex, "0010") == 0)) {
// We'll call Model B+ (0010) rev3
} else if (strcmp(revision_hex, "0010") == 0
|| strcmp(revision_hex, "a21041") == 0
|| strcmp(revision_hex, "a01041") == 0
|| strcmp(revision_hex, "a02082") == 0 ) {
// We'll call Model B+ (0010) rev3 or Pi 2
return 3;
} else {
// assume rev 2 (0004 0005 0006 ...)
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
4 changes: 2 additions & 2 deletions source/scripts/rpio
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def main():
RPIO.setwarnings(False)

def is_valid_gpio_id(gpio_id):
if RPIO.sysinfo()[1] == 'B+':
if RPIO.sysinfo()[1] == 'B+' or RPIO.sysinfo()[1] == '2B' or RPIO.sysinfo()[1] == '3B':
return gpio_id in RPIO.GPIO_LIST_R3
elif RPIO.RPI_REVISION == 1:
return gpio_id in RPIO.GPIO_LIST_R1
Expand Down Expand Up @@ -205,7 +205,7 @@ def main():
show_help = False
ids = options.show
if options.inspect_all:
if RPIO.sysinfo()[1] == 'B+':
if RPIO.sysinfo()[1] == 'B+' or RPIO.sysinfo()[1] == '2B' or RPIO.sysinfo()[1] == '3B':
pins = RPIO.GPIO_LIST_R3
elif RPIO.RPI_REVISION == 1:
pins = RPIO.GPIO_LIST_R1
Expand Down
2 changes: 1 addition & 1 deletion source/scripts/rpio-curses
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if "no_rpio" not in CMD_OPTIONS:
RPIO.setwarnings(False)
RPIO_VERSION = RPIO.VERSION
def get_gpiolist():
if RPIO.sysinfo()[1] == 'B+':
if RPIO.sysinfo()[1] == 'B+' or RPIO.sysinfo()[1] == '2B' or RPIO.sysinfo()[1] == '3B':
pins = RPIO.GPIO_LIST_R3
elif RPIO.RPI_REVISION == 1:
pins = RPIO.GPIO_LIST_R1
Expand Down
2 changes: 1 addition & 1 deletion source/tests_gpio.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test5_board_pin_numbers(self):
logging.info("=== BCM AND BOARD NUMBERING TESTS ===")

RPIO.setmode(RPIO.BCM)
if RPIO.sysinfo()[1] == 'B+':
if RPIO.sysinfo()[1] == 'B+' or RPIO.sysinfo()[1] == '2B' or RPIO.sysinfo()[1] == '3B':
pins = RPIO.GPIO_LIST_R3
elif RPIO.RPI_REVISION == 1:
pins = RPIO.GPIO_LIST_R1
Expand Down

0 comments on commit 0b56f11

Please sign in to comment.