/* $Id: memtest.hh,v 1.8 2008-06-24 10:03:41 sybreon Exp $
|
/* $Id: memtest.hh,v 1.8 2008-06-24 10:03:41 sybreon Exp $
|
**
|
**
|
** VIRTUAL PERIPHERAL I/O LIBRARY
|
** VIRTUAL PERIPHERAL I/O LIBRARY
|
** Copyright (C) 2009 Shawn Tan <shawn.tan@aeste.net>
|
** Copyright (C) 2009 Shawn Tan <shawn.tan@aeste.net>
|
**
|
**
|
** This file is part of AEMB.
|
** This file is part of AEMB.
|
**
|
**
|
** AEMB is free software: you can redistribute it and/or modify it
|
** AEMB is free software: you can redistribute it and/or modify it
|
** under the terms of the GNU General Public License as published by
|
** under the terms of the GNU General Public License as published by
|
** the Free Software Foundation, either version 3 of the License, or
|
** the Free Software Foundation, either version 3 of the License, or
|
** (at your option) any later version.
|
** (at your option) any later version.
|
**
|
**
|
** AEMB is distributed in the hope that it will be useful, but WITHOUT
|
** AEMB is distributed in the hope that it will be useful, but WITHOUT
|
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
** License for more details.
|
** License for more details.
|
**
|
**
|
** You should have received a copy of the GNU General Public License
|
** You should have received a copy of the GNU General Public License
|
** along with AEMB. If not, see <http://www.gnu.org/licenses/>.
|
** along with AEMB. If not, see <http://www.gnu.org/licenses/>.
|
*/
|
*/
|
|
|
/*!
|
/*!
|
GPIO C Interface Library.
|
GPIO C Interface Library.
|
@file
|
@file
|
*/
|
*/
|
|
|
#ifndef VPIO_GPIO_H
|
#ifndef VPIO_GPIO_H
|
#define VPIO_GPIO_H
|
#define VPIO_GPIO_H
|
|
|
|
typedef char gpioData;
|
|
|
/*!
|
/*!
|
GPIO Directions.
|
GPIO Directions.
|
*/
|
*/
|
|
|
enum vpioGpioTrisType
|
enum gpioTrisType
|
{
|
{
|
GPIO_INPUT = 0, ///< Define GPIO pin as an input
|
GPIO_INPUT = 0, ///< Define GPIO pin as an input
|
GPIO_OUTPUT = 1 ///< Define GPIO pin as an output
|
GPIO_OUTPUT = 1 ///< Define GPIO pin as an output
|
};
|
};
|
|
|
|
typedef enum gpioTrisType gpioTrisType;
|
|
|
/*!
|
/*!
|
GPIO Register Map.
|
GPIO Register Map.
|
This structure contains the register file of a general-purpose
|
This structure contains the register file of a general-purpose
|
I/O. Care should be taken when using it due to endian issues.
|
I/O. Care should be taken when using it due to endian issues.
|
*/
|
*/
|
|
|
struct vpioGpioRegs
|
struct gpioRegs
|
{
|
{
|
volatile int regCtrl; ///< GPIO direction register.
|
volatile gpioData regCtrl; ///< GPIO direction register.
|
//unsigned int /* unused */ :24;
|
unsigned int /* unused */ :24;
|
volatile int regLine; ///< GPIO line data register.
|
volatile gpioData regLine; ///< GPIO line data register.
|
//unsigned int /* unused */ :24;
|
unsigned int /* unused */ :24;
|
};
|
};
|
|
|
typedef struct vpioGpioRegs vpioGpioRegs;
|
typedef struct gpioRegs gpioRegs;
|
|
|
|
// ==== GPIO INTERFACE ====
|
|
|
|
gpioData gpioGetBit(gpioRegs* port, gpioData bit);
|
|
void gpioSetBit(gpioRegs* port, gpioData bit);
|
|
void gpioTogBit(gpioRegs* port, gpioData bit);
|
|
void gpioClrBit(gpioRegs* port, gpioData bit);
|
|
|
|
//void gpioPutTris(gpioRegs* port, gpioData mask);
|
|
void gpioSetTris(gpioRegs* port, gpioData mask);
|
|
void gpioPutData(gpioRegs* port, gpioData data);
|
|
//gpioData gpioGetTris(gpioRegs* port);
|
|
gpioData gpioGetData(gpioRegs* port);
|
|
|
|
void gpioInit(gpioRegs* port);
|
|
|
|
|
/*!
|
/*!
|
Get a port bit.
|
Get a port bit.
|
|
|
This function reads the value of a specific bit on a GPIO port. It
|
This function reads the value of a specific bit on a GPIO port. It
|
reads the entire port line and masks out the specific bit. The
|
reads the entire port line and masks out the specific bit. The
|
result should be interpreted as zero for 0 and non-zero for 1.
|
result should be gpioDataerpreted as zero for 0 and non-zero for 1.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@param bit Get port bit value.
|
@param bit Get port bit value.
|
@return Value of the port bit.
|
@return Value of the port bit.
|
*/
|
*/
|
|
|
inline
|
inline
|
int vpioGpioGetBit(vpioGpioRegs* port, int bit)
|
gpioData gpioGetBit(gpioRegs* port, gpioData bit)
|
{
|
{
|
return port->regLine & (1 << bit);
|
return port->regLine & (1 << bit);
|
}
|
}
|
|
|
/*!
|
/*!
|
Set a port bit.
|
Set a port bit.
|
|
|
This function sets a specific bit on a GPIO port to 1. It is a
|
This function sets a specific bit on a GPIO port to 1. It is a
|
read-modify-write instruction that reads the port value, masks it
|
read-modify-write instruction that reads the port value, masks it
|
and writes the new value to the port.
|
and writes the new value to the port.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@param bit Set port bit to 1.
|
@param bit Set port bit to 1.
|
*/
|
*/
|
|
|
inline
|
inline
|
void vpioGpioSetBit(vpioGpioRegs* port, int bit)
|
void gpioSetBit(gpioRegs* port, gpioData bit)
|
{
|
{
|
port->regLine |= (1 << bit);
|
port->regLine |= (1 << bit);
|
}
|
}
|
|
|
/*!
|
/*!
|
Toggle a port bit.
|
Toggle a port bit.
|
|
|
This function toggles a specific bit on a GPIO port. It is a
|
This function toggles a specific bit on a GPIO port. It is a
|
read-modify-write instruction that reads the port value, masks it
|
read-modify-write instruction that reads the port value, masks it
|
and writes the new value to the port.
|
and writes the new value to the port.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@param bit Toggle port bit between 0 and 1.
|
@param bit Toggle port bit between 0 and 1.
|
*/
|
*/
|
|
|
inline
|
inline
|
void vpioGpioTogBit(vpioGpioRegs* port, int bit)
|
void gpioTogBit(gpioRegs* port, gpioData bit)
|
{
|
{
|
port->regLine ^= (1 << bit);
|
port->regLine ^= (1 << bit);
|
}
|
}
|
|
|
/*!
|
/*!
|
Clear a port bit.
|
Clear a port bit.
|
|
|
This function clears a specific bit on a GPIO port to 0. It is a
|
This function clears a specific bit on a GPIO port to 0. It is a
|
read-modify-write instruction that reads the port value, masks it
|
read-modify-write instruction that reads the port value, masks it
|
and writes the new value to the port.
|
and writes the new value to the port.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@param bit Clear port bit to 0.
|
@param bit Clear port bit to 0.
|
*/
|
*/
|
|
|
inline
|
inline
|
void vpioGpioClrBit(vpioGpioRegs* port, int bit)
|
void gpioClrBit(gpioRegs* port, gpioData bit)
|
{
|
{
|
port->regLine &= ~(1 << bit);
|
port->regLine &= ~(1 << bit);
|
}
|
}
|
|
|
/*!
|
/*!
|
Configure the entire port.
|
Configure the entire port.
|
|
|
This function configures the entire port, which controls the
|
This function configures the entire port, which controls the
|
direction of specific bits of the port. It writes the entire port
|
direction of specific bits of the port. It writes the entire port
|
mask to the port control register.
|
mask to the port control register.
|
|
|
@see vpioGpioTrisType
|
@see gpioTrisType
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@param mask The entire port config mask.
|
@param mask The entire port config mask.
|
*/
|
*/
|
|
|
inline
|
inline
|
void vpioGpioCfgPort(vpioGpioRegs* port, int mask)
|
void gpioSetTris(gpioRegs* port, gpioData mask)
|
{
|
{
|
port->regCtrl = mask;
|
port->regCtrl = mask;
|
}
|
}
|
|
|
|
|
|
/*!
|
|
Configure the entire port.
|
|
|
|
This function configures the entire port, which controls the
|
|
direction of specific bits of the port. It writes the entire port
|
|
mask to the port control register.
|
|
|
|
@see gpioTrisType
|
|
@param port Memory-mapped I/O port.
|
|
@param mask The entire port config mask.
|
|
*/
|
|
|
|
inline
|
|
gpioData gpioGetTris(gpioRegs* port)
|
|
{
|
|
//port->regCtrl = mask;
|
|
return port->regCtrl;
|
|
}
|
|
|
/*!
|
/*!
|
Put data on the port.
|
Put data on the port.
|
|
|
This function writes onto the entire data line of the GPIO port. It
|
This function writes onto the entire data line of the GPIO port. It
|
writes the value directly to the port register.
|
writes the value directly to the port register.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@param data The data to write to the port.
|
@param data The data to write to the port.
|
*/
|
*/
|
|
|
inline
|
inline
|
void vpioGpioPutPort(vpioGpioRegs* port, int data)
|
void gpioPutData(gpioRegs* port, gpioData data)
|
{
|
{
|
port->regLine = data;
|
port->regLine = data;
|
}
|
}
|
|
|
/*!
|
/*!
|
Get data from the port.
|
Get data from the port.
|
|
|
This function reads the entire data line of the GPIO port. It reads
|
This function reads the entire data line of the GPIO port. It reads
|
the value directly from the port register. Any output port values
|
the value directly from the port register. Any output port values
|
will appear on the input unless something is wrong.
|
will appear on the input unless something is wrong.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
@return The contents of the entire port.
|
@return The contents of the entire port.
|
*/
|
*/
|
|
|
inline
|
inline
|
int vpioGpioGetPort(vpioGpioRegs* port)
|
gpioData gpioGetData(gpioRegs* port)
|
{
|
{
|
return port->regLine;
|
return port->regLine;
|
}
|
}
|
|
|
/*!
|
/*!
|
Clear the entire port.
|
|
|
|
This function clears the data pins of the entire GPIO port to 0. It
|
|
writes the value directly to the port register.
|
|
|
|
@param port Memory-mapped I/O port.
|
|
*/
|
|
|
|
inline
|
|
void vpioGpioClrPort(vpioGpioRegs* port)
|
|
{
|
|
vpioGpioPutPort(port, 0);
|
|
}
|
|
|
|
/*!
|
|
Initialise the GPIO port.
|
Initialise the GPIO port.
|
|
|
This function configures the entire GPIO port as inputs and clears
|
This function configures the entire GPIO port as inputs and clears
|
the whole port data register. This will restore the port to its
|
the whole port data register. This will restore the port to its
|
reset condition.
|
reset condition.
|
|
|
@param port Memory-mapped I/O port.
|
@param port Memory-mapped I/O port.
|
*/
|
*/
|
|
|
inline
|
inline
|
void vpioGpioIniPort(vpioGpioRegs* port)
|
void gpioInit(gpioRegs* port)
|
{
|
{
|
vpioGpioCfgPort(port, 0);
|
gpioSetTris(port, 0);
|
vpioGpioClrPort(port);
|
gpioPutData(port, 0);
|
}
|
}
|
|
|
#endif
|
#endif
|
|
|