hal_comp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_comp.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE COMP FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_COMP_C_
  19. // Files includes
  20. #include "hal_comp.h"
  21. ////////////////////////////////////////////////////////////////////////////////
  22. /// @addtogroup MM32_Hardware_Abstract_Layer
  23. /// @{
  24. ////////////////////////////////////////////////////////////////////////////////
  25. /// @addtogroup COMP_HAL
  26. /// @{
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// @addtogroup COMP_Exported_Functions
  29. /// @{
  30. ////////////////////////////////////////////////////////////////////////////////
  31. /// @brief Deinitializes COMP peripheral registers to their default reset
  32. /// values.
  33. /// @param selection: the selected comparator.
  34. /// select the COMP peripheral.
  35. /// @retval None.
  36. ////////////////////////////////////////////////////////////////////////////////
  37. void COMP_DeInit(COMP_Selection_TypeDef selection)
  38. {
  39. *(vu32*)(COMP_BASE + selection) = 0;
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. /// @brief Initializes the COMP peripheral according to the specified
  43. /// @param selection: the selected comparator.
  44. /// select the COMP peripheral.
  45. /// @param init_struct: pointer to an COMP_InitTypeDef structure that
  46. /// contains the configuration information for the specified COMP
  47. /// peripheral.
  48. /// - COMP_InvertingInput specifies the inverting input of COMP
  49. /// - COMP_NonInvertingInput specifies the non inverting input of COMP
  50. /// - COMP_Output connect COMP output to selected timer
  51. /// input (Input capture / Output Compare Reference Clear / Break
  52. /// Input)
  53. /// - COMP_BlankingSrce specifies the blanking source of COMP
  54. /// - COMP_OutputPol select output polarity
  55. /// - COMP_Hysteresis configures COMP hysteresis value
  56. /// - COMP_Mode configures COMP power mode
  57. /// @retval None.
  58. ////////////////////////////////////////////////////////////////////////////////
  59. void COMP_Init(COMP_Selection_TypeDef selection, COMP_InitTypeDef* init_struct)
  60. {
  61. *(vu32*)(COMP_BASE + selection) = init_struct->Invert |
  62. init_struct->NonInvert |
  63. init_struct->Output |
  64. init_struct->OutputPol |
  65. init_struct->BlankingSrce |
  66. init_struct->Hysteresis |
  67. init_struct->Mode |
  68. init_struct->OFLT;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////
  71. /// @brief Fills each init_struct member with its default value.
  72. /// @param init_struct: pointer to an COMP_InitTypeDef structure which will
  73. /// be initialized.
  74. /// @retval None.
  75. ////////////////////////////////////////////////////////////////////////////////
  76. void COMP_StructInit(COMP_InitTypeDef* init_struct)
  77. {
  78. init_struct->Invert = COMP_InvertingInput_IO1;
  79. init_struct->NonInvert = COMP_NonInvertingInput_IO1;
  80. init_struct->Output = COMP_Output_None;
  81. init_struct->BlankingSrce = COMP_BlankingSrce_None;
  82. init_struct->OutputPol = COMP_NonInverted;
  83. init_struct->Hysteresis = COMP_Hysteresis_No;
  84. init_struct->Mode = COMP_Mode_UltraLowPower;
  85. init_struct->OFLT = COMP_Filter_4_Period; ///< to adjust the speed/consumption.
  86. }
  87. ////////////////////////////////////////////////////////////////////////////////
  88. /// @brief Enable or disable the COMP peripheral.
  89. /// @param selection: the selected comparator.
  90. /// select the COMP peripheral.
  91. /// @param NewState: new state of the COMP peripheral.
  92. /// This parameter can be: ENABLE or DISABLE.
  93. /// When enabled, the comparator compares the non inverting input with
  94. /// the inverting input and the comparison result is available on
  95. /// comparator output.
  96. /// When disabled, the comparator doesn't perform comparison and the
  97. /// output level is low.
  98. /// @retval None.
  99. ////////////////////////////////////////////////////////////////////////////////
  100. void COMP_Cmd(COMP_Selection_TypeDef selection, FunctionalState state)
  101. {
  102. (state) ? (*(vu32*)(COMP_BASE + selection) |= COMP_CSR_EN) :
  103. (*(vu32*)(COMP_BASE + selection) &= ~COMP_CSR_EN);
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////
  106. /// @brief Select CRV param.
  107. /// @param crv_select: Select source for CRV.
  108. /// @param crv_level: Set level for CRV.
  109. /// @retval None.
  110. ////////////////////////////////////////////////////////////////////////////////
  111. void COMP_SetCrv(u8 crv_select, u8 crv_level)
  112. {
  113. u32 temreg = 0;
  114. temreg = COMP->CRV;
  115. temreg &= ~COMP_CRV_MASK;
  116. // Load config to CRV and enable
  117. temreg |= crv_select | crv_level | (1 << 4);
  118. COMP->CRV = temreg;
  119. }
  120. ////////////////////////////////////////////////////////////////////////////////
  121. /// @brief Close or Open the SW1 switch.
  122. /// @param selection: the selected comparator.
  123. /// select the COMP peripheral.
  124. /// @param state: new state of the COMP peripheral.
  125. /// This parameter can be: ENABLE or DISABLE.
  126. /// When enabled, the comparator compares the non inverting input with
  127. /// the inverting input and the comparison result is available on
  128. /// comparator output.
  129. /// When disabled, the comparator doesn't perform comparison and the
  130. /// output level is low.
  131. /// @retval None.
  132. ////////////////////////////////////////////////////////////////////////////////
  133. void COMP_SwitchCmd(COMP_Selection_TypeDef selection, FunctionalState state)
  134. {
  135. (state) ?
  136. (*(vu32*)(COMP_BASE + selection) |= COMP_CSR_COMPSW1) :
  137. (*(vu32*)(COMP_BASE + selection) &= ~COMP_CSR_COMPSW1);
  138. }
  139. ////////////////////////////////////////////////////////////////////////////////
  140. /// @brief Return the output level (high or low) of the selected comparator.
  141. /// The output level depends on the selected polarity.
  142. /// If the polarity is not inverted:
  143. /// - Comparator output is low when the non-inverting input is at a
  144. /// lower voltage than the inverting input
  145. /// - Comparator output is high when the non-inverting input is at a
  146. /// higher voltage than the inverting input
  147. /// If the polarity is inverted:
  148. /// - Comparator output is high when the non-inverting input is at a
  149. /// lower voltage than the inverting input
  150. /// - Comparator output is low when the non-inverting input is at a
  151. /// higher voltage than the inverting input
  152. /// @param comp: the selected comparator.
  153. /// select the COMP peripheral.
  154. /// @retval The selected comparator output level: low or high.
  155. ////////////////////////////////////////////////////////////////////////////////
  156. u32 COMP_GetOutputLevel(COMP_Selection_TypeDef selection)
  157. {
  158. return (((*(vu32*)(COMP_BASE + selection) & COMP_CSR_STA) != 0) ?
  159. COMP_OutputLevel_High :
  160. COMP_OutputLevel_Low );
  161. }
  162. ////////////////////////////////////////////////////////////////////////////////
  163. /// @brief Lock the selected comparator (COMP1/COMP2) configuration.
  164. /// @param selection: the selected comparator.
  165. /// select the COMP peripheral.
  166. /// @retval None.
  167. ////////////////////////////////////////////////////////////////////////////////
  168. void COMP_LockConfig(COMP_Selection_TypeDef selection)
  169. {
  170. *(vu32*)(COMP_BASE + selection) |= COMP_CSR_LOCK;
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////
  173. /// @brief Enable or disable the COMP register.
  174. /// @param state: new state of the COMP peripheral.
  175. /// This parameter can be: ENABLE or DISABLE.
  176. /// @retval None.
  177. ////////////////////////////////////////////////////////////////////////////////
  178. void exCOMP_CrvCmd(FunctionalState state)
  179. {
  180. (state) ? (COMP->CRV |= COMP_CRV_EN_ENABLE) : (COMP->CRV &= ~COMP_CRV_EN_ENABLE);
  181. }
  182. ////////////////////////////////////////////////////////////////////////////////
  183. /// @brief Select comparator external reference voltage.
  184. /// @param selection: the selected external reference voltage.
  185. /// @retval None.
  186. ////////////////////////////////////////////////////////////////////////////////
  187. void exCOMP_SwitchCrv(u32 crv)
  188. {
  189. COMP->CRV |= crv;
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////
  192. /// @brief Select comparator external reference voltage source.
  193. /// @param selection: the selected external reference voltage source.
  194. /// This parameter can be: COMP_CRV_SRC_AVDD or COMP_CRV_SRC_VREF.
  195. /// @retval None.
  196. ////////////////////////////////////////////////////////////////////////////////
  197. void exCOMP_CrvSrc(u32 src)
  198. {
  199. COMP->CRV |= src;
  200. }
  201. /// @}
  202. /// @}
  203. /// @}