Jean Mercier / Mbed 2 deprecated jmPulse

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jmPulse.c Source File

jmPulse.c

Go to the documentation of this file.
00001 /**
00002  * @file    jmPulse.c                 
00003  * @version    1.0
00004  * @date    Feb 12, 2011
00005  */
00006 
00007 #include "jmPulse.h"
00008 #include "jmRingBuffer.h"
00009 #include "LPC17xx.h"
00010 #include "main.h"
00011 #include "jmCommands.h"
00012 #include "jmMessages.h"
00013 #include "jmRingBuffer.h"
00014 #include "stdio.h"
00015 
00016 #ifndef nonStop
00017   #define nonStop 65535  // for continuous mode operation
00018 #endif
00019 
00020 // output defenitions
00021 #define Low 0
00022 #define High 1
00023 #define StatusOffset 3
00024 
00025 // State definitions
00026 #define puNotInit    0
00027 #define puStopped 2
00028 #define Pulse 1
00029 
00030 struct StructPulse sPulse[pulseQty]; // static creation
00031 
00032 /** Module Data Structure Initialization
00033  * @brief   All State Machines put to sleep.
00034  * @param[in]    none
00035  * @returns none
00036  */
00037 void PulseInit(void){
00038    int i;
00039    for(i=0;i<pulseQty;i++){
00040      sPulse[i].state = puNotInit;
00041    }
00042 }
00043 
00044 /** 
00045  * @brief   Stop one or all pulse Machines
00046  * @param[in] Extracted from command line (Pulse Number)0..7 255 for all
00047  * @returns   Return Message: GPPP0 id pin tOn tOff status
00048  */
00049 void cli_PulseStop(void){
00050    unsigned int id,i;
00051 
00052    if(ExtractUInteger(pLine,&id,0,255)){    // extract pulseNumber
00053        // put id pulse machine to sleep 
00054       if(id < pulseQty){
00055           sPulse[id].cycles = 0;
00056           sPulse[id].state = puStopped;
00057           rGPPP0(id);
00058           return;
00059       }
00060 
00061       if(id == 255){
00062           // stop all controllers
00063           for(i=0;i<pulseQty;i++){
00064               sPulse[i].cycles = 0;
00065               sPulse[i].state = puStopped;
00066           }
00067       }
00068 
00069       cli_GPPP0();    // Return 8 Messages: GPPP0 id pin tOn tOff status
00070       return;
00071     }
00072   if(Help)printf("pulseStop (Pulse number)0..%d \n",pulseQty-1);  
00073   // Ignore pending command line
00074   NextCommand(nl,pLine);
00075 }
00076 
00077 
00078 /** Module Pulse Command Line Interface
00079  * @brief        Command Line Interface to control PWM/Pulse on output pins.
00080  * @param[in]    Extracted from command line (Pulse Controller Number)0..7 (Pin id)0..432 (tOn)0..255 (tOff)0..255) (cycles)1..65535
00081  * @returns    none
00082  */
00083 void cli_Pulse(void){
00084    unsigned int pulseNum, pinNumber,tOn,tOff,cycles;
00085    if(ExtractUInteger(pLine,&pulseNum,0,pulseQty-1)){    // extract pulseNumber
00086       if(ExtractUInteger(pLine,&pinNumber,0,432)){       // extract pin id
00087          if(ExtractUInteger(pLine,&tOn,0,255)){          // extract tOn
00088             if(ExtractUInteger(pLine,&tOff,0,255)){      // extract tOff
00089                // extract cycles and test for nonStop mode
00090                if(!ExtractUInteger(pLine,&cycles,1,65535))cycles = nonStop;
00091 
00092                SetPulseParam(pulseNum, pinNumber, tOn, tOff, cycles);
00093                if(Feedback)printf("pulse %d Pin%d tOn:%d  tOff:%d  Cycles:%d\n",pulseNum,pinNumber,tOn,tOff,cycles); 
00094                return;
00095            }
00096         }
00097       }
00098     }
00099   if(Help)printf("pulse (Pulse number)0..%d (Pin id)0..432 (tOn)0..255 (tOff)0..255 (Cycles)[1..0xFFFF]\n",pulseQty-1);  
00100   // Ignore pending command line
00101   NextCommand(nl,pLine);
00102 }
00103 
00104 /** Module Parameter Setting Function
00105  * @brief   Enables PWM/pulse State Machine,
00106  * Also sets mbed pin as outputs.
00107  * @param[in]    num      pulse controller number (value)0..7  
00108  * @param[in]    pin      pin id (value)0..432
00109  * @param[in]    tOn      on time interval in ticks (tOn)0..255
00110  * @param[in]    tOff     off time interval in ticks (tOff)0..255
00111  * @param[in]    cycles   repetition number of tOn-tOff cycles
00112  * @param[in]    state    set state: inactive, pulse, high, low
00113  * @returns     Return Message: GPPP0 id pin state
00114  */
00115 void SetPulseParam(uint8_t num, uint16_t pin, uint8_t tOn, uint8_t tOff, uint16_t cycles){
00116    // Get port and bit for that pin
00117    uint32_t bitValue;
00118    uint8_t port;
00119 
00120    // Get port, bit and bit value
00121    port = pin/100;
00122    bitValue = 1<<(pin%100);
00123 
00124    sPulse[num].port = jmGPIO[port];
00125    sPulse[num].pin = (uint8_t)pin;
00126    sPulse[num].bitValue = bitValue;
00127    
00128    // set mbed pin direction as output
00129    jmGPIO[port]->FIODIR |= bitValue;
00130    // make sure FIOMASK bit is enable
00131    jmGPIO[port]->FIOMASK &= ~bitValue;
00132 
00133    
00134    // Low State
00135    if(tOn == 0 && tOff != 0)
00136    {
00137         jmGPIO[port]->FIOPIN &= ~bitValue; // reset pin low 
00138         sPulse[num].output = Low;
00139         sPulse[num].state = puStopped;
00140    }
00141    // High State 
00142    if(tOn != 0 && tOff == 0)     
00143    {
00144         jmGPIO[port]->FIOPIN |= bitValue;  // set pin High
00145         sPulse[num].output = High;
00146         sPulse[num].state = puStopped;
00147    }
00148    // Toggle output
00149    if(tOn == 0 && tOff == 0) 
00150    {
00151        jmGPIO[port]->FIOPIN ^= bitValue; // toggle pin 
00152        // Toggle state
00153        if(sPulse[num].output == High)sPulse[num].output = Low;
00154        else sPulse[num].output = High;
00155        sPulse[num].state = puStopped;
00156    }
00157    // Start Pulse controller
00158    if(tOn != 0 && tOff != 0 && cycles !=0){             
00159        sPulse[num].state = Pulse;    
00160        sPulse[num].tOn = (uint8_t)tOn;
00161        sPulse[num].tOff = (uint8_t)tOff;
00162        sPulse[num].cycles = (uint16_t)cycles;
00163        sPulse[num].eggTimer = 0;
00164    }
00165       // Stop Pulse controller
00166    if(tOn != 0 && tOff != 0 && cycles ==0){             
00167        sPulse[num].cycles = 0;
00168        sPulse[num].state = puStopped;
00169    }
00170 
00171    // Report process modification
00172    rGPPP0(num);    //Return Message: GPPP0 id pin tOn tOff status
00173 }
00174 
00175 /***********************************************************************
00176  * @brief    Module State Machine.  
00177  * Enable different pwm/pulse rates concurrently.
00178  * @param none
00179  * @returns    When cycles Done, Return Message: GPPP0 id pin tOn tOff status
00180  */ 
00181 void PulseSM(void){
00182   int i;
00183    for(i=0;i<pulseQty;i++){ // for each SM define by pins
00184      
00185      if(sPulse[i].state == puStopped) continue; //controller running ?
00186          
00187      if(sPulse[i].cycles!=0){  // Cycles to DO ?
00188        switch(sPulse[i].output){       
00189 
00190          // output is low
00191          case  Low: if(sPulse[i].eggTimer==0){                      // time to change ?     
00192                      sPulse[i].port->FIOPIN |= sPulse[i].bitValue;  // set pin High         
00193                      sPulse[i].eggTimer=sPulse[i].tOn;              // load timer with tOn   
00194                      sPulse[i].output=High;                         // udpate structure
00195                   }
00196                   break;
00197 
00198          // output is High
00199          case  High: if(sPulse[i].eggTimer==0){                           // time to change ?
00200                      sPulse[i].port->FIOPIN &= ~sPulse[i].bitValue;    // reset pin low  
00201                      sPulse[i].output=Low;                             // update structure    
00202                      sPulse[i].eggTimer=sPulse[i].tOff;                // load timer with tOff  
00203                      if(sPulse[i].cycles != nonStop){                  // continuous mode ?
00204                         if(--sPulse[i].cycles == 0){      // decrease qty if not continuous mode
00205                             sPulse[i].state = puStopped;                    // stop controller
00206                             // report end of cycles
00207                             rGPPP0(i); //Message: GPPP0 id pin tOn tOff status
00208                         }
00209                      }
00210                   }
00211                   break;
00212          }//switch
00213      }//if cycles
00214   }//for
00215 }//PulseSM
00216 
00217 /** Module Get Module Process Properties Command Line Interface
00218  * @brief        Command Line Interface to Get Module Public Process Properties
00219  * @param[in]    Extracted from command line GPPP0 id
00220  * @returns    Message: GPPP0 id pin tOn tOff status
00221  */
00222 void cli_GPPP0(void)
00223 {    unsigned int id;
00224    if(ExtractUInteger(pLine,&id,0,7)){    // extract pulse id Number
00225        rGPPP0(id); //Message: GPPP0 id pin tOn tOff status
00226        return;
00227     }
00228 
00229     if(Help)printf("GPPP0 (Pulse id)0..%d\n",pulseQty-1);  
00230     // Ignore pending command line
00231     NextCommand(nl,pLine);
00232 }
00233 
00234 /** Public Properties Message
00235  * @brief    Send Process Properties to update GUI
00236  * @param[in] id Process identification
00237  * @returns   Message: GPPP0 id pin tOn tOff status
00238  */
00239 void rGPPP0(unsigned int id ){
00240     int status;
00241     if(id < pulseQty){
00242        if( sPulse[id].state == puStopped)status = sPulse[id].output +StatusOffset;
00243        else  status = sPulse[id].state;
00244        printf("GPPP0 %d %d %d %d %d\n",id,sPulse[id].pin,sPulse[id].tOn,sPulse[id].tOff,status);
00245 
00246     }
00247 }