mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
Parent:
180:96ed750bd169
mbed-dev library. Release version 162

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1
<> 149:156823d33999 2 /** \addtogroup hal */
<> 149:156823d33999 3 /** @{*/
<> 144:ef7eb2e8f9f7 4 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 5 * Copyright (c) 2015 ARM Limited
<> 144:ef7eb2e8f9f7 6 *
<> 144:ef7eb2e8f9f7 7 * Licensed under the Apache License, Version 2.0 (the "License");
<> 144:ef7eb2e8f9f7 8 * you may not use this file except in compliance with the License.
<> 144:ef7eb2e8f9f7 9 * You may obtain a copy of the License at
<> 144:ef7eb2e8f9f7 10 *
<> 144:ef7eb2e8f9f7 11 * http://www.apache.org/licenses/LICENSE-2.0
<> 144:ef7eb2e8f9f7 12 *
<> 144:ef7eb2e8f9f7 13 * Unless required by applicable law or agreed to in writing, software
<> 144:ef7eb2e8f9f7 14 * distributed under the License is distributed on an "AS IS" BASIS,
<> 144:ef7eb2e8f9f7 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 144:ef7eb2e8f9f7 16 * See the License for the specific language governing permissions and
<> 144:ef7eb2e8f9f7 17 * limitations under the License.
<> 144:ef7eb2e8f9f7 18 */
<> 144:ef7eb2e8f9f7 19 #ifndef MBED_LPTICKER_API_H
<> 144:ef7eb2e8f9f7 20 #define MBED_LPTICKER_API_H
<> 144:ef7eb2e8f9f7 21
<> 144:ef7eb2e8f9f7 22 #include "device.h"
<> 144:ef7eb2e8f9f7 23
Anna Bridge 186:707f6e361f3e 24 #if DEVICE_LPTICKER
<> 144:ef7eb2e8f9f7 25
<> 149:156823d33999 26 #include "hal/ticker_api.h"
<> 144:ef7eb2e8f9f7 27
<> 144:ef7eb2e8f9f7 28 #ifdef __cplusplus
<> 144:ef7eb2e8f9f7 29 extern "C" {
<> 144:ef7eb2e8f9f7 30 #endif
<> 144:ef7eb2e8f9f7 31
<> 144:ef7eb2e8f9f7 32 /**
Anna Bridge 186:707f6e361f3e 33 * \defgroup hal_lp_ticker Low Power Ticker
Anna Bridge 186:707f6e361f3e 34 * Low level interface to the low power ticker of a target
Anna Bridge 186:707f6e361f3e 35 *
Anna Bridge 186:707f6e361f3e 36 * # Defined behavior
Anna Bridge 186:707f6e361f3e 37 * * Has a reported frequency between 4KHz and 64KHz - verified by ::lp_ticker_info_test
Anna Bridge 186:707f6e361f3e 38 * * Has a counter that is at least 12 bits wide - verified by ::lp_ticker_info_test
Anna Bridge 186:707f6e361f3e 39 * * Continues operating in deep sleep mode - verified by ::lp_ticker_deepsleep_test
Anna Bridge 186:707f6e361f3e 40 * * All behavior defined by the @ref hal_ticker_shared "ticker specification"
Anna Bridge 186:707f6e361f3e 41 *
Anna Bridge 186:707f6e361f3e 42 * # Undefined behavior
Anna Bridge 186:707f6e361f3e 43 * * See the @ref hal_ticker_shared "ticker specification"
Anna Bridge 186:707f6e361f3e 44 * * Calling any function other than lp_ticker_init after calling lp_ticker_free
Anna Bridge 186:707f6e361f3e 45 *
Anna Bridge 186:707f6e361f3e 46 * # Potential bugs
Anna Bridge 186:707f6e361f3e 47 * * Glitches due to ripple counter - Verified by ::lp_ticker_glitch_test
Anna Bridge 186:707f6e361f3e 48 *
Anna Bridge 186:707f6e361f3e 49 * @see hal_lp_ticker_tests
Anna Bridge 186:707f6e361f3e 50 *
<> 144:ef7eb2e8f9f7 51 * @{
<> 144:ef7eb2e8f9f7 52 */
<> 144:ef7eb2e8f9f7 53
Anna Bridge 186:707f6e361f3e 54 /**
Anna Bridge 186:707f6e361f3e 55 * \defgroup hal_lp_ticker_tests Low Power Ticker tests
Anna Bridge 186:707f6e361f3e 56 * Tests to validate the proper implementation of the low power ticker
Anna Bridge 186:707f6e361f3e 57 *
Anna Bridge 186:707f6e361f3e 58 * To run the low power ticker hal tests use the command:
Anna Bridge 186:707f6e361f3e 59 *
Anna Bridge 186:707f6e361f3e 60 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*,tests-mbed_hal-lp_ticker*
Anna Bridge 186:707f6e361f3e 61 *
Anna Bridge 186:707f6e361f3e 62 */
Anna Bridge 186:707f6e361f3e 63
Anna Bridge 180:96ed750bd169 64 typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
Anna Bridge 180:96ed750bd169 65
Anna Bridge 180:96ed750bd169 66 /** Set low power ticker IRQ handler
Anna Bridge 180:96ed750bd169 67 *
Anna Bridge 180:96ed750bd169 68 * @param ticker_irq_handler IRQ handler to be connected
Anna Bridge 180:96ed750bd169 69 *
Anna Bridge 180:96ed750bd169 70 * @return previous ticker IRQ handler
Anna Bridge 180:96ed750bd169 71 *
Anna Bridge 186:707f6e361f3e 72 * @note by default IRQ handler is set to ::ticker_irq_handler
Anna Bridge 180:96ed750bd169 73 * @note this function is primarily for testing purposes and it's not required part of HAL implementation
Anna Bridge 180:96ed750bd169 74 *
Anna Bridge 180:96ed750bd169 75 */
Anna Bridge 180:96ed750bd169 76 ticker_irq_handler_type set_lp_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler);
Anna Bridge 180:96ed750bd169 77
<> 144:ef7eb2e8f9f7 78 /** Get low power ticker's data
<> 144:ef7eb2e8f9f7 79 *
<> 144:ef7eb2e8f9f7 80 * @return The low power ticker data
<> 144:ef7eb2e8f9f7 81 */
<> 144:ef7eb2e8f9f7 82 const ticker_data_t* get_lp_ticker_data(void);
<> 144:ef7eb2e8f9f7 83
<> 144:ef7eb2e8f9f7 84 /** The wrapper for ticker_irq_handler, to pass lp ticker's data
<> 144:ef7eb2e8f9f7 85 *
<> 144:ef7eb2e8f9f7 86 */
<> 144:ef7eb2e8f9f7 87 void lp_ticker_irq_handler(void);
<> 144:ef7eb2e8f9f7 88
<> 144:ef7eb2e8f9f7 89 /* HAL lp ticker */
<> 144:ef7eb2e8f9f7 90
<> 144:ef7eb2e8f9f7 91 /** Initialize the low power ticker
<> 144:ef7eb2e8f9f7 92 *
Anna Bridge 186:707f6e361f3e 93 * Initialize or re-initialize the ticker. This resets all the
Anna Bridge 186:707f6e361f3e 94 * clocking and prescaler registers, along with disabling
Anna Bridge 186:707f6e361f3e 95 * the compare interrupt.
Anna Bridge 186:707f6e361f3e 96 *
Anna Bridge 186:707f6e361f3e 97 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 98 * @code
Anna Bridge 186:707f6e361f3e 99 * void lp_ticker_init()
Anna Bridge 186:707f6e361f3e 100 * {
Anna Bridge 186:707f6e361f3e 101 * // Enable clock gate so processor can read LPTMR registers
Anna Bridge 186:707f6e361f3e 102 * POWER_CTRL |= POWER_CTRL_LPTMR_Msk;
Anna Bridge 186:707f6e361f3e 103 *
Anna Bridge 186:707f6e361f3e 104 * // Disable the timer and ensure it is powered down
Anna Bridge 186:707f6e361f3e 105 * LPTMR_CTRL &= ~(LPTMR_CTRL_ENABLE_Msk | LPTMR_CTRL_COMPARE_ENABLE_Msk);
Anna Bridge 186:707f6e361f3e 106 *
Anna Bridge 186:707f6e361f3e 107 * // Configure divisors - no division necessary
Anna Bridge 186:707f6e361f3e 108 * LPTMR_PRESCALE = 0;
Anna Bridge 186:707f6e361f3e 109 * LPTMR_CTRL |= LPTMR_CTRL_ENABLE_Msk;
Anna Bridge 186:707f6e361f3e 110 *
Anna Bridge 186:707f6e361f3e 111 * // Install the interrupt handler
Anna Bridge 186:707f6e361f3e 112 * NVIC_SetVector(LPTMR_IRQn, (uint32_t)lp_ticker_irq_handler);
Anna Bridge 186:707f6e361f3e 113 * NVIC_EnableIRQ(LPTMR_IRQn);
Anna Bridge 186:707f6e361f3e 114 * }
Anna Bridge 186:707f6e361f3e 115 * @endcode
<> 144:ef7eb2e8f9f7 116 */
<> 144:ef7eb2e8f9f7 117 void lp_ticker_init(void);
<> 144:ef7eb2e8f9f7 118
Anna Bridge 186:707f6e361f3e 119 /** Deinitialize the lower power ticker
Anna Bridge 186:707f6e361f3e 120 *
Anna Bridge 186:707f6e361f3e 121 * Powerdown the lp ticker in preparation for sleep, powerdown, or reset.
Anna Bridge 186:707f6e361f3e 122 *
Anna Bridge 186:707f6e361f3e 123 * After calling this function no other ticker functions should be called except
Anna Bridge 186:707f6e361f3e 124 * lp_ticker_init(). Calling any function other than init after freeing is
Anna Bridge 186:707f6e361f3e 125 * undefined.
Anna Bridge 186:707f6e361f3e 126 *
Anna Bridge 186:707f6e361f3e 127 * @note This function stops the ticker from counting.
Anna Bridge 186:707f6e361f3e 128 */
Anna Bridge 186:707f6e361f3e 129 void lp_ticker_free(void);
Anna Bridge 186:707f6e361f3e 130
Anna Bridge 186:707f6e361f3e 131 /** Read the current tick
Anna Bridge 186:707f6e361f3e 132 *
Anna Bridge 186:707f6e361f3e 133 * If no rollover has occurred, the seconds passed since lp_ticker_init()
Anna Bridge 186:707f6e361f3e 134 * was called can be found by dividing the ticks returned by this function
Anna Bridge 186:707f6e361f3e 135 * by the frequency returned by ::lp_ticker_get_info.
<> 144:ef7eb2e8f9f7 136 *
Anna Bridge 186:707f6e361f3e 137 * @return The current timer's counter value in ticks
Anna Bridge 186:707f6e361f3e 138 *
Anna Bridge 186:707f6e361f3e 139 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 140 * @code
Anna Bridge 186:707f6e361f3e 141 * uint32_t lp_ticker_read()
Anna Bridge 186:707f6e361f3e 142 * {
Anna Bridge 186:707f6e361f3e 143 * uint16_t count;
Anna Bridge 186:707f6e361f3e 144 * uint16_t last_count;
Anna Bridge 186:707f6e361f3e 145 *
Anna Bridge 186:707f6e361f3e 146 * // Loop until the same tick is read twice since this
Anna Bridge 186:707f6e361f3e 147 * // is ripple counter on a different clock domain.
Anna Bridge 186:707f6e361f3e 148 * count = LPTMR_COUNT;
Anna Bridge 186:707f6e361f3e 149 * do {
Anna Bridge 186:707f6e361f3e 150 * last_count = count;
Anna Bridge 186:707f6e361f3e 151 * count = LPTMR_COUNT;
Anna Bridge 186:707f6e361f3e 152 * } while (last_count != count);
Anna Bridge 186:707f6e361f3e 153 *
Anna Bridge 186:707f6e361f3e 154 * return count;
Anna Bridge 186:707f6e361f3e 155 * }
Anna Bridge 186:707f6e361f3e 156 * @endcode
<> 144:ef7eb2e8f9f7 157 */
<> 144:ef7eb2e8f9f7 158 uint32_t lp_ticker_read(void);
<> 144:ef7eb2e8f9f7 159
<> 144:ef7eb2e8f9f7 160 /** Set interrupt for specified timestamp
<> 144:ef7eb2e8f9f7 161 *
Anna Bridge 186:707f6e361f3e 162 * @param timestamp The time in ticks to be set
Anna Bridge 186:707f6e361f3e 163 *
Anna Bridge 186:707f6e361f3e 164 * @note no special handling needs to be done for times in the past
Anna Bridge 186:707f6e361f3e 165 * as the common timer code will detect this and call
Anna Bridge 186:707f6e361f3e 166 * lp_ticker_fire_interrupt() if this is the case
Anna Bridge 186:707f6e361f3e 167 *
Anna Bridge 186:707f6e361f3e 168 * @note calling this function with timestamp of more than the supported
Anna Bridge 186:707f6e361f3e 169 * number of bits returned by ::lp_ticker_get_info results in undefined
Anna Bridge 186:707f6e361f3e 170 * behavior.
Anna Bridge 186:707f6e361f3e 171 *
Anna Bridge 186:707f6e361f3e 172 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 173 * @code
Anna Bridge 186:707f6e361f3e 174 * void lp_ticker_set_interrupt(timestamp_t timestamp)
Anna Bridge 186:707f6e361f3e 175 * {
Anna Bridge 186:707f6e361f3e 176 * LPTMR_COMPARE = timestamp;
Anna Bridge 186:707f6e361f3e 177 * LPTMR_CTRL |= LPTMR_CTRL_COMPARE_ENABLE_Msk;
Anna Bridge 186:707f6e361f3e 178 * }
Anna Bridge 186:707f6e361f3e 179 * @endcode
<> 144:ef7eb2e8f9f7 180 */
<> 144:ef7eb2e8f9f7 181 void lp_ticker_set_interrupt(timestamp_t timestamp);
<> 144:ef7eb2e8f9f7 182
<> 144:ef7eb2e8f9f7 183 /** Disable low power ticker interrupt
<> 144:ef7eb2e8f9f7 184 *
Anna Bridge 186:707f6e361f3e 185 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 186 * @code
Anna Bridge 186:707f6e361f3e 187 * void lp_ticker_disable_interrupt(void)
Anna Bridge 186:707f6e361f3e 188 * {
Anna Bridge 186:707f6e361f3e 189 * // Disable the compare interrupt
Anna Bridge 186:707f6e361f3e 190 * LPTMR_CTRL &= ~LPTMR_CTRL_COMPARE_ENABLE_Msk;
Anna Bridge 186:707f6e361f3e 191 * }
Anna Bridge 186:707f6e361f3e 192 * @endcode
<> 144:ef7eb2e8f9f7 193 */
<> 144:ef7eb2e8f9f7 194 void lp_ticker_disable_interrupt(void);
<> 144:ef7eb2e8f9f7 195
<> 144:ef7eb2e8f9f7 196 /** Clear the low power ticker interrupt
<> 144:ef7eb2e8f9f7 197 *
Anna Bridge 186:707f6e361f3e 198 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 199 * @code
Anna Bridge 186:707f6e361f3e 200 * void lp_ticker_clear_interrupt(void)
Anna Bridge 186:707f6e361f3e 201 * {
Anna Bridge 186:707f6e361f3e 202 * // Write to the ICR (interrupt clear register) of the LPTMR
Anna Bridge 186:707f6e361f3e 203 * LPTMR_ICR = LPTMR_ICR_COMPARE_Msk;
Anna Bridge 186:707f6e361f3e 204 * }
Anna Bridge 186:707f6e361f3e 205 * @endcode
<> 144:ef7eb2e8f9f7 206 */
<> 144:ef7eb2e8f9f7 207 void lp_ticker_clear_interrupt(void);
<> 144:ef7eb2e8f9f7 208
AnnaBridge 174:b96e65c34a4d 209 /** Set pending interrupt that should be fired right away.
AnnaBridge 174:b96e65c34a4d 210 *
Anna Bridge 186:707f6e361f3e 211 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 212 * @code
Anna Bridge 186:707f6e361f3e 213 * void lp_ticker_fire_interrupt(void)
Anna Bridge 186:707f6e361f3e 214 * {
Anna Bridge 186:707f6e361f3e 215 * NVIC_SetPendingIRQ(LPTMR_IRQn);
Anna Bridge 186:707f6e361f3e 216 * }
Anna Bridge 186:707f6e361f3e 217 * @endcode
AnnaBridge 174:b96e65c34a4d 218 */
AnnaBridge 174:b96e65c34a4d 219 void lp_ticker_fire_interrupt(void);
AnnaBridge 174:b96e65c34a4d 220
Anna Bridge 180:96ed750bd169 221 /** Get frequency and counter bits of this ticker.
Anna Bridge 180:96ed750bd169 222 *
Anna Bridge 186:707f6e361f3e 223 * Pseudo Code:
Anna Bridge 186:707f6e361f3e 224 * @code
Anna Bridge 186:707f6e361f3e 225 * const ticker_info_t* lp_ticker_get_info()
Anna Bridge 186:707f6e361f3e 226 * {
Anna Bridge 186:707f6e361f3e 227 * static const ticker_info_t info = {
Anna Bridge 186:707f6e361f3e 228 * 32768, // 32KHz
Anna Bridge 186:707f6e361f3e 229 * 16 // 16 bit counter
Anna Bridge 186:707f6e361f3e 230 * };
Anna Bridge 186:707f6e361f3e 231 * return &info;
Anna Bridge 186:707f6e361f3e 232 * }
Anna Bridge 186:707f6e361f3e 233 * @endcode
Anna Bridge 180:96ed750bd169 234 */
Anna Bridge 180:96ed750bd169 235 const ticker_info_t* lp_ticker_get_info(void);
Anna Bridge 180:96ed750bd169 236
<> 144:ef7eb2e8f9f7 237 /**@}*/
<> 144:ef7eb2e8f9f7 238
<> 144:ef7eb2e8f9f7 239 #ifdef __cplusplus
<> 144:ef7eb2e8f9f7 240 }
<> 144:ef7eb2e8f9f7 241 #endif
<> 144:ef7eb2e8f9f7 242
<> 144:ef7eb2e8f9f7 243 #endif
<> 144:ef7eb2e8f9f7 244
<> 144:ef7eb2e8f9f7 245 #endif
<> 149:156823d33999 246
<> 149:156823d33999 247 /** @}*/