HAL_COMP.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. ******************************************************************************
  3. * @file HAL_COMP.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief COMP HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Comparator Peripheral (COMP).
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /************************************************************************
  16. * function : HAL_COMP_MspInit
  17. * Description: Inition the comparator gpio and clock
  18. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  19. * output : none
  20. ************************************************************************/
  21. __weak void HAL_COMP_MspInit(COMP_HandleTypeDef* hcomp)
  22. {
  23. /*
  24. NOTE : This function should be modified by the user.
  25. */
  26. /* For Example */
  27. GPIO_InitTypeDef GPIO_Handle;
  28. System_Module_Enable(EN_COMP);
  29. if(hcomp->Init.Comparator == COMP1 )
  30. {
  31. /* COMP1 GPIO inition VINP:PA5(INP_0)*/
  32. /* COMP1 GPIO inition VINM:PA4(INM_0)*/
  33. /* COMP1 GPIO inition VOUT:PA0(FUNCTION_7)*/
  34. GPIO_Handle.Pin = GPIO_PIN_4 | GPIO_PIN_5;
  35. GPIO_Handle.Mode = GPIO_MODE_ANALOG;
  36. GPIO_Handle.Pull = GPIO_NOPULL;
  37. HAL_GPIO_Init(GPIOA, &GPIO_Handle);
  38. GPIO_Handle.Pin = GPIO_PIN_0;
  39. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  40. GPIO_Handle.Alternate = GPIO_FUNCTION_7;
  41. GPIO_Handle.Pull = GPIO_NOPULL;
  42. HAL_GPIO_Init(GPIOA, &GPIO_Handle);
  43. }
  44. else if(hcomp->Init.Comparator == COMP2 )
  45. {
  46. /* COMP2 GPIO inition VINP:PB4(INP_1)*/
  47. /* COMP2 GPIO inition VOUT:PA2(FUNCTION_7)*/
  48. GPIO_Handle.Pin = GPIO_PIN_4;
  49. GPIO_Handle.Mode = GPIO_MODE_ANALOG;
  50. GPIO_Handle.Pull = GPIO_NOPULL;
  51. HAL_GPIO_Init(GPIOB, &GPIO_Handle);
  52. GPIO_Handle.Pin = GPIO_PIN_2;
  53. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  54. GPIO_Handle.Alternate = GPIO_FUNCTION_7;
  55. GPIO_Handle.Pull = GPIO_NOPULL;
  56. HAL_GPIO_Init(GPIOA, &GPIO_Handle);
  57. }
  58. }
  59. /************************************************************************
  60. * function : HAL_COMP_MspDeInit
  61. * Description: De-Inition the comparator gpio and clock
  62. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  63. * output : none
  64. ************************************************************************/
  65. __weak void HAL_COMP_MspDeInit(COMP_HandleTypeDef* hcomp)
  66. {
  67. /*
  68. NOTE : This function should be modified by the user.
  69. */
  70. /* For Example */
  71. System_Module_Reset(RST_COMP);
  72. System_Module_Enable(EN_COMP);
  73. }
  74. /************************************************************************
  75. * function : HAL_COMP_Init
  76. * Description: Inition the comparator
  77. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  78. ************************************************************************/
  79. HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef* hcomp)
  80. {
  81. uint32_t u32RegTemp;
  82. __IO uint32_t *gu32RegCrx;
  83. /* Check the COMP handle allocation */
  84. if (hcomp == NULL)
  85. {
  86. return HAL_ERROR;
  87. }
  88. /* Check the parameters */
  89. if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
  90. if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
  91. if(!IS_COMP_ALL_CRV_EN(hcomp->Init.Crv_En)) return HAL_ERROR;
  92. if(!IS_COMP_ALL_CRV_SEL(hcomp->Init.Crv_Sel)) return HAL_ERROR;
  93. if(!IS_COMP_ALL_CRV_CFG(hcomp->Init.Crv_Cfg)) return HAL_ERROR;
  94. if(!IS_COMP_ALL_WINMODE(hcomp->Init.WinMode)) return HAL_ERROR;
  95. if(!IS_COMP_ALL_WINOUT(hcomp->Init.WinOut)) return HAL_ERROR;
  96. if(!IS_COMP_ALL_POLARITY(hcomp->Init.Polarity)) return HAL_ERROR;
  97. if(!IS_COMP_ALL_FLTEN(hcomp->Init.FltEn)) return HAL_ERROR;
  98. if(!IS_COMP_ALL_FLTTIME(hcomp->Init.FltTime)) return HAL_ERROR;
  99. if(!IS_COMP_ALL_BLANKTIME(hcomp->Init.BlankTime)) return HAL_ERROR;
  100. if(!IS_COMP_ALL_BLANKSEL(hcomp->Init.BlankSel)) return HAL_ERROR;
  101. if(!IS_COMP_ALL_INPSEL(hcomp->Init.InPSel)) return HAL_ERROR;
  102. if(!IS_COMP_ALL_INMSEL(hcomp->Init.InMSel)) return HAL_ERROR;
  103. if(!IS_COMP_ALL_HYS(hcomp->Init.HYS)) return HAL_ERROR;
  104. /* Init the low level hardware : GPIO, CLOCK */
  105. HAL_COMP_MspInit(hcomp);
  106. if(hcomp->Init.Comparator == COMP1 )
  107. gu32RegCrx = &hcomp->Instance->CR1;
  108. else
  109. gu32RegCrx = &hcomp->Instance->CR2;
  110. //Check if the register is locked
  111. if(READ_BIT(*gu32RegCrx , COMP_CR_LOCK))
  112. {
  113. System_Module_Reset(RST_COMP);
  114. }
  115. //Check if the comparetor is enable
  116. if(READ_BIT(*gu32RegCrx , COMP_CR_EN))
  117. CLEAR_BIT(*gu32RegCrx , COMP_CR_EN);
  118. u32RegTemp = *gu32RegCrx ;
  119. u32RegTemp = u32RegTemp | ((hcomp->Init.Crv_Cfg << COMP_CR_CRV_CFG_POS)& COMP_CR_CRV_CFG_MASK) | \
  120. ((hcomp->Init.Crv_Sel << 24) & COMP_CR_CRV_SEL) | \
  121. ((hcomp->Init.Crv_En << 23) & COMP_CR_CRV_EN) | \
  122. ((hcomp->Init.WinMode << 22) & COMP_CR_WINMODE) | \
  123. ((hcomp->Init.WinOut << 21) & COMP_CR_WINOUT) | \
  124. ((hcomp->Init.Polarity << 20) & COMP_CR_POLARITY) | \
  125. ((hcomp->Init.FltEn << 19) & COMP_CR_FLTEN) | \
  126. ((hcomp->Init.FltTime << COMP_CR_FLTTIME_POS)& COMP_CR_FLTTIME_MASK) | \
  127. ((hcomp->Init.BlankTime << COMP_CR_BLANKTIME_POS)& COMP_CR_BLANKTIME_MASK) | \
  128. ((hcomp->Init.BlankSel << COMP_CR_BLANKSEL_POS)& COMP_CR_BLANKSEL_MASK) | \
  129. ((hcomp->Init.InPSel << COMP_CR_INPSEL_POS)& COMP_CR_INPSEL_MASK) | \
  130. ((hcomp->Init.InMSel << COMP_CR_INMSEL_POS)& COMP_CR_INMSEL_MASK) | \
  131. ((hcomp->Init.HYS << COMP_CR_HYS_POS)& COMP_CR_HYS_MASK);
  132. //Write the COMP_CR register .
  133. WRITE_REG(*gu32RegCrx,u32RegTemp);
  134. SET_BIT(*gu32RegCrx , COMP_CR_EN); //enable
  135. return HAL_OK;
  136. }
  137. /************************************************************************
  138. * function : HAL_COMP_Enable
  139. * Description: Enable the comparator
  140. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  141. ************************************************************************/
  142. HAL_StatusTypeDef HAL_COMP_Enable(COMP_HandleTypeDef* hcomp)
  143. {
  144. __IO uint32_t *gu32RegCrx;
  145. /* Check the COMP handle allocation */
  146. if (hcomp == NULL)
  147. {
  148. return HAL_ERROR;
  149. }
  150. /* Check the parameters */
  151. if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
  152. if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
  153. if(hcomp->Init.Comparator == COMP1 )
  154. gu32RegCrx = &hcomp->Instance->CR1;
  155. else
  156. gu32RegCrx = &hcomp->Instance->CR2;
  157. SET_BIT(*gu32RegCrx , COMP_CR_EN); //enable
  158. /* Return function status */
  159. return HAL_OK;
  160. }
  161. /************************************************************************
  162. * function : HAL_COMP_DeInit
  163. * Description: Deinit and reset the comparator
  164. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  165. ************************************************************************/
  166. HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef* hcomp)
  167. {
  168. /* Check the COMP handle allocation */
  169. if (hcomp == NULL)
  170. {
  171. return HAL_ERROR;
  172. }
  173. HAL_COMP_MspDeInit(hcomp);
  174. memset(&hcomp->Init, 0, sizeof(hcomp->Init));
  175. /* Return function status */
  176. return HAL_OK;
  177. }
  178. /************************************************************************
  179. * function : HAL_COMP_Disable
  180. * Description: Disable the comparator
  181. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  182. ************************************************************************/
  183. HAL_StatusTypeDef HAL_COMP_Disable(COMP_HandleTypeDef* hcomp)
  184. {
  185. __IO uint32_t *gu32RegCrx;
  186. /* Check the COMP handle allocation */
  187. if (hcomp == NULL)
  188. {
  189. return HAL_ERROR;
  190. }
  191. /* Check the parameters */
  192. if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
  193. if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
  194. if(hcomp->Init.Comparator == COMP1 )
  195. gu32RegCrx = &hcomp->Instance->CR1;
  196. else
  197. gu32RegCrx = &hcomp->Instance->CR2;
  198. CLEAR_BIT(*gu32RegCrx , COMP_CR_EN); //disable
  199. /* Return function status */
  200. return HAL_OK;
  201. }
  202. /************************************************************************
  203. * function : HAL_COMP_GetOutputLevel
  204. * Description: Get the output level of the comparator
  205. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  206. ************************************************************************/
  207. HAL_StatusTypeDef HAL_COMP_GetOutputLevel(COMP_HandleTypeDef* hcomp)
  208. {
  209. uint32_t u32RegTemp;
  210. /* Check the parameters */
  211. if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
  212. if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
  213. u32RegTemp = READ_REG(hcomp->Instance->SR);
  214. if(hcomp->Init.Comparator == COMP1 )
  215. {
  216. hcomp->OutputLevel_Org = (u32RegTemp & COMP_SR_VCOUT1_ORG)? 1:0;
  217. hcomp->OutputLevel = (u32RegTemp & COMP_SR_VCOUT1)? 1:0;
  218. }
  219. else
  220. {
  221. hcomp->OutputLevel_Org = (u32RegTemp & COMP_SR_VCOUT2_ORG)? 1:0;
  222. hcomp->OutputLevel = (u32RegTemp & COMP_SR_VCOUT2)? 1:0;
  223. }
  224. /* Return function status */
  225. return HAL_OK;
  226. }
  227. /************************************************************************
  228. * function : HAL_COMP_Lock
  229. * Description: Lock the comparator
  230. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  231. ************************************************************************/
  232. HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef* hcomp)
  233. {
  234. __IO uint32_t *gu32RegCrx;
  235. /* Check the COMP handle allocation */
  236. if (hcomp == NULL)
  237. {
  238. return HAL_ERROR;
  239. }
  240. /* Check the parameters */
  241. if(!IS_COMP_ALL_INSTANCE(hcomp->Instance)) return HAL_ERROR;
  242. if(!IS_COMP_ALL_COMP(hcomp->Init.Comparator)) return HAL_ERROR;
  243. if(hcomp->Init.Comparator == COMP1 )
  244. gu32RegCrx = &hcomp->Instance->CR1;
  245. else
  246. gu32RegCrx = &hcomp->Instance->CR2;
  247. SET_BIT(*gu32RegCrx , COMP_CR_LOCK); //lock
  248. /* Return function status */
  249. return HAL_OK;
  250. }
  251. /************************************************************************
  252. * function : HAL_COMP_Start
  253. * Description: Start the comparator
  254. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  255. ************************************************************************/
  256. HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp)
  257. {
  258. return(HAL_COMP_Enable(hcomp));
  259. }
  260. /************************************************************************
  261. * function : HAL_COMP_Stop
  262. * Description: Stop the comparator
  263. * input : COMP_HandleTypeDef* hcomp: pointer to comparator structure.
  264. ************************************************************************/
  265. HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp)
  266. {
  267. return(HAL_COMP_Disable(hcomp));
  268. }