Wixel SDK
Macros | Functions
gpio.h File Reference
#include <cc2511_types.h>

Go to the source code of this file.

Macros

#define LOW   0
 
#define HIGH   1
 
#define HIGH_IMPEDANCE   0
 
#define PULLED   1
 

Functions

void setDigitalOutput (uint8 pinNumber, BIT value) __reentrant
 Configures the specified pin as a digital output. More...
 
void setDigitalInput (uint8 pinNumber, BIT pulled) __reentrant
 Configures the specified pin as an input. More...
 
BIT isPinHigh (uint8 pinNumber) __reentrant
 Returns the current input or output value of the pin. More...
 
void setPort0PullType (BIT pullType) __reentrant
 
void setPort1PullType (BIT pullType) __reentrant
 
void setPort2PullType (BIT pullType) __reentrant
 

Detailed Description

The gpio.lib library provides functions for using the CC2511's pins as general purpose inputs or outputs (GPIO). Every pin on the CC2511 that has a name starting with P can be configured as a digital input or digital output.

The functions in this library allow for simpler programmatic approaches to working with digital I/O since you no longer have to deal with a multitude of pin-specific registers.

Ports

The pins on the CC2511 are divided into three ports: Port 0 (P0), Port 1 (P1), and Port 2 (P2). Every pin's name is prefixed by the name of the port it is on. For example, P0_3 starts with "P0" so it is a pin on Port 0.

On the Wixel, none of the pins on Port 0 and Port 1 are tied to any on-board hardware so they completely free to be used as GPIO.

When the Wixel starts up, all the pins on Port 0 and Port 1 will be inputs with internal pull-up resistors enabled except P1_0 and P1_1, which do not have internal pull-up or pull-down resistors

This library supports Port 2, but all of the Wixel's Port 2 pins are handled by the functions declared in board.h so you should not need to manipulate them with this library.

The pinNumber parameter

Most of the functions in this library take a pin number as their first argument. These numbers are computed by multiplying the first digit in the pin name by ten and adding it to the second digit, as shown in the table below.

CC2511 Pins
PinpinNumber parameter
P0_00
P0_11
P0_22
P0_33
P0_44
P0_55
P1_010
P1_111
P1_212
P1_313
P1_414
P1_515
P1_616
P1_717
P2_020
P2_121
P2_222
P2_323
P2_424

Interrupts

All the functions in this library are declared as reentrant, which means it is safe to call them in your main loop and also in your interrupt service routines (ISRs). However, if you are using these functions in an ISR, you should make sure that you have no code in your main loop that does a non-atomic read-modify-write operation on any of the I/O registers that are changed in the interrupt. The risk is that the interrupt could fire after while the read-modify-write operation is in progress, after the register has been read but before it has been written. Then when the register is written by the main loop, the change made by the ISR will be unintentionally lost.

For example, it would be bad if you called setDigitalOutput(10, 1) in an interrupt and in your main loop you had some code like:

P1DIR = (P1DIR & MASK) | VALUE;

It is OK to have code like P1DIR |= VALUE; in your main loop because that compiles to a single instruction, so it should be atomic.

Overhead

Calling the functions in this library will be slower than manipulating the I/O registers yourself, but the overhead should be roughly the same for each pin.

