gpio_read() works with pin GC0 but not GA3

All information, news and questions about the Pine64 IoT-stamp can be found and discussed here
cheydrick
Posts: 8
Joined: Tue Apr 04, 2017 9:47 pm

gpio_read() works with pin GC0 but not GA3

Postby cheydrick » Sat Apr 08, 2017 12:03 am

I modified the uart sample code to output the status of a GPIO pin. I initially chose pin GA3 since it's shown on the pinout diagram as being solely for GPIO. No matter what I did, gpio_read() always returned 0.

On a whim I changed the pin to GC0, and gpio_read() correctly returned 1 when the pin is tied to +V, and 0 when tied to ground.

Can anyone explain this behavior? Here's my code:

Code: Select all

#include "device.h"
#include "serial_api.h"
#include "gpio_api.h"

#define UART_TX    PA_4
#define UART_RX    PA_0
#define GPIO_PIN   PC_0 //PA_3 doesn't work. Why?

void uart_send_string(serial_t *sobj, char *pstr)
{
    unsigned int i=0;

    while (*(pstr+i) != 0) {
        serial_putc(sobj, *(pstr+i));
        i++;
    }
}

void main(void)
{
    gpio_t gpio_in;
    gpio_init(&gpio_in, GPIO_PIN);
    gpio_dir(&gpio_in, PIN_INPUT);
    gpio_mode(&gpio_in, PullNone);

    char gpio_status_string[8] = "\0";
    char rc;
    int ret;   

    serial_t sobj;
    serial_init(&sobj,UART_TX,UART_RX);
    serial_baud(&sobj,38400);
    serial_format(&sobj, 8, ParityNone, 1);

    while(1){
        // This blocks. Hit Enter to read value and output.
        rc = serial_getc(&sobj);
        ret = gpio_read(&gpio_in);

        uart_send_string(&sobj, "\r\ngpio_read returns:");
        rtl_sprintf(gpio_status_string, " %i\0", ret);
        uart_send_string(&sobj, gpio_status_string);
}


cheydrick
Posts: 8
Joined: Tue Apr 04, 2017 9:47 pm

Re: gpio_read() works with pin GC0 but not GA3

Postby cheydrick » Tue Apr 11, 2017 4:39 pm

I modified my program so that I'm looking at the status of P_C0 and P_A3. It polls both pins and when one of them changes it outputs to serial.

I also set both pins to PullDown. What I found is that P_C0 obeys this, but P_A3 is pulled to +V.

Is there an explanation I've missed in the documentation that would explain why a GPIO pin would ignore setting the mode to PullDown?

Code: Select all

#include "device.h"
#include "serial_api.h"
#include "gpio_api.h"

#define UART_TX    PA_4
#define UART_RX    PA_0
#define GPIO_PINC0   PC_0
#define GPIO_PINA3   PA_3

void uart_send_string(serial_t *sobj, char *pstr)
{
    unsigned int i=0;

    while (*(pstr+i) != 0) {
        serial_putc(sobj, *(pstr+i));
        i++;
    }
}

void main(void)
{
    gpio_t gpio_inC0;
    gpio_init(&gpio_inC0, GPIO_PINC0);
    gpio_dir(&gpio_inC0, PIN_INPUT);
    gpio_mode(&gpio_inC0, PullDown);

    gpio_t gpio_inA3;
    gpio_init(&gpio_inA3, GPIO_PINA3);
    gpio_dir(&gpio_inA3, PIN_INPUT);
    gpio_mode(&gpio_inA3, PullDown);

    char gpio_status_string[8] = "\0";
    char rc;

    int retC0 = -1;
    int retC0_tmp = -1;
    int retA3 = -1;
    int retA3_tmp = -1;
   
    serial_t sobj;
    serial_init(&sobj,UART_TX,UART_RX);
    serial_baud(&sobj,38400);
    serial_format(&sobj, 8, ParityNone, 1);

    while(1)
    {
        retC0_tmp = gpio_read(&gpio_inC0);
        retA3_tmp = gpio_read(&gpio_inA3);

        if (retC0_tmp != retC0)
        {
            uart_send_string(&sobj, "GC_0: ");
            rtl_sprintf(gpio_status_string, "%i\r\n\0", retC0_tmp);
            uart_send_string(&sobj, gpio_status_string);
            retC0 = retC0_tmp;
        }

        if (retA3_tmp != retA3)
        {
            uart_send_string(&sobj, "GA_3: ");
            rtl_sprintf(gpio_status_string, "%i\r\n\0", retA3_tmp);
            uart_send_string(&sobj, gpio_status_string);
            retA3 = retA3_tmp;
        }
    }
}

cheydrick
Posts: 8
Joined: Tue Apr 04, 2017 9:47 pm

Re: gpio_read() works with pin GC0 but not GA3

Postby cheydrick » Tue Apr 11, 2017 5:35 pm

Here's another clue. I accidentally found out that the UART_LOG pins are always sending serial data, and have interesting debug information. Where is this documented?! That would have been nice to know.

When my program hits the gpio_init functions for P_A3, I get the message "[GPIO Err]HAL_GPIO_Init: GPIO Pin(3) Unavailable". I get this message three times. Once for gpio_init(), gpio_dir(), and gpio_mode().

Does anyone know what I have to do to enable usage of that pin?

http://imgur.com/a/y59nR


Return to “Pine64 - PADI IoT Stamp”

Who is online

Users browsing this forum: No registered users and 1 guest