A tiny library for the BCM2835
git clone git://soophie.de/libbcm2835
blob: 91ff15c01b99e094c5586723d0c7c5477f059129 (5481 bytes)[doc][blame][raw]
1
/*2
BCM2835 ARM Peripherals4
Datasheet:5
https://www.raspberrypi.org/app/uploads/2012/02/BCM2835-ARM-Peripherals.pdf8
3V3 Power *---[ 01 | 02 ]---* 5V Power9
GPIO2 (SDA) *---[ 03 | 04 ]---* 5V Power10
GPIO3 (SCL) *---[ 05 | 06 ]---* Ground11
GPIO4 (GPCLK0) *---[ 07 | 08 ]---* GPIO14 (TXD)12
Ground *---[ 09 | 10 ]---* GPIO15 (RXD)13
GPIO17 *---[ 11 | 12 ]---* GPIO18 (PCM_CLK)14
GPIO27 *---[ 13 | 14 ]---* Ground15
GPIO22 *---[ 15 | 16 ]---* GPIO2316
3V3 Power *---[ 17 | 18 ]---* GPIO2417
GPIO10 (MOSI) *---[ 19 | 20 ]---* Ground18
GPIO9 (MISO) *---[ 21 | 22 ]---* GPIO2519
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 ]---* Ground23
GPIO6 *---[ 31 | 32 ]---* GPIO12 (PWM0)24
GPIO13 (PWM1) *---[ 33 | 34 ]---* Ground25
GPIO19 (PCM_FS) *---[ 35 | 36 ]---* GPIO1626
GPIO26 *---[ 37 | 38 ]---* GPIO20 (PCM_DIN)27
Ground *---[ 39 | 40 ]---* GPIO21 (PCM_DOUT)30
gpiochip0 - 54 lines:32
--------------------- --------------------- ---------------------33
line 00: ID_SDA line 18: GPIO18 line 36: SD1_DATA034
line 01: ID_SCL line 19: GPIO19 line 37: SD1_DATA135
line 02: GPIO2 line 20: GPIO20 line 38: SD1_DATA236
line 03: GPIO3 line 21: GPIO21 line 39: SD1_DATA337
line 04: GPIO4 line 22: GPIO22 line 40: PWM0_OUT38
line 05: GPIO5 line 23: GPIO23 line 41: PWM1_OUT39
line 06: GPIO6 line 24: GPIO24 line 42: ETH_CLK40
line 07: GPIO7 line 25: GPIO25 line 43: WIFI_CLK41
line 08: GPIO8 line 26: GPIO26 line 44: SDA042
line 09: GPIO9 line 27: GPIO27 line 45: SCL043
line 10: GPIO10 line 28: NC line 46: SMPS_SCL44
line 11: GPIO11 line 29: LAN_RUN_BOOT line 47: SMPS_SDA45
line 12: GPIO12 line 30: CTS0 line 48: SD_CLK_R46
line 13: GPIO13 line 31: RTS0 line 49: SD_CMD_R47
line 14: GPIO14 line 32: TXD0 line 50: SD_DATA0_R48
line 15: GPIO15 line 33: RXD0 line 51: SD_DATA1_R49
line 16: GPIO16 line 34: SD1_CLK line 52: SD_DATA2_R50
line 17: GPIO17 line 35: SD1_CMD line 53: SD_DATA3_R51
--------------------- --------------------- ---------------------52
*/54
#ifndef LIB_BCM2835_H55
#define LIB_BCM2835_H57
#define BCM_FSEL0 058
#define BCM_FSEL1 159
#define BCM_FSEL2 260
#define BCM_FSEL3 361
#define BCM_FSEL4 462
#define BCM_FSEL5 563
#define BCM_SET0 764
#define BCM_SET1 865
#define BCM_CLR0 1066
#define BCM_CLR1 1167
#define BCM_LEV0 1368
#define BCM_LEV1 1469
#define BCM_EDS0 1670
#define BCM_EDS1 1771
#define BCM_REN0 1972
#define BCM_REN1 2073
#define BCM_FEN0 2274
#define BCM_FEN1 2375
#define BCM_HEN0 2576
#define BCM_HEN1 2677
#define BCM_LEN0 2878
#define BCM_LEN1 2979
#define BCM_AREN0 3180
#define BCM_AREN1 3281
#define BCM_AFEN0 3482
#define BCM_AFEN1 3583
#define BCM_PUD 3784
#define BCM_PUDCLK0 3885
#define BCM_PUDCLK1 3987
#define LOW 088
#define HIGH 190
typedef unsigned char u8_t;91
typedef unsigned int u32_t;93
typedef 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;104
typedef enum {105
PUD_OFF = 0x00,106
PUD_DOWN = 0x01,107
PUD_UP = 0x02,108
} bcm_pud_e;110
int bcm_init(void);111
void bcm_fsel(u32_t pin, bcm_fsel_e fsel);112
void bcm_set(u32_t pin);113
void bcm_clr(u32_t pin);114
int bcm_lev(u32_t pin);115
void bcm_pud(u32_t pin, bcm_pud_e pud);117
void bcm_write(u32_t pin, int level);118
int bcm_read(u32_t pin);119
void bcm_pulse(u32_t pin, int level, u32_t sleep);121
#ifdef LIB_BCM2835_IMPL123
#include <stdio.h>124
#include <fcntl.h>125
#include <unistd.h>126
#include <sys/mman.h>128
static volatile u32_t *reg = MAP_FAILED;130
u32_t __bcm_bank(u32_t pin) {131
return (pin >> 5);132
}134
int 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
}149
void 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
}155
void bcm_set(u32_t pin) {156
int idx = BCM_SET0 + __bcm_bank(pin);157
reg[idx] = (1 << (pin & 0x1F));158
}160
void bcm_clr(u32_t pin) {161
int idx = BCM_CLR0 + __bcm_bank(pin);162
reg[idx] = (1 << (pin & 0x1F));163
}165
int bcm_lev(u32_t pin) {166
int idx = BCM_LEV0 + __bcm_bank(pin);167
return (reg[idx] & (1 << (pin & 0x1F))) != 0;168
}170
void 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
}179
void bcm_write(u32_t pin, int level) {180
level == HIGH181
? bcm_set(pin)182
: bcm_clr(pin);183
}185
int bcm_read(u32_t pin) {186
return bcm_lev(pin);187
}189
void bcm_pulse(u32_t pin, int level, u32_t sleep) {190
level == HIGH191
? bcm_set(pin)192
: bcm_clr(pin);193
usleep(sleep);194
level != HIGH195
? bcm_set(pin)196
: bcm_clr(pin);197
}199
#endif /* LIB_BCM2835_IMPL */200
#endif /* LIB_BCM2835_H */