nrf_atomic.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. /**@file
  41. *
  42. * @defgroup nrf_atomic Atomic operations API
  43. * @ingroup app_common
  44. * @{
  45. *
  46. * @brief @tagAPI52 This module implements C11 stdatomic.h simplified API.
  47. At this point only Cortex-M3/M4 cores are supported (LDREX/STREX instructions).
  48. * Atomic types are limited to @ref nrf_atomic_u32_t and @ref nrf_atomic_flag_t.
  49. */
  50. #ifndef NRF_ATOMIC_H__
  51. #define NRF_ATOMIC_H__
  52. #include "sdk_common.h"
  53. /**
  54. * @brief Atomic 32 bit unsigned type
  55. * */
  56. typedef volatile uint32_t nrf_atomic_u32_t;
  57. /**
  58. * @brief Atomic 1 bit flag type (technically 32 bit)
  59. * */
  60. typedef volatile uint32_t nrf_atomic_flag_t;
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /**
  65. * @brief Stores value to an atomic object
  66. *
  67. * @param[in] p_data Atomic memory pointer
  68. * @param[in] value Value to store
  69. *
  70. * @return Old value stored into atomic object
  71. * */
  72. uint32_t nrf_atomic_u32_fetch_store(nrf_atomic_u32_t * p_data, uint32_t value);
  73. /**
  74. * @brief Stores value to an atomic object
  75. *
  76. * @param[in] p_data Atomic memory pointer
  77. * @param[in] value Value to store
  78. *
  79. * @return New value stored into atomic object
  80. * */
  81. uint32_t nrf_atomic_u32_store(nrf_atomic_u32_t * p_data, uint32_t value);
  82. /**
  83. * @brief Logical OR operation on an atomic object
  84. *
  85. * @param[in] p_data Atomic memory pointer
  86. * @param[in] value Value of second operand OR operation
  87. *
  88. * @return Old value stored into atomic object
  89. * */
  90. uint32_t nrf_atomic_u32_fetch_or(nrf_atomic_u32_t * p_data, uint32_t value);
  91. /**
  92. * @brief Logical OR operation on an atomic object
  93. *
  94. * @param[in] p_data Atomic memory pointer
  95. * @param[in] value Value of second operand OR operation
  96. *
  97. * @return New value stored into atomic object
  98. * */
  99. uint32_t nrf_atomic_u32_or(nrf_atomic_u32_t * p_data, uint32_t value);
  100. /**
  101. * @brief Logical AND operation on an atomic object
  102. *
  103. * @param[in] p_data Atomic memory pointer
  104. * @param[in] value Value of second operand AND operation
  105. *
  106. * @return Old value stored into atomic object
  107. * */
  108. uint32_t nrf_atomic_u32_fetch_and(nrf_atomic_u32_t * p_data, uint32_t value);
  109. /**
  110. * @brief Logical AND operation on an atomic object
  111. *
  112. * @param[in] p_data Atomic memory pointer
  113. * @param[in] value Value of second operand AND operation
  114. *
  115. * @return New value stored into atomic object
  116. * */
  117. uint32_t nrf_atomic_u32_and(nrf_atomic_u32_t * p_data, uint32_t value);
  118. /**
  119. * @brief Logical XOR operation on an atomic object
  120. *
  121. * @param[in] p_data Atomic memory pointer
  122. * @param[in] value Value of second operand XOR operation
  123. *
  124. * @return Old value stored into atomic object
  125. * */
  126. uint32_t nrf_atomic_u32_fetch_xor(nrf_atomic_u32_t * p_data, uint32_t value);
  127. /**
  128. * @brief Logical XOR operation on an atomic object
  129. *
  130. * @param[in] p_data Atomic memory pointer
  131. * @param[in] value Value of second operand XOR operation
  132. *
  133. * @return New value stored into atomic object
  134. * */
  135. uint32_t nrf_atomic_u32_xor(nrf_atomic_u32_t * p_data, uint32_t value);
  136. /**
  137. * @brief Arithmetic ADD operation on an atomic object
  138. *
  139. * @param[in] p_data Atomic memory pointer
  140. * @param[in] value Value of second operand ADD operation
  141. *
  142. * @return Old value stored into atomic object
  143. * */
  144. uint32_t nrf_atomic_u32_fetch_add(nrf_atomic_u32_t * p_data, uint32_t value);
  145. /**
  146. * @brief Arithmetic ADD operation on an atomic object
  147. *
  148. * @param[in] p_data Atomic memory pointer
  149. * @param[in] value Value of second operand ADD operation
  150. *
  151. * @return New value stored into atomic object
  152. * */
  153. uint32_t nrf_atomic_u32_add(nrf_atomic_u32_t * p_data, uint32_t value);
  154. /**
  155. * @brief Arithmetic SUB operation on an atomic object
  156. *
  157. * @param[in] p_data Atomic memory pointer
  158. * @param[in] value Value of second operand SUB operation
  159. *
  160. * @return Old value stored into atomic object
  161. * */
  162. uint32_t nrf_atomic_u32_fetch_sub(nrf_atomic_u32_t * p_data, uint32_t value);
  163. /**
  164. * @brief Arithmetic SUB operation on an atomic object
  165. *
  166. * @param[in] p_data Atomic memory pointer
  167. * @param[in] value Value of second operand SUB operation
  168. *
  169. * @return New value stored into atomic object
  170. * */
  171. uint32_t nrf_atomic_u32_sub(nrf_atomic_u32_t * p_data, uint32_t value);
  172. /**
  173. * @brief If value at pointer is equal to expected value, changes value at pointer to desired
  174. *
  175. * Atomically compares the value pointed to by p_data with the value pointed to by p_expected,
  176. * and if those are equal, replaces the former with desired. Otherwise, loads the actual value
  177. * pointed to by p_data into *p_expected.
  178. *
  179. * @param p_data Atomic memory pointer to test and modify.
  180. * @param p_expected Pointer to test value.
  181. * @param desired Value to be stored to atomic memory.
  182. *
  183. * @retval true *p_data was equal to *p_expected
  184. * @retval false *p_data was not equal to *p_expected
  185. */
  186. bool nrf_atomic_u32_cmp_exch(nrf_atomic_u32_t * p_data,
  187. uint32_t * p_expected,
  188. uint32_t desired);
  189. /**
  190. * @brief Arithmetic SUB operation on an atomic object performed if object >= value.
  191. *
  192. * @param[in] p_data Atomic memory pointer
  193. * @param[in] value Value of second operand SUB operation
  194. *
  195. * @return Old value stored into atomic object
  196. * */
  197. uint32_t nrf_atomic_u32_fetch_sub_hs(nrf_atomic_u32_t * p_data, uint32_t value);
  198. /**
  199. * @brief Arithmetic SUB operation on an atomic object performed if object >= value.
  200. *
  201. * @param[in] p_data Atomic memory pointer
  202. * @param[in] value Value of second operand SUB operation
  203. *
  204. * @return New value stored into atomic object
  205. * */
  206. uint32_t nrf_atomic_u32_sub_hs(nrf_atomic_u32_t * p_data, uint32_t value);
  207. /**************************************************************************************************/
  208. /**
  209. * @brief Logic one bit flag set operation on an atomic object
  210. *
  211. * @param[in] p_data Atomic flag memory pointer
  212. *
  213. * @return Old flag value
  214. * */
  215. uint32_t nrf_atomic_flag_set_fetch(nrf_atomic_flag_t * p_data);
  216. /**
  217. * @brief Logic one bit flag set operation on an atomic object
  218. *
  219. * @param[in] p_data Atomic flag memory pointer
  220. *
  221. * @return New flag value
  222. * */
  223. uint32_t nrf_atomic_flag_set(nrf_atomic_flag_t * p_data);
  224. /**
  225. * @brief Logic one bit flag clear operation on an atomic object
  226. *
  227. * @param[in] p_data Atomic flag memory pointer
  228. *
  229. * @return Old flag value
  230. * */
  231. uint32_t nrf_atomic_flag_clear_fetch(nrf_atomic_flag_t * p_data);
  232. /**
  233. * @brief Logic one bit flag clear operation on an atomic object
  234. *
  235. * @param[in] p_data Atomic flag memory pointer
  236. *
  237. * @return New flag value
  238. * */
  239. uint32_t nrf_atomic_flag_clear(nrf_atomic_flag_t * p_data);
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif /* NRF_ATOMIC_H__ */
  244. /** @} */