From 4db79da55b0bcb69df59b6c6486018bdbc3838b5 Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Thu, 30 May 2024 16:52:04 +0200 Subject: [PATCH] #244 add API call getPinModeAlt --- wiringPi/test/wiringpi_test5_default.c | 60 +++++++++++++++++++++++--- wiringPi/test/wpi_test.h | 20 +++++++++ wiringPi/wiringPi.c | 22 ++++++++-- wiringPi/wiringPi.h | 20 +++++++++ 4 files changed, 113 insertions(+), 9 deletions(-) diff --git a/wiringPi/test/wiringpi_test5_default.c b/wiringPi/test/wiringpi_test5_default.c index ac58be9..003e89d 100644 --- a/wiringPi/test/wiringpi_test5_default.c +++ b/wiringPi/test/wiringpi_test5_default.c @@ -10,6 +10,34 @@ const int GPIO = 19; const int GPIOIN = 26; const int ToggleValue = 4; +int RaspberryPiModel = -1; + + +void SetAndCheckMode(int pin, int mode) { + enum WPIPinALT AltGpio = WPI_ALT_UNKNOWN; + + switch(mode) { + case INPUT: + pinMode(pin, INPUT); + AltGpio = getPinModeAlt(pin); + CheckSame("Pin mode input", AltGpio, WPI_ALT_INPUT); + break; + case OUTPUT: + pinMode(pin, OUTPUT); + AltGpio = getPinModeAlt(pin); + CheckSame("Pin mode output", AltGpio, WPI_ALT_OUTPUT); + break; + case PM_OFF: + pinMode(pin, PM_OFF); + AltGpio = getPinModeAlt(pin); + CheckSame("Pin mode off(input)", AltGpio, (PI_MODEL_5 == RaspberryPiModel) ? WPI_NONE : WPI_ALT_INPUT); + break; + default: + pinMode(pin, mode); + printf("pinmode %d of pin %d not checked", mode, pin); + break; + } +} int main (void) { @@ -21,8 +49,23 @@ int main (void) { printf("wiringPiSetupGpio failed\n\n"); exit(EXIT_FAILURE); } - pinMode(GPIOIN, INPUT); - pinMode(GPIO, OUTPUT); + + int rev, mem, maker, overVolted; + piBoardId(&RaspberryPiModel, &rev, &mem, &maker, &overVolted); + CheckNotSame("Model: ", RaspberryPiModel, -1); + if (PI_MODEL_5 == RaspberryPiModel) { + printf("Raspberry Pi 5 with RP1 found\n"); + } else { + printf("Raspberry Pi with BCM GPIO found (not Pi 5)\n"); + } + + + enum WPIPinAlt AltGpio = WPI_ALT_UNKNOWN; + AltGpio = getPinModeAlt(23); + CheckSame("Pin mode default", AltGpio, PI_MODEL_5 == RaspberryPiModel ? WPI_NONE : WPI_ALT_INPUT); + + SetAndCheckMode(GPIOIN, INPUT); + SetAndCheckMode(GPIO, OUTPUT); printf("toggle %d times ...\n", ToggleValue); for (int loop=1; loop %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF); + } else { + printf("%39s (%10s<>%10s) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF); + globalError=1; + } +} + + void CheckSame(const char* msg, int value, int expect) { if (value==expect) { printf("%39s (% 3d==% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF); @@ -70,6 +80,16 @@ void CheckSame(const char* msg, int value, int expect) { } +void CheckNotSame(const char* msg, int value, int expect) { + if (value!=expect) { + printf("%39s (% 3d<>% 3d) -> %spassed%s\n", msg, value, expect, COLORGRN, COLORDEF); + } else { + printf("%39s (% 3d==% 3d) -> %sfailed%s\n", msg, value, expect, COLORRED, COLORDEF); + globalError=1; + } +} + + void CheckSameFloat(const char* msg, float value, float expect) { if (fabs(value-expect)<0.08) { printf("%35s (%.3f==%.3f) -> %spassed%s \n", msg, value, expect, COLORGRN, COLORDEF); diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ad81678..1cd1dba 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -156,8 +156,12 @@ const unsigned int RP1_DEBOUNCE_DEFAULT_VALUE = 4; const unsigned int RP1_DEBOUNCE_MASK = 0x7f; const unsigned int RP1_DEBOUNCE_DEFAULT = (RP1_DEBOUNCE_DEFAULT_VALUE << 5); +const unsigned int RP1_IRQRESET = 0x10000000; //CTRL Bit 28 + const unsigned int RP1_PAD_DEFAULT_0TO8 = (0x0B | 0x70); //Slewfast, Schmitt, PullUp, | 12mA, Input enable const unsigned int RP1_PAD_DEFAULT_FROM9 = (0x07 | 0x70); //Slewfast, Schmitt, PullDown, | 12mA, Input enable +const unsigned int RP1_PAD_IC_DEFAULT_0TO8 = 0x9A; //pull-up, Schmitt +const unsigned int RP1_PAD_IC_DEFAULT_FROM9 = 0x96; //pull-down, Schmitt const unsigned int RP1_PAD_DRIVE_MASK = 0x00000030; const unsigned int RP1_INV_PAD_DRIVE_MASK = ~(RP1_PAD_DRIVE_MASK); @@ -1314,6 +1318,11 @@ int getAlt (int pin) } +enum WPIPinALT getPinModeAlt(int pin) { + return (enum WPIPinALT) getAlt(pin); +} + + /* * pwmSetMode: * Select the native "balanced" mode, or standard mark:space mode @@ -1727,11 +1736,16 @@ void pinMode (int pin, int mode) fSel = gpioToGPFSEL [pin] ; shift = gpioToShift [pin] ; - if (mode == INPUT) { + if (INPUT==mode || PM_OFF==mode) { if (PI_MODEL_5 == RaspberryPiModel) { - pads[1+pin] = (pin<=8) ? RP1_PAD_DEFAULT_0TO8 : RP1_PAD_DEFAULT_FROM9; - gpio[2*pin+1] = RP1_FSEL_GPIO | RP1_DEBOUNCE_DEFAULT; // GPIO - rio[RP1_RIO_OE + RP1_CLR_OFFSET] = 1<