nrfx_atomic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrfx_atomic.h"
  41. #ifndef NRFX_ATOMIC_USE_BUILT_IN
  42. #define NRFX_ATOMIC_USE_BUILT_IN 0
  43. #endif // NRFX_ATOMIC_USE_BUILT_IN
  44. #if ((__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U))
  45. #define NRFX_ATOMIC_STREX_LDREX_PRESENT
  46. #endif
  47. #if (NRFX_ATOMIC_USE_BUILT_IN == 0) && defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  48. #include "nrfx_atomic_internal.h"
  49. #endif
  50. uint32_t nrfx_atomic_u32_fetch_store(nrfx_atomic_u32_t * p_data, uint32_t value)
  51. {
  52. #if NRFX_ATOMIC_USE_BUILT_IN
  53. return __atomic_exchange_n(p_data, value, __ATOMIC_SEQ_CST);
  54. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  55. uint32_t old_val;
  56. uint32_t new_val;
  57. NRFX_ATOMIC_OP(mov, old_val, new_val, p_data, value);
  58. (void) new_val;
  59. return old_val;
  60. #else
  61. NRFX_CRITICAL_SECTION_ENTER();
  62. uint32_t old_val = *p_data;
  63. *p_data = value;
  64. NRFX_CRITICAL_SECTION_EXIT();
  65. return old_val;
  66. #endif // NRFX_ATOMIC_USE_BUILT_IN
  67. }
  68. uint32_t nrfx_atomic_u32_store(nrfx_atomic_u32_t * p_data, uint32_t value)
  69. {
  70. #if NRFX_ATOMIC_USE_BUILT_IN
  71. __atomic_store_n(p_data, value, __ATOMIC_SEQ_CST);
  72. return value;
  73. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  74. uint32_t old_val;
  75. uint32_t new_val;
  76. NRFX_ATOMIC_OP(mov, old_val, new_val, p_data, value);
  77. (void) old_val;
  78. return new_val;
  79. #else
  80. NRFX_CRITICAL_SECTION_ENTER();
  81. *p_data = value;
  82. NRFX_CRITICAL_SECTION_EXIT();
  83. return value;
  84. #endif //NRFX_ATOMIC_USE_BUILT_IN
  85. }
  86. uint32_t nrfx_atomic_u32_fetch_or(nrfx_atomic_u32_t * p_data, uint32_t value)
  87. {
  88. #if NRFX_ATOMIC_USE_BUILT_IN
  89. return __atomic_fetch_or(p_data, value, __ATOMIC_SEQ_CST);
  90. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  91. uint32_t old_val;
  92. uint32_t new_val;
  93. NRFX_ATOMIC_OP(orr, old_val, new_val, p_data, value);
  94. (void) new_val;
  95. return old_val;
  96. #else
  97. NRFX_CRITICAL_SECTION_ENTER();
  98. uint32_t old_val = *p_data;
  99. *p_data |= value;
  100. NRFX_CRITICAL_SECTION_EXIT();
  101. return old_val;
  102. #endif //NRFX_ATOMIC_USE_BUILT_IN
  103. }
  104. uint32_t nrfx_atomic_u32_or(nrfx_atomic_u32_t * p_data, uint32_t value)
  105. {
  106. #if NRFX_ATOMIC_USE_BUILT_IN
  107. return __atomic_or_fetch(p_data, value, __ATOMIC_SEQ_CST);
  108. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  109. uint32_t old_val;
  110. uint32_t new_val;
  111. NRFX_ATOMIC_OP(orr, old_val, new_val, p_data, value);
  112. (void) old_val;
  113. return new_val;
  114. #else
  115. NRFX_CRITICAL_SECTION_ENTER();
  116. *p_data |= value;
  117. uint32_t new_value = *p_data;
  118. NRFX_CRITICAL_SECTION_EXIT();
  119. return new_value;
  120. #endif //NRFX_ATOMIC_USE_BUILT_IN
  121. }
  122. uint32_t nrfx_atomic_u32_fetch_and(nrfx_atomic_u32_t * p_data, uint32_t value)
  123. {
  124. #if NRFX_ATOMIC_USE_BUILT_IN
  125. return __atomic_fetch_and(p_data, value, __ATOMIC_SEQ_CST);
  126. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  127. uint32_t old_val;
  128. uint32_t new_val;
  129. NRFX_ATOMIC_OP(and, old_val, new_val, p_data, value);
  130. (void) new_val;
  131. return old_val;
  132. #else
  133. NRFX_CRITICAL_SECTION_ENTER();
  134. uint32_t old_val = *p_data;
  135. *p_data &= value;
  136. NRFX_CRITICAL_SECTION_EXIT();
  137. return old_val;
  138. #endif //NRFX_ATOMIC_USE_BUILT_IN
  139. }
  140. uint32_t nrfx_atomic_u32_and(nrfx_atomic_u32_t * p_data, uint32_t value)
  141. {
  142. #if NRFX_ATOMIC_USE_BUILT_IN
  143. return __atomic_and_fetch(p_data, value, __ATOMIC_SEQ_CST);
  144. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  145. uint32_t old_val;
  146. uint32_t new_val;
  147. NRFX_ATOMIC_OP(and, old_val, new_val, p_data, value);
  148. (void) old_val;
  149. return new_val;
  150. #else
  151. NRFX_CRITICAL_SECTION_ENTER();
  152. *p_data &= value;
  153. uint32_t new_value = *p_data;
  154. NRFX_CRITICAL_SECTION_EXIT();
  155. return new_value;
  156. #endif //NRFX_ATOMIC_USE_BUILT_IN
  157. }
  158. uint32_t nrfx_atomic_u32_fetch_xor(nrfx_atomic_u32_t * p_data, uint32_t value)
  159. {
  160. #if NRFX_ATOMIC_USE_BUILT_IN
  161. return __atomic_fetch_xor(p_data, value, __ATOMIC_SEQ_CST);
  162. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  163. uint32_t old_val;
  164. uint32_t new_val;
  165. NRFX_ATOMIC_OP(eor, old_val, new_val, p_data, value);
  166. (void) new_val;
  167. return old_val;
  168. #else
  169. NRFX_CRITICAL_SECTION_ENTER();
  170. uint32_t old_val = *p_data;
  171. *p_data ^= value;
  172. NRFX_CRITICAL_SECTION_EXIT();
  173. return old_val;
  174. #endif //NRFX_ATOMIC_USE_BUILT_IN
  175. }
  176. uint32_t nrfx_atomic_u32_xor(nrfx_atomic_u32_t * p_data, uint32_t value)
  177. {
  178. #if NRFX_ATOMIC_USE_BUILT_IN
  179. return __atomic_xor_fetch(p_data, value, __ATOMIC_SEQ_CST);
  180. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  181. uint32_t old_val;
  182. uint32_t new_val;
  183. NRFX_ATOMIC_OP(eor, old_val, new_val, p_data, value);
  184. (void) old_val;
  185. return new_val;
  186. #else
  187. NRFX_CRITICAL_SECTION_ENTER();
  188. *p_data ^= value;
  189. uint32_t new_value = *p_data;
  190. NRFX_CRITICAL_SECTION_EXIT();
  191. return new_value;
  192. #endif //NRFX_ATOMIC_USE_BUILT_IN
  193. }
  194. uint32_t nrfx_atomic_u32_fetch_add(nrfx_atomic_u32_t * p_data, uint32_t value)
  195. {
  196. #if NRFX_ATOMIC_USE_BUILT_IN
  197. return __atomic_fetch_add(p_data, value, __ATOMIC_SEQ_CST);
  198. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  199. uint32_t old_val;
  200. uint32_t new_val;
  201. NRFX_ATOMIC_OP(add, old_val, new_val, p_data, value);
  202. (void) new_val;
  203. return old_val;
  204. #else
  205. NRFX_CRITICAL_SECTION_ENTER();
  206. uint32_t old_val = *p_data;
  207. *p_data += value;
  208. NRFX_CRITICAL_SECTION_EXIT();
  209. return old_val;
  210. #endif //NRFX_ATOMIC_USE_BUILT_IN
  211. }
  212. uint32_t nrfx_atomic_u32_add(nrfx_atomic_u32_t * p_data, uint32_t value)
  213. {
  214. #if NRFX_ATOMIC_USE_BUILT_IN
  215. return __atomic_add_fetch(p_data, value, __ATOMIC_SEQ_CST);
  216. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  217. uint32_t old_val;
  218. uint32_t new_val;
  219. NRFX_ATOMIC_OP(add, old_val, new_val, p_data, value);
  220. (void) old_val;
  221. return new_val;
  222. #else
  223. NRFX_CRITICAL_SECTION_ENTER();
  224. *p_data += value;
  225. uint32_t new_value = *p_data;
  226. NRFX_CRITICAL_SECTION_EXIT();
  227. return new_value;
  228. #endif //NRFX_ATOMIC_USE_BUILT_IN
  229. }
  230. uint32_t nrfx_atomic_u32_fetch_sub(nrfx_atomic_u32_t * p_data, uint32_t value)
  231. {
  232. #if NRFX_ATOMIC_USE_BUILT_IN
  233. return __atomic_fetch_sub(p_data, value, __ATOMIC_SEQ_CST);
  234. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  235. uint32_t old_val;
  236. uint32_t new_val;
  237. NRFX_ATOMIC_OP(sub, old_val, new_val, p_data, value);
  238. (void) new_val;
  239. return old_val;
  240. #else
  241. NRFX_CRITICAL_SECTION_ENTER();
  242. uint32_t old_val = *p_data;
  243. *p_data -= value;
  244. NRFX_CRITICAL_SECTION_EXIT();
  245. return old_val;
  246. #endif //NRFX_ATOMIC_USE_BUILT_IN
  247. }
  248. uint32_t nrfx_atomic_u32_sub(nrfx_atomic_u32_t * p_data, uint32_t value)
  249. {
  250. #if NRFX_ATOMIC_USE_BUILT_IN
  251. return __atomic_sub_fetch(p_data, value, __ATOMIC_SEQ_CST);
  252. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  253. uint32_t old_val;
  254. uint32_t new_val;
  255. NRFX_ATOMIC_OP(sub, old_val, new_val, p_data, value);
  256. (void) old_val;
  257. return new_val;
  258. #else
  259. NRFX_CRITICAL_SECTION_ENTER();
  260. *p_data -= value;
  261. uint32_t new_value = *p_data;
  262. NRFX_CRITICAL_SECTION_EXIT();
  263. return new_value;
  264. #endif //NRFX_ATOMIC_USE_BUILT_IN
  265. }
  266. bool nrfx_atomic_u32_cmp_exch(nrfx_atomic_u32_t * p_data,
  267. uint32_t * p_expected,
  268. uint32_t desired)
  269. {
  270. #if NRFX_ATOMIC_USE_BUILT_IN
  271. return __atomic_compare_exchange(p_data,
  272. p_expected,
  273. &desired,
  274. 1,
  275. __ATOMIC_SEQ_CST,
  276. __ATOMIC_SEQ_CST);
  277. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  278. return nrfx_atomic_internal_cmp_exch(p_data, p_expected, desired);
  279. #else
  280. bool result;
  281. NRFX_CRITICAL_SECTION_ENTER();
  282. if (*p_data == *p_expected)
  283. {
  284. *p_data = desired;
  285. result = true;
  286. }
  287. else
  288. {
  289. *p_expected = *p_data;
  290. result = false;
  291. }
  292. NRFX_CRITICAL_SECTION_EXIT();
  293. return result;
  294. #endif
  295. }
  296. uint32_t nrfx_atomic_u32_fetch_sub_hs(nrfx_atomic_u32_t * p_data, uint32_t value)
  297. {
  298. #if NRFX_ATOMIC_USE_BUILT_IN
  299. uint32_t expected = *p_data;
  300. uint32_t new_val;
  301. do {
  302. if (expected >= value)
  303. {
  304. new_val = expected - value;
  305. }
  306. else
  307. {
  308. new_val = expected;
  309. }
  310. } while (!__atomic_compare_exchange(p_data, &expected, &new_val,
  311. 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
  312. return expected;
  313. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  314. uint32_t old_val;
  315. uint32_t new_val;
  316. NRFX_ATOMIC_OP(sub_hs, old_val, new_val, p_data, value);
  317. (void) new_val;
  318. return old_val;
  319. #else
  320. NRFX_CRITICAL_SECTION_ENTER();
  321. uint32_t old_val = *p_data;
  322. *p_data -= value;
  323. NRFX_CRITICAL_SECTION_EXIT();
  324. return old_val;
  325. #endif //NRFX_ATOMIC_USE_BUILT_IN
  326. }
  327. uint32_t nrfx_atomic_u32_sub_hs(nrfx_atomic_u32_t * p_data, uint32_t value)
  328. {
  329. #if NRFX_ATOMIC_USE_BUILT_IN
  330. uint32_t expected = *p_data;
  331. uint32_t new_val;
  332. do {
  333. if (expected >= value)
  334. {
  335. new_val = expected - value;
  336. }
  337. else
  338. {
  339. new_val = expected;
  340. }
  341. } while (!__atomic_compare_exchange(p_data, &expected, &new_val,
  342. 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST));
  343. return new_val;
  344. #elif defined(NRFX_ATOMIC_STREX_LDREX_PRESENT)
  345. uint32_t old_val;
  346. uint32_t new_val;
  347. NRFX_ATOMIC_OP(sub_hs, old_val, new_val, p_data, value);
  348. (void) old_val;
  349. return new_val;
  350. #else
  351. NRFX_CRITICAL_SECTION_ENTER();
  352. *p_data -= value;
  353. uint32_t new_value = *p_data;
  354. NRFX_CRITICAL_SECTION_EXIT();
  355. return new_value;
  356. #endif //NRFX_ATOMIC_USE_BUILT_IN
  357. }
  358. uint32_t nrfx_atomic_flag_set_fetch(nrfx_atomic_flag_t * p_data)
  359. {
  360. return nrfx_atomic_u32_fetch_or(p_data, 1);
  361. }
  362. uint32_t nrfx_atomic_flag_set(nrfx_atomic_flag_t * p_data)
  363. {
  364. return nrfx_atomic_u32_or(p_data, 1);
  365. }
  366. uint32_t nrfx_atomic_flag_clear_fetch(nrfx_atomic_flag_t * p_data)
  367. {
  368. return nrfx_atomic_u32_fetch_and(p_data, 0);
  369. }
  370. uint32_t nrfx_atomic_flag_clear(nrfx_atomic_flag_t * p_data)
  371. {
  372. return nrfx_atomic_u32_and(p_data, 0);
  373. }