trng_callback.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "trng_callback.h"
  47. #include "system_interrupt.h"
  48. struct trng_module *_trng_instance;
  49. /**
  50. * \brief Registers a callback
  51. *
  52. * Registers a callback function which is implemented by the user.
  53. *
  54. * \note The callback must be enabled by \ref trng_enable_callback,
  55. * in order for the interrupt handler to call it when the conditions for the
  56. * callback type is met.
  57. *
  58. * \param[in] module Pointer to TC software instance struct
  59. * \param[in] callback_func Pointer to callback function
  60. * \param[in] callback_type Callback type given by an enum
  61. *
  62. * \retval STATUS_OK The function exited successfully
  63. */
  64. enum status_code trng_register_callback(
  65. struct trng_module *const module,
  66. trng_callback_t callback_func,
  67. const enum trng_callback callback_type)
  68. {
  69. /* Sanity check arguments */
  70. Assert(module);
  71. Assert(callback_func);
  72. /* Register callback function */
  73. module->callback[callback_type] = callback_func;
  74. /* Set the bit corresponding to the callback_type */
  75. module->register_callback_mask |= (1 << callback_type);
  76. /* Enable interrupt for this TRNG module */
  77. system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_TRNG);
  78. return STATUS_OK;
  79. }
  80. /**
  81. * \brief Unregisters a callback
  82. *
  83. * Unregisters a callback function implemented by the user. The callback should be
  84. * disabled before it is unregistered.
  85. *
  86. * \param[in] module Pointer to TC software instance struct
  87. * \param[in] callback_type Callback type given by an enum
  88. *
  89. * \retval STATUS_OK The function exited successfully
  90. */
  91. enum status_code trng_unregister_callback(
  92. struct trng_module *const module,
  93. const enum trng_callback callback_type)
  94. {
  95. /* Sanity check arguments */
  96. Assert(module);
  97. /* Unregister callback function */
  98. module->callback[callback_type] = NULL;
  99. /* Clear the bit corresponding to the callback_type */
  100. module->register_callback_mask &= ~(1 << callback_type);
  101. /* Disable interrupt for this TRNG module */
  102. if (module->register_callback_mask == 0) {
  103. system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_TRNG);
  104. }
  105. return STATUS_OK;
  106. }
  107. /**
  108. * \internal ISR handler for TRNG
  109. *
  110. * TRNG interrupt handler for random data ready.
  111. */
  112. void TRNG_Handler(void)
  113. {
  114. /* Temporary variable */
  115. uint8_t interrupt_and_callback_status_mask;
  116. /* Get device instance from the look-up table */
  117. struct trng_module *module = _trng_instance;
  118. /* Read and mask interrupt flag register */
  119. interrupt_and_callback_status_mask = module->hw->INTFLAG.reg &
  120. (module->register_callback_mask & module->enable_callback_mask);
  121. /* Check if data ready needs to be serviced */
  122. if (interrupt_and_callback_status_mask & TRNG_INTFLAG_DATARDY) {
  123. /* Store random result in job buffer (it will clear data ready flag) */
  124. *(module->job_buffer++) = module->hw->DATA.reg;
  125. module->remaining_number -= 1;
  126. if (module->remaining_number == 0) {
  127. if (module->job_status == STATUS_BUSY) {
  128. /* Job is complete. Update status, disable interrupt
  129. * and call callback */
  130. module->job_status = STATUS_OK;
  131. module->hw->INTENCLR.reg = TRNG_INTENCLR_DATARDY;
  132. (module->callback[TRNG_CALLBACK_READ_BUFFER])(module);
  133. }
  134. }
  135. }
  136. /* Clear interrupt flag */
  137. module->hw->INTFLAG.reg = TRNG_INTFLAG_DATARDY;
  138. }
  139. /**
  140. * \brief Read multiple random data from TRNG
  141. *
  142. * As soon as the TRNG is enabled, the module provides a new 32-bits
  143. * random data for every 84 CLK_TRNG_APB clock cycles.
  144. *
  145. * \param[in] module_inst Pointer to the TRNG software instance struct
  146. * \param[in] number Number of random data to get
  147. * \param[out] buffer Buffer to store the random data
  148. *
  149. * \return Status of the job start.
  150. * \retval STATUS_OK The read job was started successfully and is
  151. * in progress
  152. * \retval STATUS_BUSY The TRNG is already busy with another job
  153. */
  154. enum status_code trng_read_buffer_job(
  155. struct trng_module *const module_inst,
  156. uint32_t *buffer,
  157. uint32_t number)
  158. {
  159. Assert(module_inst);
  160. Assert(number);
  161. Assert(buffer);
  162. if (module_inst->remaining_number != 0 ||
  163. module_inst->job_status == STATUS_BUSY) {
  164. return STATUS_BUSY;
  165. }
  166. module_inst->job_status = STATUS_BUSY;
  167. module_inst->remaining_number = number;
  168. module_inst->job_buffer = buffer;
  169. /* Enable data ready interrupt */
  170. module_inst->hw->INTENSET.reg = TRNG_INTENSET_DATARDY;
  171. return STATUS_OK;
  172. }
  173. /**
  174. * \brief Gets the status of a job
  175. *
  176. * Gets the status of an ongoing or the last job.
  177. *
  178. * \param [in] module_inst Pointer to the TRNG software instance struct
  179. * \param [in] type Type of job to abort
  180. *
  181. * \return Status of the job.
  182. */
  183. enum status_code trng_get_job_status(
  184. struct trng_module *module_inst,
  185. enum trng_job_type type)
  186. {
  187. /* Sanity check arguments */
  188. Assert(module_inst);
  189. if (type == TRNG_JOB_READ_BUFFER) {
  190. return module_inst->job_status;
  191. } else {
  192. return STATUS_ERR_INVALID_ARG;
  193. }
  194. }
  195. /**
  196. * \brief Aborts an ongoing job
  197. *
  198. * \param [in] module_inst Pointer to the TRNG software instance struct
  199. * \param [in] type Type of job to abort
  200. */
  201. void trng_abort_job(
  202. struct trng_module *module_inst,
  203. enum trng_job_type type)
  204. {
  205. /* Sanity check arguments */
  206. Assert(module_inst);
  207. if (type == TRNG_JOB_READ_BUFFER) {
  208. /* Disable interrupt */
  209. module_inst->hw->INTENCLR.reg = TRNG_INTENCLR_DATARDY;
  210. /* Mark job as aborted */
  211. module_inst->job_status = STATUS_ABORTED;
  212. module_inst->remaining_number = 0;
  213. }
  214. }