Skip to content

Commit

Permalink
use the _Wait function where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald committed Dec 14, 2020
1 parent 1354090 commit 53cb7f2
Show file tree
Hide file tree
Showing 24 changed files with 231 additions and 79 deletions.
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
5 changes: 2 additions & 3 deletions RaspberryPi_JetsonNano/c/lib/e-Paper/EPD_1in54.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ function : Wait until the busy_pin goes LOW
******************************************************************************/
void EPD_1IN54_ReadBusy(void)
{
if(DEV_Digital_Read(EPD_BUSY_PIN) == 0) return;
Debug("e-Paper busy\r\n");
while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) { //LOW: idle, HIGH: busy
DEV_Delay_ms(100);
}
DEV_Digital_Wait(EPD_BUSY_PIN, 0);
Debug("e-Paper busy release\r\n");
}

Expand Down
6 changes: 2 additions & 4 deletions RaspberryPi_JetsonNano/c/lib/e-Paper/EPD_1in54b.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ function : Wait until the busy_pin goes LOW
******************************************************************************/
static void EPD_1IN54B_ReadBusy(void)
{
if(DEV_Digital_Read(EPD_BUSY_PIN) == 1) return;
Debug("e-Paper busy\r\n");
while(1) {
if(DEV_Digital_Read(EPD_BUSY_PIN) == 1)
break;
}
DEV_Digital_Wait(EPD_BUSY_PIN, 1); //0: busy, 1: idle
DEV_Delay_ms(200);
Debug("e-Paper busy release\r\n");
}
Expand Down
Loading

0 comments on commit 53cb7f2

Please sign in to comment.