[374] | 1 | /* mbed Microcontroller Library
|
---|
| 2 | * Copyright (c) 2018 ARM Limited
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | */
|
---|
| 16 | #include "hal/mbed_lp_ticker_wrapper.h"
|
---|
| 17 |
|
---|
| 18 | #if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
|
---|
| 19 |
|
---|
| 20 | #include "hal/LowPowerTickerWrapper.h"
|
---|
| 21 | #include "platform/mbed_critical.h"
|
---|
| 22 |
|
---|
| 23 | // Do not use SingletonPtr since this must be initialized in a critical section
|
---|
| 24 | static LowPowerTickerWrapper *ticker_wrapper;
|
---|
| 25 | static uint64_t ticker_wrapper_data[(sizeof(LowPowerTickerWrapper) + 7) / 8];
|
---|
| 26 | static bool init = false;
|
---|
| 27 |
|
---|
| 28 | static void lp_ticker_wrapper_init()
|
---|
| 29 | {
|
---|
| 30 | ticker_wrapper->init();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | static uint32_t lp_ticker_wrapper_read()
|
---|
| 34 | {
|
---|
| 35 | return ticker_wrapper->read();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | static void lp_ticker_wrapper_set_interrupt(timestamp_t timestamp)
|
---|
| 39 | {
|
---|
| 40 | ticker_wrapper->set_interrupt(timestamp);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | static void lp_ticker_wrapper_disable_interrupt()
|
---|
| 44 | {
|
---|
| 45 | ticker_wrapper->disable_interrupt();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | static void lp_ticker_wrapper_clear_interrupt()
|
---|
| 49 | {
|
---|
| 50 | ticker_wrapper->clear_interrupt();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | static void lp_ticker_wrapper_fire_interrupt()
|
---|
| 54 | {
|
---|
| 55 | ticker_wrapper->fire_interrupt();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | static const ticker_info_t *lp_ticker_wrapper_get_info()
|
---|
| 59 | {
|
---|
| 60 | return ticker_wrapper->get_info();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | static void lp_ticker_wrapper_free()
|
---|
| 64 | {
|
---|
| 65 | ticker_wrapper->free();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | static const ticker_interface_t lp_interface = {
|
---|
| 69 | lp_ticker_wrapper_init,
|
---|
| 70 | lp_ticker_wrapper_read,
|
---|
| 71 | lp_ticker_wrapper_disable_interrupt,
|
---|
| 72 | lp_ticker_wrapper_clear_interrupt,
|
---|
| 73 | lp_ticker_wrapper_set_interrupt,
|
---|
| 74 | lp_ticker_wrapper_fire_interrupt,
|
---|
| 75 | lp_ticker_wrapper_free,
|
---|
| 76 | lp_ticker_wrapper_get_info
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | void lp_ticker_wrapper_irq_handler(ticker_irq_handler_type handler)
|
---|
| 80 | {
|
---|
| 81 | core_util_critical_section_enter();
|
---|
| 82 |
|
---|
| 83 | if (!init) {
|
---|
| 84 | // Force ticker to initialize
|
---|
| 85 | get_lp_ticker_data();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | ticker_wrapper->irq_handler(handler);
|
---|
| 89 |
|
---|
| 90 | core_util_critical_section_exit();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | const ticker_data_t *get_lp_ticker_wrapper_data(const ticker_data_t *data)
|
---|
| 94 | {
|
---|
| 95 | core_util_critical_section_enter();
|
---|
| 96 |
|
---|
| 97 | if (!init) {
|
---|
| 98 | ticker_wrapper = new (ticker_wrapper_data) LowPowerTickerWrapper(data, &lp_interface, LPTICKER_DELAY_TICKS, LPTICKER_DELAY_TICKS);
|
---|
| 99 | init = true;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | core_util_critical_section_exit();
|
---|
| 103 |
|
---|
| 104 | return &ticker_wrapper->data;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void lp_ticker_wrapper_suspend()
|
---|
| 108 | {
|
---|
| 109 | if (!init) {
|
---|
| 110 | // Force ticker to initialize
|
---|
| 111 | get_lp_ticker_data();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | ticker_wrapper->suspend();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | void lp_ticker_wrapper_resume()
|
---|
| 118 | {
|
---|
| 119 | if (!init) {
|
---|
| 120 | // Force ticker to initialize
|
---|
| 121 | get_lp_ticker_data();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | ticker_wrapper->resume();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | #endif
|
---|