1 /*
2  * @brief LPC15xx Analog comparator driver
3  *
4  * Copyright(C) NXP Semiconductors, 2014
5  * All rights reserved.
6  *
7  * Software that is described herein is for illustrative purposes only
8  * which provides customers with programming information regarding the
9  * LPC products.  This software is supplied "AS IS" without any warranties of
10  * any kind, and NXP Semiconductors and its licensor disclaim any and
11  * all warranties, express or implied, including all implied warranties of
12  * merchantability, fitness for a particular purpose and non-infringement of
13  * intellectual property rights.  NXP Semiconductors assumes no responsibility
14  * or liability for the use of the software, conveys no license or rights under any
15  * patent, copyright, mask work right, or any other intellectual property rights in
16  * or to any products. NXP Semiconductors reserves the right to make changes
17  * in the software without notification. NXP Semiconductors also makes no
18  * representation or warranty that such application will be suitable for the
19  * specified use without further testing or modification.
20  *
21  * Permission to use, copy, modify, and distribute this software and its
22  * documentation is hereby granted, under NXP Semiconductors' and its
23  * licensor's relevant copyrights in the software, without fee, provided that it
24  * is used in conjunction with NXP Semiconductors microcontrollers.  This
25  * copyright, permission, and disclaimer notice must appear in all copies of
26  * this code.
27  */
28 
29 #include "chip.h"
30 
31 /*****************************************************************************
32  * Private types/enumerations/variables
33  ****************************************************************************/
34 
35 /*****************************************************************************
36  * Public types/enumerations/variables
37  ****************************************************************************/
38 
39 /*****************************************************************************
40  * Private functions
41  ****************************************************************************/
42 
43 /*****************************************************************************
44  * Public functions
45  ****************************************************************************/
46 
47 /* Initializes the ACMP */
Chip_ACMP_Init(LPC_CMP_T * pACMP)48 void Chip_ACMP_Init(LPC_CMP_T *pACMP)
49 {
50 	Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_ACMP0_PD | SYSCTL_POWERDOWN_ACMP1_PD |
51 						SYSCTL_POWERDOWN_ACMP2_PD | SYSCTL_POWERDOWN_ACMP3_PD);
52 	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_ACMP);
53 	Chip_SYSCTL_PeriphReset(RESET_ACMP);
54 }
55 
56 /* De-initializes the ACMP */
Chip_ACMP_Deinit(LPC_CMP_T * pACMP)57 void Chip_ACMP_Deinit(LPC_CMP_T *pACMP)
58 {
59 	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_ACMP);
60 	Chip_SYSCTL_PowerDown(SYSCTL_POWERDOWN_ACMP0_PD | SYSCTL_POWERDOWN_ACMP1_PD |
61 						  SYSCTL_POWERDOWN_ACMP2_PD | SYSCTL_POWERDOWN_ACMP3_PD);
62 }
63 
64 /*Sets up ACMP edge selection */
Chip_ACMP_SetIntEdgeSelection(LPC_CMP_T * pACMP,uint8_t index,CHIP_ACMP_EDGESEL_T edgeSel)65 void Chip_ACMP_SetIntEdgeSelection(LPC_CMP_T *pACMP, uint8_t index, CHIP_ACMP_EDGESEL_T edgeSel)
66 {
67 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
68 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~ACMP_INTEDGE_MASK;
69 
70 	pACMP->ACMP[index].CMP = reg | (uint32_t) edgeSel;
71 }
72 
73 /*Selects positive voltage input */
Chip_ACMP_SetPosVoltRef(LPC_CMP_T * pACMP,uint8_t index,CHIP_ACMP_POS_INPUT_T Posinput)74 void Chip_ACMP_SetPosVoltRef(LPC_CMP_T *pACMP, uint8_t index, CHIP_ACMP_POS_INPUT_T Posinput)
75 {
76 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
77 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~ACMP_COMPVPSEL_MASK;
78 
79 	/* Select positive input */
80 	pACMP->ACMP[index].CMP = reg | (uint32_t) Posinput;
81 }
82 
83 /*Selects negative voltage input */
Chip_ACMP_SetNegVoltRef(LPC_CMP_T * pACMP,uint8_t index,CHIP_ACMP_NEG_INPUT_T Neginput)84 void Chip_ACMP_SetNegVoltRef(LPC_CMP_T *pACMP, uint8_t index, CHIP_ACMP_NEG_INPUT_T Neginput)
85 {
86 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
87 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~ACMP_COMPVMSEL_MASK;
88 
89 	/* Select negative input */
90 	pACMP->ACMP[index].CMP = reg | (uint32_t) Neginput;
91 }
92 
93 /*Selects hysteresis level */
Chip_ACMP_SetHysteresis(LPC_CMP_T * pACMP,uint8_t index,CHIP_ACMP_HYS_T hys)94 void Chip_ACMP_SetHysteresis(LPC_CMP_T *pACMP, uint8_t index, CHIP_ACMP_HYS_T hys)
95 {
96 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
97 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~ACMP_HYSTERESIS_MASK;
98 
99 	pACMP->ACMP[index].CMP = reg | (uint32_t) hys;
100 }
101 
102 /*Helper function for setting up ACMP voltage settings */
Chip_ACMP_SetupACMPRefs(LPC_CMP_T * pACMP,uint8_t index,CHIP_ACMP_POS_INPUT_T Posinput,CHIP_ACMP_NEG_INPUT_T Neginput,CHIP_ACMP_HYS_T hys)103 void Chip_ACMP_SetupACMPRefs(LPC_CMP_T *pACMP, uint8_t index, CHIP_ACMP_POS_INPUT_T Posinput,
104 							 CHIP_ACMP_NEG_INPUT_T Neginput, CHIP_ACMP_HYS_T hys)
105 {
106 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
107 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~(ACMP_HYSTERESIS_MASK |
108 																	ACMP_COMPVMSEL_MASK | ACMP_COMPVPSEL_MASK);
109 
110 	pACMP->ACMP[index].CMP = reg | (uint32_t) Posinput | (uint32_t) Neginput | (uint32_t) hys;
111 }
112 
113 /*Helper function for setting up ACMP interrupt settings */
Chip_ACMP_SetupACMPInt(LPC_CMP_T * pACMP,uint8_t index,bool level,bool invert,CHIP_ACMP_EDGESEL_T edgeSel)114 void Chip_ACMP_SetupACMPInt(LPC_CMP_T *pACMP, uint8_t index, bool level,
115 							bool invert, CHIP_ACMP_EDGESEL_T edgeSel)
116 {
117 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
118 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~(ACMP_INTPOL_BIT |
119 																	ACMP_INTTYPE_BIT | ACMP_INTEDGE_MASK);
120 	/* For Level triggered interrupt, invert sets the polarity
121 	     For edge triggered interrupt edgeSel sets the edge type */
122 	if (level) {
123 		reg |= ACMP_INTTYPE_BIT;
124 		if (invert) {
125 			reg |= ACMP_INTPOL_BIT;
126 		}
127 	}
128 	else {
129 		reg |= (uint32_t) edgeSel;
130 	}
131 
132 	pACMP->ACMP[index].CMP = reg;
133 }
134 
135 /*Sets up voltage ladder */
Chip_ACMP_SetupVoltLadder(LPC_CMP_T * pACMP,uint8_t index,uint32_t ladsel,bool ladrefVDDCMP)136 void Chip_ACMP_SetupVoltLadder(LPC_CMP_T *pACMP, uint8_t index, uint32_t ladsel, bool ladrefVDDCMP)
137 {
138 	/* Make sure interrupt flag is not set during read OR/AND and write operation */
139 	uint32_t reg = (pACMP->ACMP[index].CMP & ~ACMP_INTFLAG_BIT) & ~(ACMP_LADSEL_MASK | ACMP_LADREF_BIT);
140 
141 	/* Setup voltage ladder and ladder reference */
142 	if (!ladrefVDDCMP) {
143 		reg |= ACMP_LADREF_BIT;
144 	}
145 	pACMP->ACMP[index].CMP = reg | (ladsel << 24);
146 }
147