Hexley Ball / CaptureT0_11U24
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers capturet0_11u24.cpp Source File

capturet0_11u24.cpp

00001  /* mbed Library - Capture driver for CT32B0
00002  * Copyright (c) 2010
00003  * released under MIT license http://mbed.org/licence/mit
00004  */
00005  /***********************************************************************//**
00006  * @file        capturet1_11u24.cpp
00007  * @brief       Driver file for the CAP1 function of CT32B0 in the  mbed 11U24. 
00008  * @version     0.0
00009  * @date        05 Feb 2014
00010  * @author      
00011  **************************************************************************/
00012 #include "mbed.h"
00013 #include "capturet0_11u24.h"
00014 
00015 Capture *Capture::instance;
00016 
00017 /*********************************************************************//**
00018  * @brief       Create a Capture object and configure it. 
00019  * @param       None   Uses p7 as the Capture input
00020  * @return      None
00021  **********************************************************************/
00022 Capture::Capture(void)
00023 {       
00024     /* Set up clock and power, using default peripheral system clock divider */   
00025     LPC_SYSCON->SYSAHBCLKCTRL |=   CT32B0_CLK_ENABLE; // See page 30 of the 11U24 User Guide
00026     // Check the clock divider ratio by reading   LPC_SYSCON->SYSAHBCLKDIV ls byte
00027     LPC_CT32B0->PR  =  0;                           // Prescale counter divide by 1
00028        
00029     /* Configure IOCON for the proper function of the capture pin. Use a pull-up.     */
00030     LPC_IOCON->PIO1_29 = (CT32B0_CAP1 | CAP_PULLUP); // See page 114 of the 11U24 User Guide   
00031               
00032     // Select the capture edge and int mode
00033     LPC_CT32B0->CCR =   CCR_CAP1FE  | CCR_CAP1I;    // Select inputs and interrupts. See page 345
00034     
00035     // Set up the rising edge clear, using timer mode
00036     LPC_CT32B0->CTCR = CTCR_CTM_TIMER | CTCR_ENCC | CTCR_SEICC_CAP1RE; // See page 350
00037     
00038         
00039     // Clear interrupt flag
00040     LPC_CT32B0->IR = IR_CR1INT;     // See page 342             
00041   
00042     /* preemption = 1, sub-priority = 1 */
00043 //    NVIC_SetPriority(TIMER2_IRQn, ((0x01<<3)|0x01));
00044 //     NVIC_SetPriority(TIMER2_IRQn, 0);          // Set to top priority to minimize interference from tickers.
00045      
00046     //* Attach IRQ
00047     instance = this;
00048     NVIC_SetVector(TIMER_32_0_IRQn, (uint32_t)&_Captureisr);                   
00049   
00050 }
00051 
00052 /*********************************************************************//**
00053  * @brief        Start capturing data. Average the specified number of samples. 
00054  * @param        (int) numsamples    Log base 2 of the number of samples to be averaged
00055  * @return        None
00056  **********************************************************************/
00057 void Capture::Start(int numsamples) {
00058     Nsamples = numsamples;
00059     capturedata = 0;                        // Clear the accumulator
00060     flag = 0;                               // No data ready
00061     
00062     /* Enable interrupt for CAPTURE  */
00063     NVIC_EnableIRQ(TIMER_32_0_IRQn); 
00064     
00065     // Start the timer
00066     LPC_CT32B0->TCR =  TCR_CEN; // enable
00067 }
00068 
00069 /*********************************************************************//**
00070  * @brief        Read (accumulated) sample count. 
00071  * 
00072  * @return       (unsigned int) Accumulated capture value
00073  **********************************************************************/
00074 unsigned int Capture::Read(void) {
00075     flag = 0;                   // Clear previous status (or error)
00076     return capturedata;
00077 }
00078 
00079 /*********************************************************************//**
00080  * @brief        Check for accumulation complete (data is ready to read). 
00081  * 
00082  * @return       (unsigned int) Accumulated capture value
00083  **********************************************************************/
00084 int Capture::Ready(void) {
00085     return flag;
00086 }
00087 
00088 
00089 /* Capture isr instantiator */    
00090 void Capture::_Captureisr(void)
00091 {
00092     instance->Captureisr();
00093 }
00094 
00095 /*********************************************************************//**
00096  * @brief        Capture interrupt service routine. Handles accumulation.
00097  * 
00098  * @param[in]    none
00099  *                                 
00100  * @return       none
00101  **********************************************************************/
00102 void Capture::Captureisr(void) {
00103     static unsigned int samplecount = 0;   // Counts passes
00104     static unsigned int capturesum = 0;    // Accumulates the readings
00105     
00106     if(LPC_CT32B0->IR & IR_CR1INT) {
00107         LPC_CT32B0->IR |= IR_CR1INT;       // Clear the interrupt request
00108         capturesum += LPC_CT32B0->CR2;     // Read and accumulate the captured results
00109                                             // It is necessary to use "CR2" in order to get the proper
00110                                             //  offset from the base address (an issue with CMSIS)
00111         if(++samplecount == Nsamples) {
00112             capturedata = capturesum;      // Update the output data
00113             samplecount = 0;               // Restart the pass counter
00114             capturesum = 0;                // Re-initialize the running total
00115             if(flag != 0) flag = -1;       // Make flag negative on an overrun
00116             else flag = 1;                 // Set flag to 1 if no error
00117         }     
00118     }
00119     return;
00120 }