Tripple Controller for the TLE5206 H Bridge motor controller
inc/SimpleTLE5206Output.h@5:bfc5c5cc161e, 2011-07-05 (annotated)
- Committer:
- AjK
- Date:
- Tue Jul 05 16:08:38 2011 +0000
- Revision:
- 5:bfc5c5cc161e
- Parent:
- 0:e2433ca2ce59
0.7 Beta See ChangeLog.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:e2433ca2ce59 | 1 | /* |
AjK | 0:e2433ca2ce59 | 2 | Copyright (c) 2011 Andy Kirkham |
AjK | 0:e2433ca2ce59 | 3 | |
AjK | 0:e2433ca2ce59 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
AjK | 0:e2433ca2ce59 | 5 | of this software and associated documentation files (the "Software"), to deal |
AjK | 0:e2433ca2ce59 | 6 | in the Software without restriction, including without limitation the rights |
AjK | 0:e2433ca2ce59 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AjK | 0:e2433ca2ce59 | 8 | copies of the Software, and to permit persons to whom the Software is |
AjK | 0:e2433ca2ce59 | 9 | furnished to do so, subject to the following conditions: |
AjK | 0:e2433ca2ce59 | 10 | |
AjK | 0:e2433ca2ce59 | 11 | The above copyright notice and this permission notice shall be included in |
AjK | 0:e2433ca2ce59 | 12 | all copies or substantial portions of the Software. |
AjK | 0:e2433ca2ce59 | 13 | |
AjK | 0:e2433ca2ce59 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AjK | 0:e2433ca2ce59 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AjK | 0:e2433ca2ce59 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AjK | 0:e2433ca2ce59 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AjK | 0:e2433ca2ce59 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AjK | 0:e2433ca2ce59 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
AjK | 0:e2433ca2ce59 | 20 | THE SOFTWARE. |
AjK | 0:e2433ca2ce59 | 21 | */ |
AjK | 0:e2433ca2ce59 | 22 | |
AjK | 0:e2433ca2ce59 | 23 | #ifndef AJK_SIMPLETLE5206OUTPUT_H |
AjK | 0:e2433ca2ce59 | 24 | #define AJK_SIMPLETLE5206OUTPUT_H |
AjK | 0:e2433ca2ce59 | 25 | |
AjK | 0:e2433ca2ce59 | 26 | #ifndef __ARMCC_VERSION |
AjK | 0:e2433ca2ce59 | 27 | #include "SimpleTLE5206Mbed.h" |
AjK | 0:e2433ca2ce59 | 28 | #define error(...) |
AjK | 0:e2433ca2ce59 | 29 | #else |
AjK | 0:e2433ca2ce59 | 30 | #include "mbed.h" |
AjK | 0:e2433ca2ce59 | 31 | #endif |
AjK | 0:e2433ca2ce59 | 32 | |
AjK | 0:e2433ca2ce59 | 33 | |
AjK | 0:e2433ca2ce59 | 34 | namespace AjK { |
AjK | 0:e2433ca2ce59 | 35 | |
AjK | 0:e2433ca2ce59 | 36 | /** SimpleTLE5206Output - Adds pin output objects. |
AjK | 0:e2433ca2ce59 | 37 | * |
AjK | 0:e2433ca2ce59 | 38 | * The Mbed library supplies the DigitalIn and DigitalOut objects to allow you |
AjK | 0:e2433ca2ce59 | 39 | * to specify ins and outs. |
AjK | 0:e2433ca2ce59 | 40 | * |
AjK | 0:e2433ca2ce59 | 41 | * SimpleTLE5206Output allows library objects to implement pins without the requirement |
AjK | 0:e2433ca2ce59 | 42 | * to link against the Mbed library. This increase portability when using |
AjK | 0:e2433ca2ce59 | 43 | * alternate compilers (such as the Code Red GCC C++ compiler for LPCXpresso). |
AjK | 0:e2433ca2ce59 | 44 | * |
AjK | 0:e2433ca2ce59 | 45 | * Additionally, this class allows the pin to be configured as a GPIO output or swapped |
AjK | 0:e2433ca2ce59 | 46 | * to PWM output mode. |
AjK | 0:e2433ca2ce59 | 47 | * |
AjK | 0:e2433ca2ce59 | 48 | * Note, this class can only be used for pins p21 through p26 as these are used for PWM pins. |
AjK | 0:e2433ca2ce59 | 49 | * Trying to set this to any other pin will result in a fatal error() call. |
AjK | 0:e2433ca2ce59 | 50 | * |
AjK | 0:e2433ca2ce59 | 51 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 52 | */ |
AjK | 0:e2433ca2ce59 | 53 | class SimpleTLE5206Output { |
AjK | 0:e2433ca2ce59 | 54 | public: |
AjK | 0:e2433ca2ce59 | 55 | enum Direction { |
AjK | 0:e2433ca2ce59 | 56 | Out = 0 |
AjK | 0:e2433ca2ce59 | 57 | , In |
AjK | 0:e2433ca2ce59 | 58 | }; |
AjK | 0:e2433ca2ce59 | 59 | |
AjK | 0:e2433ca2ce59 | 60 | enum PinType { |
AjK | 0:e2433ca2ce59 | 61 | IsGPIO = 0 |
AjK | 0:e2433ca2ce59 | 62 | , IsPWM |
AjK | 0:e2433ca2ce59 | 63 | }; |
AjK | 0:e2433ca2ce59 | 64 | |
AjK | 0:e2433ca2ce59 | 65 | protected: |
AjK | 0:e2433ca2ce59 | 66 | PinName pin; |
AjK | 0:e2433ca2ce59 | 67 | uint32_t mask; |
AjK | 0:e2433ca2ce59 | 68 | uint32_t fiodir; |
AjK | 0:e2433ca2ce59 | 69 | uint32_t fiomask; |
AjK | 0:e2433ca2ce59 | 70 | uint32_t fiopin; |
AjK | 0:e2433ca2ce59 | 71 | uint32_t fioset; |
AjK | 0:e2433ca2ce59 | 72 | uint32_t fioclr; |
AjK | 0:e2433ca2ce59 | 73 | |
AjK | 0:e2433ca2ce59 | 74 | PinType pin_type; |
AjK | 0:e2433ca2ce59 | 75 | |
AjK | 0:e2433ca2ce59 | 76 | inline void setpin(PinName p) { pin = p; } |
AjK | 0:e2433ca2ce59 | 77 | inline void setmask(PinName p) { mask = (uint32_t)(1UL << ((uint32_t)p & 0x1F)); } |
AjK | 0:e2433ca2ce59 | 78 | inline void setDir(PinName p) { fiodir = (uint32_t)(p & ~0x1F) + 0x00; } |
AjK | 0:e2433ca2ce59 | 79 | inline void setMask(PinName p) { fiomask = (uint32_t)(p & ~0x1F) + 0x10; } |
AjK | 0:e2433ca2ce59 | 80 | inline void setPin(PinName p) { fiopin = (uint32_t)(p & ~0x1F) + 0x14; } |
AjK | 0:e2433ca2ce59 | 81 | inline void setSet(PinName p) { fioset = (uint32_t)(p & ~0x1F) + 0x18; } |
AjK | 0:e2433ca2ce59 | 82 | inline void setClr(PinName p) { fioclr = (uint32_t)(p & ~0x1F) + 0x1C; } |
AjK | 0:e2433ca2ce59 | 83 | |
AjK | 0:e2433ca2ce59 | 84 | inline void pinUp() { *((volatile uint32_t *)fioset) = mask; } |
AjK | 0:e2433ca2ce59 | 85 | inline void pinDn() { *((volatile uint32_t *)fioclr) = mask; } |
AjK | 0:e2433ca2ce59 | 86 | inline uint32_t pinIs() { return *((volatile uint32_t *)(fiopin)) & mask; } |
AjK | 0:e2433ca2ce59 | 87 | |
AjK | 0:e2433ca2ce59 | 88 | public: |
AjK | 0:e2433ca2ce59 | 89 | |
AjK | 0:e2433ca2ce59 | 90 | /** Constructor |
AjK | 0:e2433ca2ce59 | 91 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 92 | */ |
AjK | 0:e2433ca2ce59 | 93 | SimpleTLE5206Output(PinName p, Direction d = Out, PinMode m = PullDown) { |
AjK | 0:e2433ca2ce59 | 94 | |
AjK | 0:e2433ca2ce59 | 95 | if (p == p21 || p == p22 || p == p23 || p == p24 || p == p25 || p == p26 || p == LED1 || p == LED2 || p == LED3 || p == LED4) { |
AjK | 0:e2433ca2ce59 | 96 | init(p, d, m); |
AjK | 0:e2433ca2ce59 | 97 | } |
AjK | 0:e2433ca2ce59 | 98 | else { |
AjK | 0:e2433ca2ce59 | 99 | error("Invalid pin supplied.\n"); |
AjK | 0:e2433ca2ce59 | 100 | } |
AjK | 0:e2433ca2ce59 | 101 | }; |
AjK | 0:e2433ca2ce59 | 102 | |
AjK | 0:e2433ca2ce59 | 103 | /** write |
AjK | 0:e2433ca2ce59 | 104 | * |
AjK | 0:e2433ca2ce59 | 105 | * Writes a value to the pin. |
AjK | 0:e2433ca2ce59 | 106 | * |
AjK | 0:e2433ca2ce59 | 107 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 108 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 109 | * @param i Zero makes the pin 0v, non-zero makes the pin 1. |
AjK | 0:e2433ca2ce59 | 110 | */ |
AjK | 0:e2433ca2ce59 | 111 | void write(int i) { if (i!=0) { pinUp(); } else { pinDn(); } } |
AjK | 0:e2433ca2ce59 | 112 | |
AjK | 0:e2433ca2ce59 | 113 | /** read |
AjK | 0:e2433ca2ce59 | 114 | * |
AjK | 0:e2433ca2ce59 | 115 | * Reads the value on the pin. |
AjK | 0:e2433ca2ce59 | 116 | * |
AjK | 0:e2433ca2ce59 | 117 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 118 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 119 | * @return int 0v returns zero, otherwise returns 1. |
AjK | 0:e2433ca2ce59 | 120 | */ |
AjK | 0:e2433ca2ce59 | 121 | int read(void) { return pinIs() ? 1 : 0; }; |
AjK | 0:e2433ca2ce59 | 122 | |
AjK | 0:e2433ca2ce59 | 123 | /** output |
AjK | 0:e2433ca2ce59 | 124 | * |
AjK | 0:e2433ca2ce59 | 125 | * Setup the pin to be an output. |
AjK | 0:e2433ca2ce59 | 126 | * |
AjK | 0:e2433ca2ce59 | 127 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 128 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 129 | * @ingroup API |
AjK | 0:e2433ca2ce59 | 130 | * @return int 0v returns zero, otherwise returns 1. |
AjK | 0:e2433ca2ce59 | 131 | */ |
AjK | 0:e2433ca2ce59 | 132 | void output(void) { *((volatile uint32_t *)fiodir) |= mask; } |
AjK | 0:e2433ca2ce59 | 133 | |
AjK | 0:e2433ca2ce59 | 134 | /** input |
AjK | 0:e2433ca2ce59 | 135 | * |
AjK | 0:e2433ca2ce59 | 136 | * Setup the pin to be an input. |
AjK | 0:e2433ca2ce59 | 137 | * |
AjK | 0:e2433ca2ce59 | 138 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 139 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 140 | * @return int 0v returns zero, otherwise returns 1. |
AjK | 0:e2433ca2ce59 | 141 | */ |
AjK | 0:e2433ca2ce59 | 142 | void input(void) { *((volatile uint32_t *)fiodir) &= ~mask; } |
AjK | 0:e2433ca2ce59 | 143 | |
AjK | 0:e2433ca2ce59 | 144 | /** getPin |
AjK | 0:e2433ca2ce59 | 145 | * |
AjK | 0:e2433ca2ce59 | 146 | * Get the PinName this object is operating on. |
AjK | 0:e2433ca2ce59 | 147 | * |
AjK | 0:e2433ca2ce59 | 148 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 149 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 150 | * @return int 0v returns zero, otherwise returns 1. |
AjK | 0:e2433ca2ce59 | 151 | */ |
AjK | 0:e2433ca2ce59 | 152 | PinName getPin(void) { return pin; } |
AjK | 0:e2433ca2ce59 | 153 | |
AjK | 0:e2433ca2ce59 | 154 | /** getDirection |
AjK | 0:e2433ca2ce59 | 155 | * |
AjK | 0:e2433ca2ce59 | 156 | * Get the operational direction this pin is setup for. |
AjK | 0:e2433ca2ce59 | 157 | * |
AjK | 0:e2433ca2ce59 | 158 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 159 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 160 | * @return int 0v returns zero, otherwise returns 1. |
AjK | 0:e2433ca2ce59 | 161 | */ |
AjK | 0:e2433ca2ce59 | 162 | int getDirection(void) { return *((volatile uint32_t *)fiomask) & mask ? 1 : 0; } |
AjK | 0:e2433ca2ce59 | 163 | |
AjK | 0:e2433ca2ce59 | 164 | /** operator int() |
AjK | 0:e2433ca2ce59 | 165 | * |
AjK | 0:e2433ca2ce59 | 166 | * Reads the value on the pin. |
AjK | 0:e2433ca2ce59 | 167 | * |
AjK | 0:e2433ca2ce59 | 168 | * @see read |
AjK | 0:e2433ca2ce59 | 169 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 170 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 171 | * @return int 0v returns zero, otherwise returns 1. |
AjK | 0:e2433ca2ce59 | 172 | */ |
AjK | 0:e2433ca2ce59 | 173 | operator int() { return read(); } |
AjK | 0:e2433ca2ce59 | 174 | |
AjK | 0:e2433ca2ce59 | 175 | /** operator= |
AjK | 0:e2433ca2ce59 | 176 | * |
AjK | 0:e2433ca2ce59 | 177 | * Writes a value to the pin. |
AjK | 0:e2433ca2ce59 | 178 | * |
AjK | 0:e2433ca2ce59 | 179 | * @see write |
AjK | 0:e2433ca2ce59 | 180 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 181 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 182 | */ |
AjK | 0:e2433ca2ce59 | 183 | SimpleTLE5206Output& operator= (int value) { write(value); return *this; } |
AjK | 0:e2433ca2ce59 | 184 | |
AjK | 0:e2433ca2ce59 | 185 | /** operator= |
AjK | 0:e2433ca2ce59 | 186 | * |
AjK | 0:e2433ca2ce59 | 187 | * Writes a value to the pin. |
AjK | 0:e2433ca2ce59 | 188 | * |
AjK | 0:e2433ca2ce59 | 189 | * @see write |
AjK | 0:e2433ca2ce59 | 190 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 191 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 192 | */ |
AjK | 0:e2433ca2ce59 | 193 | SimpleTLE5206Output& operator= (SimpleTLE5206Output& rhs) { write(rhs.read()); return *this; } |
AjK | 0:e2433ca2ce59 | 194 | |
AjK | 0:e2433ca2ce59 | 195 | /** getMask |
AjK | 0:e2433ca2ce59 | 196 | * |
AjK | 0:e2433ca2ce59 | 197 | * Get the mask value for this pin. |
AjK | 0:e2433ca2ce59 | 198 | * |
AjK | 0:e2433ca2ce59 | 199 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 200 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 201 | * @return uint32_t The mask value used by this pin. |
AjK | 0:e2433ca2ce59 | 202 | */ |
AjK | 0:e2433ca2ce59 | 203 | uint32_t getMask(void) { return mask; } |
AjK | 0:e2433ca2ce59 | 204 | |
AjK | 0:e2433ca2ce59 | 205 | /** getFiodir |
AjK | 0:e2433ca2ce59 | 206 | * |
AjK | 0:e2433ca2ce59 | 207 | * Get the FIODIR register for the port the pin is on. |
AjK | 0:e2433ca2ce59 | 208 | * |
AjK | 0:e2433ca2ce59 | 209 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 210 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 211 | * @return uint32_t The register value. |
AjK | 0:e2433ca2ce59 | 212 | */ |
AjK | 0:e2433ca2ce59 | 213 | uint32_t getFiodir(void) { return fiodir; } |
AjK | 0:e2433ca2ce59 | 214 | |
AjK | 0:e2433ca2ce59 | 215 | /** getFiomask |
AjK | 0:e2433ca2ce59 | 216 | * |
AjK | 0:e2433ca2ce59 | 217 | * Get the FIOMASK register for the port the pin is on. |
AjK | 0:e2433ca2ce59 | 218 | * |
AjK | 0:e2433ca2ce59 | 219 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 220 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 221 | * @return uint32_t The register value. |
AjK | 0:e2433ca2ce59 | 222 | */ |
AjK | 0:e2433ca2ce59 | 223 | uint32_t getFiomask(void) { return fiomask; } |
AjK | 0:e2433ca2ce59 | 224 | |
AjK | 0:e2433ca2ce59 | 225 | /** getFiopin |
AjK | 0:e2433ca2ce59 | 226 | * |
AjK | 0:e2433ca2ce59 | 227 | * Get the FIOPIN register for the port the pin is on. |
AjK | 0:e2433ca2ce59 | 228 | * |
AjK | 0:e2433ca2ce59 | 229 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 230 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 231 | * @return uint32_t The register value. |
AjK | 0:e2433ca2ce59 | 232 | */ |
AjK | 0:e2433ca2ce59 | 233 | uint32_t getFiopin(void) { return fiopin; } |
AjK | 0:e2433ca2ce59 | 234 | |
AjK | 0:e2433ca2ce59 | 235 | /** getFioset |
AjK | 0:e2433ca2ce59 | 236 | * |
AjK | 0:e2433ca2ce59 | 237 | * Get the FIOSET register for the port the pin is on. |
AjK | 0:e2433ca2ce59 | 238 | * |
AjK | 0:e2433ca2ce59 | 239 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 240 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 241 | * @return uint32_t The register value. |
AjK | 0:e2433ca2ce59 | 242 | */ |
AjK | 0:e2433ca2ce59 | 243 | uint32_t getFioset(void) { return fioset; } |
AjK | 0:e2433ca2ce59 | 244 | |
AjK | 0:e2433ca2ce59 | 245 | /** getFioclr |
AjK | 0:e2433ca2ce59 | 246 | * |
AjK | 0:e2433ca2ce59 | 247 | * Get the FIOCLR register for the port the pin is on. |
AjK | 0:e2433ca2ce59 | 248 | * |
AjK | 0:e2433ca2ce59 | 249 | * @see http://cornflakes.wikidot.com/lib17:core:lib17-dio |
AjK | 0:e2433ca2ce59 | 250 | * @ingroup SimpleTLE5206Output |
AjK | 0:e2433ca2ce59 | 251 | * @return uint32_t The register value. |
AjK | 0:e2433ca2ce59 | 252 | */ |
AjK | 0:e2433ca2ce59 | 253 | uint32_t getFioclr(void) { return fioclr; } |
AjK | 0:e2433ca2ce59 | 254 | |
AjK | 0:e2433ca2ce59 | 255 | |
AjK | 0:e2433ca2ce59 | 256 | protected: |
AjK | 0:e2433ca2ce59 | 257 | void init(PinName p, Direction d, PinMode m) |
AjK | 0:e2433ca2ce59 | 258 | { |
AjK | 0:e2433ca2ce59 | 259 | // We rely on the fact that by default the LPC1768 |
AjK | 0:e2433ca2ce59 | 260 | // sets all pins to be GPIO. The user will change |
AjK | 0:e2433ca2ce59 | 261 | // that if they need to. So we don't bother trying |
AjK | 0:e2433ca2ce59 | 262 | // to setup PINSELx |
AjK | 0:e2433ca2ce59 | 263 | |
AjK | 0:e2433ca2ce59 | 264 | // psel(); // Not used, see above. |
AjK | 0:e2433ca2ce59 | 265 | |
AjK | 0:e2433ca2ce59 | 266 | setpin(p); |
AjK | 0:e2433ca2ce59 | 267 | setmask(p); |
AjK | 0:e2433ca2ce59 | 268 | setDir(p); |
AjK | 0:e2433ca2ce59 | 269 | setMask(p); |
AjK | 0:e2433ca2ce59 | 270 | setPin(p); |
AjK | 0:e2433ca2ce59 | 271 | setSet(p); |
AjK | 0:e2433ca2ce59 | 272 | setClr(p); |
AjK | 0:e2433ca2ce59 | 273 | |
AjK | 0:e2433ca2ce59 | 274 | if (d == Out) output(); |
AjK | 0:e2433ca2ce59 | 275 | else mode( m ); |
AjK | 0:e2433ca2ce59 | 276 | |
AjK | 0:e2433ca2ce59 | 277 | pin_type = IsGPIO; // GPIO. |
AjK | 0:e2433ca2ce59 | 278 | } |
AjK | 0:e2433ca2ce59 | 279 | |
AjK | 0:e2433ca2ce59 | 280 | public: |
AjK | 0:e2433ca2ce59 | 281 | void as_gpio(void) |
AjK | 0:e2433ca2ce59 | 282 | { |
AjK | 0:e2433ca2ce59 | 283 | pin_type = IsGPIO; |
AjK | 0:e2433ca2ce59 | 284 | |
AjK | 0:e2433ca2ce59 | 285 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 286 | case P2_0: |
AjK | 0:e2433ca2ce59 | 287 | LPC_PINCON->PINSEL4 &= ~(3UL << 0); // Mbed p26 P2.0 clr bits |
AjK | 0:e2433ca2ce59 | 288 | return; |
AjK | 0:e2433ca2ce59 | 289 | case P1_18: // Mbed LED1 |
AjK | 0:e2433ca2ce59 | 290 | LPC_PINCON->PINSEL3 &= ~(3UL << 4); // Mbed LED2 P1.18 clr bits |
AjK | 0:e2433ca2ce59 | 291 | return; |
AjK | 0:e2433ca2ce59 | 292 | } |
AjK | 0:e2433ca2ce59 | 293 | |
AjK | 0:e2433ca2ce59 | 294 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 295 | case P2_1: |
AjK | 0:e2433ca2ce59 | 296 | LPC_PINCON->PINSEL4 &= ~(3UL << 2); // Mbed p25 P2.1 clr bits |
AjK | 0:e2433ca2ce59 | 297 | return; |
AjK | 0:e2433ca2ce59 | 298 | case P1_20: // Mbed LED2 |
AjK | 0:e2433ca2ce59 | 299 | LPC_PINCON->PINSEL3 &= ~(3UL << 8); // Mbed LED2 P1.20 clr bits |
AjK | 0:e2433ca2ce59 | 300 | return; |
AjK | 0:e2433ca2ce59 | 301 | } |
AjK | 0:e2433ca2ce59 | 302 | |
AjK | 0:e2433ca2ce59 | 303 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 304 | case P2_2: |
AjK | 0:e2433ca2ce59 | 305 | LPC_PINCON->PINSEL4 &= ~(3UL << 4); // Mbed p24 P2.2 clr bits |
AjK | 0:e2433ca2ce59 | 306 | return; |
AjK | 0:e2433ca2ce59 | 307 | case P1_21: // Mbed LED3 |
AjK | 0:e2433ca2ce59 | 308 | LPC_PINCON->PINSEL3 &= ~(3UL << 10); // Mbed LED3 P1.21 clr bits |
AjK | 0:e2433ca2ce59 | 309 | return; |
AjK | 0:e2433ca2ce59 | 310 | } |
AjK | 0:e2433ca2ce59 | 311 | |
AjK | 0:e2433ca2ce59 | 312 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 313 | case P2_3: |
AjK | 0:e2433ca2ce59 | 314 | LPC_PINCON->PINSEL4 &= ~(3UL << 6); // Mbed p23 P2.3 clr bits |
AjK | 0:e2433ca2ce59 | 315 | return; |
AjK | 0:e2433ca2ce59 | 316 | case P1_23: // Mbed LED4 |
AjK | 0:e2433ca2ce59 | 317 | LPC_PINCON->PINSEL3 &= ~(3UL << 14); // Mbed LED4 P1.23 clr bits |
AjK | 0:e2433ca2ce59 | 318 | return; |
AjK | 0:e2433ca2ce59 | 319 | } |
AjK | 0:e2433ca2ce59 | 320 | |
AjK | 0:e2433ca2ce59 | 321 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 322 | case P2_4: // Mbed p22 |
AjK | 0:e2433ca2ce59 | 323 | LPC_PINCON->PINSEL4 &= ~(3UL << 8); // Mbed p22 P2.4 clr bits |
AjK | 0:e2433ca2ce59 | 324 | return; |
AjK | 0:e2433ca2ce59 | 325 | case P1_24: |
AjK | 0:e2433ca2ce59 | 326 | LPC_PINCON->PINSEL3 &= ~(3UL << 16); // P1.24 clr bits |
AjK | 0:e2433ca2ce59 | 327 | return; |
AjK | 0:e2433ca2ce59 | 328 | } |
AjK | 0:e2433ca2ce59 | 329 | |
AjK | 0:e2433ca2ce59 | 330 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 331 | case P2_5: // Mbed p21 |
AjK | 0:e2433ca2ce59 | 332 | LPC_PINCON->PINSEL4 &= ~(3UL << 10); // Mbed p21 P2.5 clr bits |
AjK | 0:e2433ca2ce59 | 333 | return; |
AjK | 0:e2433ca2ce59 | 334 | case P1_26: |
AjK | 0:e2433ca2ce59 | 335 | LPC_PINCON->PINSEL3 &= ~(3UL << 20); // P1.26 clr bits |
AjK | 0:e2433ca2ce59 | 336 | return; |
AjK | 0:e2433ca2ce59 | 337 | } |
AjK | 0:e2433ca2ce59 | 338 | } |
AjK | 0:e2433ca2ce59 | 339 | |
AjK | 0:e2433ca2ce59 | 340 | void as_pwm(void) |
AjK | 0:e2433ca2ce59 | 341 | { |
AjK | 0:e2433ca2ce59 | 342 | pin_type = IsPWM; |
AjK | 0:e2433ca2ce59 | 343 | |
AjK | 0:e2433ca2ce59 | 344 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 345 | case P2_0: |
AjK | 0:e2433ca2ce59 | 346 | LPC_PINCON->PINSEL4 &= ~(3UL << 0); // Mbed p26 P2.0 clr bits |
AjK | 0:e2433ca2ce59 | 347 | LPC_PINCON->PINSEL4 |= (1UL << 0); // Mbed p26 P2.0 set bits |
AjK | 0:e2433ca2ce59 | 348 | LPC_PWM1->PCR |= (1UL << 9); |
AjK | 0:e2433ca2ce59 | 349 | return; |
AjK | 0:e2433ca2ce59 | 350 | case P1_18: // Mbed LED1 |
AjK | 0:e2433ca2ce59 | 351 | LPC_PINCON->PINSEL3 &= ~(3UL << 4); // Mbed LED2 P1.18 clr bits |
AjK | 0:e2433ca2ce59 | 352 | LPC_PINCON->PINSEL3 |= (2UL << 4); // Mbed LED2 P1.18 set bits |
AjK | 0:e2433ca2ce59 | 353 | LPC_PWM1->PCR |= (1UL << 9); |
AjK | 0:e2433ca2ce59 | 354 | return; |
AjK | 0:e2433ca2ce59 | 355 | } |
AjK | 0:e2433ca2ce59 | 356 | |
AjK | 0:e2433ca2ce59 | 357 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 358 | case P2_1: |
AjK | 0:e2433ca2ce59 | 359 | LPC_PINCON->PINSEL4 &= ~(3UL << 2); // Mbed p25 P2.1 clr bits |
AjK | 0:e2433ca2ce59 | 360 | LPC_PINCON->PINSEL4 |= (1UL << 2); // Mbed p25 P2.1 set bits |
AjK | 0:e2433ca2ce59 | 361 | LPC_PWM1->PCR |= (1UL << 10); |
AjK | 0:e2433ca2ce59 | 362 | return; |
AjK | 0:e2433ca2ce59 | 363 | case P1_20: // Mbed LED2 |
AjK | 0:e2433ca2ce59 | 364 | LPC_PINCON->PINSEL3 &= ~(3UL << 8); // Mbed LED2 P1.20 clr bits |
AjK | 0:e2433ca2ce59 | 365 | LPC_PINCON->PINSEL3 |= (2UL << 8); // Mbed LED2 P1.20 set bits |
AjK | 0:e2433ca2ce59 | 366 | LPC_PWM1->PCR |= (1UL << 10); |
AjK | 0:e2433ca2ce59 | 367 | return; |
AjK | 0:e2433ca2ce59 | 368 | } |
AjK | 0:e2433ca2ce59 | 369 | |
AjK | 0:e2433ca2ce59 | 370 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 371 | case P2_2: |
AjK | 0:e2433ca2ce59 | 372 | LPC_PINCON->PINSEL4 &= ~(3UL << 4); // Mbed p24 P2.2 clr bits |
AjK | 0:e2433ca2ce59 | 373 | LPC_PINCON->PINSEL4 |= (1UL << 4); // Mbed p24 P2.2 set bits |
AjK | 0:e2433ca2ce59 | 374 | LPC_PWM1->PCR |= (1UL << 11); |
AjK | 0:e2433ca2ce59 | 375 | return; |
AjK | 0:e2433ca2ce59 | 376 | case P1_21: // Mbed LED3 |
AjK | 0:e2433ca2ce59 | 377 | LPC_PINCON->PINSEL3 &= ~(3UL << 10); // Mbed LED3 P1.21 clr bits |
AjK | 0:e2433ca2ce59 | 378 | LPC_PINCON->PINSEL3 |= (2UL << 10); // Mbed LED3 P1.21 set bits |
AjK | 0:e2433ca2ce59 | 379 | LPC_PWM1->PCR |= (1UL << 11); |
AjK | 0:e2433ca2ce59 | 380 | return; |
AjK | 0:e2433ca2ce59 | 381 | } |
AjK | 0:e2433ca2ce59 | 382 | |
AjK | 0:e2433ca2ce59 | 383 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 384 | case P2_3: |
AjK | 0:e2433ca2ce59 | 385 | LPC_PINCON->PINSEL4 &= ~(3UL << 6); // Mbed p23 P2.3 clr bits |
AjK | 0:e2433ca2ce59 | 386 | LPC_PINCON->PINSEL4 |= (1UL << 6); // Mbed p23 P2.3 set bits |
AjK | 0:e2433ca2ce59 | 387 | LPC_PWM1->PCR |= (1UL << 12); |
AjK | 0:e2433ca2ce59 | 388 | return; |
AjK | 0:e2433ca2ce59 | 389 | case P1_23: // Mbed LED4 |
AjK | 0:e2433ca2ce59 | 390 | LPC_PINCON->PINSEL3 &= ~(3UL << 14); // Mbed LED4 P1.23 clr bits |
AjK | 0:e2433ca2ce59 | 391 | LPC_PINCON->PINSEL3 |= (2UL << 14); // Mbed LED4 P1.23 set bits |
AjK | 0:e2433ca2ce59 | 392 | LPC_PWM1->PCR |= (1UL << 12); |
AjK | 0:e2433ca2ce59 | 393 | return; |
AjK | 0:e2433ca2ce59 | 394 | } |
AjK | 0:e2433ca2ce59 | 395 | |
AjK | 0:e2433ca2ce59 | 396 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 397 | case P2_4: // Mbed p22 |
AjK | 0:e2433ca2ce59 | 398 | LPC_PINCON->PINSEL4 &= ~(3UL << 8); // Mbed p22 P2.4 clr bits |
AjK | 0:e2433ca2ce59 | 399 | LPC_PINCON->PINSEL4 |= (1UL << 8); // Mbed p22 P2.4 set bits |
AjK | 0:e2433ca2ce59 | 400 | LPC_PWM1->PCR |= (1UL << 13); |
AjK | 0:e2433ca2ce59 | 401 | return; |
AjK | 0:e2433ca2ce59 | 402 | case P1_24: |
AjK | 0:e2433ca2ce59 | 403 | LPC_PINCON->PINSEL3 &= ~(3UL << 16); // P1.24 clr bits |
AjK | 0:e2433ca2ce59 | 404 | LPC_PINCON->PINSEL3 |= (2UL << 16); // P1.24 set bits |
AjK | 0:e2433ca2ce59 | 405 | LPC_PWM1->PCR |= (1UL << 13); |
AjK | 0:e2433ca2ce59 | 406 | return; |
AjK | 0:e2433ca2ce59 | 407 | } |
AjK | 0:e2433ca2ce59 | 408 | |
AjK | 0:e2433ca2ce59 | 409 | switch(pin) { |
AjK | 0:e2433ca2ce59 | 410 | case P2_5: // Mbed p21 |
AjK | 0:e2433ca2ce59 | 411 | LPC_PINCON->PINSEL4 &= ~(3UL << 10); // Mbed p21 P2.5 clr bits |
AjK | 0:e2433ca2ce59 | 412 | LPC_PINCON->PINSEL4 |= (1UL << 10); // Mbed p21 P2.5 set bits |
AjK | 0:e2433ca2ce59 | 413 | LPC_PWM1->PCR |= (1UL << 14); |
AjK | 0:e2433ca2ce59 | 414 | return; |
AjK | 0:e2433ca2ce59 | 415 | case P1_26: |
AjK | 0:e2433ca2ce59 | 416 | LPC_PINCON->PINSEL3 &= ~(3UL << 20); // P1.26 clr bits |
AjK | 0:e2433ca2ce59 | 417 | LPC_PINCON->PINSEL3 |= (2UL << 20); // P1.26 set bits |
AjK | 0:e2433ca2ce59 | 418 | LPC_PWM1->PCR |= (1UL << 14); |
AjK | 0:e2433ca2ce59 | 419 | return; |
AjK | 0:e2433ca2ce59 | 420 | } |
AjK | 0:e2433ca2ce59 | 421 | |
AjK | 0:e2433ca2ce59 | 422 | /* |
AjK | 0:e2433ca2ce59 | 423 | uint32_t ppsel, pumask; |
AjK | 0:e2433ca2ce59 | 424 | |
AjK | 0:e2433ca2ce59 | 425 | if (pin >= P2_0 && pin <= P2_5) { |
AjK | 0:e2433ca2ce59 | 426 | ppsel = (uint32_t)(&LPC_PINCON->PINSEL4); |
AjK | 0:e2433ca2ce59 | 427 | pumask = ~(3UL << ((pin & 0x1F)>>1)); |
AjK | 0:e2433ca2ce59 | 428 | *((volatile uint32_t *)ppsel) &= pumask; |
AjK | 0:e2433ca2ce59 | 429 | pumask = (1UL << ((pin & 0x1F)>>1)); |
AjK | 0:e2433ca2ce59 | 430 | *((volatile uint32_t *)ppsel) |= pumask; |
AjK | 0:e2433ca2ce59 | 431 | } |
AjK | 0:e2433ca2ce59 | 432 | else if (pin >= LED1 && pin <= LED4) { |
AjK | 0:e2433ca2ce59 | 433 | ppsel = (uint32_t)(&LPC_PINCON->PINSEL3); |
AjK | 0:e2433ca2ce59 | 434 | pumask = ~(3UL << ((pin & 0x1F)>>1)); |
AjK | 0:e2433ca2ce59 | 435 | *((volatile uint32_t *)ppsel) &= pumask; |
AjK | 0:e2433ca2ce59 | 436 | pumask = (2UL << ((pin & 0x1F)>>1)); |
AjK | 0:e2433ca2ce59 | 437 | *((volatile uint32_t *)ppsel) |= pumask; |
AjK | 0:e2433ca2ce59 | 438 | } |
AjK | 0:e2433ca2ce59 | 439 | else return; |
AjK | 0:e2433ca2ce59 | 440 | */ |
AjK | 0:e2433ca2ce59 | 441 | |
AjK | 0:e2433ca2ce59 | 442 | } |
AjK | 0:e2433ca2ce59 | 443 | |
AjK | 0:e2433ca2ce59 | 444 | PinType get_pin_type(void) { return pin_type; } |
AjK | 0:e2433ca2ce59 | 445 | |
AjK | 0:e2433ca2ce59 | 446 | void mode(PinMode m) |
AjK | 0:e2433ca2ce59 | 447 | { |
AjK | 0:e2433ca2ce59 | 448 | uint32_t ppmod, pumask; |
AjK | 0:e2433ca2ce59 | 449 | |
AjK | 0:e2433ca2ce59 | 450 | if (m == OpenDrain) { |
AjK | 0:e2433ca2ce59 | 451 | openDrain(1); |
AjK | 0:e2433ca2ce59 | 452 | } |
AjK | 0:e2433ca2ce59 | 453 | else { |
AjK | 0:e2433ca2ce59 | 454 | if (pin >= P2_0 && pin <= P2_5) { |
AjK | 0:e2433ca2ce59 | 455 | ppmod = (uint32_t)(&LPC_PINCON->PINMODE4); |
AjK | 0:e2433ca2ce59 | 456 | pumask = ((m & 0x3) << ( ((pin & 0x1F)-0)*2) ); |
AjK | 0:e2433ca2ce59 | 457 | } |
AjK | 0:e2433ca2ce59 | 458 | else return; |
AjK | 0:e2433ca2ce59 | 459 | |
AjK | 0:e2433ca2ce59 | 460 | *((volatile uint32_t *)ppmod) |= pumask; |
AjK | 0:e2433ca2ce59 | 461 | } |
AjK | 0:e2433ca2ce59 | 462 | } |
AjK | 0:e2433ca2ce59 | 463 | |
AjK | 0:e2433ca2ce59 | 464 | public: |
AjK | 0:e2433ca2ce59 | 465 | void openDrain(int i = 1) |
AjK | 0:e2433ca2ce59 | 466 | { |
AjK | 0:e2433ca2ce59 | 467 | if (pin >= P2_0 && pin <= P2_5) { if (i) LPC_PINCON->PINMODE_OD2 |= mask; else LPC_PINCON->PINMODE_OD2 &= ~mask; } |
AjK | 0:e2433ca2ce59 | 468 | } |
AjK | 0:e2433ca2ce59 | 469 | |
AjK | 0:e2433ca2ce59 | 470 | }; |
AjK | 0:e2433ca2ce59 | 471 | |
AjK | 0:e2433ca2ce59 | 472 | }; /* namespace AjK ends. */ |
AjK | 0:e2433ca2ce59 | 473 | |
AjK | 0:e2433ca2ce59 | 474 | using namespace AjK; |
AjK | 0:e2433ca2ce59 | 475 | |
AjK | 0:e2433ca2ce59 | 476 | #endif /* AJK_SIMPLETLE5206OUTPUT_H */ |