APPENDIX C

image

Class GPIO 3

This appendix documents the static class method calls for the GPIO class and is useful for those wanting to:

  • Determine the PWM number and alternate function based on gpio number

  • Delay a short period of time for configuration reasons

  • Obtain the “peripheral base” value

  • Obtain a text string for GPIO::Source value

  • Return a text string for GPIO::IO value

  • Return a descriptive string for the combination of gpio number and GPIO::IO value

For include file and linking instructions, see Appendix A.

Class Definition (Part 3)

In this third part of our look at the GPIO class, I’ll describe the methods applicable to static methods only:

Class GPIO {
    bool    errcode;                // errno if GPIO open failed

public:

    GPIO();
    ~GPIO();

    inline int get_error();         // Test for error

    // Static methods
    static int pwm(int gpio,int& pwm,IO& altf);
    static void delay();
    static uint32_t peripheral_base();
    static const char *source_name(Source src);
    static const char *alt_name(IO io);
    static const char *gpio_alt_func(int gpio,IO io);
};

GPIO::pwm

This static class method returns information about the selected PWM peripheral and it’s alternate function number based on the input gpio number alone:

static int pwm(int gpio,int& pwm,IO& altf);

Example code explains this better than words:

int rc, pwm;
GPIO::IO altf;

rc = GPIO::pwm(13,pwm,altf);
assert(!rc);
// pwm == 1 (PWM1)
// altf == GPIO::Alt0

Upon successful return rc will contain zero (else a system error code). When successful, pwm with be populated with the PWM number that the gpio accesses. The argument altf returns which alternate function the PWM uses for this gpio. In the example (gpio=13), the alternate function is returned as GPIO::Alt0.

GPIO::delay

The GPIO::delay static method call takes no arguments. The call does not return until a short delay has occurred. Some internals of the GPIO class make use of this since the CPU is able to act faster than some peripherals can respond:

static void delay();

It is currently implemented as follows, but may be amended in the future if the CPU pace increases significantly:

void
GPIO::delay() {
    for ( int i=0; i<150; i++ ) {
        asm volatile("nop");
    }
}

From this, you can see the delay is a matter of executing 150 nop instructions within a loop. An example of use is:

GPIO::delay();

GPIO::peripheral_base

This is another static method that is used internally. This function opens and reads the Linux pseudo file:

/proc/device-tree/soc/ranges

This determines the peripheral base value for:

  • Raspberry Pi (0x20000000)

  • or Rasbperry Pi 2 (0x3F000000)

The method call takes no parameters and returns the peripheral base address:

static uint32_t peripheral_base();

In this particular case, the value returned should be one of the values listed above. When zero is returned, the procedure has failed.

GPIO::source_name

This static method converts a GPIO::Source constant into a string, so that it can be printed:

static const char *source_name(Source src);

For example:

GPIO::Source src = GPIO::Oscillator;
const char *description = GPIO::source_name(src);
printf("Source is ’%s’ ",description); // “”Oscillator“”

GPIO::alt_name

This is another static method to return a descriptive string that can be printed. This method takes a GPIO::IO constant as input and returns descriptive text for it:

static const char *alt_name(IO io);

The following illustrates the call:

GPIO::IO io = GPIO::Output;
const char *description = GPIO::alt_name(io);

printf("io = ’%s’ ",description); // "Output"

GPIO::gpio_alt_func

This static method call returns descriptive text for a given gpio pin and GPIO::IO setting:

static const char *gpio_alt_func(int gpio,IO io);

The following is an example:

int gpio_no = 20;
GPIO::IO io = GPIO::Alt4;
const char *desc = GPIO::gpio_alt_func(gpio_no,io);

// Returns desc = "SPI1_MOSI"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.96.135