Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficiently wait for pin change #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RaspberryPi&JetsonNano/c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
epd
11 changes: 7 additions & 4 deletions RaspberryPi_JetsonNano/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ DEBUG = -D DEBUG
USELIB_RPI = USE_WIRINGPI_LIB
# USELIB_RPI = USE_DEV_LIB

LIB_RPI=-Wl,--gc-sections
ifeq ($(USELIB_RPI), USE_BCM2835_LIB)
LIB_RPI = -lbcm2835 -lm
LIB_RPI += -lbcm2835 -lm
else ifeq ($(USELIB_RPI), USE_WIRINGPI_LIB)
LIB_RPI = -lwiringPi -lm
LIB_RPI += -lwiringPi -lm
else ifeq ($(USELIB_RPI), USE_DEV_LIB)
LIB_RPI = -lm
LIB_RPI += -lm
endif
DEBUG_RPI = -D $(USELIB_RPI) -D RPI

Expand All @@ -42,7 +43,7 @@ JETSON: JETSON_DEV JETSON_epd

TARGET = epd
CC = gcc
MSG = -g -O0 -Wall
MSG = -g -O -ffunction-sections -fdata-sections -Wall
CFLAGS += $(MSG)

RPI_epd:${OBJ_O}
Expand All @@ -53,6 +54,8 @@ JETSON_epd:${OBJ_O}
echo $(@)
$(CC) $(CFLAGS) $(OBJ_O) $(JETSON_DEV_C) -o $(TARGET) $(LIB_JETSONI) $(DEBUG)

$(shell mkdir -p $(DIR_BIN))

${DIR_BIN}/%.o:$(DIR_Examples)/%.c
$(CC) $(CFLAGS) -c $< -o $@ -I $(DIR_Config) -I $(DIR_GUI) -I $(DIR_EPD) $(DEBUG)

Expand Down
38 changes: 32 additions & 6 deletions RaspberryPi_JetsonNano/c/lib/Config/DEV_Config.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
******************************************************************************/
#include "DEV_Config.h"
#include <fcntl.h>
#include <time.h>

/**
* GPIO
Expand Down Expand Up @@ -85,6 +86,31 @@ UBYTE DEV_Digital_Read(UWORD Pin)
return Read_value;
}

void DEV_Digital_Wait(UWORD Pin, UBYTE Value)
{
#ifdef RPI
#ifdef USE_BCM2835_LIB
do {
DEV_Delay_ms(10);
} while(bcm2835_gpio_lev(Pin) != Value);
#elif USE_WIRINGPI_LIB
do {
DEV_Delay_ms(10);
} while(digitalRead(Pin) != Value);
#elif USE_DEV_LIB
SYSFS_GPIO_Wait(Pin, Value);
#endif
#endif

#ifdef JETSON
#ifdef USE_DEV_LIB
SYSFS_GPIO_Wait(Pin, Value);
#elif USE_HARDWARE_LIB
Debug("not support");
#endif
#endif
}

/**
* SPI
**/
Expand Down Expand Up @@ -185,10 +211,10 @@ void DEV_Delay_ms(UDOUBLE xms)
#elif USE_WIRINGPI_LIB
delay(xms);
#elif USE_DEV_LIB
UDOUBLE i;
for(i=0; i < xms; i++) {
usleep(1000);
}
struct timespec tv;
tv.tv_nsec=(xms%1000)*1000000;
tv.tv_sec=xms/1000;
nanosleep(&tv, NULL);
#endif
#endif

