Skip to content

Commit

Permalink
wiringPi: Refactor source codes
Browse files Browse the repository at this point in the history
Change-Id: Id121bfc6cb211b78b7ade56d235d43bdf11fc690
  • Loading branch information
joshua-yang committed Nov 29, 2018
1 parent 175d1f6 commit c375207
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 2,862 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ lib*.so.*
debian-template/wiringPi
debian-template/wiringpi-*.deb
gpio/gpio
*.aux
*.dvi
*.log
.DS_Store
.vscode
57 changes: 5 additions & 52 deletions gpio/gpio_odroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <sys/stat.h>

#include <wiringPi.h>
#include <wiringOdroid.h>
#include <wpiExtensions.h>

#include <gertboard.h>
Expand Down Expand Up @@ -625,52 +624,6 @@ static void doPadDrive (int argc, char *argv [])
}


/*
* doUsbP:
* Control USB Power - High (1.2A) or Low (600mA)
* gpio usbp high/low
*********************************************************************************
*/

static void doUsbP (int argc, char *argv [])
{
int model, rev, mem, maker, overVolted ;

if (argc != 3) {
fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
exit (1) ;
}

// Make sure we're on a B+
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;

if (!((model == PI_MODEL_BP) || (model == PI_MODEL_2))) {
fprintf (stderr, "USB power contol is applicable to B+ and v2 boards only.\n") ;
exit (1) ;
}

// Make sure we start in BCM_GPIO mode
wiringPiSetupGpio () ;

if ((strcasecmp (argv [2], "high") == 0) || (strcasecmp (argv [2], "hi") == 0)) {
digitalWrite (PI_USB_POWER_CONTROL, 1) ;
pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
printf ("Switched to HIGH current USB (1.2A)\n") ;
return ;
}

if ((strcasecmp (argv [2], "low") == 0) || (strcasecmp (argv [2], "lo") == 0)) {
digitalWrite (PI_USB_POWER_CONTROL, 0) ;
pinMode (PI_USB_POWER_CONTROL, OUTPUT) ;
printf ("Switched to LOW current USB (600mA)\n") ;
return ;
}

fprintf (stderr, "Usage: %s usbp high|low\n", argv [0]) ;
exit (1) ;
}


/*
* doGbw:
* gpio gbw channel value
Expand Down Expand Up @@ -1189,29 +1142,29 @@ int main (int argc, char *argv [])
for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
wpMode = WPI_MODE_GPIO ;
wpMode = MODE_GPIO ;
} else if (strcasecmp (argv [1], "-1") == 0) { // Check for -1 argument
wiringPiSetupPhys () ;

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
wpMode = WPI_MODE_PHYS ;
wpMode = MODE_PHYS ;
} else if (strcasecmp (argv [1], "-p") == 0) { // Check for -p argument for PiFace
piFaceSetup (200) ;

for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
wpMode = WPI_MODE_PIFACE ;
wpMode = MODE_PIFACE ;
} else if (strcasecmp (argv [1], "-z") == 0) { // Check for -z argument so we don't actually initialise wiringPi
for (i = 2 ; i < argc ; ++i)
argv [i - 1] = argv [i] ;
--argc ;
wpMode = WPI_MODE_UNINITIALISED ;
wpMode = MODE_UNINITIALISED ;
} else { // Default to wiringPi mode
wiringPiSetup () ;
wpMode = WPI_MODE_PINS ;
wpMode = MODE_PINS ;
}

// Check for -x argument to load in a new extension
Expand Down
9 changes: 4 additions & 5 deletions gpio/readall_odroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include <wiringPi.h>
/*----------------------------------------------------------------------------*/
#include <wiringOdroid.h>

extern int wpMode ;

