Skip to content

Commit

Permalink
Merge pull request orangepi-xunlong#56 from bulateek/OPi3_PUD
Browse files Browse the repository at this point in the history
  • Loading branch information
baiywt committed Jan 31, 2023
2 parents a920be8 + 82d597d commit bdfad0f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
69 changes: 69 additions & 0 deletions wiringPi/OrangePi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,75 @@ int OrangePi_set_gpio_mode(int pin, int mode)
return 0;
}

/*
* Set GPIO Pull Up/Down
*/
int OrangePi_set_gpio_pullUpDnControl(int pin, int pud)
{
unsigned int regval = 0;
unsigned int bank = pin >> 5;
unsigned int index = pin - (bank << 5);
unsigned int phyaddr = 0;
unsigned int pullOffset = 0x1C;

int pud_OP = -1;
switch (pud)
{
case PUD_OFF:
pud_OP = SUNXI_PUD_OFF;
break;
case PUD_UP:
pud_OP = SUNXI_PUD_UP;
break;
case PUD_DOWN:
pud_OP = SUNXI_PUD_DOWN;
break;
default:
printf("Unknow pull mode\n");
return 0;
}

#ifdef CONFIG_ORANGEPI_3
int offset = ((index - ((index >> 4) << 4)) << 1);
pullOffset = 0x1C;

if (bank == 11)
{
phyaddr = pullOffset + GPIOL_BASE + ((index >> 4) << 2);
}
else
phyaddr = pullOffset + GPIO_BASE_MAP + (bank * 36) + ((index >> 4) << 2);
#endif

/* Ignore unused gpio */
if (ORANGEPI_PIN_MASK[bank][index] != -1)
{
#ifdef CONFIG_ORANGEPI_3
regval = readR(phyaddr);
if (wiringPiDebug)
printf("Before read reg val: 0x%x offset:%d\n", regval, offset);

if (wiringPiDebug)
printf("Register[%#x]: %#x index:%d\n", phyaddr, regval, index);

regval &= ~(3 << offset);
regval |= (pud_OP & 3) << offset;
if (wiringPiDebug)
printf("New reg val to be set: %#x\n", regval);
writeR(regval, phyaddr);
regval = readR(phyaddr);
if (wiringPiDebug)
printf("Pull set over reg val: %#x\n", regval);
#elif
printf("Pin pull control is not implemented!\n");
#endif
}
else
printf("Pin pull control failed!\n");

return 0;
}

#if !(defined CONFIG_ORANGEPI_RK3399 || defined CONFIG_ORANGEPI_4 || defined CONFIG_ORANGEPI_4_LTS || defined CONFIG_ORANGEPI_800 || defined CONFIG_ORANGEPI_R1PLUS || CONFIG_ORANGEPI_2G_IOT)
int OrangePi_set_gpio_alt(int pin, int mode)
{
Expand Down
5 changes: 5 additions & 0 deletions wiringPi/OrangePi.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ extern volatile unsigned int *grf_base;
#define PWM_CLK_DIV_48K 11
#define PWM_CLK_DIV_72K 12

#define SUNXI_PUD_OFF 0
#define SUNXI_PUD_UP 1
#define SUNXI_PUD_DOWN 2

extern int pinToGpioOrangePi[64];
extern int physToGpioOrangePi[64];
extern int physToPinOrangePi[64];
Expand All @@ -233,6 +237,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_pullUpDnControl(int pin, int pud);
#if !(defined CONFIG_ORANGEPI_RK3399 || defined CONFIG_ORANGEPI_4 || defined CONFIG_ORANGEPI_4_LTS || defined CONFIG_ORANGEPI_800 || defined CONFIG_ORANGEPI_R1PLUS || CONFIG_ORANGEPI_2G_IOT)
extern int OrangePi_set_gpio_alt(int pin, int mode);
#endif
Expand Down
31 changes: 31 additions & 0 deletions wiringPi/wiringPi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,37 @@ void pullUpDnControl (int pin, int pud)

setupCheck ("pullUpDnControl") ;

#ifdef CONFIG_ORANGEPI
if(version == ORANGEPI ) {
if (wiringPiDebug)
printf("PinMode: pin:%d,pull up/down:%d\n", pin, pud);
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 (pud == PUD_UP || pud == PUD_DOWN || pud == PUD_OFF) {
OrangePi_set_gpio_pullUpDnControl(pin, pud);
return;
} else
return;
} else {
if ((node = wiringPiFindNode (pin)) != NULL)
node->pullUpDnControl (node, pin, pud) ;
return ;
}
}
#endif

if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
{
/**/ if (wiringPiMode == WPI_MODE_PINS)
Expand Down

0 comments on commit bdfad0f

Please sign in to comment.