| 1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
| 2 |
|
|
* ATMEL Microcontroller Software Support
|
| 3 |
|
|
* ----------------------------------------------------------------------------
|
| 4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
| 5 |
|
|
*
|
| 6 |
|
|
* All rights reserved.
|
| 7 |
|
|
*
|
| 8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
| 10 |
|
|
*
|
| 11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
| 12 |
|
|
* this list of conditions and the disclaimer below.
|
| 13 |
|
|
*
|
| 14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
| 15 |
|
|
* this software without specific prior written permission.
|
| 16 |
|
|
*
|
| 17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
| 18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
| 19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
| 20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
| 23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
| 24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
| 25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
| 26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 27 |
|
|
* ----------------------------------------------------------------------------
|
| 28 |
|
|
*/
|
| 29 |
|
|
|
| 30 |
|
|
//------------------------------------------------------------------------------
|
| 31 |
|
|
// Headers
|
| 32 |
|
|
//------------------------------------------------------------------------------
|
| 33 |
|
|
|
| 34 |
|
|
#include "pio.h"
|
| 35 |
|
|
#include <board.h>
|
| 36 |
|
|
|
| 37 |
|
|
//------------------------------------------------------------------------------
|
| 38 |
|
|
// Internal definitions
|
| 39 |
|
|
//------------------------------------------------------------------------------
|
| 40 |
|
|
/// \internal Returns the current value of a register.
|
| 41 |
|
|
#define READ(peripheral, register) (peripheral->register)
|
| 42 |
|
|
/// \internal Modifies the current value of a register.
|
| 43 |
|
|
#define WRITE(peripheral, register, value) (peripheral->register = value)
|
| 44 |
|
|
|
| 45 |
|
|
//------------------------------------------------------------------------------
|
| 46 |
|
|
// Internal functions
|
| 47 |
|
|
//------------------------------------------------------------------------------
|
| 48 |
|
|
//------------------------------------------------------------------------------
|
| 49 |
|
|
/// Configures one or more pin(s) of a PIO controller as being controlled by
|
| 50 |
|
|
/// peripheral A. Optionally, the corresponding internal pull-up(s) can be
|
| 51 |
|
|
/// enabled.
|
| 52 |
|
|
/// \param pio Pointer to a PIO controller.
|
| 53 |
|
|
/// \param mask Bitmask of one or more pin(s) to configure.
|
| 54 |
|
|
/// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
|
| 55 |
|
|
/// configured.
|
| 56 |
|
|
//------------------------------------------------------------------------------
|
| 57 |
|
|
static void PIO_SetPeripheralA(AT91S_PIO *pio,
|
| 58 |
|
|
unsigned int mask,
|
| 59 |
|
|
unsigned char enablePullUp)
|
| 60 |
|
|
{
|
| 61 |
|
|
// Disable interrupts on the pin(s)
|
| 62 |
|
|
WRITE(pio, PIO_IDR, mask);
|
| 63 |
|
|
|
| 64 |
|
|
// Enable the pull-up(s) if necessary
|
| 65 |
|
|
if (enablePullUp) {
|
| 66 |
|
|
|
| 67 |
|
|
WRITE(pio, PIO_PPUER, mask);
|
| 68 |
|
|
}
|
| 69 |
|
|
else {
|
| 70 |
|
|
|
| 71 |
|
|
WRITE(pio, PIO_PPUDR, mask);
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
// Configure pin
|
| 75 |
|
|
WRITE(pio, PIO_ASR, mask);
|
| 76 |
|
|
WRITE(pio, PIO_PDR, mask);
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
//------------------------------------------------------------------------------
|
| 80 |
|
|
/// Configures one or more pin(s) of a PIO controller as being controlled by
|
| 81 |
|
|
/// peripheral A. Optionally, the corresponding internal pull-up(s) can be
|
| 82 |
|
|
/// enabled.
|
| 83 |
|
|
/// \param pio Pointer to a PIO controller.
|
| 84 |
|
|
/// \param mask Bitmask of one or more pin(s) to configure.
|
| 85 |
|
|
/// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
|
| 86 |
|
|
/// configured.
|
| 87 |
|
|
//------------------------------------------------------------------------------
|
| 88 |
|
|
static void PIO_SetPeripheralB(AT91S_PIO *pio,
|
| 89 |
|
|
unsigned int mask,
|
| 90 |
|
|
unsigned char enablePullUp)
|
| 91 |
|
|
{
|
| 92 |
|
|
// Disable interrupts on the pin(s)
|
| 93 |
|
|
WRITE(pio, PIO_IDR, mask);
|
| 94 |
|
|
|
| 95 |
|
|
// Enable the pull-up(s) if necessary
|
| 96 |
|
|
if (enablePullUp) {
|
| 97 |
|
|
|
| 98 |
|
|
WRITE(pio, PIO_PPUER, mask);
|
| 99 |
|
|
}
|
| 100 |
|
|
else {
|
| 101 |
|
|
|
| 102 |
|
|
WRITE(pio, PIO_PPUDR, mask);
|
| 103 |
|
|
}
|
| 104 |
|
|
|
| 105 |
|
|
// Configure pin
|
| 106 |
|
|
WRITE(pio, PIO_BSR, mask);
|
| 107 |
|
|
WRITE(pio, PIO_PDR, mask);
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
//------------------------------------------------------------------------------
|
| 111 |
|
|
/// Configures one or more pin(s) or a PIO controller as inputs. Optionally,
|
| 112 |
|
|
/// the corresponding internal pull-up(s) and glitch filter(s) can be
|
| 113 |
|
|
/// enabled.
|
| 114 |
|
|
/// \param pio Pointer to a PIO controller.
|
| 115 |
|
|
/// \param mask Bitmask indicating which pin(s) to configure as input(s).
|
| 116 |
|
|
/// \param enablePullUp Indicates if the internal pull-up(s) must be enabled.
|
| 117 |
|
|
/// \param enableFilter Indicates if the glitch filter(s) must be enabled.
|
| 118 |
|
|
//------------------------------------------------------------------------------
|
| 119 |
|
|
static void PIO_SetInput(AT91S_PIO *pio,
|
| 120 |
|
|
unsigned int mask,
|
| 121 |
|
|
unsigned char enablePullUp,
|
| 122 |
|
|
unsigned char enableFilter)
|
| 123 |
|
|
{
|
| 124 |
|
|
// Disable interrupts
|
| 125 |
|
|
WRITE(pio, PIO_IDR, mask);
|
| 126 |
|
|
|
| 127 |
|
|
// Enable pull-up(s) if necessary
|
| 128 |
|
|
if (enablePullUp) {
|
| 129 |
|
|
|
| 130 |
|
|
WRITE(pio, PIO_PPUER, mask);
|
| 131 |
|
|
}
|
| 132 |
|
|
else {
|
| 133 |
|
|
|
| 134 |
|
|
WRITE(pio, PIO_PPUDR, mask);
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
// Enable filter(s) if necessary
|
| 138 |
|
|
if (enableFilter) {
|
| 139 |
|
|
|
| 140 |
|
|
WRITE(pio, PIO_IFER, mask);
|
| 141 |
|
|
}
|
| 142 |
|
|
else {
|
| 143 |
|
|
|
| 144 |
|
|
WRITE(pio, PIO_IFDR, mask);
|
| 145 |
|
|
}
|
| 146 |
|
|
|
| 147 |
|
|
// Configure pin as input
|
| 148 |
|
|
WRITE(pio, PIO_ODR, mask);
|
| 149 |
|
|
WRITE(pio, PIO_PER, mask);
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
//------------------------------------------------------------------------------
|
| 153 |
|
|
/// Configures one or more pin(s) of a PIO controller as outputs, with the
|
| 154 |
|
|
/// given default value. Optionally, the multi-drive feature can be enabled
|
| 155 |
|
|
/// on the pin(s).
|
| 156 |
|
|
/// \param pio Pointer to a PIO controller.
|
| 157 |
|
|
/// \param mask Bitmask indicating which pin(s) to configure.
|
| 158 |
|
|
/// \param defaultValue Default level on the pin(s).
|
| 159 |
|
|
/// \param enableMultiDrive Indicates if the pin(s) shall be configured as
|
| 160 |
|
|
/// open-drain.
|
| 161 |
|
|
/// \param enablePullUp Indicates if the pin shall have its pull-up activated.
|
| 162 |
|
|
//------------------------------------------------------------------------------
|
| 163 |
|
|
static void PIO_SetOutput(AT91S_PIO *pio,
|
| 164 |
|
|
unsigned int mask,
|
| 165 |
|
|
unsigned char defaultValue,
|
| 166 |
|
|
unsigned char enableMultiDrive,
|
| 167 |
|
|
unsigned char enablePullUp)
|
| 168 |
|
|
{
|
| 169 |
|
|
// Disable interrupts
|
| 170 |
|
|
WRITE(pio, PIO_IDR, mask);
|
| 171 |
|
|
|
| 172 |
|
|
// Enable pull-up(s) if necessary
|
| 173 |
|
|
if (enablePullUp) {
|
| 174 |
|
|
|
| 175 |
|
|
WRITE(pio, PIO_PPUER, mask);
|
| 176 |
|
|
}
|
| 177 |
|
|
else {
|
| 178 |
|
|
|
| 179 |
|
|
WRITE(pio, PIO_PPUDR, mask);
|
| 180 |
|
|
}
|
| 181 |
|
|
|
| 182 |
|
|
// Enable multi-drive if necessary
|
| 183 |
|
|
if (enableMultiDrive) {
|
| 184 |
|
|
|
| 185 |
|
|
WRITE(pio, PIO_MDER, mask);
|
| 186 |
|
|
}
|
| 187 |
|
|
else {
|
| 188 |
|
|
|
| 189 |
|
|
WRITE(pio, PIO_MDDR, mask);
|
| 190 |
|
|
}
|
| 191 |
|
|
|
| 192 |
|
|
// Set default value
|
| 193 |
|
|
if (defaultValue) {
|
| 194 |
|
|
|
| 195 |
|
|
WRITE(pio, PIO_SODR, mask);
|
| 196 |
|
|
}
|
| 197 |
|
|
else {
|
| 198 |
|
|
|
| 199 |
|
|
WRITE(pio, PIO_CODR, mask);
|
| 200 |
|
|
}
|
| 201 |
|
|
|
| 202 |
|
|
// Configure pin(s) as output(s)
|
| 203 |
|
|
WRITE(pio, PIO_OER, mask);
|
| 204 |
|
|
WRITE(pio, PIO_PER, mask);
|
| 205 |
|
|
}
|
| 206 |
|
|
|
| 207 |
|
|
//------------------------------------------------------------------------------
|
| 208 |
|
|
// Exported functions
|
| 209 |
|
|
//------------------------------------------------------------------------------
|
| 210 |
|
|
//------------------------------------------------------------------------------
|
| 211 |
|
|
/// Configures a list of Pin instances, which can either hold a single pin or a
|
| 212 |
|
|
/// group of pins, depending on the mask value; all pins are configured by this
|
| 213 |
|
|
/// function.
|
| 214 |
|
|
/// Returns 1 if the configuration has been performed successfully; otherwise 0.
|
| 215 |
|
|
/// \param list Pointer to a list of Pin instances.
|
| 216 |
|
|
/// \param size Size of the Pin list (see <PIO_LISTSIZE>).
|
| 217 |
|
|
//------------------------------------------------------------------------------
|
| 218 |
|
|
unsigned char PIO_Configure(const Pin *list, unsigned int size)
|
| 219 |
|
|
{
|
| 220 |
|
|
// Configure pins
|
| 221 |
|
|
while (size > 0) {
|
| 222 |
|
|
|
| 223 |
|
|
switch (list->type) {
|
| 224 |
|
|
|
| 225 |
|
|
case PIO_PERIPH_A:
|
| 226 |
|
|
PIO_SetPeripheralA(list->pio,
|
| 227 |
|
|
list->mask,
|
| 228 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
| 229 |
|
|
break;
|
| 230 |
|
|
|
| 231 |
|
|
case PIO_PERIPH_B:
|
| 232 |
|
|
PIO_SetPeripheralB(list->pio,
|
| 233 |
|
|
list->mask,
|
| 234 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
| 235 |
|
|
break;
|
| 236 |
|
|
|
| 237 |
|
|
case PIO_INPUT:
|
| 238 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << list->id;
|
| 239 |
|
|
PIO_SetInput(list->pio,
|
| 240 |
|
|
list->mask,
|
| 241 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0,
|
| 242 |
|
|
(list->attribute & PIO_DEGLITCH)? 1 : 0);
|
| 243 |
|
|
break;
|
| 244 |
|
|
|
| 245 |
|
|
case PIO_OUTPUT_0:
|
| 246 |
|
|
case PIO_OUTPUT_1:
|
| 247 |
|
|
PIO_SetOutput(list->pio,
|
| 248 |
|
|
list->mask,
|
| 249 |
|
|
(list->type == PIO_OUTPUT_1),
|
| 250 |
|
|
(list->attribute & PIO_OPENDRAIN) ? 1 : 0,
|
| 251 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
| 252 |
|
|
break;
|
| 253 |
|
|
|
| 254 |
|
|
default: return 0;
|
| 255 |
|
|
}
|
| 256 |
|
|
|
| 257 |
|
|
list++;
|
| 258 |
|
|
size--;
|
| 259 |
|
|
}
|
| 260 |
|
|
|
| 261 |
|
|
return 1;
|
| 262 |
|
|
}
|
| 263 |
|
|
|
| 264 |
|
|
//------------------------------------------------------------------------------
|
| 265 |
|
|
/// Sets a high output level on one or more pin(s) (if configured as output(s)).
|
| 266 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
| 267 |
|
|
//------------------------------------------------------------------------------
|
| 268 |
|
|
void PIO_Set(const Pin *pin)
|
| 269 |
|
|
{
|
| 270 |
|
|
WRITE(pin->pio, PIO_SODR, pin->mask);
|
| 271 |
|
|
}
|
| 272 |
|
|
|
| 273 |
|
|
//------------------------------------------------------------------------------
|
| 274 |
|
|
/// Sets a low output level on one or more pin(s) (if configured as output(s)).
|
| 275 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
| 276 |
|
|
//------------------------------------------------------------------------------
|
| 277 |
|
|
void PIO_Clear(const Pin *pin)
|
| 278 |
|
|
{
|
| 279 |
|
|
WRITE(pin->pio, PIO_CODR, pin->mask);
|
| 280 |
|
|
}
|
| 281 |
|
|
|
| 282 |
|
|
//------------------------------------------------------------------------------
|
| 283 |
|
|
/// Returns 1 if one or more PIO of the given Pin instance currently have a high
|
| 284 |
|
|
/// level; otherwise returns 0.
|
| 285 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
| 286 |
|
|
//------------------------------------------------------------------------------
|
| 287 |
|
|
unsigned char PIO_Get(const Pin *pin)
|
| 288 |
|
|
{
|
| 289 |
|
|
unsigned int reg;
|
| 290 |
|
|
if ((pin->type == PIO_OUTPUT_0) || (pin->type == PIO_OUTPUT_1)) {
|
| 291 |
|
|
|
| 292 |
|
|
reg = READ(pin->pio, PIO_ODSR);
|
| 293 |
|
|
}
|
| 294 |
|
|
else {
|
| 295 |
|
|
|
| 296 |
|
|
reg = READ(pin->pio, PIO_PDSR);
|
| 297 |
|
|
}
|
| 298 |
|
|
|
| 299 |
|
|
if ((reg & pin->mask) == 0) {
|
| 300 |
|
|
|
| 301 |
|
|
return 0;
|
| 302 |
|
|
}
|
| 303 |
|
|
else {
|
| 304 |
|
|
|
| 305 |
|
|
return 1;
|
| 306 |
|
|
}
|
| 307 |
|
|
}
|
| 308 |
|
|
|
| 309 |
|
|
|
| 310 |
|
|
//------------------------------------------------------------------------------
|
| 311 |
|
|
/// Returns 1 if one or more PIO of the given Pin data to be driven on the I/O line
|
| 312 |
|
|
/// level; otherwise returns 0.
|
| 313 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
| 314 |
|
|
//------------------------------------------------------------------------------
|
| 315 |
|
|
unsigned char PIO_GetOutputDataStatus(const Pin *pin)
|
| 316 |
|
|
{
|
| 317 |
|
|
if ((READ(pin->pio, PIO_ODSR) & pin->mask) == 0) {
|
| 318 |
|
|
|
| 319 |
|
|
return 0;
|
| 320 |
|
|
}
|
| 321 |
|
|
else {
|
| 322 |
|
|
|
| 323 |
|
|
return 1;
|
| 324 |
|
|
}
|
| 325 |
|
|
}
|
| 326 |
|
|
|
| 327 |
|
|
//------------------------------------------------------------------------------
|
| 328 |
|
|
/// Returns the value of ISR for the PIO controller of the pin.
|
| 329 |
|
|
/// Reading this register acknoledges all the ITs.
|
| 330 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
| 331 |
|
|
//------------------------------------------------------------------------------
|
| 332 |
|
|
unsigned int PIO_GetISR(const Pin *pin)
|
| 333 |
|
|
{
|
| 334 |
|
|
return (READ(pin->pio, PIO_ISR));
|
| 335 |
|
|
}
|
| 336 |
|
|
|