From e80604113ee17b95a343f5538e265b019abf02dc Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Thu, 14 Nov 2024 23:05:26 +0100 Subject: [PATCH 1/8] Change use of /dev/mem to /dev/gpiomem ... this way, a 64bit build works, and we also do not need root access anymore because /dev/gpiomem access is permitted to group gpio members --- src/gpio_test.cpp | 18 +++++++++--------- src/hosts/a10.h | 2 -- src/hosts/rpi.h | 2 -- src/hosts/rpi2.h | 2 -- src/hosts/rpi4.h | 2 -- src/picberry.cpp | 19 ++++++++++--------- 6 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/gpio_test.cpp b/src/gpio_test.cpp index b70efa2..0478e36 100644 --- a/src/gpio_test.cpp +++ b/src/gpio_test.cpp @@ -38,7 +38,7 @@ #include "common.h" -int mem_fd; +int gpio_fd; void *gpio_map; volatile uint32_t *gpio; @@ -129,15 +129,15 @@ int main(int argc, char *argv[]) /* Set up a memory regions to access GPIO */ void setup_io(void) { - /* open /dev/mem */ - mem_fd = open("/dev/mem", O_RDWR|O_SYNC); - if (mem_fd == -1) { - perror("Cannot open /dev/mem"); + /* open /dev/gpiomem */ + gpio_fd = open("/dev/gpiomem", O_RDWR|O_SYNC); + if (gpio_fd == -1) { + perror("Cannot open /dev/gpiomem"); exit(1); } /* mmap GPIO */ - gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, GPIO_BASE); + gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, gpio_fd, 0); if (gpio_map == MAP_FAILED) { perror("mmap() failed"); exit(1); @@ -160,10 +160,10 @@ void close_io(void) exit(1); } - /* close /dev/mem */ - ret = close(mem_fd); + /* close /dev/gpiomem */ + ret = close(gpio_fd); if (ret == -1) { - perror("Cannot close /dev/mem"); + perror("Cannot close /dev/gpiomem"); exit(1); } } diff --git a/src/hosts/a10.h b/src/hosts/a10.h index 9afc950..ac0141c 100644 --- a/src/hosts/a10.h +++ b/src/hosts/a10.h @@ -18,8 +18,6 @@ * along with this program. If not, see . */ -#define SW_PORTC_IO_BASE 0x01c20800 -#define GPIO_BASE 0x01c20000 #define OFFSET 0x00000800 #define BLOCK_SIZE 0x00002000 diff --git a/src/hosts/rpi.h b/src/hosts/rpi.h index 71c2fe3..44b3d1f 100644 --- a/src/hosts/rpi.h +++ b/src/hosts/rpi.h @@ -20,8 +20,6 @@ /* GPIO registers address */ -#define BCM2708_PERI_BASE 0x20000000 -#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ #define BLOCK_SIZE (256) #define PORTOFFSET 0 diff --git a/src/hosts/rpi2.h b/src/hosts/rpi2.h index b994941..1319e7a 100644 --- a/src/hosts/rpi2.h +++ b/src/hosts/rpi2.h @@ -20,8 +20,6 @@ /* GPIO registers address */ -#define BCM2708_PERI_BASE 0x3F000000 -#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ #define BLOCK_SIZE (256) #define PORTOFFSET 0 diff --git a/src/hosts/rpi4.h b/src/hosts/rpi4.h index 2fab271..7316fae 100644 --- a/src/hosts/rpi4.h +++ b/src/hosts/rpi4.h @@ -20,8 +20,6 @@ /* GPIO registers address */ -#define BCM2708_PERI_BASE 0xFE000000 -#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ #define BLOCK_SIZE (256) #define PORTOFFSET 0 diff --git a/src/picberry.cpp b/src/picberry.cpp index ba5001c..04c7263 100644 --- a/src/picberry.cpp +++ b/src/picberry.cpp @@ -52,7 +52,8 @@ #include "devices/pic24fjxxxga2_gb2.h" #include "devices/pic24fxxka1xx.h" -int mem_fd; +//int mem_fd; +int gpio_fd; void *gpio_map; volatile uint32_t *gpio; @@ -374,16 +375,16 @@ int main(int argc, char *argv[]) /* Set up a memory regions to access GPIO */ void setup_io(void) { - /* open /dev/mem */ - mem_fd = open("/dev/mem", O_RDWR|O_SYNC); - if (mem_fd == -1) { - perror("Cannot open /dev/mem"); + /* open /dev/gpiomem */ + gpio_fd = open("/dev/gpiomem", O_RDWR|O_SYNC); + if (gpio_fd == -1) { + perror("Cannot open /dev/gpiomem"); exit(1); } /* mmap GPIO */ gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, - MAP_SHARED, mem_fd, GPIO_BASE); + MAP_SHARED, gpio_fd, 0); if (gpio_map == MAP_FAILED) { perror("mmap() failed"); exit(1); @@ -421,10 +422,10 @@ void close_io(void) exit(1); } - /* close /dev/mem */ - ret = close(mem_fd); + /* close /dev/gpiomem */ + ret = close(gpio_fd); if (ret == -1) { - perror("Cannot close /dev/mem"); + perror("Cannot close /dev/gpiomem"); exit(1); } } From 797541d949b091f7a643b05727f10e4151c80b44 Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Mon, 18 Nov 2024 15:07:21 +0100 Subject: [PATCH 2/8] remove rpi2.h and rpi4.h as they are not needed any more --- Makefile | 2 -- src/common.h | 4 ---- src/hosts/rpi2.h | 37 ------------------------------------- src/hosts/rpi4.h | 37 ------------------------------------- 4 files changed, 80 deletions(-) delete mode 100644 src/hosts/rpi2.h delete mode 100644 src/hosts/rpi4.h diff --git a/Makefile b/Makefile index 9c47525..5c95060 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,6 @@ DEVICES = $(BUILDDIR)/devices/dspic33e.o \ a10: CFLAGS += -DBOARD_A10 raspberrypi: CFLAGS += -DBOARD_RPI -raspberrypi2: CFLAGS += -DBOARD_RPI2 -raspberrypi4: CFLAGS += -DBOARD_RPI4 am335x: CFLAGS += -DBOARD_AM335X default: diff --git a/src/common.h b/src/common.h index 5cbfc05..4a4d29e 100644 --- a/src/common.h +++ b/src/common.h @@ -25,10 +25,6 @@ #include "hosts/a10.h" #elif defined(BOARD_RPI) #include "hosts/rpi.h" -#elif defined(BOARD_RPI2) -#include "hosts/rpi2.h" -#elif defined(BOARD_RPI4) -#include "hosts/rpi4.h" #elif defined(BOARD_AM335X) #include "hosts/am335x.h" #endif diff --git a/src/hosts/rpi2.h b/src/hosts/rpi2.h deleted file mode 100644 index 1319e7a..0000000 --- a/src/hosts/rpi2.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Raspberry Pi PIC Programmer using GPIO connector - * https://github.com/WallaceIT/picberry - * Copyright 2016 Francesco Valla - * - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -/* GPIO registers address */ -#define BLOCK_SIZE (256) -#define PORTOFFSET 0 - -/* GPIO setup macros. Always use GPIO_IN(x) before using GPIO_OUT(x) */ -#define GPIO_IN(g) *(gpio+((g&0xFF)/10)) &= ~(7<<(((g&0xFF)%10)*3)) -#define GPIO_OUT(g) *(gpio+((g&0xFF)/10)) |= (1<<(((g&0xFF)%10)*3)) - -#define GPIO_SET(g) *(gpio+7) = 1<<(g&0xFF) -#define GPIO_CLR(g) *(gpio+10) = 1<<(g&0xFF) -#define GPIO_LEV(g) (*(gpio+13) >> (g&0xFF)) & 0x1 /* reads pin level */ - -/* default GPIO <-> PIC connections */ -#define DEFAULT_PIC_CLK 23 /* PGC - Output */ -#define DEFAULT_PIC_DATA 24 /* PGD - I/O */ -#define DEFAULT_PIC_MCLR 18 /* MCLR - Output */ diff --git a/src/hosts/rpi4.h b/src/hosts/rpi4.h deleted file mode 100644 index 7316fae..0000000 --- a/src/hosts/rpi4.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Raspberry Pi PIC Programmer using GPIO connector - * https://github.com/WallaceIT/picberry - * Copyright 2016 Francesco Valla - * - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -/* GPIO registers address */ -#define BLOCK_SIZE (256) -#define PORTOFFSET 0 - -/* GPIO setup macros. Always use GPIO_IN(x) before using GPIO_OUT(x) */ -#define GPIO_IN(g) *(gpio+((g&0xFF)/10)) &= ~(7<<(((g&0xFF)%10)*3)) -#define GPIO_OUT(g) *(gpio+((g&0xFF)/10)) |= (1<<(((g&0xFF)%10)*3)) - -#define GPIO_SET(g) *(gpio+7) = 1<<(g&0xFF) -#define GPIO_CLR(g) *(gpio+10) = 1<<(g&0xFF) -#define GPIO_LEV(g) (*(gpio+13) >> (g&0xFF)) & 0x1 /* reads pin level */ - -/* default GPIO <-> PIC connections */ -#define DEFAULT_PIC_CLK 23 /* PGC - Output */ -#define DEFAULT_PIC_DATA 24 /* PGD - I/O */ -#define DEFAULT_PIC_MCLR 18 /* MCLR - Output */ From 458792f54afb955cbee51f1f3afeede8bbf6f7b8 Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Mon, 18 Nov 2024 16:18:13 +0100 Subject: [PATCH 3/8] updated README and removed remaining raspberrypi2 and raspberrypi4 references --- Makefile | 2 -- README.md | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5c95060..ae05912 100644 --- a/Makefile +++ b/Makefile @@ -31,8 +31,6 @@ default: @echo "Please specify a target with 'make raspberrypi', 'make a10' or 'make am335x'." raspberrypi: prepare picberry -raspberrypi2: prepare picberry -raspberrypi4: prepare picberry a10: prepare picberry am335x: prepare picberry gpio_test diff --git a/README.md b/README.md index c099e5f..9b51c03 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,7 @@ To build picberry launch `make TARGET`, where _TARGET_ can be one of the followi | _TARGET_ | Processor/Board | | ------------- | ------------------------------------------ | -| raspberrypi | Raspberry Pi v1 or Zero | -| raspberrypi2 | Raspberry Pi v2 or v3 | -| raspberrypi4 | Raspberry Pi 4A or 4B or CM4 +| raspberrypi | Raspberry Pi all models | | am335x | Boards based on TI AM335x (BeagleBone) | | a10 | Boards based on Allwinner A10 (Cubieboard) | @@ -62,7 +60,7 @@ Then launch `sudo make install` to install it to /usr/bin. To change destination prefix use PREFIX=, e.g. `sudo make install PREFIX=/usr/local`. -For cross-compilation, given that you have the required cross toolchain in you PATH, simply export the `CROSS_COMPILE` variable before launching `make`, e.g. `CROSS_COMPILE=arm-linux-gnueabihf- make raspberrypi2`. +For cross-compilation, given that you have the required cross toolchain in you PATH, simply export the `CROSS_COMPILE` variable before launching `make`, e.g. `CROSS_COMPILE=arm-linux-gnueabihf- make raspberrypi`. ## Using picberry From ceb3194dd4a02d774e77646a871345a7e1f8a97b Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Tue, 19 Nov 2024 00:19:29 +0100 Subject: [PATCH 4/8] add flag USE_DEV_GPIOMEM which is only used by raspberry build --- src/hosts/a10.h | 2 ++ src/hosts/rpi.h | 2 ++ src/picberry.cpp | 28 ++++++++++++++++++++++------ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/hosts/a10.h b/src/hosts/a10.h index ac0141c..9afc950 100644 --- a/src/hosts/a10.h +++ b/src/hosts/a10.h @@ -18,6 +18,8 @@ * along with this program. If not, see . */ +#define SW_PORTC_IO_BASE 0x01c20800 +#define GPIO_BASE 0x01c20000 #define OFFSET 0x00000800 #define BLOCK_SIZE 0x00002000 diff --git a/src/hosts/rpi.h b/src/hosts/rpi.h index 44b3d1f..b753b35 100644 --- a/src/hosts/rpi.h +++ b/src/hosts/rpi.h @@ -18,6 +18,8 @@ * along with this program. If not, see . */ +/* Set flag to use /dev/gpiomem instead of /dev/mem */ +#define USE_DEV_GPIOMEM /* GPIO registers address */ #define BLOCK_SIZE (256) diff --git a/src/picberry.cpp b/src/picberry.cpp index 04c7263..5c271f4 100644 --- a/src/picberry.cpp +++ b/src/picberry.cpp @@ -52,8 +52,7 @@ #include "devices/pic24fjxxxga2_gb2.h" #include "devices/pic24fxxka1xx.h" -//int mem_fd; -int gpio_fd; +int mem_fd; void *gpio_map; volatile uint32_t *gpio; @@ -375,20 +374,37 @@ int main(int argc, char *argv[]) /* Set up a memory regions to access GPIO */ void setup_io(void) { +#ifdef USE_DEV_GPIOMEM /* open /dev/gpiomem */ - gpio_fd = open("/dev/gpiomem", O_RDWR|O_SYNC); - if (gpio_fd == -1) { + mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC); + if (mem_fd == -1) { perror("Cannot open /dev/gpiomem"); exit(1); } /* mmap GPIO */ gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, - MAP_SHARED, gpio_fd, 0); + MAP_SHARED, mem_fd, 0); if (gpio_map == MAP_FAILED) { perror("mmap() failed"); exit(1); } +#else + /* open /dev/mem */ + mem_fd = open("/dev/mem", O_RDWR|O_SYNC); + if (mem_fd == -1) { + perror("Cannot open /dev/mem"); + exit(1); + } + + /* mmap GPIO */ + gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, + MAP_SHARED, mem_fd, GPIO_BASE); + if (gpio_map == MAP_FAILED) { + perror("mmap() failed"); + exit(1); + } +#endif /* Always use volatile pointer! */ gpio = (volatile uint32_t *) gpio_map; @@ -423,7 +439,7 @@ void close_io(void) } /* close /dev/gpiomem */ - ret = close(gpio_fd); + ret = close(mem_fd); if (ret == -1) { perror("Cannot close /dev/gpiomem"); exit(1); From ebd2d0b4e7677a066e23f5f994d3bf608650718c Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Tue, 19 Nov 2024 00:26:22 +0100 Subject: [PATCH 5/8] also add usage of gpiomem flag in gpio test module --- src/gpio_test.cpp | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/gpio_test.cpp b/src/gpio_test.cpp index 0478e36..b28aacc 100644 --- a/src/gpio_test.cpp +++ b/src/gpio_test.cpp @@ -38,7 +38,7 @@ #include "common.h" -int gpio_fd; +int mem_fd; void *gpio_map; volatile uint32_t *gpio; @@ -129,22 +129,38 @@ int main(int argc, char *argv[]) /* Set up a memory regions to access GPIO */ void setup_io(void) { - /* open /dev/gpiomem */ - gpio_fd = open("/dev/gpiomem", O_RDWR|O_SYNC); - if (gpio_fd == -1) { - perror("Cannot open /dev/gpiomem"); - exit(1); - } +#ifdef USE_DEV_GPIOMEM + /* open /dev/gpiomem */ + mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC); + if (mem_fd == -1) { + perror("Cannot open /dev/gpiomem"); + exit(1); + } - /* mmap GPIO */ - gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, gpio_fd, 0); - if (gpio_map == MAP_FAILED) { - perror("mmap() failed"); - exit(1); - } + /* mmap GPIO */ + gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0); + if (gpio_map == MAP_FAILED) { + perror("mmap() failed"); + exit(1); + } +#else + /* open /dev/mem */ + mem_fd = open("/dev/mem", O_RDWR|O_SYNC); + if (mem_fd == -1) { + perror("Cannot open /dev/mem"); + exit(1); + } + + /* mmap GPIO */ + gpio_map = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, GPIO_BASE); + if (gpio_map == MAP_FAILED) { + perror("mmap() failed"); + exit(1); + } +#endif - /* Always use volatile pointer! */ - gpio = (volatile uint32_t *) gpio_map; + /* Always use volatile pointer! */ + gpio = (volatile uint32_t *) gpio_map; } From e02b5121786f8317ac75324a76b9a32c22526da6 Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Tue, 19 Nov 2024 00:27:37 +0100 Subject: [PATCH 6/8] fix typo --- src/gpio_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gpio_test.cpp b/src/gpio_test.cpp index b28aacc..5807621 100644 --- a/src/gpio_test.cpp +++ b/src/gpio_test.cpp @@ -177,7 +177,7 @@ void close_io(void) } /* close /dev/gpiomem */ - ret = close(gpio_fd); + ret = close(mem_fd); if (ret == -1) { perror("Cannot close /dev/gpiomem"); exit(1); From e5226fdc4613dd75c6aa5ad060d31500d6a1f70d Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Tue, 19 Nov 2024 00:31:04 +0100 Subject: [PATCH 7/8] fix error output according to gpiomem flag --- src/gpio_test.cpp | 6 +++++- src/picberry.cpp | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/gpio_test.cpp b/src/gpio_test.cpp index 5807621..c5131da 100644 --- a/src/gpio_test.cpp +++ b/src/gpio_test.cpp @@ -176,10 +176,14 @@ void close_io(void) exit(1); } - /* close /dev/gpiomem */ + /* close /dev/gpiomem or /dev/mem */ ret = close(mem_fd); if (ret == -1) { +#ifdef USE_DEV_GPIOMEM perror("Cannot close /dev/gpiomem"); +#else + perror("Cannot close /dev/mem"); +#endif exit(1); } } diff --git a/src/picberry.cpp b/src/picberry.cpp index 5c271f4..3fbe8c7 100644 --- a/src/picberry.cpp +++ b/src/picberry.cpp @@ -438,10 +438,14 @@ void close_io(void) exit(1); } - /* close /dev/gpiomem */ + /* close /dev/gpiomem or /dev/mem */ ret = close(mem_fd); if (ret == -1) { +#ifdef USE_DEV_GPIOMEM perror("Cannot close /dev/gpiomem"); +#else + perror("Cannot close /dev/mem"); +#endif exit(1); } } From 65b29b5b88e185754ebaa8558f0fe6cc41e1007b Mon Sep 17 00:00:00 2001 From: Michael Kaplan Date: Tue, 19 Nov 2024 00:33:26 +0100 Subject: [PATCH 8/8] fix indents in close_io() functions --- src/gpio_test.cpp | 28 ++++++++++++++-------------- src/picberry.cpp | 34 +++++++++++++++++----------------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/gpio_test.cpp b/src/gpio_test.cpp index c5131da..9067078 100644 --- a/src/gpio_test.cpp +++ b/src/gpio_test.cpp @@ -167,23 +167,23 @@ void setup_io(void) /* Release GPIO memory region */ void close_io(void) { - int ret; + int ret; - /* munmap GPIO */ - ret = munmap(gpio_map, BLOCK_SIZE); - if (ret == -1) { - perror("munmap() failed"); - exit(1); - } + /* munmap GPIO */ + ret = munmap(gpio_map, BLOCK_SIZE); + if (ret == -1) { + perror("munmap() failed"); + exit(1); + } - /* close /dev/gpiomem or /dev/mem */ - ret = close(mem_fd); - if (ret == -1) { + /* close /dev/gpiomem or /dev/mem */ + ret = close(mem_fd); + if (ret == -1) { #ifdef USE_DEV_GPIOMEM - perror("Cannot close /dev/gpiomem"); + perror("Cannot close /dev/gpiomem"); #else - perror("Cannot close /dev/mem"); + perror("Cannot close /dev/mem"); #endif - exit(1); - } + exit(1); + } } diff --git a/src/picberry.cpp b/src/picberry.cpp index 3fbe8c7..c0feb2d 100644 --- a/src/picberry.cpp +++ b/src/picberry.cpp @@ -426,28 +426,28 @@ void setup_io(void) /* Release GPIO memory region */ void close_io(void) { - int ret; - - /* MCLR as input, puts the output driver in Hi-Z */ - GPIO_IN(pic_mclr); + int ret; + + /* MCLR as input, puts the output driver in Hi-Z */ + GPIO_IN(pic_mclr); - /* munmap GPIO */ - ret = munmap(gpio_map, BLOCK_SIZE); - if (ret == -1) { - perror("munmap() failed"); - exit(1); - } + /* munmap GPIO */ + ret = munmap(gpio_map, BLOCK_SIZE); + if (ret == -1) { + perror("munmap() failed"); + exit(1); + } - /* close /dev/gpiomem or /dev/mem */ - ret = close(mem_fd); - if (ret == -1) { + /* close /dev/gpiomem or /dev/mem */ + ret = close(mem_fd); + if (ret == -1) { #ifdef USE_DEV_GPIOMEM - perror("Cannot close /dev/gpiomem"); + perror("Cannot close /dev/gpiomem"); #else - perror("Cannot close /dev/mem"); + perror("Cannot close /dev/mem"); #endif - exit(1); - } + exit(1); + } } /* reset the device */