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