|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Nordic Semiconductor ASA |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from this |
| 17 | + * software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +#ifndef NRFX_COREDEP_H__ |
| 33 | +#define NRFX_COREDEP_H__ |
| 34 | + |
| 35 | +/** |
| 36 | + * @defgroup nrfx_coredep Core-dependent functionality |
| 37 | + * @{ |
| 38 | + * @ingroup nrfx |
| 39 | + * @brief Module containing functions with core-dependent implementation, like delay. |
| 40 | + */ |
| 41 | + |
| 42 | +#if defined(__NRFX_DOXYGEN__) |
| 43 | + |
| 44 | +/** @brief Core frequency (in MHz). */ |
| 45 | +#define NRFX_DELAY_CPU_FREQ_MHZ |
| 46 | +/** @brief Availability of DWT unit in the given SoC. */ |
| 47 | +#define NRFX_DELAY_DWT_PRESENT |
| 48 | + |
| 49 | +#elif defined(NRF51) |
| 50 | + #define NRFX_DELAY_CPU_FREQ_MHZ 16 |
| 51 | + #define NRFX_DELAY_DWT_PRESENT 0 |
| 52 | +#elif defined(NRF52810_XXAA) |
| 53 | + #define NRFX_DELAY_CPU_FREQ_MHZ 64 |
| 54 | + #define NRFX_DELAY_DWT_PRESENT 0 |
| 55 | +#elif defined(NRF52832_XXAA) || defined (NRF52832_XXAB) |
| 56 | + #define NRFX_DELAY_CPU_FREQ_MHZ 64 |
| 57 | + #define NRFX_DELAY_DWT_PRESENT 1 |
| 58 | +#elif defined(NRF52840_XXAA) |
| 59 | + #define NRFX_DELAY_CPU_FREQ_MHZ 64 |
| 60 | + #define NRFX_DELAY_DWT_PRESENT 1 |
| 61 | +#else |
| 62 | + #error "Unknown device." |
| 63 | +#endif |
| 64 | + |
| 65 | +/** |
| 66 | + * @brief Function for delaying execution for a number of microseconds. |
| 67 | + * |
| 68 | + * The value of @p time_us is multiplied by the frequency in MHz. Therefore, the delay is limited to |
| 69 | + * maximum uint32_t capacity divided by frequency. For example: |
| 70 | + * - For SoCs working at 64MHz: 0xFFFFFFFF/64 = 0x03FFFFFF (67108863 microseconds) |
| 71 | + * - For SoCs working at 16MHz: 0xFFFFFFFF/16 = 0x0FFFFFFF (268435455 microseconds) |
| 72 | + * |
| 73 | + * @param time_us Number of microseconds to wait. |
| 74 | + */ |
| 75 | +__STATIC_INLINE void nrfx_coredep_delay_us(uint32_t time_us); |
| 76 | + |
| 77 | +/** @} */ |
| 78 | + |
| 79 | +#ifndef SUPPRESS_INLINE_IMPLEMENTATION |
| 80 | + |
| 81 | +#if NRFX_CHECK(NRFX_DELAY_DWT_BASED) |
| 82 | + |
| 83 | +#if !NRFX_DELAY_DWT_PRESENT |
| 84 | +#error "DWT unit not present in the SoC that is used." |
| 85 | +#endif |
| 86 | + |
| 87 | +__STATIC_INLINE void nrfx_coredep_delay_us(uint32_t time_us) |
| 88 | +{ |
| 89 | + if (time_us == 0) |
| 90 | + { |
| 91 | + return; |
| 92 | + } |
| 93 | + uint32_t time_cycles = time_us * NRFX_DELAY_CPU_FREQ_MHZ; |
| 94 | + |
| 95 | + // Save the current state of the DEMCR register to be able to restore it before exiting |
| 96 | + // this function. Enable the trace and debug blocks (DWT is one of them). |
| 97 | + uint32_t core_debug = CoreDebug->DEMCR; |
| 98 | + CoreDebug->DEMCR = core_debug | CoreDebug_DEMCR_TRCENA_Msk; |
| 99 | + |
| 100 | + // Save the current state of the CTRL register in DWT block. Make sure |
| 101 | + // that cycle counter is enabled. |
| 102 | + uint32_t dwt_ctrl = DWT->CTRL; |
| 103 | + DWT->CTRL = dwt_ctrl | DWT_CTRL_CYCCNTENA_Msk; |
| 104 | + |
| 105 | + // Store start value of cycle counter. |
| 106 | + uint32_t cyccnt_initial = DWT->CYCCNT; |
| 107 | + |
| 108 | + // Delay required time. |
| 109 | + while ((DWT->CYCCNT - cyccnt_initial) < time_cycles) |
| 110 | + {} |
| 111 | + |
| 112 | + // Restore preserved registers. |
| 113 | + DWT->CTRL = dwt_ctrl; |
| 114 | + CoreDebug->DEMCR = core_debug; |
| 115 | +} |
| 116 | +#else // NRFX_CHECK(NRFX_DELAY_DWT_BASED) |
| 117 | + |
| 118 | + |
| 119 | +__STATIC_INLINE void nrfx_coredep_delay_us(uint32_t time_us) |
| 120 | +{ |
| 121 | + if (time_us == 0) |
| 122 | + { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + #if defined(NRF51) |
| 127 | + // The loop takes 4 cycles: 1 for SUBS and 3 for BHI. |
| 128 | + static const uint16_t delay_bytecode[] = { |
| 129 | + 0x3804, // SUBS r0, #4 |
| 130 | + 0xd8fd, // BHI .-2 |
| 131 | + 0x4770 // BX LR |
| 132 | + }; |
| 133 | + #elif defined(NRF52810_XXAA) |
| 134 | + // The loop takes 7 cycles: 1 for SUBS and 2 for BHI and 2 for flash wait states. |
| 135 | + static const uint16_t delay_bytecode[] = { |
| 136 | + 0x3807, // SUBS r0, #7 |
| 137 | + 0xd8fd, // BHI .-2 |
| 138 | + 0x4770 // BX LR |
| 139 | + }; |
| 140 | + #elif defined(NRF52832_XXAA) || defined (NRF52832_XXAB) || defined(NRF52840_XXAA) |
| 141 | + // The loop takes 3 cycles: 1 for SUBS and 2 for BHI. |
| 142 | + // Make sure that code will be cached properly, so that no extra wait states appear. |
| 143 | + __ALIGN(16) |
| 144 | + static const uint16_t delay_bytecode[] = { |
| 145 | + 0x3803, // SUBS r0, #3 |
| 146 | + 0xd8fd, // BHI .-2 |
| 147 | + 0x4770 // BX LR |
| 148 | + }; |
| 149 | + #endif |
| 150 | + |
| 151 | + typedef void (* delay_func_t)(uint32_t); |
| 152 | + // Set LSB to 1 to execute code in Thumb mode. |
| 153 | + const delay_func_t delay_cycles = (delay_func_t)((((uint32_t)delay_bytecode) | 1)); |
| 154 | + uint32_t cycles = time_us * NRFX_DELAY_CPU_FREQ_MHZ; |
| 155 | + delay_cycles(cycles); |
| 156 | +} |
| 157 | + |
| 158 | +#endif // !NRFX_CHECK(NRFX_DELAY_DWT_BASED_DELAY) |
| 159 | + |
| 160 | +#endif // SUPPRESS_INLINE_IMPLEMENTATION |
| 161 | + |
| 162 | +#endif // NRFX_COREDEP_H__ |
0 commit comments