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.cpp Source File

RIT.cpp

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 #include "LPC17xx.h"
00024 #include "RIT.h"
00025 #include "mbed.h"
00026 
00027 //Note: Code Ideas taken from BLDC and ADC Library.
00028 
00029 //TODO Document RIT.h
00030 //TODO Publish as Library
00031 
00032 RIT *RIT::instance;
00033 
00034 //------------------------------------------------------------------------------
00035 //Public Stuff.
00036 //------------------------------------------------------------------------------
00037 
00038 RIT::RIT(uint32_t ms) {
00039     //Default NULL global custom isr
00040     _rit_g_isr = NULL;
00041 
00042     // Set RIT interrupt priority
00043     NVIC_SetPriority(RIT_IRQn, 11);
00044 
00045     // Power the RIT
00046     power_enable();
00047 
00048     // Select perhiperal clk
00049     select_clk();
00050 
00051     // Set Clock
00052     setup_ms(ms);
00053 
00054     // Set counter clear/reset after interrupt
00055     LPC_RIT->RICTRL |= (2L); //RITENCLR
00056 
00057     // Connect the RIT interrupt to the interrupt handler
00058     instance = this;
00059     NVIC_SetVector(RIT_IRQn, (uint32_t)&_ritisr);
00060 
00061     // Disable the RIT interrupt
00062     disable();
00063 }
00064 
00065 //Setup Timing in ms.
00066 void RIT::setup_ms(uint32_t ms) {
00067     LPC_RIT->RICOMPVAL = (uint32_t)(((SYSCLK / ONEMHZ) * ms * 1000)-1);
00068     LPC_RIT->RICOUNTER = 0;
00069 }
00070 
00071 //Setup Timing in us.
00072 void RIT::setup_us(uint32_t us) {
00073     LPC_RIT->RICOMPVAL = (uint32_t)(((SYSCLK / ONEMHZ) * us)-1);
00074     LPC_RIT->RICOUNTER = 0;
00075 }
00076 
00077 //Attach custom interrupt handler replacing default
00078 void RIT::attach(void(*fptr)(void)) {
00079     //* Attach IRQ
00080     NVIC_SetVector(ADC_IRQn, (uint32_t)fptr);
00081 }
00082 
00083 //Restore default interrupt handler
00084 void RIT::detach(void) {
00085     //* Attach IRQ
00086     instance = this;
00087 
00088     NVIC_SetVector(ADC_IRQn, (uint32_t)&_ritisr);
00089 }
00090 
00091 //Unappend global interrupt handler to function isr
00092 void RIT::append(void(*fptr)()) {
00093     _rit_g_isr = fptr;
00094 }
00095 
00096 //Detach global interrupt handler to function isr
00097 void RIT::unappend() {
00098     _rit_g_isr = NULL;
00099 }
00100 
00101 void RIT::enable(void) {
00102     // Enable RIT interrupt
00103     NVIC_EnableIRQ(RIT_IRQn);
00104 
00105     // Enable the RIT
00106     LPC_RIT->RICTRL |= (8L); //RITEN;
00107 }
00108 
00109 void RIT::disable(void) {
00110     // Disable RIT interrupt
00111     NVIC_DisableIRQ(RIT_IRQn);
00112 
00113     // Disable the RIT
00114     LPC_RIT->RICTRL &= ~(8L); //RITEN;
00115 }
00116 
00117 void RIT::power_enable(void) {
00118     // Power the TRIT
00119     LPC_SC->PCONP |= (1L<<16); //PCRIT
00120 }
00121 
00122 void RIT::power_disable(void) {
00123     // Powerdown the RIT
00124     LPC_SC->PCONP &= ~(1L<<16); //PCRIT
00125 }
00126 //------------------------------------------------------------------------------
00127 //Private Stuff.
00128 //------------------------------------------------------------------------------
00129 
00130 void RIT::_ritisr(void) {
00131     instance->ritisr();
00132 }
00133 
00134 void RIT::ritisr(void) {
00135     // RIT interrupt occured
00136     if ((LPC_RIT->RICTRL & 1L) == 1L) //RITINT
00137         // Clear the RIT interrupt
00138         LPC_RIT->RICTRL |= 1L;
00139     
00140     //Call User defined interrupt handlers if any
00141     if (_rit_g_isr != NULL)
00142         _rit_g_isr();
00143 }
00144 
00145 void RIT::select_clk(void) {
00146     // Timer0 perhiperal clock select (01: PCLK Peripheral = CCLK)
00147     LPC_SC->PCLKSEL1 &= ~(3L << 26); // Clear PCLK_RIT bits;
00148     LPC_SC->PCLKSEL1 |=  (1L << 26); // Set PCLK_RIT bits to 0x01;
00149 }