wiringPi: Clean warning messages from gcc
Signed-off-by: Joshua Yang <joshua.yang@hardkernel.com>
This commit is contained in:
@@ -208,7 +208,6 @@ static void doUnLoad (int UNU argc, char *argv [])
|
||||
static void doI2Cdetect (UNU int argc, char *argv [])
|
||||
{
|
||||
int model, rev, mem, maker, overVolted, port;
|
||||
const char *device = NULL;
|
||||
char *c, *command ;
|
||||
|
||||
piBoardId(&model, &rev, &mem, &maker, &overVolted);
|
||||
@@ -216,15 +215,12 @@ static void doI2Cdetect (UNU int argc, char *argv [])
|
||||
switch (model) {
|
||||
case MODEL_ODROID_C1: case MODEL_ODROID_C2:
|
||||
case MODEL_ODROID_XU3:
|
||||
device = "/dev/i2c-1";
|
||||
port = 1;
|
||||
break;
|
||||
case MODEL_ODROID_N1:
|
||||
device = "/dev/i2c-4";
|
||||
port = 4;
|
||||
break;
|
||||
case MODEL_ODROID_N2:
|
||||
device = "/dev/i2c-3";
|
||||
port = 3;
|
||||
default:
|
||||
break;
|
||||
@@ -239,7 +235,6 @@ static void doI2Cdetect (UNU int argc, char *argv [])
|
||||
case MAKER_AMLOGIC:
|
||||
if (!moduleLoaded (AML_MODULE_I2C)) {
|
||||
fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
|
||||
return ;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
@@ -1028,7 +1023,8 @@ static void doVersion (char *argv [])
|
||||
// Output Kernel idea of board type
|
||||
if (stat ("/proc/device-tree/model", &statBuf) == 0) {
|
||||
if ((fd = fopen ("/proc/device-tree/model", "r")) != NULL) {
|
||||
fgets (name, 80, fd) ;
|
||||
if (fgets (name, 80, fd) == NULL)
|
||||
fprintf(stderr, "Unable to read from the file descriptor: %s \n", strerror(errno));
|
||||
fclose (fd) ;
|
||||
printf (" *--> %s\n", name) ;
|
||||
}
|
||||
|
||||
@@ -415,7 +415,7 @@ void ReadallOdroid (int model, int rev, const char *physNames[])
|
||||
void doReadall (void)
|
||||
{
|
||||
int model, rev, mem, maker, overVolted;
|
||||
const char (*physNames)[];
|
||||
char *physNames;
|
||||
|
||||
// External readall
|
||||
if (wiringPiNodes != NULL) {
|
||||
@@ -428,32 +428,32 @@ void doReadall (void)
|
||||
switch (model) {
|
||||
case MODEL_ODROID_C1:
|
||||
printf (" +------+-----+----------+------+---+----+---- Model ODROID-C1 ----+----+---+------+----------+-----+------+\n") ;
|
||||
physNames = physNamesOdroidC1;
|
||||
physNames = (char *) physNamesOdroidC1;
|
||||
break;
|
||||
case MODEL_ODROID_C2:
|
||||
printf (" +------+-----+----------+------+---+----+---- Model ODROID-C2 ----+----+---+------+----------+-----+------+\n") ;
|
||||
if (rev == 1)
|
||||
physNames = physNamesOdroidC2_Rev1;
|
||||
physNames = (char *) physNamesOdroidC2_Rev1;
|
||||
else
|
||||
physNames = physNamesOdroidC2_Rev2;
|
||||
physNames = (char *) physNamesOdroidC2_Rev2;
|
||||
break;
|
||||
case MODEL_ODROID_XU3:
|
||||
printf (" +------+-----+----------+------+---+----+--- Model ODROID-XU3/4 ---+----+---+------+----------+-----+------+\n") ;
|
||||
physNames = physNamesOdroidXU3;
|
||||
physNames = (char *) physNamesOdroidXU3;
|
||||
break;
|
||||
case MODEL_ODROID_N1:
|
||||
printf (" +------+-----+----------+------+---+----+---- Model ODROID-N1 ----+----+---+------+----------+-----+------+\n") ;
|
||||
physNames = physNamesOdroidN1;
|
||||
physNames = (char *) physNamesOdroidN1;
|
||||
break;
|
||||
case MODEL_ODROID_N2:
|
||||
printf (" +------+-----+----------+------+---+----+---- Model ODROID-N2 ----+----+---+------+----------+-----+------+\n") ;
|
||||
physNames = physNamesOdroidN2;
|
||||
physNames = (char *) physNamesOdroidN2;
|
||||
break;
|
||||
default:
|
||||
printf ("Oops - unable to determine board type... model: %d\n", model) ;
|
||||
return;
|
||||
}
|
||||
ReadallOdroid(model, rev, physNames);
|
||||
ReadallOdroid(model, rev, (const char **) physNames);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
@@ -47,7 +49,9 @@ void waitForConversion (int fd, unsigned char *buffer, int n)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
read (fd, buffer, n) ;
|
||||
if (read(fd, buffer, n) < 0) {
|
||||
fprintf(stderr, "Unable to read from the file descriptor: %s \n", strerror(errno));
|
||||
}
|
||||
if ((buffer [n-1] & 0x80) == 0)
|
||||
break ;
|
||||
delay (1) ;
|
||||
|
||||
@@ -88,7 +88,7 @@ static const int phyToGpio[64] = {
|
||||
//
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* ADC file descriptor */
|
||||
static char *adcFds[2];
|
||||
static int adcFds[2];
|
||||
|
||||
/* GPIO mmap control */
|
||||
static volatile uint32_t *gpio;
|
||||
@@ -110,14 +110,14 @@ static int gpioToGPFSELReg (int pin);
|
||||
// wiringPi core function
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _getModeToGpio (int mode, int pin);
|
||||
static void _pinMode (int pin, int mode);
|
||||
static int _pinMode (int pin, int mode);
|
||||
static int _getAlt (int pin);
|
||||
static int _getPUPD (int pin);
|
||||
static void _pullUpDnControl (int pin, int pud);
|
||||
static int _pullUpDnControl (int pin, int pud);
|
||||
static int _digitalRead (int pin);
|
||||
static void _digitalWrite (int pin, int value);
|
||||
static int _digitalWrite (int pin, int value);
|
||||
static int _analogRead (int pin);
|
||||
static void _digitalWriteByte (const int value);
|
||||
static int _digitalWriteByte (const unsigned int value);
|
||||
static unsigned int _digitalReadByte (void);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -262,15 +262,15 @@ static int _getModeToGpio (int mode, int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pinMode (int pin, int mode)
|
||||
static int _pinMode (int pin, int mode)
|
||||
{
|
||||
int fsel, shift, origPin = pin;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
softPwmStop (origPin);
|
||||
softToneStop (origPin);
|
||||
@@ -281,20 +281,22 @@ static void _pinMode (int pin, int mode)
|
||||
switch (mode) {
|
||||
case INPUT:
|
||||
*(gpio + fsel) = (*(gpio + fsel) | (1 << shift));
|
||||
break;
|
||||
break;
|
||||
case OUTPUT:
|
||||
*(gpio + fsel) = (*(gpio + fsel) & ~(1 << shift));
|
||||
break;
|
||||
break;
|
||||
case SOFT_PWM_OUTPUT:
|
||||
softPwmCreate (origPin, 0, 100);
|
||||
break;
|
||||
break;
|
||||
case SOFT_TONE_OUTPUT:
|
||||
softToneCreate (origPin);
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
|
||||
break;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -304,10 +306,10 @@ static int _getAlt (int pin)
|
||||
int mode = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 2;
|
||||
return -1;
|
||||
|
||||
fsel = gpioToGPFSELReg(pin);
|
||||
shift = gpioToShiftReg(pin);
|
||||
@@ -426,10 +428,10 @@ static int _getPUPD (int pin)
|
||||
int puen, pupd, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
puen = gpioToPUENReg(pin);
|
||||
pupd = gpioToPUPDReg(pin);
|
||||
@@ -442,15 +444,15 @@ static int _getPUPD (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pullUpDnControl (int pin, int pud)
|
||||
static int _pullUpDnControl (int pin, int pud)
|
||||
{
|
||||
int shift = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
shift = gpioToShiftReg(pin);
|
||||
|
||||
@@ -468,6 +470,8 @@ static void _pullUpDnControl (int pin, int pud)
|
||||
} else // Disable Pull/Pull-down resister
|
||||
*(gpio + gpioToPUENReg(pin)) =
|
||||
(*(gpio + gpioToPUENReg(pin)) & ~(1 << shift));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -477,16 +481,19 @@ static int _digitalRead (int pin)
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] == -1)
|
||||
return LOW ;
|
||||
return -1;
|
||||
|
||||
lseek (lib->sysFds[pin], 0L, SEEK_SET);
|
||||
read (lib->sysFds[pin], &c, 1);
|
||||
if (read(lib->sysFds[pin], &c, 1) < 0) {
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (c == '0') ? LOW : HIGH;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((*(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
|
||||
return HIGH ;
|
||||
@@ -495,34 +502,39 @@ static int _digitalRead (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWrite (int pin, int value)
|
||||
static int _digitalWrite (int pin, int value)
|
||||
{
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] != -1) {
|
||||
if (value == LOW)
|
||||
write (lib->sysFds[pin], "0\n", 2);
|
||||
else
|
||||
write (lib->sysFds[pin], "1\n", 2);
|
||||
if (value == LOW) {
|
||||
if (write(lib->sysFds[pin], "0\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
} else {
|
||||
if (write(lib->sysFds[pin], "1\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if (value == LOW)
|
||||
*(gpio + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
|
||||
else
|
||||
*(gpio + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _analogRead (int pin)
|
||||
{
|
||||
unsigned char value[5] = {0,};
|
||||
char value[5] = {0,};
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
/* wiringPi ADC number = pin 25, pin 29 */
|
||||
switch (pin) {
|
||||
@@ -548,14 +560,17 @@ static int _analogRead (int pin)
|
||||
if (adcFds [pin] == -1)
|
||||
return 0;
|
||||
|
||||
lseek (adcFds [pin], 0L, SEEK_SET);
|
||||
read (adcFds [pin], &value[0], 4);
|
||||
lseek(adcFds [pin], 0L, SEEK_SET);
|
||||
if (read(adcFds [pin], &value[0], 4) < 0) {
|
||||
msg(MSG_WARN, "%s: Error occurs when it reads from ADC file descriptor. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return atoi(value);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWriteByte (const int value)
|
||||
static int _digitalWriteByte (const unsigned int value)
|
||||
{
|
||||
union reg_bitfield gpiox, gpioy;
|
||||
|
||||
@@ -581,6 +596,8 @@ static void _digitalWriteByte (const int value)
|
||||
|
||||
*(gpio + C1_GPIOX_OUTP_REG_OFFSET) = gpiox.wvalue;
|
||||
*(gpio + C1_GPIOY_OUTP_REG_OFFSET) = gpioy.wvalue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -623,33 +640,37 @@ static unsigned int _digitalReadByte (void)
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void init_gpio_mmap (void)
|
||||
{
|
||||
int fd;
|
||||
int fd = -1;
|
||||
void *mapped;
|
||||
|
||||
/* GPIO mmap setup */
|
||||
if (!getuid()) {
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/mem: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
if (access("/dev/gpiomem",0) == 0) {
|
||||
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
|
||||
strerror (errno));
|
||||
} else
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
|
||||
}
|
||||
|
||||
// #define C1_GPIO_BASE 0xC1108000
|
||||
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, C1_GPIO_BASE) ;
|
||||
if (fd < 0) {
|
||||
msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
|
||||
} else {
|
||||
// #define C1_GPIO_BASE 0xC1108000
|
||||
mapped = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, C1_GPIO_BASE);
|
||||
|
||||
if ((int32_t)gpio == -1)
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GPIO) failed: %s\n",
|
||||
strerror (errno));
|
||||
if (mapped == MAP_FAILED)
|
||||
msg(MSG_ERR, "wiringPiSetup: mmap (GPIO) failed: %s \n", strerror (errno));
|
||||
else
|
||||
gpio = (uint32_t *) mapped;
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
@@ -143,10 +143,10 @@ static const int phyToGpio_rev2[64] = {
|
||||
const int *pinToGpio, *phyToGpio;
|
||||
|
||||
/* ADC file descriptor */
|
||||
static char *adcFds[2];
|
||||
static int adcFds[2];
|
||||
|
||||
/* GPIO mmap control */
|
||||
static volatile uint32_t *gpio, *gpio1;
|
||||
static volatile uint32_t *gpio;
|
||||
|
||||
/* wiringPi Global library */
|
||||
static struct libodroid *lib = NULL;
|
||||
@@ -165,14 +165,14 @@ static int gpioToGPFSELReg (int pin);
|
||||
// wiringPi core function
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _getModeToGpio (int mode, int pin);
|
||||
static void _pinMode (int pin, int mode);
|
||||
static int _pinMode (int pin, int mode);
|
||||
static int _getAlt (int pin);
|
||||
static int _getPUPD (int pin);
|
||||
static void _pullUpDnControl (int pin, int pud);
|
||||
static int _pullUpDnControl (int pin, int pud);
|
||||
static int _digitalRead (int pin);
|
||||
static void _digitalWrite (int pin, int value);
|
||||
static int _digitalWrite (int pin, int value);
|
||||
static int _analogRead (int pin);
|
||||
static void _digitalWriteByte (const int value);
|
||||
static int _digitalWriteByte (const unsigned int value);
|
||||
static unsigned int _digitalReadByte (void);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -316,15 +316,15 @@ static int _getModeToGpio (int mode, int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pinMode (int pin, int mode)
|
||||
static int _pinMode (int pin, int mode)
|
||||
{
|
||||
int fsel, shift, origPin = pin;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
softPwmStop (origPin);
|
||||
softToneStop (origPin);
|
||||
@@ -335,20 +335,22 @@ static void _pinMode (int pin, int mode)
|
||||
switch (mode) {
|
||||
case INPUT:
|
||||
*(gpio + fsel) = (*(gpio + fsel) | (1 << shift));
|
||||
break;
|
||||
break;
|
||||
case OUTPUT:
|
||||
*(gpio + fsel) = (*(gpio + fsel) & ~(1 << shift));
|
||||
break;
|
||||
break;
|
||||
case SOFT_PWM_OUTPUT:
|
||||
softPwmCreate (origPin, 0, 100);
|
||||
break;
|
||||
break;
|
||||
case SOFT_TONE_OUTPUT:
|
||||
softToneCreate (origPin);
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
|
||||
break;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -358,10 +360,10 @@ static int _getAlt (int pin)
|
||||
int mode = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 2;
|
||||
return -1;
|
||||
|
||||
fsel = gpioToGPFSELReg(pin);
|
||||
shift = gpioToShiftReg(pin);
|
||||
@@ -496,10 +498,10 @@ static int _getPUPD (int pin)
|
||||
int puen, pupd, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
puen = gpioToPUENReg(pin);
|
||||
pupd = gpioToPUPDReg(pin);
|
||||
@@ -512,15 +514,15 @@ static int _getPUPD (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pullUpDnControl (int pin, int pud)
|
||||
static int _pullUpDnControl (int pin, int pud)
|
||||
{
|
||||
int shift = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
shift = gpioToShiftReg(pin);
|
||||
|
||||
@@ -538,6 +540,8 @@ static void _pullUpDnControl (int pin, int pud)
|
||||
} else // Disable Pull/Pull-down resister
|
||||
*(gpio + gpioToPUENReg(pin)) =
|
||||
(*(gpio + gpioToPUENReg(pin)) & ~(1 << shift));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -547,16 +551,19 @@ static int _digitalRead (int pin)
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] == -1)
|
||||
return LOW ;
|
||||
return -1;
|
||||
|
||||
lseek (lib->sysFds[pin], 0L, SEEK_SET);
|
||||
read (lib->sysFds[pin], &c, 1);
|
||||
if (read(lib->sysFds[pin], &c, 1) < 0) {
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (c == '0') ? LOW : HIGH;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((*(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
|
||||
return HIGH ;
|
||||
@@ -565,34 +572,39 @@ static int _digitalRead (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWrite (int pin, int value)
|
||||
static int _digitalWrite (int pin, int value)
|
||||
{
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] != -1) {
|
||||
if (value == LOW)
|
||||
write (lib->sysFds[pin], "0\n", 2);
|
||||
else
|
||||
write (lib->sysFds[pin], "1\n", 2);
|
||||
if (value == LOW) {
|
||||
if (write(lib->sysFds[pin], "0\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
} else {
|
||||
if (write(lib->sysFds[pin], "1\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if (value == LOW)
|
||||
*(gpio + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
|
||||
else
|
||||
*(gpio + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _analogRead (int pin)
|
||||
{
|
||||
unsigned char value[5] = {0,};
|
||||
char value[5] = {0,};
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
/* wiringPi ADC number = pin 25, pin 29 */
|
||||
switch (pin) {
|
||||
@@ -618,14 +630,17 @@ static int _analogRead (int pin)
|
||||
if (adcFds [pin] == -1)
|
||||
return 0;
|
||||
|
||||
lseek (adcFds [pin], 0L, SEEK_SET);
|
||||
read (adcFds [pin], &value[0], 4);
|
||||
lseek(adcFds [pin], 0L, SEEK_SET);
|
||||
if (read(adcFds [pin], &value[0], 4) < 0) {
|
||||
msg(MSG_WARN, "%s: Error occurs when it reads from ADC file descriptor. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return atoi(value);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWriteByte (const int value)
|
||||
static int _digitalWriteByte (const unsigned int value)
|
||||
{
|
||||
union reg_bitfield gpiox;
|
||||
|
||||
@@ -649,6 +664,8 @@ static void _digitalWriteByte (const int value)
|
||||
gpiox.bits.bit21 = (value & 0x80);
|
||||
|
||||
*(gpio + C2_GPIOX_OUTP_REG_OFFSET) = gpiox.wvalue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -660,33 +677,39 @@ static unsigned int _digitalReadByte (void)
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void init_gpio_mmap (void)
|
||||
{
|
||||
int fd;
|
||||
int fd = -1;
|
||||
void *mapped;
|
||||
|
||||
/* GPIO mmap setup */
|
||||
if (!getuid()) {
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/mem: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
if (access("/dev/gpiomem",0) == 0) {
|
||||
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
|
||||
strerror (errno));
|
||||
} else
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
|
||||
}
|
||||
|
||||
//#define C2_GPIO_BASE 0xC8834000
|
||||
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, C2_GPIO_BASE) ;
|
||||
|
||||
if ((int32_t)gpio == -1)
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GPIO) failed: %s\n",
|
||||
strerror (errno));
|
||||
|
||||
if (fd < 0) {
|
||||
msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
|
||||
} else {
|
||||
//#define C2_GPIO_BASE 0xC8834000
|
||||
mapped = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, C2_GPIO_BASE);
|
||||
|
||||
if (mapped == MAP_FAILED)
|
||||
msg(MSG_ERR, "wiringPiSetup: mmap (GPIO) failed: %s \n", strerror (errno));
|
||||
else
|
||||
gpio = (uint32_t *) mapped;
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
@@ -88,7 +88,7 @@ static const int phyToGpio[64] = {
|
||||
//
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* ADC file descriptor */
|
||||
static char *adcFds[2];
|
||||
static int adcFds[2];
|
||||
|
||||
/* GPIO mmap control. Actual GPIO bank number. */
|
||||
static volatile uint32_t *gpio[5];
|
||||
@@ -114,13 +114,13 @@ static void setIomuxMode (int pin, int mode);
|
||||
// wiringPi core function
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _getModeToGpio (int mode, int pin);
|
||||
static void _pinMode (int pin, int mode);
|
||||
static int _pinMode (int pin, int mode);
|
||||
static int _getAlt (int pin);
|
||||
static void _pullUpDnControl (int pin, int pud);
|
||||
static int _pullUpDnControl (int pin, int pud);
|
||||
static int _digitalRead (int pin);
|
||||
static void _digitalWrite (int pin, int value);
|
||||
static int _digitalWrite (int pin, int value);
|
||||
static int _analogRead (int pin);
|
||||
static void _digitalWriteByte (const int value);
|
||||
static int _digitalWriteByte (const unsigned int value);
|
||||
static unsigned int _digitalReadByte (void);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -263,16 +263,16 @@ static void setIomuxMode (int pin, int mode)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pinMode (int pin, int mode)
|
||||
static int _pinMode (int pin, int mode)
|
||||
{
|
||||
int origPin, bank;
|
||||
unsigned long UNU flags;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
origPin = pin;
|
||||
bank = pin / 32;
|
||||
@@ -298,10 +298,12 @@ static void _pinMode (int pin, int mode)
|
||||
break;
|
||||
default:
|
||||
msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
|
||||
break;
|
||||
return -1;
|
||||
}
|
||||
|
||||
setClkState (pin, N1_CLK_DISABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -312,10 +314,10 @@ static int _getAlt (int pin)
|
||||
uint8_t ret = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 2;
|
||||
return -1;
|
||||
|
||||
bank = pin / 32;
|
||||
group = (pin - bank * 32) / 8;
|
||||
@@ -347,16 +349,16 @@ static int _getAlt (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pullUpDnControl (int pin, int pud)
|
||||
static int _pullUpDnControl (int pin, int pud)
|
||||
{
|
||||
uint32_t offset, target;
|
||||
uint8_t bank, group;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 2;
|
||||
return -1;
|
||||
|
||||
bank = pin / 32;
|
||||
group = (pin - bank * 32) / 8;
|
||||
@@ -441,6 +443,8 @@ static void _pullUpDnControl (int pin, int pud)
|
||||
}
|
||||
|
||||
setClkState(pin, N1_CLK_DISABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -451,16 +455,19 @@ static int _digitalRead (int pin)
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] == -1)
|
||||
return LOW;
|
||||
return -1;
|
||||
|
||||
lseek (lib->sysFds[pin], 0L, SEEK_SET);
|
||||
read (lib->sysFds[pin], &c, 1);
|
||||
if (read(lib->sysFds[pin], &c, 1) < 0) {
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (c == '0') ? LOW : HIGH;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
bank = pin / 32;
|
||||
setClkState(pin, N1_CLK_ENABLE);
|
||||
@@ -472,7 +479,7 @@ static int _digitalRead (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWrite (int pin, int value)
|
||||
static int _digitalWrite (int pin, int value)
|
||||
{
|
||||
int bank;
|
||||
|
||||
@@ -490,11 +497,11 @@ static void _digitalWrite (int pin, int value)
|
||||
__func__, strerror(errno), pin + N1_GPIO_PIN_BASE);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
bank = pin / 32;
|
||||
setClkState(pin, N1_CLK_ENABLE);
|
||||
@@ -511,15 +518,17 @@ static void _digitalWrite (int pin, int value)
|
||||
}
|
||||
|
||||
setClkState(pin, N1_CLK_DISABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _analogRead (int pin)
|
||||
{
|
||||
unsigned char value[5] = {0,};
|
||||
char value[5] = {0,};
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
/* wiringPi ADC number = pin 25, pin 29 */
|
||||
switch (pin) {
|
||||
@@ -545,19 +554,22 @@ static int _analogRead (int pin)
|
||||
if (adcFds [pin] == -1)
|
||||
return 0;
|
||||
|
||||
lseek (adcFds [pin], 0L, SEEK_SET);
|
||||
read (adcFds [pin], &value[0], 4);
|
||||
lseek(adcFds [pin], 0L, SEEK_SET);
|
||||
if (read(adcFds [pin], &value[0], 4) < 0) {
|
||||
msg(MSG_WARN, "%s: Error occurs when it reads from ADC file descriptor. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return atoi(value);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWriteByte (const int value)
|
||||
static int _digitalWriteByte (const unsigned int value)
|
||||
{
|
||||
union reg_bitfield gpioBits1;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Enable clock for GPIO 1 bank
|
||||
@@ -587,6 +599,8 @@ static void _digitalWriteByte (const int value)
|
||||
*(gpio[1] + (N1_GPIO_SET_OFFSET >> 2)) = gpioBits1.wvalue;
|
||||
|
||||
setClkState(32, N1_CLK_DISABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -638,90 +652,98 @@ static unsigned int _digitalReadByte (void)
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void init_gpio_mmap (void)
|
||||
{
|
||||
int fd;
|
||||
int fd = -1;
|
||||
void *mapped_cru[2], *mapped_grf[2], *mapped_gpio[5];
|
||||
|
||||
/* GPIO mmap setup */
|
||||
if (!getuid()) {
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/mem: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
if (access("/dev/gpiomem",0) == 0) {
|
||||
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
|
||||
strerror (errno));
|
||||
} else
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
|
||||
}
|
||||
|
||||
// GPIO{0, 1}
|
||||
//#define ODROIDN1_PMUCRU_BASE 0xFF750000
|
||||
cru[0] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_PMUCRU_BASE) ;
|
||||
if (fd < 0) {
|
||||
msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
|
||||
} else {
|
||||
// GPIO{0, 1}
|
||||
//#define ODROIDN1_PMUCRU_BASE 0xFF750000
|
||||
mapped_cru[0] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_PMUCRU_BASE);
|
||||
|
||||
// GPIO{2, 3, 4}
|
||||
//#define ODROIDN1_CRU_BASE 0xFF760000
|
||||
cru[1] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_CRU_BASE) ;
|
||||
// GPIO{2, 3, 4}
|
||||
//#define ODROIDN1_CRU_BASE 0xFF760000
|
||||
mapped_cru[1] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_CRU_BASE);
|
||||
|
||||
// GPIO{0, 1}
|
||||
//#define ODROIDN1_PMU_BASE 0xFF320000
|
||||
grf[0] = (uint32_t *)mmap(0, N1_GRF_BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_PMUGRF_BASE) ;
|
||||
// GPIO{0, 1}
|
||||
//#define ODROIDN1_PMU_BASE 0xFF320000
|
||||
mapped_grf[0] = mmap(0, N1_GRF_BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_PMUGRF_BASE);
|
||||
|
||||
// GPIO{2, 3, 4}
|
||||
//#define ODROIDN1_GRF_BASE 0xFF770000
|
||||
grf[1] = (uint32_t *)mmap(0, N1_GRF_BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_GRF_BASE) ;
|
||||
// GPIO{2, 3, 4}
|
||||
//#define ODROIDN1_GRF_BASE 0xFF770000
|
||||
mapped_grf[1] = mmap(0, N1_GRF_BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_GRF_BASE);
|
||||
|
||||
// GPIO1_A. 0,1,2,3,4,7
|
||||
// GPIO1_B. 0,1,2,3,4,5
|
||||
// GPIO1_C. 2,4,5,6
|
||||
// GPIO1_D. 0
|
||||
//#define ODROIDN1_GPIO1_BASE 0xFF730000
|
||||
gpio[1] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_GPIO_1_BASE) ;
|
||||
// GPIO1_A. 0,1,2,3,4,7
|
||||
// GPIO1_B. 0,1,2,3,4,5
|
||||
// GPIO1_C. 2,4,5,6
|
||||
// GPIO1_D. 0
|
||||
//#define ODROIDN1_GPIO1_BASE 0xFF730000
|
||||
mapped_gpio[1] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_GPIO_1_BASE);
|
||||
|
||||
// GPIO2_C. 0_B,1_B
|
||||
//#define ODROIDN1_GPIO2_BASE 0xFF780000
|
||||
gpio[2] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_GPIO_2_BASE) ;
|
||||
// GPIO2_C. 0_B,1_B
|
||||
//#define ODROIDN1_GPIO2_BASE 0xFF780000
|
||||
mapped_gpio[2] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_GPIO_2_BASE);
|
||||
|
||||
// GPIO4_C. 5,6
|
||||
// GPIO4_D. 0,4,5,6
|
||||
//#define ODROIDN1_GPIO4_BASE 0xFF790000
|
||||
gpio[4] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_GPIO_4_BASE) ;
|
||||
// GPIO4_C. 5,6
|
||||
// GPIO4_D. 0,4,5,6
|
||||
//#define ODROIDN1_GPIO4_BASE 0xFF790000
|
||||
mapped_gpio[4] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_GPIO_4_BASE);
|
||||
|
||||
// Reserved
|
||||
gpio[0] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_GPIO_0_BASE) ;
|
||||
gpio[3] = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N1_GPIO_3_BASE) ;
|
||||
// Reserved
|
||||
mapped_gpio[0] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_GPIO_0_BASE);
|
||||
mapped_gpio[3] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N1_GPIO_3_BASE);
|
||||
|
||||
if (((int32_t)cru[0] == -1) || ((int32_t)cru[1] == -1)) {
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (CRU) failed: %s\n",
|
||||
strerror (errno));
|
||||
}
|
||||
if ((mapped_cru[0] == MAP_FAILED) || (mapped_cru[1] == MAP_FAILED)) {
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (CRU) failed: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
cru[0] = (uint32_t *) mapped_cru[0];
|
||||
cru[1] = (uint32_t *) mapped_cru[1];
|
||||
}
|
||||
|
||||
if (((int32_t)grf[0] == -1) || ((int32_t)grf[1] == -1)) {
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GRF) failed: %s\n",
|
||||
strerror (errno));
|
||||
}
|
||||
if ((mapped_grf[0] == MAP_FAILED) || (mapped_grf[1] == MAP_FAILED)) {
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GRF) failed: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
grf[0] = (uint32_t *) mapped_grf[0];
|
||||
grf[1] = (uint32_t *) mapped_grf[1];
|
||||
}
|
||||
|
||||
if ( ((int32_t)gpio[0] == -1) ||
|
||||
((int32_t)gpio[1] == -1) ||
|
||||
((int32_t)gpio[2] == -1) ||
|
||||
((int32_t)gpio[3] == -1) ||
|
||||
((int32_t)gpio[4] == -1)) {
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GPIO) failed: %s\n",
|
||||
strerror (errno));
|
||||
if ( (mapped_gpio[0] == MAP_FAILED) ||
|
||||
(mapped_gpio[1] == MAP_FAILED) ||
|
||||
(mapped_gpio[2] == MAP_FAILED) ||
|
||||
(mapped_gpio[3] == MAP_FAILED) ||
|
||||
(mapped_gpio[4] == MAP_FAILED)) {
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GPIO) failed: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
gpio[0] = (uint32_t *) mapped_gpio[0];
|
||||
gpio[1] = (uint32_t *) mapped_gpio[1];
|
||||
gpio[2] = (uint32_t *) mapped_gpio[2];
|
||||
gpio[3] = (uint32_t *) mapped_gpio[3];
|
||||
gpio[4] = (uint32_t *) mapped_gpio[4];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,10 +93,10 @@ static const int phyToGpio_rev1[64] = {
|
||||
const int *pinToGpio, *phyToGpio;
|
||||
|
||||
/* ADC file descriptor */
|
||||
static char *adcFds[2];
|
||||
static int adcFds[2];
|
||||
|
||||
/* GPIO mmap control */
|
||||
static volatile uint32_t *gpio, *gpio1;
|
||||
static volatile uint32_t *gpio;
|
||||
|
||||
/* wiringPi Global library */
|
||||
static struct libodroid *lib = NULL;
|
||||
@@ -117,16 +117,16 @@ static int gpioToMuxReg (int pin);
|
||||
// wiringPi core function
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _getModeToGpio (int mode, int pin);
|
||||
static void _setPadDrive (int pin, int value);
|
||||
static int _setPadDrive (int pin, int value);
|
||||
static int _getPadDrive (int pin);
|
||||
static void _pinMode (int pin, int mode);
|
||||
static int _pinMode (int pin, int mode);
|
||||
static int _getAlt (int pin);
|
||||
static int _getPUPD (int pin);
|
||||
static void _pullUpDnControl (int pin, int pud);
|
||||
static int _pullUpDnControl (int pin, int pud);
|
||||
static int _digitalRead (int pin);
|
||||
static void _digitalWrite (int pin, int value);
|
||||
static int _digitalWrite (int pin, int value);
|
||||
static int _analogRead (int pin);
|
||||
static void _digitalWriteByte (const int value);
|
||||
static int _digitalWriteByte (const unsigned int value);
|
||||
static unsigned int _digitalReadByte (void);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -292,19 +292,19 @@ static int _getModeToGpio (int mode, int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _setPadDrive (int pin, int value)
|
||||
static int _setPadDrive (int pin, int value)
|
||||
{
|
||||
int ds, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if (value < 0 || value > 3) {
|
||||
msg(MSG_WARN, "%s : Invalid value %d (Must be 0 ~ 3)\n", __func__, value);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ds = gpioToDSReg(pin);
|
||||
@@ -313,6 +313,8 @@ static void _setPadDrive (int pin, int value)
|
||||
|
||||
*(gpio + ds) &= ~(0b11 << shift);
|
||||
*(gpio + ds) |= (value << shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -321,10 +323,10 @@ static int _getPadDrive (int pin)
|
||||
int ds, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
ds = gpioToDSReg(pin);
|
||||
shift = gpioToShiftReg(pin);
|
||||
@@ -334,15 +336,15 @@ static int _getPadDrive (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pinMode (int pin, int mode)
|
||||
static int _pinMode (int pin, int mode)
|
||||
{
|
||||
int fsel, shift, origPin = pin;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
softPwmStop (origPin);
|
||||
softToneStop (origPin);
|
||||
@@ -353,20 +355,22 @@ static void _pinMode (int pin, int mode)
|
||||
switch (mode) {
|
||||
case INPUT:
|
||||
*(gpio + fsel) = (*(gpio + fsel) | (1 << shift));
|
||||
break;
|
||||
break;
|
||||
case OUTPUT:
|
||||
*(gpio + fsel) = (*(gpio + fsel) & ~(1 << shift));
|
||||
break;
|
||||
break;
|
||||
case SOFT_PWM_OUTPUT:
|
||||
softPwmCreate (origPin, 0, 100);
|
||||
break;
|
||||
break;
|
||||
case SOFT_TONE_OUTPUT:
|
||||
softToneCreate (origPin);
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
|
||||
break;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -375,10 +379,10 @@ static int _getAlt (int pin)
|
||||
int fsel, mux, shift, target, mode;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 2;
|
||||
return -1;
|
||||
|
||||
fsel = gpioToGPFSELReg(pin);
|
||||
mux = gpioToMuxReg(pin);
|
||||
@@ -398,10 +402,10 @@ static int _getPUPD (int pin)
|
||||
int puen, pupd, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
puen = gpioToPUENReg(pin);
|
||||
pupd = gpioToPUPDReg(pin);
|
||||
@@ -414,15 +418,15 @@ static int _getPUPD (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pullUpDnControl (int pin, int pud)
|
||||
static int _pullUpDnControl (int pin, int pud)
|
||||
{
|
||||
int shift = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
shift = gpioToShiftReg(pin);
|
||||
|
||||
@@ -440,6 +444,8 @@ static void _pullUpDnControl (int pin, int pud)
|
||||
} else // Disable Pull/Pull-down resister
|
||||
*(gpio + gpioToPUENReg(pin)) =
|
||||
(*(gpio + gpioToPUENReg(pin)) & ~(1 << shift));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -449,16 +455,19 @@ static int _digitalRead (int pin)
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] == -1)
|
||||
return LOW ;
|
||||
return -1;
|
||||
|
||||
lseek (lib->sysFds[pin], 0L, SEEK_SET);
|
||||
read (lib->sysFds[pin], &c, 1);
|
||||
if (read(lib->sysFds[pin], &c, 1) < 0) {
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (c == '0') ? LOW : HIGH;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((*(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin))) != 0)
|
||||
return HIGH ;
|
||||
@@ -467,34 +476,39 @@ static int _digitalRead (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWrite (int pin, int value)
|
||||
static int _digitalWrite (int pin, int value)
|
||||
{
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] != -1) {
|
||||
if (value == LOW)
|
||||
write (lib->sysFds[pin], "0\n", 2);
|
||||
else
|
||||
write (lib->sysFds[pin], "1\n", 2);
|
||||
if (value == LOW) {
|
||||
if (write(lib->sysFds[pin], "0\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
} else {
|
||||
if (write(lib->sysFds[pin], "1\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if (value == LOW)
|
||||
*(gpio + gpioToGPSETReg(pin)) &= ~(1 << gpioToShiftReg(pin));
|
||||
else
|
||||
*(gpio + gpioToGPSETReg(pin)) |= (1 << gpioToShiftReg(pin));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _analogRead (int pin)
|
||||
{
|
||||
unsigned char value[5] = {0,};
|
||||
char value[5] = {0,};
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
/* wiringPi ADC number = pin 25, pin 29 */
|
||||
switch (pin) {
|
||||
@@ -520,14 +534,17 @@ static int _analogRead (int pin)
|
||||
if (adcFds [pin] == -1)
|
||||
return 0;
|
||||
|
||||
lseek (adcFds [pin], 0L, SEEK_SET);
|
||||
read (adcFds [pin], &value[0], 4);
|
||||
lseek(adcFds [pin], 0L, SEEK_SET);
|
||||
if (read(adcFds [pin], &value[0], 4) < 0) {
|
||||
msg(MSG_WARN, "%s: Error occurs when it reads from ADC file descriptor. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return atoi(value);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWriteByte (const int value)
|
||||
static int _digitalWriteByte (const unsigned int value)
|
||||
{
|
||||
union reg_bitfield gpiox;
|
||||
union reg_bitfield gpioa;
|
||||
@@ -554,6 +571,8 @@ static void _digitalWriteByte (const int value)
|
||||
|
||||
*(gpio + N2_GPIOX_OUTP_REG_OFFSET) = gpiox.wvalue;
|
||||
*(gpio + N2_GPIOA_OUTP_REG_OFFSET) = gpioa.wvalue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -565,33 +584,37 @@ static unsigned int _digitalReadByte (void)
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void init_gpio_mmap (void)
|
||||
{
|
||||
int fd;
|
||||
int fd = -1;
|
||||
void *mapped;
|
||||
|
||||
/* GPIO mmap setup */
|
||||
if (!getuid()) {
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg(MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/mem: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
if (access("/dev/gpiomem",0) == 0) {
|
||||
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg(MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
|
||||
strerror (errno));
|
||||
} else
|
||||
return msg (MSG_ERR,
|
||||
msg(MSG_ERR,
|
||||
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
|
||||
}
|
||||
|
||||
//#define N2_GPIO_BASE 0xff634000
|
||||
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, N2_GPIO_BASE) ;
|
||||
if (fd < 0) {
|
||||
msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
|
||||
} else {
|
||||
//#define N2_GPIO_BASE 0xff634000
|
||||
mapped = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, N2_GPIO_BASE);
|
||||
|
||||
if ((int32_t)gpio == -1)
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GPIO) failed: %s\n",
|
||||
strerror (errno));
|
||||
if (mapped == MAP_FAILED)
|
||||
msg(MSG_ERR, "wiringPiSetup: mmap (GPIO) failed: %s \n", strerror (errno));
|
||||
else
|
||||
gpio = (uint32_t *) mapped;
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
@@ -88,7 +88,7 @@ static const int phyToGpio[64] = {
|
||||
//
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* ADC file descriptor */
|
||||
static char *adcFds[2];
|
||||
static int adcFds[2];
|
||||
|
||||
/* GPIO mmap control */
|
||||
static volatile uint32_t *gpio, *gpio1;
|
||||
@@ -99,7 +99,6 @@ static struct libodroid *lib = NULL;
|
||||
/*----------------------------------------------------------------------------*/
|
||||
// Function prototype define
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int gpioToGPSETReg (int pin);
|
||||
static int gpioToGPLEVReg (int pin);
|
||||
static int gpioToPUPDReg (int pin);
|
||||
static int gpioToShiftReg (int pin);
|
||||
@@ -110,16 +109,16 @@ static int gpioToDSReg (int pin);
|
||||
// wiringPi core function
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _getModeToGpio (int mode, int pin);
|
||||
static void _setPadDrive (int pin, int value);
|
||||
static int _setPadDrive (int pin, int value);
|
||||
static int _getPadDrive (int pin);
|
||||
static void _pinMode (int pin, int mode);
|
||||
static int _pinMode (int pin, int mode);
|
||||
static int _getAlt (int pin);
|
||||
static int _getPUPD (int pin);
|
||||
static void _pullUpDnControl (int pin, int pud);
|
||||
static int _pullUpDnControl (int pin, int pud);
|
||||
static int _digitalRead (int pin);
|
||||
static void _digitalWrite (int pin, int value);
|
||||
static int _digitalWrite (int pin, int value);
|
||||
static int _analogRead (int pin);
|
||||
static void _digitalWriteByte (const int value);
|
||||
static int _digitalWriteByte (const unsigned int value);
|
||||
static unsigned int _digitalReadByte (void);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -130,31 +129,6 @@ static void init_adc_fds (void);
|
||||
void init_odroidxu3 (struct libodroid *libwiring);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
//
|
||||
// offset to the GPIO Set regsiter
|
||||
//
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int gpioToGPSETReg (int pin)
|
||||
{
|
||||
switch (pin) {
|
||||
case XU3_GPIO_X1_START...XU3_GPIO_X1_END:
|
||||
return (XU3_GPIO_X1_DAT_OFFSET >> 2);
|
||||
case XU3_GPIO_X2_START...XU3_GPIO_X2_END:
|
||||
return (XU3_GPIO_X2_DAT_OFFSET >> 2);
|
||||
case XU3_GPIO_X3_START...XU3_GPIO_X3_END:
|
||||
return (XU3_GPIO_X3_DAT_OFFSET >> 2);
|
||||
case XU3_GPIO_A0_START...XU3_GPIO_A0_END:
|
||||
return (XU3_GPIO_A0_DAT_OFFSET >> 2);
|
||||
case XU3_GPIO_A2_START...XU3_GPIO_A2_END:
|
||||
return (XU3_GPIO_A2_DAT_OFFSET >> 2);
|
||||
case XU3_GPIO_B3_START...XU3_GPIO_B3_END:
|
||||
return (XU3_GPIO_B3_DAT_OFFSET >> 2);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
//
|
||||
@@ -310,19 +284,19 @@ static int _getModeToGpio (int mode, int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _setPadDrive (int pin, int value)
|
||||
static int _setPadDrive (int pin, int value)
|
||||
{
|
||||
int ds, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if (value < 0 || value > 3) {
|
||||
msg(MSG_WARN, "%s : Invalid value %d (Must be 0 ~ 3)\n", __func__, value);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ds = gpioToDSReg(pin);
|
||||
@@ -335,6 +309,8 @@ static void _setPadDrive (int pin, int value)
|
||||
*(gpio1 + ds) &= ~(0b11 << shift);
|
||||
*(gpio1 + ds) |= (value << shift);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -343,10 +319,10 @@ static int _getPadDrive (int pin)
|
||||
int ds, shift;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
ds = gpioToDSReg(pin);
|
||||
shift = gpioToShiftReg(pin) << 1;
|
||||
@@ -358,15 +334,15 @@ static int _getPadDrive (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pinMode (int pin, int mode)
|
||||
static int _pinMode (int pin, int mode)
|
||||
{
|
||||
int fsel, shift, origPin = pin;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
softPwmStop (origPin);
|
||||
softToneStop (origPin);
|
||||
@@ -380,7 +356,7 @@ static void _pinMode (int pin, int mode)
|
||||
*(gpio + fsel) &= ~(0xF << shift);
|
||||
else
|
||||
*(gpio1 + fsel) &= ~(0xF << shift);
|
||||
break;
|
||||
break;
|
||||
case OUTPUT:
|
||||
if(pin < 100) {
|
||||
*(gpio + fsel) &= ~(0xF << shift);
|
||||
@@ -389,17 +365,19 @@ static void _pinMode (int pin, int mode)
|
||||
*(gpio1 + fsel) &= ~(0xF << shift);
|
||||
*(gpio1 + fsel) |= (0x1 << shift);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case SOFT_PWM_OUTPUT:
|
||||
softPwmCreate (origPin, 0, 100);
|
||||
break;
|
||||
break;
|
||||
case SOFT_TONE_OUTPUT:
|
||||
softToneCreate (origPin);
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
msg(MSG_WARN, "%s : Unknown Mode %d\n", __func__, mode);
|
||||
break;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -408,10 +386,10 @@ static int _getAlt (int pin)
|
||||
int fsel, shift, mode;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 2;
|
||||
return -1;
|
||||
|
||||
fsel = gpioToGPFSELReg(pin);
|
||||
shift = gpioToShiftReg(pin) << 2;
|
||||
@@ -431,10 +409,10 @@ static int _getPUPD (int pin)
|
||||
int pupd, shift, pull;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
pupd = gpioToPUPDReg(pin);
|
||||
shift = gpioToShiftReg(pin) << 1;
|
||||
@@ -449,15 +427,15 @@ static int _getPUPD (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _pullUpDnControl (int pin, int pud)
|
||||
static int _pullUpDnControl (int pin, int pud)
|
||||
{
|
||||
int shift = 0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
shift = gpioToShiftReg(pin) << 1;
|
||||
|
||||
@@ -482,6 +460,8 @@ static void _pullUpDnControl (int pin, int pud)
|
||||
else
|
||||
*(gpio1 + gpioToPUPDReg(pin)) &= ~(0x3 << shift);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -491,16 +471,19 @@ static int _digitalRead (int pin)
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] == -1)
|
||||
return LOW ;
|
||||
return -1;
|
||||
|
||||
lseek (lib->sysFds[pin], 0L, SEEK_SET);
|
||||
read (lib->sysFds[pin], &c, 1);
|
||||
if (read(lib->sysFds[pin], &c, 1) < 0) {
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (c == '0') ? LOW : HIGH;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
if (pin < 100)
|
||||
return *(gpio + gpioToGPLEVReg(pin)) & (1 << gpioToShiftReg(pin)) ? HIGH : LOW;
|
||||
@@ -509,20 +492,23 @@ static int _digitalRead (int pin)
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWrite (int pin, int value)
|
||||
static int _digitalWrite (int pin, int value)
|
||||
{
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
if (lib->sysFds[pin] != -1) {
|
||||
if (value == LOW)
|
||||
write (lib->sysFds[pin], "0\n", 2);
|
||||
else
|
||||
write (lib->sysFds[pin], "1\n", 2);
|
||||
if (value == LOW) {
|
||||
if (write(lib->sysFds[pin], "0\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
} else {
|
||||
if (write(lib->sysFds[pin], "1\n", 2) < 0)
|
||||
msg(MSG_WARN, "%s: Failed with reading from sysfs GPIO node. \n", __func__);
|
||||
}
|
||||
}
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((pin = _getModeToGpio(lib->mode, pin)) < 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
if (pin < 100) {
|
||||
if (value == LOW)
|
||||
@@ -535,15 +521,17 @@ static void _digitalWrite (int pin, int value)
|
||||
else
|
||||
*(gpio1 + gpioToGPLEVReg(pin)) |= (1 << gpioToShiftReg(pin));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static int _analogRead (int pin)
|
||||
{
|
||||
unsigned char value[5] = {0,};
|
||||
char value[5] = {0,};
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS)
|
||||
return 0;
|
||||
return -1;
|
||||
|
||||
/* wiringPi ADC number = pin 25, pin 29 */
|
||||
switch (pin) {
|
||||
@@ -569,19 +557,22 @@ static int _analogRead (int pin)
|
||||
if (adcFds [pin] == -1)
|
||||
return 0;
|
||||
|
||||
lseek (adcFds [pin], 0L, SEEK_SET);
|
||||
read (adcFds [pin], &value[0], 4);
|
||||
lseek(adcFds [pin], 0L, SEEK_SET);
|
||||
if (read(adcFds [pin], &value[0], 4) < 0) {
|
||||
msg(MSG_WARN, "%s: Error occurs when it reads from ADC file descriptor. \n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return atoi(value);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void _digitalWriteByte (const int value)
|
||||
static int _digitalWriteByte (const unsigned int value)
|
||||
{
|
||||
union reg_bitfield gpx1, gpx2, gpa0;
|
||||
|
||||
if (lib->mode == MODE_GPIO_SYS) {
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
/* Read data register */
|
||||
gpx1.wvalue = *(gpio + (XU3_GPIO_X1_DAT_OFFSET >> 2));
|
||||
@@ -609,6 +600,8 @@ static void _digitalWriteByte (const int value)
|
||||
*(gpio + (XU3_GPIO_X1_DAT_OFFSET >> 2)) = gpx1.wvalue;
|
||||
*(gpio + (XU3_GPIO_X2_DAT_OFFSET >> 2)) = gpx2.wvalue;
|
||||
*(gpio1 + (XU3_GPIO_A0_DAT_OFFSET >> 2)) = gpa0.wvalue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -656,35 +649,41 @@ static unsigned int _digitalReadByte (void)
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void init_gpio_mmap (void)
|
||||
{
|
||||
int fd;
|
||||
int fd = -1;
|
||||
void *mapped[2];
|
||||
|
||||
/* GPIO mmap setup */
|
||||
if (!getuid()) {
|
||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/mem: %s\n",
|
||||
strerror (errno));
|
||||
} else {
|
||||
if (access("/dev/gpiomem",0) == 0) {
|
||||
if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) < 0)
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: Unable to open /dev/gpiomem: %s\n",
|
||||
strerror (errno));
|
||||
} else
|
||||
return msg (MSG_ERR,
|
||||
msg (MSG_ERR,
|
||||
"wiringPiSetup: /dev/gpiomem doesn't exist. Please try again with sudo.\n");
|
||||
}
|
||||
|
||||
//#define ODROIDXU_GPX_BASE 0x13400000 // GPX0,1,2,3
|
||||
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, XU3_GPX_BASE) ;
|
||||
//#define ODROIDXU_GPA_BASE 0x14010000 // GPA0,1,2, GPB0,1,2,3,4
|
||||
gpio1 = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE,
|
||||
MAP_SHARED, fd, XU3_GPA_BASE) ;
|
||||
if (((int32_t)gpio == -1) || ((int32_t)gpio1 == -1))
|
||||
return msg (MSG_ERR,
|
||||
"wiringPiSetup: mmap (GPIO) failed: %s\n",
|
||||
strerror (errno));
|
||||
if (fd < 0) {
|
||||
msg(MSG_ERR, "wiringPiSetup: Cannot open memory area for GPIO use. \n");
|
||||
} else {
|
||||
//#define ODROIDXU_GPX_BASE 0x13400000 // GPX0,1,2,3
|
||||
mapped[0] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, XU3_GPX_BASE);
|
||||
//#define ODROIDXU_GPA_BASE 0x14010000 // GPA0,1,2, GPB0,1,2,3,4
|
||||
mapped[1] = mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, XU3_GPA_BASE);
|
||||
|
||||
if (mapped[0] == MAP_FAILED || mapped[1] == MAP_FAILED) {
|
||||
msg(MSG_ERR, "wiringPiSetup: mmap (GPIO) failed: %s \n", strerror (errno));
|
||||
} else {
|
||||
gpio = (uint32_t *) mapped[0];
|
||||
gpio1 = (uint32_t *) mapped[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "wiringPi.h"
|
||||
#include "wiringPiI2C.h"
|
||||
@@ -41,7 +43,9 @@ static void myAnalogWrite (struct wiringPiNodeStruct *node, UNU int pin, int val
|
||||
unsigned char b [2] ;
|
||||
b [0] = 0x40 ;
|
||||
b [1] = value & 0xFF ;
|
||||
write (node->fd, b, 2) ;
|
||||
if (write(node->fd, b, 2) < 0) {
|
||||
fprintf(stderr, "Unable to write to the file descriptor: %s \n", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#define PSEUDO_PINS 64
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -47,7 +48,7 @@
|
||||
|
||||
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
{
|
||||
int *ptr = (int *)node->data0 ;
|
||||
int *ptr = (int *)(uintptr_t) node->data0 ;
|
||||
int myPin = pin - node->pinBase ;
|
||||
|
||||
return *(ptr + myPin) ;
|
||||
@@ -56,7 +57,7 @@ static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
|
||||
|
||||
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
|
||||
{
|
||||
int *ptr = (int *)node->data0 ;
|
||||
int *ptr = (int *)(uintptr_t) node->data0 ;
|
||||
int myPin = pin - node->pinBase ;
|
||||
|
||||
*(ptr + myPin) = value ;
|
||||
@@ -86,7 +87,7 @@ int pseudoPinsSetup (const int pinBase)
|
||||
|
||||
ptr = mmap (NULL, PSEUDO_PINS * sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0) ;
|
||||
|
||||
node->data0 = (unsigned int)ptr ;
|
||||
node->data0 = (unsigned int)(uintptr_t) ptr ;
|
||||
|
||||
node->analogRead = myAnalogRead ;
|
||||
node->analogWrite = myAnalogWrite ;
|
||||
|
||||
@@ -366,7 +366,9 @@ int piGpioLayout (void)
|
||||
printf ("ERROR : file not found.(boardrev)\n");
|
||||
libwiring.rev = 1;
|
||||
} else {
|
||||
(void)read (fd, buf, sizeof(buf));
|
||||
if (read(fd, buf, sizeof(buf)) < 0) {
|
||||
fprintf(stderr, "Unable to read from the file descriptor: %s \n", strerror(errno));
|
||||
}
|
||||
close(fd);
|
||||
libwiring.rev = atoi(buf) + 1;
|
||||
}
|
||||
@@ -467,7 +469,8 @@ int physPinToGpio (int physPin)
|
||||
void setPadDrive (int pin, int value)
|
||||
{
|
||||
if (libwiring.setPadDrive)
|
||||
return libwiring.setPadDrive(pin, value);
|
||||
if (libwiring.setPadDrive(pin, value) < 0)
|
||||
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -522,7 +525,8 @@ int getPUPD (int pin)
|
||||
void pinMode (int pin, int mode)
|
||||
{
|
||||
if (libwiring.pinMode)
|
||||
return libwiring.pinMode(pin, mode);
|
||||
if (libwiring.pinMode(pin, mode) < 0)
|
||||
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
|
||||
|
||||
}
|
||||
|
||||
@@ -530,7 +534,8 @@ void pinMode (int pin, int mode)
|
||||
void pullUpDnControl (int pin, int pud)
|
||||
{
|
||||
if (libwiring.pullUpDnControl)
|
||||
return libwiring.pullUpDnControl(pin, pud);
|
||||
if (libwiring.pullUpDnControl(pin, pud) < 0)
|
||||
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -546,7 +551,8 @@ int digitalRead (int pin)
|
||||
void digitalWrite (int pin, int value)
|
||||
{
|
||||
if (libwiring.digitalWrite)
|
||||
return libwiring.digitalWrite(pin, value);
|
||||
if (libwiring.digitalWrite(pin, value) < 0)
|
||||
msg(MSG_WARN, "%s: Not available for pin %d. \n", __func__, pin);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -562,7 +568,8 @@ int analogRead (int pin)
|
||||
void digitalWriteByte (const int value)
|
||||
{
|
||||
if (libwiring.digitalWriteByte)
|
||||
return libwiring.digitalWriteByte(value);
|
||||
if (libwiring.digitalWriteByte(value) < 0)
|
||||
msg(MSG_WARN, "%s: Not available. \n", __func__);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
@@ -595,7 +602,9 @@ int waitForInterrupt (int pin, int mS)
|
||||
// A one character read appars to be enough.
|
||||
if (x > 0) {
|
||||
lseek (fd, 0, SEEK_SET) ; // Rewind
|
||||
(void)read (fd, &c, 1) ; // Read & clear
|
||||
if (read (fd, &c, 1) < 0) { // Read & clear
|
||||
fprintf(stderr, "Unable to read from the file descriptor: %s \n", strerror(errno));
|
||||
}
|
||||
}
|
||||
return x ;
|
||||
}
|
||||
@@ -706,7 +715,9 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
|
||||
// Clear any initial pending interrupt
|
||||
ioctl (libwiring.sysFds [PIN_NUM_CALC_SYSFD(GpioPin)], FIONREAD, &count) ;
|
||||
for (i = 0 ; i < count ; ++i)
|
||||
(void)read (libwiring.sysFds [PIN_NUM_CALC_SYSFD(GpioPin)], &c, 1) ;
|
||||
if (read(libwiring.sysFds [PIN_NUM_CALC_SYSFD(GpioPin)], &c, 1) < 0) {
|
||||
fprintf(stderr, "Unable to read from the sysfs GPIO node: %s \n", strerror(errno));
|
||||
}
|
||||
|
||||
libwiring.isrFunctions [PIN_NUM_CALC_SYSFD(GpioPin)] = function ;
|
||||
|
||||
|
||||
@@ -123,16 +123,16 @@ struct libodroid
|
||||
|
||||
/* wiringPi core func */
|
||||
int (*getModeToGpio) (int mode, int pin);
|
||||
void (*setPadDrive) (int pin, int value);
|
||||
int (*setPadDrive) (int pin, int value);
|
||||
int (*getPadDrive) (int pin);
|
||||
void (*pinMode) (int pin, int mode);
|
||||
int (*pinMode) (int pin, int mode);
|
||||
int (*getAlt) (int pin);
|
||||
int (*getPUPD) (int pin);
|
||||
void (*pullUpDnControl) (int pin, int pud);
|
||||
int (*pullUpDnControl) (int pin, int pud);
|
||||
int (*digitalRead) (int pin);
|
||||
void (*digitalWrite) (int pin, int value);
|
||||
int (*digitalWrite) (int pin, int value);
|
||||
int (*analogRead) (int pin);
|
||||
void (*digitalWriteByte) (const int value);
|
||||
int (*digitalWriteByte) (const unsigned int value);
|
||||
unsigned int (*digitalReadByte) (void);
|
||||
|
||||
/* ISR Function pointer */
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -155,7 +156,8 @@ void serialClose (const int fd)
|
||||
|
||||
void serialPutchar (const int fd, const unsigned char c)
|
||||
{
|
||||
write (fd, &c, 1) ;
|
||||
if (write(fd, &c, 1) < 0)
|
||||
fprintf(stderr, "Unable to send to the opened serial device: %s \n", strerror(errno));
|
||||
}
|
||||
|
||||
|
||||
@@ -167,7 +169,8 @@ void serialPutchar (const int fd, const unsigned char c)
|
||||
|
||||
void serialPuts (const int fd, const char *s)
|
||||
{
|
||||
write (fd, s, strlen (s)) ;
|
||||
if (write(fd, s, strlen(s)) < 0)
|
||||
fprintf(stderr, "Unable to send to the opened serial device: %s \n", strerror(errno));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user