Jean Mercier
/
jmAll
Command all jmCLIG modules from serial port
jmPulse.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 jmPulse.c |
jm | 0:9112e09912db | 3 | * @version 1.0 |
jm | 0:9112e09912db | 4 | * @date Feb 12, 2011 |
jm | 0:9112e09912db | 5 | */ |
jm | 0:9112e09912db | 6 | |
jm | 0:9112e09912db | 7 | #include "jmPulse.h" |
jm | 0:9112e09912db | 8 | #include "jmRingBuffer.h" |
jm | 0:9112e09912db | 9 | #include "LPC17xx.h" |
jm | 0:9112e09912db | 10 | #include "main.h" |
jm | 0:9112e09912db | 11 | #include "jmCommands.h" |
jm | 0:9112e09912db | 12 | #include "jmMessages.h" |
jm | 0:9112e09912db | 13 | #include "jmRingBuffer.h" |
jm | 0:9112e09912db | 14 | #include "stdio.h" |
jm | 0:9112e09912db | 15 | |
jm | 0:9112e09912db | 16 | #ifndef nonStop |
jm | 0:9112e09912db | 17 | #define nonStop 65535 // for continuous mode operation |
jm | 0:9112e09912db | 18 | #endif |
jm | 0:9112e09912db | 19 | |
jm | 0:9112e09912db | 20 | // output defenitions |
jm | 0:9112e09912db | 21 | #define Low 0 |
jm | 0:9112e09912db | 22 | #define High 1 |
jm | 0:9112e09912db | 23 | #define StatusOffset 3 |
jm | 0:9112e09912db | 24 | |
jm | 0:9112e09912db | 25 | // State definitions |
jm | 0:9112e09912db | 26 | #define puNotInit 0 |
jm | 0:9112e09912db | 27 | #define puStopped 2 |
jm | 0:9112e09912db | 28 | #define Pulse 1 |
jm | 0:9112e09912db | 29 | |
jm | 0:9112e09912db | 30 | struct StructPulse sPulse[pulseQty]; // static creation |
jm | 0:9112e09912db | 31 | |
jm | 0:9112e09912db | 32 | /** Module Data Structure Initialization |
jm | 0:9112e09912db | 33 | * @brief All State Machines put to sleep. |
jm | 0:9112e09912db | 34 | * @param[in] none |
jm | 0:9112e09912db | 35 | * @returns none |
jm | 0:9112e09912db | 36 | */ |
jm | 0:9112e09912db | 37 | void PulseInit(void){ |
jm | 0:9112e09912db | 38 | int i; |
jm | 0:9112e09912db | 39 | for(i=0;i<pulseQty;i++){ |
jm | 0:9112e09912db | 40 | sPulse[i].state = puNotInit; |
jm | 0:9112e09912db | 41 | } |
jm | 0:9112e09912db | 42 | } |
jm | 0:9112e09912db | 43 | |
jm | 0:9112e09912db | 44 | /** |
jm | 0:9112e09912db | 45 | * @brief Stop one or all pulse Machines |
jm | 0:9112e09912db | 46 | * @param[in] Extracted from command line (Pulse Number)0..7 255 for all |
jm | 0:9112e09912db | 47 | * @returns Return Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 48 | */ |
jm | 0:9112e09912db | 49 | void cli_PulseStop(void){ |
jm | 0:9112e09912db | 50 | unsigned int id,i; |
jm | 0:9112e09912db | 51 | |
jm | 0:9112e09912db | 52 | if(ExtractUInteger(pLine,&id,0,255)){ // extract pulseNumber |
jm | 0:9112e09912db | 53 | // put id pulse machine to sleep |
jm | 0:9112e09912db | 54 | if(id < pulseQty){ |
jm | 0:9112e09912db | 55 | sPulse[id].cycles = 0; |
jm | 0:9112e09912db | 56 | sPulse[id].state = puStopped; |
jm | 0:9112e09912db | 57 | rGPPP0(id); |
jm | 0:9112e09912db | 58 | return; |
jm | 0:9112e09912db | 59 | } |
jm | 0:9112e09912db | 60 | |
jm | 0:9112e09912db | 61 | if(id == 255){ |
jm | 0:9112e09912db | 62 | // stop all controllers |
jm | 0:9112e09912db | 63 | for(i=0;i<pulseQty;i++){ |
jm | 0:9112e09912db | 64 | sPulse[i].cycles = 0; |
jm | 0:9112e09912db | 65 | sPulse[i].state = puStopped; |
jm | 0:9112e09912db | 66 | } |
jm | 0:9112e09912db | 67 | } |
jm | 0:9112e09912db | 68 | |
jm | 0:9112e09912db | 69 | cli_GPPP0(); // Return 8 Messages: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 70 | return; |
jm | 0:9112e09912db | 71 | } |
jm | 0:9112e09912db | 72 | if(Help)printf("pulseStop (Pulse number)0..%d \n",pulseQty-1); |
jm | 0:9112e09912db | 73 | // Ignore pending command line |
jm | 0:9112e09912db | 74 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 75 | } |
jm | 0:9112e09912db | 76 | |
jm | 0:9112e09912db | 77 | |
jm | 0:9112e09912db | 78 | /** Module Pulse Command Line Interface |
jm | 0:9112e09912db | 79 | * @brief Command Line Interface to control PWM/Pulse on output pins. |
jm | 0:9112e09912db | 80 | * @param[in] Extracted from command line (Pulse Controller Number)0..7 (Pin id)0..432 (tOn)0..255 (tOff)0..255) (cycles)1..65535 |
jm | 0:9112e09912db | 81 | * @returns none |
jm | 0:9112e09912db | 82 | */ |
jm | 0:9112e09912db | 83 | void cli_Pulse(void){ |
jm | 0:9112e09912db | 84 | unsigned int pulseNum, pinNumber,tOn,tOff,cycles; |
jm | 0:9112e09912db | 85 | if(ExtractUInteger(pLine,&pulseNum,0,pulseQty-1)){ // extract pulseNumber |
jm | 0:9112e09912db | 86 | if(ExtractUInteger(pLine,&pinNumber,0,432)){ // extract pin id |
jm | 0:9112e09912db | 87 | if(ExtractUInteger(pLine,&tOn,0,255)){ // extract tOn |
jm | 0:9112e09912db | 88 | if(ExtractUInteger(pLine,&tOff,0,255)){ // extract tOff |
jm | 0:9112e09912db | 89 | // extract cycles and test for nonStop mode |
jm | 0:9112e09912db | 90 | if(!ExtractUInteger(pLine,&cycles,1,65535))cycles = nonStop; |
jm | 0:9112e09912db | 91 | |
jm | 0:9112e09912db | 92 | SetPulseParam(pulseNum, pinNumber, tOn, tOff, cycles); |
jm | 0:9112e09912db | 93 | if(Feedback)printf("pulse %d Pin%d tOn:%d tOff:%d Cycles:%d\n",pulseNum,pinNumber,tOn,tOff,cycles); |
jm | 0:9112e09912db | 94 | return; |
jm | 0:9112e09912db | 95 | } |
jm | 0:9112e09912db | 96 | } |
jm | 0:9112e09912db | 97 | } |
jm | 0:9112e09912db | 98 | } |
jm | 0:9112e09912db | 99 | if(Help)printf("pulse (Pulse number)0..%d (Pin id)0..432 (tOn)0..255 (tOff)0..255 (Cycles)[1..0xFFFF]\n",pulseQty-1); |
jm | 0:9112e09912db | 100 | // Ignore pending command line |
jm | 0:9112e09912db | 101 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 102 | } |
jm | 0:9112e09912db | 103 | |
jm | 0:9112e09912db | 104 | /** Module Parameter Setting Function |
jm | 0:9112e09912db | 105 | * @brief Enables PWM/pulse State Machine, |
jm | 0:9112e09912db | 106 | * Also sets mbed pin as outputs. |
jm | 0:9112e09912db | 107 | * @param[in] num pulse controller number (value)0..7 |
jm | 0:9112e09912db | 108 | * @param[in] pin pin id (value)0..432 |
jm | 0:9112e09912db | 109 | * @param[in] tOn on time interval in ticks (tOn)0..255 |
jm | 0:9112e09912db | 110 | * @param[in] tOff off time interval in ticks (tOff)0..255 |
jm | 0:9112e09912db | 111 | * @param[in] cycles repetition number of tOn-tOff cycles |
jm | 0:9112e09912db | 112 | * @param[in] state set state: inactive, pulse, high, low |
jm | 0:9112e09912db | 113 | * @returns Return Message: GPPP0 id pin state |
jm | 0:9112e09912db | 114 | */ |
jm | 0:9112e09912db | 115 | void SetPulseParam(uint8_t num, uint16_t pin, uint8_t tOn, uint8_t tOff, uint16_t cycles){ |
jm | 0:9112e09912db | 116 | // Get port and bit for that pin |
jm | 0:9112e09912db | 117 | uint32_t bitValue; |
jm | 0:9112e09912db | 118 | uint8_t port; |
jm | 0:9112e09912db | 119 | |
jm | 0:9112e09912db | 120 | // Get port, bit and bit value |
jm | 0:9112e09912db | 121 | port = pin/100; |
jm | 0:9112e09912db | 122 | bitValue = 1<<(pin%100); |
jm | 0:9112e09912db | 123 | |
jm | 0:9112e09912db | 124 | sPulse[num].port = jmGPIO[port]; |
jm | 0:9112e09912db | 125 | sPulse[num].pin = (uint8_t)pin; |
jm | 0:9112e09912db | 126 | sPulse[num].bitValue = bitValue; |
jm | 0:9112e09912db | 127 | |
jm | 0:9112e09912db | 128 | // set mbed pin direction as output |
jm | 0:9112e09912db | 129 | jmGPIO[port]->FIODIR |= bitValue; |
jm | 0:9112e09912db | 130 | // make sure FIOMASK bit is enable |
jm | 0:9112e09912db | 131 | jmGPIO[port]->FIOMASK &= ~bitValue; |
jm | 0:9112e09912db | 132 | |
jm | 0:9112e09912db | 133 | |
jm | 0:9112e09912db | 134 | // Low State |
jm | 0:9112e09912db | 135 | if(tOn == 0 && tOff != 0) |
jm | 0:9112e09912db | 136 | { |
jm | 0:9112e09912db | 137 | jmGPIO[port]->FIOPIN &= ~bitValue; // reset pin low |
jm | 0:9112e09912db | 138 | sPulse[num].output = Low; |
jm | 0:9112e09912db | 139 | sPulse[num].state = puStopped; |
jm | 0:9112e09912db | 140 | } |
jm | 0:9112e09912db | 141 | // High State |
jm | 0:9112e09912db | 142 | if(tOn != 0 && tOff == 0) |
jm | 0:9112e09912db | 143 | { |
jm | 0:9112e09912db | 144 | jmGPIO[port]->FIOPIN |= bitValue; // set pin High |
jm | 0:9112e09912db | 145 | sPulse[num].output = High; |
jm | 0:9112e09912db | 146 | sPulse[num].state = puStopped; |
jm | 0:9112e09912db | 147 | } |
jm | 0:9112e09912db | 148 | // Toggle output |
jm | 0:9112e09912db | 149 | if(tOn == 0 && tOff == 0) |
jm | 0:9112e09912db | 150 | { |
jm | 0:9112e09912db | 151 | jmGPIO[port]->FIOPIN ^= bitValue; // toggle pin |
jm | 0:9112e09912db | 152 | // Toggle state |
jm | 0:9112e09912db | 153 | if(sPulse[num].output == High)sPulse[num].output = Low; |
jm | 0:9112e09912db | 154 | else sPulse[num].output = High; |
jm | 0:9112e09912db | 155 | sPulse[num].state = puStopped; |
jm | 0:9112e09912db | 156 | } |
jm | 0:9112e09912db | 157 | // Start Pulse controller |
jm | 0:9112e09912db | 158 | if(tOn != 0 && tOff != 0 && cycles !=0){ |
jm | 0:9112e09912db | 159 | sPulse[num].state = Pulse; |
jm | 0:9112e09912db | 160 | sPulse[num].tOn = (uint8_t)tOn; |
jm | 0:9112e09912db | 161 | sPulse[num].tOff = (uint8_t)tOff; |
jm | 0:9112e09912db | 162 | sPulse[num].cycles = (uint16_t)cycles; |
jm | 0:9112e09912db | 163 | sPulse[num].eggTimer = 0; |
jm | 0:9112e09912db | 164 | } |
jm | 0:9112e09912db | 165 | // Stop Pulse controller |
jm | 0:9112e09912db | 166 | if(tOn != 0 && tOff != 0 && cycles ==0){ |
jm | 0:9112e09912db | 167 | sPulse[num].cycles = 0; |
jm | 0:9112e09912db | 168 | sPulse[num].state = puStopped; |
jm | 0:9112e09912db | 169 | } |
jm | 0:9112e09912db | 170 | |
jm | 0:9112e09912db | 171 | // Report process modification |
jm | 0:9112e09912db | 172 | rGPPP0(num); //Return Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 173 | } |
jm | 0:9112e09912db | 174 | |
jm | 0:9112e09912db | 175 | /*********************************************************************** |
jm | 0:9112e09912db | 176 | * @brief Module State Machine. |
jm | 0:9112e09912db | 177 | * Enable different pwm/pulse rates concurrently. |
jm | 0:9112e09912db | 178 | * @param none |
jm | 0:9112e09912db | 179 | * @returns When cycles Done, Return Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 180 | */ |
jm | 0:9112e09912db | 181 | void PulseSM(void){ |
jm | 0:9112e09912db | 182 | int i; |
jm | 0:9112e09912db | 183 | for(i=0;i<pulseQty;i++){ // for each SM define by pins |
jm | 0:9112e09912db | 184 | |
jm | 0:9112e09912db | 185 | if(sPulse[i].state == puStopped) continue; //controller running ? |
jm | 0:9112e09912db | 186 | |
jm | 0:9112e09912db | 187 | if(sPulse[i].cycles!=0){ // Cycles to DO ? |
jm | 0:9112e09912db | 188 | switch(sPulse[i].output){ |
jm | 0:9112e09912db | 189 | |
jm | 0:9112e09912db | 190 | // output is low |
jm | 0:9112e09912db | 191 | case Low: if(sPulse[i].eggTimer==0){ // time to change ? |
jm | 0:9112e09912db | 192 | sPulse[i].port->FIOPIN |= sPulse[i].bitValue; // set pin High |
jm | 0:9112e09912db | 193 | sPulse[i].eggTimer=sPulse[i].tOn; // load timer with tOn |
jm | 0:9112e09912db | 194 | sPulse[i].output=High; // udpate structure |
jm | 0:9112e09912db | 195 | } |
jm | 0:9112e09912db | 196 | break; |
jm | 0:9112e09912db | 197 | |
jm | 0:9112e09912db | 198 | // output is High |
jm | 0:9112e09912db | 199 | case High: if(sPulse[i].eggTimer==0){ // time to change ? |
jm | 0:9112e09912db | 200 | sPulse[i].port->FIOPIN &= ~sPulse[i].bitValue; // reset pin low |
jm | 0:9112e09912db | 201 | sPulse[i].output=Low; // update structure |
jm | 0:9112e09912db | 202 | sPulse[i].eggTimer=sPulse[i].tOff; // load timer with tOff |
jm | 0:9112e09912db | 203 | if(sPulse[i].cycles != nonStop){ // continuous mode ? |
jm | 0:9112e09912db | 204 | if(--sPulse[i].cycles == 0){ // decrease qty if not continuous mode |
jm | 0:9112e09912db | 205 | sPulse[i].state = puStopped; // stop controller |
jm | 0:9112e09912db | 206 | // report end of cycles |
jm | 0:9112e09912db | 207 | rGPPP0(i); //Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 208 | } |
jm | 0:9112e09912db | 209 | } |
jm | 0:9112e09912db | 210 | } |
jm | 0:9112e09912db | 211 | break; |
jm | 0:9112e09912db | 212 | }//switch |
jm | 0:9112e09912db | 213 | }//if cycles |
jm | 0:9112e09912db | 214 | }//for |
jm | 0:9112e09912db | 215 | }//PulseSM |
jm | 0:9112e09912db | 216 | |
jm | 0:9112e09912db | 217 | /** Module Get Module Process Properties Command Line Interface |
jm | 0:9112e09912db | 218 | * @brief Command Line Interface to Get Module Public Process Properties |
jm | 0:9112e09912db | 219 | * @param[in] Extracted from command line GPPP0 id |
jm | 0:9112e09912db | 220 | * @returns Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 221 | */ |
jm | 0:9112e09912db | 222 | void cli_GPPP0(void) |
jm | 0:9112e09912db | 223 | { unsigned int id; |
jm | 0:9112e09912db | 224 | if(ExtractUInteger(pLine,&id,0,7)){ // extract pulse id Number |
jm | 0:9112e09912db | 225 | rGPPP0(id); //Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 226 | return; |
jm | 0:9112e09912db | 227 | } |
jm | 0:9112e09912db | 228 | |
jm | 0:9112e09912db | 229 | if(Help)printf("GPPP0 (Pulse id)0..%d\n",pulseQty-1); |
jm | 0:9112e09912db | 230 | // Ignore pending command line |
jm | 0:9112e09912db | 231 | NextCommand(nl,pLine); |
jm | 0:9112e09912db | 232 | } |
jm | 0:9112e09912db | 233 | |
jm | 0:9112e09912db | 234 | /** Public Properties Message |
jm | 0:9112e09912db | 235 | * @brief Send Process Properties to update GUI |
jm | 0:9112e09912db | 236 | * @param[in] id Process identification |
jm | 0:9112e09912db | 237 | * @returns Message: GPPP0 id pin tOn tOff status |
jm | 0:9112e09912db | 238 | */ |
jm | 0:9112e09912db | 239 | void rGPPP0(unsigned int id ){ |
jm | 0:9112e09912db | 240 | int status; |
jm | 0:9112e09912db | 241 | if(id < pulseQty){ |
jm | 0:9112e09912db | 242 | if( sPulse[id].state == puStopped)status = sPulse[id].output +StatusOffset; |
jm | 0:9112e09912db | 243 | else status = sPulse[id].state; |
jm | 0:9112e09912db | 244 | printf("GPPP0 %d %d %d %d %d\n",id,sPulse[id].pin,sPulse[id].tOn,sPulse[id].tOff,status); |
jm | 0:9112e09912db | 245 | |
jm | 0:9112e09912db | 246 | } |
jm | 0:9112e09912db | 247 | } |