diff --git a/wiringPi/test/Makefile b/wiringPi/test/Makefile index 0b68f96..40cbd25 100644 --- a/wiringPi/test/Makefile +++ b/wiringPi/test/Makefile @@ -3,7 +3,7 @@ CFLAGS = -Wall LDFLAGS = # Need BCM19 <-> BCM26, +PWM: BCM12 <-> BCM13, BCM18 <-> BCM17 connected (1kOhm) -tests = wiringpi_test0_version wiringpi_test1_sysfs wiringpi_test2_sysfs wiringpi_test3_device_wpi wiringpi_test4_device_phys wiringpi_test5_default wiringpi_test6_isr wiringpi_test61_isr2 wiringpi_test7_bench wiringpi_test8_pwm wiringpi_test9_pwm +tests = wiringpi_test0_version wiringpi_test1_sysfs wiringpi_test2_sysfs wiringpi_test3_device_wpi wiringpi_test4_device_phys wiringpi_test5_default wiringpi_test6_isr wiringpi_test61_isr2 wiringpi_test62_isr_wpin wiringpi_test7_bench wiringpi_test8_pwm wiringpi_test9_pwm # Need XO hardware xotests = wiringpi_xotest_test1_spi wiringpi_i2c_test1_pcf8574 wiringpi_test8_pwm wiringpi_test9_pwm @@ -37,6 +37,9 @@ wiringpi_test6_isr: wiringpi_test61_isr2: ${CC} ${CFLAGS} wiringpi_test61_isr2.c -o wiringpi_test61_isr2 -lwiringPi +wiringpi_test62_isr_wpin: + ${CC} ${CFLAGS} wiringpi_test62_isr_wpin.c -o wiringpi_test62_isr_wpin -lwiringPi + wiringpi_test7_bench: ${CC} ${CFLAGS} wiringpi_test7_bench.c -o wiringpi_test7_bench -lwiringPi diff --git a/wiringPi/test/wiringpi_test62_isr_wpin.c b/wiringPi/test/wiringpi_test62_isr_wpin.c new file mode 100644 index 0000000..7447c35 --- /dev/null +++ b/wiringPi/test/wiringpi_test62_isr_wpin.c @@ -0,0 +1,114 @@ +// WiringPi test program: Kernel char device interface / sysfs successor +// Compile: gcc -Wall wiringpi_test62_isr_wpin.c -o wiringpi_test62_isr_wpin -lwiringPi + +#include "wpi_test.h" +#include +#include +#include +#include + +//WPI pin numbers +int GPIO = 28; +int GPIOIN = 29; + +static volatile int globalCounter; + + +static void wfiup(void) { + + globalCounter++; + printf("I[%d] ", globalCounter); + fflush(stdout); +} + +static void wfidown(void) { + + globalCounter--; + printf("I[%d] ", globalCounter); + fflush(stdout); +} + + +void StartSequence (int Enge, int OUTpin, int count,int expected) { + /* + int expected = up ? count : 0; + globalCounter = up ? 0 : count; + */ + globalCounter = 0; + + printf("Start: "); + fflush(stdout); + for (int loop=0; loop 28\n"); + + printf("\nWiringPi %d.%d\n", major, minor); + + //printf("Error check - next call create fatal error with exit!\n"); + //CheckSame("wiringPiISRStop with wrong pin, result code:", wiringPiISRStop(5), 0); + + CheckSame("wiringPiSetupPinType(WPI_PIN_WPI)", wiringPiSetupPinType(WPI_PIN_WPI), 0); + + int rev, mem, maker, overVolted, RaspberryPiModel; + piBoardId(&RaspberryPiModel, &rev, &mem, &maker, &overVolted); + + CheckNotSame("piBoardId", RaspberryPiModel, 0); + + int IRQpin = GPIOIN; + int OUTpin = GPIO; + + if (RaspberryPiModel==PI_MODEL_4B) { + pinMode(IRQpin, INPUT); + pinMode(OUTpin, OUTPUT); + digitalWrite (OUTpin, LOW) ; + + printf("\nTesting IRQ @ WPI-GPIO%d with trigger @ WPI-GPIO%d rising\n", IRQpin, OUTpin); + CheckSame("wiringPiISR", wiringPiISR(IRQpin, INT_EDGE_RISING, &wfiup), 0); + sleep(1); + StartSequence(INT_EDGE_RISING, OUTpin, 3 , 3); + sleep(1); + CheckSame("wiringPiISRStop", wiringPiISRStop(IRQpin), 0); + printf("\n.IRQ off\n"); + sleep(1); + StartSequence(INT_EDGE_RISING, OUTpin, 2, 0); + + printf("\nTesting IRQ @ WPI-GPIO%d with trigger @ WPI-GPIO%d falling\n", IRQpin, OUTpin); + CheckSame("wiringPiISR", wiringPiISR(IRQpin, INT_EDGE_RISING, &wfidown), 0); + sleep(1); + StartSequence(INT_EDGE_FALLING, OUTpin, 4, -4); + sleep(1); + CheckSame("wiringPiISRStop", wiringPiISRStop(IRQpin), 0); + printf("\n.IRQ off\n"); + sleep(1); + StartSequence(INT_EDGE_RISING, OUTpin, 2, 0); + + printf("Error check - next call must be wrong!\n"); + CheckSame("wiringPiISRStop with wrong pin, result code:", wiringPiISRStop(5555), EINVAL); + + pinMode(OUTpin, INPUT); + } + + return UnitTestState(); +} diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index b5bfbed..f2e8252 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -646,6 +646,7 @@ int piBoard40Pin() { } } + int piRP1Model() { switch(RaspberryPiModel){ case PI_MODEL_5: @@ -658,8 +659,43 @@ int piRP1Model() { } } + int GetMaxPin() { - return piRP1Model() ? 27 : 63; + if (piRP1Model()) { + switch(wiringPiMode) { + case WPI_MODE_PHYS: + return 40; + case WPI_MODE_PINS: + return 31; + default: + return 27; + } + } else { + return 63; + } +} + + +int ToBCMPin(int* pin) { + if (*pin<0 || *pin>63) { + return FALSE; + } + switch(wiringPiMode) { + case WPI_MODE_PINS: + *pin = pinToGpio[*pin]; + break; + case WPI_MODE_PHYS: + *pin = physToGpio[*pin]; + break; + case WPI_MODE_GPIO: + return TRUE; + default: + return FALSE; + } + if (piRP1Model() && *pin>27) { + return FALSE; + } + return TRUE; } @@ -1286,8 +1322,9 @@ int physPinToGpio (int physPin) ********************************************************************************* */ void setPadDrivePin (int pin, int value) { - if (!piRP1Model()) return; - if (pin < 0 || pin > GetMaxPin()) return ; + if (!piRP1Model() || !ToBCMPin(&pin)) { + return; + } uint32_t wrVal; value = value & 3; // 0-3 supported @@ -1368,14 +1405,9 @@ int getAlt (int pin) { int alt; - pin &= 63 ; - - /**/ if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; - else if (wiringPiMode == WPI_MODE_PHYS) - pin = physToGpio [pin] ; - else if (wiringPiMode != WPI_MODE_GPIO) - return 0 ; + if (!ToBCMPin(&pin)) { + return 0; + } if (piRP1Model()) { alt = (gpio[2*pin+1] & RP1_FSEL_NONE_HW); //0-4 function @@ -1586,14 +1618,9 @@ void gpioClockSet (int pin, int freq) int divi, divr, divf ; FailOnModel5("gpioClockSet"); - pin &= 63 ; - - /**/ if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; - else if (wiringPiMode == WPI_MODE_PHYS) - pin = physToGpio [pin] ; - else if (wiringPiMode != WPI_MODE_GPIO) - return ; + if (!ToBCMPin(&pin)) { + return; + } divi = 19200000 / freq ; divr = 19200000 % freq ; @@ -1848,65 +1875,58 @@ void pinModeAlt (int pin, int mode) { setupCheck ("pinModeAlt") ; - if ((pin & PI_GPIO_MASK) == 0) // On-board pin - { - /**/ if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; - else if (wiringPiMode == WPI_MODE_PHYS) - pin = physToGpio [pin] ; - else if (wiringPiMode != WPI_MODE_GPIO) - return ; - - if (piRP1Model()) { - //confusion! diffrent to to BCM! this is taking directly the value for the register - int modeRP1; - switch(mode) { - case FSEL_ALT0: - modeRP1 = 0; - break; - case FSEL_ALT1: - modeRP1 = 1; - break; - case FSEL_ALT2: - modeRP1 = 2; - break; - case FSEL_ALT3: - modeRP1 = 3; - break; - case FSEL_ALT4: - modeRP1 = 4; - break; - case FSEL_ALT5: - modeRP1 = 5; - break; - case FSEL_ALT6: - modeRP1 = 6; - break; - case FSEL_ALT7: - modeRP1 = 7; - break; - case FSEL_ALT8: - modeRP1 = 8; - break; - case FSEL_OUTP: - case FSEL_INPT: - modeRP1 = RP1_FSEL_GPIO; - break; - default: - fprintf(stderr, "pinModeAlt: invalid mode %d\n", mode); - return; - } - //printf("pinModeAlt: Pi5 alt pin %d to %d\n", pin, modeRP1); - gpio[2*pin+1] = (modeRP1 & RP1_FSEL_NONE_HW) | RP1_DEBOUNCE_DEFAULT; //0-4 function, 5-11 debounce time - } else { - int fSel = gpioToGPFSEL [pin] ; - int shift = gpioToShift [pin] ; - - *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ; - } - + if (!ToBCMPin(&pin)) { + return; } -} + + if (piRP1Model()) { + //confusion! diffrent to to BCM! this is taking directly the value for the register + int modeRP1; + switch(mode) { + case FSEL_ALT0: + modeRP1 = 0; + break; + case FSEL_ALT1: + modeRP1 = 1; + break; + case FSEL_ALT2: + modeRP1 = 2; + break; + case FSEL_ALT3: + modeRP1 = 3; + break; + case FSEL_ALT4: + modeRP1 = 4; + break; + case FSEL_ALT5: + modeRP1 = 5; + break; + case FSEL_ALT6: + modeRP1 = 6; + break; + case FSEL_ALT7: + modeRP1 = 7; + break; + case FSEL_ALT8: + modeRP1 = 8; + break; + case FSEL_OUTP: + case FSEL_INPT: + modeRP1 = RP1_FSEL_GPIO; + break; + default: + fprintf(stderr, "pinModeAlt: invalid mode %d\n", mode); + return; + } + //printf("pinModeAlt: Pi5 alt pin %d to %d\n", pin, modeRP1); + gpio[2*pin+1] = (modeRP1 & RP1_FSEL_NONE_HW) | RP1_DEBOUNCE_DEFAULT; //0-4 function, 5-11 debounce time + } else { + int fSel = gpioToGPFSEL [pin] ; + int shift = gpioToShift [pin] ; + + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ; + } + } /* @@ -2447,12 +2467,9 @@ void pwmWrite (int pin, int value) if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin { - /**/ if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; - else if (wiringPiMode == WPI_MODE_PHYS) - pin = physToGpio [pin] ; - else if (wiringPiMode != WPI_MODE_GPIO) - return ; + if (!ToBCMPin(&pin)) { + return; + } /* would be possible on ms mode but not on bal, deactivated, use pwmc modify instead if (piGpioBase == GPIO_PERI_BASE_2711) { @@ -2677,15 +2694,9 @@ struct WPIWfiStatus waitForInterrupt2(int pin, int edgeMode, int ms, unsigned lo const char* strmode = ""; struct WPIWfiStatus wfiStatus; - if (wiringPiMode == WPI_MODE_PINS) - pin = pinToGpio [pin] ; - else if (wiringPiMode == WPI_MODE_PHYS) - pin = physToGpio [pin] ; - memset(&wfiStatus, 0, sizeof(wfiStatus)); - /* open gpio */ - if (wiringPiGpioDeviceGetFd()<0) { + if (wiringPiGpioDeviceGetFd()<0 || !ToBCMPin(&pin)) { wfiStatus.statusOK = -1; return wfiStatus; } @@ -2844,12 +2855,24 @@ int waitForInterrupt (int pin, int ms) { ********************************************************************************* */ -int wiringPiISRStop (int pin) { - void *res; - +int wiringPiISRStop(int pin) { + + if (wiringPiMode == WPI_MODE_UNINITIALISED) { + return wiringPiFailure(WPI_FATAL, "wiringPiISRStop: wiringPi has not been initialised. Unable to continue.\n"); + } + if (!ToBCMPin(&pin)) { + fprintf(stderr, "wiringPiISRStop: wrong pin %d (mode: %d) number!\n", pin, wiringPiMode); + return EINVAL; + } + if (wiringPiDebug) { + printf("wiringPiISRStop: pin %d\n", pin) ; + } + if (isrFds[pin] > 0) { + void *res; + if (wiringPiDebug) - printf ("wiringPiISRStop: close thread 0x%lX\n", (unsigned long)isrThreads[pin]) ; + printf("wiringPiISRStop: close thread 0x%lX\n", (unsigned long)isrThreads[pin]); if (isrThreads[pin] != 0) { if (pthread_cancel(isrThreads[pin]) == 0) { @@ -2864,10 +2887,13 @@ int wiringPiISRStop (int pin) { } } else { if (wiringPiDebug) - printf ("wiringPiISRStop: could not cancel thread\n"); + printf("wiringPiISRStop: could not cancel thread\n"); } } close(isrFds [pin]); + } else { + if (wiringPiDebug) + printf("wiringPiISRStop: Warning stop isr, but its not active\n"); } isrFds [pin] = -1; isrFunctions[pin] = NULL; @@ -2882,7 +2908,7 @@ int wiringPiISRStop (int pin) { chipFd = -1; */ if (wiringPiDebug) { - printf ("wiringPiISRStop: wiringPiISRStop finished\n") ; + printf("wiringPiISRStop: wiringPiISRStop finished\n"); } return 0; } @@ -3077,24 +3103,18 @@ void *interruptHandlerV2(void *arg) int wiringPiISRInternal(int pin, int edgeMode, void (*function)(struct WPIWfiStatus wfiStatus, void* userdata), void (*functionClassic)(void), unsigned long debounce_period_us, void* userdata) { - const int maxpin = GetMaxPin(); - - if (pin < 0 || pin > maxpin) - return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-%d (%d)\n", maxpin, pin) ; - if (wiringPiMode == WPI_MODE_UNINITIALISED) - return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ; - - if (wiringPiMode == WPI_MODE_PINS) { - pin = pinToGpio [pin] ; - } else if (wiringPiMode == WPI_MODE_PHYS) { - pin = physToGpio [pin] ; + if (wiringPiMode == WPI_MODE_UNINITIALISED) { + return wiringPiFailure(WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n"); + } + if (!ToBCMPin(&pin)) { + fprintf(stderr, "wiringPiISRStop: wrong pin %d (mode: %d) number!\n", pin, wiringPiMode); + return EINVAL; } - if (wiringPiDebug) { - printf ("wiringPi: wiringPiISR pin %d, edgeMode %d\n", pin, edgeMode) ; + printf("wiringPi: wiringPiISR pin %d, edgeMode %d\n", pin, edgeMode); } - if (isrFunctions[pin] || isrFunctionsV2[pin] ) { - printf ("wiringPi: ISR function already active, ignoring \n") ; + if (isrFunctions[pin] || isrFunctionsV2[pin]) { + fprintf(stderr, "wiringPi: ISR function already active, ignoring \n"); } isrFunctionsV2[pin] = function; @@ -3104,7 +3124,7 @@ int wiringPiISRInternal(int pin, int edgeMode, void (*function)(struct WPIWfiSta isrDebouncePeriodUs[pin] = debounce_period_us; if (wiringPiDebug) { - printf ("wiringPi: mutex in\n") ; + printf("wiringPi: mutex in\n"); } pthread_mutex_lock (&pinMutex) ; pinPass = pin ; @@ -3129,12 +3149,12 @@ int wiringPiISRInternal(int pin, int edgeMode, void (*function)(struct WPIWfiSta } if (wiringPiDebug) { - printf ("wiringPi: mutex out\n") ; + printf("wiringPi: mutex out\n"); } pthread_mutex_unlock (&pinMutex) ; if (wiringPiDebug) { - printf ("wiringPi: wiringPiISR finished\n") ; + printf("wiringPi: wiringPiISR finished\n"); } return 0 ; } diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 9ae92d0..e9be5a2 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -281,8 +281,8 @@ extern int piBoardRev (void) ; // Deprecated, but does the sa extern void piBoardId (int *model, int *rev, int *mem, int *maker, int *overVolted) ; extern int piBoard40Pin (void) ; // Interface V3.7 extern int piRP1Model (void) ; // Interface V3.14 -extern int wpiPinToGpio (int wpiPin) ; -extern int physPinToGpio (int physPin) ; +extern int wpiPinToGpio (int wpiPin) ; // please don't use outside 0-63 and on RP1 +extern int physPinToGpio (int physPin) ; // please don't use outside 0-63 and on RP1 extern void setPadDrive (int group, int value) ; extern void setPadDrivePin (int pin, int value); // Interface V3.0 extern int getAlt (int pin) ;