This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to use Gpio in functions?

Other Parts Discussed in Thread: CONTROLSUITE

Hello,

I m trying to use gpio in function parameter. For example;

 void gpioset (x,y) { x=1; y=0; }

X is GpioDataRegs.GPADAT.bit.GPIO0, Y is GpioDataRegs.GPADAT.bit.GPIO1.

I've tried all kinds of ways, but couldn't do it. (struct, pointer etc.)

I have to create this function,I'm waiting for suggestions.Thanks!

  • Hello Amie,
    I am sorry to ask, are you still learning C?
    If so, you can refer to this tutorial www.tutorialspoint.com/.../c_functions.htm about making the function in c.
    Hope after learning about making function in c, you can see what the solution of your problem.
    If actually you are good in C, I am sorry about my sentences.
    Btw, GPADAT is a kind of function, therefore you can't put is as a parameter. It should be in the body of the function.

    Good luck!

    Best regards,
    Maria
  • Adding to what Maria has suggested please refer the GPIO example codes that you can find in controlSuite.
    Let us know if you need some more info.

    Regards,
    Gautam
  • GPADAT.bit.GPIO0 and GPADAT.bit.GPIO1 are bit fields. Bit fields don't work like normal variables -- you can't create a pointer to them or get their size using sizeof(). There's no way to pass a reference to a bit field into a function.

    If you want to make a function that works on any GPIO pin, you'll either need to pass the register address and bit offset as parameters or calculate them on the fly. The second approach is probably easier. Here's one way to do it. It's better to use GPASET and GPACLEAR instead of writing directly to GPADAT, so I do that instead.

    void gpioset(uint16_t firstGpio, uint16_t secondGpio)
    {

    const uint32_t *setRegs[] = {&GpioDataRegs.GPASET.all, &GpioDataRegs.GPBSET.all, &GpioDataRegs.GPCSET.all};  //Add more GPySETs if needed
    const uint32_t *clearRegs[] = {&GpioDataRegs.GPACLEAR.all, &GpioDataRegs.GPBCLEAR.all, &GpioDataRegs.GPCCLEAR.all};  //Add more GPyCLEARs if needed
    setRegs[firstGpio/32] = 1ul << (firstGpio % 32);
    clearRegs[secondGpio/32] = 1ul << (secondGpio % 32);

    }

    I didn't actually compile that code, so I can't guarantee that it's bug-free, but it should give you the right idea. It would be better to use GPASET and GPACLEAR instead of writing to the data register directly, of course.