APP 4

Dependencies:   mbed CRC16 mbed-rtos

APP.cpp

Committer:
vinbel93
Date:
2016-02-23
Revision:
21:137d010e7469
Parent:
20:f0932bfe09ed

File content as of revision 21:137d010e7469:

#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;
bitset<MAX_DATA> decodedMessage;
int debugMessage;
bool debugMessageReady = false;
bool dataReady;
bool frameDropped; 
int counter = 0;
unsigned int period = 0;
unsigned int currentClocks = 0;
bool periodCalculated = false;
bool firstBit = true;
bool value = false;
MEF mef;
STATES mefSTATE;
int payloadSize = 0;

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], clockTick);

        if (clockTick)
        {
            counter++;
        }

        if (counter >= FRAMESIZE)
        {
            LPC_TIM1->MR0 = 1000000000;
        }
    }
}

extern "C" void TIMER2_IRQHandler()
{
    unsigned int clocks = LPC_TIM2->CR0;
    bool inputValue = in.read();

    // Discard first bit
    if (!periodCalculated && !firstBit)
    {
        period = clocks / 2;
        periodCalculated = true;
    }
    
    if (firstBit)
    {
        mef.ReceiveBit(!inputValue);
        firstBit = false;
    }

    if (periodCalculated)
    {
        if (clocks >= period*1.5 || (currentClocks + clocks) >= period*1.5)
        {
            currentClocks = 0;
            mef.ReceiveBit(!inputValue);
        }
        else
        {
            currentClocks += clocks;
        }
    }

    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 / 1000;  // 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("BLUBLUBLUBLUBLU", 17), 17);    

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

    while (true)
    {
        if (dataReady)
        {
            for (int i = 0; i < payloadSize * 8; i++)
            {
                if((i + 1) % 8 == 0)
                {
                    char tempChar = (decodedMessage[i] << 0) | (decodedMessage[i - 1] << 1) | (decodedMessage[i - 2] << 2) | (decodedMessage[i - 3] << 3) | (decodedMessage[i - 4] << 4) | (decodedMessage[i - 5] << 5) | (decodedMessage[i - 6] << 6) | (decodedMessage[i - 7] << 7);
                    pc.printf("%c", tempChar);        
                }
            }
            dataReady = false;
        }
    }
}

void _decodeCallback(bitset<MAX_DATA> decMessage, int size)
{
    decodedMessage = decMessage;
    payloadSize = size;
    dataReady = true;
}

void _decodeError()
{
    frameDropped = true;
}