From 081073390a0ecfcc86dd7d77d52aca364b0f0aab Mon Sep 17 00:00:00 2001 From: leeboby Date: Fri, 12 Mar 2021 09:46:41 +0800 Subject: [PATCH] Added command to set GPIO multiplexing function for allwinner soc "gpio mode 1 alt7" command sets gpio to io_disable --- gpio/gpio.c | 12 ++++++------ wiringPi/OrangePi.c | 28 ++++++++++++++++++++++++++++ wiringPi/OrangePi.h | 1 + wiringPi/wiringPi.c | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index f852562..26b1c9b 100755 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -889,12 +889,12 @@ void doMode (int argc, char *argv []) else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; - else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ; - else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ; - else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ; - else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ; - else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ; - else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ; + else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b010) ; + else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b011) ; + else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b100) ; + else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b101) ; + else if (strcasecmp (mode, "alt6") == 0) pinModeAlt (pin, 0b110) ; + else if (strcasecmp (mode, "alt7") == 0) pinModeAlt (pin, 0b111) ; else { fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; diff --git a/wiringPi/OrangePi.c b/wiringPi/OrangePi.c index 876b74a..b8c3dfc 100755 --- a/wiringPi/OrangePi.c +++ b/wiringPi/OrangePi.c @@ -1996,6 +1996,34 @@ int OrangePi_set_gpio_mode(int pin, int mode) return 0; } +int OrangePi_set_gpio_alt(int pin, int mode) +{ + unsigned int regval = 0; + unsigned int bank = pin >> 5; + unsigned int index = pin - (bank << 5); + unsigned int phyaddr = 0; + int offset = ((index - ((index >> 3) << 3)) << 2); + + if (bank == 11) { + phyaddr = GPIOL_BASE + ((index >> 3) << 2); + }else + phyaddr = GPIO_BASE_MAP + (bank * 36) + ((index >> 3) << 2); + + /* Ignore unused gpio */ + if (ORANGEPI_PIN_MASK[bank][index] != -1) { + if (wiringPiDebug) + printf("Register[%#x]: %#x index:%d\n", phyaddr, regval, index); + + regval = readR(phyaddr); + regval &= ~(7 << offset); + regval |= (mode << offset); + writeR(regval, phyaddr); + } else + printf("Pin alt mode failed!\n"); + + return 0; +} + /* * OrangePi Digital write */ diff --git a/wiringPi/OrangePi.h b/wiringPi/OrangePi.h index f6dc204..31318a2 100755 --- a/wiringPi/OrangePi.h +++ b/wiringPi/OrangePi.h @@ -233,6 +233,7 @@ extern int pwmmode; extern unsigned int readR(unsigned int addr); extern void writeR(unsigned int val, unsigned int addr); extern int OrangePi_set_gpio_mode(int pin, int mode); +extern int OrangePi_set_gpio_alt(int pin, int mode); extern int OrangePi_get_gpio_mode(int pin); extern int isOrangePi_2G_IOT(void); extern int isOrangePi(void); diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ab54ae0..88ad1bc 100755 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -1338,6 +1338,40 @@ void pinModeAlt (int pin, int mode) setupCheck ("pinModeAlt") ; + +#ifdef CONFIG_ORANGEPI + if(version == ORANGEPI) { + if (wiringPiDebug) + printf("PinModeAlt: pin:%d,mode:%d\n", pin, mode); + if ((pin & PI_GPIO_MASK) == 0) { + 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 (-1 == pin) { + printf("[%s:L%d] the pin:%d is invaild,please check it over!\n", + __func__, __LINE__, pin); + return; + } + + if ( mode <= 1 || mode >= 8){ + printf("[%s:L%d] the mode:%d is invaild,please check it over!\n", + __func__, __LINE__, mode); + return; + } + + OrangePi_set_gpio_alt(pin, mode); + + return; + } else { + return ; + } + } +#endif + if ((pin & PI_GPIO_MASK) == 0) // On-board pin { /**/ if (wiringPiMode == WPI_MODE_PINS)