Legen Dary / Mbed 2 deprecated APP4

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "trame.h"
00004 #include "LPC17xx.h"
00005 
00006 #define SIGNAL_READ 0x23
00007 
00008 DigitalOut led1(LED1);
00009 DigitalOut led2(LED2);
00010 DigitalOut led3(LED3);
00011 DigitalOut led4(LED4);
00012 
00013 DigitalOut d_out(p9);
00014 DigitalIn d_in(p30);
00015 Thread* debugTH;
00016 
00017 Serial pc(USBTX, USBRX);
00018 
00019 const int PERIODE = 9600000;
00020 
00021 //Variables for MEF
00022 enum states
00023 {
00024     START           = 0,
00025     ENTETE          = 1,
00026     CHARGE_UTILE    = 2,
00027     CONTROLE        = 3,
00028     END             = 4
00029 };
00030 
00031 int state;
00032 int cntState = 0;
00033 bool nextBit;
00034 bitset<16> buffer;
00035 bitset<640> chargeUtile;
00036 int LENGTH_CHARGE_UTILE_BIT;
00037 int LENGTH_CHARGE_UTILE_BYTE;
00038 char c_chargeUtile[80];
00039 unsigned short CRC_Calculated;
00040 unsigned short CRC_Received;
00041 bool messageReady;
00042 
00043 //Variables for send
00044 Thread* frame_sender_Th;
00045 Mail<trame, 8> OutGoingMail;
00046 bool increment_c = false;
00047 bool sending = false;
00048 bool tclock;
00049 bitset<696> *outGoingTrame;
00050 int outGoingLength = 0;
00051 int count = 0;
00052 
00053 //Variables for receive
00054 struct inboundChargeMail
00055 {
00056     char* charge;
00057     int length;
00058 };
00059 Mail<inboundChargeMail, 8> inboundChargeUtile;
00060 int sync = 0;
00061 bool gbitValueOk = 1;
00062 bool in_val;
00063 int rcv_count = 0;
00064 unsigned long tick_count_read;
00065 bool state_adjust = true;
00066 unsigned long period = 0;
00067 unsigned long tc_periods[8] = {0};
00068 bool rcv_buff[696];
00069 
00070 void inputReceiver( void const *args )
00071 {
00072     int c_count;
00073     char temp_c;
00074     string message = "";
00075     
00076     while( true )
00077     {
00078         temp_c = pc.getc();
00079         
00080         if( temp_c == 0xD )
00081         {
00082             if( !message.empty() )
00083             {
00084                pc.putc( 0xA );
00085                pc.putc( 0xD );
00086                std::printf("debug : message sent to mailbox : %s\r\n", message.c_str() );
00087                OutGoingMail.put( new trame( message ) );
00088                message = "";
00089                c_count = 0;
00090            }
00091         }
00092         else if( temp_c == 0x8 )
00093         {
00094             pc.putc( temp_c );
00095             pc.putc( 0x20 );
00096             pc.putc( temp_c );
00097             message = message.substr(0, message.length() - 1);
00098             c_count--;
00099         }
00100         else
00101         {
00102             if( c_count < 80 )
00103             {
00104                 pc.putc( temp_c );
00105                 message += temp_c;
00106                 c_count++;
00107             }
00108         }
00109     }
00110 }
00111 
00112 bool analyze_state(bool bit)
00113 {
00114     switch(state)
00115     {
00116         case START:
00117         
00118             std::printf("START\t");
00119             
00120             if(cntState == 0 && bit == 0)
00121             {
00122                 nextBit = 1;
00123                 cntState++;
00124             }
00125             else if (cntState < 6 && bit == nextBit)
00126             {
00127                 cntState++;
00128             }
00129             else if (cntState == 6 && bit == nextBit)
00130             {
00131                 nextBit = 0;
00132                 cntState++;
00133             }
00134             else if (cntState == 7 && bit == nextBit)
00135             {
00136                 state = ENTETE;
00137                 cntState = 0;
00138                 nextBit = 0;
00139             }
00140             else
00141             {
00142                 state = START;
00143                 return 0;
00144             }
00145             
00146             break;
00147            
00148         case ENTETE:
00149         
00150             if(cntState < 7 && nextBit == 0)
00151             {
00152                 std::printf("TEST1: %i \t", cntState);
00153                 cntState++;
00154                 
00155             }
00156             else if (cntState == 7 && nextBit == 0)
00157             {
00158                 std::printf("TEST2: %i \t", cntState);
00159                 nextBit = 1;
00160             }
00161             else if (nextBit == 1 && cntState > 0)
00162             {
00163                 std::printf("TEST3: %i \t", cntState);
00164                 buffer.set(cntState, bit);
00165                 cntState--;
00166             }
00167             else if (nextBit == 1 && cntState == 0)
00168             { 
00169                 buffer.set(cntState, bit);
00170                 state = CHARGE_UTILE;
00171                 LENGTH_CHARGE_UTILE_BIT = (int)(buffer.to_ulong());
00172                 
00173                 cntState = LENGTH_CHARGE_UTILE_BIT - 1;
00174             }
00175             else
00176             {
00177                 state = START;
00178                 return 0;
00179             }
00180             
00181             break;
00182            
00183         case CHARGE_UTILE:
00184             
00185             std::printf("CHARGE UTILE\t");
00186             
00187             if (cntState > 0)
00188             {
00189                chargeUtile.set(cntState, bit); 
00190                cntState--;
00191             }
00192             else if (cntState == 0)
00193             {
00194                 chargeUtile.set(cntState, bit); 
00195                 cntState = 0;
00196                 state = CONTROLE;
00197                 nextBit = 0;
00198             }
00199             else
00200             {
00201                 state = START;
00202                 return 0;
00203             }
00204             
00205             break;
00206            
00207         case CONTROLE:
00208             
00209             if(nextBit == 0)
00210             {
00211                 LENGTH_CHARGE_UTILE_BYTE = LENGTH_CHARGE_UTILE_BIT / 8;
00212                 
00213                 for (int i = LENGTH_CHARGE_UTILE_BIT-1; i >= 0; i--)
00214                 {
00215                     std::printf("%i", (chargeUtile[i] & 0x1));
00216                     if (i % 8 == 0)
00217                         c_chargeUtile[ ( ( LENGTH_CHARGE_UTILE_BIT - 1 ) - i ) / 8] = (chargeUtile[i] << 7) | (chargeUtile[i + 1] << 6) | (chargeUtile[i + 2] << 5) | (chargeUtile[i + 3] << 4) | (chargeUtile[i + 4] << 3) | (chargeUtile[i + 5] << 2) | (chargeUtile[i + 6] << 1) | (chargeUtile[i + 7] << 0);
00218                 }
00219                 
00220                 CRC_Calculated = CRC16::calculateCRC16(c_chargeUtile, LENGTH_CHARGE_UTILE_BYTE);
00221                 
00222                 std::printf("CRC Calculated: %hu\t", CRC_Calculated);
00223                 
00224                 nextBit = 1;
00225                 cntState = 14;
00226                 buffer.reset();
00227                 buffer.set(15, bit);
00228             }
00229             else if (nextBit == 1 && cntState > 0)
00230             {
00231                 buffer.set(cntState, bit);
00232                 cntState--;
00233             }
00234             else if (nextBit == 1 && cntState == 0)
00235             {
00236                 buffer.set(cntState, bit);
00237                 CRC_Received = (unsigned short)buffer.to_ulong();
00238                 
00239                 std::printf("CRC Received: %hu\t", CRC_Received);
00240                 
00241                 if (CRC_Received == CRC_Calculated)
00242                 {
00243                     state = END;
00244                     nextBit = 0;
00245                     cntState = 0;
00246                 }
00247                 else
00248                 {
00249                     state = START;
00250                     return 0;
00251                 }
00252             }
00253             else
00254             {
00255                 state = START;
00256                 return 0;
00257             }
00258             
00259             break;
00260            
00261         case END:
00262         
00263             if (nextBit == 0 && nextBit == bit && cntState == 0)
00264             {
00265                 nextBit = 1;
00266                 cntState++;
00267             }
00268             else if (nextBit == 1 && nextBit == bit && cntState < 6)
00269             {
00270                 cntState++;
00271             }
00272             else if (nextBit == 1 && nextBit == bit && cntState == 6)
00273             {
00274                 nextBit = 0;
00275                 cntState++;
00276             }
00277             else if (nextBit == 0 && nextBit == bit && cntState == 7)
00278             {
00279                 messageReady = true;
00280                 state = START;
00281             }
00282             else
00283             {
00284                 state = START;
00285                 messageReady = false;
00286                 return 0;
00287             }
00288         
00289             break;
00290     }
00291     
00292     return 1;
00293 }
00294 
00295 void read()
00296 {
00297     in_val = d_in.read();
00298     if( sync < 8 )
00299     {
00300         if( in_val || sync > 0 )
00301         {
00302             tc_periods[sync] = tick_count_read;
00303             sync++;
00304             
00305             if (sync == 8)
00306             {
00307                 for (int i = 1; i < sync; i++)
00308                 {
00309                    period += tc_periods[i]; 
00310                 }
00311                 period = (period/7)/2;
00312             }
00313             debugTH->signal_set(0x1);
00314         }
00315     }
00316     else
00317     {
00318         debugTH->signal_set(0x1);
00319         if( tick_count_read > period * 1.5 )
00320         {
00321             rcv_buff[ rcv_count ] = !in_val;
00322             gbitValueOk = analyze_state( !in_val );
00323             rcv_count++;
00324         }
00325         else
00326         {
00327             if( state_adjust )
00328             {
00329                 state_adjust = !state_adjust;
00330             }
00331             else
00332             {
00333                 state_adjust = !state_adjust;
00334                 rcv_buff[ rcv_count ] = !in_val;
00335                 gbitValueOk = analyze_state( !in_val );
00336                 rcv_count++;
00337             }
00338         }
00339         
00340     }
00341     if( messageReady )
00342     {
00343         inboundChargeMail *mail_t = new inboundChargeMail;
00344         char ctemp[ 80 ];
00345         strcpy( ctemp, c_chargeUtile );
00346         mail_t->charge = ctemp;
00347         mail_t->length = LENGTH_CHARGE_UTILE_BYTE;
00348         inboundChargeUtile.put( mail_t );
00349         
00350         rcv_count = 0;
00351         state_adjust = true;
00352         sync = 0;
00353         messageReady = false;
00354         cntState = 0;
00355     }
00356     if( !gbitValueOk )
00357     {
00358         rcv_count = 0;
00359         state_adjust = true;
00360         sync = 0;
00361         cntState = 0;
00362     }
00363     LPC_TIM2->TC = 0;
00364     LPC_TIM2->IR |= 0xFFFFFFFF;
00365 }
00366 
00367 extern "C" void TIMER2_IRQHandler()
00368 {
00369     tick_count_read = LPC_TIM2->CR0;
00370     //read();
00371     LPC_TIM2->TC = 0;
00372     LPC_TIM2->IR |= 0xFFFFFFFF;
00373 }
00374 
00375 extern "C" void TIMER1_IRQHandler( void )
00376 {
00377     if ((LPC_TIM1->IR & 0x01) == 0x01)
00378     {
00379         if( sending )
00380         {
00381             tclock = !tclock;
00382             if( count < outGoingLength )
00383             {
00384                 d_out = ( ( *outGoingTrame )[ count ] == tclock );
00385                 if(tclock)
00386                 {
00387                     count++;
00388                 }
00389                 LPC_TIM1->IR |= 1;
00390             }
00391             
00392             if( count == outGoingLength ){
00393                 frame_sender_Th->signal_set(0x3);
00394                 count = 0;
00395                 sending = false;
00396             }
00397             debugTH->signal_set(0x1);
00398         }
00399         LPC_TIM1->IR |= 1 << 0;
00400     }
00401 }
00402 
00403 void debug( void const *args )
00404 {
00405     while( true )
00406     {
00407         Thread::signal_wait(0x1);
00408         int tempi1;
00409         if(tclock)
00410         {
00411             tempi1 = d_out;
00412             std::printf( "%d", tempi1 & 0x1 );
00413             std::printf( "%d", in_val & 0x1 );
00414             std::printf( "  %d", count );
00415             std::printf( "  %d", period );
00416             std::printf( "  %d\r\n", tick_count_read );
00417         }
00418     }
00419 }
00420 
00421 void send( trame *trm )
00422 {
00423     outGoingTrame = trm->getBitset();
00424     outGoingLength = trm->length;
00425     std::printf("debug : Sending \r\n");
00426 }
00427 
00428 void frame_sender( void const *args )
00429 {
00430     while( true )
00431     {
00432         osEvent evt = OutGoingMail.get();
00433  
00434         if( evt.status == osEventMail ) 
00435         {
00436             trame *trm = ( trame* )evt.value.p;
00437  
00438             std::printf( "frame sender thread\r\n" );
00439             send( trm );
00440             sending = true;
00441             Thread::signal_wait(0x3);
00442             sending = false;
00443             count = 0;
00444             OutGoingMail.free( trm );
00445             std::printf( "frame sender thread Over \r\n" );
00446         }
00447     }
00448 }
00449 
00450 void frame_printer( void const *args )
00451 {
00452     while( true )
00453     {
00454         osEvent evt = inboundChargeUtile.get();
00455         
00456         if( evt.status == osEventMail )
00457         {
00458             inboundChargeMail* mailed_charge = ( inboundChargeMail* )evt.value.p;
00459             std::printf( "Received frame :\r\n%s", mailed_charge->charge );
00460             inboundChargeUtile.free( mailed_charge );
00461         }
00462     }
00463 }
00464 
00465 void init_timer1()
00466 {
00467     LPC_SC->PCLKSEL0 |= ( 1 << 4 ); //Select Timer 1 = CCLK
00468     LPC_SC->PCONP |= ( 1 << 2 );    //Power-Up Timer 1
00469     LPC_TIM1->MR0 = PERIODE;        //Set ticks till interrupt
00470     LPC_TIM1->MCR = 3;              //Interrupt on MR0 and Reset TC on MR0
00471     LPC_TIM1->EMR = (3 << 4);
00472     NVIC_EnableIRQ(TIMER1_IRQn);    //Bind timer 1 to custom funk
00473     LPC_TIM1->TCR = 1;              //Enable Timer 1
00474 }
00475 
00476 void init_timer2()
00477 {
00478     LPC_PINCON->PINSEL0 |= ( 3 << 8 ); //Select Capture register 2.0
00479     LPC_SC->PCLKSEL1 |= ( 1 << 12 ); //Select Timer 2 = CCLK
00480     LPC_SC->PCONP |= ( 1 << 22 ); //Power-Up Timer 2
00481     LPC_TIM2->TC = 0;
00482     LPC_TIM2->PC = 0;
00483     LPC_TIM2->PR = 0;
00484     LPC_TIM2->TCR |= ( 1 << 1 );
00485     LPC_TIM2->TCR &= ~( 1 << 1 );
00486     LPC_TIM2->IR = 0xFFFFFFFF;
00487     LPC_TIM2->CCR |= 0x00000007;
00488     NVIC_EnableIRQ(TIMER2_IRQn);
00489     LPC_TIM2->TCR = 1;
00490 }
00491 
00492 
00493 int main() 
00494 {
00495     d_out = 0;
00496     std::printf("Init Timers...");
00497     tclock = 1;
00498     state = START;
00499     messageReady = false;
00500     
00501     init_timer1();
00502     init_timer2();
00503     
00504     std::printf("done\r\n");
00505     
00506     std::printf("Init Threads...");
00507     debugTH = new Thread(debug);
00508     Thread fp( frame_printer );
00509     Thread ir( inputReceiver );
00510     frame_sender_Th = new Thread( frame_sender );
00511     std::printf("done\r\n");
00512     
00513     while( true )
00514     {
00515         wait(50000);
00516     }
00517 }