RIT (Repetitive Interrupt Timer) Library.

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

Committer:
wvd_vegt
Date:
Fri Mar 11 10:37:04 2011 +0000
Revision:
3:682b29493aa0
Parent:
0:af45bc81d34c
Child:
4:64198265f56f
Minor Revision.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wvd_vegt 0:af45bc81d34c 1 /* mbed Repetitive Interrupt Timer Library
wvd_vegt 0:af45bc81d34c 2 * Copyright (c) 2011 wvd_vegt
wvd_vegt 0:af45bc81d34c 3 *
wvd_vegt 0:af45bc81d34c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wvd_vegt 0:af45bc81d34c 5 * of this software and associated documentation files (the "Software"), to deal
wvd_vegt 0:af45bc81d34c 6 * in the Software without restriction, including without limitation the rights
wvd_vegt 0:af45bc81d34c 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wvd_vegt 0:af45bc81d34c 8 * copies of the Software, and to permit persons to whom the Software is
wvd_vegt 0:af45bc81d34c 9 * furnished to do so, subject to the following conditions:
wvd_vegt 0:af45bc81d34c 10 *
wvd_vegt 0:af45bc81d34c 11 * The above copyright notice and this permission notice shall be included in
wvd_vegt 0:af45bc81d34c 12 * all copies or substantial portions of the Software.
wvd_vegt 0:af45bc81d34c 13 *
wvd_vegt 0:af45bc81d34c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wvd_vegt 0:af45bc81d34c 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wvd_vegt 0:af45bc81d34c 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wvd_vegt 0:af45bc81d34c 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wvd_vegt 0:af45bc81d34c 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wvd_vegt 0:af45bc81d34c 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wvd_vegt 0:af45bc81d34c 20 * THE SOFTWARE.
wvd_vegt 0:af45bc81d34c 21 */
wvd_vegt 0:af45bc81d34c 22
wvd_vegt 0:af45bc81d34c 23 #ifndef __RIT_h
wvd_vegt 0:af45bc81d34c 24 #define __RIT_h
wvd_vegt 0:af45bc81d34c 25
wvd_vegt 0:af45bc81d34c 26 #include "mbed.h"
wvd_vegt 0:af45bc81d34c 27
wvd_vegt 0:af45bc81d34c 28 #define SYSCLK 96000000L
wvd_vegt 0:af45bc81d34c 29 #define ONEMHZ 1000000L
wvd_vegt 0:af45bc81d34c 30 #define RIT_FREQ 96*1000L
wvd_vegt 0:af45bc81d34c 31 #define RIT_DIV (SYSCLK/RIT_FREQ)-1
wvd_vegt 0:af45bc81d34c 32
wvd_vegt 0:af45bc81d34c 33 /** Repetitive Interrupt Timer (RIT) class.<br/>
wvd_vegt 0:af45bc81d34c 34 * <br/>
wvd_vegt 0:af45bc81d34c 35 * Sample Code:<br/>
wvd_vegt 0:af45bc81d34c 36 * <br/>
wvd_vegt 0:af45bc81d34c 37 * DigitalOut rit_led(LED3); <br/>
wvd_vegt 0:af45bc81d34c 38 * <br/>
wvd_vegt 0:af45bc81d34c 39 * //Our function isr. <br/>
wvd_vegt 0:af45bc81d34c 40 * void RIT_IRQHandler(void) { <br/>
wvd_vegt 0:af45bc81d34c 41 * rit_led=!rit_led; // Flash a Led <br/>
wvd_vegt 0:af45bc81d34c 42 * } <br/>
wvd_vegt 0:af45bc81d34c 43 * <br/>
wvd_vegt 0:af45bc81d34c 44 * RIT rit(1000); //Set interval to 1000 ms or 1 second. <br/>
wvd_vegt 0:af45bc81d34c 45 * <br/>
wvd_vegt 0:af45bc81d34c 46 * rit.append(RIT_IRQHandler); //Append our own function ISR. <br/>
wvd_vegt 0:af45bc81d34c 47 * rit.enable(); <br/>
wvd_vegt 0:af45bc81d34c 48 * <br/>
wvd_vegt 0:af45bc81d34c 49 * wait(10); //wait 10 seconds (and watch the led flashing). <br/>
wvd_vegt 0:af45bc81d34c 50 * <br/>
wvd_vegt 0:af45bc81d34c 51 * rit.disable(); <br/>
wvd_vegt 0:af45bc81d34c 52 * rit.unappend(); <br/>
wvd_vegt 0:af45bc81d34c 53 */
wvd_vegt 0:af45bc81d34c 54 class RIT {
wvd_vegt 0:af45bc81d34c 55 public:
wvd_vegt 0:af45bc81d34c 56 /** Constructor.
wvd_vegt 0:af45bc81d34c 57 *
wvd_vegt 0:af45bc81d34c 58 * @parm ms the number of milliseconds between two interrupts.
wvd_vegt 0:af45bc81d34c 59 */
wvd_vegt 0:af45bc81d34c 60 RIT(uint32_t ms);
wvd_vegt 0:af45bc81d34c 61
wvd_vegt 0:af45bc81d34c 62 /** Setup timing in ms.
wvd_vegt 0:af45bc81d34c 63 *
wvd_vegt 0:af45bc81d34c 64 * @parm ms the number of milliseconds between two interrupts.
wvd_vegt 0:af45bc81d34c 65 */
wvd_vegt 0:af45bc81d34c 66 void setup_ms(uint32_t ms);
wvd_vegt 0:af45bc81d34c 67
wvd_vegt 0:af45bc81d34c 68 /** Setup timing in us.
wvd_vegt 0:af45bc81d34c 69 *
wvd_vegt 0:af45bc81d34c 70 * @parm us the number of microseconds between two interrupts.
wvd_vegt 0:af45bc81d34c 71 */
wvd_vegt 0:af45bc81d34c 72 void setup_us(uint32_t us);
wvd_vegt 0:af45bc81d34c 73
wvd_vegt 0:af45bc81d34c 74 /** Attach custom interrupt handler replacing default.
wvd_vegt 0:af45bc81d34c 75 *
wvd_vegt 0:af45bc81d34c 76 * @parm fptr the new interrupt handler.
wvd_vegt 0:af45bc81d34c 77 */
wvd_vegt 0:af45bc81d34c 78 void attach(void(*fptr)(void));
wvd_vegt 0:af45bc81d34c 79
wvd_vegt 0:af45bc81d34c 80 /** Restore default interrupt handler.
wvd_vegt 0:af45bc81d34c 81 *
wvd_vegt 0:af45bc81d34c 82 */
wvd_vegt 0:af45bc81d34c 83 void detach(void);
wvd_vegt 0:af45bc81d34c 84
wvd_vegt 0:af45bc81d34c 85 /** Append function isr to global interrupt handler.
wvd_vegt 0:af45bc81d34c 86 *
wvd_vegt 0:af45bc81d34c 87 * @parm fptr the function isr to be called from within the default interrupt handler.
wvd_vegt 0:af45bc81d34c 88 */
wvd_vegt 0:af45bc81d34c 89 void append(void(*fptr)(void));
wvd_vegt 0:af45bc81d34c 90
wvd_vegt 0:af45bc81d34c 91 /** Remove function isr from global interrupt handler.
wvd_vegt 0:af45bc81d34c 92 *
wvd_vegt 0:af45bc81d34c 93 */
wvd_vegt 0:af45bc81d34c 94 void unappend();
wvd_vegt 0:af45bc81d34c 95
wvd_vegt 0:af45bc81d34c 96 /** Enable RIT & Interrupt
wvd_vegt 0:af45bc81d34c 97 *
wvd_vegt 0:af45bc81d34c 98 */
wvd_vegt 0:af45bc81d34c 99 void enable(void);
wvd_vegt 0:af45bc81d34c 100
wvd_vegt 0:af45bc81d34c 101 /** Disable RIT & Interrupt.
wvd_vegt 0:af45bc81d34c 102 *
wvd_vegt 0:af45bc81d34c 103 */
wvd_vegt 0:af45bc81d34c 104 void disable(void);
wvd_vegt 0:af45bc81d34c 105
wvd_vegt 0:af45bc81d34c 106 /** Power up RIT.
wvd_vegt 0:af45bc81d34c 107 *
wvd_vegt 0:af45bc81d34c 108 */
wvd_vegt 0:af45bc81d34c 109 void power_enable(void);
wvd_vegt 0:af45bc81d34c 110
wvd_vegt 0:af45bc81d34c 111 /** Power down RIT
wvd_vegt 0:af45bc81d34c 112 *
wvd_vegt 0:af45bc81d34c 113 * Note: Powering down might take some more effect when entering Deep Sleep,
wvd_vegt 0:af45bc81d34c 114 * accoring to errata.lpc1768.pdf.
wvd_vegt 0:af45bc81d34c 115 */
wvd_vegt 0:af45bc81d34c 116 void power_disable(void);
wvd_vegt 0:af45bc81d34c 117
wvd_vegt 0:af45bc81d34c 118 private:
wvd_vegt 0:af45bc81d34c 119
wvd_vegt 0:af45bc81d34c 120 /** There can only be one.
wvd_vegt 0:af45bc81d34c 121 *
wvd_vegt 0:af45bc81d34c 122 */
wvd_vegt 0:af45bc81d34c 123 static RIT *instance;
wvd_vegt 0:af45bc81d34c 124
wvd_vegt 0:af45bc81d34c 125 /** Storage for an appended function isr.
wvd_vegt 0:af45bc81d34c 126 *
wvd_vegt 0:af45bc81d34c 127 */
wvd_vegt 0:af45bc81d34c 128 void(*_rit_g_isr)(void);
wvd_vegt 0:af45bc81d34c 129
wvd_vegt 0:af45bc81d34c 130 /** The default (instance) isr.
wvd_vegt 0:af45bc81d34c 131 *
wvd_vegt 0:af45bc81d34c 132 * Note: this isr calls the static one so there is only be a single one.
wvd_vegt 0:af45bc81d34c 133 */
wvd_vegt 0:af45bc81d34c 134 void ritisr(void);
wvd_vegt 0:af45bc81d34c 135
wvd_vegt 0:af45bc81d34c 136 /** The actual (static) default isr.
wvd_vegt 0:af45bc81d34c 137 *
wvd_vegt 0:af45bc81d34c 138 */
wvd_vegt 0:af45bc81d34c 139 static void _ritisr(void);
wvd_vegt 0:af45bc81d34c 140
wvd_vegt 0:af45bc81d34c 141 /** Select the Peripheral Clock (PCLK_RIT) for the RIT
wvd_vegt 0:af45bc81d34c 142 * to be equal to the CCLK.
wvd_vegt 0:af45bc81d34c 143 *
wvd_vegt 0:af45bc81d34c 144 */
wvd_vegt 0:af45bc81d34c 145 void select_clk(void);
wvd_vegt 0:af45bc81d34c 146 };
wvd_vegt 0:af45bc81d34c 147
wvd_vegt 0:af45bc81d34c 148 #endif