Changing GPIO pin direction

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Changing GPIO pin direction

跳至解决方案
2,659 次查看
derekdrost
Contributor III

I am working on using data pins to transmit data back and forth on a bus.

However I am having problems with changing the direction of the data pins from input to output and back.

I have tried using lwgpio_set_direction() but it doesn't seem to be working.

Any ideas?

Thanks in advance,

Derek

1 解答
2,011 次查看
derekdrost
Contributor III

Thank you for the reply Kamil,

     I'm not so sure about initializing the pin again as the first parameter in lwgpio_set_direction() is: handle [in] - Pointer to the LWGPIO_STRUCT pre-initialized by lwgpio_init() function.

     In respect to the pin being in gpio mode, I'll try taking it out of gpio mode, changing the direction, and setting the pin back in gpio mode.

     I'll let you know how it goes.

在原帖中查看解决方案

5 回复数
2,011 次查看
litao
Contributor I

have you init the lwgpio correctly?lwgpio_init()

LWGPIO_STRUCT led1;
lwgpio_init(&led1, BSP_LED1, LWGPIO_DIR_OUTPUT, LWGPIO_VALUE_NOCHANGE);
lwgpio_set_functionality(&led1, BSP_LED1_MUX_GPIO);

0 项奖励
回复
2,011 次查看
derekdrost
Contributor III

Here's the relevant code. No this isn't how it looks, just putting the relevant information.

     Define the data pins

#define PIN_D0(GPIO_PORT_D | GPIO_PIN6)
#define PIN_D1(GPIO_PORT_D | GPIO_PIN5)
#define PIN_D2(GPIO_PORT_D | GPIO_PIN4)
#define PIN_D3(GPIO_PORT_D | GPIO_PIN3)
#define PIN_D4(GPIO_PORT_D | GPIO_PIN2)
#define PIN_D5(GPIO_PORT_C | GPIO_PIN17)
#define PIN_D6(GPIO_PORT_C | GPIO_PIN13)
#define PIN_D7

(GPIO_PORT_C | GPIO_PIN9)

#define MUX_GPIO    (1)

#define LOW   LWGPIO_VALUE_LOW
#define HIGH  LWGPIO_VALUE_HIGH

     Setup the pin structure

LWGPIO_STRUCT* dataPins[8];

LWGPIO_STRUCT D0;

LWGPIO_STRUCT D1;

LWGPIO_STRUCT D2;

LWGPIO_STRUCT D3;

LWGPIO_STRUCT D4;

LWGPIO_STRUCT D5;

LWGPIO_STRUCT D6;

LWGPIO_STRUCT D7;

     Initialize the data pins as output for writing to the bus, all low

if (!lwgpio_init(&D0, PIN_D0, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D1, PIN_D1, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D2, PIN_D2, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D3, PIN_D3, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D4, PIN_D4, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D5, PIN_D5, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D6, PIN_D6, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

    if (!lwgpio_init(&D7, PIN_D7, LWGPIO_DIR_OUTPUT, LOW))

        success = 0;

     set gpio functionality

lwgpio_set_functionality(&D0, MUX_GPIO);

    lwgpio_set_functionality(&D1, MUX_GPIO);

    lwgpio_set_functionality(&D2, MUX_GPIO);

    lwgpio_set_functionality(&D3, MUX_GPIO);

    lwgpio_set_functionality(&D4, MUX_GPIO);

    lwgpio_set_functionality(&D5, MUX_GPIO);

    lwgpio_set_functionality(&D6, MUX_GPIO);

    lwgpio_set_functionality(&D7, MUX_GPIO);

     put pins in an array for easy usage

dataPins [0] = &D0;

    dataPins [1] = &D1;

    dataPins [2] = &D2;

    dataPins [3] = &D3;

    dataPins [4] = &D4;

    dataPins [5] = &D5;

    dataPins [6] = &D6;

    dataPins [7] = &D7;

     Then, if I want to read from the bus I change the direction with the following code

if (dataMode != READ) // make sure data pins are in input mode

    {

        dataMode = READ;

        for (int i = 0; i < 8; i++)

        {

            lwgpio_set_direction(dataPins[i],LWGPIO_DIR_INPUT);

        }

    }

Hopefully this makes the situation a little clearer?

0 项奖励
回复
2,011 次查看
kamilwistuba
Contributor I

Hello Derek,

the description of lwgpio_set_direction() says that "..., it is possible to set the direction of a pin that is currently not in the GPIO mode." I think Your problem has to do something with that. As a second suggestion, I would say that You have to reinitialize with lwgpio_init() if You want to change the pin directions.

Have a nice day,

Kamil

2,012 次查看
derekdrost
Contributor III

Thank you for the reply Kamil,

     I'm not so sure about initializing the pin again as the first parameter in lwgpio_set_direction() is: handle [in] - Pointer to the LWGPIO_STRUCT pre-initialized by lwgpio_init() function.

     In respect to the pin being in gpio mode, I'll try taking it out of gpio mode, changing the direction, and setting the pin back in gpio mode.

     I'll let you know how it goes.

2,011 次查看
panpwr
Contributor IV

Did it work for you? I would be glad if you could elaborate as for the method you've eventually used, in order to achieve the desired GPIO direction change....

0 项奖励
回复