![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
Diff: main.cpp
- Revision:
- 25:d0c348c3c85d
- Parent:
- 24:2034362db66f
- Child:
- 26:6ffbf3161f6f
diff -r 2034362db66f -r d0c348c3c85d main.cpp --- a/main.cpp Tue Mar 07 08:52:38 2017 +0000 +++ b/main.cpp Tue Mar 07 17:25:16 2017 +0000 @@ -1,9 +1,7 @@ #include "CRC16.h" #include "mbed.h" #include "rtos.h" -#include <vector> -using std::vector; -DigitalIn in(p30); + Thread ThreadLecture; Thread ThreadWrite; const int PREAMBULE = 0b01010101; @@ -13,6 +11,11 @@ DigitalOut out(p13); DigitalOut myled1(LED1); DigitalOut myled2(LED2); +InterruptIn interrupt_p30(p30); +CRC16 myCRC; +Timer t; +int periode = 0; +char sync = 0; bool transmit = false; //faux pour le debut de demi-periode d'envoie d'un bit, vrai pour la 2ème demi-periode char trame_sent[80]; //tableau de la trame à envoyer @@ -30,7 +33,7 @@ //Envoie de données bit par bit void send_data() -{ +{ out = codeManchester(((trame_sent[byte_sent_pos] >> bit_sent) & 0x01), bTimer1); if(bTimer1) { @@ -50,6 +53,103 @@ } } +char messageBuffer[80]; +char messageLength = 0; +char lastpos = 0; +void show_message() +{ + while (true) + { + ThreadLecture.signal_wait(1); + for (int i = lastpos; i < messageLength; i++) + { + pc.printf("%x",messageBuffer[i]); + + if ((i % 11) == 0 && i > 0) + { + pc.printf("\n"); + } + } + lastpos = messageLength; + } +} + +char byte = 0; +char shift = 0; +char totalsize = 7; +void resetMessage() +{ + byte = 0; + shift = 0; + totalsize = 7; + messageLength = 0; + lastpos = 0; +} + +void read(bool bit) +{ + int time = t.read_us(); + if (sync == 1) + { + periode = time; + } + sync++; + + if (sync > 1 && (time*1.5 >= periode)) + { + messageBuffer[messageLength] = (messageBuffer[messageLength] << 1) + bit; + } + + shift++; + if (shift == 8) + { + shift = 0; + + messageLength++; + + // debug + ThreadLecture.signal_set(1); + + // Validations de base + if (messageLength == 2 && messageBuffer[1] != START) + { + resetMessage(); + } + + if (messageLength == 4) + { + totalsize = 7 + messageBuffer[3]; + } + + // fin + if (totalsize == messageLength) + { + // Calcul du CRC + unsigned short currentCRC = messageBuffer[totalsize-2] + messageBuffer[totalsize-3]<<8; + + if (currentCRC == myCRC.calculateCRC16(messageBuffer+4, totalsize-7) && messageBuffer[totalsize-1] == END) + { + // Affiche à l'écran le message valide + ThreadLecture.signal_set(1); + } + resetMessage(); + } + } + + // reset le timer pour mesurer la période + t.reset(); +} + +void rise() +{ + read(0); +} + +void fall() +{ + read(1); +} + extern "C" void TIMER1_IRQHandler() { LPC_TIM1->TC = 0; @@ -62,41 +162,9 @@ } } -int PeriodLength = 0; -bool bReceivedFirstBit = false; -bool bInvert = false; -extern "C" void TIMER2_IRQHandler() -{ - LPC_TIM2->TC = 0; - LPC_TIM2->IR |= 0xFFFFFFFF; // clear - - myled2 = !myled2; - if (PeriodLength > 0) - { - if (LPC_TIM2->CR0 >= PeriodLength) - { - bInvert = false; - } - else - { - bInvert = true; - } - } - else if (bReceivedFirstBit) - { - PeriodLength = (LPC_TIM2->CR0 / 2) * 1.5; - } - else - { - bReceivedFirstBit = true; - } - ThreadLecture.signal_set(1); -} - //création de la trame void create_trame(char message[],unsigned char taille) { - CRC16 myCRC; unsigned short resultCRC = myCRC.calculateCRC16(message,taille); trame_sent[0] = PREAMBULE; //Preambule @@ -132,7 +200,7 @@ bTimer1 = false; LPC_TIM1->TCR = 1; // enable and reset Timer - wait_ms(1000); + wait_ms(5000); } /*pc.printf("\n\rYour text : "); unsigned char count = 0; @@ -147,81 +215,6 @@ pc.printf("get_text();\n");*/ } -void read() -{ - char byte = 0; - vector<char> bytes; - char shift = 0; - char totalsize = 7; - CRC16 mycrc16; - while (true) - { - ThreadLecture.signal_wait(1); - - if (bInvert) - { - byte = (byte << 1) + !in; - } - else - { - byte = (byte << 1) + in; - } - - shift++; - if (shift == 8) - { - shift = 0; - - // à partir d'ici, je travaille en byte et non bit - bytes.push_back(byte); - - // Validations de base - if ((bytes.size() == 1 && bytes[0] != PREAMBULE) || - (bytes.size() == 2 && bytes[1] != START)) - { - if (bytes.size() == 1) - { - pc.printf("[DEBUG] - Invalid PREAMBULE : %x\n", bytes[0]); - } - else - { - pc.printf("[DEBUG] - Invalid START : %x\n", bytes[1]); - } - bytes.clear(); - } - if (bytes.size() == 3) - { - pc.printf("[DEBUG] - YESSSSSSSSSSSSSSSSSSSS\n"); - } - - if (bytes.size() == 4) - { - totalsize = 7 + bytes[3]; - } - - // fin - if (totalsize == bytes.size()) - { - // Calcul du CRC - unsigned short currentCRC = bytes[bytes.size()-2] + bytes[bytes.size()-3]<<8; - - vector<char> charge_utile(&bytes[4], &bytes[bytes.size()-4]); - - if (currentCRC == mycrc16.calculateCRC16(&charge_utile[0], charge_utile.size()) && bytes.back() == END) - { - // Affiche à l'écran le message valide - pc.printf(&charge_utile[0], charge_utile.size()); - } - else - { - pc.printf("[DEBUG] - Invalid CRC, dumping message\n"); - } - bytes.clear(); - } - } - } -} - void initTimers() { //Timer 1 (match) @@ -231,30 +224,21 @@ LPC_TIM1->TC = 0; // clear timer counter LPC_TIM1->PC = 0; // clear prescale counter LPC_TIM1->PR = 0; // clear prescale register - LPC_TIM1->MR0 = (960000 / 2) * 10; // 10/2 ms + LPC_TIM1->MR0 = (960000); // 10 ms LPC_TIM1->EMR = (3 << 4); // Interrupt & reset timer on match NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt LPC_TIM1->TCR = 0; // disable Timer - - //Timer 2 (cap) - LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2 - LPC_SC->PCONP |= (1 << 22); // timer2 power on - LPC_TIM2->TC = 0; // clear timer counter - LPC_TIM2->PC = 0; // clear prescale counter - LPC_TIM2->PR = 0; // clear prescale register - LPC_TIM2->TCR |= (1 << 1); // reset timer - LPC_TIM2->TCR &= ~(1 << 1); // release reset - LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture - NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt - LPC_TIM2->TCR = 1; // start Timer } int main() { LPC_PINCON->PINSEL0 |= (3 << 8); // pin30 LPC_PINCON->PINMODE1 |= 3; initTimers(); - ThreadLecture.start(read); + ThreadLecture.start(show_message); ThreadWrite.start(get_text); + interrupt_p30.fall(fall); + interrupt_p30.rise(rise); + t.start(); while(true) {