From 107b9ee4c28674f531045832d203684d8b543d0a Mon Sep 17 00:00:00 2001 From: mstroh76 Date: Fri, 16 Feb 2024 21:16:20 +0100 Subject: [PATCH] #17 PinDrive --- gpio/gpio.c | 30 ++++++++++++++++++++++++++++-- wiringPi/wiringPi.c | 24 ++++++++++++++++++++---- wiringPi/wiringPi.h | 1 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index d029877..7c448a5 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -794,6 +794,31 @@ void doMode (int argc, char *argv []) ********************************************************************************* */ +static void doPadDrivePin (int argc, char *argv []) +{ + + if (argc != 4) { + fprintf (stderr, "Usage: %s drivepin pin value\n", argv [0]) ; + exit (1) ; + } + + int pin = atoi (argv [2]) ; + int val = atoi (argv [3]) ; + + if ((pin < 0) || (pin > 27)) { + fprintf (stderr, "%s: drive pin not 0-27: %d\n", argv [0], pin) ; + exit (1) ; + } + + if ((val < 0) || (val > 3)) { + fprintf (stderr, "%s: drive value not 0-3: %d\n", argv [0], val) ; + exit (1) ; + } + + setPadDrivePin (pin, val) ; +} + + static void doPadDrive (int argc, char *argv []) { int group, val ; @@ -807,13 +832,13 @@ static void doPadDrive (int argc, char *argv []) group = atoi (argv [2]) ; val = atoi (argv [3]) ; - if ((group < -1) || (group > 2)) //-1 hidden feature for read value of all + if ((group < -1) || (group > 2)) //-1 hidden feature for read and print values { fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ; exit (1) ; } - if ((val < -1) || (val > 7)) + if ((val < 0) || (val > 7)) { fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ; exit (1) ; @@ -1536,6 +1561,7 @@ int main (int argc, char *argv []) else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ; else if (strcasecmp (argv [1], "pwmTone" ) == 0) doPwmTone (argc, argv) ; else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ; + else if (strcasecmp (argv [1], "drivepin" ) == 0) doPadDrivePin(argc, argv) ; else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ; else if (strcasecmp (argv [1], "pins" ) == 0) doReadall () ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index a30d453..baf5d86 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -151,7 +151,10 @@ const unsigned int RP1_STATUS_LEVEL_MASK = 0x00C00000; 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_PAD_DEFAULT = 0x5a; + +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_DRIVE_MASK = 0x00000030; const unsigned int RP1_INV_PAD_DRIVE_MASK = ~(RP1_PAD_DRIVE_MASK); @@ -1248,6 +1251,19 @@ int physPinToGpio (int physPin) * Set the PAD driver value ********************************************************************************* */ +void setPadDrivePin (int pin, int value) { + if (PI_MODEL_5 != RaspberryPiModel) return; + if (pin < 0 || pin > GetMaxPin()) return ; + + uint32_t wrVal; + value = value & 3; // 0-3 supported + wrVal = (value << 4); //Drive strength 0-3 + pads[1+pin] = (pads[1+pin] & RP1_INV_PAD_DRIVE_MASK) | wrVal; + if (wiringPiDebug) { + printf ("setPadDrivePin: pin: %d, value: %d (%08X)\n", pin, value, pads[1+pin]) ; + } +} + void setPadDrive (int group, int value) { @@ -1682,8 +1698,8 @@ void pinMode (int pin, int mode) shift = gpioToShift [pin] ; if (mode == INPUT) { - if (PI_MODEL_5 == RaspberryPiModel) { - pads[1+pin] = RP1_PAD_DEFAULT; + 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<