app4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

main.cpp

Committer:
JoeyDionne
Date:
2017-03-07
Revision:
11:b27d1a83f688
Parent:
10:8b066285c2e0
Child:
12:a55f77a0e188
Child:
14:bd909277eb13

File content as of revision 11:b27d1a83f688:

#include "mbed.h"
#include "rtos.h"
#include "bit.h"
#include "uart.h"

const float HALF_PERIOD = 0.05; // secondes

Serial pc(USBTX, USBRX, 9600);
Serial uart(p13, p14, 9600);
Thread ThreadLecture;

//test

bool bIsHalfPeriod = false;
  
extern "C" void RIT_IRQHandler(void) {
    //clear flag
    LPC_RIT->RICTRL |= bit0; //write 1 to clear bit
    
    bIsHalfPeriod = !bIsHalfPeriod;
}

void p14_interrupt()
{    
    // On turn off les interrupts de lecture une fois qu'on a détecter un message
    uart.attach(NULL, uart.RxIrq);

    // On envoie le signal au thread de lecture
    ThreadLecture.signal_set(1);
};

void read()
{
    while(true) 
    {
        // Attente passive d'un message entrant
        uart.attach(&p14_interrupt, uart.RxIrq);
        ThreadLecture.signal_wait(1);
        
        // Lis le message. Retourne une exception si il y a une erreur
        vector<char> message = uart_read(uart);
        
        if (!message.empty())
        {
            // Affiche le message à l'écran
            pc.printf(&message[0], message.size());
        }
    }
};

void rit_init()
{
    LPC_SC->PCONP |= bit16;                             //Power Control for Peripherals register: power up RIT clock
    LPC_SC->PCLKSEL1 |= (bit26 && bit27);               //Peripheral clock selection: divide clock by 8 (run RIT clock by 12.5MHz)
    LPC_RIT->RICOUNTER = 0;                             //set counter to zero
    LPC_RIT->RICOMPVAL = 250000000 * HALF_PERIOD;       //interrupt tick every HALF_PERIOD
    LPC_RIT->RICTRL |= bit1;                            // clear timer when counter reaches value
    LPC_RIT->RICTRL |= bit3;                            // enable timer
     
    //enable interrupt
    NVIC_SetPriority(RIT_IRQn, 31);
    NVIC_EnableIRQ(RIT_IRQn);
};

int main() {
    rit_init();
    
    ThreadLecture.start(read);
    
    while(true) {
        // TODO: Mettre ici le code pour l'input et l'envoie de message
        uart.printf("12345");
        wait_ms(100);
    }
};