Pin Drivers

All applications at some point will need to manipulate the pins on the microcontroller. For each supported microcontroller, the Embedded SDK includes a driver that provides the ability to read, write and configure the pins.

Each driver contains an enumeration of the available ports (typedef PORT) and pins (typedef PIN) for the target controller. Although each MCU has a different set of available pins, the pin drivers provide a consistent method for reading and setting the pin states.

Basic Usage

The code snippet below illustrates how to read and write a single pin.

#include "mcu_pin.h"


void APP_Function(void)
{
    UINT32 state;


    state = PIN_Read(P1_01);        /* Read pin P1.1, either a '0' or '1' */
    PIN_Write(P1_02, state);        /* Set pin P1.2 to the state of pin P1.1 */
}

The pin drivers also provide the ability to read and write entire ports of pins.

#include "mcu_pin.h"


void APP_Function(void)
{
    UINT32 values;


    values = PORT_Read(PORT_1);     /* Read all of the unmasked pins on port 1 */
    PORT_Write(PORT_2, values);     /* Write all of the unmasked pins on port 1 */
}

Pin Configuration

The pin driver for each supported MCU platform provides functions for configuring the pins. The specific configuration depends upon the MCU, since each MCU has different methods for configuring the pins. The code snippet below shows an example of configuring pins for general purpose outputs and and a UART.

#include "mcu_pin.h"


void APP_Function(void)
{
    PIN_SetDirection(P0_25, DIRECTION_OUTPUT);          /* Configure P0.25 as an output */
    PIN_SetDirection(P0_26, DIRECTION_OUTPUT);          /* Configure P0.26 as an output */

    PIN_SetFunction(P0_02, 1);                          /* Assign P0.2 to the U0_TXD function */
    PIN_SetFunction(P0_03, 1);                          /* Assign P0.3 to the U0_RXD function */
}

API Reference

UINT32 PIN_Read(PIN pin)
Reads and returns the current state of a pin.
PARAMETERS
pin The target pin.
RETURNS
A value '1' if the pin is currently high; otherwise '0' if low.
void PIN_Write(PIN pin, UINT32 value)
Writes a value to a pin.
PARAMETERS
pin The target pin.
value The new value for the specified pin. Use '1' to set the pin as logic level high, or use '0' to set the pin as logic level low.
RETURNS
None.
UINT32 PORT_Read(PORT port)
Reads and returns a bit-field of states of each of the pins for a port. Only valid for the pins that are not currently masked by the PINPORT_SetMask() function.
PARAMETERS
port The target pin port.
RETURNS
A bit value '1' indicates the pin is currently high; otherwise a '0' if the pin is low.
void PORT_Write(PORT port, UINT32 value)
Writes and sets the states of each of the pins for a port. Only applies to the pins that are not currently masked by the PINPORT_SetMask() function.
PARAMETERS
port The target pin port to be written.
value A bit-field of states for each of the pins. A bit value of '1' sets the pin to a logic level high while a '0' sets the pin to a logic level low.
RETURNS
None.
UINT32 PORT_Mask(PORT port)
Reads and returns the current mask for a pin port. A zero in this register's bit enables an access to the corresponding physical pin via a read or write access using either PORT_Write() or PORT_Read(). If a bit in this register is one, the corresponding pin will not be changed with a write using PORT_Write().
PARAMETERS
port The target pin port.
RETURNS
The current mask that is applied to the specified port.
void PORT_SetMask(PORT port, UINT32 mask)
Sets the mask register for a pin port. A zero in this register's bit enables an access to the corresponding physical pin via a read or write access using either PORT_Write() or PORT_Read(). If a bit in this register is one, the corresponding pin will not be changed with a write using PORT_Write().
PARAMETERS
port The target pin port.
mask The new mask value to be applied to the specified port.
RETURNS
None.