QUOTE: Be your own kind of beautiful.

libbcm2835

A tiny library for the BCM2835

libbcm2835.h (5481B)


      1/*
      2BCM2835 ARM Peripherals
      3
      4Datasheet:
      5https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
      6
      7
      8      3V3 Power  *---[ 01 | 02 ]---*  5V Power
      9    GPIO2 (SDA)  *---[ 03 | 04 ]---*  5V Power
     10    GPIO3 (SCL)  *---[ 05 | 06 ]---*  Ground
     11 GPIO4 (GPCLK0)  *---[ 07 | 08 ]---*  GPIO14 (TXD)
     12         Ground  *---[ 09 | 10 ]---*  GPIO15 (RXD)
     13         GPIO17  *---[ 11 | 12 ]---*  GPIO18 (PCM_CLK)
     14         GPIO27  *---[ 13 | 14 ]---*  Ground
     15         GPIO22  *---[ 15 | 16 ]---*  GPIO23
     16      3V3 Power  *---[ 17 | 18 ]---*  GPIO24
     17  GPIO10 (MOSI)  *---[ 19 | 20 ]---*  Ground
     18   GPIO9 (MISO)  *---[ 21 | 22 ]---*  GPIO25
     19  GPIO11 (SCLK)  *---[ 23 | 24 ]---*  GPIO8 (CE0)
     20         Ground  *---[ 25 | 26 ]---*  GPIO7 (CE1)
     21  GPIO0 (ID_SD)  *---[ 27 | 28 ]---*  GPIO1 (ID_SC)
     22          GPIO5  *---[ 29 | 30 ]---*  Ground
     23          GPIO6  *---[ 31 | 32 ]---*  GPIO12 (PWM0)
     24  GPIO13 (PWM1)  *---[ 33 | 34 ]---*  Ground
     25GPIO19 (PCM_FS)  *---[ 35 | 36 ]---*  GPIO16
     26         GPIO26  *---[ 37 | 38 ]---*  GPIO20 (PCM_DIN)
     27         Ground  *---[ 39 | 40 ]---*  GPIO21 (PCM_DOUT)
     28
     29
     30gpiochip0 - 54 lines:
     31
     32---------------------   ---------------------   ---------------------
     33line 00: ID_SDA         line 18: GPIO18         line 36: SD1_DATA0
     34line 01: ID_SCL         line 19: GPIO19         line 37: SD1_DATA1
     35line 02: GPIO2          line 20: GPIO20         line 38: SD1_DATA2
     36line 03: GPIO3          line 21: GPIO21         line 39: SD1_DATA3
     37line 04: GPIO4          line 22: GPIO22         line 40: PWM0_OUT
     38line 05: GPIO5          line 23: GPIO23         line 41: PWM1_OUT
     39line 06: GPIO6          line 24: GPIO24         line 42: ETH_CLK
     40line 07: GPIO7          line 25: GPIO25         line 43: WIFI_CLK
     41line 08: GPIO8          line 26: GPIO26         line 44: SDA0
     42line 09: GPIO9          line 27: GPIO27         line 45: SCL0
     43line 10: GPIO10         line 28: NC             line 46: SMPS_SCL
     44line 11: GPIO11         line 29: LAN_RUN_BOOT   line 47: SMPS_SDA
     45line 12: GPIO12         line 30: CTS0           line 48: SD_CLK_R
     46line 13: GPIO13         line 31: RTS0           line 49: SD_CMD_R
     47line 14: GPIO14         line 32: TXD0           line 50: SD_DATA0_R
     48line 15: GPIO15         line 33: RXD0           line 51: SD_DATA1_R
     49line 16: GPIO16         line 34: SD1_CLK        line 52: SD_DATA2_R
     50line 17: GPIO17         line 35: SD1_CMD        line 53: SD_DATA3_R
     51---------------------   ---------------------   ---------------------
     52*/
     53
     54#ifndef LIB_BCM2835_H
     55#define LIB_BCM2835_H
     56
     57#define BCM_FSEL0   0
     58#define BCM_FSEL1   1
     59#define BCM_FSEL2   2
     60#define BCM_FSEL3   3
     61#define BCM_FSEL4   4
     62#define BCM_FSEL5   5
     63#define BCM_SET0    7
     64#define BCM_SET1    8
     65#define BCM_CLR0    10
     66#define BCM_CLR1    11
     67#define BCM_LEV0    13
     68#define BCM_LEV1    14
     69#define BCM_EDS0    16
     70#define BCM_EDS1    17
     71#define BCM_REN0    19
     72#define BCM_REN1    20
     73#define BCM_FEN0    22
     74#define BCM_FEN1    23
     75#define BCM_HEN0    25
     76#define BCM_HEN1    26
     77#define BCM_LEN0    28
     78#define BCM_LEN1    29
     79#define BCM_AREN0   31
     80#define BCM_AREN1   32
     81#define BCM_AFEN0   34
     82#define BCM_AFEN1   35
     83#define BCM_PUD     37
     84#define BCM_PUDCLK0 38
     85#define BCM_PUDCLK1 39
     86
     87#define LOW  0
     88#define HIGH 1
     89
     90typedef unsigned char u8_t;
     91typedef unsigned int u32_t;
     92
     93typedef enum {
     94  FSEL_IN   = 0x00,
     95  FSEL_OUT  = 0x01,
     96  FSEL_ALT0 = 0x04,
     97  FSEL_ALT1 = 0x05,
     98  FSEL_ALT2 = 0x06,
     99  FSEL_ALT3 = 0x07,
    100  FSEL_ALT4 = 0x03,
    101  FSEL_ALT5 = 0x02,
    102} bcm_fsel_e;
    103
    104typedef enum {
    105  PUD_OFF  = 0x00,
    106  PUD_DOWN = 0x01,
    107  PUD_UP   = 0x02,
    108} bcm_pud_e;
    109
    110int bcm_init(void);
    111void bcm_fsel(u32_t pin, bcm_fsel_e fsel);
    112void bcm_set(u32_t pin);
    113void bcm_clr(u32_t pin);
    114int bcm_lev(u32_t pin);
    115void bcm_pud(u32_t pin, bcm_pud_e pud);
    116
    117void bcm_write(u32_t pin, int level);
    118int bcm_read(u32_t pin);
    119void bcm_pulse(u32_t pin, int level, u32_t sleep);
    120
    121#ifdef LIB_BCM2835_IMPL
    122
    123#include <stdio.h>
    124#include <fcntl.h>
    125#include <unistd.h>
    126#include <sys/mman.h>
    127
    128static volatile u32_t *reg = MAP_FAILED;
    129
    130u32_t __bcm_bank(u32_t pin) {
    131  return (pin >> 5);
    132}
    133
    134int bcm_init(void) {
    135  int fd = open("/dev/gpiomem", O_RDWR | O_SYNC);
    136  if (fd < 0) {
    137    fprintf(stderr, "Failed to open() /dev/gpiomem!\n");
    138    return -1;
    139  }
    140  reg = (u32_t *) mmap(NULL, 0xB4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    141  close(fd);
    142  if (reg == MAP_FAILED) {
    143    fprintf(stderr, "Bad, mmap() failed!\n");
    144    return -1;
    145  }
    146  return 0;
    147}
    148
    149void bcm_fsel(u32_t pin, bcm_fsel_e fsel) {
    150  int idx = BCM_FSEL0 + (pin / 10);
    151  int shift = 3 * (pin % 10);
    152  reg[idx] = (reg[idx] & ~(7 << shift)) | (fsel << shift);
    153}
    154
    155void bcm_set(u32_t pin) {
    156  int idx = BCM_SET0 + __bcm_bank(pin);
    157  reg[idx] = (1 << (pin & 0x1F));
    158}
    159
    160void bcm_clr(u32_t pin) {
    161  int idx = BCM_CLR0 + __bcm_bank(pin);
    162  reg[idx] = (1 << (pin & 0x1F));
    163}
    164
    165int bcm_lev(u32_t pin) {
    166  int idx = BCM_LEV0 + __bcm_bank(pin);
    167  return (reg[idx] & (1 << (pin & 0x1F))) != 0;
    168}
    169
    170void bcm_pud(u32_t pin, bcm_pud_e pud) {
    171  reg[BCM_PUD] = pud;
    172  usleep(20);
    173  reg[BCM_PUDCLK0 + __bcm_bank(pin)] = (1 << (pin & 0x1F));
    174  usleep(20);
    175  reg[BCM_PUD] = PUD_OFF;
    176  reg[BCM_PUDCLK0 + __bcm_bank(pin)] = 0x00;
    177}
    178
    179void bcm_write(u32_t pin, int level) {
    180  level == HIGH
    181    ? bcm_set(pin)
    182    : bcm_clr(pin);
    183}
    184
    185int bcm_read(u32_t pin) {
    186  return bcm_lev(pin);
    187}
    188
    189void bcm_pulse(u32_t pin, int level, u32_t sleep) {
    190  level == HIGH
    191    ? bcm_set(pin)
    192    : bcm_clr(pin);
    193  usleep(sleep);
    194  level != HIGH
    195    ? bcm_set(pin)
    196    : bcm_clr(pin);
    197}
    198
    199#endif /* LIB_BCM2835_IMPL */
    200#endif /* LIB_BCM2835_H */