Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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