Andy K / Lib17_Core
Committer:
AjK
Date:
Mon Apr 11 16:16:23 2011 +0000
Revision:
5:a3b5b8fc77e3
Parent:
4:3418d67a1969
Child:
6:40a18fb62f1f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:127224866798 1 /****************************************************************************
AjK 0:127224866798 2 * Product: LIB17 Open Source Library
AjK 0:127224866798 3 *
AjK 0:127224866798 4 * Steller Technologies Limited
AjK 0:127224866798 5 * ----------------------------
AjK 0:127224866798 6 *
AjK 0:127224866798 7 * Copyright (C) 2002-2011 Steller Technologies Limited. All rights reserved.
AjK 0:127224866798 8 *
AjK 0:127224866798 9 * This software may be distributed and modified under the terms of the GNU
AjK 0:127224866798 10 * General Public License version 2 (GPL) as published by the Free Software
AjK 0:127224866798 11 * Foundation and appearing in the file GPL.TXT included in the packaging of
AjK 0:127224866798 12 * this file. Please note that GPL Section 2[b] requires that all works based
AjK 0:127224866798 13 * on this software must also be made publicly available under the terms of
AjK 0:127224866798 14 * the GPL ("Copyleft").
AjK 0:127224866798 15 *
AjK 0:127224866798 16 * Alternatively, this software may be distributed and modified under the
AjK 0:127224866798 17 * terms of Steller Technologies Limited commercial licenses, which expressly
AjK 0:127224866798 18 * supersede the GPL and are specifically designed for licensees interested in
AjK 0:127224866798 19 * retaining the proprietary status of their code.
AjK 0:127224866798 20 *
AjK 1:2f99edb5545c 21 * $Id:$
AjK 1:2f99edb5545c 22 *
AjK 0:127224866798 23 ***************************************************************************/
AjK 0:127224866798 24
AjK 1:2f99edb5545c 25 /**
AjK 1:2f99edb5545c 26 * @defgroup API The Lib17 Core API
AjK 1:2f99edb5545c 27 * @defgroup Lib17_DIO Lib17_DIO functions
AjK 1:2f99edb5545c 28 */
AjK 1:2f99edb5545c 29
AjK 0:127224866798 30 #ifndef AJK_Lib17_DIO_H
AjK 0:127224866798 31 #define AJK_Lib17_DIO_H
AjK 0:127224866798 32
AjK 0:127224866798 33 #include "Lib17_Mbed.h"
AjK 0:127224866798 34 #include "Lib17_IOmacros.h"
AjK 0:127224866798 35
AjK 0:127224866798 36 namespace AjK {
AjK 0:127224866798 37
AjK 1:2f99edb5545c 38 /** Lib17_DIO - Adds pin input/output objects.
AjK 1:2f99edb5545c 39 *
AjK 1:2f99edb5545c 40 * The Mbed library supplies the DigitalIn and DigitalOut objects to allow you
AjK 1:2f99edb5545c 41 * to specify ins and outs.
AjK 1:2f99edb5545c 42 *
AjK 1:2f99edb5545c 43 * Lib17_DIO allows library objects to implement pins without the requirement
AjK 1:2f99edb5545c 44 * to link against the Mbed library. This increase portability when using
AjK 1:2f99edb5545c 45 * alternate compilers (such as the Code Red GCC C++ compiler for LPCXpresso).
AjK 3:0e24c9d4ff2c 46 *
AjK 5:a3b5b8fc77e3 47 * @see http://cornflakes.wikidot.com/lib17:core
AjK 3:0e24c9d4ff2c 48 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 49 */
AjK 0:127224866798 50 class Lib17_DIO {
AjK 0:127224866798 51 public:
AjK 0:127224866798 52 enum Direction {
AjK 0:127224866798 53 Out = 0
AjK 0:127224866798 54 , In
AjK 0:127224866798 55 };
AjK 0:127224866798 56
AjK 0:127224866798 57 protected:
AjK 0:127224866798 58 PinName pin;
AjK 0:127224866798 59 uint32_t mask;
AjK 0:127224866798 60 uint32_t fiodir;
AjK 0:127224866798 61 uint32_t fiomask;
AjK 0:127224866798 62 uint32_t fiopin;
AjK 0:127224866798 63 uint32_t fioset;
AjK 0:127224866798 64 uint32_t fioclr;
AjK 0:127224866798 65
AjK 0:127224866798 66 inline void setpin(PinName p) { pin = p; }
AjK 0:127224866798 67 inline void setmask(PinName p) { mask = (uint32_t)(1UL << ((uint32_t)p & 0x1F)); }
AjK 0:127224866798 68 inline void setDir(PinName p) { fiodir = (uint32_t)(p & ~0x1F) + 0x00; }
AjK 0:127224866798 69 inline void setMask(PinName p) { fiomask = (uint32_t)(p & ~0x1F) + 0x10; }
AjK 0:127224866798 70 inline void setPin(PinName p) { fiopin = (uint32_t)(p & ~0x1F) + 0x14; }
AjK 0:127224866798 71 inline void setSet(PinName p) { fioset = (uint32_t)(p & ~0x1F) + 0x18; }
AjK 0:127224866798 72 inline void setClr(PinName p) { fioclr = (uint32_t)(p & ~0x1F) + 0x1C; }
AjK 0:127224866798 73
AjK 0:127224866798 74 inline void pinUp() { *((volatile uint32_t *)fioset) = mask; }
AjK 0:127224866798 75 inline void pinDn() { *((volatile uint32_t *)fioclr) = mask; }
AjK 0:127224866798 76 inline uint32_t pinIs() { return *((volatile uint32_t *)(fiopin)) & mask; }
AjK 0:127224866798 77
AjK 0:127224866798 78 public:
AjK 0:127224866798 79
AjK 2:b77d327026af 80 /** Constructor
AjK 4:3418d67a1969 81 * @ingroup Lib17_DIO
AjK 2:b77d327026af 82 */
AjK 0:127224866798 83 Lib17_DIO(PinName p, Direction d = Out, PinMode m = PullDown) { init(p, d, m); };
AjK 0:127224866798 84
AjK 2:b77d327026af 85 /** write
AjK 1:2f99edb5545c 86 *
AjK 1:2f99edb5545c 87 * Writes a value to the pin.
AjK 1:2f99edb5545c 88 *
AjK 1:2f99edb5545c 89 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 90 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 91 * @param i Zero makes the pin 0v, non-zero makes the pin 1.
AjK 1:2f99edb5545c 92 */
AjK 0:127224866798 93 void write(int i) { if (i!=0) { pinUp(); } else { pinDn(); } }
AjK 1:2f99edb5545c 94
AjK 2:b77d327026af 95 /** read
AjK 1:2f99edb5545c 96 *
AjK 1:2f99edb5545c 97 * Reads the value on the pin.
AjK 1:2f99edb5545c 98 *
AjK 1:2f99edb5545c 99 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 100 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 101 * @return int 0v returns zero, otherwise returns 1.
AjK 1:2f99edb5545c 102 */
AjK 0:127224866798 103 int read(void) { return pinIs() ? 1 : 0; };
AjK 1:2f99edb5545c 104
AjK 2:b77d327026af 105 /** output
AjK 1:2f99edb5545c 106 *
AjK 1:2f99edb5545c 107 * Setup the pin to be an output.
AjK 1:2f99edb5545c 108 *
AjK 1:2f99edb5545c 109 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 110 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 111 * @ingroup API
AjK 1:2f99edb5545c 112 * @return int 0v returns zero, otherwise returns 1.
AjK 1:2f99edb5545c 113 */
AjK 0:127224866798 114 void output(void) { *((volatile uint32_t *)fiodir) |= mask; }
AjK 1:2f99edb5545c 115
AjK 2:b77d327026af 116 /** input
AjK 1:2f99edb5545c 117 *
AjK 1:2f99edb5545c 118 * Setup the pin to be an input.
AjK 1:2f99edb5545c 119 *
AjK 1:2f99edb5545c 120 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 121 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 122 * @return int 0v returns zero, otherwise returns 1.
AjK 1:2f99edb5545c 123 */
AjK 0:127224866798 124 void input(void) { *((volatile uint32_t *)fiodir) &= ~mask; }
AjK 0:127224866798 125
AjK 2:b77d327026af 126 /** getPin
AjK 1:2f99edb5545c 127 *
AjK 1:2f99edb5545c 128 * Get the PinName this object is operating on.
AjK 1:2f99edb5545c 129 *
AjK 1:2f99edb5545c 130 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 131 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 132 * @return int 0v returns zero, otherwise returns 1.
AjK 1:2f99edb5545c 133 */
AjK 0:127224866798 134 PinName getPin(void) { return pin; }
AjK 1:2f99edb5545c 135
AjK 2:b77d327026af 136 /** getDirection
AjK 1:2f99edb5545c 137 *
AjK 1:2f99edb5545c 138 * Get the operational direction this pin is setup for.
AjK 1:2f99edb5545c 139 *
AjK 1:2f99edb5545c 140 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 141 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 142 * @return int 0v returns zero, otherwise returns 1.
AjK 1:2f99edb5545c 143 */
AjK 0:127224866798 144 int getDirection(void) { return *((volatile uint32_t *)fiomask) & mask ? 1 : 0; }
AjK 0:127224866798 145
AjK 2:b77d327026af 146 /** operator int()
AjK 1:2f99edb5545c 147 *
AjK 1:2f99edb5545c 148 * Reads the value on the pin.
AjK 1:2f99edb5545c 149 *
AjK 1:2f99edb5545c 150 * @see read
AjK 1:2f99edb5545c 151 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 152 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 153 * @return int 0v returns zero, otherwise returns 1.
AjK 1:2f99edb5545c 154 */
AjK 0:127224866798 155 operator int() { return read(); }
AjK 1:2f99edb5545c 156
AjK 2:b77d327026af 157 /** operator=
AjK 1:2f99edb5545c 158 *
AjK 1:2f99edb5545c 159 * Writes a value to the pin.
AjK 1:2f99edb5545c 160 *
AjK 1:2f99edb5545c 161 * @see write
AjK 1:2f99edb5545c 162 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 163 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 164 */
AjK 0:127224866798 165 Lib17_DIO& operator= (int value) { write(value); return *this; }
AjK 1:2f99edb5545c 166
AjK 2:b77d327026af 167 /** operator=
AjK 1:2f99edb5545c 168 *
AjK 1:2f99edb5545c 169 * Writes a value to the pin.
AjK 1:2f99edb5545c 170 *
AjK 1:2f99edb5545c 171 * @see write
AjK 1:2f99edb5545c 172 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 173 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 174 */
AjK 0:127224866798 175 Lib17_DIO& operator= (Lib17_DIO& rhs) { write(rhs.read()); return *this; }
AjK 0:127224866798 176
AjK 2:b77d327026af 177 /** getMask
AjK 1:2f99edb5545c 178 *
AjK 1:2f99edb5545c 179 * Get the mask value for this pin.
AjK 1:2f99edb5545c 180 *
AjK 1:2f99edb5545c 181 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 182 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 183 * @return uint32_t The mask value used by this pin.
AjK 1:2f99edb5545c 184 */
AjK 0:127224866798 185 uint32_t getMask(void) { return mask; }
AjK 1:2f99edb5545c 186
AjK 2:b77d327026af 187 /** getFiodir
AjK 1:2f99edb5545c 188 *
AjK 1:2f99edb5545c 189 * Get the FIODIR register for the port the pin is on.
AjK 1:2f99edb5545c 190 *
AjK 1:2f99edb5545c 191 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 192 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 193 * @return uint32_t The register value.
AjK 1:2f99edb5545c 194 */
AjK 0:127224866798 195 uint32_t getFiodir(void) { return fiodir; }
AjK 1:2f99edb5545c 196
AjK 2:b77d327026af 197 /** getFiomask
AjK 1:2f99edb5545c 198 *
AjK 1:2f99edb5545c 199 * Get the FIOMASK register for the port the pin is on.
AjK 1:2f99edb5545c 200 *
AjK 1:2f99edb5545c 201 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 202 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 203 * @return uint32_t The register value.
AjK 1:2f99edb5545c 204 */
AjK 0:127224866798 205 uint32_t getFiomask(void) { return fiomask; }
AjK 1:2f99edb5545c 206
AjK 2:b77d327026af 207 /** getFiopin
AjK 1:2f99edb5545c 208 *
AjK 1:2f99edb5545c 209 * Get the FIOPIN register for the port the pin is on.
AjK 1:2f99edb5545c 210 *
AjK 1:2f99edb5545c 211 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 212 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 213 * @return uint32_t The register value.
AjK 1:2f99edb5545c 214 */
AjK 0:127224866798 215 uint32_t getFiopin(void) { return fiopin; }
AjK 1:2f99edb5545c 216
AjK 2:b77d327026af 217 /** getFioset
AjK 1:2f99edb5545c 218 *
AjK 1:2f99edb5545c 219 * Get the FIOSET register for the port the pin is on.
AjK 1:2f99edb5545c 220 *
AjK 1:2f99edb5545c 221 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 222 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 223 * @return uint32_t The register value.
AjK 1:2f99edb5545c 224 */
AjK 0:127224866798 225 uint32_t getFioset(void) { return fioset; }
AjK 1:2f99edb5545c 226
AjK 2:b77d327026af 227 /** getFioclr
AjK 1:2f99edb5545c 228 *
AjK 1:2f99edb5545c 229 * Get the FIOCLR register for the port the pin is on.
AjK 1:2f99edb5545c 230 *
AjK 1:2f99edb5545c 231 * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio
AjK 1:2f99edb5545c 232 * @ingroup Lib17_DIO
AjK 1:2f99edb5545c 233 * @return uint32_t The register value.
AjK 1:2f99edb5545c 234 */
AjK 0:127224866798 235 uint32_t getFioclr(void) { return fioclr; }
AjK 0:127224866798 236
AjK 0:127224866798 237
AjK 0:127224866798 238 protected:
AjK 0:127224866798 239 void init(PinName p, Direction d, PinMode m)
AjK 0:127224866798 240 {
AjK 0:127224866798 241 // We rely on the fact that by default the LPC1768
AjK 0:127224866798 242 // sets all pins to be GPIO. The user will change
AjK 0:127224866798 243 // that if they need to. So we don't bother trying
AjK 0:127224866798 244 // to setup PINSELx
AjK 0:127224866798 245
AjK 0:127224866798 246 // psel(); // Not used, see above.
AjK 0:127224866798 247
AjK 0:127224866798 248 setpin(p);
AjK 0:127224866798 249 setmask(p);
AjK 0:127224866798 250 setDir(p);
AjK 0:127224866798 251 setMask(p);
AjK 0:127224866798 252 setPin(p);
AjK 0:127224866798 253 setSet(p);
AjK 0:127224866798 254 setClr(p);
AjK 0:127224866798 255
AjK 0:127224866798 256 if (d == Out) output();
AjK 0:127224866798 257 else mode( m );
AjK 0:127224866798 258 }
AjK 0:127224866798 259
AjK 0:127224866798 260 protected:
AjK 0:127224866798 261 void psel(void)
AjK 0:127224866798 262 {
AjK 0:127224866798 263 uint32_t ppsel, pumask;
AjK 0:127224866798 264
AjK 0:127224866798 265 if (pin >= P0_0 && pin <= P0_15) ppsel = (uint32_t)(&LPC_PINCON->PINSEL0);
AjK 0:127224866798 266 else if (pin >= P0_16 && pin <= P0_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL1);
AjK 0:127224866798 267 else if (pin >= P1_0 && pin <= P1_15) ppsel = (uint32_t)(&LPC_PINCON->PINSEL2);
AjK 0:127224866798 268 else if (pin >= P1_16 && pin <= P1_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL3);
AjK 0:127224866798 269 else if (pin >= P2_0 && pin <= P2_15) ppsel = (uint32_t)(&LPC_PINCON->PINSEL4);
AjK 0:127224866798 270 else if (pin >= P3_16 && pin <= P3_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL7);
AjK 0:127224866798 271 else if (pin >= P4_16 && pin <= P4_31) ppsel = (uint32_t)(&LPC_PINCON->PINSEL9);
AjK 0:127224866798 272 else return;
AjK 0:127224866798 273
AjK 0:127224866798 274 pumask = ~(3UL << ((pin & 0x1F)>>1));
AjK 0:127224866798 275 *((volatile uint32_t *)ppsel) &= pumask;
AjK 0:127224866798 276 }
AjK 0:127224866798 277
AjK 0:127224866798 278 public:
AjK 0:127224866798 279 void mode(PinMode m)
AjK 0:127224866798 280 {
AjK 0:127224866798 281 uint32_t ppmod, pumask;
AjK 0:127224866798 282
AjK 0:127224866798 283 if (m == OpenDrain) {
AjK 0:127224866798 284 openDrain(1);
AjK 0:127224866798 285 }
AjK 0:127224866798 286 else {
AjK 0:127224866798 287 if (pin >= P0_0 && pin <= P0_15) {
AjK 0:127224866798 288 ppmod = (uint32_t)(&LPC_PINCON->PINMODE0);
AjK 0:127224866798 289 pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) );
AjK 0:127224866798 290 }
AjK 0:127224866798 291 else if (pin >= P0_16 && pin <= P0_31) {
AjK 0:127224866798 292 ppmod = (uint32_t)(&LPC_PINCON->PINMODE1);
AjK 0:127224866798 293 pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) );
AjK 0:127224866798 294 }
AjK 0:127224866798 295 else if (pin >= P1_0 && pin <= P1_15) {
AjK 0:127224866798 296 ppmod = (uint32_t)(&LPC_PINCON->PINMODE2);
AjK 0:127224866798 297 pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) );
AjK 0:127224866798 298 }
AjK 0:127224866798 299 else if (pin >= P1_16 && pin <= P1_31) {
AjK 0:127224866798 300 ppmod = (uint32_t)(&LPC_PINCON->PINMODE3);
AjK 0:127224866798 301 pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) );
AjK 0:127224866798 302 }
AjK 0:127224866798 303 else if (pin >= P2_0 && pin <= P2_15) {
AjK 0:127224866798 304 ppmod = (uint32_t)(&LPC_PINCON->PINMODE4);
AjK 0:127224866798 305 pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) );
AjK 0:127224866798 306 }
AjK 0:127224866798 307 else if (pin >= P3_16 && pin <= P3_31) {
AjK 0:127224866798 308 ppmod = (uint32_t)(&LPC_PINCON->PINMODE7);
AjK 0:127224866798 309 pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) );
AjK 0:127224866798 310 }
AjK 0:127224866798 311 else if (pin >= P4_16 && pin <= P4_31) {
AjK 0:127224866798 312 ppmod = (uint32_t)(&LPC_PINCON->PINMODE9);
AjK 0:127224866798 313 pumask = ((m & 0x3) << ( ((pin & 0x1F)-16)*2) );
AjK 0:127224866798 314 }
AjK 0:127224866798 315 else return;
AjK 0:127224866798 316
AjK 0:127224866798 317 *((volatile uint32_t *)ppmod) |= pumask;
AjK 0:127224866798 318 }
AjK 0:127224866798 319 }
AjK 0:127224866798 320
AjK 0:127224866798 321 public:
AjK 0:127224866798 322 void openDrain(int i = 1)
AjK 0:127224866798 323 {
AjK 0:127224866798 324 if (pin >= P0_0 && pin <= P0_31) { if (i) LPC_PINCON->PINMODE_OD0 |= mask; else LPC_PINCON->PINMODE_OD0 &= ~mask; }
AjK 0:127224866798 325 else if (pin >= P1_0 && pin <= P1_31) { if (i) LPC_PINCON->PINMODE_OD1 |= mask; else LPC_PINCON->PINMODE_OD1 &= ~mask; }
AjK 0:127224866798 326 else if (pin >= P2_0 && pin <= P2_31) { if (i) LPC_PINCON->PINMODE_OD2 |= mask; else LPC_PINCON->PINMODE_OD2 &= ~mask; }
AjK 0:127224866798 327 else if (pin >= P3_16 && pin <= P3_31) { if (i) LPC_PINCON->PINMODE_OD3 |= mask; else LPC_PINCON->PINMODE_OD3 &= ~mask; }
AjK 0:127224866798 328 else if (pin >= P4_16 && pin <= P4_31) { if (i) LPC_PINCON->PINMODE_OD4 |= mask; else LPC_PINCON->PINMODE_OD4 &= ~mask; }
AjK 0:127224866798 329 }
AjK 0:127224866798 330
AjK 0:127224866798 331 };
AjK 0:127224866798 332
AjK 0:127224866798 333 }; /* namespace AjK ends. */
AjK 0:127224866798 334
AjK 0:127224866798 335 using namespace AjK;
AjK 0:127224866798 336
AjK 0:127224866798 337 #endif /* AJK_Lib17_DIO_H */