Expand Down Expand Up @@ -229,7 +255,7 @@ static int DEV_Equipment_Testing(void)
if(i<5) {
printf("Unrecognizable\r\n");
} else {
char RPI_System[10] = {"Raspbian"};
char RPI_System[10] = {"Debian"};
for(i=0; i<6; i++) {
if(RPI_System[i]!= value_str[i]) {
printf("Please make JETSON !!!!!!!!!!\r\n");
Expand Down Expand Up @@ -366,10 +392,10 @@ void DEV_Module_Exit(void)
DEV_Digital_Write(EPD_DC_PIN, 0);
DEV_Digital_Write(EPD_RST_PIN, 0);
#elif USE_DEV_LIB
DEV_HARDWARE_SPI_end();
DEV_Digital_Write(EPD_CS_PIN, 0);
DEV_Digital_Write(EPD_DC_PIN, 0);
DEV_Digital_Write(EPD_RST_PIN, 0);
DEV_HARDWARE_SPI_end();
#endif

#elif JETSON
Expand Down
1 change: 1 addition & 0 deletions RaspberryPi_JetsonNano/c/lib/Config/DEV_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extern int EPD_BUSY_PIN;
/*------------------------------------------------------------------------------------------------------*/
void DEV_Digital_Write(UWORD Pin, UBYTE Value);
UBYTE DEV_Digital_Read(UWORD Pin);
void DEV_Digital_Wait(UWORD Pin, UBYTE Value);

void DEV_SPI_WriteByte(UBYTE Value);
void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len);
Expand Down
73 changes: 73 additions & 0 deletions RaspberryPi_JetsonNano/c/lib/Config/RPI_sysfs_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/poll.h>

int SYSFS_GPIO_Export(int Pin)
{
Expand Down Expand Up @@ -107,6 +108,32 @@ int SYSFS_GPIO_Direction(int Pin, int Dir)
return 0;
}

int SYSFS_GPIO_Edge(int Pin, int edge)
{
const char *edge_str[] = {"rising","falling","both"};
const int edge_str_l[] = {6, 7, 4};
char path[DIR_MAXSIZ];
int fd;

snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/edge", Pin);
fd = open(path, O_WRONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Set Edge failed: Pin%d\n", Pin);
return -1;
}

if (edge>2) edge=2;
if (write(fd, edge_str[edge], edge_str_l[edge]) < 0) {
SYSFS_GPIO_Debug("failed to set edge!\r\n");
return -1;
}

SYSFS_GPIO_Debug("Pin%d:%s edge\r\n", Pin, edge_str[edge]);

close(fd);
return 0;
}

int SYSFS_GPIO_Read(int Pin)
{
char path[DIR_MAXSIZ];
Expand All @@ -122,13 +149,58 @@ int SYSFS_GPIO_Read(int Pin)

if (read(fd, value_str, 3) < 0) {
SYSFS_GPIO_Debug( "failed to read value!\n");
close(fd);
return -1;
}

close(fd);
return(atoi(value_str));
}

int SYSFS_GPIO_Wait(int Pin, int value)
{
char path[DIR_MAXSIZ];
char value_str[3];
int fd;
struct pollfd pfd[1];

SYSFS_GPIO_Edge(Pin, SYSFS_GPIO_BOTH);
snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin);
fd = open(path, O_RDONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Read failed Pin%d\n", Pin);
return -1;
}

while (1) {
int n;
if (read(fd, value_str, 3) < 0) {
SYSFS_GPIO_Debug( "failed to read value!\n");
close(fd);
return -1;
}
if (atoi(value_str) == value) break;
pfd[0].fd=fd;
pfd[0].events=POLLPRI;
n = poll(pfd, 1, -1);
if (n < 0) {
SYSFS_GPIO_Debug( "poll failed: %m!\n");
close(fd);
return -1;
}
/* lseek(0) doesn't seem to work reliably */
close(fd);
fd = open(path, O_RDONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "open failed: %m\n");
return -1;
}
}

close(fd);
return 0;
}

