|
| 1 | +/* |
| 2 | + * Copyright (c) Nordic Semiconductor ASA |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * 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 notice, this |
| 12 | + * list of conditions and the following disclaimer in the documentation and/or |
| 13 | + * other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
| 16 | + * contributors to this software may be used to endorse or promote products |
| 17 | + * derived from this software without specific prior written permission. |
| 18 | + * |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 21 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 24 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 27 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * |
| 31 | + */ |
| 32 | + |
| 33 | +#ifndef _NRF_DELAY_H |
| 34 | +#define _NRF_DELAY_H |
| 35 | + |
| 36 | +#include "nrf.h" |
| 37 | + |
| 38 | +/** |
| 39 | + * @brief Function for delaying execution for number of microseconds. |
| 40 | + * |
| 41 | + * @note NRF52 has instruction cache and because of that delay is not precise. |
| 42 | + * |
| 43 | + * @param number_of_ms |
| 44 | + */ |
| 45 | +/*lint --e{438, 522} "Variable not used" "Function lacks side-effects" */ |
| 46 | +#if defined ( __CC_ARM ) |
| 47 | + |
| 48 | +static __ASM void __INLINE nrf_delay_us(uint32_t volatile number_of_us) |
| 49 | +{ |
| 50 | +loop |
| 51 | + SUBS R0, R0, #1 |
| 52 | + NOP |
| 53 | + NOP |
| 54 | + NOP |
| 55 | + NOP |
| 56 | + NOP |
| 57 | + NOP |
| 58 | + NOP |
| 59 | + NOP |
| 60 | + NOP |
| 61 | + NOP |
| 62 | + NOP |
| 63 | + NOP |
| 64 | +#ifdef NRF52 |
| 65 | + NOP |
| 66 | + NOP |
| 67 | + NOP |
| 68 | + NOP |
| 69 | + NOP |
| 70 | + NOP |
| 71 | + NOP |
| 72 | + NOP |
| 73 | + NOP |
| 74 | + NOP |
| 75 | + NOP |
| 76 | + NOP |
| 77 | + NOP |
| 78 | + NOP |
| 79 | + NOP |
| 80 | + NOP |
| 81 | + NOP |
| 82 | + NOP |
| 83 | + NOP |
| 84 | + NOP |
| 85 | + NOP |
| 86 | + NOP |
| 87 | + NOP |
| 88 | + NOP |
| 89 | + NOP |
| 90 | + NOP |
| 91 | + NOP |
| 92 | + NOP |
| 93 | + NOP |
| 94 | + NOP |
| 95 | + NOP |
| 96 | + NOP |
| 97 | + NOP |
| 98 | + NOP |
| 99 | + NOP |
| 100 | + NOP |
| 101 | + NOP |
| 102 | + NOP |
| 103 | + NOP |
| 104 | + NOP |
| 105 | + NOP |
| 106 | + NOP |
| 107 | + NOP |
| 108 | + NOP |
| 109 | +#endif |
| 110 | + BNE loop |
| 111 | + BX LR |
| 112 | +} |
| 113 | + |
| 114 | +#elif defined ( __ICCARM__ ) |
| 115 | + |
| 116 | +static void __INLINE nrf_delay_us(uint32_t volatile number_of_us) |
| 117 | +{ |
| 118 | +__ASM ( |
| 119 | +"loop:\n\t" |
| 120 | + " SUBS R0, R0, #1\n\t" |
| 121 | + " NOP\n\t" |
| 122 | + " NOP\n\t" |
| 123 | + " NOP\n\t" |
| 124 | + " NOP\n\t" |
| 125 | + " NOP\n\t" |
| 126 | + " NOP\n\t" |
| 127 | + " NOP\n\t" |
| 128 | + " NOP\n\t" |
| 129 | + " NOP\n\t" |
| 130 | + " NOP\n\t" |
| 131 | + " NOP\n\t" |
| 132 | + " NOP\n\t" |
| 133 | +#ifdef NRF52 |
| 134 | + " NOP\n\t" |
| 135 | + " NOP\n\t" |
| 136 | + " NOP\n\t" |
| 137 | + " NOP\n\t" |
| 138 | + " NOP\n\t" |
| 139 | + " NOP\n\t" |
| 140 | + " NOP\n\t" |
| 141 | + " NOP\n\t" |
| 142 | + " NOP\n\t" |
| 143 | + " NOP\n\t" |
| 144 | + " NOP\n\t" |
| 145 | + " NOP\n\t" |
| 146 | + " NOP\n\t" |
| 147 | + " NOP\n\t" |
| 148 | + " NOP\n\t" |
| 149 | + " NOP\n\t" |
| 150 | + " NOP\n\t" |
| 151 | + " NOP\n\t" |
| 152 | + " NOP\n\t" |
| 153 | + " NOP\n\t" |
| 154 | + " NOP\n\t" |
| 155 | + " NOP\n\t" |
| 156 | + " NOP\n\t" |
| 157 | + " NOP\n\t" |
| 158 | + " NOP\n\t" |
| 159 | + " NOP\n\t" |
| 160 | + " NOP\n\t" |
| 161 | + " NOP\n\t" |
| 162 | + " NOP\n\t" |
| 163 | + " NOP\n\t" |
| 164 | + " NOP\n\t" |
| 165 | + " NOP\n\t" |
| 166 | + " NOP\n\t" |
| 167 | + " NOP\n\t" |
| 168 | + " NOP\n\t" |
| 169 | + " NOP\n\t" |
| 170 | + " NOP\n\t" |
| 171 | + " NOP\n\t" |
| 172 | + " NOP\n\t" |
| 173 | + " NOP\n\t" |
| 174 | + " NOP\n\t" |
| 175 | + " NOP\n\t" |
| 176 | + " NOP\n\t" |
| 177 | + " NOP\n\t" |
| 178 | + " NOP\n\t" |
| 179 | +#endif |
| 180 | + " BNE.n loop\n\t"); |
| 181 | +} |
| 182 | + |
| 183 | +#elif defined ( _WIN32 ) || defined ( __unix ) || defined( __APPLE__ ) |
| 184 | + |
| 185 | +__STATIC_INLINE void nrf_delay_us(uint32_t volatile number_of_us); |
| 186 | + |
| 187 | +#ifndef CUSTOM_NRF_DELAY_US |
| 188 | +__STATIC_INLINE void nrf_delay_us(uint32_t volatile number_of_us) |
| 189 | +{} |
| 190 | +#endif |
| 191 | + |
| 192 | +#elif defined ( __GNUC__ ) |
| 193 | + |
| 194 | +static void __INLINE nrf_delay_us(uint32_t volatile number_of_us) __attribute__((always_inline)); |
| 195 | +static void __INLINE nrf_delay_us(uint32_t volatile number_of_us) |
| 196 | +{ |
| 197 | +register uint32_t delay __ASM ("r0") = number_of_us; |
| 198 | +__ASM volatile ( |
| 199 | +#ifdef NRF51 |
| 200 | + ".syntax unified\n" |
| 201 | +#endif |
| 202 | + "1:\n" |
| 203 | + " SUBS %0, %0, #1\n" |
| 204 | + " NOP\n" |
| 205 | + " NOP\n" |
| 206 | + " NOP\n" |
| 207 | + " NOP\n" |
| 208 | + " NOP\n" |
| 209 | + " NOP\n" |
| 210 | + " NOP\n" |
| 211 | + " NOP\n" |
| 212 | + " NOP\n" |
| 213 | + " NOP\n" |
| 214 | + " NOP\n" |
| 215 | + " NOP\n" |
| 216 | +#ifdef NRF52 |
| 217 | + " NOP\n" |
| 218 | + " NOP\n" |
| 219 | + " NOP\n" |
| 220 | + " NOP\n" |
| 221 | + " NOP\n" |
| 222 | + " NOP\n" |
| 223 | + " NOP\n" |
| 224 | + " NOP\n" |
| 225 | + " NOP\n" |
| 226 | + " NOP\n" |
| 227 | + " NOP\n" |
| 228 | + " NOP\n" |
| 229 | + " NOP\n" |
| 230 | + " NOP\n" |
| 231 | + " NOP\n" |
| 232 | + " NOP\n" |
| 233 | + " NOP\n" |
| 234 | + " NOP\n" |
| 235 | + " NOP\n" |
| 236 | + " NOP\n" |
| 237 | + " NOP\n" |
| 238 | + " NOP\n" |
| 239 | + " NOP\n" |
| 240 | + " NOP\n" |
| 241 | + " NOP\n" |
| 242 | + " NOP\n" |
| 243 | + " NOP\n" |
| 244 | + " NOP\n" |
| 245 | + " NOP\n" |
| 246 | + " NOP\n" |
| 247 | + " NOP\n" |
| 248 | + " NOP\n" |
| 249 | + " NOP\n" |
| 250 | + " NOP\n" |
| 251 | + " NOP\n" |
| 252 | + " NOP\n" |
| 253 | + " NOP\n" |
| 254 | + " NOP\n" |
| 255 | + " NOP\n" |
| 256 | + " NOP\n" |
| 257 | + " NOP\n" |
| 258 | + " NOP\n" |
| 259 | + " NOP\n" |
| 260 | + " NOP\n" |
| 261 | + " NOP\n" |
| 262 | + " NOP\n" |
| 263 | +#endif |
| 264 | + " BNE 1b\n" |
| 265 | +#ifdef NRF51 |
| 266 | + ".syntax divided\n" |
| 267 | +#endif |
| 268 | + : "+r" (delay)); |
| 269 | +} |
| 270 | +#endif |
| 271 | + |
| 272 | +void nrf_delay_ms(uint32_t volatile number_of_ms); |
| 273 | + |
| 274 | +#endif |
0 commit comments