Jason Garner / registers-example

Dependencies:   ARM registers

Files at this revision

API Documentation at this revision

Comitter:
elevatorguy
Date:
Thu Jan 03 02:57:29 2013 +0000
Parent:
2:e26c096f1946
Child:
4:6ed8900881e8
Commit message:
added Ticker

Changed in this revision

Serial.h Show annotated file Show diff for this revision Revisions of this file
Ticker.cpp Show annotated file Show diff for this revision Revisions of this file
Ticker.h Show annotated file Show diff for this revision Revisions of this file
Timer.cpp Show annotated file Show diff for this revision Revisions of this file
Timer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
registers.h Show annotated file Show diff for this revision Revisions of this file
--- a/Serial.h	Sun Dec 30 04:58:42 2012 +0000
+++ b/Serial.h	Thu Jan 03 02:57:29 2013 +0000
@@ -3,7 +3,6 @@
 
 #include "LPC17xx.h"
 
-
 //This is really just UART0 which goes over USB to the PC
 class Serial
 {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ticker.cpp	Thu Jan 03 02:57:29 2013 +0000
@@ -0,0 +1,94 @@
+#include "Ticker.h"
+//This class uses Timer 2
+
+extern "C" void TIMER2_IRQHandler(void)
+{
+  if ( LPC_TIM2->IR & 0x1 )
+  {
+    LPC_TIM2->IR = 0x1;              /* clear interrupt flag */
+    for(char count = 0; count < Ticker::active_tickers; count++)
+    {
+        if((*(Ticker::tickers[count])).remaining == 0)
+        {
+            //void (*func)(void) = (void (*)(void))((uint32_t)&isr);
+            //func();
+            (*(void (*)(void))((*(Ticker::tickers[count])).func))();
+            //set up for next interval
+            (*(Ticker::tickers[count])).remaining = (*(Ticker::tickers[count])).interval - 1;
+        }
+        else
+        {
+            (*(Ticker::tickers[count])).remaining = (*(Ticker::tickers[count])).remaining - 1;
+        }
+    }
+  }
+}
+
+// static data initialization  (only called once)
+bool Ticker::timer2initialized = false; 
+int Ticker::active_tickers = 0;
+int Ticker::MAX = 5;
+Ticker** Ticker::tickers = 0; //NULL POINTER
+
+void Ticker::initTimer2() 
+{
+    LPC_SC->PCLKSEL1 &= (3 << 12); //mask
+    LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
+
+    LPC_SC->PCONP |= (0x1<<22);     // turn on power for timer 2
+    LPC_TIM2->TCR = 0x02;           // reset timer
+    LPC_TIM2->PR  = (SystemCoreClock / 1000000); //microsecond steps
+    LPC_TIM2->MR0 = 100;            // 100 microsecond interval interrupts
+    LPC_TIM2->IR  = 0x3f;           // reset all interrrupts
+    LPC_TIM2->MCR = 0x03;           // reset timer on match and generate interrupt (MR0)
+    LPC_TIM2->TCR = 0x01;           // start timer
+    
+    NVIC_EnableIRQ(TIMER2_IRQn); // Enable the interrupt
+    
+    timer2initialized = true;
+}
+
+Ticker::Ticker()
+{
+    if(!timer2initialized)
+    {
+        initTimer2();
+        tickers = new Ticker*[MAX];
+    }
+    active = false;
+}
+
+Ticker::Ticker(int maxsize)
+{
+    if(!timer2initialized)
+    {
+        initTimer2();
+        tickers = new Ticker*[maxsize];
+        MAX = maxsize;
+    }
+    active = false;
+}
+
+void Ticker::attach(void (*funcaddr)(void), float secinterval)
+{
+
+    func = (uint32_t)funcaddr; //pointer to function address
+    interval = secinterval*10000;
+    remaining = secinterval*10000 - 1; // 100 microsecond resolution
+    tickers[active_tickers] = this;
+    active_tickers = active_tickers + 1;
+    active = true;
+}
+
+void Ticker::detach()
+{
+    active_tickers = active_tickers - 1;
+    tickers[active_tickers] = 0; //NULL pointer
+    active = false;
+}
+
+Ticker::~Ticker()
+{
+    if(active)
+        detach();   
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ticker.h	Thu Jan 03 02:57:29 2013 +0000
@@ -0,0 +1,59 @@
+#ifndef __TICKER_H_
+#define __TICKER_H_
+
+#include "LPC17xx.h"
+//#include "cmsis_nvic.h"
+//old way: NVIC_SetVector(TIMER2_IRQn, funcaddr); //tell it to run this function
+
+
+/**
+ * @author Jason Garner
+ *
+ * <b>Ticker</b> is an alternative implementation of the Mbed library <a href="/handbook/Ticker">Ticker</a> to save memory.
+ * The maximum number of Ticker instances is fully customizable. 
+ *
+ */
+class Ticker
+{
+    void initTimer2(); // the constructor initializes the hardware timer, if it wasn't already initialized
+    bool active; // whether or not the instance is an active Ticker
+public:
+    static bool timer2initialized; //stores whether the timer has been initialized
+    static Ticker** tickers; //array of pointers to ticker instances
+    uint32_t func; //function that NVIC will call on interrupts
+    uint32_t interval; // unit interval to execute func (unit = 100 microseconds)
+    uint32_t remaining; // units remaning until executing func (unit = 100 microseconds)
+    static int active_tickers; //how many instances of this class we have that are attached. (NVIC needs this)
+    static int MAX; // max size of Ticker* array
+
+     /**
+     * Regular constructor: the default internal array size is 5.
+     */  
+    Ticker();
+
+     /**
+     * Custom constructor: initializes the object with the specified parameter.
+     *
+     * @param max Maximum instances of Ticker objects allowed. (sets internal array size of Ticker* to instances)
+     */  
+    Ticker(int);
+    
+    ~Ticker(); //DESTRUCTOR   
+    
+    
+     /**
+     * Attach a function to be called by the Ticker, specifiying the interval in seconds. 
+     * The timer is a 32bit 100 microsecond Timer, so the smallest time is 100 microseconds and the largest time is about 59 hours. 
+     *
+     * @param fptr pointer to the function to be called 
+     * @param t The time in seconds in between function calls.
+     */  
+    void attach(void (*fptr)(void), float t); //address of function to call, and interval to call it
+    
+     /**
+     * Detaches the function from the Ticker. The function will no longer be called, and a new one can now be attached.
+     */  
+    void detach(); //removes address from ticker array
+};
+
+#endif
\ No newline at end of file
--- a/Timer.cpp	Sun Dec 30 04:58:42 2012 +0000
+++ b/Timer.cpp	Thu Jan 03 02:57:29 2013 +0000
@@ -3,9 +3,9 @@
 
 // static data initialization  (only called once)
 bool Timer::timer1initialized = false; 
-int Timer::resolution = 1000000;
+int Timer::resolution = 1000000; //default: microseconds accuracy
 
-void Timer::initTimer1(int res) //microseconds accuracy
+void Timer::initTimer1(int res) 
 {
     uint8_t pclk;
     uint32_t pclkdiv = (LPC_SC->PCLKSEL0 >> 4) & 0x03; //PCLK for Timer 1 (page 56)
@@ -27,14 +27,14 @@
             break;
     }
 
-    LPC_TIM1->TCR = 0x02;           /* reset timer */
+    LPC_TIM1->TCR = 0x02;           // reset timer
     LPC_TIM1->PR  = (SystemCoreClock / (pclk * res)); //default: microsecond steps
-    LPC_TIM1->MR0 = 2147483647;             /* highest number a 32bit signed int can store (for us ~ 35.79 minutes, or, for ms ~ 596.52 hours ) */
-    LPC_TIM1->IR  = 0xff;           /* reset all interrrupts */
-    LPC_TIM1->MCR = 0x02;           /* reset timer on match */
-    LPC_TIM1->TCR = 0x01;           /* start timer */
+    LPC_TIM1->MR0 = 2147483647;             // highest number a 32bit signed int can store (for us ~ 35.79 minutes, or, for ms ~ 596.52 hours )
+    LPC_TIM1->IR  = 0xff;           // reset all interrrupts
+    LPC_TIM1->MCR = 0x02;           // reset timer on match
+    LPC_TIM1->TCR = 0x01;           // start timer
 
-    /* The timer simply goes on forever! It just resets itself when it hits the max number for TC */
+    // The timer simply goes on forever! It just resets itself when it hits the max number for TC
     timer1initialized = true;
     resolution = res;
 }
--- a/Timer.h	Sun Dec 30 04:58:42 2012 +0000
+++ b/Timer.h	Thu Jan 03 02:57:29 2013 +0000
@@ -3,8 +3,6 @@
 
 #include "LPC17xx.h"
 
-void TIMER1_interrupt(void);
-
 class Timer
 {
     void initTimer1(int); // the constructor initializes the hardware timer, if it wasn't already initialized
--- a/main.cpp	Sun Dec 30 04:58:42 2012 +0000
+++ b/main.cpp	Thu Jan 03 02:57:29 2013 +0000
@@ -1,17 +1,30 @@
 #include "registers.h"
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
+DigitalOut led1(p20);
+DigitalOut led2(p19);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 DigitalIn switch1(p21);
 
-Serial pc;
+Serial pc(115200);
 Timer t(1000); //ms resolution for a serial stopwatch demo
 
-int main() {  //Timer demonstration
+Ticker tock;
+Ticker tock2;
+
+extern "C" void isr(void) {
+    led1 = !led1;
+}
+
+extern "C" void isr2(void) {
+    led2 = !led2;
+}
+
+int main() {  //Timer and Ticker demonstration
     switch1.mode(PULLDOWN);
     t.start();
-
+    
+    tock.attach(&isr, 0.50);
+    tock2.attach(&isr2, 0.25);
     while(1)
     {
         double time = t;
--- a/registers.h	Sun Dec 30 04:58:42 2012 +0000
+++ b/registers.h	Thu Jan 03 02:57:29 2013 +0000
@@ -8,6 +8,7 @@
 #include "Serial.h"
 #include "functions.h"
 #include "Timer.h"
+#include "Ticker.h"
 //TODO : MORE!!!!!!
 
 #endif
\ No newline at end of file