nrfx_atomic.h 9.7 KB

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