app4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Committer:
ericbisson
Date:
Mon Mar 06 22:49:58 2017 +0000
Revision:
10:8b066285c2e0
Parent:
8:5b87b1f9d91f
Child:
11:b27d1a83f688
menage effectu?

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 "bit.h"
ericbisson 8:5b87b1f9d91f 4 #include "uart.h"
ericbisson 1:b3ae0d9f02ad 5
ericbisson 8:5b87b1f9d91f 6 const float HALF_PERIOD = 0.05; // secondes
ericbisson 2:c6465d4e82d2 7
ericbisson 7:5501dbea5650 8 Serial pc(USBTX, USBRX, 9600);
ericbisson 7:5501dbea5650 9 Serial uart(p13, p14, 9600);
ericbisson 8:5b87b1f9d91f 10 Thread ThreadLecture;
ericbisson 2:c6465d4e82d2 11
ericbisson 7:5501dbea5650 12 bool bIsHalfPeriod = false;
ericbisson 7:5501dbea5650 13
ericbisson 7:5501dbea5650 14 extern "C" void RIT_IRQHandler(void) {
ericbisson 7:5501dbea5650 15 //clear flag
ericbisson 7:5501dbea5650 16 LPC_RIT->RICTRL |= bit0; //write 1 to clear bit
ericbisson 5:e878d87782ce 17
ericbisson 7:5501dbea5650 18 bIsHalfPeriod = !bIsHalfPeriod;
ericbisson 1:b3ae0d9f02ad 19 }
ericbisson 1:b3ae0d9f02ad 20
ericbisson 7:5501dbea5650 21 void p14_interrupt()
ericbisson 7:5501dbea5650 22 {
ericbisson 8:5b87b1f9d91f 23 // On turn off les interrupts de lecture une fois qu'on a détecter un message
ericbisson 8:5b87b1f9d91f 24 uart.attach(NULL, uart.RxIrq);
ericbisson 8:5b87b1f9d91f 25
ericbisson 7:5501dbea5650 26 // On envoie le signal au thread de lecture
ericbisson 8:5b87b1f9d91f 27 ThreadLecture.signal_set(1);
ericbisson 7:5501dbea5650 28 };
ericbisson 1:b3ae0d9f02ad 29
ericbisson 7:5501dbea5650 30 void read()
ericbisson 7:5501dbea5650 31 {
ericbisson 7:5501dbea5650 32 while(true)
ericbisson 7:5501dbea5650 33 {
ericbisson 8:5b87b1f9d91f 34 // Attente passive d'un message entrant
ericbisson 8:5b87b1f9d91f 35 uart.attach(&p14_interrupt, uart.RxIrq);
ericbisson 8:5b87b1f9d91f 36 ThreadLecture.signal_wait(1);
ericbisson 2:c6465d4e82d2 37
ericbisson 8:5b87b1f9d91f 38 // Lis le message. Retourne une exception si il y a une erreur
ericbisson 10:8b066285c2e0 39 vector<char> message = uart_read(uart);
ericbisson 2:c6465d4e82d2 40
ericbisson 8:5b87b1f9d91f 41 if (!message.empty())
ericbisson 8:5b87b1f9d91f 42 {
ericbisson 8:5b87b1f9d91f 43 // Affiche le message à l'écran
ericbisson 8:5b87b1f9d91f 44 pc.printf(&message[0], message.size());
ericbisson 8:5b87b1f9d91f 45 }
ericbisson 1:b3ae0d9f02ad 46 }
ericbisson 7:5501dbea5650 47 };
ericbisson 3:3ecbcc05bc85 48
ericbisson 7:5501dbea5650 49 void rit_init()
ericbisson 4:87e9b434bb4d 50 {
ericbisson 7:5501dbea5650 51 LPC_SC->PCONP |= bit16; //Power Control for Peripherals register: power up RIT clock
ericbisson 7:5501dbea5650 52 LPC_SC->PCLKSEL1 |= (bit26 && bit27); //Peripheral clock selection: divide clock by 8 (run RIT clock by 12.5MHz)
ericbisson 7:5501dbea5650 53 LPC_RIT->RICOUNTER = 0; //set counter to zero
ericbisson 8:5b87b1f9d91f 54 LPC_RIT->RICOMPVAL = 250000000 * HALF_PERIOD; //interrupt tick every HALF_PERIOD
ericbisson 7:5501dbea5650 55 LPC_RIT->RICTRL |= bit1; // clear timer when counter reaches value
ericbisson 7:5501dbea5650 56 LPC_RIT->RICTRL |= bit3; // enable timer
ericbisson 7:5501dbea5650 57
ericbisson 7:5501dbea5650 58 //enable interrupt
ericbisson 7:5501dbea5650 59 NVIC_SetPriority(RIT_IRQn, 31);
ericbisson 7:5501dbea5650 60 NVIC_EnableIRQ(RIT_IRQn);
ericbisson 7:5501dbea5650 61 };
ericbisson 4:87e9b434bb4d 62
ericbisson 2:c6465d4e82d2 63 int main() {
ericbisson 7:5501dbea5650 64 rit_init();
ericbisson 0:c637467eeb8f 65
ericbisson 8:5b87b1f9d91f 66 ThreadLecture.start(read);
ericbisson 0:c637467eeb8f 67
ericbisson 3:3ecbcc05bc85 68 while(true) {
ericbisson 7:5501dbea5650 69 // TODO: Mettre ici le code pour l'input et l'envoie de message
ericbisson 8:5b87b1f9d91f 70 uart.printf("12345");
ericbisson 7:5501dbea5650 71 wait_ms(100);
ericbisson 0:c637467eeb8f 72 }
ericbisson 7:5501dbea5650 73 };