Jean Mercier
/
jmAll
Command all jmCLIG modules from serial port
jmLPC17xx_gpio.c@0:9112e09912db, 2011-02-12 (annotated)
- Committer:
- jm
- Date:
- Sat Feb 12 16:50:25 2011 +0000
- Revision:
- 0:9112e09912db
jmAll Command Line Interface Module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jm | 0:9112e09912db | 1 | /************************************************************************* |
jm | 0:9112e09912db | 2 | * @file jmLPC17xx_gpio.c |
jm | 0:9112e09912db | 3 | * @brief Command Line Interface Module for LPC_GPIO |
jm | 0:9112e09912db | 4 | * |
jm | 0:9112e09912db | 5 | * @version 1.1 |
jm | 0:9112e09912db | 6 | * @date Jan 24, 2011 |
jm | 0:9112e09912db | 7 | */ |
jm | 0:9112e09912db | 8 | |
jm | 0:9112e09912db | 9 | #include "jmLPC17xx_gpio.h" |
jm | 0:9112e09912db | 10 | #include "LPC17xx.h" |
jm | 0:9112e09912db | 11 | #include "main.h" |
jm | 0:9112e09912db | 12 | #include "jmCommands.h" |
jm | 0:9112e09912db | 13 | #include "jmMessages.h" |
jm | 0:9112e09912db | 14 | #include "jmRingBuffer.h" |
jm | 0:9112e09912db | 15 | #include "stdio.h" |
jm | 0:9112e09912db | 16 | |
jm | 0:9112e09912db | 17 | |
jm | 0:9112e09912db | 18 | #define DIR 0 |
jm | 0:9112e09912db | 19 | #define PIN 1 |
jm | 0:9112e09912db | 20 | #define MASK 2 |
jm | 0:9112e09912db | 21 | #define CLR 3 |
jm | 0:9112e09912db | 22 | #define SET 4 |
jm | 0:9112e09912db | 23 | |
jm | 0:9112e09912db | 24 | #define Clear 0 |
jm | 0:9112e09912db | 25 | #define Set 1 |
jm | 0:9112e09912db | 26 | #define Toggle 2 |
jm | 0:9112e09912db | 27 | |
jm | 0:9112e09912db | 28 | /*********************************************************************** |
jm | 0:9112e09912db | 29 | * @brief Send port registers content |
jm | 0:9112e09912db | 30 | * @param[in] portNum (Port Number value)0..4 |
jm | 0:9112e09912db | 31 | * @return none |
jm | 0:9112e09912db | 32 | **********************************************************************/ |
jm | 0:9112e09912db | 33 | void PortInfo(unsigned int portNum){ |
jm | 0:9112e09912db | 34 | if(portNum <5){ |
jm | 0:9112e09912db | 35 | printf("Port %1d\n",portNum); |
jm | 0:9112e09912db | 36 | printf("FIODIR \t0x%08X\n",jmGPIO[portNum]->FIODIR); |
jm | 0:9112e09912db | 37 | printf("FIOMASK \t0x%08X\n",jmGPIO[portNum]->FIOMASK); |
jm | 0:9112e09912db | 38 | printf("FIOPIN \t0x%08X\n",jmGPIO[portNum]->FIOPIN); |
jm | 0:9112e09912db | 39 | printf("FIOSET \t0x%08X\n",jmGPIO[portNum]->FIOSET); |
jm | 0:9112e09912db | 40 | printf("FIOCLR \t0x%08X\n",jmGPIO[portNum]->FIOCLR); |
jm | 0:9112e09912db | 41 | printf("\n"); |
jm | 0:9112e09912db | 42 | } |
jm | 0:9112e09912db | 43 | } |
jm | 0:9112e09912db | 44 | |
jm | 0:9112e09912db | 45 | /*********************************************************************** |
jm | 0:9112e09912db | 46 | * @brief Send all ports registers content |
jm | 0:9112e09912db | 47 | * @param[in] none |
jm | 0:9112e09912db | 48 | * @return none |
jm | 0:9112e09912db | 49 | **********************************************************************/ |
jm | 0:9112e09912db | 50 | void PortsInfo(void){ |
jm | 0:9112e09912db | 51 | unsigned int portNum; |
jm | 0:9112e09912db | 52 | for(portNum=0;portNum<5;portNum++) PortInfo(portNum); |
jm | 0:9112e09912db | 53 | } |
jm | 0:9112e09912db | 54 | |
jm | 0:9112e09912db | 55 | /*********************************************************************** |
jm | 0:9112e09912db | 56 | * @brief iport Command Line Interface. |
jm | 0:9112e09912db | 57 | * Send port register content |
jm | 0:9112e09912db | 58 | * Command Line Format: iport (portNumber)0..4 |
jm | 0:9112e09912db | 59 | * @example iport 1 Will printout Port1 registers content |
jm | 0:9112e09912db | 60 | * @param[in] Extracted from command line ring buffer (PortNumber)0..4 |
jm | 0:9112e09912db | 61 | * @return none |
jm | 0:9112e09912db | 62 | **********************************************************************/ |
jm | 0:9112e09912db | 63 | void cli_PortInfo(void){ |
jm | 0:9112e09912db | 64 | unsigned int portNum; |
jm | 0:9112e09912db | 65 | |
jm | 0:9112e09912db | 66 | if(ExtractUInteger(pLine,&portNum,0,4)) |
jm | 0:9112e09912db | 67 | PortInfo(portNum); |
jm | 0:9112e09912db | 68 | else{ |
jm | 0:9112e09912db | 69 | if(Help)printf("iport (portNumber)0..4\n"); |
jm | 0:9112e09912db | 70 | // Ignore pending command line |
jm | 0:9112e09912db | 71 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 72 | } |
jm | 0:9112e09912db | 73 | } |
jm | 0:9112e09912db | 74 | |
jm | 0:9112e09912db | 75 | |
jm | 0:9112e09912db | 76 | /*********************************************************************** |
jm | 0:9112e09912db | 77 | * @brief Command line interface to read and send Port.Bit per bit position |
jm | 0:9112e09912db | 78 | * Command Line Format: bitRead (Port Number)0..4 (Bit position)0..31 |
jm | 0:9112e09912db | 79 | * ex: bitRead 1 18 read and send P1.18 input pin value |
jm | 0:9112e09912db | 80 | * pin must be previously set to input in GPIODIR and associated GPIOMASK bit= 0 |
jm | 0:9112e09912db | 81 | * @param[in] Extracted from command line (Port Number)0..4 (Bit position)0..31 |
jm | 0:9112e09912db | 82 | * @return none |
jm | 0:9112e09912db | 83 | **********************************************************************/ |
jm | 0:9112e09912db | 84 | void cli_BitRead(void){ |
jm | 0:9112e09912db | 85 | unsigned int portNumber, bitPosition, bitValue; |
jm | 0:9112e09912db | 86 | // Extract pin number from Command Line |
jm | 0:9112e09912db | 87 | if(ExtractUInteger(pLine,&portNumber,0,4)){ |
jm | 0:9112e09912db | 88 | // Extract bit position from Command Line |
jm | 0:9112e09912db | 89 | if(ExtractUInteger(pLine,&bitPosition,0,31)){ |
jm | 0:9112e09912db | 90 | // Extract state from Command Line |
jm | 0:9112e09912db | 91 | bitValue = 1<<bitPosition; |
jm | 0:9112e09912db | 92 | if(jmGPIO[portNumber]->FIOPIN & bitValue)bitValue =1; |
jm | 0:9112e09912db | 93 | printf("P%d.%d %d\n",portNumber,bitPosition,bitValue); |
jm | 0:9112e09912db | 94 | return; |
jm | 0:9112e09912db | 95 | } |
jm | 0:9112e09912db | 96 | } |
jm | 0:9112e09912db | 97 | if(Help)printf("bitRead (Port Number)0..4 (Bit position)0..31\n"); |
jm | 0:9112e09912db | 98 | // Ignore pending command line |
jm | 0:9112e09912db | 99 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 100 | } |
jm | 0:9112e09912db | 101 | |
jm | 0:9112e09912db | 102 | /** Read GPIO Registers |
jm | 0:9112e09912db | 103 | * @brief Command Line Interface to Read and Send GPIO register value |
jm | 0:9112e09912db | 104 | * @param[in] Extracted from command line GPPG0 (Port)0..4 (Register DIR/PIN/MASK)0..2 |
jm | 0:9112e09912db | 105 | * @returns Message: GPPG0 (Port)0..4 (Register DIR/PIN/MASK)0..2 (value)0..0xFFFFFFFF |
jm | 0:9112e09912db | 106 | */ |
jm | 0:9112e09912db | 107 | void cli_GPPG0(void){ |
jm | 0:9112e09912db | 108 | uint32_t value=0; |
jm | 0:9112e09912db | 109 | unsigned int port, reg; |
jm | 0:9112e09912db | 110 | if(ExtractUInteger(pLine,&port,0,4)){ // extract port id |
jm | 0:9112e09912db | 111 | if(ExtractUInteger(pLine,®,0,4)){ // extract register id |
jm | 0:9112e09912db | 112 | switch(reg){ |
jm | 0:9112e09912db | 113 | case DIR: value = jmGPIO[port]->FIODIR; break; |
jm | 0:9112e09912db | 114 | case PIN: value = jmGPIO[port]->FIOPIN; break; |
jm | 0:9112e09912db | 115 | case MASK: value = jmGPIO[port]->FIOMASK; break; |
jm | 0:9112e09912db | 116 | case CLR: value = jmGPIO[port]->FIOCLR; break; |
jm | 0:9112e09912db | 117 | case SET: value = jmGPIO[port]->FIOSET; break; |
jm | 0:9112e09912db | 118 | } |
jm | 0:9112e09912db | 119 | printf("GPPG0 %d %d 0x%08X\n",port,reg,value); |
jm | 0:9112e09912db | 120 | return; |
jm | 0:9112e09912db | 121 | } |
jm | 0:9112e09912db | 122 | } |
jm | 0:9112e09912db | 123 | if(Help)printf("GPPG0 (Port)0..4 (Register DIR/PIN/MASK/CLR/SET)0..4\n"); |
jm | 0:9112e09912db | 124 | // Ignore pending command line |
jm | 0:9112e09912db | 125 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 126 | } |
jm | 0:9112e09912db | 127 | |
jm | 0:9112e09912db | 128 | /*********************************************************************** |
jm | 0:9112e09912db | 129 | * @brief Command line interface to access GPIO register bits |
jm | 0:9112e09912db | 130 | * Use with care. Designed to be used with gui which locks non MBED DIP pins |
jm | 0:9112e09912db | 131 | * Command Line Format: gpioBits (Port id)0..4 (Register DIR/PIN/MASK/CLR/SET)0..4 (value)0..0xFFFFFFFF (clear/set/toggle)0..2\n |
jm | 0:9112e09912db | 132 | * @param[in] Extracted from command line (Port)0..4 (Register DIR/PIN/MASK)0..2 (value)0..0xFFFFFFFF |
jm | 0:9112e09912db | 133 | * @return Message: GPPG0 (Port)0..4 (Register DIR/PIN/MASK)0..2 (value)0..0xFFFFFFFF |
jm | 0:9112e09912db | 134 | **********************************************************************/ |
jm | 0:9112e09912db | 135 | void cli_gpioBits(void){ |
jm | 0:9112e09912db | 136 | unsigned int port, reg, value, action; |
jm | 0:9112e09912db | 137 | // Extract port id from Command Line |
jm | 0:9112e09912db | 138 | if(ExtractUInteger(pLine,&port,0,4)){ |
jm | 0:9112e09912db | 139 | // Extract register id from Command Line |
jm | 0:9112e09912db | 140 | if(ExtractUInteger(pLine,®,0,4)){ |
jm | 0:9112e09912db | 141 | // Extract value from Command Line |
jm | 0:9112e09912db | 142 | if(ExtractUInteger(pLine,&value,0,0xFFFFFFFF)){ |
jm | 0:9112e09912db | 143 | // Extract state from Command Line |
jm | 0:9112e09912db | 144 | if(ExtractUInteger(pLine,&action,0,2)){ |
jm | 0:9112e09912db | 145 | switch(reg){ |
jm | 0:9112e09912db | 146 | case DIR: |
jm | 0:9112e09912db | 147 | switch(action){ |
jm | 0:9112e09912db | 148 | case Clear: jmGPIO[port]->FIODIR &= ~value; |
jm | 0:9112e09912db | 149 | break; |
jm | 0:9112e09912db | 150 | case Set: jmGPIO[port]->FIODIR |= value; |
jm | 0:9112e09912db | 151 | break; |
jm | 0:9112e09912db | 152 | case Toggle: jmGPIO[port]->FIODIR ^= value; |
jm | 0:9112e09912db | 153 | break; |
jm | 0:9112e09912db | 154 | } |
jm | 0:9112e09912db | 155 | break; |
jm | 0:9112e09912db | 156 | case PIN: |
jm | 0:9112e09912db | 157 | switch(action){ |
jm | 0:9112e09912db | 158 | case Clear: jmGPIO[port]->FIOPIN &= ~value; |
jm | 0:9112e09912db | 159 | break; |
jm | 0:9112e09912db | 160 | case Set: jmGPIO[port]->FIOPIN |= value; |
jm | 0:9112e09912db | 161 | break; |
jm | 0:9112e09912db | 162 | case Toggle: jmGPIO[port]->FIOPIN ^= value; |
jm | 0:9112e09912db | 163 | break; |
jm | 0:9112e09912db | 164 | } |
jm | 0:9112e09912db | 165 | break; |
jm | 0:9112e09912db | 166 | case MASK: |
jm | 0:9112e09912db | 167 | switch(action){ |
jm | 0:9112e09912db | 168 | case Clear: jmGPIO[port]->FIOMASK &= ~value; |
jm | 0:9112e09912db | 169 | break; |
jm | 0:9112e09912db | 170 | case Set: jmGPIO[port]->FIOMASK |= value; |
jm | 0:9112e09912db | 171 | break; |
jm | 0:9112e09912db | 172 | case Toggle: jmGPIO[port]->FIOMASK ^= value; |
jm | 0:9112e09912db | 173 | break; |
jm | 0:9112e09912db | 174 | } |
jm | 0:9112e09912db | 175 | break; |
jm | 0:9112e09912db | 176 | case CLR: |
jm | 0:9112e09912db | 177 | switch(action){ |
jm | 0:9112e09912db | 178 | case Clear: jmGPIO[port]->FIOCLR &= ~value; |
jm | 0:9112e09912db | 179 | break; |
jm | 0:9112e09912db | 180 | case Set: jmGPIO[port]->FIOCLR |= value; |
jm | 0:9112e09912db | 181 | break; |
jm | 0:9112e09912db | 182 | case Toggle: jmGPIO[port]->FIOCLR ^= value; |
jm | 0:9112e09912db | 183 | break; |
jm | 0:9112e09912db | 184 | } |
jm | 0:9112e09912db | 185 | break; |
jm | 0:9112e09912db | 186 | case SET: |
jm | 0:9112e09912db | 187 | switch(action){ |
jm | 0:9112e09912db | 188 | case Clear: jmGPIO[port]->FIOSET &= ~value; |
jm | 0:9112e09912db | 189 | break; |
jm | 0:9112e09912db | 190 | case Set: jmGPIO[port]->FIOSET |= value; |
jm | 0:9112e09912db | 191 | break; |
jm | 0:9112e09912db | 192 | case Toggle: jmGPIO[port]->FIOSET ^= value; |
jm | 0:9112e09912db | 193 | break; |
jm | 0:9112e09912db | 194 | } |
jm | 0:9112e09912db | 195 | break; |
jm | 0:9112e09912db | 196 | |
jm | 0:9112e09912db | 197 | } |
jm | 0:9112e09912db | 198 | // gui feedback |
jm | 0:9112e09912db | 199 | switch(reg) |
jm | 0:9112e09912db | 200 | { |
jm | 0:9112e09912db | 201 | case DIR: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIODIR); |
jm | 0:9112e09912db | 202 | break; |
jm | 0:9112e09912db | 203 | case PIN: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOPIN); |
jm | 0:9112e09912db | 204 | break; |
jm | 0:9112e09912db | 205 | case MASK: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOMASK); |
jm | 0:9112e09912db | 206 | break; |
jm | 0:9112e09912db | 207 | case CLR: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOCLR); |
jm | 0:9112e09912db | 208 | break; |
jm | 0:9112e09912db | 209 | case SET: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOSET); |
jm | 0:9112e09912db | 210 | break; |
jm | 0:9112e09912db | 211 | } |
jm | 0:9112e09912db | 212 | if(Feedback)printf("GPIO %d %d 0x%08X %d\n",port,reg,value,action); |
jm | 0:9112e09912db | 213 | return; |
jm | 0:9112e09912db | 214 | } |
jm | 0:9112e09912db | 215 | } |
jm | 0:9112e09912db | 216 | } |
jm | 0:9112e09912db | 217 | } |
jm | 0:9112e09912db | 218 | if(Help)printf("gpioBits (Port id)0..4 (Register DIR/PIN/MASK/CLR/SET)0..4 (value)0..0xFFFFFFFF (clear/set/toggle)0..2\n"); |
jm | 0:9112e09912db | 219 | // Ignore pending command line |
jm | 0:9112e09912db | 220 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 221 | } |
jm | 0:9112e09912db | 222 | |
jm | 0:9112e09912db | 223 | /** @brief Reset/Set/Toggle an output pin |
jm | 0:9112e09912db | 224 | * Also sets pin as outputs. |
jm | 0:9112e09912db | 225 | * @param[in] pin pin id (value)0..432 |
jm | 0:9112e09912db | 226 | * @param[in] action 0..2 Clear/Set/Toggle |
jm | 0:9112e09912db | 227 | * @returns Return Message: GPPB0 pin state |
jm | 0:9112e09912db | 228 | */ |
jm | 0:9112e09912db | 229 | void gpio(unsigned int pin, unsigned int action){ |
jm | 0:9112e09912db | 230 | // Get port and bit for that pin |
jm | 0:9112e09912db | 231 | uint32_t bitValue; |
jm | 0:9112e09912db | 232 | uint8_t port; |
jm | 0:9112e09912db | 233 | |
jm | 0:9112e09912db | 234 | // Get port, bit and bit value |
jm | 0:9112e09912db | 235 | port = pin/100; |
jm | 0:9112e09912db | 236 | bitValue = 1<<(pin%100); |
jm | 0:9112e09912db | 237 | |
jm | 0:9112e09912db | 238 | // set mbed pin direction as output |
jm | 0:9112e09912db | 239 | jmGPIO[port]->FIODIR |= bitValue; |
jm | 0:9112e09912db | 240 | // make sure FIOMASK bit is enable |
jm | 0:9112e09912db | 241 | jmGPIO[port]->FIOMASK &= ~bitValue; |
jm | 0:9112e09912db | 242 | |
jm | 0:9112e09912db | 243 | switch(action) |
jm | 0:9112e09912db | 244 | { |
jm | 0:9112e09912db | 245 | case Clear: jmGPIO[port]->FIOPIN &= ~bitValue; // reset pin low |
jm | 0:9112e09912db | 246 | break; |
jm | 0:9112e09912db | 247 | case Set: jmGPIO[port]->FIOPIN |= bitValue; // set pin High |
jm | 0:9112e09912db | 248 | break; |
jm | 0:9112e09912db | 249 | case Toggle:jmGPIO[port]->FIOPIN ^= bitValue; // toggle pin |
jm | 0:9112e09912db | 250 | break; |
jm | 0:9112e09912db | 251 | } |
jm | 0:9112e09912db | 252 | if( (jmGPIO[port]->FIOPIN & bitValue) == 0)bitValue = 0; |
jm | 0:9112e09912db | 253 | else bitValue = 1; |
jm | 0:9112e09912db | 254 | |
jm | 0:9112e09912db | 255 | // Report bit modification |
jm | 0:9112e09912db | 256 | printf("GPPB0 %d %d\n",pin, bitValue); |
jm | 0:9112e09912db | 257 | } |
jm | 0:9112e09912db | 258 | |
jm | 0:9112e09912db | 259 | /** @brief Command line to Reset/Set/Toggle an output pin |
jm | 0:9112e09912db | 260 | * Also sets pin as outputs. Not all pins are available |
jm | 0:9112e09912db | 261 | * Use with care. Designed to be used with gui which locks non MBED DIP pins |
jm | 0:9112e09912db | 262 | * Command Line Format: gpioBit (Pin id)0..432 (action clear/set/toggle)0..2 |
jm | 0:9112e09912db | 263 | * @param[in] From command line: (Pin id)0..432 (action clear/set/toggle)0..2 |
jm | 0:9112e09912db | 264 | * @returns Return Message: GPPB0 pin state |
jm | 0:9112e09912db | 265 | */ |
jm | 0:9112e09912db | 266 | void cli_gpioBit(void){ |
jm | 0:9112e09912db | 267 | unsigned int pin, action; |
jm | 0:9112e09912db | 268 | // Extract pin number from Command Line |
jm | 0:9112e09912db | 269 | if(ExtractUInteger(pLine,&pin,0,432)){ |
jm | 0:9112e09912db | 270 | // Extract action from Command Line |
jm | 0:9112e09912db | 271 | if(ExtractUInteger(pLine,&action,0,2)){ |
jm | 0:9112e09912db | 272 | gpio(pin,action); |
jm | 0:9112e09912db | 273 | return; |
jm | 0:9112e09912db | 274 | } |
jm | 0:9112e09912db | 275 | } |
jm | 0:9112e09912db | 276 | if(Help)printf("Use with care ! gpioBit (pin Number)0..432 (action Clear/Set/Toggle)0..2\n"); |
jm | 0:9112e09912db | 277 | // Ignore pending command line |
jm | 0:9112e09912db | 278 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 279 | } |