bcm2835 lib

This commit is contained in:
kraus 2021-07-06 22:52:59 +02:00
parent 31493b40d9
commit c076bf785e
2 changed files with 12 additions and 103 deletions

View File

@ -14,10 +14,14 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
/*
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
*/
#include "bcm2835.h"
#include "u8x8.h"
@ -30,101 +34,6 @@ void delaynanoseconds(unsigned long ns)
nanosleep(&ts, (struct timespec *)NULL);
}
int gpio_export(int pin)
{
char buffer[6];
ssize_t bytes;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0)
return perror("/sys/class/gpio/export"), 0;
bytes = sprintf(buffer, "%d", pin);
if (write(fd, buffer, bytes) < 0)
return perror("write"), close(fd), 0;
close(fd);
return 1;
}
/* dir: 0=in, 1=out */
int gpio_direction(int pin, int dir)
{
char path[256];
int fd;
int res;
snprintf(path, 256, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
return perror(path), 0;
switch (dir)
{
case 0 : res = write(fd,"in",2); break;
case 1: res = write(fd,"out",3); break;
default: return close(fd), 0;
}
if (res < 0)
return perror("write"), close(fd), 0;
close(fd);
return 1;
}
int gpio_fd[U8X8_PIN_CNT];
int gpio_prepare_write(int msg, int pin)
{
char path[256];
snprintf(path, 256, "/sys/class/gpio/gpio%d/value", pin);
gpio_fd[msg] = open(path, O_WRONLY);
if (gpio_fd[msg] < 0)
return perror(path), 0;
printf("prepare write success '%s' fd %d msg %d\n", path, gpio_fd[msg], msg);
return 1;
}
int gpio_quick_write(int msg, int value)
{
int res;
//printf("quick write fd %d msg %d\n", gpio_fd[msg], msg);
if ( msg > U8X8_PIN_OUTPUT_CNT )
exit(0);
switch (value)
{
case 0 : res = write(gpio_fd[msg] ,"0",1); break;
default: res = write(gpio_fd[msg] ,"1",1); break;
}
if (res < 0)
return perror("quick_write"), 0;
return 1;
}
int gpio_write(int pin, int value)
{
char path[256];
int fd;
int res;
snprintf(path, 256, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
return perror(path), 0;
switch (value)
{
case 0 : res = write(fd,"0",1); break;
default: res = write(fd,"1",1); break;
}
if (res < 0)
return perror("write"), close(fd), 0;
close(fd);
return 1;
}
uint8_t u8x8_gpio_and_delay_raspi_gpio_hal(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
@ -133,8 +42,6 @@ uint8_t u8x8_gpio_and_delay_raspi_gpio_hal(u8x8_t *u8x8, uint8_t msg, uint8_t ar
switch(msg)
{
case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
//printf("i2c clock msg %d %d\n", msg, U8X8_MSG_GPIO_I2C_CLOCK);
case U8X8_MSG_GPIO_I2C_DATA: // arg_int=0: Output low at I2C data pin
case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int
case U8X8_MSG_GPIO_D1: // D1 or SPI data pin: Output level in arg_int
@ -152,21 +59,20 @@ uint8_t u8x8_gpio_and_delay_raspi_gpio_hal(u8x8_t *u8x8, uint8_t msg, uint8_t ar
case U8X8_MSG_GPIO_CS2: // CS2 (chip select) pin: Output level in arg_int
if ( u8x8_GetPinValue(u8x8, msg) != U8X8_PIN_NONE )
{
//gpio_write(u8x8_GetPinValue(u8x8, msg), arg_int);
//printf("gpio msg %d\n", msg);
gpio_quick_write(u8x8_GetPinIndex(u8x8, msg), arg_int);
bcm2835_gpio_write(u8x8_GetPinValue(u8x8, msg), arg_int);
}
break;
case U8X8_MSG_GPIO_AND_DELAY_INIT: // called once during init phase of u8g2/u8x8
if (!bcm2835_init())
exit(1);
atexit(bcm2835_close);
for( i = 0; i < U8X8_PIN_CNT; i++ )
if ( u8x8->pins[i] != U8X8_PIN_NONE )
{
if ( i < U8X8_PIN_OUTPUT_CNT )
{
gpio_export(u8x8->pins[i]);
gpio_direction(u8x8->pins[i], 1);
gpio_prepare_write(i, u8x8->pins[i]);
bcm2835_gpio_fsel(u8x8->pins[i], BCM2835_GPIO_FSEL_OUTP);
}
else
{

View File

@ -6,3 +6,6 @@ https://www.elektronik-kompendium.de/sites/raspberry-pi/2202101.htm
http://www.netzmafia.de/skripten/hardware/RasPi/RasPi_GPIO_C.html
The bcm2835.[hc] files are under GPL v3 copyright.
taken from here:
http://www.airspayce.com/mikem/bcm2835/