1 /*****************************************************************************
2  * Copyright (c) 2022, 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 /**
29  * @file n32l43x_opamp.h
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #ifndef __N32L43X_OPAMPMP_H__
36 #define __N32L43X_OPAMPMP_H__
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include "n32l43x.h"
43 #include <stdbool.h>
44 
45 /** @addtogroup n32l43x_StdPeriph_Driver
46  * @{
47  */
48 
49 /** @addtogroup OPAMP
50  * @{
51  */
52 
53 /** @addtogroup OPAMP_Exported_Constants
54  * @{
55  */
56 typedef enum
57 {
58     OPAMP1 = 0,
59     OPAMP2 = 4,
60 } OPAMPX;
61 
62 // OPAMP_CS
63 typedef enum
64 {
65     OPAMP2_CS_TIMSRCSEL_TIM1CC6  = (0x0L << 24),
66     OPAMP2_CS_TIMSRCSEL_TIM8CC6  = (0x1L << 24),
67 }OPAMP2_CS_TIMSRCSEL;
68 typedef enum
69 {
70     OPAMP1_CS_VPSSEL_PA1        = (0x00L << 19),
71     OPAMP1_CS_VPSSEL_PA5        = (0x01L << 19),
72     OPAMP1_CS_VPSSEL_PA4        = (0x02L << 19),
73     OPAMP1_CS_VPSSEL_PA7        = (0x03L << 19),
74     OPAMP1_CS_VPSSEL_NC         = (0x04L << 19),
75 
76     OPAMP2_CS_VPSSEL_PA7        = (0x00L << 19),
77     OPAMP2_CS_VPSSEL_PA4        = (0x01L << 19),
78     OPAMP2_CS_VPSSEL_PB14       = (0x02L << 19),
79     OPAMP2_CS_VPSSEL_PD13       = (0x03L << 19),
80     OPAMP2_CS_VPSSEL_NC         = (0x04L << 19),
81 } OPAMP_CS_VPSSEL;
82 typedef enum
83 {
84     OPAMP1_CS_VMSSEL_PA3    = (0x00L << 17),
85     OPAMP1_CS_VMSSEL_PC5    = (0x01L << 17),
86     OPAMP1_CS_VMSSEL_NC     = (0x02L << 17),
87     OPAMP1_CS_VMSSEL_FLOAT  = (0x03L << 17),
88 
89     OPAMP2_CS_VMSSEL_PC5    = (0x00L << 17),
90     OPAMP2_CS_VMSSEL_PB0    = (0x01L << 17),
91     OPAMP2_CS_VMSSEL_PA5    = (0x02L << 17),
92     OPAMP2_CS_VMSSEL_FLOAT  = (0x03L << 17),
93 } OPAMP_CS_VMSSEL;
94 
95 typedef enum
96 {
97     OPAMP1_CS_VPSEL_PA1     = (0x00L << 8),
98     OPAMP1_CS_VPSEL_PA5     = (0x01L << 8),
99     OPAMP1_CS_VPSEL_PA4     = (0x02L << 8),
100     OPAMP1_CS_VPSEL_PA7     = (0x03L << 8),
101     OPAMP1_CS_VPSEL_NC      = (0x04L << 8),
102 
103     OPAMP2_CS_VPSEL_PA7     = (0x00L << 8),
104     OPAMP2_CS_VPSEL_PA4     = (0x01L << 8),
105     OPAMP2_CS_VPSEL_PB14    = (0x02L << 8),
106     OPAMP2_CS_VPSEL_PD13    = (0x03L << 8),
107     OPAMP2_CS_VPSEL_NC      = (0x04L << 8),
108 } OPAMP_CS_VPSEL;
109 typedef enum
110 {
111     OPAMP1_CS_VMSEL_PA3     = (0x00L << 6),
112     OPAMP1_CS_VMSEL_PC5     = (0x01L << 6),
113     OPAMPx_CS_VMSEL_NC      = (0x02L << 6),
114     OPAMPx_CS_VMSEL_FLOAT   = (0x03L << 6),
115 
116     OPAMP2_CS_VMSEL_PC5     = (0x00L << 6),
117     OPAMP2_CS_VMSEL_PB0     = (0x01L << 6),
118     OPAMP2_CS_VMSEL_PA5     = (0x02L << 6),
119     OPAMP2_CS_VMSEL_FLOAT   = (0x03L << 6),
120 } OPAMP_CS_VMSEL;
121 typedef enum
122 {
123     OPAMP_CS_PGA_GAIN_2  = (0x00 << 3),
124     OPAMP_CS_PGA_GAIN_4  = (0x01 << 3),
125     OPAMP_CS_PGA_GAIN_8  = (0x02 << 3),
126     OPAMP_CS_PGA_GAIN_16 = (0x03 << 3),
127     OPAMP_CS_PGA_GAIN_32 = (0x04 << 3),
128 } OPAMP_CS_PGA_GAIN;
129 typedef enum
130 {
131     OPAMP_CS_EXT_OPAMP = (0x00 << 1),
132     OPAMP_CS_PGA_EN    = (0x02 << 1),
133     OPAMP_CS_FOLLOW    = (0x03 << 1),
134 } OPAMP_CS_MOD;
135 
136 // bit mask
137 #define OPAMP_CS_EN_MASK           (0x01L << 0)
138 #define OPAMP_CS_MOD_MASK          (0x03L << 1)
139 #define OPAMP_CS_PGA_GAIN_MASK     (0x07L << 3)
140 #define OPAMP_CS_VMSEL_MASK        (0x03L << 6)
141 #define OPAMP_CS_VPSEL_MASK        (0x07L << 8)
142 #define OPAMP_CS_CALON_MASK        (0x01L << 11)
143 #define OPAMP_CS_TSTREF_MASK       (0x01L << 13)
144 #define OPAMP_CS_CALOUT_MASK       (0x01L << 14)
145 #define OPAMP_CS_RANGE_MASK        (0x01L << 15)
146 #define OPAMP_CS_TCMEN_MASK        (0x01L << 16)
147 #define OPAMP_CS_VMSEL_SECOND_MASK (0x03L << 17)
148 #define OPAMP_CS_VPSEL_SECOND_MASK (0x07L << 19)
149 #define OPAMP_CS_OPAMP2_TIMSRCSEL  (0x01L << 24)
150 /** @addtogroup OPAMP_LOCK
151  * @{
152  */
153 #define OPAMP_LOCK_1 0x01L
154 #define OPAMP_LOCK_2 0x02L
155 /**
156  * @}
157  */
158 /**
159  * @}
160  */
161 
162 /**
163  * @brief  OPAMP Init structure definition
164  */
165 
166 typedef struct
167 {
168     OPAMP2_CS_TIMSRCSEL Opa2SrcSel; /*only for opa2 can sel,opa1 always TIM1_CC6*/
169 
170     FunctionalState TimeAutoMuxEn; /*call ENABLE or DISABLE */
171 
172     FunctionalState HighVolRangeEn; /*call ENABLE or DISABLE ,low range VDDA < 2.4V,high range VDDA >= 2.4V*/
173 
174     OPAMP_CS_PGA_GAIN Gain; /*see @EM_PGA_GAIN */
175 
176     OPAMP_CS_MOD Mod; /*see @EM_OPAMP_MOD*/
177 } OPAMP_InitType;
178 
179 /** @addtogroup OPAMP_Exported_Functions
180  * @{
181  */
182 
183 void OPAMP_DeInit(void);
184 void OPAMP_StructInit(OPAMP_InitType* OPAMP_InitStruct);
185 void OPAMP_Init(OPAMPX OPAMPx, OPAMP_InitType* OPAMP_InitStruct);
186 void OPAMP_Enable(OPAMPX OPAMPx, FunctionalState en);
187 void OPAMP_SetPgaGain(OPAMPX OPAMPx, OPAMP_CS_PGA_GAIN Gain);
188 void OPAMP_SetVpSecondSel(OPAMPX OPAMPx, OPAMP_CS_VPSSEL VpSSel);
189 void OPAMP_SetVmSecondSel(OPAMPX OPAMPx, OPAMP_CS_VMSSEL VmSSel);
190 void OPAMP_SetVpSel(OPAMPX OPAMPx, OPAMP_CS_VPSEL VpSel);
191 void OPAMP_SetVmSel(OPAMPX OPAMPx, OPAMP_CS_VMSEL VmSel);
192 bool OPAMP_IsCalOutHigh(OPAMPX OPAMPx);
193 void OPAMP_CalibrationEnable(OPAMPX OPAMPx, FunctionalState en);
194 void OPAMP_SetLock(uint32_t Lock); // see @OPAMP_LOCK
195 /**
196  * @}
197  */
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif /*__N32L43X_ADC_H */
204        /**
205         * @}
206         */
207        /**
208         * @}
209         */
210