Andy K / Lib17_Core
Committer:
AjK
Date:
Mon Apr 11 15:46:11 2011 +0000
Revision:
2:b77d327026af
Parent:
1:2f99edb5545c
Child:
3:0e24c9d4ff2c

        

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