Pedro Correia / mbed-dev

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lp_ticker_api.h Source File

lp_ticker_api.h

00001 
00002 /** \addtogroup hal */
00003 /** @{*/
00004 /* mbed Microcontroller Library
00005  * Copyright (c) 2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 #ifndef MBED_LPTICKER_API_H
00020 #define MBED_LPTICKER_API_H
00021 
00022 #include "device.h"
00023 
00024 #if DEVICE_LPTICKER
00025 
00026 #include "hal/ticker_api.h"
00027 
00028 #ifdef __cplusplus
00029 extern "C" {
00030 #endif
00031 
00032 /**
00033  * \defgroup hal_lp_ticker Low Power Ticker
00034  * Low level interface to the low power ticker of a target
00035  *
00036  * # Defined behavior
00037  * * Has a reported frequency between 4KHz and 64KHz - verified by ::lp_ticker_info_test
00038  * * Has a counter that is at least 12 bits wide - verified by ::lp_ticker_info_test
00039  * * Continues operating in deep sleep mode - verified by ::lp_ticker_deepsleep_test
00040  * * All behavior defined by the @ref hal_ticker_shared "ticker specification"
00041  *
00042  * # Undefined behavior
00043  * * See the @ref hal_ticker_shared "ticker specification"
00044  * * Calling any function other than lp_ticker_init after calling lp_ticker_free
00045  *
00046  * # Potential bugs
00047  * * Glitches due to ripple counter - Verified by ::lp_ticker_glitch_test
00048  *
00049  * @see hal_lp_ticker_tests
00050  *
00051  * @{
00052  */
00053 
00054 /**
00055  * \defgroup hal_lp_ticker_tests Low Power Ticker tests
00056  * Tests to validate the proper implementation of the low power ticker
00057  *
00058  * To run the low power ticker hal tests use the command:
00059  *
00060  *     mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*,tests-mbed_hal-lp_ticker*
00061  *
00062  */
00063 
00064 typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
00065 
00066 /** Set low power ticker IRQ handler
00067  *
00068  * @param ticker_irq_handler IRQ handler to be connected
00069  *
00070  * @return previous ticker IRQ handler
00071  *
00072  * @note by default IRQ handler is set to ::ticker_irq_handler
00073  * @note this function is primarily for testing purposes and it's not required part of HAL implementation
00074  *
00075  */
00076 ticker_irq_handler_type set_lp_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler);
00077 
00078 /** Get low power ticker's data
00079  *
00080  * @return The low power ticker data
00081  */
00082 const ticker_data_t* get_lp_ticker_data(void);
00083 
00084 /** The wrapper for ticker_irq_handler, to pass lp ticker's data
00085  *
00086  */
00087 void lp_ticker_irq_handler(void);
00088 
00089 /* HAL lp ticker */
00090 
00091 /** Initialize the low power ticker
00092  *
00093  * Initialize or re-initialize the ticker. This resets all the
00094  * clocking and prescaler registers, along with disabling
00095  * the compare interrupt.
00096  *
00097  * Pseudo Code:
00098  * @code
00099  * void lp_ticker_init()
00100  * {
00101  *     // Enable clock gate so processor can read LPTMR registers
00102  *     POWER_CTRL |= POWER_CTRL_LPTMR_Msk;
00103  *
00104  *     // Disable the timer and ensure it is powered down
00105  *     LPTMR_CTRL &= ~(LPTMR_CTRL_ENABLE_Msk | LPTMR_CTRL_COMPARE_ENABLE_Msk);
00106  *
00107  *     // Configure divisors - no division necessary
00108  *     LPTMR_PRESCALE = 0;
00109  *     LPTMR_CTRL |= LPTMR_CTRL_ENABLE_Msk;
00110  *
00111  *     // Install the interrupt handler
00112  *     NVIC_SetVector(LPTMR_IRQn, (uint32_t)lp_ticker_irq_handler);
00113  *     NVIC_EnableIRQ(LPTMR_IRQn);
00114  * }
00115  * @endcode
00116  */
00117 void lp_ticker_init(void);
00118 
00119 /** Deinitialize the lower power ticker
00120  *
00121  * Powerdown the lp ticker in preparation for sleep, powerdown, or reset.
00122  *
00123  * After calling this function no other ticker functions should be called except
00124  * lp_ticker_init(). Calling any function other than init after freeing is
00125  * undefined.
00126  *
00127  * @note This function stops the ticker from counting.
00128  */
00129 void lp_ticker_free(void);
00130 
00131 /** Read the current tick
00132  *
00133  * If no rollover has occurred, the seconds passed since lp_ticker_init()
00134  * was called can be found by dividing the ticks returned by this function
00135  * by the frequency returned by ::lp_ticker_get_info.
00136  *
00137  * @return The current timer's counter value in ticks
00138  *
00139  * Pseudo Code:
00140  * @code
00141  * uint32_t lp_ticker_read()
00142  * {
00143  *     uint16_t count;
00144  *     uint16_t last_count;
00145  *
00146  *     // Loop until the same tick is read twice since this
00147  *     // is ripple counter on a different clock domain.
00148  *     count = LPTMR_COUNT;
00149  *     do {
00150  *         last_count = count;
00151  *         count = LPTMR_COUNT;
00152  *     } while (last_count != count);
00153  *
00154  *     return count;
00155  * }
00156  * @endcode
00157  */
00158 uint32_t lp_ticker_read(void);
00159 
00160 /** Set interrupt for specified timestamp
00161  *
00162  * @param timestamp The time in ticks to be set
00163  *
00164  * @note no special handling needs to be done for times in the past
00165  * as the common timer code will detect this and call
00166  * lp_ticker_fire_interrupt() if this is the case
00167  *
00168  * @note calling this function with timestamp of more than the supported
00169  * number of bits returned by ::lp_ticker_get_info results in undefined
00170  * behavior.
00171  *
00172  * Pseudo Code:
00173  * @code
00174  * void lp_ticker_set_interrupt(timestamp_t timestamp)
00175  * {
00176  *     LPTMR_COMPARE = timestamp;
00177  *     LPTMR_CTRL |= LPTMR_CTRL_COMPARE_ENABLE_Msk;
00178  * }
00179  * @endcode
00180  */
00181 void lp_ticker_set_interrupt(timestamp_t timestamp);
00182 
00183 /** Disable low power ticker interrupt
00184  *
00185  * Pseudo Code:
00186  * @code
00187  * void lp_ticker_disable_interrupt(void)
00188  * {
00189  *     // Disable the compare interrupt
00190  *     LPTMR_CTRL &= ~LPTMR_CTRL_COMPARE_ENABLE_Msk;
00191  * }
00192  * @endcode
00193  */
00194 void lp_ticker_disable_interrupt(void);
00195 
00196 /** Clear the low power ticker interrupt
00197  *
00198  * Pseudo Code:
00199  * @code
00200  * void lp_ticker_clear_interrupt(void)
00201  * {
00202  *     // Write to the ICR (interrupt clear register) of the LPTMR
00203  *     LPTMR_ICR = LPTMR_ICR_COMPARE_Msk;
00204  * }
00205  * @endcode
00206  */
00207 void lp_ticker_clear_interrupt(void);
00208 
00209 /** Set pending interrupt that should be fired right away.
00210  * 
00211  * Pseudo Code:
00212  * @code
00213  * void lp_ticker_fire_interrupt(void)
00214  * {
00215  *     NVIC_SetPendingIRQ(LPTMR_IRQn);
00216  * }
00217  * @endcode
00218  */
00219 void lp_ticker_fire_interrupt(void);
00220 
00221 /** Get frequency and counter bits of this ticker.
00222  *
00223  * Pseudo Code:
00224  * @code
00225  * const ticker_info_t* lp_ticker_get_info()
00226  * {
00227  *     static const ticker_info_t info = {
00228  *         32768,      // 32KHz
00229  *         16          // 16 bit counter
00230  *     };
00231  *     return &info;
00232  * }
00233  * @endcode
00234  */
00235 const ticker_info_t* lp_ticker_get_info(void);
00236 
00237 /**@}*/
00238 
00239 #ifdef __cplusplus
00240 }
00241 #endif
00242 
00243 #endif
00244 
00245 #endif
00246 
00247 /** @}*/