GPIO (JSAPI_GPIO)

GPIO (General Purpose Input Output) pins are the most basic of MCU peripherals. A GPIO pin can serve as an input, an output, or a signal pin for another peripheral (e.g. TX for an USART) – this is called the alternate functions of the pin. Some GPIO pins also support ADC (Analog Digital Converter) functions. What alternate functions are possible on a GPIO pin are device dependent, and users must refer to the device datasheet to check which features are allowed on a given GPIO pin.

Object

extern JSAPI_GPIO porta, portb, portc, portd, porte, portf, portg, portk;

Enum

enum PUPDR {

    PUPDR_NONE = 0,    

    PUPDR_UP,          

    PUPDR_DOWN

};        

 

enum OTYPE {

    OTYPE_PUSHPULL = 0,

    OTYPE_OPENDRAIN

};   

STM32F0xx Only

enum OSPEED {

    OSPEED_LOW,         /**< 2 MHz */

    OSPEED_MED,         /**< 10 MHz */

    OSPEED_HIGH,

    OSPEED_HIGHEST = OSPEED_HIGH

};

Non-STM32F0xx Only

enum OSPEED {

    OSPEED_LOW,         /**< 2 MHz */

    OSPEED_MED,         /**< 10 MHz */

    OSPEED_HIGH,

    OSPEED_VERYHIGH,

    OSPEED_HIGHEST = OSPEED_VERYHIGH

};

Input Pin Functions

Make a pin into an input

void MakeInput(unsigned pin_no);

Read the port’s  value

unsigned Read(void);

Output Pin Functions

Make a pin into an output

void MakeOutput(unsigned pin_no, enum OSPEED speed);

Set the output type

void SetOType(unsigned pin_no, enum OTYPE otype);

Write a value to the PORT

void Write(unsigned val);

Write a “1” to a pin

void Set(unsigned pin_no);

Write a “0” to a pin

void Clear(unsigned pin_no);

Assuming the current pin out is high/1, write a "0" to an output pin

void JiggleLow(

    unsigned pin_no,

    unsigned millisecs_delay

);

Assuming the current pin out is low/0, write a "1" to an (output) pin

void JiggleHigh(

    unsigned pin_no,

    unsigned millisecs_delay

);

Other Functions

Make a pin into an alternate function pin

void MakeAltFunction(

    unsigned pin_no,

    unsigned f,

    enum OSPEED ospeed

);

Make a pin into an analog pin

void MakeAnalog(unsigned pin_no);

Set internal pull-up or pull-down

void SetPullUpDown(unsigned pin_no, enum PUPDR pupdr);

 

The SysTick timer is used for the “Jiggle” functions, so it must be set up to accommodate the time delay resolution.

How to Use

To use the JumpStart GPIO API, with a JSAPI_GPIO object (e.g. porta):

1.       Call one of the “Make” functions, e.g. MakeInput, MakeAltFunction, … etc. to initialize a pin.

2.       If applicable, call SetPullUpDown to set the internal pull-up or pull-down resistor.

3.       For an output pin, optionally call the SetOType to set the output type (open drain or push-pull).

Example

// blink an LED connected to PORTA.5

porta.MakeOutput(5, OSPEED_LOW);

while (1)

      {

      porta.Set(5);

      DelayMilliseconds(500);

porta.Clear(5);

DelayMillisecons(700);

}