n32g45x_comp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_comp.c
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_comp.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup COMP
  40. * @brief COMP driver modules
  41. * @{
  42. */
  43. /** @addtogroup COMP_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup COMP_Private_Defines
  50. * @{
  51. */
  52. /**
  53. * @}
  54. */
  55. /** @addtogroup COMP_Private_Macros
  56. * @{
  57. */
  58. /**
  59. * @}
  60. */
  61. /** @addtogroup COMP_Private_Variables
  62. * @{
  63. */
  64. /**
  65. * @}
  66. */
  67. /** @addtogroup COMP_Private_FunctionPrototypes
  68. * @{
  69. */
  70. /**
  71. * @}
  72. */
  73. /** @addtogroup COMP_Private_Functions
  74. * @{
  75. */
  76. #define SetBitMsk(reg, bit, msk) ((reg) = (((reg) & ~(msk)) | (bit)))
  77. #define ClrBit(reg, bit) ((reg) &= ~(bit))
  78. #define SetBit(reg, bit) ((reg) |= (bit))
  79. #define GetBit(reg, bit) ((reg) & (bit))
  80. /**
  81. * @brief Deinitializes the COMP peripheral registers to their default reset values.
  82. */
  83. void COMP_DeInit(void)
  84. {
  85. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_COMP, ENABLE);
  86. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_COMP, DISABLE);
  87. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_COMP_FILT, ENABLE);
  88. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_COMP_FILT, DISABLE);
  89. }
  90. void COMP_StructInit(COMP_InitType* COMP_InitStruct)
  91. {
  92. COMP_InitStruct->InpDacConnect = false; // only COMP1 have this bit
  93. COMP_InitStruct->Blking = COMP_CTRL_BLKING_NO; /*see @ref COMP_CTRL_BLKING */
  94. COMP_InitStruct->Hyst = COMP_CTRL_HYST_NO; // see @COMPx_CTRL_HYST_MASK
  95. COMP_InitStruct->PolRev = false; // out polarity reverse
  96. COMP_InitStruct->OutSel = COMPX_CTRL_OUTSEL_NC;
  97. COMP_InitStruct->InpSel = COMPX_CTRL_INPSEL_RES;
  98. COMP_InitStruct->InmSel = COMPX_CTRL_INMSEL_RES;
  99. COMP_InitStruct->FilterEn=false;
  100. COMP_InitStruct->ClkPsc=0;
  101. COMP_InitStruct->SampWindow=0;
  102. COMP_InitStruct->Thresh=0;
  103. COMP_InitStruct->En = false;
  104. }
  105. void COMP_Init(COMPX COMPx, COMP_InitType* COMP_InitStruct)
  106. {
  107. COMP_SingleType* pCS = &COMP->Cmp[COMPx];
  108. __IO uint32_t tmp;
  109. // filter
  110. tmp = pCS->FILC;
  111. SetBitMsk(tmp, COMP_InitStruct->SampWindow << 6, COMP_FILC_SAMPW_MASK);
  112. SetBitMsk(tmp, COMP_InitStruct->Thresh << 1, COMP_FILC_THRESH_MASK);
  113. SetBitMsk(tmp, COMP_InitStruct->FilterEn << 0, COMP_FILC_FILEN_MASK);
  114. pCS->FILC = tmp;
  115. // filter psc
  116. pCS->FILP = COMP_InitStruct->ClkPsc;
  117. // ctrl
  118. tmp = pCS->CTRL;
  119. if (COMPx == COMP1)
  120. {
  121. if (COMP_InitStruct->InpDacConnect)
  122. SetBit(tmp, COMP1_CTRL_INPDAC_MASK);
  123. else
  124. ClrBit(tmp, COMP1_CTRL_INPDAC_MASK);
  125. }
  126. SetBitMsk(tmp, COMP_InitStruct->Blking, COMP_CTRL_BLKING_MASK);
  127. SetBitMsk(tmp, COMP_InitStruct->Hyst, COMPx_CTRL_HYST_MASK);
  128. if (COMP_InitStruct->PolRev)
  129. SetBit(tmp, COMP_POL_MASK);
  130. else
  131. ClrBit(tmp, COMP_POL_MASK);
  132. SetBitMsk(tmp, COMP_InitStruct->OutSel, COMP_CTRL_OUTSEL_MASK);
  133. SetBitMsk(tmp, COMP_InitStruct->InpSel, COMP_CTRL_INPSEL_MASK);
  134. SetBitMsk(tmp, COMP_InitStruct->InmSel, COMP_CTRL_INMSEL_MASK);
  135. if (COMP_InitStruct->En)
  136. SetBit(tmp, COMP_CTRL_EN_MASK);
  137. else
  138. ClrBit(tmp, COMP_CTRL_EN_MASK);
  139. pCS->CTRL = tmp;
  140. }
  141. void COMP_Enable(COMPX COMPx, FunctionalState en)
  142. {
  143. if (en)
  144. SetBit(COMP->Cmp[COMPx].CTRL, COMP_CTRL_EN_MASK);
  145. else
  146. ClrBit(COMP->Cmp[COMPx].CTRL, COMP_CTRL_EN_MASK);
  147. }
  148. void COMP_SetInpSel(COMPX COMPx, COMP_CTRL_INPSEL VpSel)
  149. {
  150. __IO uint32_t tmp = COMP->Cmp[COMPx].CTRL;
  151. SetBitMsk(tmp, VpSel, COMP_CTRL_INPSEL_MASK);
  152. COMP->Cmp[COMPx].CTRL = tmp;
  153. }
  154. void COMP_SetInmSel(COMPX COMPx, COMP_CTRL_INMSEL VmSel)
  155. {
  156. __IO uint32_t tmp = COMP->Cmp[COMPx].CTRL;
  157. SetBitMsk(tmp, VmSel, COMP_CTRL_INMSEL_MASK);
  158. COMP->Cmp[COMPx].CTRL = tmp;
  159. }
  160. void COMP_SetOutTrig(COMPX COMPx, COMP_CTRL_OUTTRIG OutTrig)
  161. {
  162. __IO uint32_t tmp = COMP->Cmp[COMPx].CTRL;
  163. SetBitMsk(tmp, OutTrig, COMP_CTRL_OUTSEL_MASK);
  164. COMP->Cmp[COMPx].CTRL = tmp;
  165. }
  166. // Lock see @COMP_LOCK
  167. void COMP_SetLock(uint32_t Lock)
  168. {
  169. COMP->LOCK = Lock;
  170. }
  171. // IntEn see @COMP_INTEN_CMPIEN
  172. void COMP_SetIntEn(uint32_t IntEn)
  173. {
  174. COMP->INTEN = IntEn;
  175. }
  176. // return see @COMP_INTSTS_CMPIS
  177. uint32_t COMP_GetIntSts(void)
  178. {
  179. return COMP->INTSTS;
  180. }
  181. // parma range see @COMP_VREFSCL
  182. // Vv2Trim,Vv1Trim max 63
  183. void COMP_SetRefScl(uint8_t Vv2Trim, bool Vv2En, uint8_t Vv1Trim, bool Vv1En)
  184. {
  185. __IO uint32_t tmp = 0;
  186. SetBitMsk(tmp, Vv2Trim << 8, COMP_VREFSCL_VV2TRM_MSK);
  187. SetBitMsk(tmp, Vv2En << 7, COMP_VREFSCL_VV2EN_MSK);
  188. SetBitMsk(tmp, Vv1Trim << 1, COMP_VREFSCL_VV1TRM_MSK);
  189. SetBitMsk(tmp, Vv1En << 0, COMP_VREFSCL_VV1EN_MSK);
  190. COMP->VREFSCL = tmp;
  191. }
  192. // SET when comp out 1
  193. // RESET when comp out 0
  194. FlagStatus COMP_GetOutStatus(COMPX COMPx)
  195. {
  196. return (COMP->Cmp[COMPx].CTRL & COMP_CTRL_OUT_MASK) ? SET : RESET;
  197. }
  198. // get one comp interrupt flags
  199. FlagStatus COMP_GetIntStsOneComp(COMPX COMPx)
  200. {
  201. return (COMP_GetIntSts() & (0x01 << COMPx)) ? SET : RESET;
  202. }
  203. /**
  204. * @brief Set the COMP filter clock Prescaler value.
  205. * @param COMPx where x can be 1 to 7 to select the COMP peripheral.
  206. * @param FilPreVal Prescaler Value,Div clock = FilPreVal+1.
  207. * @return void
  208. */
  209. void COMP_SetFilterPrescaler(COMPX COMPx , uint16_t FilPreVal)
  210. {
  211. COMP->Cmp[COMPx].FILP=FilPreVal;
  212. }
  213. /**
  214. * @brief Set the COMP filter control value.
  215. * @param COMPx where x can be 1 to 7 to select the COMP peripheral.
  216. * @param FilEn 1 for enable ,0 or disable
  217. * @param TheresNum num under this value is noise
  218. * @param SampPW total sample number in a window
  219. * @return void
  220. */
  221. void COMP_SetFilterControl(COMPX COMPx , uint8_t FilEn, uint8_t TheresNum , uint8_t SampPW)
  222. {
  223. COMP->Cmp[COMPx].FILC=(FilEn&COMP_FILC_FILEN_MASK)+((TheresNum<<1)&COMP_FILC_THRESH_MASK)+((SampPW<<6)&COMP_FILC_SAMPW_MASK);
  224. }
  225. /**
  226. * @brief Set the COMP Hyst value.
  227. * @param COMPx where x can be 1 to 7 to select the COMP peripheral.
  228. * @param HYST specifies the HYST level.
  229. * This parameter can be one of the following values:
  230. * @arg COMP_CTRL_HYST_NO Hyst disable
  231. * @arg COMP_CTRL_HYST_LOW Hyst level 5.1mV
  232. * @arg COMP_CTRL_HYST_MID Hyst level 15mV
  233. * @arg COMP_CTRL_HYST_HIGH Hyst level 25mV
  234. * @return void
  235. */
  236. void COMP_SetHyst(COMPX COMPx , COMP_CTRL_HYST HYST)
  237. {
  238. uint32_t tmp=COMP->Cmp[COMPx].CTRL;
  239. tmp&=~COMP_CTRL_HYST_HIGH;
  240. tmp|=HYST;
  241. COMP->Cmp[COMPx].CTRL=tmp;
  242. }
  243. /**
  244. * @brief Set the COMP Blanking source .
  245. * @param COMPx where x can be 1 to 7 to select the COMP peripheral.
  246. * @param BLK specifies the blanking source .
  247. * This parameter can be one of the following values:
  248. * @arg COMP_CTRL_BLKING_NO Blanking disable
  249. * @arg COMP_CTRL_BLKING_TIM1_OC5 Blanking source TIM1_OC5
  250. * @arg COMP_CTRL_BLKING_TIM8_OC5 Blanking source TIM8_OC5
  251. * @return void
  252. */
  253. void COMP_SetBlanking(COMPX COMPx , COMP_CTRL_BLKING BLK)
  254. {
  255. uint32_t tmp=COMP->Cmp[COMPx].CTRL;
  256. tmp&=~(7<<14);
  257. tmp|=BLK;
  258. COMP->Cmp[COMPx].CTRL=tmp;
  259. }
  260. /**
  261. * @}
  262. */
  263. /**
  264. * @}
  265. */
  266. /**
  267. * @}
  268. */