app4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Committer:
ericbisson
Date:
Mon Mar 06 19:18:30 2017 +0000
Revision:
7:5501dbea5650
Parent:
6:193013e37136
Child:
8:5b87b1f9d91f
app4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ericbisson 7:5501dbea5650 1 #include "mbed.h"
ericbisson 0:c637467eeb8f 2 #include "rtos.h"
ericbisson 7:5501dbea5650 3 #include <string>
ericbisson 7:5501dbea5650 4 #include "bit.h"
ericbisson 7:5501dbea5650 5 using std::string;
ericbisson 1:b3ae0d9f02ad 6
ericbisson 7:5501dbea5650 7 #define PREAMBULE 0b01010101
ericbisson 7:5501dbea5650 8 const char START = 0b01111110;
ericbisson 7:5501dbea5650 9 const char END = 0b01111110;
ericbisson 7:5501dbea5650 10 const char FLAGS = 0x00;
ericbisson 7:5501dbea5650 11 const char MAX_LENGTH = 80;
ericbisson 7:5501dbea5650 12 const float HALF_PERIOD = 0.0005; // secondes
ericbisson 2:c6465d4e82d2 13
ericbisson 7:5501dbea5650 14 Serial pc(USBTX, USBRX, 9600);
ericbisson 7:5501dbea5650 15 Serial uart(p13, p14, 9600);
ericbisson 7:5501dbea5650 16 DigitalOut myled1(LED1);
ericbisson 7:5501dbea5650 17 DigitalOut myled2(LED2);
ericbisson 7:5501dbea5650 18 InterruptIn read_pin(p14);
ericbisson 7:5501dbea5650 19 Thread* thread;
ericbisson 2:c6465d4e82d2 20
ericbisson 7:5501dbea5650 21 bool bIsHalfPeriod = false;
ericbisson 7:5501dbea5650 22
ericbisson 7:5501dbea5650 23 extern "C" void RIT_IRQHandler(void) {
ericbisson 7:5501dbea5650 24 //clear flag
ericbisson 7:5501dbea5650 25 LPC_RIT->RICTRL |= bit0; //write 1 to clear bit
ericbisson 5:e878d87782ce 26
ericbisson 7:5501dbea5650 27 myled1 = !myled1;
ericbisson 7:5501dbea5650 28 bIsHalfPeriod = !bIsHalfPeriod;
ericbisson 1:b3ae0d9f02ad 29 }
ericbisson 1:b3ae0d9f02ad 30
ericbisson 7:5501dbea5650 31 void p14_interrupt()
ericbisson 7:5501dbea5650 32 {
ericbisson 7:5501dbea5650 33 // On envoie le signal au thread de lecture
ericbisson 7:5501dbea5650 34 thread->signal_set(1);
ericbisson 2:c6465d4e82d2 35
ericbisson 7:5501dbea5650 36 // On turn off les interrupts de lecture une fois qu'on a détecter un message
ericbisson 7:5501dbea5650 37 read_pin.disable_irq();
ericbisson 7:5501dbea5650 38 };
ericbisson 1:b3ae0d9f02ad 39
ericbisson 7:5501dbea5650 40 void read()
ericbisson 7:5501dbea5650 41 {
ericbisson 7:5501dbea5650 42 while(true)
ericbisson 7:5501dbea5650 43 {
ericbisson 7:5501dbea5650 44 thread->signal_wait(1);
ericbisson 2:c6465d4e82d2 45
ericbisson 7:5501dbea5650 46 // TODO: mettre ici le code pour la lecture de trame
ericbisson 7:5501dbea5650 47 myled2 = !myled2;
ericbisson 2:c6465d4e82d2 48
ericbisson 7:5501dbea5650 49 // Une fois fini, on réactive l'interrupt de lecture
ericbisson 7:5501dbea5650 50 read_pin.enable_irq();
ericbisson 1:b3ae0d9f02ad 51 }
ericbisson 7:5501dbea5650 52 };
ericbisson 3:3ecbcc05bc85 53
ericbisson 7:5501dbea5650 54 void rit_init()
ericbisson 4:87e9b434bb4d 55 {
ericbisson 7:5501dbea5650 56 LPC_SC->PCONP |= bit16; //Power Control for Peripherals register: power up RIT clock
ericbisson 7:5501dbea5650 57 LPC_SC->PCLKSEL1 |= (bit26 && bit27); //Peripheral clock selection: divide clock by 8 (run RIT clock by 12.5MHz)
ericbisson 7:5501dbea5650 58 LPC_RIT->RICOUNTER = 0; //set counter to zero
ericbisson 7:5501dbea5650 59 LPC_RIT->RICOMPVAL = 1000000000 * HALF_PERIOD; //interrupt tick every HALF_PERIOD
ericbisson 7:5501dbea5650 60 LPC_RIT->RICTRL |= bit1; // clear timer when counter reaches value
ericbisson 7:5501dbea5650 61 LPC_RIT->RICTRL |= bit3; // enable timer
ericbisson 7:5501dbea5650 62
ericbisson 7:5501dbea5650 63 //enable interrupt
ericbisson 7:5501dbea5650 64 NVIC_SetPriority(RIT_IRQn, 31);
ericbisson 7:5501dbea5650 65 NVIC_EnableIRQ(RIT_IRQn);
ericbisson 7:5501dbea5650 66 };
ericbisson 4:87e9b434bb4d 67
ericbisson 2:c6465d4e82d2 68 int main() {
ericbisson 7:5501dbea5650 69 rit_init();
ericbisson 7:5501dbea5650 70 read_pin.fall(p14_interrupt);
ericbisson 7:5501dbea5650 71 read_pin.enable_irq();
ericbisson 0:c637467eeb8f 72
ericbisson 7:5501dbea5650 73 Thread ThreadLecture; // Thread pour pas avoir à rien faire dans l'interrupt
ericbisson 7:5501dbea5650 74 thread = &ThreadLecture;
ericbisson 7:5501dbea5650 75 thread->start(read);
ericbisson 0:c637467eeb8f 76
ericbisson 3:3ecbcc05bc85 77 while(true) {
ericbisson 7:5501dbea5650 78 // TODO: Mettre ici le code pour l'input et l'envoie de message
ericbisson 7:5501dbea5650 79 uart.putc(0);
ericbisson 7:5501dbea5650 80 wait_ms(100);
ericbisson 0:c637467eeb8f 81 }
ericbisson 7:5501dbea5650 82 };