APP 4
Dependencies: mbed CRC16 mbed-rtos
APP.cpp@14:9505b98c6623, 2016-02-22 (annotated)
- Committer:
- vinbel93
- Date:
- Mon Feb 22 18:06:59 2016 +0000
- Revision:
- 14:9505b98c6623
- Parent:
- 13:195826b8c61b
- Parent:
- 12:715af3660c73
- Child:
- 15:ed9511c3aac6
fixed laurent's incompetence
Who changed what in which revision?
User | Revision | Line number | New 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 | 13:195826b8c61b | 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; |
vinbel93 | 0:ac5e42371639 | 20 | |
vinbel93 | 1:f212b6676849 | 21 | int benchmark(void (*function) (void)) |
vinbel93 | 0:ac5e42371639 | 22 | { |
vinbel93 | 3:3ffa14e75b8a | 23 | int count = LPC_TIM2->TC; |
vinbel93 | 0:ac5e42371639 | 24 | function(); |
vinbel93 | 3:3ffa14e75b8a | 25 | return LPC_TIM2->TC - count; |
vinbel93 | 0:ac5e42371639 | 26 | } |
vinbel93 | 0:ac5e42371639 | 27 | |
vinbel93 | 13:195826b8c61b | 28 | extern "C" void TIMER1_IRQHandler() |
vinbel93 | 0:ac5e42371639 | 29 | { |
vinbel93 | 13:195826b8c61b | 30 | if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed |
vinbel93 | 0:ac5e42371639 | 31 | { |
vinbel93 | 13:195826b8c61b | 32 | LPC_TIM1->IR |= 1 << 0; // Clear MR0 interrupt flag |
vinbel93 | 6:3181f546e812 | 33 | clockTick = !clockTick; |
vinbel93 | 13:195826b8c61b | 34 | out = encode(message[counter] & 0x1, clockTick); |
vinbel93 | 6:3181f546e812 | 35 | |
vinbel93 | 6:3181f546e812 | 36 | if (clockTick) |
vinbel93 | 0:ac5e42371639 | 37 | { |
vinbel93 | 6:3181f546e812 | 38 | counter++; |
vinbel93 | 0:ac5e42371639 | 39 | } |
vinbel93 | 6:3181f546e812 | 40 | |
vinbel93 | 6:3181f546e812 | 41 | if (counter >= FRAMESIZE) |
vinbel93 | 0:ac5e42371639 | 42 | { |
vinbel93 | 6:3181f546e812 | 43 | counter = 0; |
vinbel93 | 0:ac5e42371639 | 44 | } |
vinbel93 | 0:ac5e42371639 | 45 | } |
vinbel93 | 0:ac5e42371639 | 46 | } |
vinbel93 | 13:195826b8c61b | 47 | |
vinbel93 | 13:195826b8c61b | 48 | extern "C" void TIMER2_IRQHandler() |
manl2003 | 10:51ee22e230c7 | 49 | { |
vinbel93 | 13:195826b8c61b | 50 | unsigned int clocks = LPC_TIM2->CR0; |
vinbel93 | 13:195826b8c61b | 51 | period = clocks; // preambule 01010101 |
manl2003 | 10:51ee22e230c7 | 52 | |
vinbel93 | 13:195826b8c61b | 53 | if (!readBufferReady) |
vinbel93 | 13:195826b8c61b | 54 | { |
vinbel93 | 13:195826b8c61b | 55 | readBuffer[0] = in.read(); |
vinbel93 | 13:195826b8c61b | 56 | } |
vinbel93 | 13:195826b8c61b | 57 | else |
vinbel93 | 13:195826b8c61b | 58 | { |
vinbel93 | 13:195826b8c61b | 59 | readBuffer[1] = in.read(); |
vinbel93 | 13:195826b8c61b | 60 | // Appel MEF |
vinbel93 | 13:195826b8c61b | 61 | bool value = decode(readBuffer[0], readBuffer[1]); |
vinbel93 | 14:9505b98c6623 | 62 | mef.ReceiveBit(value); |
vinbel93 | 13:195826b8c61b | 63 | } |
manl2003 | 10:51ee22e230c7 | 64 | |
vinbel93 | 13:195826b8c61b | 65 | LPC_TIM2->TC = 0; |
manl2003 | 10:51ee22e230c7 | 66 | |
vinbel93 | 13:195826b8c61b | 67 | LPC_TIM2->IR |= 0xFFFFFFFF; // clear Timer interrupt register |
vinbel93 | 13:195826b8c61b | 68 | } |
vinbel93 | 13:195826b8c61b | 69 | |
manl2003 | 7:733d500dbe5c | 70 | void initTimers() |
vinbel93 | 0:ac5e42371639 | 71 | { |
vinbel93 | 13:195826b8c61b | 72 | //Timer 1 (match) |
vinbel93 | 13:195826b8c61b | 73 | LPC_SC->PCLKSEL0 |= (1 << 4); // pclk = cclk timer1 |
manl2003 | 10:51ee22e230c7 | 74 | LPC_SC->PCONP |= (1 << 2); // timer1 power on |
manl2003 | 10:51ee22e230c7 | 75 | LPC_TIM1->MR0 = CLOCKS_TO_SECOND / 10; // 100 ms |
manl2003 | 10:51ee22e230c7 | 76 | LPC_TIM1->MCR = 3; // interrupt and reset control |
vinbel93 | 13:195826b8c61b | 77 | // Interrupt & reset timer on match |
vinbel93 | 13:195826b8c61b | 78 | LPC_TIM1->EMR = (3 << 4); |
vinbel93 | 13:195826b8c61b | 79 | NVIC_EnableIRQ(TIMER1_IRQn); // enable timer interrupt |
vinbel93 | 13:195826b8c61b | 80 | LPC_TIM1->TCR = 1; // enable Timer |
vinbel93 | 13:195826b8c61b | 81 | |
vinbel93 | 13:195826b8c61b | 82 | //Timer 2 (cap) |
vinbel93 | 13:195826b8c61b | 83 | LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2 |
vinbel93 | 13:195826b8c61b | 84 | LPC_SC->PCONP |= (1 << 22); // timer2 power on |
vinbel93 | 13:195826b8c61b | 85 | LPC_TIM2->TC = 0; // clear timer counter |
vinbel93 | 13:195826b8c61b | 86 | LPC_TIM2->PC = 0; // clear prescale counter |
vinbel93 | 13:195826b8c61b | 87 | LPC_TIM2->PR = 0; // clear prescale register |
vinbel93 | 13:195826b8c61b | 88 | LPC_TIM2->TCR |= (1 << 1); // reset timer |
vinbel93 | 13:195826b8c61b | 89 | LPC_TIM2->TCR &= ~(1 << 1); // release reset |
vinbel93 | 13:195826b8c61b | 90 | LPC_TIM2->IR = 0xFFFFFFFF; // clear interrupt register |
vinbel93 | 13:195826b8c61b | 91 | LPC_TIM2->CCR |= 0x0000007; // enable rising-edge and falling-edge capture on 2.0 |
vinbel93 | 13:195826b8c61b | 92 | NVIC_EnableIRQ(TIMER2_IRQn); // enable timer interrupt |
vinbel93 | 13:195826b8c61b | 93 | LPC_TIM2->TCR = 1; // start Timer |
vinbel93 | 0:ac5e42371639 | 94 | } |
vinbel93 | 0:ac5e42371639 | 95 | |
vinbel93 | 1:f212b6676849 | 96 | int main() |
vinbel93 | 0:ac5e42371639 | 97 | { |
vinbel93 | 13:195826b8c61b | 98 | //message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc); |
vinbel93 | 9:b937f9c6d682 | 99 | |
vinbel93 | 13:195826b8c61b | 100 | LPC_PINCON->PINSEL0 |= (3 << 8); // P0.4 = CAP2.0 |
manl2003 | 8:60499583959f | 101 | initTimers(); |
vinbel93 | 3:3ffa14e75b8a | 102 | |
vinbel93 | 3:3ffa14e75b8a | 103 | while (true) |
vinbel93 | 0:ac5e42371639 | 104 | { |
vinbel93 | 13:195826b8c61b | 105 | pc.printf("%i ", period); |
vinbel93 | 13:195826b8c61b | 106 | pc.printf("%i \r\n", in.read()); |
vinbel93 | 13:195826b8c61b | 107 | //pc.printf("%i ", decode(in.read(), clockTick)); |
vinbel93 | 13:195826b8c61b | 108 | wait(0.1); |
vinbel93 | 0:ac5e42371639 | 109 | } |
vinbel93 | 0:ac5e42371639 | 110 | } |
manl2003 | 12:715af3660c73 | 111 | |
vinbel93 | 14:9505b98c6623 | 112 | void _decodeCallback(bitset<FRAMESIZE> decMessage) |
manl2003 | 12:715af3660c73 | 113 | { |
vinbel93 | 14:9505b98c6623 | 114 | //decodedMessage = decMessage; |
manl2003 | 12:715af3660c73 | 115 | dataReady = true; |
manl2003 | 12:715af3660c73 | 116 | } |
manl2003 | 12:715af3660c73 | 117 | |
vinbel93 | 14:9505b98c6623 | 118 | void _decodeError() |
manl2003 | 12:715af3660c73 | 119 | { |
manl2003 | 12:715af3660c73 | 120 | frameDropped = true; |
manl2003 | 12:715af3660c73 | 121 | } |