1
0

HAL_COMP.c 12 KB

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