raspi gpio example

This commit is contained in:
kraus 2021-07-05 13:15:37 +02:00
parent 915ac72b8a
commit 09b816d591
5 changed files with 230 additions and 0 deletions

View File

@ -0,0 +1,171 @@
/*
raspi_gpio_hal.c
This could needs to be compiled with -DU8X8_USE_PINS
Code partly taken over from
http://www.netzmafia.de/skripten/hardware/RasPi/RasPi_GPIO_C.html
https://github.com/olikraus/u8g2/wiki/Porting-to-new-MCU-platform#template-for-the-gpio-and-delay-callback
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "u8x8.h"
void delaynanoseconds(unsigned long ns)
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 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_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)
{
int i;
switch(msg)
{
case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
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
case U8X8_MSG_GPIO_D2: // D2 pin: Output level in arg_int
case U8X8_MSG_GPIO_D3: // D3 pin: Output level in arg_int
case U8X8_MSG_GPIO_D4: // D4 pin: Output level in arg_int
case U8X8_MSG_GPIO_D5: // D5 pin: Output level in arg_int
case U8X8_MSG_GPIO_D6: // D6 pin: Output level in arg_int
case U8X8_MSG_GPIO_D7: // D7 pin: Output level in arg_int
case U8X8_MSG_GPIO_E: // E/WR pin: Output level in arg_int
case U8X8_MSG_GPIO_CS: // CS (chip select) pin: Output level in arg_int
case U8X8_MSG_GPIO_DC: // DC (data/cmd, A0, register select) pin: Output level in arg_int
case U8X8_MSG_GPIO_RESET: // Reset pin: Output level in arg_int
case U8X8_MSG_GPIO_CS1: // CS1 (chip select) pin: Output level in arg_int
case U8X8_MSG_GPIO_CS2: // CS2 (chip select) pin: Output level in arg_int
gpio_write(u8x8_GetPinValue(u8x8, msg), arg_int);
break;
case U8X8_MSG_GPIO_AND_DELAY_INIT: // called once during init phase of u8g2/u8x8
for( i = 0; i < U8X8_PIN_CNT; i++ )
if ( u8x8->pins[i] != U8X8_PIN_NONE )
{
if ( i < U8X8_PIN_OUTPUT_CNT )
{
gpio_direction(u8x8->pins[i], 1);
}
else
{
// not supported at the moment
//gpio_direction(u8x8->pins[i], 0);
}
}
delaynanoseconds(100000);
break; // can be used to setup pins
case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
delaynanoseconds(arg_int);
break;
case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
delaynanoseconds((unsigned long)arg_int*100UL);
break;
case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
delaynanoseconds((unsigned long)arg_int*10000UL);
break;
case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
delaynanoseconds((unsigned long)arg_int*1000000UL);
break;
case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
//delaynanoseconds(1250UL*arg_int);
delaynanoseconds(1000UL*arg_int);
break; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
case U8X8_MSG_GPIO_MENU_SELECT:
u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_NEXT:
u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_PREV:
u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
break;
case U8X8_MSG_GPIO_MENU_HOME:
u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
break;
default:
u8x8_SetGPIOResult(u8x8, 1); // default return value
break;
}
return 1;
}

View File

@ -0,0 +1,21 @@
/*
raspi_gpio_hal.h
This could needs to be compiled with -DU8X8_USE_PINS
*/
#ifndef raspi_gpio_hal_h
#define raspi_gpio_hal_h
#include "u8x8.h"
void delaynanoseconds(unsigned long ns);
int gpio_export(int pin);
int gpio_direction(int pin, int dir);
int gpio_write(int pin, int value);
uint8_t u8x8_gpio_and_delay_raspi_gpio_hal(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
#endif /* raspi_gpio_hal_h */

8
sys/raspi_gpio/info.txt Normal file
View File

@ -0,0 +1,8 @@
GPIO only HAL functions for Raspberry Pi
Manly based of the following (german) web sites:
https://www.elektronik-kompendium.de/sites/raspberry-pi/2202101.htm
http://www.netzmafia.de/skripten/hardware/RasPi/RasPi_GPIO_C.html

View File

@ -0,0 +1,15 @@
CFLAGS = -O -I../hal -I../../../csrc -DU8X8_USE_PINS -ffunction-sections -fdata-sections
LDFLAGS = -Wl,--gc-sections
SRC = $(wildcard *.c) $(wildcard ../hal/*.c) $(wildcard ../../../csrc/*.c)
OBJ = $(SRC:.c=.o)
main: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o main
clean:
-rm $(OBJ)

View File

@ -0,0 +1,15 @@
#include "raspi_gpio_hal.h"
#include "u8g2.h"
int main(void)
{
u8g2_t u8g2; // a structure which will contain all the data for one display
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_sw_i2c, u8x8_gpio_and_delay_raspi_gpio_hal); // init u8g2 structure
u8g2_InitDisplay(&u8g2); // send init sequence to the display, display is in sleep mode after this,
u8g2_SetPowerSave(&u8g2, 0); // wake up display
u8g2_SetFont(&u8g2, u8g2_font_6x12_tr);
u8g2_DrawStr(&u8g2, 0, 20, "Hello World!");
u8g2_SendBuffer(&u8g2);
}