Wim van der Vegt / RIT

Dependents:   ORTP-L_SensorTest ORTP-L_V01 ORTP-L_V01 RIT_Demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RIT.h Source File

RIT.h

00001 /* mbed Repetitive Interrupt Timer Library
00002  * Copyright (c) 2011 wvd_vegt
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #ifndef __RIT_h
00024 #define __RIT_h
00025 
00026 #include "mbed.h"
00027 
00028 #define SYSCLK 96000000L
00029 #define ONEMHZ  1000000L
00030 #define RIT_FREQ 96*1000L
00031 #define RIT_DIV (SYSCLK/RIT_FREQ)-1
00032 
00033 /** Repetitive Interrupt Timer (RIT) class.
00034 * 
00035 * Sample Code:
00036 * 
00037 * DigitalOut rit_led(LED3); 
00038 * 
00039 * //Our function isr. 
00040 * void RIT_IRQHandler(void) { 
00041 *   rit_led=!rit_led; // Flash a Led 
00042 * } 
00043 * 
00044 * RIT rit(1000); //Set interval to 1000 ms or 1 second. 
00045 * 
00046 * rit.append(RIT_IRQHandler); //Append our own function ISR. 
00047 * rit.enable(); 
00048 * 
00049 * wait(10); //wait 10 seconds (and watch the led flashing). 
00050 * 
00051 * rit.disable(); 
00052 * rit.unappend(); 
00053 */
00054 class RIT {
00055 public:
00056     /** Constructor.
00057      *
00058      * @parm ms the number of milliseconds between two interrupts.
00059      */
00060     RIT(uint32_t ms);
00061 
00062     /** Setup timing in ms.
00063      *
00064      * @parm ms the number of milliseconds between two interrupts.
00065      */
00066     void setup_ms(uint32_t ms);
00067 
00068     /** Setup timing in us.
00069      *
00070      * @parm us the number of microseconds between two interrupts.
00071      */
00072     void setup_us(uint32_t us);
00073 
00074     /** Attach custom interrupt handler replacing default.
00075      *
00076      * @parm fptr the new interrupt handler.
00077      */
00078     void attach(void(*fptr)(void));
00079 
00080     /** Restore default interrupt handler.
00081      *
00082      */
00083     void detach(void);
00084 
00085     /** Append function isr to global interrupt handler.
00086      *
00087      * @parm fptr the function isr to be called from within the default interrupt handler.
00088      */
00089     void append(void(*fptr)(void));
00090 
00091     /** Remove function isr from global interrupt handler.
00092      *
00093      */
00094     void unappend();
00095 
00096     /** Enable RIT & Interrupt
00097      *
00098      */
00099     void enable(void);
00100 
00101     /** Disable RIT & Interrupt.
00102      *
00103      */
00104     void disable(void);
00105 
00106     /** Power up RIT.
00107      *
00108      */
00109     void power_enable(void);
00110 
00111     /** Power down RIT
00112      *
00113      * Note: Powering down might take some more effect when entering Deep Sleep,
00114      * accoring to errata.lpc1768.pdf.
00115      */
00116     void power_disable(void);
00117 
00118 private:
00119 
00120     /** There can only be one.
00121      *
00122      */
00123     static RIT *instance;
00124 
00125     /** Storage for an appended function isr.
00126      *
00127      */
00128     void(*_rit_g_isr)(void);
00129 
00130     /** The default (instance) isr.
00131      *
00132      * Note: this isr calls the static one so there is only be a single one.
00133      */
00134     void ritisr(void);
00135 
00136     /** The actual (static) default isr.
00137      *
00138      */
00139     static void _ritisr(void);
00140 
00141     /** Select the Peripheral Clock (PCLK_RIT) for the RIT
00142      *  to be equal to the CCLK.
00143      *
00144      */
00145     void select_clk(void);
00146 };
00147 
00148 #endif