WiringPi: Minor changes
- Fix compile warning
- Remove pwmSetMode function that is not used
- Change the name of changing drive functions
- Aggressively use of WiringPi setupCheck() function
- Move usingGpiomem variable into libodroid structure
- etc...
Signed-off-by: Deokgyu Yang <secugyu@gmail.com>
Change-Id: I355556975cedc686885f3b91d7f1a4ec02f1e232
diff --git a/gpio/gpio.c b/gpio/gpio.c
index aa1ec1b..e0daf0b 100644
--- a/gpio/gpio.c
+++ b/gpio/gpio.c
@@ -67,7 +67,6 @@
" gpio export/edge/unexport ...\n"
" gpio wfi <pin> <mode>\n"
" gpio drive <pin> <value>\n"
- " gpio pwm-bal/pwm-ms \n"
" gpio pwmr <range> \n"
" gpio pwmc <divider> \n"
" gpio load spi/i2c\n"
@@ -563,12 +562,12 @@
/*
- * doPadDrive:
+ * doDrive:
* gpio drive pin value for ODROIDs since it depends on the SoC
*********************************************************************************
*/
-static void doPadDrive (int argc, char *argv [])
+static void doDrive (int argc, char *argv [])
{
int pin, val;
@@ -580,7 +579,7 @@
pin = atoi (argv [2]) ;
val = atoi (argv [3]) ;
- setPadDrive (pin, val) ;
+ setDrive (pin, val) ;
}
/*
@@ -759,16 +758,11 @@
/*
- * doPwmMode: doPwmRange: doPwmClock:
+ * doPwmRange: doPwmClock:
* Change the PWM mode, range and clock divider values
*********************************************************************************
*/
-static void doPwmMode (int mode)
-{
- pwmSetMode (mode) ;
-}
-
static void doPwmRange (int argc, char *argv [])
{
unsigned int range ;
@@ -984,11 +978,9 @@
else if (strcasecmp (argv [1], "blink" ) == 0) doBlink (argc, argv) ;
// Pi Specifics
- else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
- else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
- else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
+ else if (strcasecmp (argv [1], "drive" ) == 0) doDrive (argc, argv) ;
else if (strcasecmp (argv [1], "readall" ) == 0) doReadall (argc, argv) ;
else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall (argc, argv) ;
else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
diff --git a/gpio/readall.c b/gpio/readall.c
index e3db3e7..1279f01 100644
--- a/gpio/readall.c
+++ b/gpio/readall.c
@@ -574,7 +574,7 @@
case MODEL_ODROID_XU3:
case MODEL_ODROID_N2:
case MODEL_ODROID_C4:
- printf (" | %2d | %5s", getPadDrive(pin), pupd[getPUPD(pin)]);
+ printf (" | %2d | %5s", getDrive(pin), pupd[getPUPD(pin)]);
break;
default:
break;
@@ -615,7 +615,7 @@
case MODEL_ODROID_XU3:
case MODEL_ODROID_N2:
case MODEL_ODROID_C4:
- printf (" | %-5s | %-2d", pupd[getPUPD(pin)], getPadDrive(pin));
+ printf (" | %-5s | %-2d", pupd[getPUPD(pin)], getDrive(pin));
break;
default:
break;
diff --git a/wiringPi/odroidc1.c b/wiringPi/odroidc1.c
index 6cf7629..81e1ac5 100644
--- a/wiringPi/odroidc1.c
+++ b/wiringPi/odroidc1.c
@@ -664,8 +664,7 @@
msg (MSG_ERR,
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
strerror (errno));
-
- setUsingGpioMem( TRUE );
+ setUsingGpiomem(TRUE);
} else
msg (MSG_ERR,
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
diff --git a/wiringPi/odroidc2.c b/wiringPi/odroidc2.c
index dd585c8..367e96d 100644
--- a/wiringPi/odroidc2.c
+++ b/wiringPi/odroidc2.c
@@ -701,15 +701,12 @@
msg (MSG_ERR,
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
strerror (errno));
-
- setUsingGpioMem( TRUE );
+ setUsingGpiomem(TRUE);
} else
msg (MSG_ERR,
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
}
-
-
if (fd < 0) {
msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
} else {
diff --git a/wiringPi/odroidc4.c b/wiringPi/odroidc4.c
index 8b9b3ff..54f0ff2 100644
--- a/wiringPi/odroidc4.c
+++ b/wiringPi/odroidc4.c
@@ -112,8 +112,8 @@
// wiringPi core function
/*----------------------------------------------------------------------------*/
static int _getModeToGpio (int mode, int pin);
-static int _setPadDrive (int pin, int value);
-static int _getPadDrive (int pin);
+static int _setDrive (int pin, int value);
+static int _getDrive (int pin);
static int _pinMode (int pin, int mode);
static int _getAlt (int pin);
static int _getPUPD (int pin);
@@ -303,7 +303,7 @@
}
/*----------------------------------------------------------------------------*/
-static int _setPadDrive (int pin, int value)
+static int _setDrive (int pin, int value)
{
int ds, shift;
@@ -329,7 +329,7 @@
}
/*----------------------------------------------------------------------------*/
-static int _getPadDrive (int pin)
+static int _getDrive (int pin)
{
int ds, shift;
@@ -633,6 +633,7 @@
msg (MSG_ERR,
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
strerror (errno));
+ setUsingGpiomem(TRUE);
} else
msg (MSG_ERR,
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
@@ -681,8 +682,8 @@
/* wiringPi Core function initialize */
libwiring->getModeToGpio = _getModeToGpio;
- libwiring->setPadDrive = _setPadDrive;
- libwiring->getPadDrive = _getPadDrive;
+ libwiring->setDrive = _setDrive;
+ libwiring->getDrive = _getDrive;
libwiring->pinMode = _pinMode;
libwiring->getAlt = _getAlt;
libwiring->getPUPD = _getPUPD;
diff --git a/wiringPi/odroidn1.c b/wiringPi/odroidn1.c
index 15211e6..15f18e9 100644
--- a/wiringPi/odroidn1.c
+++ b/wiringPi/odroidn1.c
@@ -676,8 +676,7 @@
msg (MSG_ERR,
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
strerror (errno));
-
- setUsingGpioMem( TRUE );
+ setUsingGpiomem(TRUE);
} else
msg (MSG_ERR,
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
diff --git a/wiringPi/odroidn2.c b/wiringPi/odroidn2.c
index 9ec7ca4..66ba5d4 100644
--- a/wiringPi/odroidn2.c
+++ b/wiringPi/odroidn2.c
@@ -193,8 +193,8 @@
// wiringPi core function
/*----------------------------------------------------------------------------*/
static int _getModeToGpio (int mode, int pin);
-static int _setPadDrive (int pin, int value);
-static int _getPadDrive (int pin);
+static int _setDrive (int pin, int value);
+static int _getDrive (int pin);
static int _pinMode (int pin, int mode);
static int _getAlt (int pin);
static int _getPUPD (int pin);
@@ -383,7 +383,7 @@
}
/*----------------------------------------------------------------------------*/
-static int _setPadDrive (int pin, int value)
+static int _setDrive (int pin, int value)
{
int ds, shift;
@@ -409,7 +409,7 @@
}
/*----------------------------------------------------------------------------*/
-static int _getPadDrive (int pin)
+static int _getDrive (int pin)
{
int ds, shift;
@@ -472,7 +472,7 @@
softToneCreate (origPin);
break;
case PWM_OUTPUT:
- usingGpioMemCheck ("pinMode PWM");
+ usingGpiomemCheck("pinMode PWM");
int pwm_pin, alt;
pwm_pin = gpioToPwmPin(pin);
@@ -495,7 +495,6 @@
_pwmSetClock(120);
_pwmSetRange(500);
#endif
-
break;
default:
msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
@@ -646,8 +645,6 @@
* struct wiringPiNodeStruct *node = wiringPiNodes;
*/
- setupCheck ("pwmWrite") ;
-
if (lib->mode == MODE_GPIO_SYS)
return -1;
@@ -806,8 +803,7 @@
msg(MSG_ERR,
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
strerror (errno));
-
- setUsingGpioMem( TRUE );
+ setUsingGpiomem(TRUE);
} else
msg(MSG_ERR,
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
@@ -865,8 +861,8 @@
/* wiringPi Core function initialize */
libwiring->getModeToGpio = _getModeToGpio;
- libwiring->setPadDrive = _setPadDrive;
- libwiring->getPadDrive = _getPadDrive;
+ libwiring->setDrive = _setDrive;
+ libwiring->getDrive = _getDrive;
libwiring->pinMode = _pinMode;
libwiring->getAlt = _getAlt;
libwiring->getPUPD = _getPUPD;
diff --git a/wiringPi/odroidxu3.c b/wiringPi/odroidxu3.c
index 813535a..cd0283d 100644
--- a/wiringPi/odroidxu3.c
+++ b/wiringPi/odroidxu3.c
@@ -109,8 +109,8 @@
// wiringPi core function
/*----------------------------------------------------------------------------*/
static int _getModeToGpio (int mode, int pin);
-static int _setPadDrive (int pin, int value);
-static int _getPadDrive (int pin);
+static int _setDrive (int pin, int value);
+static int _getDrive (int pin);
static int _pinMode (int pin, int mode);
static int _getAlt (int pin);
static int _getPUPD (int pin);
@@ -284,7 +284,7 @@
}
/*----------------------------------------------------------------------------*/
-static int _setPadDrive (int pin, int value)
+static int _setDrive (int pin, int value)
{
int ds, shift;
@@ -314,7 +314,7 @@
}
/*----------------------------------------------------------------------------*/
-static int _getPadDrive (int pin)
+static int _getDrive (int pin)
{
int ds, shift;
@@ -682,8 +682,7 @@
msg (MSG_ERR,
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
strerror (errno));
-
- setUsingGpioMem( TRUE );
+ setUsingGpiomem(TRUE);
} else
msg (MSG_ERR,
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
@@ -737,8 +736,8 @@
/* wiringPi Core function initialize */
libwiring->getModeToGpio = _getModeToGpio;
- libwiring->setPadDrive = _setPadDrive;
- libwiring->getPadDrive = _getPadDrive;
+ libwiring->setDrive = _setDrive;
+ libwiring->getDrive = _getDrive;
libwiring->pinMode = _pinMode;
libwiring->getAlt = _getAlt;
libwiring->getPUPD = _getPUPD;
diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c
index d1e5f01..793735e 100644
--- a/wiringPi/wiringPi.c
+++ b/wiringPi/wiringPi.c
@@ -125,13 +125,11 @@
// Debugging & Return codes
int wiringPiDebug = FALSE ;
int wiringPiReturnCodes = FALSE ;
+int wiringPiSetuped = FALSE ;
// ODROID Wiring Library
struct libodroid libwiring;
-unsigned int usingGpioMem = FALSE;
-int wiringPiSetuped = FALSE;
-
/*----------------------------------------------------------------------------*/
//
// Return true/false if the supplied module is loaded
@@ -221,7 +219,7 @@
* function. Mosty because they need feeding C drip by drip )-:
*/
/*----------------------------------------------------------------------------*/
-void setupCheck (const char *fName)
+void setupCheck(const char *fName)
{
if (!wiringPiSetuped) {
fprintf (stderr, "%s: You have not called one of the wiringPiSetup\n"
@@ -232,23 +230,22 @@
/*----------------------------------------------------------------------------*/
/*
- * gpioMemCheck:
+ * usingGpiomemCheck: setUsingGpiomem:
* See if we're using the /dev/gpiomem interface, if-so then some operations
* can't be done and will crash the Pi.
*/
/*----------------------------------------------------------------------------*/
-void usingGpioMemCheck (const char *what)
+void usingGpiomemCheck(const char *what)
{
- if (usingGpioMem) {
- fprintf (stderr, "%s: Unable to do this when using /dev/gpiomem. Try sudo?\n", what) ;
- exit (EXIT_FAILURE) ;
+ if (libwiring.usingGpiomem) {
+ fprintf (stderr, "%s: Unable to do this when using /dev/gpiomem. Try sudo?\n", what) ;
+ exit (EXIT_FAILURE) ;
}
}
-/*----------------------------------------------------------------------------*/
-void setUsingGpioMem( const unsigned int value )
+void setUsingGpiomem(const unsigned int value)
{
- usingGpioMem = value;
+ libwiring.usingGpiomem = value;
}
/*----------------------------------------------------------------------------*/
@@ -436,6 +433,8 @@
/*----------------------------------------------------------------------------*/
int wpiPinToGpio (int wpiPin)
{
+ setupCheck(__func__);
+
if (libwiring.getModeToGpio)
return libwiring.getModeToGpio(MODE_PINS, wpiPin);
@@ -451,6 +450,8 @@
/*----------------------------------------------------------------------------*/
int physPinToGpio (int physPin)
{
+ setupCheck(__func__);
+
if (libwiring.getModeToGpio)
return libwiring.getModeToGpio(MODE_PHYS, physPin);
@@ -459,27 +460,31 @@
/*----------------------------------------------------------------------------*/
/*
- * setPadDrive:
- * Set the PAD driver value
+ * setDrive:
+ * Set the pin driver value
*/
/*----------------------------------------------------------------------------*/
-void setPadDrive (int pin, int value)
+void setDrive (int pin, int value)
{
- if (libwiring.setPadDrive)
- if (libwiring.setPadDrive(pin, value) < 0)
+ setupCheck(__func__);
+
+ if (libwiring.setDrive)
+ if (libwiring.setDrive(pin, value) < 0)
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
}
/*----------------------------------------------------------------------------*/
/*
- * getPadDrive:
- * Get the PAD driver value
+ * getDrive:
+ * Get the pin driver value
*/
/*----------------------------------------------------------------------------*/
-int getPadDrive (int pin)
+int getDrive (int pin)
{
- if (libwiring.getPadDrive)
- return libwiring.getPadDrive(pin);
+ setupCheck(__func__);
+
+ if (libwiring.getDrive)
+ return libwiring.getDrive(pin);
return -1;
}
@@ -493,6 +498,8 @@
/*----------------------------------------------------------------------------*/
int getAlt (int pin)
{
+ setupCheck(__func__);
+
if (libwiring.getAlt)
return libwiring.getAlt(pin);
@@ -501,30 +508,15 @@
/*----------------------------------------------------------------------------*/
/*
- * pwmSetMode:
- * Select the native "balanced" mode, or standard mark:space mode
- */
-/*----------------------------------------------------------------------------*/
-
-void pwmSetMode (int mode)
-{
- if (libwiring.pwmSetMode) {
- libwiring.pwmSetMode(mode);
- } else {
- warn_msg(__func__);
- }
-}
-
-/*----------------------------------------------------------------------------*/
-/*
* pwmSetRange:
* Set the PWM range register. We set both range registers to the same
* value. If you want different in your own code, then write your own.
*/
/*----------------------------------------------------------------------------*/
-
void pwmSetRange (unsigned int range)
{
+ setupCheck(__func__);
+
if (libwiring.pwmSetRange) {
libwiring.pwmSetRange(range);
} else {
@@ -540,9 +532,10 @@
* after further study of the manual and testing with a 'scope
*/
/*----------------------------------------------------------------------------*/
-
void pwmSetClock (int divisor)
{
+ setupCheck(__func__);
+
if (libwiring.pwmSetClock) {
libwiring.pwmSetClock(divisor);
} else {
@@ -559,6 +552,8 @@
/*----------------------------------------------------------------------------*/
int getPUPD (int pin)
{
+ setupCheck(__func__);
+
if (libwiring.getPUPD)
return libwiring.getPUPD(pin);
@@ -572,6 +567,8 @@
/*----------------------------------------------------------------------------*/
void pinMode (int pin, int mode)
{
+ setupCheck(__func__);
+
if (libwiring.pinMode)
if (libwiring.pinMode(pin, mode) < 0)
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
@@ -581,6 +578,8 @@
/*----------------------------------------------------------------------------*/
void pullUpDnControl (int pin, int pud)
{
+ setupCheck(__func__);
+
if (libwiring.pullUpDnControl)
if (libwiring.pullUpDnControl(pin, pud) < 0)
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
@@ -589,6 +588,8 @@
/*----------------------------------------------------------------------------*/
int digitalRead (int pin)
{
+ setupCheck(__func__);
+
if (libwiring.digitalRead)
return libwiring.digitalRead(pin);
@@ -598,6 +599,8 @@
/*----------------------------------------------------------------------------*/
void digitalWrite (int pin, int value)
{
+ setupCheck(__func__);
+
if (libwiring.digitalWrite)
if (libwiring.digitalWrite(pin, value) < 0)
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
@@ -606,6 +609,8 @@
/*----------------------------------------------------------------------------*/
void pwmWrite(int pin, int value)
{
+ setupCheck(__func__);
+
if (libwiring.pwmWrite) {
if (libwiring.pwmWrite(pin, value) < 0)
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
@@ -617,6 +622,8 @@
/*----------------------------------------------------------------------------*/
int analogRead (int pin)
{
+ setupCheck(__func__);
+
if (libwiring.analogRead)
return libwiring.analogRead(pin);
@@ -626,6 +633,8 @@
/*----------------------------------------------------------------------------*/
void digitalWriteByte (const int value)
{
+ setupCheck(__func__);
+
if (libwiring.digitalWriteByte)
if (libwiring.digitalWriteByte(value) < 0)
msg(MSG_WARN, "%s: Not available. \n", __func__);
@@ -634,6 +643,8 @@
/*----------------------------------------------------------------------------*/
unsigned int digitalReadByte (void)
{
+ setupCheck(__func__);
+
if (libwiring.digitalReadByte)
return libwiring.digitalReadByte();
@@ -696,10 +707,8 @@
int wiringPiISR (int pin, int mode, void (*function)(void))
{
pthread_t threadId;
- const char *modeS;
char fName [64];
char pinS [8];
- pid_t pid;
int count, i;
char c;
int GpioPin;
@@ -724,13 +733,6 @@
// is a way that will work when we're running in "Sys" mode, as
// a non-root user. (without sudo)
if (mode != INT_EDGE_SETUP) {
- if (mode == INT_EDGE_FALLING)
- modeS = "falling" ;
- else if (mode == INT_EDGE_RISING)
- modeS = "rising" ;
- else
- modeS = "both" ;
-
sprintf (pinS, "%d", GpioPin) ;
FILE *export, *direct, *edge;
@@ -968,9 +970,9 @@
int wiringPiSetup (void)
{
int i;
+
if (wiringPiSetuped)
return 0;
-
wiringPiSetuped = TRUE;
// libwiring init
@@ -980,6 +982,7 @@
libwiring.sysFds[i] = -1;
// init wiringPi mode
libwiring.mode = MODE_UNINITIALISED;
+ libwiring.usingGpiomem = FALSE;
if (getenv (ENV_DEBUG) != NULL)
wiringPiDebug = TRUE;
diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h
index a7b0bf1..473cfa2 100644
--- a/wiringPi/wiringPi.h
+++ b/wiringPi/wiringPi.h
@@ -36,10 +36,9 @@
#define FALSE (!TRUE)
#endif
+#define UNU __attribute__((unused))
#define PIN_NUM_CALC_SYSFD(X) (X > 255 ? X - libwiring.pinBase : X)
-#define UNU __attribute__((unused))
-
#define ENV_DEBUG "WIRINGPI_DEBUG"
#define ENV_CODES "WIRINGPI_CODES"
#define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
@@ -83,10 +82,6 @@
#define PUD_DOWN 1
#define PUD_UP 2
-// PWM
-#define PWM_MODE_MS 0
-#define PWM_MODE_BAL 1
-
// Interrupt levels
#define INT_EDGE_SETUP 0
#define INT_EDGE_FALLING 1
@@ -131,8 +126,8 @@
/* wiringPi core func */
int (*getModeToGpio) (int mode, int pin);
- int (*setPadDrive) (int pin, int value);
- int (*getPadDrive) (int pin);
+ int (*setDrive) (int pin, int value);
+ int (*getDrive) (int pin);
int (*pinMode) (int pin, int mode);
int (*getAlt) (int pin);
int (*getPUPD) (int pin);
@@ -143,7 +138,6 @@
int (*analogRead) (int pin);
int (*digitalWriteByte) (const unsigned int value);
unsigned int (*digitalReadByte) (void);
- void (*pwmSetMode) (int mode);
void (*pwmSetRange) (unsigned int range);
void (*pwmSetClock) (int divisor);
@@ -157,6 +151,9 @@
/* GPIO pin base number */
int pinBase;
+ /* Running with gpiomem */
+ char usingGpiomem;
+
// Time for easy calculations
uint64_t epochMilli, epochMicro ;
};
@@ -213,8 +210,8 @@
extern int msg (int type, const char *message, ...);
extern int moduleLoaded (char *);
extern void setupCheck (const char *fName);
-extern void usingGpioMemCheck (const char *what);
-extern void setUsingGpioMem (const unsigned int value);
+extern void usingGpiomemCheck (const char *what);
+extern void setUsingGpiomem (const unsigned int value);
// Core WiringPi functions
extern void wiringPiVersion (int *major, char **minor);
@@ -223,8 +220,8 @@
extern int wiringPiSetupGpio (void);
extern int wiringPiSetupPhys (void);
-extern void setPadDrive (int pin, int value);
-extern int getPadDrive (int pin);
+extern void setDrive (int pin, int value);
+extern int getDrive (int pin);
extern int getAlt (int pin);
extern int getPUPD (int pin);
extern void pinMode (int pin, int mode);
@@ -242,7 +239,6 @@
extern int wpiPinToGpio (int wpiPin);
extern int physPinToGpio (int physPin);
-extern void pwmSetMode (int mode);
extern void pwmSetRange (unsigned int range);
extern void pwmSetClock (int divisor);