int SYSFS_GPIO_Write(int Pin, int value)
{
const char s_values_str[] = "01";
Expand All @@ -144,6 +216,7 @@ int SYSFS_GPIO_Write(int Pin, int value)

if (write(fd, &s_values_str[value == SYSFS_GPIO_LOW ? 0 : 1], 1) < 0) {
SYSFS_GPIO_Debug( "failed to write value!\n");
close(fd);
return -1;
}

Expand Down
7 changes: 6 additions & 1 deletion RaspberryPi_JetsonNano/c/lib/Config/RPI_sysfs_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#define SYSFS_GPIO_LOW 0
#define SYSFS_GPIO_HIGH 1

#define SYSFS_GPIO_RISING 0
#define SYSFS_GPIO_FALLING 1
#define SYSFS_GPIO_BOTH 2

#define NUM_MAXBUF 4
#define DIR_MAXSIZ 60

Expand Down Expand Up @@ -78,5 +82,6 @@ int SYSFS_GPIO_Unexport(int Pin);
int SYSFS_GPIO_Direction(int Pin, int Dir);
int SYSFS_GPIO_Read(int Pin);
int SYSFS_GPIO_Write(int Pin, int value);
int SYSFS_GPIO_Wait(int Pin, int value);

#endif
#endif
11 changes: 3 additions & 8 deletions RaspberryPi_JetsonNano/c/lib/Config/dev_hardware_SPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,13 @@ function: SPI port sends one byte of data
uint8_t DEV_HARDWARE_SPI_TransferByte(uint8_t buf)
{
uint8_t rbuf[1];
tr.len = 1;
tr.tx_buf = (unsigned long)&buf;
tr.rx_buf = (unsigned long)rbuf;

//ioctl Operation, transmission of data
if ( ioctl(hardware_SPI.fd, SPI_IOC_MESSAGE(1), &tr) < 1 )
DEV_HARDWARE_SPI_Debug("can't send spi message\r\n");
rbuf[0] = buf;
DEV_HARDWARE_SPI_Transfer(rbuf, 1);
return rbuf[0];
}

/******************************************************************************
function: The SPI port reads a byte
function: The SPI port reads some bytes
parameter:
Info: Return read data
******************************************************************************/
Expand Down
73 changes: 73 additions & 0 deletions RaspberryPi_JetsonNano/c/lib/Config/sysfs_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/poll.h>

int SYSFS_GPIO_Export(int Pin)
{
Expand Down Expand Up @@ -107,6 +108,32 @@ int SYSFS_GPIO_Direction(int Pin, int Dir)
return 0;
}

int SYSFS_GPIO_Edge(int Pin, int edge)
{
const char *edge_str[] = {"rising","falling","both"};
const int edge_str_l[] = {6, 7, 4};
char path[DIR_MAXSIZ];
int fd;

snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/edge", Pin);
fd = open(path, O_WRONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Set Edge failed: Pin%d\n", Pin);
return -1;
}

if (edge>2) edge=2;
if (write(fd, edge_str[edge], edge_str_l[edge]) < 0) {
SYSFS_GPIO_Debug("failed to set edge!\r\n");
return -1;
}

SYSFS_GPIO_Debug("Pin%d:%s edge\r\n", Pin, edge_str[edge]);

close(fd);
return 0;
}

int SYSFS_GPIO_Read(int Pin)
{
char path[DIR_MAXSIZ];
Expand All @@ -122,13 +149,58 @@ int SYSFS_GPIO_Read(int Pin)

if (read(fd, value_str, 3) < 0) {
SYSFS_GPIO_Debug( "failed to read value!\n");
close(fd);
return -1;
}

close(fd);
return(atoi(value_str));
}

int SYSFS_GPIO_Wait(int Pin, int value)
{
char path[DIR_MAXSIZ];
char value_str[3];
int fd;
struct pollfd pfd[1];

SYSFS_GPIO_Edge(Pin, SYSFS_GPIO_BOTH);
snprintf(path, DIR_MAXSIZ, "/sys/class/gpio/gpio%d/value", Pin);
fd = open(path, O_RDONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "Read failed Pin%d\n", Pin);
return -1;
}

while (1) {
int n;
if (read(fd, value_str, 3) < 0) {
SYSFS_GPIO_Debug( "failed to read value!\n");
close(fd);
return -1;
}
if (atoi(value_str) == value) break;
pfd[0].fd=fd;
pfd[0].events=POLLPRI;
n = poll(pfd, 1, -1);
if (n < 0) {
SYSFS_GPIO_Debug( "poll failed: %m!\n");
close(fd);
return -1;
}
/* lseek(0) doesn't seem to work reliably */
close(fd);
fd = open(path, O_RDONLY);
if (fd < 0) {
SYSFS_GPIO_Debug( "open failed: %m\n");
return -1;
}
}

close(fd);
return 0;
}

int SYSFS_GPIO_Write(int Pin, int value)
{
const char s_values_str[] = "01";
Expand All @@ -144,6 +216,7 @@ int SYSFS_GPIO_Write(int Pin, int value)

if (write(fd, &s_values_str[value == LOW ? 0 : 1], 1) < 0) {
SYSFS_GPIO_Debug( "failed to write value!\n");
close(fd);
return -1;
}

Expand Down
7 changes: 6 additions & 1 deletion RaspberryPi_JetsonNano/c/lib/Config/sysfs_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#define LOW 0
#define HIGH 1

#define SYSFS_GPIO_RISING 0
#define SYSFS_GPIO_FALLING 1
#define SYSFS_GPIO_BOTH 2

#define NUM_MAXBUF 4
#define DIR_MAXSIZ 60

Expand Down Expand Up @@ -80,5 +84,6 @@ int SYSFS_GPIO_Unexport(int Pin);
int SYSFS_GPIO_Direction(int Pin, int Dir);
int SYSFS_GPIO_Read(int Pin);
int SYSFS_GPIO_Write(int Pin, int value);
int SYSFS_GPIO_Wait(int Pin, int value);

#endif
#endif
Loading