ald_temp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_temp.c
  5. * @brief TEMP module driver.
  6. *
  7. * @version V1.0
  8. * @date 15 Dec 2017
  9. * @author AE Team
  10. * @note
  11. *
  12. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  13. *
  14. *********************************************************************************
  15. */
  16. #include "ald_temp.h"
  17. #include "ald_bkpc.h"
  18. /** @addtogroup ES32FXXX_ALD
  19. * @{
  20. */
  21. /** @defgroup TEMP TEMP
  22. * @brief TEMP module driver
  23. * @{
  24. */
  25. #ifdef ALD_TEMP
  26. /** @defgroup TEMP_Private_Variables TEMP Private Variables
  27. * @{
  28. */
  29. temp_cbk __temp_cbk;
  30. /**
  31. * @}
  32. */
  33. /** @defgroup TEMP_Public_Functions TEMP Public Functions
  34. * @{
  35. */
  36. /** @addtogroup TEMP_Public_Functions_Group1 Initialization functions
  37. * @brief Initialization functions
  38. *
  39. * @verbatim
  40. ==============================================================================
  41. ##### Initialization functions #####
  42. ==============================================================================
  43. [..] This section provides functions allowing to initialize the TEMP:
  44. (+) This parameters can be configured:
  45. (++) Update Cycle
  46. (++) Output Mode
  47. (++) Perscaler
  48. (+) Select TEMP source clock(default LOSC)
  49. @endverbatim
  50. * @{
  51. */
  52. /**
  53. * @brief Initializes the TEMP according to the specified
  54. * parameters in the temp_init_t.
  55. * @param init: Pointer to a temp_init_t structure that contains
  56. * the configuration information.
  57. * @retval None
  58. */
  59. void temp_init(temp_init_t *init)
  60. {
  61. assert_param(IS_TEMP_UPDATE_CYCLE(init->cycle));
  62. assert_param(IS_TEMP_OUTPUT_MODE(init->mode));
  63. TEMP_UNLOCK();
  64. MODIFY_REG(TEMP->CR, TEMP_CR_TSU_MSK, init->cycle << TEMP_CR_TSU_POSS);
  65. MODIFY_REG(TEMP->CR, TEMP_CR_TOM_MSK, init->mode << TEMP_CR_TOM_POSS);
  66. MODIFY_REG(TEMP->CR, TEMP_CR_CTN_MSK, init->ctn << TEMP_CR_CTN_POS);
  67. MODIFY_REG(TEMP->PSR, TEMP_PSR_PRS_MSK, init->psc << TEMP_PSR_PRS_POSS);
  68. TEMP_LOCK();
  69. return;
  70. }
  71. /**
  72. * @brief Configure the TEMP source.
  73. * @param sel: TEMP source type.
  74. * @retval None
  75. */
  76. void temp_source_selcet(temp_source_sel_t sel)
  77. {
  78. assert_param(IS_TEMP_SOURCE_SEL(sel));
  79. BKPC_UNLOCK();
  80. MODIFY_REG(BKPC->PCCR, BKPC_PCCR_TEMPCS_MSK, sel << BKPC_PCCR_TEMPCS_POSS);
  81. if (sel == TEMP_SOURCE_LOSC) {
  82. SET_BIT(BKPC->CR, BKPC_CR_LOSCEN_MSK);
  83. }
  84. else if (sel == TEMP_SOURCE_LRC) {
  85. SET_BIT(BKPC->CR, BKPC_CR_LRCEN_MSK);
  86. }
  87. else {
  88. ; /* do nothing */
  89. }
  90. BKPC_LOCK();
  91. return;
  92. }
  93. /**
  94. * @}
  95. */
  96. /** @addtogroup TEMP_Public_Functions_Group2 Peripheral Control functions
  97. * @brief Peripheral Control functions
  98. *
  99. * @verbatim
  100. ==============================================================================
  101. ##### Peripheral Control functions #####
  102. ==============================================================================
  103. [..] This section provides functions allowing to:
  104. (+) temp_get_value() API can get the current temperature.
  105. (+) temp_get_value_by_it() API can get the current temperature by interrupt.
  106. (+) temp_irq_handle() API can handle the interrupt request.
  107. @endverbatim
  108. * @{
  109. */
  110. /**
  111. * @brief Get the current temperature
  112. * @param temp: The value of current temperature.
  113. * @retval ALD status:
  114. * @arg @ref OK The value is valid
  115. * @arg @ref ERROR The value is invalid
  116. */
  117. ald_status_t temp_get_value(uint16_t *temp)
  118. {
  119. TEMP_UNLOCK();
  120. SET_BIT(TEMP->IFCR, TEMP_IFCR_TEMP_MSK);
  121. SET_BIT(TEMP->CR, TEMP_CR_EN_MSK);
  122. TEMP_LOCK();
  123. while (!(READ_BIT(TEMP->IF, TEMP_IF_TEMP_MSK)));
  124. TEMP_UNLOCK();
  125. SET_BIT(TEMP->IFCR, TEMP_IFCR_TEMP_MSK);
  126. TEMP_LOCK();
  127. if (READ_BIT(TEMP->DR, TEMP_DR_ERR_MSK))
  128. return ERROR;
  129. *temp = READ_BITS(TEMP->DR, TEMP_DR_DATA_MSK, TEMP_DR_DATA_POSS);
  130. return OK;
  131. }
  132. /**
  133. * @brief Get the current temperature by interrupt
  134. * @param cbk: The callback function
  135. * @retval None
  136. */
  137. void temp_get_value_by_it(temp_cbk cbk)
  138. {
  139. __temp_cbk = cbk;
  140. TEMP_UNLOCK();
  141. SET_BIT(TEMP->IFCR, TEMP_IFCR_TEMP_MSK);
  142. SET_BIT(TEMP->IE, TEMP_IE_TEMP_MSK);
  143. SET_BIT(TEMP->CR, TEMP_CR_EN_MSK);
  144. TEMP_LOCK();
  145. return;
  146. }
  147. /**
  148. * @brief This function handles TEMP interrupt request.
  149. * @retval None
  150. */
  151. void temp_irq_handle(void)
  152. {
  153. TEMP_UNLOCK();
  154. SET_BIT(TEMP->IFCR, TEMP_IFCR_TEMP_MSK);
  155. TEMP_LOCK();
  156. if (__temp_cbk == NULL)
  157. return;
  158. if (READ_BIT(TEMP->DR, TEMP_DR_ERR_MSK)) {
  159. __temp_cbk(0, ERROR);
  160. return;
  161. }
  162. __temp_cbk(READ_BITS(TEMP->DR, TEMP_DR_DATA_MSK, TEMP_DR_DATA_POSS), OK);
  163. return;
  164. }
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @}
  170. */
  171. #endif /* ALD_TEMP */
  172. /**
  173. * @}
  174. */
  175. /**
  176. * @}
  177. */