1 /******************************************************************************
2 * Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd ("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 /******************************************************************************/
43 /** \file opa.c
44  **
45  ** opa driver API.
46  ** @link opa Group Some description @endlink
47  **
48  **   - 2018-04-15   Devi    First Version
49  **
50  ******************************************************************************/
51 
52 /******************************************************************************
53  * Include files
54  ******************************************************************************/
55 #include "opa.h"
56 
57 /**
58  ******************************************************************************
59  ** \addtogroup OPAGroup
60  ******************************************************************************/
61 //@{
62 
63 /******************************************************************************
64  * Local pre-processor symbols/macros ('#define')
65  ******************************************************************************/
66 
67 #define IS_VALID_pagagain(x)     ( (x) <= 7 )
68 
69 #define IS_VALID_channel(x)      (  (OPA0 == (x)) ||\
70                                     (OPA1 == (x)) ||\
71                                     (OPA2 == (x)) )
72 
73 #define IS_VALID_Mode(x)         (   (OpaUintMode == (x)) ||\
74                                      (OpaForWardMode == (x)) ||\
75                                      (OpaOppositeMode == (x)) ||\
76                                      (OpaThreeOppMode == (x)) ||\
77                                      (OpaThreeForMode == (x)) ||\
78                                      (OpaDiffMode == (x)) ||\
79                                      (OpaMeterMode == (x)) ||\
80                                      (OpaGpMode == (x)) )
81 
82 #define IS_VALID_metergain(x)      (  (OpaMeterGain3   == (x)) ||\
83                                       (OpaMeterGain1_3 == (x)) ||\
84                                       (OpaMeterGain1   == (x)) )
85 
86 #define IS_VALID_calsel(x)         (  (OpaSoftMode   == (x)) ||\
87                                       (OpaSoftTriggerMode == (x)) ||\
88                                       (OpaADCTriggerMode   == (x)) )
89 
90 /******************************************************************************
91  * Global variable definitions (declared in header file with 'extern')
92  ******************************************************************************/
93 
94 
95 /******************************************************************************
96  * Local type definitions ('typedef')
97  ******************************************************************************/
98 
99 /******************************************************************************
100  * Local function prototypes ('static')
101  ******************************************************************************/
102 
103 /******************************************************************************
104  * Local variable definitions ('static')
105  ******************************************************************************/
106 
107 /*****************************************************************************
108  * Function implementation - global ('extern') and local ('static')
109  *****************************************************************************/
110 
111 /**
112  * \brief
113  *          OPA 初始化
114  *
115  * \param   无
116  * \param   无
117  *
118  * \retval  无
119  * \retval  无
120  */
OPA_Init(void)121 en_result_t OPA_Init(void)
122 {
123 	 uint16_t i;
124 
125 	 M0P_SYSCTRL->PERI_CLKEN_f.ADC = 1;
126 	 M0P_BGR->CR_f.BGR_EN = 1;
127    for(i=0;i<2000;i++)
128    {
129       ;
130    }
131 
132    M0P_OPA->CR0 = 0x120;
133    M0P_OPA->CR1 = 0x120;
134 	 M0P_OPA->CR2 = 0x120;
135    M0P_OPA->CR = 0x00;
136    return Ok;
137 }
138 
139 /**
140  * \brief
141  *          OPA 去初始化
142  *
143  * \param   无
144  * \param   无
145  *
146  * \retval  无
147  * \retval  无
148  */
OPA_DeInit(void)149 en_result_t OPA_DeInit(void)
150 {
151 
152    M0P_OPA->CR0 = 0x120;
153    M0P_OPA->CR1 = 0x120;
154 	 M0P_OPA->CR2 = 0x120;
155    M0P_OPA->CR = 0x00;
156    M0P_BGR->CR_f.BGR_EN = 0;
157    M0P_SYSCTRL->PERI_CLKEN_f.ADC = 0;
158    return Ok;
159 }
160 
161 /**
162  * \brief
163  *          OPA 基本功能设置
164  *
165  * \param   [in]  en_opa_channel_t  使用那个通道的OPA
166  * \param   [in]  en_opa_modesel_t  OPA模式选择
167   * \param  [in]  stc_opa_gain_config_t  OPA增益选择
168  *
169  * \retval  无
170  */
OPA_Operate(en_opa_channel_t enchannel,en_opa_modesel_t enMode,stc_opa_gain_config_t * pstrGain)171 en_result_t OPA_Operate(en_opa_channel_t enchannel ,en_opa_modesel_t enMode,stc_opa_gain_config_t *pstrGain)
172 {
173     stc_opa_cr0_field_t *stcOpacr;
174 
175     ASSERT( IS_VALID_Mode(enMode) );
176     ASSERT( IS_VALID_channel(enchannel) );
177 
178     if (OPA0 == enchannel)
179     {
180         stcOpacr = (stc_opa_cr0_field_t*)&M0P_OPA->CR0_f;
181     }
182     if (OPA1 == enchannel)
183     {
184         stcOpacr = (stc_opa_cr0_field_t*)&M0P_OPA->CR1_f;
185     }
186     if (OPA2 == enchannel)
187     {
188         stcOpacr = (stc_opa_cr0_field_t*)&M0P_OPA->CR2_f;
189     }
190 
191     if(enMode == OpaUintMode)
192     {
193       	stcOpacr->NEGSEL = 0;
194 			  stcOpacr->POSSEL = 3;
195       	stcOpacr->UBUFSEL = 1;
196         stcOpacr->POEN = 1;
197     }
198     else if(enMode == OpaForWardMode)
199     {
200         stcOpacr->NEGSEL = 1;
201 	      stcOpacr->POEN = 1;
202 	      stcOpacr->PGAGAIN = pstrGain->enNoInGain;
203 	      stcOpacr->POSSEL = 3;
204 	      stcOpacr->RESINMUX = 0;
205 	      stcOpacr->RESSEL = 1;
206     }
207     else if(enMode == OpaOppositeMode)
208     {
209          stcOpacr->NEGSEL = 1;
210 	       stcOpacr->POEN = 1;
211 	       stcOpacr->PGAGAIN = pstrGain->enInGain;
212 	       stcOpacr->POSSEL = 3;
213 	       stcOpacr->RESINMUX = 2;
214 	       stcOpacr->RESSEL = 1;
215     }
216     else if(enMode == OpaDiffMode)
217     {
218           M0P_OPA->CR0_f.POSSEL = 3;
219         	M0P_OPA->CR1_f.POSSEL = 3;
220         	M0P_OPA->CR2_f.POSSEL = 0;
221 
222         	M0P_OPA->CR0_f.NEGSEL = 0;
223           M0P_OPA->CR1_f.NEGSEL = 1;
224           M0P_OPA->CR2_f.NEGSEL = 1;
225 
226         	M0P_OPA->CR0_f.RESINMUX = 0;
227         	M0P_OPA->CR1_f.RESINMUX = 1;
228         	M0P_OPA->CR2_f.RESINMUX = 0;
229 
230         	M0P_OPA->CR0_f.UBUFSEL = 1;
231         	M0P_OPA->CR1_f.UBUFSEL = 0;
232         	M0P_OPA->CR2_f.UBUFSEL = 0;
233 
234         	M0P_OPA->CR0_f.RESSEL = 0;
235         	M0P_OPA->CR1_f.RESSEL = 1;
236         	M0P_OPA->CR2_f.RESSEL = 0;
237 
238         	M0P_OPA->CR0_f.POEN = 0;
239         	M0P_OPA->CR1_f.POEN = 1;
240         	M0P_OPA->CR2_f.POEN = 0;
241 
242         	M0P_OPA->CR0_f.PGAGAIN = 0;
243           M0P_OPA->CR1_f.PGAGAIN = pstrGain->enNoInGain;
244           M0P_OPA->CR2_f.PGAGAIN = 0;
245     }
246     else if(enMode == OpaGpMode)
247     {
248 	        stcOpacr->BIASSEL = 1;
249           stcOpacr->MODE = 1;
250           stcOpacr->NEGSEL = 3;
251 	        stcOpacr->POEN = 0;
252 	        stcOpacr->PGAGAIN = 5;
253 	        stcOpacr->POSSEL = 3;
254 	        stcOpacr->RESINMUX = 0;
255 	        stcOpacr->RESSEL = 0;
256 	        stcOpacr->UBUFSEL = 0;
257     }
258     else
259     {
260     	return ErrorInvalidParameter;
261     }
262  	 M0P_OPA->CR0_f.EN = 1;
263  	 M0P_OPA->CR1_f.EN = 1;
264  	 M0P_OPA->CR2_f.EN = 1;
265    return Ok;
266 }
267 
268 /**
269  * \brief
270  *          OPA 基本功能设置  (级联正向和反向模式以及仪表模式)
271  * \param   [in]  en_opa_modesel_t  OPA模式选择
272   * \param  [in]  stc_opa_gain_config_t  OPA增益选择
273  *
274  * \retval  无
275  */
OPA_ThreeOperate(en_opa_modesel_t enMode,stc_opa_gain_config_t * pstrGain0,stc_opa_gain_config_t * pstrGain1,stc_opa_gain_config_t * pstrGain2)276 en_result_t OPA_ThreeOperate(en_opa_modesel_t enMode,stc_opa_gain_config_t *pstrGain0,stc_opa_gain_config_t *pstrGain1,stc_opa_gain_config_t *pstrGain2)
277 {
278 
279     ASSERT( IS_VALID_Mode(enMode) );
280 
281     if(enMode == OpaThreeOppMode)
282     {
283 	     M0P_OPA->CR0_f.POSSEL = 3;
284 	     M0P_OPA->CR1_f.POSSEL = 3;
285 	     M0P_OPA->CR2_f.POSSEL = 3;
286 
287 	     M0P_OPA->CR0_f.NEGSEL = 1;
288        M0P_OPA->CR1_f.NEGSEL = 1;
289        M0P_OPA->CR2_f.NEGSEL = 1;
290 
291 	     M0P_OPA->CR0_f.RESINMUX = 2;
292 	     M0P_OPA->CR1_f.RESINMUX = 1;
293 	     M0P_OPA->CR2_f.RESINMUX = 1;
294 
295 	     M0P_OPA->CR0_f.RESSEL = 1;
296 	     M0P_OPA->CR1_f.RESSEL = 1;
297 	     M0P_OPA->CR2_f.RESSEL = 1;
298 
299 	     M0P_OPA->CR0_f.POEN = 0;
300 	     M0P_OPA->CR1_f.POEN = 0;
301 	     M0P_OPA->CR2_f.POEN = 1;
302 
303 	     M0P_OPA->CR0_f.PGAGAIN = pstrGain0->enInGain;
304        M0P_OPA->CR1_f.PGAGAIN = pstrGain1->enInGain;
305        M0P_OPA->CR2_f.PGAGAIN = pstrGain2->enInGain;
306     }
307     else if(enMode == OpaThreeForMode)
308     {
309 	     M0P_OPA->CR0_f.POSSEL = 3;
310 	     M0P_OPA->CR1_f.POSSEL = 2;
311 	     M0P_OPA->CR2_f.POSSEL = 2;
312 
313 	     M0P_OPA->CR0_f.NEGSEL = 1;
314        M0P_OPA->CR1_f.NEGSEL = 1;
315        M0P_OPA->CR2_f.NEGSEL = 1;
316 
317 	     M0P_OPA->CR0_f.RESINMUX = 0;
318 	     M0P_OPA->CR1_f.RESINMUX = 0;
319 	     M0P_OPA->CR2_f.RESINMUX = 0;
320 
321 	     M0P_OPA->CR0_f.UBUFSEL = 0;
322 	     M0P_OPA->CR1_f.UBUFSEL = 0;
323 	     M0P_OPA->CR2_f.UBUFSEL = 0;
324 
325 	     M0P_OPA->CR0_f.RESSEL = 1;
326 	     M0P_OPA->CR1_f.RESSEL = 1;
327 	     M0P_OPA->CR2_f.RESSEL = 1;
328 
329 	     M0P_OPA->CR0_f.POEN = 0;
330 	     M0P_OPA->CR1_f.POEN = 0;
331 	     M0P_OPA->CR2_f.POEN = 1;
332 
333 	     M0P_OPA->CR0_f.PGAGAIN = pstrGain0->enNoInGain;
334        M0P_OPA->CR1_f.PGAGAIN = pstrGain1->enNoInGain;
335        M0P_OPA->CR2_f.PGAGAIN = pstrGain2->enNoInGain;
336     }
337     else
338     {
339     	return ErrorInvalidParameter;
340     }
341  	 M0P_OPA->CR0_f.EN = 1;
342  	 M0P_OPA->CR1_f.EN = 1;
343  	 M0P_OPA->CR2_f.EN = 1;
344    return Ok;
345 }
346 
347 /**
348  * \brief
349  *          OPA 仪表模式
350   * \param  [in]  en_opa_metergain_t  OPA增益选择
351  *
352  * \retval  无
353  */
OPA_MeterOperate(en_opa_metergain_t enGainMode)354 en_result_t OPA_MeterOperate(en_opa_metergain_t enGainMode)
355 {
356    ASSERT( IS_VALID_metergain(enGainMode) );
357 
358   M0P_OPA->CR0_f.POSSEL = 3;
359 	M0P_OPA->CR1_f.POSSEL = 3;
360 	M0P_OPA->CR2_f.POSSEL = 1;
361 
362 	M0P_OPA->CR0_f.NEGSEL = 0;
363   M0P_OPA->CR1_f.NEGSEL = 0;
364   M0P_OPA->CR2_f.NEGSEL = 1;
365 
366 	M0P_OPA->CR0_f.RESINMUX = 0;
367 	M0P_OPA->CR1_f.RESINMUX = 0;
368 	M0P_OPA->CR2_f.RESINMUX = 1;
369 
370 	M0P_OPA->CR0_f.UBUFSEL = 1;
371 	M0P_OPA->CR1_f.UBUFSEL = 1;
372 	M0P_OPA->CR2_f.UBUFSEL = 0;
373 
374 	M0P_OPA->CR0_f.RESSEL = 1;
375 	M0P_OPA->CR1_f.RESSEL = 0;
376 	M0P_OPA->CR2_f.RESSEL = 1;
377 
378 	M0P_OPA->CR0_f.POEN = 0;
379 	M0P_OPA->CR1_f.POEN = 0;
380 	M0P_OPA->CR2_f.POEN = 1;
381 
382 	if(enGainMode == OpaMeterGain3)
383 	{
384 	 M0P_OPA->CR0_f.PGAGAIN = 6;
385    M0P_OPA->CR2_f.PGAGAIN = 3;
386 	}
387 	if(enGainMode == OpaMeterGain1_3)
388 	{
389 	 M0P_OPA->CR0_f.PGAGAIN = 3;
390    M0P_OPA->CR2_f.PGAGAIN = 6;
391 	}
392 	if(enGainMode == OpaMeterGain1)
393 	{
394 	 M0P_OPA->CR0_f.PGAGAIN = 5;
395    M0P_OPA->CR2_f.PGAGAIN = 5;
396 	}
397 
398  	 M0P_OPA->CR0_f.EN = 1;
399  	 M0P_OPA->CR1_f.EN = 1;
400  	 M0P_OPA->CR2_f.EN = 1;
401    return Ok;
402 }
403 /**
404  * \brief
405  *          OPA 校正模式
406   * \param  [in]  en_opa_calsel_t  OPA校正模式选择
407  *
408  * \retval  无
409  */
OPA_Cal(en_opa_calsel_t enCalMode)410 en_result_t OPA_Cal(en_opa_calsel_t enCalMode)
411 {
412    ASSERT( IS_VALID_calsel(enCalMode) );
413 
414 	if(enCalMode == OpaSoftMode)
415 	{
416 
417 	}
418 	if(enCalMode == OpaSoftTriggerMode)
419 	{
420 
421 	}
422 	if (enCalMode == OpaADCTriggerMode)
423 	{
424 
425 	}
426 
427  	 M0P_OPA->CR0_f.EN = 1;
428  	 M0P_OPA->CR1_f.EN = 1;
429  	 M0P_OPA->CR2_f.EN = 1;
430    return Ok;
431 }
432 //@} // OPAGroup
433 
434 
435 /******************************************************************************
436  * EOF (not truncated)
437  ******************************************************************************/
438 
439