Version v3.16: added wfi status for wiringPiISR and waitForInterrupt
This commit is contained in:
@@ -291,7 +291,7 @@ Registriert eine Interrupt Service Routine (ISR) bzw. Funktion die bei Flankenwe
|
||||
|
||||
>>>
|
||||
```C
|
||||
int wiringPiISR(int pin, int edgeMode, void (*function)(unsigned int, long long int), unsigned long bouncetime);
|
||||
int wiringPiISR(int pin, int edgeMode, void (*function)(struct WPIWfiStatus wfiStatus), unsigned long bouncetime);
|
||||
```
|
||||
|
||||
``pin``: Der gewünschte Pin (BCM\-, WiringPi\- oder Pin\-Nummer).
|
||||
@@ -301,9 +301,15 @@ int wiringPiISR(int pin, int edgeMode, void (*function)(unsigned int, long long
|
||||
- INT_EDGE_FALLING ... Fallende Flanke
|
||||
- INT_EDGE_BOTH ... Steigende und fallende Flanke
|
||||
|
||||
``*function``: Funktionspointer für ISR mit Rückgabeparameter pin und Zeitstempel:
|
||||
> unsigned int: pin
|
||||
> long long int: Zeitstempel
|
||||
``*function``: Funktionspointer für ISR mit Rückgabeparameter struct WPIWfiStatus:
|
||||
```C
|
||||
struct WPIWfiStatus {
|
||||
int status; // -1: error, 0: timeout, 1: valid values for edge and timeStamp_us
|
||||
unsigned int gpioPin; // gpio as BCM pin
|
||||
int edge; // One of INT_EDGE_FALLING or INT_EDGE_RISING
|
||||
long long int timeStamp_us; // time stamp in microseconds, when interrupt happened
|
||||
};
|
||||
```
|
||||
|
||||
``bouncetime``: Entprellzeit in Microsekunden, 0 ms schaltet das Entprellen ab
|
||||
|
||||
@@ -335,7 +341,7 @@ Wartet auf einen Aufruf der Interrupt Service Routine (ISR) mit Timeout und Entp
|
||||
Blockiert das Programm bis zum Eintreffen der auslösenden Flanke oder bis zum Ablauf des Timeouts.
|
||||
>>>
|
||||
```C
|
||||
long long int waitForInterrupt (int pin, int edgeMode, int mS, unsigned long bouncetime)
|
||||
struct WPIWfiStatus wfiStatus waitForInterrupt (int pin, int edgeMode, int mS, unsigned long bouncetime)
|
||||
```
|
||||
|
||||
``pin``: Der gewünschte Pin (BCM\-, WiringPi\- oder Pin\-Nummer).
|
||||
@@ -353,18 +359,21 @@ long long int waitForInterrupt (int pin, int edgeMode, int mS, unsigned long bo
|
||||
``bouncetime``: Entprellzeit in Microsekunden, 0 schaltet Entprellen ab.
|
||||
|
||||
``Rückgabewert``:
|
||||
> \>0 ... Zeitstempel des Interrupt Ereignisses in Mikrosekunden
|
||||
> 0 ... Timeout
|
||||
> \-1 ... GPIO Device Chip nicht erfolgreich geöffnet
|
||||
> \-2 ... ISR wurde nicht registriert
|
||||
|
||||
```C
|
||||
struct WPIWfiStatus {
|
||||
int status; // -1: error, 0: timeout, 1: valid values for edge and timeStamp_us
|
||||
unsigned int gpioPin; // gpio as BCM pin
|
||||
int edge; // One of INT_EDGE_FALLING or INT_EDGE_RISING
|
||||
long long int timeStamp_us; // time stamp in microseconds, when interrupt happened
|
||||
};
|
||||
```
|
||||
|
||||
**Beispiel:**
|
||||
|
||||
```C
|
||||
/*
|
||||
* isr_debounce.c:
|
||||
* Wait for Interrupt test program WiringPi >=3.14 - ISR method
|
||||
* Wait for Interrupt test program WiringPi >=3.16 - ISR method
|
||||
*
|
||||
*
|
||||
*/
|
||||
@@ -396,20 +405,26 @@ int toggle = 0;
|
||||
*********************************************************************************
|
||||
*/
|
||||
|
||||
static void wfi (unsigned int gpio, long long int timestamp) {
|
||||
static void wfi (struct WPIWfiStatus wfiStatus) {
|
||||
// struct timeval now;
|
||||
long long int timenow, diff;
|
||||
struct timespec curr;
|
||||
|
||||
char *edgeType;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1) {
|
||||
printf("clock_gettime error");
|
||||
return;
|
||||
}
|
||||
|
||||
timenow = curr.tv_sec * 1000000LL + curr.tv_nsec/1000L; // convert to microseconds
|
||||
diff = timenow - timestamp;
|
||||
|
||||
printf("gpio BCM = %d, IRQ timestamp = %lld microseconds, timenow = %lld, diff = %lld\n", gpio, timestamp, timenow, diff);
|
||||
diff = timenow - wfiStatus.timeStamp_us;
|
||||
if (wfiStatus.edge == INT_EDGE_RISING)
|
||||
edgeType = "rising";
|
||||
else if (wfiStatus.edge == INT_EDGE_FALLING)
|
||||
edgeType = "falling";
|
||||
else
|
||||
edgeType = "none";
|
||||
printf("gpio BCM = %d, IRQ edge = %s, timestamp = %lld microseconds, timenow = %lld, diff = %lld\n", wfiStatus.gpioPin, edgeType, wfiStatus.timeStamp_us, timenow, diff);
|
||||
if (toggle == 0) {
|
||||
digitalWrite (OUTpin, HIGH) ;
|
||||
toggle = 1;
|
||||
@@ -425,7 +440,6 @@ static void wfi (unsigned int gpio, long long int timestamp) {
|
||||
int main (void)
|
||||
{
|
||||
int major, minor;
|
||||
long long int ret;
|
||||
|
||||
wiringPiVersion(&major, &minor);
|
||||
|
||||
@@ -441,24 +455,27 @@ int main (void)
|
||||
pinMode(OUTpin, OUTPUT);
|
||||
digitalWrite (OUTpin, LOW) ;
|
||||
|
||||
printf("Testing waitForInterrupt IRQ @ GPIO%d, timeout is %d\n", IRQpin, TIMEOUT);
|
||||
ret = waitForInterrupt(IRQpin, INT_EDGE_FALLING, TIMEOUT, BOUNCETIME_WFI );
|
||||
if (ret < 0) {
|
||||
printf("waitForInterrupt returned error %lld\n", ret);
|
||||
printf("Testing waitForInterrupt on both edges IRQ @ GPIO%d, timeout is %d\n", IRQpin, TIMEOUT);
|
||||
struct WPIWfiStatus wfiStatus = waitForInterrupt(IRQpin, INT_EDGE_BOTH, TIMEOUT, BOUNCETIME_WFI );
|
||||
if (wfiStatus.status < 0) {
|
||||
printf("waitForInterrupt returned error\n");
|
||||
pinMode(OUTpin, INPUT);
|
||||
return 0;
|
||||
}
|
||||
else if (ret == 0) {
|
||||
printf("waitForInterrupt timed out %lld\n\n", ret);
|
||||
else if (wfiStatus.status == 0) {
|
||||
printf("waitForInterrupt timed out\n\n");
|
||||
}
|
||||
else {
|
||||
printf("waitForInterrupt: falling edge fired at %lld microseconds\n\n", ret);
|
||||
if (wfiStatus.edge == INT_EDGE_FALLING)
|
||||
printf("waitForInterrupt: GPIO pin %d falling edge fired at %lld microseconds\n\n", wfiStatus.gpioPin, wfiStatus.timeStamp_us);
|
||||
else
|
||||
printf("waitForInterrupt: GPIO pin %d rising edge fired at %lld microseconds\n\n", wfiStatus.gpioPin, wfiStatus.timeStamp_us);
|
||||
}
|
||||
|
||||
printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d falling edge and bouncetime %d microseconds. Toggle LED @ OUTpin on IRQ.\n\n", IRQpin, IRQpin, BOUNCETIME, OUTpin);
|
||||
printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d on both edges and bouncetime %d microseconds. Toggle LED @ OUTpin on IRQ.\n\n", IRQpin, IRQpin, BOUNCETIME, OUTpin);
|
||||
printf("To stop program hit return key\n\n");
|
||||
|
||||
wiringPiISR (IRQpin, INT_EDGE_FALLING, &wfi, BOUNCETIME) ;
|
||||
wiringPiISR (IRQpin, INT_EDGE_BOTH, &wfi, BOUNCETIME) ;
|
||||
|
||||
getc(stdin);
|
||||
|
||||
@@ -469,6 +486,37 @@ int main (void)
|
||||
}
|
||||
```
|
||||
|
||||
Output auf dem Terminal:
|
||||
|
||||
```
|
||||
pi@RaspberryPi:~/wiringpi-test-v3.16 $ gcc -o isr_debounce isr_debounce.c -l wiringPi
|
||||
pi@RaspberryPi:~/wiringpi-test-v3.16 $ ./isr_debounce
|
||||
|
||||
ISR debounce test (WiringPi 3.16)
|
||||
|
||||
Testing waitForInterrupt on both edges IRQ @ GPIO16, timeout is 10000
|
||||
waitForInterrupt: GPIO pin 16 falling edge fired at 256522528012 microseconds
|
||||
|
||||
Testing IRQ @ GPIO16 with trigger @ GPIO16 on both edges and bouncetime 3000 microseconds. Toggle LED @ OUTpin on IRQ.
|
||||
|
||||
To stop program hit return key
|
||||
|
||||
gpio BCM = 16, IRQ edge = rising, timestamp = 256522668010 microseconds, timenow = 256522668017, diff = 7
|
||||
gpio BCM = 16, IRQ edge = falling, timestamp = 256536364014 microseconds, timenow = 256536364021, diff = 7
|
||||
gpio BCM = 16, IRQ edge = rising, timestamp = 256536952011 microseconds, timenow = 256536952018, diff = 7
|
||||
gpio BCM = 16, IRQ edge = falling, timestamp = 256537856010 microseconds, timenow = 256537856016, diff = 6
|
||||
gpio BCM = 16, IRQ edge = rising, timestamp = 256538744011 microseconds, timenow = 256538744018, diff = 7
|
||||
gpio BCM = 16, IRQ edge = falling, timestamp = 256539664010 microseconds, timenow = 256539664017, diff = 7
|
||||
gpio BCM = 16, IRQ edge = rising, timestamp = 256540516010 microseconds, timenow = 256540516016, diff = 6
|
||||
gpio BCM = 16, IRQ edge = falling, timestamp = 256541560013 microseconds, timenow = 256541560022, diff = 9
|
||||
gpio BCM = 16, IRQ edge = rising, timestamp = 256542360010 microseconds, timenow = 256542360016, diff = 6
|
||||
gpio BCM = 16, IRQ edge = falling, timestamp = 256543320012 microseconds, timenow = 256543320020, diff = 8
|
||||
gpio BCM = 16, IRQ edge = rising, timestamp = 256544092021 microseconds, timenow = 256544092029, diff = 8
|
||||
^C
|
||||
pi@RaspberryPi:~/wiringpi-test-v3.16 $
|
||||
```
|
||||
|
||||
|
||||
## Hardware PWM (Pulsweitenmodulation)
|
||||
|
||||
Verfügbare GPIOs: https://pinout.xyz/pinout/pwm
|
||||
|
||||
Reference in New Issue
Block a user