Andy K / Lib17_Core
Committer:
AjK
Date:
Mon Apr 11 15:53:36 2011 +0000
Revision:
3:0e24c9d4ff2c
Parent:
2:b77d327026af
Child:
4:3418d67a1969

        

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