APP 4

Dependencies:   mbed CRC16 mbed-rtos

APP.cpp

Committer:
vinbel93
Date:
2016-02-22
Revision:
14:9505b98c6623
Parent:
13:195826b8c61b
Parent:
12:715af3660c73
Child:
15:ed9511c3aac6

File content as of revision 14:9505b98c6623:

#include "APP.h"
#include "Manchester.h"
#include "Frame.h"
#include "MEF.h"

Serial pc(USBTX, USBRX);
DigitalOut out(p8);
DigitalIn in(p30);

bool clockTick = false;
bitset<FRAMESIZE> message (string("1010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
bitset<MAX_DATA> decodedMessage;
bool dataReady;
bool frameDropped; 
int counter = 0;
unsigned int period = 0;
bool readBuffer[2] = {false, false};
bool readBufferReady = false;
MEF mef;

int benchmark(void (*function) (void))
{
    int count = LPC_TIM2->TC;
    function();
    return LPC_TIM2->TC - count;
}

extern "C" void TIMER1_IRQHandler()
{
    if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
    {
        LPC_TIM1->IR |= 1 << 0;        // Clear MR0 interrupt flag
        clockTick = !clockTick;
        out = encode(message[counter] & 0x1, clockTick);

        if (clockTick)
        {
            counter++;
        }

        if (counter >= FRAMESIZE)
        {
            counter = 0;
        }
    }
}

extern "C" void TIMER2_IRQHandler()
{
    unsigned int clocks = LPC_TIM2->CR0;
    period = clocks;    // preambule 01010101

    if (!readBufferReady)
    {
        readBuffer[0] = in.read();
    }
    else
    {
        readBuffer[1] = in.read();
        // Appel MEF
        bool value = decode(readBuffer[0], readBuffer[1]);
        mef.ReceiveBit(value);
    }

    LPC_TIM2->TC = 0;

    LPC_TIM2->IR |= 0xFFFFFFFF;     // clear Timer interrupt register
}

void initTimers()
{
    //Timer 1 (match)
    LPC_SC->PCLKSEL0 |= (1 << 4);           // pclk = cclk timer1
    LPC_SC->PCONP |= (1 << 2);              // timer1 power on
    LPC_TIM1->MR0 = CLOCKS_TO_SECOND / 10;  // 100 ms
    LPC_TIM1->MCR = 3;                      // interrupt and reset control
                                            // Interrupt & reset timer on match
    LPC_TIM1->EMR = (3 << 4);
    NVIC_EnableIRQ(TIMER1_IRQn);            // enable timer interrupt
    LPC_TIM1->TCR = 1;                      // enable 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->IR = 0xFFFFFFFF;              // clear interrupt register
    LPC_TIM2->CCR |= 0x0000007;             // enable rising-edge and falling-edge capture on 2.0
    NVIC_EnableIRQ(TIMER2_IRQn);            // enable timer interrupt
    LPC_TIM2->TCR = 1;                      // start Timer
}

int main()
{
    //message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc);

    LPC_PINCON->PINSEL0 |= (3 << 8);   // P0.4 = CAP2.0
    initTimers();

    while (true)
    {
        pc.printf("%i ", period);
        pc.printf("%i \r\n", in.read());
        //pc.printf("%i ", decode(in.read(), clockTick));
        wait(0.1);
    }
}

void _decodeCallback(bitset<FRAMESIZE> decMessage)
{
   //decodedMessage = decMessage;
   dataReady = true;
}

void _decodeError()
{
    frameDropped = true;
}