APP 4

Dependencies:   mbed CRC16 mbed-rtos

Committer:
manl2003
Date:
Mon Feb 22 18:24:53 2016 +0000
Revision:
16:cf433716f8d6
Parent:
15:ed9511c3aac6
Child:
18:493a5aa7e4ec
'ti fix de Meuf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manl2003 2:1250280a511b 1 #include "APP.h"
vinbel93 3:3ffa14e75b8a 2 #include "Manchester.h"
vinbel93 9:b937f9c6d682 3 #include "Frame.h"
vinbel93 14:9505b98c6623 4 #include "MEF.h"
vinbel93 0:ac5e42371639 5
vinbel93 0:ac5e42371639 6 Serial pc(USBTX, USBRX);
vinbel93 13:195826b8c61b 7 DigitalOut out(p8);
vinbel93 13:195826b8c61b 8 DigitalIn in(p30);
vinbel93 0:ac5e42371639 9
vinbel93 6:3181f546e812 10 bool clockTick = false;
vinbel93 15:ed9511c3aac6 11 bitset<FRAMESIZE> message;// (string("1010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
manl2003 12:715af3660c73 12 bitset<MAX_DATA> decodedMessage;
manl2003 12:715af3660c73 13 bool dataReady;
manl2003 12:715af3660c73 14 bool frameDropped;
vinbel93 3:3ffa14e75b8a 15 int counter = 0;
vinbel93 13:195826b8c61b 16 unsigned int period = 0;
vinbel93 13:195826b8c61b 17 bool readBuffer[2] = {false, false};
vinbel93 13:195826b8c61b 18 bool readBufferReady = false;
vinbel93 14:9505b98c6623 19 MEF mef;
manl2003 16:cf433716f8d6 20 STATES mefSTATE;
vinbel93 0:ac5e42371639 21
vinbel93 1:f212b6676849 22 int benchmark(void (*function) (void))
vinbel93 0:ac5e42371639 23 {
vinbel93 3:3ffa14e75b8a 24 int count = LPC_TIM2->TC;
vinbel93 0:ac5e42371639 25 function();
vinbel93 3:3ffa14e75b8a 26 return LPC_TIM2->TC - count;
vinbel93 0:ac5e42371639 27 }
vinbel93 0:ac5e42371639 28
vinbel93 13:195826b8c61b 29 extern "C" void TIMER1_IRQHandler()
vinbel93 0:ac5e42371639 30 {
vinbel93 13:195826b8c61b 31 if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
vinbel93 0:ac5e42371639 32 {
vinbel93 13:195826b8c61b 33 LPC_TIM1->IR |= 1 << 0; // Clear MR0 interrupt flag
vinbel93 6:3181f546e812 34 clockTick = !clockTick;
vinbel93 13:195826b8c61b 35 out = encode(message[counter] & 0x1, clockTick);
vinbel93 6:3181f546e812 36
vinbel93 6:3181f546e812 37 if (clockTick)
vinbel93 0:ac5e42371639 38 {
vinbel93 6:3181f546e812 39 counter++;
vinbel93 0:ac5e42371639 40 }
vinbel93 6:3181f546e812 41
vinbel93 6:3181f546e812 42 if (counter >= FRAMESIZE)
vinbel93 0:ac5e42371639 43 {
vinbel93 6:3181f546e812 44 counter = 0;
vinbel93 0:ac5e42371639 45 }
vinbel93 0:ac5e42371639 46 }
vinbel93 0:ac5e42371639 47 }
vinbel93 13:195826b8c61b 48
vinbel93 13:195826b8c61b 49 extern "C" void TIMER2_IRQHandler()
manl2003 10:51ee22e230c7 50 {
vinbel93 13:195826b8c61b 51 unsigned int clocks = LPC_TIM2->CR0;
vinbel93 13:195826b8c61b 52 period = clocks; // preambule 01010101
manl2003 10:51ee22e230c7 53
vinbel93 13:195826b8c61b 54 if (!readBufferReady)
vinbel93 13:195826b8c61b 55 {
vinbel93 13:195826b8c61b 56 readBuffer[0] = in.read();
vinbel93 13:195826b8c61b 57 }
vinbel93 13:195826b8c61b 58 else
vinbel93 13:195826b8c61b 59 {
vinbel93 13:195826b8c61b 60 readBuffer[1] = in.read();
vinbel93 13:195826b8c61b 61 // Appel MEF
vinbel93 13:195826b8c61b 62 bool value = decode(readBuffer[0], readBuffer[1]);
vinbel93 14:9505b98c6623 63 mef.ReceiveBit(value);
vinbel93 13:195826b8c61b 64 }
manl2003 10:51ee22e230c7 65
vinbel93 13:195826b8c61b 66 LPC_TIM2->TC = 0;
manl2003 10:51ee22e230c7 67
vinbel93 13:195826b8c61b 68 LPC_TIM2->IR |= 0xFFFFFFFF; // clear Timer interrupt register
vinbel93 13:195826b8c61b 69 }
vinbel93 13:195826b8c61b 70
manl2003 7:733d500dbe5c 71 void initTimers()
vinbel93 0:ac5e42371639 72 {
vinbel93 13:195826b8c61b 73 //Timer 1 (match)
vinbel93 13:195826b8c61b 74 LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1
manl2003 10:51ee22e230c7 75 LPC_SC->PCONP |= (1 << 2); // timer1 power on
manl2003 10:51ee22e230c7 76 LPC_TIM1->MR0 = CLOCKS_TO_SECOND / 10; // 100 ms
manl2003 10:51ee22e230c7 77 LPC_TIM1->MCR = 3; // interrupt and reset control
vinbel93 13:195826b8c61b 78 // Interrupt & reset timer on match
vinbel93 13:195826b8c61b 79 LPC_TIM1->EMR = (3 << 4);
vinbel93 13:195826b8c61b 80 NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt
vinbel93 13:195826b8c61b 81 LPC_TIM1->TCR = 1; // enable Timer
vinbel93 13:195826b8c61b 82
vinbel93 13:195826b8c61b 83 //Timer 2 (cap)
vinbel93 13:195826b8c61b 84 LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2
vinbel93 13:195826b8c61b 85 LPC_SC->PCONP |= (1 << 22); // timer2 power on
vinbel93 13:195826b8c61b 86 LPC_TIM2->TC = 0; // clear timer counter
vinbel93 13:195826b8c61b 87 LPC_TIM2->PC = 0; // clear prescale counter
vinbel93 13:195826b8c61b 88 LPC_TIM2->PR = 0; // clear prescale register
vinbel93 13:195826b8c61b 89 LPC_TIM2->TCR |= (1 << 1); // reset timer
vinbel93 13:195826b8c61b 90 LPC_TIM2->TCR &= ~(1 << 1); // release reset
vinbel93 13:195826b8c61b 91 LPC_TIM2->IR = 0xFFFFFFFF; // clear interrupt register
vinbel93 13:195826b8c61b 92 LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture on 2.0
vinbel93 13:195826b8c61b 93 NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt
vinbel93 13:195826b8c61b 94 LPC_TIM2->TCR = 1; // start Timer
vinbel93 0:ac5e42371639 95 }
vinbel93 0:ac5e42371639 96
vinbel93 1:f212b6676849 97 int main()
vinbel93 0:ac5e42371639 98 {
vinbel93 15:ed9511c3aac6 99 message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc);
vinbel93 9:b937f9c6d682 100
vinbel93 13:195826b8c61b 101 LPC_PINCON->PINSEL0 |= (3 << 8); // P0.4 = CAP2.0
manl2003 8:60499583959f 102 initTimers();
vinbel93 3:3ffa14e75b8a 103
vinbel93 3:3ffa14e75b8a 104 while (true)
vinbel93 0:ac5e42371639 105 {
vinbel93 15:ed9511c3aac6 106 if (dataReady)
vinbel93 15:ed9511c3aac6 107 {
vinbel93 15:ed9511c3aac6 108 pc.printf("ASDF");
vinbel93 15:ed9511c3aac6 109 }
vinbel93 13:195826b8c61b 110 pc.printf("%i ", period);
vinbel93 13:195826b8c61b 111 pc.printf("%i \r\n", in.read());
vinbel93 13:195826b8c61b 112 //pc.printf("%i ", decode(in.read(), clockTick));
vinbel93 13:195826b8c61b 113 wait(0.1);
vinbel93 0:ac5e42371639 114 }
vinbel93 0:ac5e42371639 115 }
manl2003 12:715af3660c73 116
vinbel93 15:ed9511c3aac6 117 void _decodeCallback(bitset<MAX_DATA> decMessage)
manl2003 12:715af3660c73 118 {
vinbel93 15:ed9511c3aac6 119 decodedMessage = decMessage;
manl2003 12:715af3660c73 120 dataReady = true;
manl2003 12:715af3660c73 121 }
manl2003 12:715af3660c73 122
vinbel93 14:9505b98c6623 123 void _decodeError()
manl2003 12:715af3660c73 124 {
manl2003 12:715af3660c73 125 frameDropped = true;
manl2003 16:cf433716f8d6 126 }
manl2003 16:cf433716f8d6 127
manl2003 16:cf433716f8d6 128
manl2003 16:cf433716f8d6 129 void _updateState(STATES state)
manl2003 16:cf433716f8d6 130 {
manl2003 16:cf433716f8d6 131 mefSTATE = state;
manl2003 12:715af3660c73 132 }