Expand Down Expand Up @@ -277,9 +276,9 @@ static void readallPhysOdroid (int model, int rev, int physPin, const char *phys
if ((physToWpi [physPin] == -1) || (physPinToGpio (physPin) == -1))
printf (" | | ") ;
else {
if (wpMode == WPI_MODE_GPIO)
if (wpMode == MODE_GPIO)
pin = physPinToGpio (physPin);
else if (wpMode == WPI_MODE_PHYS)
else if (wpMode == MODE_PHYS)
pin = physPin ;
else
pin = physToWpi [physPin];
Expand All @@ -297,9 +296,9 @@ static void readallPhysOdroid (int model, int rev, int physPin, const char *phys
if ((physToWpi [physPin] == -1) || (physPinToGpio (physPin) == -1))
printf (" | | ") ;
else {
if (wpMode == WPI_MODE_GPIO)
if (wpMode == MODE_GPIO)
pin = physPinToGpio (physPin);
else if (wpMode == WPI_MODE_PHYS)
else if (wpMode == MODE_PHYS)
pin = physPin ;
else
pin = physToWpi [physPin];
Expand Down
Binary file added pins/odroid_c1.pdf
Binary file not shown.
Binary file added pins/odroid_c2.pdf
Binary file not shown.
Binary file added pins/odroid_n1.pdf
Binary file not shown.
Binary file added pins/odroid_xu3.pdf
Binary file not shown.
6 changes: 3 additions & 3 deletions wiringPi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ LIBS = -lm -lpthread -lrt -lcrypt
SRC = wiring${BRAND_NAME}.c \
wiringSerial.c wiringShift.c \
piHiPri.c piThread.c \
wiringPiSPI.c wiringPiI2C.c \
wiring${BRAND_NAME}SPI.c \
wiring${BRAND_NAME}I2C.c \
softPwm.c softTone.c \
mcp23008.c mcp23016.c mcp23017.c \
mcp23s08.c mcp23s17.c \
Expand All @@ -60,13 +61,12 @@ SRC = wiring${BRAND_NAME}.c \
mcp3002.c mcp3004.c mcp4802.c mcp3422.c \
max31855.c max5322.c ads1115.c \
sn3218.c \
bmp180.c htu21d.c ds18b20.c rht03.c \
bmp180.c htu21d.c ds18b20.c \
drcSerial.c drcNet.c \
pseudoPins.c \
wpiExtensions.c

ifeq ($(BRAND_NAME), Odroid)
DEFS += -D BOARD_ODROID
SRC += odroidc1.c \
odroidc2.c \
odroidxu3.c \
Expand Down
20 changes: 9 additions & 11 deletions wiringPi/wiringOdroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
#include "softTone.h"

/*----------------------------------------------------------------------------*/
#include "wiringPi.h"
#include "../version.h"

#include "wiringOdroid.h"
#include "../version.h"

#include "odroidc1.h"
#include "odroidc2.h"
Expand Down Expand Up @@ -432,7 +430,7 @@ void piBoardId (int *model, int *rev, int *mem, int *maker, int *warranty)
int wpiPinToGpio (int wpiPin)
{
if (libwiring.getModeToGpio)
return libwiring.getModeToGpio(WPI_MODE_PINS, wpiPin);
return libwiring.getModeToGpio(MODE_PINS, wpiPin);

return -1;
}
Expand All @@ -447,7 +445,7 @@ int wpiPinToGpio (int wpiPin)
int physPinToGpio (int physPin)
{
if (libwiring.getModeToGpio)
return libwiring.getModeToGpio(WPI_MODE_PHYS, physPin);
return libwiring.getModeToGpio(MODE_PHYS, physPin);

return -1;
}
Expand Down Expand Up @@ -582,7 +580,7 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
char c;
int GpioPin;

if (libwiring.mode == WPI_MODE_UNINITIALISED)
if (libwiring.mode == MODE_UNINITIALISED)
return wiringPiFailure (
WPI_FATAL,
"wiringPiISR: wiringPi has not been initialised. " \
Expand Down Expand Up @@ -801,7 +799,7 @@ int wiringPiSetup (void)
for(i = 0; i < 256; i++)
libwiring.sysFds[i] = -1;
// init wiringPi mode
libwiring.mode = WPI_MODE_UNINITIALISED;
libwiring.mode = MODE_UNINITIALISED;

if (getenv (ENV_DEBUG) != NULL)
wiringPiDebug = TRUE;
Expand Down Expand Up @@ -839,7 +837,7 @@ int wiringPiSetup (void)

initialiseEpoch ();

libwiring.mode = WPI_MODE_PINS;
libwiring.mode = MODE_PINS;
return 0;
}

Expand All @@ -859,7 +857,7 @@ int wiringPiSetupGpio (void)
if (wiringPiDebug)
printf ("wiringPi: wiringPiSetupGpio called\n") ;

libwiring.mode = WPI_MODE_GPIO;
libwiring.mode = MODE_GPIO;
return 0 ;
}

Expand All @@ -879,7 +877,7 @@ int wiringPiSetupPhys (void)
if (wiringPiDebug)
printf ("wiringPi: wiringPiSetupPhys called\n") ;

libwiring.mode = WPI_MODE_PHYS ;
libwiring.mode = MODE_PHYS ;
return 0 ;
}

Expand Down Expand Up @@ -922,7 +920,7 @@ int wiringPiSetupSys (void)

initialiseEpoch ();

libwiring.mode = WPI_MODE_GPIO_SYS;
libwiring.mode = MODE_GPIO_SYS;
return 0;
}

Expand Down
67 changes: 66 additions & 1 deletion wiringPi/wiringOdroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
#include <stdlib.h>

/*----------------------------------------------------------------------------*/

#ifndef TRUE
#define TRUE (1==1)
#define FALSE (!TRUE)
#endif

#define UNU __attribute__((unused))

#define ENV_DEBUG "WIRINGPI_DEBUG"
#define ENV_CODES "WIRINGPI_CODES"
#define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
Expand Down Expand Up @@ -70,15 +78,33 @@
#define PUD_DOWN 1
#define PUD_UP 2

// Interrupt levels
#define INT_EDGE_SETUP 0
#define INT_EDGE_FALLING 1
#define INT_EDGE_RISING 2
#define INT_EDGE_BOTH 3

// Module names
#define AML_MODULE_I2C "aml_i2c"

// Threads
#define PI_THREAD(X) void *X (UNU void *dummy)

// Failure modes
#define WPI_FATAL (1==1)
#define WPI_ALMOST (1==2)

extern const char *piModelNames [16];
extern const char *piRevisionNames [16];
extern const char *piMakerNames [16];
extern const int piMemorySize [ 8];

/*----------------------------------------------------------------------------*/
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)

