Jean Mercier / Mbed 2 deprecated jmGPIO

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jmLPC17xx_gpio.c Source File

jmLPC17xx_gpio.c

00001 /*************************************************************************
00002  * @file    jmLPC17xx_gpio.c
00003  * @brief    Command Line Interface Module for LPC_GPIO
00004  *                  
00005  * @version    1.1
00006  * @date    Jan 24, 2011
00007  */
00008 
00009 #include "jmLPC17xx_gpio.h"
00010 #include "LPC17xx.h"
00011 #include "main.h"
00012 #include "jmCommands.h"
00013 #include "jmMessages.h"
00014 #include "jmRingBuffer.h"
00015 #include "stdio.h"
00016 
00017 
00018 #define DIR 0
00019 #define PIN 1
00020 #define MASK 2
00021 #define CLR 3
00022 #define SET 4
00023 
00024 #define Clear 0
00025 #define Set 1
00026 #define Toggle 2
00027 
00028 /***********************************************************************
00029  * @brief        Send port registers content
00030  * @param[in]    portNum    (Port Number value)0..4
00031  * @return        none
00032  **********************************************************************/
00033 void PortInfo(unsigned int portNum){
00034    if(portNum <5){
00035       printf("Port %1d\n",portNum);
00036       printf("FIODIR   \t0x%08X\n",jmGPIO[portNum]->FIODIR);
00037       printf("FIOMASK  \t0x%08X\n",jmGPIO[portNum]->FIOMASK);
00038       printf("FIOPIN   \t0x%08X\n",jmGPIO[portNum]->FIOPIN);
00039       printf("FIOSET   \t0x%08X\n",jmGPIO[portNum]->FIOSET);
00040       printf("FIOCLR   \t0x%08X\n",jmGPIO[portNum]->FIOCLR);
00041       printf("\n");
00042    }
00043 }
00044 
00045 /***********************************************************************
00046  * @brief        Send all ports registers content
00047  * @param[in]    none
00048  * @return        none
00049  **********************************************************************/
00050 void PortsInfo(void){
00051    unsigned int portNum;
00052    for(portNum=0;portNum<5;portNum++) PortInfo(portNum);
00053 }
00054 
00055 /***********************************************************************
00056  * @brief    iport Command Line Interface.
00057  * Send port register content
00058  * Command Line Format: iport (portNumber)0..4 
00059  * @example iport 1     Will printout Port1 registers content
00060  * @param[in]    Extracted from command line ring buffer (PortNumber)0..4
00061  * @return        none
00062  **********************************************************************/
00063 void cli_PortInfo(void){
00064    unsigned int portNum;
00065 
00066    if(ExtractUInteger(pLine,&portNum,0,4))
00067       PortInfo(portNum);
00068    else{
00069       if(Help)printf("iport (portNumber)0..4\n");
00070       // Ignore pending command line
00071       NextCommand(nl,pLine);
00072    }
00073 }
00074 
00075 
00076 /***********************************************************************
00077  * @brief        Command line interface to read and send Port.Bit per bit position
00078  * Command Line Format: bitRead  (Port Number)0..4 (Bit position)0..31
00079  * ex: bitRead  1 18  read and send P1.18 input pin value
00080  * pin must be previously set to input in GPIODIR and associated GPIOMASK bit= 0
00081  * @param[in]    Extracted from command line (Port Number)0..4 (Bit position)0..31
00082  * @return        none
00083  **********************************************************************/
00084 void cli_BitRead(void){
00085   unsigned int portNumber, bitPosition, bitValue;
00086    // Extract pin number from Command Line
00087    if(ExtractUInteger(pLine,&portNumber,0,4)){
00088       // Extract bit position from Command Line
00089       if(ExtractUInteger(pLine,&bitPosition,0,31)){
00090          // Extract state from Command Line
00091          bitValue = 1<<bitPosition;
00092          if(jmGPIO[portNumber]->FIOPIN & bitValue)bitValue =1;
00093          printf("P%d.%d %d\n",portNumber,bitPosition,bitValue);
00094           return;
00095       }
00096    } 
00097    if(Help)printf("bitRead (Port Number)0..4 (Bit position)0..31\n");
00098    // Ignore pending command line
00099    NextCommand(nl,pLine);
00100 }
00101 
00102 /** Read GPIO Registers 
00103  * @brief        Command Line Interface to Read and Send GPIO register value
00104  * @param[in]    Extracted from command line GPPG0 (Port)0..4 (Register DIR/PIN/MASK)0..2
00105  * @returns    Message: GPPG0 (Port)0..4 (Register DIR/PIN/MASK)0..2 (value)0..0xFFFFFFFF 
00106  */
00107 void cli_GPPG0(void){
00108    uint32_t value=0;
00109    unsigned int port, reg;
00110    if(ExtractUInteger(pLine,&port,0,4)){    // extract port id
00111          if(ExtractUInteger(pLine,&reg,0,4)){  // extract register id
00112          switch(reg){
00113              case DIR: value = jmGPIO[port]->FIODIR; break;
00114             case PIN: value = jmGPIO[port]->FIOPIN; break;
00115             case MASK: value = jmGPIO[port]->FIOMASK; break;
00116             case CLR: value = jmGPIO[port]->FIOCLR; break;
00117             case SET: value = jmGPIO[port]->FIOSET; break;
00118         }
00119         printf("GPPG0 %d %d 0x%08X\n",port,reg,value);
00120           return;
00121       }
00122     }
00123   if(Help)printf("GPPG0 (Port)0..4 (Register DIR/PIN/MASK/CLR/SET)0..4\n");  
00124   // Ignore pending command line
00125   NextCommand(nl,pLine);
00126 }
00127 
00128 /***********************************************************************
00129  * @brief        Command line interface to access GPIO register bits
00130  * Use with care. Designed to be used with gui which locks non MBED DIP pins
00131  * 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
00132  * @param[in]    Extracted from command line   (Port)0..4 (Register DIR/PIN/MASK)0..2 (value)0..0xFFFFFFFF 
00133  * @return        Message: GPPG0 (Port)0..4 (Register DIR/PIN/MASK)0..2 (value)0..0xFFFFFFFF 
00134  **********************************************************************/
00135 void cli_gpioBits(void){
00136   unsigned int port, reg, value, action;
00137    // Extract port id from Command Line
00138    if(ExtractUInteger(pLine,&port,0,4)){
00139       // Extract register id from Command Line
00140       if(ExtractUInteger(pLine,&reg,0,4)){
00141           // Extract value from Command Line
00142           if(ExtractUInteger(pLine,&value,0,0xFFFFFFFF)){
00143              // Extract state from Command Line
00144              if(ExtractUInteger(pLine,&action,0,2)){
00145                 switch(reg){
00146                    case DIR:
00147                        switch(action){
00148                            case Clear: jmGPIO[port]->FIODIR &= ~value;
00149                               break;
00150                            case Set: jmGPIO[port]->FIODIR |= value;
00151                               break;
00152                            case Toggle: jmGPIO[port]->FIODIR ^= value;
00153                               break;
00154                         }
00155                         break;
00156                    case PIN:
00157                        switch(action){
00158                            case Clear: jmGPIO[port]->FIOPIN &= ~value;
00159                               break;
00160                            case Set: jmGPIO[port]->FIOPIN |= value;
00161                               break;
00162                            case Toggle: jmGPIO[port]->FIOPIN ^= value;
00163                               break;
00164                         }
00165                         break;
00166                    case MASK:
00167                        switch(action){
00168                            case Clear: jmGPIO[port]->FIOMASK &= ~value;
00169                               break;
00170                            case Set: jmGPIO[port]->FIOMASK |= value;
00171                               break;
00172                            case Toggle: jmGPIO[port]->FIOMASK ^= value;
00173                               break;
00174                         }
00175                         break;
00176                    case CLR:
00177                        switch(action){
00178                            case Clear: jmGPIO[port]->FIOCLR &= ~value;
00179                               break;
00180                            case Set: jmGPIO[port]->FIOCLR |= value;
00181                               break;
00182                            case Toggle: jmGPIO[port]->FIOCLR ^= value;
00183                               break;
00184                         }
00185                         break;
00186                    case SET:
00187                        switch(action){
00188                            case Clear: jmGPIO[port]->FIOSET &= ~value;
00189                               break;
00190                            case Set: jmGPIO[port]->FIOSET |= value;
00191                               break;
00192                            case Toggle: jmGPIO[port]->FIOSET ^= value;
00193                               break;
00194                         }
00195                         break;
00196 
00197                 }
00198                 // gui feedback  
00199                 switch(reg)
00200                 {    
00201                     case DIR: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIODIR);
00202                         break;
00203                     case PIN: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOPIN);
00204                         break;
00205                     case MASK: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOMASK);
00206                         break;
00207                     case CLR: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOCLR);
00208                         break;
00209                     case SET: printf("GPPG0 %d %d 0x%08X\n",port,reg,jmGPIO[port]->FIOSET);
00210                         break;
00211                 }
00212                 if(Feedback)printf("GPIO %d %d 0x%08X %d\n",port,reg,value,action);
00213                 return;
00214              }
00215          }
00216       }
00217    } 
00218    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");
00219    // Ignore pending command line
00220    NextCommand(nl,pLine);
00221 }
00222 
00223 /** @brief   Reset/Set/Toggle an output pin
00224  * Also sets pin as outputs.
00225  * @param[in]    pin      pin id (value)0..432
00226  * @param[in]    action  0..2 Clear/Set/Toggle
00227  * @returns     Return Message: GPPB0 pin state
00228  */
00229 void gpio(unsigned int pin, unsigned int action){
00230    // Get port and bit for that pin
00231    uint32_t bitValue;
00232    uint8_t port;
00233 
00234    // Get port, bit and bit value
00235    port = pin/100;
00236    bitValue = 1<<(pin%100);
00237    
00238    // set mbed pin direction as output
00239    jmGPIO[port]->FIODIR |= bitValue;
00240    // make sure FIOMASK bit is enable
00241    jmGPIO[port]->FIOMASK &= ~bitValue;
00242 
00243    switch(action)
00244    {
00245     case Clear:     jmGPIO[port]->FIOPIN &= ~bitValue; // reset pin low 
00246                 break;
00247     case Set:    jmGPIO[port]->FIOPIN |= bitValue;  // set pin High
00248                 break;
00249     case Toggle:jmGPIO[port]->FIOPIN ^= bitValue; // toggle pin 
00250                 break;
00251    }
00252    if( (jmGPIO[port]->FIOPIN & bitValue) == 0)bitValue = 0;
00253    else bitValue = 1;
00254 
00255    // Report bit modification
00256    printf("GPPB0 %d %d\n",pin, bitValue);
00257 }
00258 
00259 /** @brief   Command line to Reset/Set/Toggle an output pin
00260  * Also sets pin as outputs.  Not all pins are available
00261  * Use with care. Designed to be used with gui which locks non MBED DIP pins
00262  * Command Line Format:   gpioBit (Pin id)0..432 (action clear/set/toggle)0..2
00263  * @param[in]    From command line: (Pin id)0..432 (action clear/set/toggle)0..2
00264  * @returns     Return Message: GPPB0 pin state
00265  */
00266 void cli_gpioBit(void){
00267   unsigned int pin, action;
00268    // Extract pin number from Command Line
00269    if(ExtractUInteger(pLine,&pin,0,432)){
00270       // Extract action from Command Line
00271       if(ExtractUInteger(pLine,&action,0,2)){
00272          gpio(pin,action);
00273          return;
00274       }
00275    } 
00276    if(Help)printf("Use with care ! gpioBit (pin Number)0..432 (action Clear/Set/Toggle)0..2\n");
00277    // Ignore pending command line
00278    NextCommand(nl,pLine);
00279 }