Browse Source

Cleanup and bug fixes

pull/158/head
Jim Parziale 2 years ago
parent
commit
adb40119db
4 changed files with 35 additions and 28 deletions
  1. +26
    -16
      wiringPi/wiringPi.c
  2. +3
    -6
      wiringPi/wiringPi.h
  3. +2
    -2
      wiringPi/wiringPiI2C.c
  4. +4
    -4
      wiringPi/wiringPiSPI.c

+ 26
- 16
wiringPi/wiringPi.c View File

@@ -2069,19 +2069,28 @@ int wiringPiSetup (void)
int fd;
int model, proc, rev, mem, maker, overVolted;

// Exit with OK status if this has already been called.
if (wiringPiSetuped)
{
return 0;
}

wiringPiSetuped = TRUE;

if (getenv (ENV_DEBUG) != NULL)
{
wiringPiDebug = TRUE;
}

if (getenv (ENV_CODES) != NULL)
{
wiringPiReturnCodes = TRUE;
}

if (wiringPiDebug)
{
printf ("wiringPi: wiringPiSetup called\n");
}

// Get the board ID information. We're not really using the information here,
// but it will give us information like the GPIO layout scheme (2 variants
@@ -2090,14 +2099,15 @@ int wiringPiSetup (void)
// don't really many anything, so force native BCM mode anyway.
piBoardId (&model, &proc, &rev, &mem, &maker, &overVolted);

// Set default GPIO/pin mode.
if ((model == PI_MODEL_CM1) ||
(model == PI_MODEL_CM3) ||
(model == PI_MODEL_CM3P))
wiringPiMode = WPI_MODE_GPIO;
wiringPiMode = WPI_MODE_GPIO; // Virtual pin numbers 0 through 16
else
wiringPiMode = WPI_MODE_PINS;
wiringPiMode = WPI_MODE_PINS; // Broadcom GPIO pin numbers

if (piGpioLayout() == 1) // A, B, Rev 1, 1.1
if (piGpioLayout() == 1) // A, B, Rev 1, 1.1 (Oldest boards)
{
pinToGpio = pinToGpioR1;
physToGpio = physToGpioR1;
@@ -2145,46 +2155,46 @@ int wiringPiSetup (void)
usingGpioMem = TRUE;
}
else
return wiringPiFailure (WPI_ALMOST,
return wiringPiFailure (WPI_NON_FATAL,
"wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n"
" Aborting your program because if it can not access the GPIO\n"
" hardware then it most certianly won't work\n"
" Try running with sudo?\n", strerror (errno));
" Aborting your program because it must be able to access the GPIO hardware.\n"
" NOTE: You may need to run with sudo.\n",
strerror (errno));
}

// Set the offsets into the memory interface.
GPIO_PADS = piGpioBase + 0x00100000;
GPIO_PADS = piGpioBase + 0x00100000;
GPIO_CLOCK_BASE = piGpioBase + 0x00101000;
GPIO_BASE = piGpioBase + 0x00200000;
GPIO_TIMER = piGpioBase + 0x0000B000;
GPIO_PWM = piGpioBase + 0x0020C000;
GPIO_BASE = piGpioBase + 0x00200000;
GPIO_TIMER = piGpioBase + 0x0000B000;
GPIO_PWM = piGpioBase + 0x0020C000;

// Map the individual hardware components

// GPIO:
gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE);
if (gpio == MAP_FAILED)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno));
return wiringPiFailure (WPI_NON_FATAL, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno));

// PWM
pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM);
if (pwm == MAP_FAILED)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno));
return wiringPiFailure (WPI_NON_FATAL, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno));

// Clock control (needed for PWM)
clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE);
if (clk == MAP_FAILED)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno));
return wiringPiFailure (WPI_NON_FATAL, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno));

// The drive pads
pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS);
if (pads == MAP_FAILED)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno));
return wiringPiFailure (WPI_NON_FATAL, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno));

// The system timer
timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER);
if (timer == MAP_FAILED)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno));
return wiringPiFailure (WPI_NON_FATAL, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno));

// Set the timer to free-running, 1MHz.
// 0xF9 is 249, the timer divide is base clock / (divide+1)


+ 3
- 6
wiringPi/wiringPi.h View File

@@ -31,7 +31,6 @@
#endif

// GCC warning suppressor - don't warn about unused parameters (-Wunused-parameter)
// @TODO Remove this and fix instances where parameters are unused.
#define UNU __attribute__((unused))

// Mask for the bottom 64 pins which belong to the Raspberry Pi
@@ -44,7 +43,7 @@
#define WPI_MODE_PINS 0 // Virtual pin numbers 0 through 16
#define WPI_MODE_GPIO 1 // Broadcom GPIO pin numbers
#define WPI_MODE_GPIO_SYS 2 // Broadcom GPIO pin numbers, but uses /sys/class/gpio (slower)
#define WPI_MODE_PHYS 3 // Raspberry Pi pysical pins
#define WPI_MODE_PHYS 3 // Raspberry Pi physical pins
#define WPI_MODE_PIFACE 4 // UNUSED
#define WPI_MODE_UNINITIALISED -1

@@ -153,8 +152,8 @@ extern const int piMemorySize [ 8];
#define PI_THREAD(X) void *X (UNU void *dummy)

// Failure modes
#define WPI_FATAL true
#define WPI_ALMOST false
#define WPI_FATAL true
#define WPI_NON_FATAL false


// wiringPiNodeStruct:
@@ -220,8 +219,6 @@ extern void pinMode (int pin, int mode);
extern void pullUpDnControl (int pin, int pud);
extern int digitalRead (int pin);
extern void digitalWrite (int pin, int value);
extern unsigned int digitalRead8 (int pin);
extern void digitalWrite8 (int pin, int value);
extern void pwmWrite (int pin, int value);
extern int analogRead (int pin);
extern void analogWrite (int pin, int value);


+ 2
- 2
wiringPi/wiringPiI2C.c View File

@@ -202,10 +202,10 @@ int wiringPiI2CSetupInterface (const char *device, int devId)
int fd ;

if ((fd = open (device, O_RDWR)) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_NON_FATAL, "Unable to open I2C device: %s\n", strerror (errno)) ;

if (ioctl (fd, I2C_SLAVE, devId) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_NON_FATAL, "Unable to select I2C device: %s\n", strerror (errno)) ;

return fd ;
}


+ 4
- 4
wiringPi/wiringPiSPI.c View File

@@ -112,7 +112,7 @@ int wiringPiSPISetupMode (int channel, int speed, int mode)
snprintf (spiDev, 31, "/dev/spidev0.%d", channel) ;

if ((fd = open (spiDev, O_RDWR)) < 0)
return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_NON_FATAL, "Unable to open SPI device: %s\n", strerror (errno)) ;

spiSpeeds [channel] = speed ;
spiFds [channel] = fd ;
@@ -120,13 +120,13 @@ int wiringPiSPISetupMode (int channel, int speed, int mode)
// Set SPI parameters.

if (ioctl (fd, SPI_IOC_WR_MODE, &mode) < 0)
return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_NON_FATAL, "SPI Mode Change failure: %s\n", strerror (errno)) ;
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_NON_FATAL, "SPI BPW Change failure: %s\n", strerror (errno)) ;

if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
return wiringPiFailure (WPI_NON_FATAL, "SPI Speed Change failure: %s\n", strerror (errno)) ;

return fd ;
}


Loading…
Cancel
Save