/*----------------------------------------------------------------------------*/
/* Debuf message display function */
/* Debug message display function */
/*----------------------------------------------------------------------------*/
#define MSG_ERR -1
#define MSG_WARN -2
Expand All @@ -98,6 +124,45 @@ extern int moduleLoaded(char *);
}
#endif

/*----------------------------------------------------------------------------*/
// wiringPiNodeStruct:
// This describes additional device nodes in the extended wiringPi
// 2.0 scheme of things.
// It's a simple linked list for now, but will hopefully migrate to
// a binary tree for efficiency reasons - but then again, the chances
// of more than 1 or 2 devices being added are fairly slim, so who
// knows....
/*----------------------------------------------------------------------------*/
struct wiringPiNodeStruct
{
int pinBase;
int pinMax;

int fd; // Node specific
unsigned int data0; // ditto
unsigned int data1; // ditto
unsigned int data2; // ditto
unsigned int data3; // ditto

void (*pinMode) (struct wiringPiNodeStruct *node, int pin, int mode);
void (*pullUpDnControl) (struct wiringPiNodeStruct *node, int pin, int mode);
int (*digitalRead) (struct wiringPiNodeStruct *node, int pin);
// unsigned int (*digitalRead8) (struct wiringPiNodeStruct *node, int pin);
void (*digitalWrite) (struct wiringPiNodeStruct *node, int pin, int value);
// void (*digitalWrite8) (struct wiringPiNodeStruct *node, int pin, int value);
void (*pwmWrite) (struct wiringPiNodeStruct *node, int pin, int value);
int (*analogRead) (struct wiringPiNodeStruct *node, int pin);
void (*analogWrite) (struct wiringPiNodeStruct *node, int pin, int value);

struct wiringPiNodeStruct *next;
};

extern struct wiringPiNodeStruct *wiringPiNodes;
extern struct wiringPiNodeStruct *wiringPiFindNode (int pin);
extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins);

extern int wiringPiFailure (int fatal, const char *message, ...) ;

/*----------------------------------------------------------------------------*/
struct libodroid
{
Expand Down
22 changes: 0 additions & 22 deletions wiringPi/wiringPiI2C.c → wiringPi/wiringOdroidI2C.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,6 @@ int wiringPiI2CSetupInterface (const char *device, int devId)
* Open the I2C device, and regsiter the target device
*********************************************************************************
*/
#if defined(BOARD_ODROID)

#include "wiringOdroid.h"

int wiringPiI2CSetup (const int devId)
{
Expand All @@ -239,22 +236,3 @@ int wiringPiI2CSetup (const int devId)

return wiringPiI2CSetupInterface (device, devId) ;
}

#else

int wiringPiI2CSetup (const int devId)
{
int rev ;
const char *device ;

rev = piGpioLayout () ;

if (rev == 1)
device = "/dev/i2c-0" ;
else
device = "/dev/i2c-1" ;

return wiringPiI2CSetupInterface (device, devId) ;
}

#endif // #defined(BOARD_ODROID)
Loading

0 comments on commit c375207

Please sign in to comment.