nrf_ccm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #ifndef NRF_CCM_H__
  41. #define NRF_CCM_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_ccm_hal AES CCM HAL
  48. * @{
  49. * @ingroup nrf_ccm
  50. * @brief Hardware access layer for managing the AES CCM peripheral.
  51. */
  52. /** @brief CCM tasks. */
  53. typedef enum
  54. {
  55. NRF_CCM_TASK_KSGEN = offsetof(NRF_CCM_Type, TASKS_KSGEN), ///< Start generation of key-stream.
  56. NRF_CCM_TASK_CRYPT = offsetof(NRF_CCM_Type, TASKS_CRYPT), ///< Start encryption/decryption.
  57. NRF_CCM_TASK_STOP = offsetof(NRF_CCM_Type, TASKS_STOP), ///< Stop encryption/decryption.
  58. #if defined(CCM_RATEOVERRIDE_RATEOVERRIDE_Pos) || defined(__NRFX_DOXYGEN__)
  59. NRF_CCM_TASK_RATEOVERRIDE = offsetof(NRF_CCM_Type, TASKS_RATEOVERRIDE), ///< Override DATARATE setting in MODE register.
  60. #endif
  61. } nrf_ccm_task_t;
  62. /** @brief CCM events. */
  63. typedef enum
  64. {
  65. NRF_CCM_EVENT_ENDKSGEN = offsetof(NRF_CCM_Type, EVENTS_ENDKSGEN), ///< Keystream generation complete.
  66. NRF_CCM_EVENT_ENDCRYPT = offsetof(NRF_CCM_Type, EVENTS_ENDCRYPT), ///< Encrypt/decrypt complete.
  67. NRF_CCM_EVENT_ERROR = offsetof(NRF_CCM_Type, EVENTS_ERROR), ///< CCM error event.
  68. } nrf_ccm_event_t;
  69. /** @brief CCM interrupts. */
  70. typedef enum
  71. {
  72. NRF_CCM_INT_ENDKSGEN_MASK = CCM_INTENSET_ENDKSGEN_Msk, ///< Interrupt on ENDKSGEN event.
  73. NRF_CCM_INT_ENDCRYPT_MASK = CCM_INTENSET_ENDCRYPT_Msk, ///< Interrupt on ENDCRYPT event.
  74. NRF_CCM_INT_ERROR_MASK = CCM_INTENSET_ERROR_Msk, ///< Interrupt on ERROR event.
  75. } nrf_ccm_int_mask_t;
  76. /** @brief CCM modes of operation. */
  77. typedef enum
  78. {
  79. NRF_CCM_MODE_ENCRYPTION = CCM_MODE_MODE_Encryption, ///< Encryption mode.
  80. NRF_CCM_MODE_DECRYPTION = CCM_MODE_MODE_Decryption, ///< Decryption mode.
  81. } nrf_ccm_mode_t;
  82. #if defined(CCM_MODE_DATARATE_Pos) || defined(__NRFX_DOXYGEN__)
  83. /** @brief CCM data rates. */
  84. typedef enum
  85. {
  86. NRF_CCM_DATARATE_1M = CCM_MODE_DATARATE_1Mbit, ///< 1 Mbps.
  87. NRF_CCM_DATARATE_2M = CCM_MODE_DATARATE_2Mbit, ///< 2 Mbps.
  88. #if defined(CCM_MODE_DATARATE_125Kbps) || defined(__NRFX_DOXYGEN__)
  89. NRF_CCM_DATARATE_125K = CCM_MODE_DATARATE_125Kbps, ///< 125 Kbps.
  90. #endif
  91. #if defined(CCM_MODE_DATARATE_500Kbps) || defined(__NRFX_DOXYGEN__)
  92. NRF_CCM_DATARATE_500K = CCM_MODE_DATARATE_500Kbps, ///< 500 Kbps.
  93. #endif
  94. } nrf_ccm_datarate_t;
  95. #endif // defined(CCM_MODE_DATARATE_Pos) || defined(__NRFX_DOXYGEN__)
  96. #if defined(CCM_MODE_LENGTH_Pos) || defined(__NRFX_DOXYGEN__)
  97. /** @brief CCM packet length options. */
  98. typedef enum
  99. {
  100. NRF_CCM_LENGTH_DEFAULT = CCM_MODE_LENGTH_Default, ///< Default length.
  101. NRF_CCM_LENGTH_EXTENDED = CCM_MODE_LENGTH_Extended, ///< Extended length.
  102. } nrf_ccm_length_t;
  103. #endif // defined(CCM_MODE_LENGTH_Pos) || defined(__NRFX_DOXYGEN__)
  104. /** @brief CCM configuration. */
  105. typedef struct {
  106. nrf_ccm_mode_t mode; ///< Operation mode.
  107. #if defined(CCM_MODE_DATARATE_Pos) || defined(__NRFX_DOXYGEN__)
  108. nrf_ccm_datarate_t datarate; ///< Data rate.
  109. #endif
  110. #if defined(CCM_MODE_LENGTH_Pos) || defined(__NRFX_DOXYGEN__)
  111. nrf_ccm_length_t length; ///< Lenght of the CCM packet.
  112. #endif
  113. } nrf_ccm_config_t;
  114. /**
  115. * @brief Function for activating a specific CCM task.
  116. *
  117. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  118. * @param[in] task Task to be activated.
  119. */
  120. __STATIC_INLINE void nrf_ccm_task_trigger(NRF_CCM_Type * p_reg,
  121. nrf_ccm_task_t task);
  122. /**
  123. * @brief Function for getting the address of a specific CCM task register.
  124. *
  125. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  126. * @param[in] task Requested task.
  127. *
  128. * @return Address of the specified task register.
  129. */
  130. __STATIC_INLINE uint32_t nrf_ccm_task_address_get(NRF_CCM_Type const * p_reg,
  131. nrf_ccm_task_t task);
  132. /**
  133. * @brief Function for clearing a specific CCM event.
  134. *
  135. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  136. * @param[in] event Event to clear.
  137. */
  138. __STATIC_INLINE void nrf_ccm_event_clear(NRF_CCM_Type * p_reg,
  139. nrf_ccm_event_t event);
  140. /**
  141. * @brief Function for retrieving the state of a specific CCM event.
  142. *
  143. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  144. * @param[in] event Event to be checked.
  145. *
  146. * @retval true The event has been generated.
  147. * @retval false The event has not been generated.
  148. */
  149. __STATIC_INLINE bool nrf_ccm_event_check(NRF_CCM_Type const * p_reg,
  150. nrf_ccm_event_t event);
  151. /**
  152. * @brief Function for getting the address of a specific CCM event register.
  153. *
  154. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  155. * @param[in] event Requested event.
  156. *
  157. * @return Address of the specified event register.
  158. */
  159. __STATIC_INLINE uint32_t nrf_ccm_event_address_get(NRF_CCM_Type const * p_reg,
  160. nrf_ccm_event_t event);
  161. /**
  162. * @brief Function for enabling specified interrupts.
  163. *
  164. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  165. * @param[in] mask Interrupts to be enabled.
  166. */
  167. __STATIC_INLINE void nrf_ccm_int_enable(NRF_CCM_Type * p_reg, uint32_t mask);
  168. /**
  169. * @brief Function for disabling specified interrupts.
  170. *
  171. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  172. * @param[in] mask Interrupts to be disabled.
  173. */
  174. __STATIC_INLINE void nrf_ccm_int_disable(NRF_CCM_Type * p_reg, uint32_t mask);
  175. /**
  176. * @brief Function for retrieving the state of a given interrupt.
  177. *
  178. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  179. * @param[in] ccm_int Interrupt to be checked.
  180. *
  181. * @retval true The interrupt is enabled.
  182. * @retval false The interrupt is not enabled.
  183. */
  184. __STATIC_INLINE bool nrf_ccm_int_enable_check(NRF_CCM_Type const * p_reg,
  185. nrf_ccm_int_mask_t ccm_int);
  186. /**
  187. * @brief Function for enabling the CCM peripheral.
  188. *
  189. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  190. */
  191. __STATIC_INLINE void nrf_ccm_enable(NRF_CCM_Type * p_reg);
  192. /**
  193. * @brief Function for disabling the CCM peripheral.
  194. *
  195. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  196. */
  197. __STATIC_INLINE void nrf_ccm_disable(NRF_CCM_Type * p_reg);
  198. /**
  199. * @brief Function for setting the CCM peripheral configuration.
  200. *
  201. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  202. * @param[in] p_config Pointer to the structure with configuration to be set.
  203. */
  204. __STATIC_INLINE void nrf_ccm_configure(NRF_CCM_Type * p_reg,
  205. nrf_ccm_config_t const * p_config);
  206. #if defined(CCM_MAXPACKETSIZE_MAXPACKETSIZE_Pos) || defined(__NRFX_DOXYGEN__)
  207. /**
  208. * @brief Function for setting the length of key-stream generated
  209. * when the packet length is configured as extended.
  210. *
  211. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  212. * @param[in] size Maximum length of the key-stream.
  213. */
  214. __STATIC_INLINE void nrf_ccm_maxpacketsize_set(NRF_CCM_Type * p_reg,
  215. uint8_t size);
  216. #endif // defined(CCM_MAXPACKETSIZE_MAXPACKETSIZE_Pos) || defined(__NRFX_DOXYGEN__)
  217. /**
  218. * @brief Function for getting the MIC check result.
  219. *
  220. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  221. *
  222. * @retval true The MIC check passed.
  223. * @retval false The MIC check failed.
  224. */
  225. __STATIC_INLINE bool nrf_ccm_micstatus_get(NRF_CCM_Type const * p_reg);
  226. /**
  227. * @brief Function for setting the pointer to the data structure
  228. * holding the AES key and the CCM NONCE vector.
  229. *
  230. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  231. * @param[in] p_data Pointer to the data structure.
  232. */
  233. __STATIC_INLINE void nrf_ccm_cnfptr_set(NRF_CCM_Type * p_reg,
  234. uint32_t const * p_data);
  235. /**
  236. * @brief Function for getting the pointer to the data structure
  237. * holding the AES key and the CCM NONCE vector.
  238. *
  239. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  240. *
  241. * @return Pointer to the data structure.
  242. */
  243. __STATIC_INLINE uint32_t * nrf_ccm_cnfptr_get(NRF_CCM_Type const * p_reg);
  244. /**
  245. * @brief Function for setting the input data pointer.
  246. *
  247. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  248. * @param[in] p_data Input data pointer.
  249. */
  250. __STATIC_INLINE void nrf_ccm_inptr_set(NRF_CCM_Type * p_reg,
  251. uint32_t const * p_data);
  252. /**
  253. * @brief Function for getting the input data pointer.
  254. *
  255. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  256. *
  257. * @return Input data pointer.
  258. */
  259. __STATIC_INLINE uint32_t * nrf_ccm_inptr_get(NRF_CCM_Type const * p_reg);
  260. /**
  261. * @brief Function for setting the output data pointer.
  262. *
  263. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  264. * @param[in] p_data Output data pointer.
  265. */
  266. __STATIC_INLINE void nrf_ccm_outptr_set(NRF_CCM_Type * p_reg,
  267. uint32_t const * p_data);
  268. /**
  269. * @brief Function for getting the output data pointer.
  270. *
  271. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  272. *
  273. * @return Output data pointer.
  274. */
  275. __STATIC_INLINE uint32_t * nrf_ccm_outptr_get(NRF_CCM_Type const * p_reg);
  276. /**
  277. * @brief Function for setting the pointer to the scratch area used for
  278. * temporary storage.
  279. *
  280. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  281. * @param[in] p_area Pointer to the scratch area.
  282. */
  283. __STATIC_INLINE void nrf_ccm_scratchptr_set(NRF_CCM_Type * p_reg,
  284. uint32_t const * p_area);
  285. /**
  286. * @brief Function for getting the pointer to the scratch area.
  287. *
  288. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  289. *
  290. * @return Pointer to the scratch area.
  291. */
  292. __STATIC_INLINE uint32_t * nrf_ccm_stratchptr_get(NRF_CCM_Type const * p_reg);
  293. #if defined(CCM_RATEOVERRIDE_RATEOVERRIDE_Pos) || defined(__NRFX_DOXYGEN__)
  294. /**
  295. * @brief Function for setting the data rate override value.
  296. *
  297. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  298. * @param[in] datarate Override value to be applied when the RATEOVERRIDE task
  299. * is triggered.
  300. */
  301. __STATIC_INLINE void nrf_ccm_datarate_override_set(NRF_CCM_Type * p_reg,
  302. nrf_ccm_datarate_t datarate);
  303. #endif // defined(CCM_RATEOVERRIDE_RATEOVERRIDE_Pos) || defined(__NRFX_DOXYGEN__)
  304. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  305. __STATIC_INLINE void nrf_ccm_task_trigger(NRF_CCM_Type * p_reg,
  306. nrf_ccm_task_t task)
  307. {
  308. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  309. }
  310. __STATIC_INLINE uint32_t nrf_ccm_task_address_get(NRF_CCM_Type const * p_reg,
  311. nrf_ccm_task_t task)
  312. {
  313. return ((uint32_t)p_reg + (uint32_t)task);
  314. }
  315. __STATIC_INLINE void nrf_ccm_event_clear(NRF_CCM_Type * p_reg,
  316. nrf_ccm_event_t event)
  317. {
  318. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  319. #if __CORTEX_M == 0x04
  320. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  321. (void)dummy;
  322. #endif
  323. }
  324. __STATIC_INLINE bool nrf_ccm_event_check(NRF_CCM_Type const * p_reg,
  325. nrf_ccm_event_t event)
  326. {
  327. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  328. }
  329. __STATIC_INLINE uint32_t nrf_ccm_event_address_get(NRF_CCM_Type const * p_reg,
  330. nrf_ccm_event_t event)
  331. {
  332. return ((uint32_t)p_reg + (uint32_t)event);
  333. }
  334. __STATIC_INLINE void nrf_ccm_int_enable(NRF_CCM_Type * p_reg, uint32_t mask)
  335. {
  336. p_reg->INTENSET = mask;
  337. }
  338. __STATIC_INLINE void nrf_ccm_int_disable(NRF_CCM_Type * p_reg, uint32_t mask)
  339. {
  340. p_reg->INTENCLR = mask;
  341. }
  342. __STATIC_INLINE bool nrf_ccm_int_enable_check(NRF_CCM_Type const * p_reg,
  343. nrf_ccm_int_mask_t ccm_int)
  344. {
  345. return (bool)(p_reg->INTENSET & ccm_int);
  346. }
  347. __STATIC_INLINE void nrf_ccm_enable(NRF_CCM_Type * p_reg)
  348. {
  349. p_reg->ENABLE = (CCM_ENABLE_ENABLE_Enabled << CCM_ENABLE_ENABLE_Pos);
  350. }
  351. __STATIC_INLINE void nrf_ccm_disable(NRF_CCM_Type * p_reg)
  352. {
  353. p_reg->ENABLE = (CCM_ENABLE_ENABLE_Disabled << CCM_ENABLE_ENABLE_Pos);
  354. }
  355. __STATIC_INLINE void nrf_ccm_configure(NRF_CCM_Type * p_reg,
  356. nrf_ccm_config_t const * p_config)
  357. {
  358. p_reg->MODE = (((uint32_t)p_config->mode << CCM_MODE_MODE_Pos) |
  359. #if defined(CCM_MODE_DATARATE_Pos)
  360. ((uint32_t)p_config->datarate << CCM_MODE_DATARATE_Pos) |
  361. #endif
  362. #if defined(CCM_MODE_LENGTH_Pos)
  363. ((uint32_t)p_config->length << CCM_MODE_LENGTH_Pos) |
  364. #endif
  365. 0);
  366. }
  367. #if defined(CCM_MAXPACKETSIZE_MAXPACKETSIZE_Pos)
  368. __STATIC_INLINE void nrf_ccm_maxpacketsize_set(NRF_CCM_Type * p_reg,
  369. uint8_t size)
  370. {
  371. NRFX_ASSERT((size >= 0x1B) && (size <= 0xFB));
  372. p_reg->MAXPACKETSIZE = size;
  373. }
  374. #endif // defined(CCM_MAXPACKETSIZE_MAXPACKETSIZE_Pos)
  375. __STATIC_INLINE bool nrf_ccm_micstatus_get(NRF_CCM_Type const * p_reg)
  376. {
  377. return (bool)(p_reg->MICSTATUS);
  378. }
  379. __STATIC_INLINE void nrf_ccm_cnfptr_set(NRF_CCM_Type * p_reg,
  380. uint32_t const * p_data)
  381. {
  382. p_reg->CNFPTR = (uint32_t)p_data;
  383. }
  384. __STATIC_INLINE uint32_t * nrf_ccm_cnfptr_get(NRF_CCM_Type const * p_reg)
  385. {
  386. return (uint32_t *)(p_reg->CNFPTR);
  387. }
  388. __STATIC_INLINE void nrf_ccm_inptr_set(NRF_CCM_Type * p_reg,
  389. uint32_t const * p_data)
  390. {
  391. p_reg->INPTR = (uint32_t)p_data;
  392. }
  393. __STATIC_INLINE uint32_t * nrf_ccm_inptr_get(NRF_CCM_Type const * p_reg)
  394. {
  395. return (uint32_t *)(p_reg->INPTR);
  396. }
  397. __STATIC_INLINE void nrf_ccm_outptr_set(NRF_CCM_Type * p_reg,
  398. uint32_t const * p_data)
  399. {
  400. p_reg->OUTPTR = (uint32_t)p_data;
  401. }
  402. __STATIC_INLINE uint32_t * nrf_ccm_outptr_get(NRF_CCM_Type const * p_reg)
  403. {
  404. return (uint32_t *)(p_reg->OUTPTR);
  405. }
  406. __STATIC_INLINE void nrf_ccm_scratchptr_set(NRF_CCM_Type * p_reg,
  407. uint32_t const * p_area)
  408. {
  409. p_reg->SCRATCHPTR = (uint32_t)p_area;
  410. }
  411. __STATIC_INLINE uint32_t * nrf_ccm_stratchptr_get(NRF_CCM_Type const * p_reg)
  412. {
  413. return (uint32_t *)(p_reg->SCRATCHPTR);
  414. }
  415. #if defined(CCM_RATEOVERRIDE_RATEOVERRIDE_Pos)
  416. __STATIC_INLINE void nrf_ccm_datarate_override_set(NRF_CCM_Type * p_reg,
  417. nrf_ccm_datarate_t datarate)
  418. {
  419. p_reg->RATEOVERRIDE = ((uint32_t)datarate << CCM_RATEOVERRIDE_RATEOVERRIDE_Pos);
  420. }
  421. #endif
  422. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  423. /** @} */
  424. #ifdef __cplusplus
  425. }
  426. #endif
  427. #endif // NRF_CCM_H__