Éric Bisson / Mbed 2 deprecated S5info_APP4

Dependencies:   mbed-rtos mbed CRC16

Fork of S5info_APP2 by Éric Bisson

Files at this revision

API Documentation at this revision

Comitter:
ericbisson
Date:
Tue Mar 07 04:39:57 2017 +0000
Parent:
11:b27d1a83f688
Child:
15:7c2e70c36b98
Commit message:
temp commit

Changed in this revision

bit.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
read.h Show annotated file Show diff for this revision Revisions of this file
--- a/bit.h	Tue Mar 07 00:54:26 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-int bit0 = 1;
-int bit1 = 1<<1;
-int bit2 = 1<<2;
-int bit3 = 1<<3;
-int bit16 = 1<<16;
-int bit26 = 1<<26;
-int bit27 = 1<<27;
\ No newline at end of file
--- a/main.cpp	Tue Mar 07 00:54:26 2017 +0000
+++ b/main.cpp	Tue Mar 07 04:39:57 2017 +0000
@@ -1,75 +1,83 @@
-#include "mbed.h"
-#include "rtos.h"
-#include "bit.h"
-#include "uart.h"
+#include "read.h"
 
-const float HALF_PERIOD = 0.05; // secondes
+Serial pc(USBTX, USBRX);
+DigitalOut out(p8);
 
-Serial pc(USBTX, USBRX, 9600);
-Serial uart(p13, p14, 9600);
-Thread ThreadLecture;
-
-//test
+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 / 100; // 100 ms
+    LPC_TIM1->MCR = 3;                      // interrupt and reset control
+    LPC_TIM1->EMR = (3 << 4);               // Interrupt & reset timer on match
+    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
+    NVIC_EnableIRQ(TIMER2_IRQn);            // enable timer interrupt
+    LPC_TIM2->TCR = 1;                      // start Timer
+}
 
-bool bIsHalfPeriod = false;
-  
-extern "C" void RIT_IRQHandler(void) {
-    //clear flag
-    LPC_RIT->RICTRL |= bit0; //write 1 to clear bit
-    
-    bIsHalfPeriod = !bIsHalfPeriod;
+bool bTimer1 = false;
+extern "C" void TIMER1_IRQHandler()
+{
+    if ((LPC_TIM1->IR & 0x01) == 0x01)
+    {
+        bTimer1 = !bTimer1;
+        
+        LPC_TIM1->IR |= 1 << 0; // clear
+    }
 }
 
-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()
+int sumClocks = 0;
+int PeriodLength = 0;
+bool bReceivedFirstBit = false;
+extern "C" void TIMER2_IRQHandler()
 {
-    while(true) 
+    if (PeriodLength > 0)
     {
-        // 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())
+        if (LPC_TIM2->CR0 >= PeriodLength*1.5 || (sumClocks + clocks) >= PeriodLength*1.5)
         {
-            // Affiche le message à l'écran
-            pc.printf(&message[0], message.size());
+            sumClocks = 0;
+            
+            ThreadLecture.signal_set(1);
+        }
+        else
+        {
+            sumClocks += LPC_TIM2->CR0;
         }
     }
-};
-
-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);
-};
+    else if (bReceivedFirstBit)
+    {
+        PeriodLength = LPC_TIM2->CR0 / 2;
+        ThreadLecture.signal_set(1);
+    }
+    else
+    {
+        bReceivedFirstBit = true;
+        ThreadLecture.signal_set(1);
+    }
+ 
+    LPC_TIM2->TC = 0;
+    LPC_TIM2->IR |= 0xFFFFFFFF; // clear
+}
 
 int main() {
-    rit_init();
-    
-    ThreadLecture.start(read);
+    LPC_PINCON->PINSEL0 |= (3 << 8); // pin30
+    initTimers();
+    ThreadLecture.attach(read);
+    ThreadLecture.start();
     
     while(true) {
-        // TODO: Mettre ici le code pour l'input et l'envoie de message
-        uart.printf("12345");
-        wait_ms(100);
     }
 };
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/read.h	Tue Mar 07 04:39:57 2017 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "rtos.h"
+DigitalIn in(p30);
+Thread ThreadLecture;
+const int PREAMBULE = 0b01010101;
+const int START = 0b01111110;
+const int END = 0b01111110;
+
+void read()
+{
+    char byte;
+    vector<char> bytes;
+    while (true)
+    {
+        ThreadLecture.signal_wait(1);
+        
+        byte = (byte << 1) + in;
+        
+        shift++;
+        if (shift == 8)
+        {
+            bytes.push_back(byte);
+            shift = 0;
+        }
+    }
+}
\ No newline at end of file