[En-Nut-Discussion] RFA_4: Nut/OS GPIO API

Uwe Bonnes bon at elektron.ikp.physik.tu-darmstadt.de
Wed Oct 24 13:30:15 CEST 2012


>>>>> "Ulrich" == Ulrich Prinz <ulrich.prinz at googlemail.com> writes:

    Ulrich> I just wanted to warn, not extend the discussion. Just keep the
    Ulrich> warning in the RFA, that's all.

    Ulrich> In you rexample, asserts (<port is valid> && <pin_is_valid>)
    Ulrich> highly depends if both conditions can be decoded to conditions
    Ulrich> without variables in pass one by the compiler. So I would
    Ulrich> consider this as only 90% true.

    Ulrich> But normally users will write their code for a certain platform
    Ulrich> an will check that the pin is valid and the port is available.
    Ulrich> We additionally defined, that the port and pin can be detected
    Ulrich> for availability and support of a certain function by the
    Ulrich> appropriate setup functions. So we are on safe ground.

    Ulrich> There is only a slight chance that someone is going nuts while
    Ulrich> trying to figure out why his timing is going wrong, while
    Ulrich> seconds ago it perfectly worked...

Appended code compiled on linux gives:

main.c: In function ‘main’:
main.c:30:5: error: call to ‘port_check’ declared with attribute error: assertion failure: 'IS_GPIO_ALL_PERIPH(6)' not true
main.c:31:5: error: call to ‘pin_check’ declared with attribute error: assertion failure: 'PIN_IS_VALID(17)' not true

I think something similar can be done for arm-xx-gcc and the GPIO API (on gcc).

Bye
-- 
Uwe Bonnes                bon at elektron.ikp.physik.tu-darmstadt.de

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
#include <stdio.h>
#include <stdint.h>

#define CTC_PORT(X) ({ extern int __attribute__((error("assertion failure: '" #X "' not true"))) port_check(); ((X)?0:port_check()),0; })
#define CTC_PIN(X) ({ extern int __attribute__((error("assertion failure: '" #X "' not true"))) pin_check(); ((X)?0:pin_check()),0; })
#define GPIOA 0
#define GPIOB 1
#define GPIOC 2
#define GPIOD 3
#define GPIOE 4
#define GPIOF 5

#define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIOA) || \
                                    ((PERIPH) == GPIOB) || \
                                    ((PERIPH) == GPIOC) || \
                                    ((PERIPH) == GPIOD) || \
                                    ((PERIPH) == GPIOE))
#define PIN_IS_VALID(PIN) (PIN >=0 && PIN <16)

#define GpioPinGet(bank, bit) do {\
        (__builtin_constant_p (bank))?CTC_PORT(IS_GPIO_ALL_PERIPH(bank)):assert(IS_GPIO_ALL_PERIPH(bank)); \
        (__builtin_constant_p (bit))?CTC_PIN(PIN_IS_VALID(bit)):assert(PIN_IS_VALID(bit)); \
        printf("Port %d pin %d\n", bank, bit); } while(0)

int main(int argc, char **argv)
{

    GpioPinGet(argc, argc);
    GpioPinGet(0,0);
    GpioPinGet(6,0);
    GpioPinGet(0,17);
    
    return 0;
}


More information about the En-Nut-Discussion mailing list