acmp.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /******************************************************************************
  2. * @brief header file for ACMP utilities.
  3. *
  4. *******************************************************************************
  5. *
  6. * provide APIs for accessing ACMP
  7. ******************************************************************************/
  8. #ifndef _MY_ACMP_H_
  9. #define _MY_ACMP_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /******************************************************************************
  14. * Includes
  15. ******************************************************************************/
  16. /******************************************************************************
  17. * Constants
  18. ******************************************************************************/
  19. /* DAC reference select */
  20. enum
  21. {
  22. DAC_REF_BANDGAP = 0,
  23. DAC_REF_VDDA
  24. };
  25. /******************************************************************************
  26. * Macros
  27. ******************************************************************************/
  28. /******************************************************************************
  29. * ACMP module number definition *
  30. ******************************************************************************/
  31. #define MAX_ACMP_NO 2
  32. /******************************************************************************
  33. * ACMP positive and negative pin select definition
  34. *
  35. *//*! @addtogroup acmp_pinsel_list
  36. * @{
  37. *******************************************************************************/
  38. #define ACMP_INPUT_P_EXT0 (0<<4) /*!< positive pin select external pin 0 */
  39. #define ACMP_INPUT_P_EXT1 (1<<4) /*!< positive pin select external pin 1 */
  40. #define ACMP_INPUT_P_EXT2 (2<<4) /*!< positive pin select external pin 2 */
  41. #define ACMP_INPUT_P_DAC (3<<4) /*!< positive pin select internal DAC */
  42. #define ACMP_INPUT_N_EXT0 0 /*!< positive pin select external pin 0 */
  43. #define ACMP_INPUT_N_EXT1 1 /*!< positive pin select external pin 1 */
  44. #define ACMP_INPUT_N_EXT2 2 /*!< positive pin select external pin 2 */
  45. #define ACMP_INPUT_N_DAC 3 /*!< positive pin select internal DAC */
  46. /*! @} End of acmp_pinsel_list */
  47. /******************************************************************************
  48. * ACMP interrupt sensitivity edge definition
  49. *
  50. *//*! @addtogroup acmp_intedgesel
  51. * @{
  52. *******************************************************************************/
  53. #define ACMP_SENSITIVITYMODE_FALLING 0 /*!< interrupt on falling edge */
  54. #define ACMP_SENSITIVITYMODE_RISING 1 /*!< interrupt on rising edge */
  55. #define ACMP_SENSITIVITYMODE_ANY 3 /*!< interrupt on falling or rising edge */
  56. /*! @} End of acmp_intedgesel */
  57. /******************************************************************************
  58. * ACMP hysterisis selection definition
  59. *
  60. *//*! @addtogroup acmp_hyst
  61. * @{
  62. *******************************************************************************/
  63. #define ACMP_HYST_20MV (0<<6) /*!< 20mv hyst */
  64. #define ACMP_HYST_30MV (1<<6) /*!< 30mv hyst */
  65. /*! @} End of acmp_hyst */
  66. /******************************************************************************
  67. * ACMP internal DAC reference selection definition
  68. *
  69. *//*! @addtogroup acmp_dacref
  70. * @{
  71. *******************************************************************************/
  72. #define ACMP_DAC_REFERENCE_BANDGAP (0<<6) /*!< select bandgap as refference */
  73. #define ACMP_DAC_REFERENCE_VDDA (1<<6) /*!< select VDDA as refference */
  74. /*! @} End of acmp_dacref */
  75. /******************************************************************************
  76. * Types
  77. ******************************************************************************/
  78. /*! @brief ACMP_CALLBACK function declaration */
  79. typedef void (*ACMP_CallbackPtr)(void);
  80. /*! @} End of acmp_callback */
  81. /******************************************************************************
  82. * ACMP control status struct
  83. *
  84. *//*! @addtogroup acmp_ctrlstatusstruct
  85. * @{
  86. *******************************************************************************/
  87. /*!
  88. * @brief ACMP control and status fields type.
  89. *
  90. */
  91. typedef union
  92. {
  93. uint8_t byte; /*!< byte field of union type */
  94. struct
  95. {
  96. uint8_t bMod : 2; /*!< Sensitivity modes of the interrupt trigger */
  97. uint8_t bOutEn : 1; /*!< Output can be placed onto an external pin */
  98. uint8_t bOutState : 1; /*!< The current value of the analog comparator output */
  99. uint8_t bIntEn : 1; /*!< ACMP interrupt enable */
  100. uint8_t bIntFlag : 1; /*!< ACMP Interrupt Flag Bit */
  101. uint8_t bHyst : 1; /*!< Selects ACMP hystersis */
  102. uint8_t bEn : 1; /*!< Enables the ACMP module */
  103. }bits; /*!< bitfield of union type */
  104. }ACMP_CtrlStatusType, *ACMP_CtrlStatusPtr; /*!< ACMP Control/Status reg structure */
  105. /*! @} End of acmp_ctrlstatusstruct */
  106. /******************************************************************************
  107. * ACMP pin select struct
  108. *
  109. *//*! @addtogroup acmp_pinselectstruct
  110. * @{
  111. *******************************************************************************/
  112. /*!
  113. * @brief ACMP external pins control struct.
  114. *
  115. */
  116. typedef union
  117. {
  118. uint8_t byte; /*!< byte field of union type */
  119. struct
  120. {
  121. uint8_t bNegPin : 2; /*!< Negative pin select */
  122. uint8_t : 2;
  123. uint8_t bPosPin : 2; /*!< Positive pin select */
  124. uint8_t : 2;
  125. }bits; /*!< bitfield of union type */
  126. }ACMP_PinSelType, *ACMP_PinSelPtr; /*!< ACMP Pin select structure */
  127. /*! @} End of acmp_pinselectstruct */
  128. /******************************************************************************
  129. * ACMP DAC control struct
  130. *
  131. *//*! @addtogroup acmp_dacctrlstruct
  132. * @{
  133. *******************************************************************************/
  134. /*!
  135. * @brief ACMP internal ADC control struct.
  136. *
  137. */
  138. typedef union
  139. {
  140. uint8_t byte; /*!< byte field of union type */
  141. struct
  142. {
  143. uint8_t bVal : 6; /*!< 6 bit DAC value */
  144. uint8_t bRef : 1; /*!< 6 bit DAC reference select */
  145. uint8_t bEn : 1; /*!< 6 bit DAC enable bit */
  146. }bits; /*!< bitfield of union type */
  147. }ACMP_DACType, *ACMP_DACPtr; /*!< ACMP DAC control structure */
  148. /*! @} End of acmp_dacctrlstruct */
  149. /******************************************************************************
  150. * ACMP pin enable union
  151. *
  152. *//*! @addtogroup acmp_pinenunion
  153. * @{
  154. *******************************************************************************/
  155. /*!
  156. * @brief ACMP external input pin enable control struct.
  157. *
  158. */
  159. typedef union
  160. {
  161. uint8_t byte; /*!< byte field of union type */
  162. struct
  163. {
  164. uint8_t bEn : 3; /*!< ACMP external input pin enable */
  165. uint8_t bRsvd : 5;
  166. }bits; /*!< bitfield of union type */
  167. }ACMP_PinEnType, *ACMP_PinEnPtr; /*!< ACMP Pin enable structure */
  168. /*! @} End of acmp_pinenunion */
  169. /******************************************************************************
  170. * ACMP config struct
  171. *
  172. *//*! @addtogroup acmp_configstruct
  173. * @{
  174. *******************************************************************************/
  175. /*!
  176. * @brief ACMP module configuration struct.
  177. *
  178. */
  179. typedef struct
  180. {
  181. ACMP_CtrlStatusType sCtrlStatus; /*!< ACMP control and status */
  182. ACMP_PinSelType sPinSelect; /*!< ACMP pin select */
  183. ACMP_DACType sDacSet; /*!< ACMP internal dac set */
  184. ACMP_PinEnType sPinEnable; /*!< ACMP external pin control */
  185. }ACMP_ConfigType, *ACMP_ConfigPtr;
  186. /*! @} End of acmp_configstruct */
  187. /******************************************************************************
  188. * Global variables
  189. ******************************************************************************/
  190. /*!
  191. * inline functions
  192. */
  193. /******************************************************************************
  194. * ACMP api list.
  195. *
  196. *//*! @addtogroup acmp_api_list
  197. * @{
  198. *******************************************************************************/
  199. /*****************************************************************************//*!
  200. *
  201. * @brief enable the acmp module.
  202. *
  203. * @param[in] pACMPx pointer to an ACMP module.
  204. *
  205. * @return none.
  206. *
  207. * @ Pass/ Fail criteria: none.
  208. *
  209. * @see ACMP_Disable.
  210. *
  211. *****************************************************************************/
  212. __STATIC_INLINE void ACMP_Enable(ACMP_Type *pACMPx)
  213. {
  214. pACMPx->CS |= ACMP_CS_ACE_MASK;
  215. }
  216. /*****************************************************************************//*!
  217. *
  218. * @brief disable the acmp module.
  219. *
  220. * @param[in] pACMPx pointer to an ACMP module.
  221. *
  222. * @return none.
  223. *
  224. * @ Pass/ Fail criteria: none.
  225. *
  226. * @see ACMP_Enable.
  227. *
  228. *****************************************************************************/
  229. __STATIC_INLINE void ACMP_Disable(ACMP_Type *pACMPx)
  230. {
  231. pACMPx->CS &= ~ACMP_CS_ACE_MASK;
  232. }
  233. /*****************************************************************************//*!
  234. *
  235. * @brief select sensitivity modes of the interrupt trigger.
  236. *
  237. * @param[in] pACMPx pointer to an ACMP module.
  238. * @param[in] u8EdgeSelect falling or rising selction, 0~3.
  239. *
  240. * @return none.
  241. *
  242. * @ Pass/ Fail criteria: none.
  243. *
  244. *****************************************************************************/
  245. __STATIC_INLINE void ACMP_SelectIntMode(ACMP_Type *pACMPx, uint8_t u8EdgeSelect)
  246. {
  247. pACMPx->CS &= ~ACMP_CS_ACMOD_MASK;
  248. pACMPx->CS |= ACMP_CS_ACMOD(u8EdgeSelect & 0x3);
  249. }
  250. /*****************************************************************************//*!
  251. *
  252. * @brief enable the ACMP module analog comparator output.
  253. *
  254. * @param[in] pACMPx pointer to an ACMP module.
  255. *
  256. * @return none.
  257. *
  258. * @ Pass/ Fail criteria: none.
  259. *
  260. * @see ACMP_DisablePinOut.
  261. *
  262. *****************************************************************************/
  263. __STATIC_INLINE void ACMP_EnablePinOut(ACMP_Type *pACMPx)
  264. {
  265. pACMPx->CS |= ACMP_CS_ACOPE_MASK;
  266. }
  267. /*****************************************************************************//*!
  268. *
  269. * @brief disable the ACMP module analog comparator output.
  270. *
  271. * @param[in] pACMPx pointer to an ACMP module.
  272. *
  273. * @return none.
  274. *
  275. * @ Pass/ Fail criteria: none.
  276. *
  277. * @see ACMP_EnablePinOut.
  278. *
  279. *****************************************************************************/
  280. __STATIC_INLINE void ACMP_DisablePinOut(ACMP_Type *pACMPx)
  281. {
  282. pACMPx->CS &= ~ACMP_CS_ACOPE_MASK;
  283. }
  284. /*****************************************************************************//*!
  285. *
  286. * @brief select ACMP hystersis.
  287. *
  288. * @param[in] pACMPx pointer to an ACMP module.
  289. * @param[in] u8HystSelect ACMP_HYST_20MV or ACMP_HYST_30MV.
  290. *
  291. * @return none.
  292. *
  293. * @ Pass/ Fail criteria: none.
  294. *
  295. *****************************************************************************/
  296. __STATIC_INLINE void ACMP_SelectHyst(ACMP_Type *pACMPx, uint8_t u8HystSelect)
  297. {
  298. pACMPx->CS &= ~ACMP_CS_HYST_MASK;
  299. pACMPx->CS |= u8HystSelect;
  300. }
  301. /*****************************************************************************//*!
  302. *
  303. * @brief enable the acmp module interrupt.
  304. *
  305. * @param[in] pACMPx pointer to an ACMP module.
  306. *
  307. * @return none.
  308. *
  309. * @ Pass/ Fail criteria: none.
  310. *
  311. * @see ACMP_DisableInterrupt.
  312. *
  313. *****************************************************************************/
  314. __STATIC_INLINE void ACMP_EnableInterrupt(ACMP_Type *pACMPx)
  315. {
  316. pACMPx->CS |= ACMP_CS_ACIE_MASK;
  317. }
  318. /*****************************************************************************//*!
  319. *
  320. * @brief disable the acmp module interrupt.
  321. *
  322. * @param[in] pACMPx pointer to an ACMP module.
  323. *
  324. * @return none.
  325. *
  326. * @ Pass/ Fail criteria: none.
  327. *
  328. * @see ACMP_EnableInterrupt.
  329. *
  330. *****************************************************************************/
  331. __STATIC_INLINE void ACMP_DisableInterrupt(ACMP_Type *pACMPx)
  332. {
  333. pACMPx->CS &= ~ACMP_CS_ACIE_MASK;
  334. }
  335. /*****************************************************************************//*!
  336. *
  337. * @brief get the interrupt flag bit.
  338. *
  339. * @param[in] pACMPx pointer to an ACMP module.
  340. *
  341. * @return none.
  342. *
  343. * @ Pass/ Fail criteria: none.
  344. *
  345. * @see ACMP_ClrFlag.
  346. *
  347. *****************************************************************************/
  348. __STATIC_INLINE uint8_t ACMP_GetFlag(ACMP_Type *pACMPx)
  349. {
  350. return (pACMPx->CS & ACMP_CS_ACF_MASK);
  351. }
  352. /*****************************************************************************//*!
  353. *
  354. * @brief clear the interrupt flag bit.
  355. *
  356. * @param[in] pACMPx pointer to an ACMP module.
  357. *
  358. * @return none.
  359. *
  360. * @ Pass/ Fail criteria: none.
  361. *
  362. * @see ACMP_GetFlag.
  363. *
  364. *****************************************************************************/
  365. __STATIC_INLINE void ACMP_ClrFlag(ACMP_Type *pACMPx)
  366. {
  367. pACMPx->CS &= ~ACMP_CS_ACF_MASK;
  368. }
  369. /*****************************************************************************//*!
  370. *
  371. * @brief ACMP Positive Input Select.
  372. *
  373. * @param[in] pACMPx pointer to an ACMP module.
  374. * @param[in] u8PosPinSel positive input select, ACMP_INPUT_P_EXT0~2 or ACMP_INPUT_P_DAC.
  375. *
  376. * @return none.
  377. *
  378. * @ Pass/ Fail criteria: none.
  379. *
  380. * @see ACMP_NegativeInputSelect.
  381. *
  382. *****************************************************************************/
  383. __STATIC_INLINE void ACMP_PositiveInputSelect(ACMP_Type *pACMPx, uint8_t u8PosPinSel)
  384. {
  385. pACMPx->C0 &= ~ACMP_C0_ACPSEL_MASK;
  386. pACMPx->C0 |= u8PosPinSel;
  387. }
  388. /*****************************************************************************//*!
  389. *
  390. * @brief ACMP Negative Input Select.
  391. *
  392. * @param[in] pACMPx pointer to an ACMP module.
  393. * @param[in] u8NegPinSel negative input select, ACMP_INPUT_N_EXT0~2 or ACMP_INPUT_N_DAC.
  394. *
  395. * @return none.
  396. *
  397. * @ Pass/ Fail criteria: none.
  398. *
  399. * @see ACMP_PositiveInputSelect.
  400. *
  401. *****************************************************************************/
  402. __STATIC_INLINE void ACMP_NegativeInputSelect(ACMP_Type *pACMPx, uint8_t u8NegPinSel)
  403. {
  404. pACMPx->C0 &= ~ACMP_C0_ACNSEL_MASK;
  405. pACMPx->C0 |= u8NegPinSel;
  406. }
  407. /*****************************************************************************//*!
  408. *
  409. * @brief Enable 6 bit DAC in ACMP module.
  410. *
  411. * @param[in] pACMPx pointer to an ACMP module.
  412. *
  413. * @return none.
  414. *
  415. * @ Pass/ Fail criteria: none.
  416. *
  417. * @see ACMP_DacDisable.
  418. *
  419. *****************************************************************************/
  420. __STATIC_INLINE void ACMP_DacEnable(ACMP_Type *pACMPx)
  421. {
  422. pACMPx->C1 |= ACMP_C1_DACEN_MASK;
  423. }
  424. /*****************************************************************************//*!
  425. *
  426. * @brief Disable 6 bit DAC in ACMP module.
  427. *
  428. * @param[in] pACMPx pointer to an ACMP module.
  429. *
  430. * @return none.
  431. *
  432. * @ Pass/ Fail criteria: none.
  433. *
  434. * @see ACMP_DacEnable.
  435. *
  436. *****************************************************************************/
  437. __STATIC_INLINE void ACMP_DacDisable(ACMP_Type *pACMPx)
  438. {
  439. pACMPx->C1 &= ~ACMP_C1_DACEN_MASK;
  440. }
  441. /*****************************************************************************//*!
  442. *
  443. * @brief ACMP 6 bit DAC Reference Select.
  444. *
  445. * @param[in] pACMPx pointer to an ACMP module.
  446. * @param[in] u8RefSelect dac reference select:ACMP_DAC_REFERENCE_BANDGAP or ACMP_DAC_REFERENCE_VDDA.
  447. *
  448. * @return none.
  449. *
  450. * @ Pass/ Fail criteria: none.
  451. *
  452. *****************************************************************************/
  453. __STATIC_INLINE void ACMP_DacReferenceSelect(ACMP_Type *pACMPx, uint8_t u8RefSelect)
  454. {
  455. pACMPx->C1 &= ~ACMP_C1_DACREF_MASK;
  456. pACMPx->C1 |= u8RefSelect;
  457. }
  458. /*****************************************************************************//*!
  459. *
  460. * @brief ACMP 6 bit DAC Output Value Set.
  461. *
  462. * @param[in] pACMPx pointer to an ACMP module.
  463. * @param[in] u8DacValue dac output set, Voutput= (Vin/64)x(DACVAL[5:0]+1).
  464. *
  465. * @return none.
  466. *
  467. * @ Pass/ Fail criteria: none.
  468. *
  469. *****************************************************************************/
  470. __STATIC_INLINE void ACMP_DacOutputSet(ACMP_Type *pACMPx, uint8_t u8DacValue)
  471. {
  472. ASSERT(!(u8DacValue & (~ACMP_C1_DACVAL_MASK)));
  473. pACMPx->C1 &= ~ACMP_C1_DACVAL_MASK;
  474. pACMPx->C1 |= ACMP_C1_DACVAL(u8DacValue);
  475. }
  476. /*****************************************************************************//*!
  477. *
  478. * @brief Enable ACMP input pin.
  479. *
  480. * @param[in] pACMPx pointer to an ACMP module.
  481. * @param[in] u8InputPin ACMP external pin, 0~2.
  482. *
  483. * @return none.
  484. *
  485. * @ Pass/ Fail criteria: none.
  486. *
  487. *****************************************************************************/
  488. __STATIC_INLINE void ACMP_InputPinEnable(ACMP_Type *pACMPx, uint8_t u8InputPin)
  489. {
  490. ASSERT(!(u8InputPin & (~ACMP_C2_ACIPE_MASK)));
  491. pACMPx->C2 |= ACMP_C2_ACIPE(u8InputPin);
  492. }
  493. /*****************************************************************************//*!
  494. *
  495. * @brief Disable ACMP input pin.
  496. *
  497. * @param[in] pACMPx pointer to an ACMP module.
  498. * @param[in] u8InputPin ACMP external pin, 0~2.
  499. *
  500. * @return none.
  501. *
  502. * @ Pass/ Fail criteria: none.
  503. *
  504. *****************************************************************************/
  505. __STATIC_INLINE void ACMP_InputPinDisable(ACMP_Type *pACMPx, uint8_t u8InputPin)
  506. {
  507. ASSERT(!(u8InputPin & (~ACMP_C2_ACIPE_MASK)));
  508. pACMPx->C2 &= ~ACMP_C2_ACIPE(u8InputPin);
  509. }
  510. /*! @} End of acmp_api_list */
  511. /******************************************************************************
  512. * Global functions
  513. ******************************************************************************/
  514. void ACMP_Init(ACMP_Type *pACMPx, ACMP_ConfigType *pConfig);
  515. void ACMP_DeInit(ACMP_Type *pACMPx);
  516. void ACMP_ConfigDAC(ACMP_Type *pACMPx, ACMP_DACType *pDACConfig);
  517. void ACMP_SetCallback(ACMP_Type *pACMPx, ACMP_CallbackPtr pfnCallback);
  518. #ifdef __cplusplus
  519. }
  520. #endif
  521. #endif /* _MY_ACMP_H_ */