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:24:47 2011 +0000
Revision:
0:af45bc81d34c
Child:
3:682b29493aa0
Initial Release.

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 //veg: Copied parts BLDC's main.h and ADC_Full
wvd_vegt 0:af45bc81d34c 29 //veg: Changed RIT_FREQ to 1ms default
wvd_vegt 0:af45bc81d34c 30
wvd_vegt 0:af45bc81d34c 31 #define SYSCLK 96000000L
wvd_vegt 0:af45bc81d34c 32 #define ONEMHZ 1000000L
wvd_vegt 0:af45bc81d34c 33 #define RIT_FREQ 96*1000L
wvd_vegt 0:af45bc81d34c 34 #define RIT_DIV (SYSCLK/RIT_FREQ)-1
wvd_vegt 0:af45bc81d34c 35
wvd_vegt 0:af45bc81d34c 36 /** Repetitive Interrupt Timer (RIT) class.<br/>
wvd_vegt 0:af45bc81d34c 37 * <br/>
wvd_vegt 0:af45bc81d34c 38 * Sample Code:<br/>
wvd_vegt 0:af45bc81d34c 39 * <br/>
wvd_vegt 0:af45bc81d34c 40 * DigitalOut rit_led(LED3); <br/>
wvd_vegt 0:af45bc81d34c 41 * <br/>
wvd_vegt 0:af45bc81d34c 42 * //Our function isr. <br/>
wvd_vegt 0:af45bc81d34c 43 * void RIT_IRQHandler(void) { <br/>
wvd_vegt 0:af45bc81d34c 44 * rit_led=!rit_led; // Flash a Led <br/>
wvd_vegt 0:af45bc81d34c 45 * } <br/>
wvd_vegt 0:af45bc81d34c 46 * <br/>
wvd_vegt 0:af45bc81d34c 47 * RIT rit(1000); //Set interval to 1000 ms or 1 second. <br/>
wvd_vegt 0:af45bc81d34c 48 * <br/>
wvd_vegt 0:af45bc81d34c 49 * rit.append(RIT_IRQHandler); //Append our own function ISR. <br/>
wvd_vegt 0:af45bc81d34c 50 * rit.enable(); <br/>
wvd_vegt 0:af45bc81d34c 51 * <br/>
wvd_vegt 0:af45bc81d34c 52 * wait(10); //wait 10 seconds (and watch the led flashing). <br/>
wvd_vegt 0:af45bc81d34c 53 * <br/>
wvd_vegt 0:af45bc81d34c 54 * rit.disable(); <br/>
wvd_vegt 0:af45bc81d34c 55 * rit.unappend(); <br/>
wvd_vegt 0:af45bc81d34c 56 */
wvd_vegt 0:af45bc81d34c 57 class RIT {
wvd_vegt 0:af45bc81d34c 58 public:
wvd_vegt 0:af45bc81d34c 59 /** Constructor.
wvd_vegt 0:af45bc81d34c 60 *
wvd_vegt 0:af45bc81d34c 61 * @parm ms the number of milliseconds between two interrupts.
wvd_vegt 0:af45bc81d34c 62 */
wvd_vegt 0:af45bc81d34c 63 RIT(uint32_t ms);
wvd_vegt 0:af45bc81d34c 64
wvd_vegt 0:af45bc81d34c 65 /** Setup timing in ms.
wvd_vegt 0:af45bc81d34c 66 *
wvd_vegt 0:af45bc81d34c 67 * @parm ms the number of milliseconds between two interrupts.
wvd_vegt 0:af45bc81d34c 68 */
wvd_vegt 0:af45bc81d34c 69 void setup_ms(uint32_t ms);
wvd_vegt 0:af45bc81d34c 70
wvd_vegt 0:af45bc81d34c 71 /** Setup timing in us.
wvd_vegt 0:af45bc81d34c 72 *
wvd_vegt 0:af45bc81d34c 73 * @parm us the number of microseconds between two interrupts.
wvd_vegt 0:af45bc81d34c 74 */
wvd_vegt 0:af45bc81d34c 75 void setup_us(uint32_t us);
wvd_vegt 0:af45bc81d34c 76
wvd_vegt 0:af45bc81d34c 77 /** Attach custom interrupt handler replacing default.
wvd_vegt 0:af45bc81d34c 78 *
wvd_vegt 0:af45bc81d34c 79 * @parm fptr the new interrupt handler.
wvd_vegt 0:af45bc81d34c 80 */
wvd_vegt 0:af45bc81d34c 81 void attach(void(*fptr)(void));
wvd_vegt 0:af45bc81d34c 82
wvd_vegt 0:af45bc81d34c 83 /** Restore default interrupt handler.
wvd_vegt 0:af45bc81d34c 84 *
wvd_vegt 0:af45bc81d34c 85 */
wvd_vegt 0:af45bc81d34c 86 void detach(void);
wvd_vegt 0:af45bc81d34c 87
wvd_vegt 0:af45bc81d34c 88 /** Append function isr to global interrupt handler.
wvd_vegt 0:af45bc81d34c 89 *
wvd_vegt 0:af45bc81d34c 90 * @parm fptr the function isr to be called from within the default interrupt handler.
wvd_vegt 0:af45bc81d34c 91 */
wvd_vegt 0:af45bc81d34c 92 void append(void(*fptr)(void));
wvd_vegt 0:af45bc81d34c 93
wvd_vegt 0:af45bc81d34c 94 /** Remove function isr from global interrupt handler.
wvd_vegt 0:af45bc81d34c 95 *
wvd_vegt 0:af45bc81d34c 96 */
wvd_vegt 0:af45bc81d34c 97 void unappend();
wvd_vegt 0:af45bc81d34c 98
wvd_vegt 0:af45bc81d34c 99 /** Enable RIT & Interrupt
wvd_vegt 0:af45bc81d34c 100 *
wvd_vegt 0:af45bc81d34c 101 */
wvd_vegt 0:af45bc81d34c 102 void enable(void);
wvd_vegt 0:af45bc81d34c 103
wvd_vegt 0:af45bc81d34c 104 /** Disable RIT & Interrupt.
wvd_vegt 0:af45bc81d34c 105 *
wvd_vegt 0:af45bc81d34c 106 */
wvd_vegt 0:af45bc81d34c 107 void disable(void);
wvd_vegt 0:af45bc81d34c 108
wvd_vegt 0:af45bc81d34c 109 /** Power up RIT.
wvd_vegt 0:af45bc81d34c 110 *
wvd_vegt 0:af45bc81d34c 111 */
wvd_vegt 0:af45bc81d34c 112 void power_enable(void);
wvd_vegt 0:af45bc81d34c 113
wvd_vegt 0:af45bc81d34c 114 /** Power down RIT
wvd_vegt 0:af45bc81d34c 115 *
wvd_vegt 0:af45bc81d34c 116 * Note: Powering down might take some more effect when entering Deep Sleep,
wvd_vegt 0:af45bc81d34c 117 * accoring to errata.lpc1768.pdf.
wvd_vegt 0:af45bc81d34c 118 */
wvd_vegt 0:af45bc81d34c 119 void power_disable(void);
wvd_vegt 0:af45bc81d34c 120
wvd_vegt 0:af45bc81d34c 121 private:
wvd_vegt 0:af45bc81d34c 122
wvd_vegt 0:af45bc81d34c 123 /** There can only be one.
wvd_vegt 0:af45bc81d34c 124 *
wvd_vegt 0:af45bc81d34c 125 */
wvd_vegt 0:af45bc81d34c 126 static RIT *instance;
wvd_vegt 0:af45bc81d34c 127
wvd_vegt 0:af45bc81d34c 128 /** Storage for an appended function isr.
wvd_vegt 0:af45bc81d34c 129 *
wvd_vegt 0:af45bc81d34c 130 */
wvd_vegt 0:af45bc81d34c 131 void(*_rit_g_isr)(void);
wvd_vegt 0:af45bc81d34c 132
wvd_vegt 0:af45bc81d34c 133 /** The default (instance) isr.
wvd_vegt 0:af45bc81d34c 134 *
wvd_vegt 0:af45bc81d34c 135 * Note: this isr calls the static one so there is only be a single one.
wvd_vegt 0:af45bc81d34c 136 */
wvd_vegt 0:af45bc81d34c 137 void ritisr(void);
wvd_vegt 0:af45bc81d34c 138
wvd_vegt 0:af45bc81d34c 139 /** The actual (static) default isr.
wvd_vegt 0:af45bc81d34c 140 *
wvd_vegt 0:af45bc81d34c 141 */
wvd_vegt 0:af45bc81d34c 142 static void _ritisr(void);
wvd_vegt 0:af45bc81d34c 143
wvd_vegt 0:af45bc81d34c 144 /** Select the Peripheral Clock (PCLK_RIT) for the RIT
wvd_vegt 0:af45bc81d34c 145 * to be equal to the CCLK.
wvd_vegt 0:af45bc81d34c 146 *
wvd_vegt 0:af45bc81d34c 147 */
wvd_vegt 0:af45bc81d34c 148 void select_clk(void);
wvd_vegt 0:af45bc81d34c 149 };
wvd_vegt 0:af45bc81d34c 150
wvd_vegt 0:af45bc81d34c 151 #endif