nrfx_atomic.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * Copyright (c) 2016 - 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. #ifndef NRFX_ATOMIC_H__
  41. #define NRFX_ATOMIC_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrfx_atomic Atomic operations API
  48. * @ingroup nrfx
  49. * @{
  50. *
  51. * @brief This module implements C11 stdatomic.h simplified API.
  52. *
  53. * At this point, only Cortex-M3 and M4 cores are supported (LDREX/STREX instructions).
  54. * Atomic types are limited to @ref nrfx_atomic_u32_t and @ref nrfx_atomic_flag_t.
  55. */
  56. /** @brief Atomic 32-bit unsigned type. */
  57. typedef volatile uint32_t nrfx_atomic_u32_t;
  58. /** @brief Atomic 1-bit flag type (technically 32-bit). */
  59. typedef volatile uint32_t nrfx_atomic_flag_t;
  60. /**
  61. * @brief Function for storing a value to an atomic object and returning its previous value.
  62. *
  63. * @param[in] p_data Atomic memory pointer.
  64. * @param[in] value Value to store.
  65. *
  66. * @return Previous value stored in the atomic object.
  67. */
  68. uint32_t nrfx_atomic_u32_fetch_store(nrfx_atomic_u32_t * p_data, uint32_t value);
  69. /**
  70. * @brief Function for storing a value to an atomic object and returning its new value.
  71. *
  72. * @param[in] p_data Atomic memory pointer.
  73. * @param[in] value Value to store.
  74. *
  75. * @return New value stored in the atomic object.
  76. */
  77. uint32_t nrfx_atomic_u32_store(nrfx_atomic_u32_t * p_data, uint32_t value);
  78. /**
  79. * @brief Function for running a logical OR operation on an atomic object
  80. * and returning its previous value.
  81. *
  82. * @param[in] p_data Atomic memory pointer.
  83. * @param[in] value Value of the second operand in the OR operation.
  84. *
  85. * @return Previous value stored in the atomic object.
  86. */
  87. uint32_t nrfx_atomic_u32_fetch_or(nrfx_atomic_u32_t * p_data, uint32_t value);
  88. /**
  89. * @brief Function for running a logical OR operation on an atomic object
  90. * and returning its new value.
  91. *
  92. * @param[in] p_data Atomic memory pointer.
  93. * @param[in] value Value of the second operand in the OR operation.
  94. *
  95. * @return New value stored in the atomic object.
  96. */
  97. uint32_t nrfx_atomic_u32_or(nrfx_atomic_u32_t * p_data, uint32_t value);
  98. /**
  99. * @brief Function for running a logical AND operation on an atomic object
  100. * and returning its previous value.
  101. *
  102. * @param[in] p_data Atomic memory pointer.
  103. * @param[in] value Value of the second operand in the AND operation.
  104. *
  105. * @return Previous value stored in the atomic object.
  106. */
  107. uint32_t nrfx_atomic_u32_fetch_and(nrfx_atomic_u32_t * p_data, uint32_t value);
  108. /**
  109. * @brief Function for running a logical AND operation on an atomic object
  110. * and returning its new value.
  111. *
  112. * @param[in] p_data Atomic memory pointer.
  113. * @param[in] value Value of the second operand in the AND operation.
  114. *
  115. * @return New value stored in the atomic object.
  116. */
  117. uint32_t nrfx_atomic_u32_and(nrfx_atomic_u32_t * p_data, uint32_t value);
  118. /**
  119. * @brief Function for running a logical XOR operation on an atomic object
  120. * and returning its previous value.
  121. *
  122. * @param[in] p_data Atomic memory pointer.
  123. * @param[in] value Value of the second operand in the XOR operation.
  124. *
  125. * @return Previous value stored in the atomic object.
  126. */
  127. uint32_t nrfx_atomic_u32_fetch_xor(nrfx_atomic_u32_t * p_data, uint32_t value);
  128. /**
  129. * @brief Function for running a logical XOR operation on an atomic object
  130. * and returning its new value.
  131. *
  132. * @param[in] p_data Atomic memory pointer.
  133. * @param[in] value Value of the second operand in the XOR operation.
  134. *
  135. * @return New value stored in the atomic object.
  136. */
  137. uint32_t nrfx_atomic_u32_xor(nrfx_atomic_u32_t * p_data, uint32_t value);
  138. /**
  139. * @brief Function for running an arithmetic ADD operation on an atomic object
  140. * and returning its previous value.
  141. *
  142. * @param[in] p_data Atomic memory pointer.
  143. * @param[in] value Value of the second operand in the ADD operation.
  144. *
  145. * @return Previous value stored in the atomic object.
  146. */
  147. uint32_t nrfx_atomic_u32_fetch_add(nrfx_atomic_u32_t * p_data, uint32_t value);
  148. /**
  149. * @brief Function for running an arithmetic ADD operation on an atomic object
  150. * and returning its new value.
  151. *
  152. * @param[in] p_data Atomic memory pointer.
  153. * @param[in] value Value of the second operand in the ADD operation.
  154. *
  155. * @return New value stored in the atomic object.
  156. */
  157. uint32_t nrfx_atomic_u32_add(nrfx_atomic_u32_t * p_data, uint32_t value);
  158. /**
  159. * @brief Function for running an arithmetic SUB operation on an atomic object
  160. * and returning its previous value.
  161. *
  162. * @param[in] p_data Atomic memory pointer.
  163. * @param[in] value Value of the second operand in the SUB operation.
  164. *
  165. * @return Old value stored in the atomic object.
  166. */
  167. uint32_t nrfx_atomic_u32_fetch_sub(nrfx_atomic_u32_t * p_data, uint32_t value);
  168. /**
  169. * @brief Function for running an arithmetic SUB operation on an atomic object
  170. * and returning its new value.
  171. *
  172. * @param[in] p_data Atomic memory pointer.
  173. * @param[in] value Value of the second operand in the SUB operation.
  174. *
  175. * @return New value stored in the atomic object.
  176. */
  177. uint32_t nrfx_atomic_u32_sub(nrfx_atomic_u32_t * p_data, uint32_t value);
  178. /**
  179. * @brief Function for atomic conditional value replacement.
  180. *
  181. * Atomically compares the value pointed to by @p p_data with the value pointed to by @p p_expected.
  182. * If those are equal, replaces the former with desired. Otherwise, loads the actual value
  183. * pointed to by @p p_data into @p *p_expected.
  184. *
  185. * @param p_data Atomic memory pointer to test and modify.
  186. * @param p_expected Pointer to the test value.
  187. * @param desired Value to be stored to atomic memory.
  188. *
  189. * @retval true @p *p_data was equal to @p *p_expected.
  190. * @retval false @p *p_data was not equal to @p *p_expected.
  191. */
  192. bool nrfx_atomic_u32_cmp_exch(nrfx_atomic_u32_t * p_data,
  193. uint32_t * p_expected,
  194. uint32_t desired);
  195. /**
  196. * @brief Function for running an arithmetic SUB operation on an atomic object
  197. * if object >= value, and returning its previous value.
  198. *
  199. * @param[in] p_data Atomic memory pointer.
  200. * @param[in] value Value of the second operand in the SUB operation.
  201. *
  202. * @return Previous value stored in the atomic object.
  203. */
  204. uint32_t nrfx_atomic_u32_fetch_sub_hs(nrfx_atomic_u32_t * p_data, uint32_t value);
  205. /**
  206. * @brief Function for running an arithmetic SUB operation on an atomic object
  207. * if object >= value, and returning its new value.
  208. *
  209. * @param[in] p_data Atomic memory pointer.
  210. * @param[in] value Value of the second operand in the SUB operation.
  211. *
  212. * @return New value stored in the atomic object.
  213. */
  214. uint32_t nrfx_atomic_u32_sub_hs(nrfx_atomic_u32_t * p_data, uint32_t value);
  215. /**
  216. * @brief Function for running a logical one bit flag set operation
  217. * on an atomic object and returning its previous value.
  218. *
  219. * @param[in] p_data Atomic flag memory pointer.
  220. *
  221. * @return Previous flag value.
  222. */
  223. uint32_t nrfx_atomic_flag_set_fetch(nrfx_atomic_flag_t * p_data);
  224. /**
  225. * @brief Function for running a logical one bit flag set operation
  226. * on an atomic object and returning its new value.
  227. *
  228. * @param[in] p_data Atomic flag memory pointer.
  229. *
  230. * @return New flag value.
  231. */
  232. uint32_t nrfx_atomic_flag_set(nrfx_atomic_flag_t * p_data);
  233. /**
  234. * @brief Function for running a logical one bit flag clear operation
  235. * on an atomic object and returning its previous value.
  236. *
  237. * @param[in] p_data Atomic flag memory pointer.
  238. *
  239. * @return Previous flag value.
  240. */
  241. uint32_t nrfx_atomic_flag_clear_fetch(nrfx_atomic_flag_t * p_data);
  242. /**
  243. * @brief Function for running a logical one bit flag clear operation
  244. * on an atomic object and returning its new value.
  245. *
  246. * @param[in] p_data Atomic flag memory pointer.
  247. *
  248. * @return New flag value.
  249. */
  250. uint32_t nrfx_atomic_flag_clear(nrfx_atomic_flag_t * p_data);
  251. /** @} */
  252. #ifdef __cplusplus
  253. }
  254. #endif
  255. #endif // NRFX_ATOMIC_H__