Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: XBee mbed NetServicesMin
layer_frame.cpp
00001 #include "mbed.h" 00002 #include "types.h" 00003 #include "config.h" 00004 #include "culc_crc16.h" 00005 #include "layer_frame.h" 00006 #include "data_models.h" 00007 #include "leds.h" 00008 #include "serialRecieve.h" 00009 00010 00011 volatile int symbolCode; 00012 volatile int pulseCounter; 00013 volatile int pulseState; 00014 volatile bool isSend; 00015 00016 volatile bool isSendContinueous; 00017 volatile bool isSendContinued; 00018 00019 Payload sendedPayload; 00020 uint16_t sendedFrameNumber; 00021 00022 volatile uint8_t buff[70]; 00023 volatile uint8_t buffIndex; 00024 00025 volatile float txMax; 00026 volatile float txMin; 00027 volatile float txMid; 00028 00029 00030 AnalogOut dac(p18); 00031 DigitalOut lightPowerOn(p8); 00032 DigitalOut out(p19); 00033 00034 DigitalOut lightButtonLed(p29); 00035 00036 extern PrefSender spref; 00037 00038 00039 void lightIlluminateChange(uint8_t _sw){ 00040 00041 if(_sw == 0){ 00042 lightPowerOn=0; 00043 spref.lightsw = 0; 00044 lightButtonLed = 0; 00045 } 00046 else{ 00047 00048 lightPowerOn=1; 00049 spref.lightsw = 1; 00050 lightButtonLed = 1; 00051 00052 } 00053 00054 } 00055 00056 void setTxLevel(TxGainData *_data) 00057 { 00058 00059 uint8_t _gain = _data->gain; 00060 uint8_t _mid = _data->middleLevel; 00061 00062 if( (_mid > 10) || (_mid <1) ) return; 00063 if(_gain > 10) return; 00064 00065 if(_gain == 0){ 00066 txMax = 0; 00067 txMin = 0; 00068 txMid = 0; 00069 return; 00070 } 00071 00072 float g = (float)_gain*0.1; 00073 float mid = (float)_mid*0.1; 00074 00075 txMax = kDAC_GAIN_MAX * g; 00076 txMin = kDAC_GAIN_MIN * g; 00077 txMid = mid * g; 00078 } 00079 00080 void timer1_init(void) 00081 { 00082 LPC_SC->PCLKSEL0 &= ~(3UL << 4); //clear bits 96MHz 00083 LPC_SC->PCLKSEL0 |= (1UL << 4); //set bit 00084 00085 LPC_SC->PCONP |=1 << 2; //timer1 power on 00086 LPC_TIM1->MR0 = kTImerCount; //1/96MHz = 0.010416666 us :( 1/(0.010416666 us * 1667) * 2) = 28.794kHz 00087 LPC_TIM1->MCR = 3; //interrupt and reset control 00088 //3 = Interrupt & reset timer1 on match 00089 //1 = Interrupt only, no reset of timer1 00090 // NVIC_EnableIRQ(TIMER1_IRQn); //enable timer1 interrupt 00091 // LPC_TIM1->TCR = 1; //enable Timer1 00092 enableTimer(); 00093 } 00094 00095 void enableTimer(void) 00096 { 00097 LPC_TIM1->MR0 = kTImerCount; 00098 NVIC_EnableIRQ(TIMER1_IRQn); 00099 LPC_TIM1->TCR = 1; //enable Timer1 00100 } 00101 void disableTimer(void) 00102 { 00103 NVIC_DisableIRQ(TIMER1_IRQn); 00104 LPC_TIM1->TCR = 0; //disable Timer1 00105 } 00106 00107 void preambleGenerator(){ 00108 00109 symbolCode = 4; 00110 while( pulseCounter <=96 ) {readProcess();} 00111 pulseCounter = 0; 00112 } 00113 00114 void sendByteData(uint8_t _data){ 00115 // printf("%02X ",_data); 00116 pulseCounter = 0; 00117 00118 symbolCode = (_data & 0x03); 00119 while( pulseCounter < 24 ) {readProcess();} 00120 pulseCounter = 0; 00121 00122 symbolCode = ( (_data & 0x0C ) >> 2); 00123 while( pulseCounter < 24 ) {readProcess();} 00124 pulseCounter = 0; 00125 00126 symbolCode = ( (_data & 0x30 ) >> 4); 00127 while( pulseCounter < 24 ) {readProcess();} 00128 pulseCounter = 0; 00129 00130 symbolCode = ( (_data & 0xC0 ) >> 6); 00131 while( pulseCounter < 24 ){readProcess();} 00132 pulseCounter = 0; 00133 00134 00135 00136 } 00137 00138 00139 void pulseGenerator(){ 00140 00141 if(pulseState == -1) pulseState = 1; 00142 else pulseState = -1; 00143 00144 00145 if(pulseState<0) { out = 1; dac = txMax; } 00146 else { out = 0; dac = txMin; } 00147 00148 } 00149 extern "C" void TIMER1_IRQHandler (void) 00150 { 00151 if((LPC_TIM1->IR & 0x01) != 0x01) return; // if MR0 interrupt, proceed 00152 00153 LPC_TIM1->IR |= 1 << 0; // Clear MR0 interrupt flag 00154 00155 pulseCounter++; 00156 00157 00158 switch(symbolCode){ 00159 00160 case 0: 00161 { 00162 if( pulseCounter <= 6) pulseGenerator(); 00163 else { out = 0; dac=txMid;} 00164 } 00165 break; 00166 00167 case 2: 00168 { 00169 if( (pulseCounter > 6)&(pulseCounter <= 12) ) pulseGenerator(); 00170 else { out = 0; dac=txMid;} 00171 } 00172 break; 00173 00174 case 1: 00175 { 00176 if( (pulseCounter > 12)&(pulseCounter <= 18) ) pulseGenerator(); 00177 else { out = 0; dac=txMid;} 00178 } 00179 break; 00180 00181 case 3: 00182 { 00183 if( (pulseCounter > 18)&(pulseCounter <= 24 )) pulseGenerator(); 00184 else { out = 0; dac=txMid;} 00185 } 00186 break; 00187 00188 case 4: 00189 { 00190 // if( pulseCounter <= 18 ) pulseGenerator(); 00191 if( (pulseCounter > 18) & (pulseCounter <= 36) ) pulseGenerator(); 00192 else { out = 0; dac=txMid;} 00193 } 00194 break; 00195 00196 } 00197 00198 00199 } 00200 00201 void initCarrierWave(){ 00202 dac = kDAC_OFF; 00203 out=0; 00204 00205 pulseCounter=0; 00206 symbolCode = 0; 00207 pulseState = -1; 00208 00209 spref.gainData.gain = kTxGain; 00210 spref.gainData.middleLevel = kTxMidLevel; 00211 00212 setTxLevel( &spref.gainData ); 00213 00214 spref.lightsw=1; 00215 lightIlluminateChange(spref.lightsw); 00216 00217 spref.isSend=0; 00218 spref.continueusMode=0; 00219 00220 00221 sendedFrameNumber=0xffff; 00222 timer1_init(); 00223 00224 // initPayload(&sendedPayload); 00225 } 00226 00227 00228 00229 void sendPayload(Payload *_payload){ 00230 00231 #ifdef _SERVER_TEST_MODE_ 00232 dumpPayload(_payload); 00233 #endif 00234 00235 makedata(_payload); 00236 00237 ledLightTx(); 00238 00239 for(uint8_t i=0 ; i < kContSendNum ; i++){ 00240 sendbuff(); 00241 } 00242 00243 } 00244 00245 void makedata(Payload *_payload ){ 00246 00247 uint16_t fNum= _payload->frame.message.frameNumber; 00248 00249 // printf("\n------------------- fnum:%0X\n",fNum); 00250 if( fNum != sendedFrameNumber ){ 00251 00252 sendedFrameNumber = fNum; 00253 00254 memcpy(&sendedPayload,_payload,sizeof(Payload)); 00255 sendedPayload.frame.message.sum = culcSum(&sendedPayload); 00256 00257 Payload tmp; 00258 memcpy(&tmp,&sendedPayload,sizeof(Payload)); 00259 00260 Frame *frame=&tmp.frame; 00261 00262 payloadToNetwork(&tmp); 00263 // dumpPayload(&tmp); 00264 00265 uint16_t crc = 0xffff; 00266 uint8_t index=0; 00267 00268 //frame type 00269 buff[index]=tmp.ftype; 00270 crc = One_Byte_CRC16_Calc( crc , buff[index]); 00271 00272 index++; 00273 uint8_t *data=(uint8_t*)&frame->id; 00274 00275 //id 00276 for(int8_t i=15 ; i>=0 ;i--){ 00277 buff[index]=data[i]; 00278 crc = One_Byte_CRC16_Calc( crc , buff[index]); 00279 index++; 00280 } 00281 00282 //message 00283 data=(uint8_t*)&frame->message; 00284 for(int8_t i=47 ; i>=0 ;i--){ 00285 buff[index]=data[i]; 00286 crc = One_Byte_CRC16_Calc( crc , buff[index]); 00287 index++; 00288 } 00289 00290 //CRC 00291 buff[index] =(uint8_t)(crc&0x00ff); 00292 index++; 00293 buff[index] =(uint8_t)(crc>>8); 00294 00295 } 00296 00297 } 00298 00299 00300 void sendComp(void){ 00301 isSend=false; 00302 } 00303 00304 void sendbuff(void){ 00305 if(isSend) return; 00306 // ledDevice(); 00307 00308 isSend=true; 00309 preambleGenerator(); 00310 for(uint16_t i=0 ; i<67 ; i++) sendByteData( buff[i] ); 00311 isSend=false; 00312 }
Generated on Wed Jul 13 2022 03:39:54 by
