diff --git a/TESTS/mbed_hal/us_ticker/main.cpp b/TESTS/mbed_hal/us_ticker/main.cpp index c701066f703..7df5f80bfc0 100644 --- a/TESTS/mbed_hal/us_ticker/main.cpp +++ b/TESTS/mbed_hal/us_ticker/main.cpp @@ -34,6 +34,11 @@ void us_ticker_info_test() TEST_ASSERT(p_ticker_info->frequency >= 250000); TEST_ASSERT(p_ticker_info->frequency <= 8000000); TEST_ASSERT(p_ticker_info->bits >= 16); + +#ifdef US_TICKER_PERIOD_NUM + TEST_ASSERT_UINT32_WITHIN(1, 1000000 * US_TICKER_PERIOD_DEN / US_TICKER_PERIOD_NUM, p_ticker_info->frequency); + TEST_ASSERT_EQUAL_UINT32(US_TICKER_MASK, ((uint64_t)1 << p_ticker_info->bits) - 1); +#endif } utest::v1::status_t test_setup(const size_t number_of_cases) diff --git a/components/TARGET_PSA/services/storage/common/psa_storage_common_impl.cpp b/components/TARGET_PSA/services/storage/common/psa_storage_common_impl.cpp index 2f38679ead8..d8ff8647f9d 100644 --- a/components/TARGET_PSA/services/storage/common/psa_storage_common_impl.cpp +++ b/components/TARGET_PSA/services/storage/common/psa_storage_common_impl.cpp @@ -127,7 +127,7 @@ static psa_status_t convert_status(int status) * \param n[in] number of bits to shift right * \return the result */ -MBED_FORCEINLINE uint32_t lsr32(uint32_t x, uint32_t n) +static MBED_FORCEINLINE uint32_t lsr32(uint32_t x, uint32_t n) { return x >> n; } @@ -140,7 +140,7 @@ MBED_FORCEINLINE uint32_t lsr32(uint32_t x, uint32_t n) * \param n[in] number of bits to shift right * \return the result */ -MBED_FORCEINLINE uint64_t lsr64(uint64_t x, uint32_t n) +static MBED_FORCEINLINE uint64_t lsr64(uint64_t x, uint32_t n) { return x >> n; } diff --git a/hal/mbed_us_ticker_api.c b/hal/mbed_us_ticker_api.c index 567ade73488..777a5e41cbb 100644 --- a/hal/mbed_us_ticker_api.c +++ b/hal/mbed_us_ticker_api.c @@ -16,6 +16,7 @@ */ #include +#include "platform/mbed_atomic.h" #include "hal/us_ticker_api.h" #if DEVICE_USTICKER @@ -24,15 +25,54 @@ static ticker_event_queue_t events = { 0 }; static ticker_irq_handler_type irq_handler = ticker_irq_handler; +#if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + +// If we are initializing at boot, we want the timer to be +// always-on, so we block any attempt to free it. We do need +// to pass through init(), as that needs to reset pending +// interrupts. +static void block_us_ticker_free() +{ +} + +#else // MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + +bool _us_ticker_initialized; + +// If we are not initializing at boot, we want to track +// whether the timer has been initialized. This permits +// a fast path for wait_us. +static void note_us_ticker_init() +{ + us_ticker_init(); + core_util_atomic_store_bool(&_us_ticker_initialized, true); +} + +static void note_us_ticker_free() +{ + core_util_atomic_store_bool(&_us_ticker_initialized, false); + us_ticker_free(); +} + +#endif // MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + static const ticker_interface_t us_interface = { +#if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT .init = us_ticker_init, +#else + .init = note_us_ticker_init, +#endif .read = us_ticker_read, .disable_interrupt = us_ticker_disable_interrupt, .clear_interrupt = us_ticker_clear_interrupt, .set_interrupt = us_ticker_set_interrupt, .fire_interrupt = us_ticker_fire_interrupt, .get_info = us_ticker_get_info, - .free = us_ticker_free, +#if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + .free = block_us_ticker_free, +#else + .free = note_us_ticker_free, +#endif .runs_in_deep_sleep = false, }; diff --git a/hal/us_ticker_api.h b/hal/us_ticker_api.h index ca43e74dc72..f62a06d189f 100644 --- a/hal/us_ticker_api.h +++ b/hal/us_ticker_api.h @@ -41,6 +41,21 @@ extern "C" { * * @see hal_us_ticker_tests * + * # Compile-time optimization macros + * + * To permit compile-time optimization, particularly of wait_us, the following macros should + * be defined by a target's device.h: + * + * US_TICKER_PERIOD_NUM, US_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator) + * of the ticker period to a microsecond. For example, an 8MHz ticker would have NUM = 1, DEN = 8; + * a 1MHz ticker would have NUM = 1, DEN = 1; a 250kHz ticker would have NUM = 4, DEN = 1. + * Both numerator and denominator must be 16 bits or less. + * + * US_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker. + * + * If any are defined, all 3 must be defined, and the macros are checked for consistency with + * us_ticker_get_info by test ::us_ticker_info_test. + * @{ */ @@ -74,6 +89,7 @@ extern "C" { * Verified by ::ticker_fire_now_test * * The ticker operations ticker_read, ticker_clear_interrupt, ticker_set_interrupt and ticker_fire_interrupt * take less than 20us to complete - Verified by ::ticker_speed_test + * * The ticker operations ticker_init and ticker_read are atomic. * * # Undefined behavior * * Calling any function other than ticker_init before the initialization of the ticker @@ -210,7 +226,7 @@ void us_ticker_free(void); * } * @endcode */ -uint32_t us_ticker_read(void); +uint32_t (us_ticker_read)(void); /** Set interrupt for specified timestamp * diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index 8a5e35fffc0..fa4951bfbb1 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -483,6 +483,9 @@ extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags) if (!mbed_sdk_inited) { mbed_copy_nvic(); mbed_sdk_init(); +#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + us_ticker_init(); +#endif mbed_sdk_inited = 1; } #endif diff --git a/platform/mbed_sdk_boot.c b/platform/mbed_sdk_boot.c index e56d7d07857..9d6694fd196 100644 --- a/platform/mbed_sdk_boot.c +++ b/platform/mbed_sdk_boot.c @@ -19,6 +19,7 @@ #include #include #include "cmsis.h" +#include "hal/us_ticker_api.h" /* This startup is for mbed 2 baremetal. There is no config for RTOS for mbed 2, * therefore we protect this file with MBED_CONF_RTOS_PRESENT @@ -82,6 +83,9 @@ void _platform_post_stackheap_init(void) { mbed_copy_nvic(); mbed_sdk_init(); +#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + us_ticker_init(); +#endif } #elif defined (__GNUC__) @@ -92,6 +96,9 @@ void software_init_hook(void) { mbed_copy_nvic(); mbed_sdk_init(); +#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + us_ticker_init(); +#endif software_init_hook_rtos(); } @@ -107,6 +114,9 @@ int __wrap_main(void) int __low_level_init(void) { mbed_copy_nvic(); +#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + us_ticker_init(); +#endif return 1; } diff --git a/platform/mbed_toolchain.h b/platform/mbed_toolchain.h index b513c71fef5..a9cccabb9f3 100644 --- a/platform/mbed_toolchain.h +++ b/platform/mbed_toolchain.h @@ -277,11 +277,11 @@ */ #ifndef MBED_FORCEINLINE #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) -#define MBED_FORCEINLINE static inline __attribute__((always_inline)) +#define MBED_FORCEINLINE inline __attribute__((always_inline)) #elif defined(__ICCARM__) -#define MBED_FORCEINLINE _Pragma("inline=forced") static +#define MBED_FORCEINLINE _Pragma("inline=forced") #else -#define MBED_FORCEINLINE static inline +#define MBED_FORCEINLINE inline #endif #endif diff --git a/platform/mbed_wait_api.h b/platform/mbed_wait_api.h index d0463e5da85..a614212092e 100644 --- a/platform/mbed_wait_api.h +++ b/platform/mbed_wait_api.h @@ -25,6 +25,9 @@ #ifndef MBED_WAIT_API_H #define MBED_WAIT_API_H +#include "platform/mbed_atomic.h" +#include "device.h" + #ifdef __cplusplus extern "C" { #endif @@ -115,6 +118,48 @@ void wait_us(int us); */ void wait_ns(unsigned int ns); +/* Optimize if we know the rate */ +#if DEVICE_USTICKER && defined US_TICKER_PERIOD_NUM +void _wait_us_ticks(uint32_t ticks); +void _wait_us_generic(unsigned int us); + +/* Further optimization if we know us_ticker is always running */ +#if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT +#define _us_ticker_is_initialized true +#else +extern bool _us_ticker_initialized; +#define _us_ticker_is_initialized core_util_atomic_load_bool(&_us_ticker_initialized) +#endif + +#if US_TICKER_PERIOD_DEN == 1 && (US_TICKER_MASK * US_TICKER_PERIOD_NUM) >= 0xFFFFFFFF +/* Ticker is wide and slow enough to have full 32-bit range - can always use it directly */ +#define _us_is_small_enough(us) true +#else +/* Threshold is determined by specification of us_ticker_api.h - smallest possible + * time range for the us_ticker is 16-bit 8MHz, which gives 8192us. This also leaves + * headroom for the multiplication in 32 bits. + */ +#define _us_is_small_enough(us) ((us) < 8192) +#endif + +/* Speed optimisation for small wait_us. Care taken to preserve binary compatibility */ +inline void _wait_us_inline(unsigned int us) +{ + /* Threshold is determined by specification of us_ticker_api.h - smallest possible + * time range for the us_ticker is 16-bit 8MHz, which gives 8192us. This also leaves + * headroom for the multiplication in 32 bits. + */ + if (_us_is_small_enough(us) && _us_ticker_is_initialized) { + const uint32_t ticks = ((us * US_TICKER_PERIOD_DEN) + US_TICKER_PERIOD_NUM - 1) / US_TICKER_PERIOD_NUM; + _wait_us_ticks(ticks); + } else { + _wait_us_generic(us); + } +} + +#define wait_us(us) _wait_us_inline(us) +#endif // Known-rate, initialised timer + #ifdef __cplusplus } #endif diff --git a/platform/mbed_wait_api_no_rtos.c b/platform/mbed_wait_api_no_rtos.c index d03305f2166..a972b063d0d 100644 --- a/platform/mbed_wait_api_no_rtos.c +++ b/platform/mbed_wait_api_no_rtos.c @@ -19,13 +19,14 @@ #include "platform/mbed_toolchain.h" #include "platform/mbed_wait_api.h" +#include "hal/lp_ticker_api.h" +#include "hal/us_ticker_api.h" +#include "hal/ticker_api.h" + // This implementation of the wait functions will be compiled only // if the RTOS is not present. #ifndef MBED_CONF_RTOS_PRESENT -#include "hal/lp_ticker_api.h" -#include "hal/us_ticker_api.h" - void wait(float s) { wait_ms(s * 1000.0f); @@ -42,24 +43,55 @@ void wait_ms(int ms) #endif } +#endif // #ifndef MBED_CONF_RTOS_PRESENT + +// This wait_us is used by both RTOS and non-RTOS builds +/* The actual time delay may be 1 less usec */ + +#if DEVICE_USTICKER + +#if defined US_TICKER_PERIOD_NUM +/* Real definition for binary compatibility with binaries not using the new macro */ +void (wait_us)(int us) +{ + wait_us(us); +} + +/* External definition for the inline function */ +extern void _wait_us_inline(unsigned int us); + +void _wait_us_ticks(uint32_t ticks) +{ + const uint32_t start = us_ticker_read(); + while (((us_ticker_read() - start) & US_TICKER_MASK) < ticks); +} + +void _wait_us_generic(unsigned int us) +#else void wait_us(int us) +#endif { -#if DEVICE_USTICKER + // Generic version using full ticker, allowing for initialization, scaling and widening of timer const ticker_data_t *const ticker = get_us_ticker_data(); - uint32_t start = ticker_read(ticker); + const uint32_t start = ticker_read(ticker); while ((ticker_read(ticker) - start) < (uint32_t)us); -#else // fallback to wait_ns for targets without usticker - while (us > 1000) { - us -= 1000; - wait_ns(1000000); +} + +#else // DEVICE_USTICKER + +// fallback to wait_ns for targets without usticker +void wait_us(int us) +{ + while (us > 1024) { + us -= 1024; + wait_ns(1024000); } if (us > 0) { wait_ns(us * 1000); } -#endif // DEVICE_USTICKER } -#endif // #ifndef MBED_CONF_RTOS_PRESENT +#endif // DEVICE_USTICKER // This wait_ns is used by both RTOS and non-RTOS builds @@ -110,7 +142,7 @@ static const uint16_t delay_loop_code[] = { }; /* Take the address of the code, set LSB to indicate Thumb, and cast to void() function pointer */ -#define delay_loop ((void(*)()) ((uintptr_t) delay_loop_code | 1)) +#define delay_loop ((void(*)()) ((uintptr_t) delay_loop_code + 1)) /* Some targets may not provide zero-wait-state flash performance. Export this function * to be overridable for targets to provide more accurate implementation like locating diff --git a/platform/mbed_wait_api_rtos.cpp b/platform/mbed_wait_api_rtos.cpp index f3b987ae699..b4333ba45fd 100644 --- a/platform/mbed_wait_api_rtos.cpp +++ b/platform/mbed_wait_api_rtos.cpp @@ -63,17 +63,5 @@ void wait_ms(int ms) } } -/* The actual time delay may be 1 less usec */ -void wait_us(int us) -{ - if (us > 10000) { - MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_UNKNOWN), - "wait_us blocks deep sleep, wait_ms recommended for long delays\n"); - } - const ticker_data_t *const ticker = get_us_ticker_data(); - uint32_t start = ticker_read(ticker); - while ((ticker_read(ticker) - start) < (uint32_t)us); -} - #endif // #if MBED_CONF_RTOS_PRESENT diff --git a/rtos/TARGET_CORTEX/mbed_boot.c b/rtos/TARGET_CORTEX/mbed_boot.c index 1f2460ba7ea..b4257f88db2 100644 --- a/rtos/TARGET_CORTEX/mbed_boot.c +++ b/rtos/TARGET_CORTEX/mbed_boot.c @@ -58,6 +58,7 @@ #include #include "cmsis.h" +#include "hal/us_ticker_api.h" #include "mbed_toolchain.h" #include "mbed_boot.h" #include "mbed_error.h" @@ -75,6 +76,9 @@ void mbed_init(void) mbed_mpu_manager_init(); mbed_cpy_nvic(); mbed_sdk_init(); +#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT + us_ticker_init(); +#endif mbed_rtos_init(); } diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker.c index 18716dd8870..ef22cb89a2c 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -81,9 +82,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker.c index 9d3b9f1f66a..dd2589f32a4 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -81,9 +82,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker.c index b77cbf6479d..bffd5610a3f 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_tpm.h" @@ -95,9 +96,9 @@ void us_ticker_init(void) } -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } void us_ticker_disable_interrupt(void) diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL27Z/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker.c index b77cbf6479d..bffd5610a3f 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_tpm.h" @@ -95,9 +96,9 @@ void us_ticker_init(void) } -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } void us_ticker_disable_interrupt(void) diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL43Z/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker.c index 1fa17b15514..1e786ba753c 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -81,9 +82,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KL82Z/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker.c index 9d3b9f1f66a..dd2589f32a4 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -81,9 +82,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker.c index 864abec9f94..68d8cf11e30 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_tpm.h" @@ -95,9 +96,9 @@ void us_ticker_init(void) } -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } void us_ticker_disable_interrupt(void) diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker.c index 9d3b9f1f66a..dd2589f32a4 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -81,9 +82,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K22F/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker.c index 18716dd8870..ef22cb89a2c 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -81,9 +82,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker.c index bc753d2ff34..87ee099278d 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker.c @@ -15,6 +15,7 @@ */ #include #include "us_ticker_api.h" +#include "us_ticker_defines.h" #include "PeripheralNames.h" #include "fsl_pit.h" #include "fsl_clock_config.h" @@ -82,9 +83,9 @@ void us_ticker_init(void) * * @return The current timer's counter value in ticks */ -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); + return us_ticker_read(); } /** Disable us ticker interrupt diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker_defines.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker_defines.h new file mode 100644 index 00000000000..04f5da8147c --- /dev/null +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/us_ticker_defines.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _FSL_US_TICKER_DEFINES_H_ +#define _FSL_US_TICKER_DEFINES_H_ + +#include "fsl_pit.h" + +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#define US_TICKER_MASK 0xFFFFFFFF + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1))) + +#endif /* _FSL_US_TICKER_DEFINES_H_ */ diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/objects.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/objects.h index 405f2e17785..0dec0dcbd28 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/objects.h +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/objects.h @@ -103,6 +103,8 @@ struct qspi_s { #include "gpio_object.h" +#include "us_ticker_defines.h" + #ifdef __cplusplus } #endif diff --git a/targets/TARGET_STM/device.h b/targets/TARGET_STM/device.h index 643ed21d432..27bb8b4042e 100644 --- a/targets/TARGET_STM/device.h +++ b/targets/TARGET_STM/device.h @@ -37,4 +37,6 @@ #include "objects.h" +#include "us_ticker_defines.h" + #endif diff --git a/targets/TARGET_STM/us_ticker.c b/targets/TARGET_STM/us_ticker.c index 0c1a4302d1c..ddf2e62ad7d 100644 --- a/targets/TARGET_STM/us_ticker.c +++ b/targets/TARGET_STM/us_ticker.c @@ -20,6 +20,7 @@ #include "us_ticker_api.h" #include "PeripheralNames.h" #include "us_ticker_data.h" +#include "us_ticker_defines.h" TIM_HandleTypeDef TimMasterHandle; @@ -212,9 +213,10 @@ void us_ticker_init(void) HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); } -uint32_t us_ticker_read() +uint32_t (us_ticker_read)() { - return TIM_MST->CNT; + /* Invoke the macro */ + return us_ticker_read(); } void us_ticker_set_interrupt(timestamp_t timestamp) diff --git a/targets/TARGET_STM/us_ticker_defines.h b/targets/TARGET_STM/us_ticker_defines.h new file mode 100644 index 00000000000..5f9a1dfaa89 --- /dev/null +++ b/targets/TARGET_STM/us_ticker_defines.h @@ -0,0 +1,37 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_US_TICKER_DEFINES_H +#define MBED_US_TICKER_DEFINES_H_ + +#include "us_ticker_data.h" + +/* All devices have 1MHz us_ticker */ +#define US_TICKER_PERIOD_NUM 1 +#define US_TICKER_PERIOD_DEN 1 + +#if TIM_MST_BIT_WIDTH == 16 +#define US_TICKER_MASK 0xFFFF +#elif TIM_MST_BIT_WIDTH == 32 +#define US_TICKER_MASK 0xFFFFFFFF +#else +#error "Bad TIM_MST_BIT_WIDTH" +#endif + +/* Macro-optimised form of us_ticker_read */ +#define us_ticker_read() (TIM_MST->CNT) + +#endif /* MBED_US_TICKER_DEFINES_H_ */ diff --git a/targets/targets.json b/targets/targets.json index caac2f6fe91..9b3fb14a17b 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -47,6 +47,10 @@ "tickless-from-us-ticker": { "help": "Run tickless from the microsecond ticker rather than the low power ticker. Running tickless off of the microsecond ticker improves interrupt latency on targets which use lpticker_delay_ticks", "value": false + }, + "init-us-ticker-at-boot": { + "help": "Initialize the microsecond ticker at boot rather than on first use, and leave it initialized. This speeds up wait_us in particular.", + "value": false } } }, @@ -1885,7 +1889,8 @@ }, "overrides": { "deep-sleep-latency": 3, - "tickless-from-us-ticker": true + "tickless-from-us-ticker": true, + "init-us-ticker-at-boot": true }, "device_has": [ "USTICKER",