trng.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /**
  2. * \file
  3. *
  4. * \brief SAM True Random Number Generator (TRNG) Driver
  5. *
  6. * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef TRNG_H_INCLUDED
  47. #define TRNG_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_trng_group SAM True Random Number Generator (TRNG) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides an interface for the configuration
  52. * and management of the device's True Random Number Generator functionality.
  53. *
  54. * The following driver API modes are covered by this manual:
  55. * - Polled APIs
  56. * \if TRNG_CALLBACK_MODE
  57. * - Callback APIs
  58. * \endif
  59. *
  60. * The following peripheral is used by this module:
  61. * - TRNG (True Random Number Generator)
  62. *
  63. * The following devices can use this module:
  64. * - Atmel | SMART SAM L21/L22
  65. *
  66. * The outline of this documentation is as follows:
  67. * - \ref asfdoc_sam0_trng_prerequisites
  68. * - \ref asfdoc_sam0_trng_module_overview
  69. * - \ref asfdoc_sam0_trng_special_considerations
  70. * - \ref asfdoc_sam0_trng_extra_info
  71. * - \ref asfdoc_sam0_trng_examples
  72. * - \ref asfdoc_sam0_trng_api_overview
  73. *
  74. *
  75. * \section asfdoc_sam0_trng_prerequisites Prerequisites
  76. *
  77. * There are no prerequisites for this module.
  78. *
  79. *
  80. * \section asfdoc_sam0_trng_module_overview Module Overview
  81. *
  82. * This driver provides an interface for the TRNG functions on the device.
  83. *
  84. * As soon as the TRNG is enabled, the module provides a new 32-bit random
  85. * data, for every 84 CLK_TRNG_APB clock cycles.
  86. *
  87. *
  88. * \section asfdoc_sam0_trng_special_considerations Special Considerations
  89. *
  90. * There are no special considerations for this module.
  91. *
  92. *
  93. * \section asfdoc_sam0_trng_extra_info Extra Information
  94. *
  95. * For extra information, see \ref asfdoc_sam0_trng_extra. This includes:
  96. * - \ref asfdoc_sam0_trng_extra_acronyms
  97. * - \ref asfdoc_sam0_trng_extra_dependencies
  98. * - \ref asfdoc_sam0_trng_extra_errata
  99. * - \ref asfdoc_sam0_trng_extra_history
  100. *
  101. *
  102. * \section asfdoc_sam0_trng_examples Examples
  103. *
  104. * For a list of examples related to this driver, see
  105. * \ref asfdoc_sam0_trng_exqsg.
  106. *
  107. *
  108. * \section asfdoc_sam0_trng_api_overview API Overview
  109. * @{
  110. */
  111. #include <compiler.h>
  112. #include <system.h>
  113. #ifdef __cplusplus
  114. extern "C" {
  115. #endif
  116. #if TRNG_CALLBACK_MODE == true
  117. /* Forward declaration of struct */
  118. struct trng_module;
  119. extern struct trng_module *_trng_instance;
  120. /** Type definition for a TRNG module callback function. */
  121. typedef void (*trng_callback_t)(struct trng_module *const module_inst);
  122. /** Enum for possible callback types for the TRNG module. */
  123. enum trng_callback {
  124. /** Callback for specific number of random data ready */
  125. TRNG_CALLBACK_READ_BUFFER = 0,
  126. /** Number of available callbacks */
  127. #if !defined(__DOXYGEN__)
  128. TRNG_CALLBACK_N,
  129. #endif
  130. };
  131. #endif
  132. /**
  133. * \brief TRNG software device instance structure.
  134. *
  135. * TRNG software instance structure, used to retain software state information
  136. * of an associated hardware module instance.
  137. *
  138. * \note The fields of this structure should not be altered by the user
  139. * application; they are reserved for module-internal use only.
  140. */
  141. struct trng_module {
  142. #if !defined(__DOXYGEN__)
  143. /** Hardware module pointer of the associated TRNG peripheral */
  144. Trng *hw;
  145. # if TRNG_CALLBACK_MODE == true
  146. /** Array of callbacks */
  147. trng_callback_t callback[TRNG_CALLBACK_N];
  148. /** Bit mask for callbacks registered */
  149. uint8_t register_callback_mask;
  150. /** Bit mask for callbacks enabled */
  151. uint8_t enable_callback_mask;
  152. /** Holds the status of the ongoing or last read job */
  153. volatile enum status_code job_status;
  154. /** Pointer to buffer used for TRNG results */
  155. volatile uint32_t *job_buffer;
  156. /** Remaining number of TRNG results in current job */
  157. volatile uint32_t remaining_number;
  158. # endif
  159. #endif
  160. };
  161. /**
  162. * \brief TRNG module configuration structure.
  163. *
  164. * Configuration structure for a True Random Number Generator.
  165. */
  166. struct trng_config {
  167. /** If \c true, the True Random Number Generator will not be stopped in
  168. * standby sleep mode */
  169. bool run_in_standby;
  170. };
  171. /**
  172. * \brief TRNG event enable/disable structure.
  173. *
  174. * Event flags for the TRNG module. This is used to enable and
  175. * disable events via \ref trng_enable_events() and \ref trng_disable_events().
  176. */
  177. struct trng_events {
  178. /** Enable event generation on random data ready */
  179. bool generate_event_on_data_ready;
  180. };
  181. /**
  182. * \name Driver Initialization and Configuration
  183. * @{
  184. */
  185. enum status_code trng_init(
  186. struct trng_module *const module_inst,
  187. Trng *const hw,
  188. struct trng_config *const config);
  189. /**
  190. * \brief Initializes all members of a TRNG configuration structure
  191. * to safe defaults.
  192. *
  193. * Initializes all members of a given True Random Number Generator configuration
  194. * structure to safe known default values. This function should be called on
  195. * all new instances of these configuration structures before being modified
  196. * by the user application.
  197. *
  198. * The default configuration is as follows:
  199. * \li True Random Number Generator will not be stopped in standby sleep mode
  200. *
  201. * \param[out] config Configuration structure to initialize to default values
  202. */
  203. static inline void trng_get_config_defaults(
  204. struct trng_config *const config)
  205. {
  206. /* Sanity check arguments */
  207. Assert(config);
  208. /* Default configuration values */
  209. config->run_in_standby = false;
  210. }
  211. /**
  212. * \brief Enables a TRNG that was previously configured.
  213. *
  214. * Enables True Random Number Generator that was previously configured via a
  215. * call to \ref trng_init().
  216. *
  217. * \param[in] module_inst Software instance for the True Random Number
  218. * Generator peripheral
  219. */
  220. static inline void trng_enable(
  221. struct trng_module *const module_inst)
  222. {
  223. /* Sanity check arguments */
  224. Assert(module_inst);
  225. Assert(module_inst->hw);
  226. Trng *const trng_module = module_inst->hw;
  227. /* Enable TRNG */
  228. trng_module->CTRLA.reg |= TRNG_CTRLA_ENABLE;
  229. }
  230. /**
  231. * \brief Disables a TRNG that was previously enabled.
  232. *
  233. * Disables True Random Number Generator that was previously started via a call
  234. * to \ref trng_enable().
  235. *
  236. * \param[in] module_inst Software instance for the True Random Number
  237. * Generator peripheral
  238. */
  239. static inline void trng_disable(
  240. struct trng_module *const module_inst)
  241. {
  242. /* Sanity check arguments */
  243. Assert(module_inst);
  244. Assert(module_inst->hw);
  245. Trng *const trng_module = module_inst->hw;
  246. /* Disbale interrupt */
  247. trng_module->INTENCLR.reg = TRNG_INTENCLR_MASK;
  248. /* Clear interrupt flag */
  249. trng_module->INTFLAG.reg = TRNG_INTFLAG_MASK;
  250. /* Disable TRNG */
  251. trng_module->CTRLA.reg &= ~TRNG_CTRLA_ENABLE;
  252. }
  253. /**
  254. * \brief Enables a TRNG event output.
  255. *
  256. * Enables output events from the True Random Number Generator
  257. * module. See \ref Section Struct trng_events for a list of events this module
  258. * supports.
  259. *
  260. * \note Events cannot be altered while the module is enabled.
  261. *
  262. * \param[in] module_inst Software instance for the TRNG peripheral
  263. * \param[in] events Struct containing flags of events to enable
  264. */
  265. static inline void trng_enable_events(
  266. struct trng_module *const module_inst,
  267. struct trng_events *const events)
  268. {
  269. /* Sanity check arguments */
  270. Assert(module_inst);
  271. Assert(module_inst->hw);
  272. Trng *const trng_module = module_inst->hw;
  273. if (events->generate_event_on_data_ready) {
  274. /* Enable data ready event output */
  275. trng_module->EVCTRL.reg |= TRNG_EVCTRL_DATARDYEO;
  276. }
  277. }
  278. /**
  279. * \brief Disables a TRNG event output.
  280. *
  281. * Disables output events from the True Random Number Generator
  282. * module. See \ref Section Struct trng_events for a list of events
  283. * this module supports.
  284. *
  285. * \note Events cannot be altered while the module is enabled.
  286. *
  287. * \param[in] module_inst Software instance for the TRNG peripheral
  288. * \param[in] events Struct containing flags of events to disable
  289. */
  290. static inline void trng_disable_events(
  291. struct trng_module *const module_inst,
  292. struct trng_events *const events)
  293. {
  294. /* Sanity check arguments */
  295. Assert(module_inst);
  296. Assert(module_inst->hw);
  297. Trng *const trng_module = module_inst->hw;
  298. if (events->generate_event_on_data_ready) {
  299. /* Disable data ready event output */
  300. trng_module->EVCTRL.reg &= ~TRNG_EVCTRL_DATARDYEO;
  301. }
  302. }
  303. /** @} */
  304. /**
  305. * \name Read TRNG Result
  306. * @{
  307. */
  308. /**
  309. * \brief Read the random data result
  310. *
  311. * Reads the random data result.
  312. *
  313. * \param[in] module_inst Pointer to the TRNG software instance struct
  314. * \param[out] result Pointer to store the result value in
  315. *
  316. * \return Status of the TRNG read request.
  317. * \retval STATUS_OK The result was retrieved successfully
  318. * \retval STATUS_BUSY A random result was not ready
  319. */
  320. static inline enum status_code trng_read(
  321. struct trng_module *const module_inst,
  322. uint32_t *result)
  323. {
  324. /* Sanity check arguments */
  325. Assert(module_inst);
  326. Assert(module_inst->hw);
  327. Assert(result);
  328. Trng *const trng_hw = module_inst->hw;
  329. if (!(trng_hw->INTFLAG.reg & TRNG_INTFLAG_DATARDY)) {
  330. /* Result not ready */
  331. return STATUS_BUSY;
  332. }
  333. /* Get randomly generated output data (it will clear data ready flag) */
  334. *result = trng_hw->DATA.reg;
  335. return STATUS_OK;
  336. }
  337. /** @} */
  338. #ifdef __cplusplus
  339. }
  340. #endif
  341. /** @} */
  342. /**
  343. * \page asfdoc_sam0_trng_extra Extra Information for TRNG Driver
  344. *
  345. * \section asfdoc_sam0_trng_extra_acronyms Acronyms
  346. *
  347. * <table>
  348. * <tr>
  349. * <th>Acronym</th>
  350. * <th>Description</th>
  351. * </tr>
  352. * <tr>
  353. * <td>TRNG</td>
  354. * <td>True Random Number Generator</td>
  355. * </tr>
  356. * </table>
  357. *
  358. *
  359. * \section asfdoc_sam0_trng_extra_dependencies Dependencies
  360. * This driver has no dependencies.
  361. *
  362. *
  363. * \section asfdoc_sam0_trng_extra_errata Errata
  364. * There are no errata related to this driver.
  365. *
  366. *
  367. * \section asfdoc_sam0_trng_extra_history Module History
  368. * An overview of the module history is presented in the table below, with
  369. * details on the enhancements and fixes made to the module since its first
  370. * release. The current version of this corresponds to the newest version in
  371. * the table.
  372. *
  373. * <table>
  374. * <tr>
  375. * <th>Changelog</th>
  376. * </tr>
  377. * <tr>
  378. * <td>Initial Release</td>
  379. * </tr>
  380. * </table>
  381. */
  382. /**
  383. * \page asfdoc_sam0_trng_exqsg Examples for TRNG Driver
  384. *
  385. * This is a list of the available Quick Start guides (QSGs) and example
  386. * applications for \ref asfdoc_sam0_trng_group. QSGs are simple examples with
  387. * step-by-step instructions to configure and use this driver in a selection of
  388. * use cases. Note that QSGs can be compiled as a standalone application or be
  389. * added to the user application.
  390. *
  391. * - \subpage asfdoc_sam0_trng_basic_use_case
  392. * \if TRNG_CALLBACK_MODE
  393. * - \subpage asfdoc_sam0_trng_basic_use_case_callback
  394. * \endif
  395. *
  396. * \page asfdoc_sam0_trng_document_revision_history Document Revision History
  397. *
  398. * <table>
  399. * <tr>
  400. * <th>Doc. Rev.</td>
  401. * <th>Date</td>
  402. * <th>Comments</td>
  403. * </tr>
  404. * <tr>
  405. * <td>42444B</td>
  406. * <td>01/2016</td>
  407. * <td>Added support for SAM L22</td>
  408. * </tr>
  409. * <tr>
  410. * <td>42444A</td>
  411. * <td>06/2015</td>
  412. * <td>Initial document release</td>
  413. * </tr>
  414. * </table>
  415. */
  416. #endif /* TRNG_H_INCLUDED */