Skip to content

Instantly share code, notes, and snippets.

@RickKimball RickKimball/rt.cpp
Last active Feb 26, 2019

Embed
What would you like to do?
raspberry pi 2 toggle using gpiomem
#include <stdio.h>#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
int main(int argc, char **argv)
{
int fd ;
const int max_p = sched_get_priority_max(SCHED_FIFO);
printf("max=%d\n", max_p);
const struct sched_param priority = { max_p }; // typically 1-low 99-high see man 7 sched
sched_setscheduler(0,SCHED_FIFO, &priority);
//Obtain handle to physical memory
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC) ) < 0) {
printf("Unable to open /dev/mem: %s\n", strerror(errno));
return -1;
}
// map a page of memory to gpio at offset 0x20200000 which is where GPIO goodnessstarts
static volatile uint32_t * const
gpio = (uint32_t * const )mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x00200000);
if ((int32_t)gpio < 0){
printf("Mmap failed: %s\n", strerror(errno));
return -1;
}
// set gpio17 as an output
// increment the pointer to 0x20200004
// set the value through a little bit twiddling where we only modify the bits 21-23 in the register
*(gpio + 1) = (*(gpio + 1) & ~(7 << 21)) | (1 << 21);
// toggle gpio17 every second
while( 1 ) {
// set the pin high
// increment the pointer to 0x2020001C
*(gpio + 7) = 1 << 17;
// sleep
//usleep(200-30);
// set the pin to low
// increment the pointer to 0x20200028
*(gpio + 10) = 1 << 17;
//usleep(300-30);
}
}
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>
int main(int argc, char **argv)
{
int fd ;
const struct sched_param priority = { SCHED_FIFO };
sched_setscheduler(0,SCHED_FIFO, &priority);
//Obtain handle to physical memory
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC) ) < 0) {
printf("Unable to open /dev/mem: %s\n", strerror(errno));
return -1;
}
// map a page of memory to gpio at offset 0x20200000 which is where GPIO goodnessstarts
static volatile uint32_t * const
gpio = (uint32_t * const )mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x00200000);
if ((int32_t)gpio < 0){
printf("Mmap failed: %s\n", strerror(errno));
return -1;
}
// set gpio17 as an output
// increment the pointer to 0x20200004
// set the value through a little bit twiddling where we only modify the bits 21-23 in the register
*(gpio + 1) = (*(gpio + 1) & ~(7 << 21)) | (1 << 21);
// toggle gpio17 every second
while( 1 ) {
// set the pin high
// increment the pointer to 0x2020001C
*(gpio + 7) = 1 << 17;
// sleep
//usleep(200-30);
// set the pin to low
// increment the pointer to 0x20200028
*(gpio + 10) = 1 << 17;
//usleep(300-30);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
static volatile uint32_t *gpio;
int main(int argc, char **argv)
{
int fd ;
//Obtain handle to physical memory
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC) ) < 0) {
printf("Unable to open /dev/mem: %s\n", strerror(errno));
return -1;
}
//map a page of memory to gpio at offset 0x20200000 which is where GPIO goodnessstarts
gpio = (uint32_t *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x00200000);
if ((int32_t)gpio < 0){
printf("Mmap failed: %s\n", strerror(errno));
return -1;
}
//set gpio17 as an output
//increment the pointer to 0x20200004
//set the value through a little bit twiddling where we only modify the bits 21-23 in the register
*(gpio + 1) = (*(gpio + 1) & ~(7 << 21)) | (1 << 21);
//toggle gpio17 every second
while(1){
//set the pin high
//increment the pointer to 0x2020001C
*(gpio + 7) = 1 << 17;
//sleep
usleep(200);
//set the pin to low
//increment the pointer to 0x20200028
*(gpio + 10) = 1 << 17;
usleep(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.