commit db75c12e4b4f16b5b4e2550ab6ebaa9af9d51ce1
parent 4068f5468d8eb1a972b060aa2948c6ab63eb7e04
Author: Sophie <info@soophie.de>
Date: Sat, 15 Feb 2025 10:31:15 +0100
feat: Converted to single header file
Diffstat:
M | Makefile | | | 15 | ++------------- |
D | src/libbcm2835.c | | | 77 | ----------------------------------------------------------------------------- |
M | src/libbcm2835.h | | | 85 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
3 files changed, 84 insertions(+), 93 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,18 +1,7 @@
-default: build
-
-build:
- cc -o libbcm2835.so -shared -fPIC -Wall -Wextra -Werror -pedantic src/libbcm2835.c
+default: install
install:
- cc -o libbcm2835.so -shared -fPIC src/libbcm2835.c
- sudo cp src/libbcm2835.h /usr/local/include/libbcm2835.h
- sudo cp libbcm2835.so /usr/local/lib/libbcm2835.so
- sudo ldconfig
- rm libbcm2835.so
+ sudo cp src/libbcm2835.h /usr/local/include/
uninstall:
sudo rm /usr/local/include/libbcm2835.h
- sudo rm /usr/local/lib/libbcm2835.so
-
-clean:
- rm libbcm2835.so
diff --git a/src/libbcm2835.c b/src/libbcm2835.c
@@ -1,77 +0,0 @@
-#include <stdio.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/mman.h>
-
-#include "libbcm2835.h"
-
-static volatile u32_t *reg = MAP_FAILED;
-
-u32_t __bcm_bank(u32_t pin) {
- return (pin >> 5);
-}
-
-int bcm_init(void) {
- int fd = open("/dev/gpiomem", O_RDWR | O_SYNC);
- if (fd < 0) {
- fprintf(stderr, "Failed to open() /dev/gpiomem!\n");
- return -1;
- }
- reg = (u32_t *) mmap(NULL, 0xB4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- close(fd);
- if (reg == MAP_FAILED) {
- fprintf(stderr, "Bad, mmap() failed!\n");
- return -1;
- }
- return 0;
-}
-
-void bcm_fsel(u32_t pin, bcm_fsel_e fsel) {
- int idx = BCM_FSEL0 + (pin / 10);
- int shift = 3 * (pin % 10);
- reg[idx] = (reg[idx] & ~(7 << shift)) | (fsel << shift);
-}
-
-void bcm_set(u32_t pin) {
- int idx = BCM_SET0 + __bcm_bank(pin);
- reg[idx] = (1 << (pin & 0x1F));
-}
-
-void bcm_clr(u32_t pin) {
- int idx = BCM_CLR0 + __bcm_bank(pin);
- reg[idx] = (1 << (pin & 0x1F));
-}
-
-int bcm_lev(u32_t pin) {
- int idx = BCM_LEV0 + __bcm_bank(pin);
- return (reg[idx] & (1 << (pin & 0x1F))) != 0;
-}
-
-void bcm_pud(u32_t pin, bcm_pud_e pud) {
- reg[BCM_PUD] = pud;
- usleep(20);
- reg[BCM_PUDCLK0 + __bcm_bank(pin)] = (1 << (pin & 0x1F));
- usleep(20);
- reg[BCM_PUD] = PUD_OFF;
- reg[BCM_PUDCLK0 + __bcm_bank(pin)] = 0x00;
-}
-
-void bcm_write(u32_t pin, int level) {
- level == HIGH
- ? bcm_set(pin)
- : bcm_clr(pin);
-}
-
-int bcm_read(u32_t pin) {
- return bcm_lev(pin);
-}
-
-void bcm_pulse(u32_t pin, int level, u32_t sleep) {
- level == HIGH
- ? bcm_set(pin)
- : bcm_clr(pin);
- usleep(sleep);
- level != HIGH
- ? bcm_set(pin)
- : bcm_clr(pin);
-}
diff --git a/src/libbcm2835.h b/src/libbcm2835.h
@@ -51,8 +51,8 @@ line 17: GPIO17 line 35: SD1_CMD line 53: SD_DATA3_R
--------------------- --------------------- ---------------------
*/
-#ifndef BCM_2835_H
-#define BCM_2835_H
+#ifndef LIB_BCM2835_H
+#define LIB_BCM2835_H
#define BCM_FSEL0 0
#define BCM_FSEL1 1
@@ -118,4 +118,83 @@ void bcm_write(u32_t pin, int level);
int bcm_read(u32_t pin);
void bcm_pulse(u32_t pin, int level, u32_t sleep);
-#endif
+#ifdef LIB_BCM2835_IMPL
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+static volatile u32_t *reg = MAP_FAILED;
+
+u32_t __bcm_bank(u32_t pin) {
+ return (pin >> 5);
+}
+
+int bcm_init(void) {
+ int fd = open("/dev/gpiomem", O_RDWR | O_SYNC);
+ if (fd < 0) {
+ fprintf(stderr, "Failed to open() /dev/gpiomem!\n");
+ return -1;
+ }
+ reg = (u32_t *) mmap(NULL, 0xB4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ close(fd);
+ if (reg == MAP_FAILED) {
+ fprintf(stderr, "Bad, mmap() failed!\n");
+ return -1;
+ }
+ return 0;
+}
+
+void bcm_fsel(u32_t pin, bcm_fsel_e fsel) {
+ int idx = BCM_FSEL0 + (pin / 10);
+ int shift = 3 * (pin % 10);
+ reg[idx] = (reg[idx] & ~(7 << shift)) | (fsel << shift);
+}
+
+void bcm_set(u32_t pin) {
+ int idx = BCM_SET0 + __bcm_bank(pin);
+ reg[idx] = (1 << (pin & 0x1F));
+}
+
+void bcm_clr(u32_t pin) {
+ int idx = BCM_CLR0 + __bcm_bank(pin);
+ reg[idx] = (1 << (pin & 0x1F));
+}
+
+int bcm_lev(u32_t pin) {
+ int idx = BCM_LEV0 + __bcm_bank(pin);
+ return (reg[idx] & (1 << (pin & 0x1F))) != 0;
+}
+
+void bcm_pud(u32_t pin, bcm_pud_e pud) {
+ reg[BCM_PUD] = pud;
+ usleep(20);
+ reg[BCM_PUDCLK0 + __bcm_bank(pin)] = (1 << (pin & 0x1F));
+ usleep(20);
+ reg[BCM_PUD] = PUD_OFF;
+ reg[BCM_PUDCLK0 + __bcm_bank(pin)] = 0x00;
+}
+
+void bcm_write(u32_t pin, int level) {
+ level == HIGH
+ ? bcm_set(pin)
+ : bcm_clr(pin);
+}
+
+int bcm_read(u32_t pin) {
+ return bcm_lev(pin);
+}
+
+void bcm_pulse(u32_t pin, int level, u32_t sleep) {
+ level == HIGH
+ ? bcm_set(pin)
+ : bcm_clr(pin);
+ usleep(sleep);
+ level != HIGH
+ ? bcm_set(pin)
+ : bcm_clr(pin);
+}
+
+#endif /* LIB_BCM2835_IMPL */
+#endif /* LIB_BCM2835_H */