nrf_ccm.h 17 KB

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