Page 17 of 21
Chapter 20
Page 350
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
int memfd = open("/dev/mem", O_RDWR | O_SYNC);
uint32_t *map = (uint32_t *)mmap(
NULL,
4 * 1024,
(PROT_READ | PROT_WRITE),
MAP_SHARED,
memfd,
(off_t)bcm2835_peripherals_base + BCM2835_GPIO_BASE);
if (map == MAP_FAILED)
printf("mmap failed: %s\n", strerror(errno));
close(memfd);
volatile uint32_t *paddr = map;
*paddr = 0x1000;
volatile uint32_t *paddr1 = map + 0x1C / 4;
volatile uint32_t *paddr2 = map + 0x28 / 4;
for (;;)
{
*paddr1 = 0x10;
*paddr2 = 0x10;
};
return (EXIT_SUCCESS);
}
Page 355
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
uint32_t *gpioBASE = bcm2835_regbase(BCM2835_REGBASE_GPIO);
bcm2835_peri_write(gpioBASE, 0x1000);
for (;;)
{
bcm2835_peri_write(gpioBASE + BCM2835_GPSET0 / 4, 0x10);
bcm2835_peri_write(gpioBASE + BCM2835_GPCLR0 / 4, 0x10);
}
return 0;
}