![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
テストモード追加、LED有効化 left -> SerialRX, Data Recieve Complete , Serial Tx , Light Tx
Dependencies: XBee mbed NetServicesMin
Diff: frame_layer/layer_frame.cpp
- Revision:
- 0:42adca80439c
- Child:
- 1:36eb173eb52b
diff -r 000000000000 -r 42adca80439c frame_layer/layer_frame.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/frame_layer/layer_frame.cpp Thu Mar 22 12:40:48 2012 +0000 @@ -0,0 +1,262 @@ +#include "mbed.h" +#include "types.h" +#include "config.h" +#include "culc_crc16.h" +#include "layer_frame.h" + +#include "leds.h" +#include "serialRecieve.h" + + +volatile int symbolCode; +volatile int pulseCounter; +volatile int pulseState; +volatile bool isSend; + +volatile bool isSendContinueous; +volatile bool isSendContinued; + +Payload sendedPayload; +uint16_t sendedFrameNumber; + +volatile uint8_t buff[70]; +volatile uint8_t buffIndex; + +volatile float txMax; +volatile float txMin; +volatile float txMid; + +//Ticker carrierWave; +AnalogOut dac(p18); + +void setTxLevel(uint8_t _gain , uint8_t _mid) +{ + + + if( (_mid > 10) || (_mid <1) ) return; + if(_gain > 10) return; + + if(_gain == 0){ + txMax = 0; + txMin = 0; + txMid = 0; + return; + } + + float g = (float)_gain*0.1; + float mid = (float)_mid*0.1; + + txMax = kDAC_GAIN_MAX * g; + txMin = kDAC_GAIN_MIN * g; + txMid = mid * g; +} + +void timer1_init(void) +{ + LPC_SC->PCLKSEL0 &= ~(3UL << 4); //clear bits 96MHz + LPC_SC->PCLKSEL0 |= (1UL << 4); //set bit + + LPC_SC->PCONP |=1 << 2; //timer1 power on + LPC_TIM1->MR0 = kTImerCount; //1/96MHz = 0.010416666 us :( 1/(0.010416666 us * 1667) * 2) = 28.794kHz + LPC_TIM1->MCR = 3; //interrupt and reset control + //3 = Interrupt & reset timer1 on match + //1 = Interrupt only, no reset of timer1 +// NVIC_EnableIRQ(TIMER1_IRQn); //enable timer1 interrupt +// LPC_TIM1->TCR = 1; //enable Timer1 + enableTimer(); +} + +void enableTimer(void) +{ + LPC_TIM1->MR0 = kTImerCount; + NVIC_EnableIRQ(TIMER1_IRQn); + LPC_TIM1->TCR = 1; //enable Timer1 +} +void disableTimer(void) +{ + NVIC_DisableIRQ(TIMER1_IRQn); + LPC_TIM1->TCR = 0; //disable Timer1 +} + +void preambleGenerator(){ + + symbolCode = 4; + while( pulseCounter <=77 ) {readProcess();} + pulseCounter = 0; +} + +void sendByteData(uint8_t _data){ +// printf("%02X ",_data); + pulseCounter = 0; + + symbolCode = (_data & 0x03); + while( pulseCounter < 24 ) {readProcess();} + pulseCounter = 0; + + symbolCode = ( (_data & 0x0C ) >> 2); + while( pulseCounter < 24 ) {readProcess();} + pulseCounter = 0; + + symbolCode = ( (_data & 0x30 ) >> 4); + while( pulseCounter < 24 ) {readProcess();} + pulseCounter = 0; + + symbolCode = ( (_data & 0xC0 ) >> 6); + while( pulseCounter < 24 ){readProcess();} + pulseCounter = 0; + + + +} + + +void pulseGenerator(){ + + if(pulseState == -1) pulseState = 1; + else pulseState = -1; + + + if(pulseState<0) dac = txMax; + else dac = txMin; + +} +extern "C" void TIMER1_IRQHandler (void) +{ + if((LPC_TIM1->IR & 0x01) != 0x01) return; // if MR0 interrupt, proceed + + LPC_TIM1->IR |= 1 << 0; // Clear MR0 interrupt flag + + pulseCounter++; + + + switch(symbolCode){ + + case 0: + { + if( pulseCounter <= 6) pulseGenerator(); + else dac=txMid; + } + break; + + case 2: + { + if( (pulseCounter > 6)&(pulseCounter <= 12) ) pulseGenerator(); + else dac=txMid; + } + break; + + case 1: + { + if( (pulseCounter > 12)&(pulseCounter <= 18) ) pulseGenerator(); + else dac=txMid; + } + break; + + case 3: + { + if( (pulseCounter > 18)&(pulseCounter <= 24 )) pulseGenerator(); + else dac=txMid; + } + break; + + case 4: + { + if( pulseCounter <= 18) pulseGenerator(); + else dac=txMid; + } + break; + + } + + +} + + + void initCarrierWave(){ + dac = kDAC_OFF; + + pulseCounter=0; + symbolCode = 0; + pulseState = -1; + + setTxLevel( kTxGain , kTxMidLevel ); + + isSend=false; + isSendContinueous=false; + sendedFrameNumber=0xffff; + timer1_init(); + + initPayload(&sendedPayload); +} + + +void sendPayload(Payload *_payload){ + + if(isSend) return; + + Frame *frame=&_payload->frame; + + if( frame->message.frameNumber != sendedFrameNumber ){ + + sendedFrameNumber = frame->message.frameNumber; + + memcpy(&sendedPayload,_payload,sizeof(Payload)); + + uint16_t crc = 0xffff; + uint8_t index=0; + + //frame type + buff[index]=_payload->ftype; + crc = One_Byte_CRC16_Calc( crc , buff[index]); + + index++; + uint8_t *tmp=(uint8_t*)&frame->id; + + //id + for(int8_t i=15 ; i>=0 ;i--){ + buff[index]=tmp[i]; + crc = One_Byte_CRC16_Calc( crc , buff[index]); + index++; + } + + //message + tmp=(uint8_t*)&frame->message; + for(int8_t i=47 ; i>=0 ;i--){ + buff[index]=tmp[i]; + crc = One_Byte_CRC16_Calc( crc , buff[index]); + index++; + } + + //CRC + buff[index] =(uint8_t)(crc&0x00ff); + index++; + buff[index] =(uint8_t)(crc>>8); + + } +// for(int i=0;i<67;i++) printf("%02X ",buff[i]); +// printf("\r\n"); + ledDevice(); + + for(uint8_t i=0 ; i<kContSendNum ; i++) + { + preambleGenerator(); + + for(uint16_t i=0 ; i<67 ; i++) sendByteData( buff[i] ); +// printf("\r\n+++++++++\r\n"); + } + + isSend=false; + +} + + +void swapPayloadData(Payload *_payload) +{ + uint8_t *org=(uint8_t*)_payload; + uint8_t *tmp=(uint8_t*)&sendedPayload; + + uint8_t i; + for(i=0 ; i<16 ;i++) tmp[i+2]=org[17-i]; + for(i=0 ; i<48 ;i++) tmp[i+18]=org[65-i]; +} +