This library (git revision 4de9ee1f) was tested with SDCC 3.0.0 (#6037) and it was found that an I/O line could be toggled once every 3.2 microseconds by calling setDigitalOutput() several times in a row.

Caveats

To use your digital I/O pins correctly, there are several things you should be aware of:

Definition in file gpio.h.

Macro Definition Documentation

#define HIGH   1

Represents a high voltage, also known as 3V3 (typically 3.3 V).

Definition at line 124 of file gpio.h.

#define HIGH_IMPEDANCE   0

Specifies that the input pin should be a high-impedance input with no pull-up or pull-down resistor enabled. See setDigitalInput().

Definition at line 128 of file gpio.h.

#define LOW   0

Represents a low voltage, also known as GND or 0 V.

Definition at line 121 of file gpio.h.

#define PULLED   1

Specifies that the pin should have a 20 kilohm pull-up or pull-down enabled. See setDigitalInput().

Definition at line 132 of file gpio.h.

Function Documentation

BIT isPinHigh ( uint8  pinNumber)

Returns the current input or output value of the pin.

Parameters
pinNumberShould be one of the pin numbers listed in the table above (e.g. 12).
Returns
LOW (0) or HIGH (1).

The return value represents a digital reading of the voltage on the pin. According to the "DC Characteristics" section of the CC2511 datasheet, voltages below 30% of VDD (typically 0.99 V on the Wixel) will read as 0, while voltages above 70% of VDD (typically 2.31 V on the Wixel) will read as 1.

This function is intended to be used for pins that are configured as inputs. For a pin configured as an output, it can be used, but it might sometimes give unexpected results in case the current voltage has not reached the voltage that the pin is configured to drive it to.

This function simply returns the bit value of the port. For example, calling isPinHigh(14) will have the effect as reading P1_4 (but the function call will be slower).

Definition at line 46 of file gpio.c.

void setDigitalInput ( uint8  pinNumber,
BIT  pulled 
)

Configures the specified pin as an input.

Parameters
pinNumberShould be one of the pin numbers listed in the table above (e.g. 12).
pulledShould be one of the following:
  • HIGH_IMPEDANCE (0): Disables the internal pull-up and pull-down resistors on that pin.
  • PULLED (1): Enables an internal 20 kilohm pull-up or pull-down resistor on the pin. The type of resistor used is set at the port level. By default, Port 0 and Port 1 use pull-up resistors, but you can change those ports to use pull-down resistors calling setPort0PullType() or setPort1PullType(). You can not have pull-up and pull-down resistors enabled simultaneously for different pins on the same port.

This function first sets the pull type, then it sets the pin direction. For example, calling setDigitalInput(15, PULLED) will have the same effect as (but be slower than) this:

1 P1SEL &= ~(1<<5);
2 P1DIR &= ~(1<<5);

Note: The pins P1_0 and P1_1 do NOT have internal pull-up or pull-down resistors, so the second argument to this function does not have any effect when configuring either of those pins.

Definition at line 41 of file gpio.c.

void setDigitalOutput ( uint8  pinNumber,
BIT  value 
)

Configures the specified pin as a digital output.

Parameters
pinNumberShould be one of the pin numbers listed in the table above (e.g. 12).
valueShould be one of the following:
  • LOW (0): Drives the line low (GND, 0 V).
  • HIGH (1): Drives the line high (3V3, typically 3.3 V).

This function will not work if the pin has previously been configured as a peripheral function pin; the bit for this pin in P0SEL/P1SEL/P2SEL must be 0.

This function first sets the output value, then it sets the pin direction. For example, calling setDigitalOutput(3, HIGH) will have the same effect as (but be slower than) this:

1 P0_3 = 1;
2 P0DIR |= (1<<3);

Definition at line 36 of file gpio.c.

void setPort0PullType ( BIT  pullType)

Selects whether Port 0 will have internal pull-down or pull-up resistors.

Parameters
pullTypeSpecifies the voltage that the resistors will pull to. Should be either LOW (0) or HIGH (1).

The resistors can be disabled individually for each pin using setDigitalInput(), but it is impossible to have pull-up and pull-down resistors enabled simultaneously for different pins on the same port.

Definition at line 52 of file gpio.c.

void setPort1PullType ( BIT  pullType)

Same as setPort0PullType() except this function affects Port 1.

Definition at line 58 of file gpio.c.

void setPort2PullType ( BIT  pullType)

Same as setPort0PullType() except this function affects Port 2. This function is included for the sake of completeness, but it should not be used on a Wixel because all of the pins on Port 2 are managed by the functions declared in board.h.

Definition at line 64 of file gpio.c.