Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change use of /dev/mem to /dev/gpiomem #25

Open
wants to merge 8 commits into
base: master
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: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ 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:
@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

Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ 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) |

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

Expand Down
4 changes: 0 additions & 4 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 47 additions & 27 deletions src/gpio_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,41 +129,61 @@ 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");
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, mem_fd, GPIO_BASE);
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);
}

/* Always use volatile pointer! */
gpio = (volatile uint32_t *) gpio_map;
/* 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;

}

/* 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/mem */
ret = close(mem_fd);
if (ret == -1) {
perror("Cannot close /dev/mem");
exit(1);
}
/* 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);
}
}
4 changes: 2 additions & 2 deletions src/hosts/rpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* Set flag to use /dev/gpiomem instead of /dev/mem */
#define USE_DEV_GPIOMEM

/* GPIO registers address */
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define BLOCK_SIZE (256)
#define PORTOFFSET 0

Expand Down
39 changes: 0 additions & 39 deletions src/hosts/rpi2.h

This file was deleted.

39 changes: 0 additions & 39 deletions src/hosts/rpi4.h

This file was deleted.

53 changes: 37 additions & 16 deletions src/picberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,22 @@ 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 */
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, 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) {
Expand All @@ -388,6 +404,7 @@ void setup_io(void)
perror("mmap() failed");
exit(1);
}
#endif

/* Always use volatile pointer! */
gpio = (volatile uint32_t *) gpio_map;
Expand All @@ -409,24 +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/mem */
ret = close(mem_fd);
if (ret == -1) {
perror("Cannot close /dev/mem");
exit(1);
}
/* 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);
}
}

/* reset the device */
Expand Down