Subversion Repositories DashDisplay

Rev

Rev 61 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32l1xx_hal_opamp.c
4
  * @author  MCD Application Team
5
  * @brief   OPAMP HAL module driver.
6
  *          This file provides firmware functions to manage the following
7
  *          functionalities of the operational amplifier(s) peripheral:
8
  *           + OPAMP configuration
9
  *           + OPAMP calibration
10
  *          Thanks to
11
  *           + Initialization and de-initialization functions
12
  *           + IO operation functions
13
  *           + Peripheral Control functions
14
  *           + Peripheral State functions
15
  *        
16
  @verbatim
17
================================================================================
18
          ##### OPAMP Peripheral Features #####
19
================================================================================
20
  [..] The device integrates up to 3 operational amplifiers OPAMP1, OPAMP2,
21
       OPAMP3 (OPAMP3 availability depends on device category)
22
 
23
       (#) The OPAMP(s) provide(s) several exclusive running modes.
24
       (++) Standalone mode
25
       (++) Follower mode
26
 
27
       (#) All OPAMP (same for all OPAMPs) can operate in
28
       (++) Either Low range (VDDA < 2.4V) power supply
29
       (++) Or High range (VDDA > 2.4V) power supply
30
 
31
       (#) Each OPAMP(s) can be configured in normal and low power mode.
32
 
33
       (#) The OPAMP(s) provide(s) calibration capabilities.  
34
       (++) Calibration aims at correcting some offset for running mode.
35
       (++) The OPAMP uses either factory calibration settings OR user defined
36
           calibration (trimming) settings (i.e. trimming mode).
37
       (++) The user defined settings can be figured out using self calibration
38
           handled by HAL_OPAMP_SelfCalibrate, HAL_OPAMPEx_SelfCalibrateAll
39
       (++) HAL_OPAMP_SelfCalibrate:
40
       (+++) Runs automatically the calibration in 2 steps: for transistors
41
            differential pair high (PMOS) or low (NMOS)
42
       (+++) Enables the user trimming mode
43
       (+++) Updates the init structure with trimming values with fresh calibration
44
            results.
45
            The user may store the calibration results for larger
46
            (ex monitoring the trimming as a function of temperature
47
            for instance)
48
       (+++) For devices having several OPAMPs, HAL_OPAMPEx_SelfCalibrateAll
49
            runs calibration of all OPAMPs in parallel to save search time.
50
 
51
       (#) Running mode: Standalone mode
52
       (++) Gain is set externally (gain depends on external loads).
53
       (++) Follower mode also possible externally by connecting the inverting input to
54
           the output.
55
 
56
       (#) Running mode: Follower mode
57
       (++) No Inverting Input is connected.
58
       (++) The OPAMP(s) output(s) are internally connected to inverting input.
59
 
60
            ##### How to use this driver #####
61
================================================================================
62
  [..]
63
 
64
    *** Power supply range ***
65
    ============================================
66
    [..] To run in low power mode:
67
 
68
      (#) Configure the OPAMP using HAL_OPAMP_Init() function:
69
      (++) Select OPAMP_POWERSUPPLY_LOW (VDDA lower than 2.4V)
70
      (++) Otherwise select OPAMP_POWERSUPPLY_HIGH (VDDA higher than 2.4V)
71
 
72
    *** Low / normal power mode ***
73
    ============================================
74
    [..] To run in low power mode:
75
 
76
      (#) Configure the OPAMP using HAL_OPAMP_Init() function:
77
      (++) Select OPAMP_POWERMODE_LOWPOWER
78
      (++) Otherwise select OPAMP_POWERMODE_NORMAL
79
 
80
    *** Calibration ***
81
    ============================================
82
    [..] To run the OPAMP calibration self calibration:
83
 
84
      (#) Start calibration using HAL_OPAMP_SelfCalibrate.
85
           Store the calibration results.
86
 
87
    *** Running mode ***
88
    ============================================
89
 
90
    [..] To use the OPAMP, perform the following steps:
91
 
92
      (#) Fill in the HAL_OPAMP_MspInit() to
93
      (++) Enable the OPAMP Peripheral clock using macro __HAL_RCC_OPAMP_CLK_ENABLE()
94
      (++) Configure the OPAMP input AND output in analog mode using
95
           HAL_GPIO_Init() to map the OPAMP output to the GPIO pin.
96
 
97
      (#) Registrate Callbacks
98
      (++) The compilation define  USE_HAL_OPAMP_REGISTER_CALLBACKS when set to 1
99
           allows the user to configure dynamically the driver callbacks.
100
 
101
      (++) Use Functions @ref HAL_OPAMP_RegisterCallback() to register a user callback,
102
           it allows to register following callbacks:
103
      (+++) MspInitCallback         : OPAMP MspInit.
104
      (+++) MspDeInitCallback       : OPAMP MspFeInit.
105
           This function takes as parameters the HAL peripheral handle, the Callback ID
106
           and a pointer to the user callback function.
107
 
108
      (++) Use function @ref HAL_OPAMP_UnRegisterCallback() to reset a callback to the default
109
           weak (surcharged) function. It allows to reset following callbacks:
110
      (+++) MspInitCallback         : OPAMP MspInit.
111
      (+++) MspDeInitCallback       : OPAMP MspdeInit.
112
      (+++) All Callbacks
113
 
114
      (#) Configure the OPAMP using HAL_OPAMP_Init() function:
115
      (++) Select the mode
116
      (++) Select the inverting input
117
      (++) Select the non-inverting input
118
      (++) Select either factory or user defined trimming mode.
119
      (++) If the user-defined trimming mode is enabled, select PMOS & NMOS trimming values
120
          (typically values set by HAL_OPAMP_SelfCalibrate function).
121
 
122
      (#) Enable the OPAMP using HAL_OPAMP_Start() function.
123
 
124
      (#) Disable the OPAMP using HAL_OPAMP_Stop() function.
125
 
126
      (#) Lock the OPAMP in running mode using HAL_OPAMP_Lock() function.
127
          Caution: On STM32L1, HAL OPAMP lock is software lock only (not
128
          hardware lock as on some other STM32 devices)
129
 
130
      (#) If needed, unlock the OPAMP using HAL_OPAMPEx_Unlock() function.
131
 
132
    *** Running mode: change of configuration while OPAMP ON  ***
133
    ============================================
134
    [..] To Re-configure OPAMP when OPAMP is ON (change on the fly)
135
      (#) If needed, fill in the HAL_OPAMP_MspInit()
136
      (++) This is the case for instance if you wish to use new OPAMP I/O
137
 
138
      (#) Configure the OPAMP using HAL_OPAMP_Init() function:
139
      (++) As in configure case, select first the parameters you wish to modify.
140
 
141
      (#) Change from low power mode to normal power mode (& vice versa) requires  
142
          first HAL_OPAMP_DeInit() (force OPAMP OFF) and then HAL_OPAMP_Init().
143
          In other words, of OPAMP is ON, HAL_OPAMP_Init can NOT change power mode
144
          alone.
145
 
146
  @endverbatim
147
  ******************************************************************************
148
  * @attention
149
  *
150
  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
151
  * All rights reserved.</center></h2>
152
  *
153
  * This software component is licensed by ST under BSD 3-Clause license,
154
  * the "License"; You may not use this file except in compliance with the
155
  * License. You may obtain a copy of the License at:
156
  *                        opensource.org/licenses/BSD-3-Clause
157
  *
158
  ******************************************************************************  
159
  */
160
 
161
/*
162
  Additionnal remark:
163
    The OPAMPs inverting input can be selected among the list shown by table below.
164
    The OPAMPs non inverting input can be selected among the list shown by table below.
165
 
166
    Table 1.  OPAMPs inverting/non-inverting inputs for STM32L1 devices:
167
    +--------------------------------------------------------------------------+
168
    |                | HAL param  |    OPAMP1    |    OPAMP2    |   OPAMP3(4)  |
169
    |                |   name     |              |              |              |
170
    |----------------|------------|--------------|--------------|--------------|
171
    |   Inverting    |    VM0     |     PA2      |     PA7      |     PC2      |
172
    |    input (1)   |    VM1     | VINM pin (2) | VINM pin (2) | VINM pin (2) |
173
    |----------------|------------|--------------|--------------|--------------|
174
    |  Non Inverting |    VP0     |     PA1      |     PA6      |     PC1      |
175
    |    input       | DAC_CH1 (3)|   DAC_CH1    |   DAC_CH1    |     ---      |
176
    |                | DAC_CH2 (3)|     ---      |   DAC_CH2    |   DAC_CH2    |
177
    +--------------------------------------------------------------------------+
178
    (1): NA in follower mode.
179
    (2): OPAMP input OPAMPx_VINM are dedicated OPAMP pins, their availability
180
         depends on device package.
181
    (3): DAC channels 1 and 2 are connected internally to OPAMP. Nevertheless,
182
         I/O pins connected to DAC can still be used as DAC output (pins PA4
183
         and PA5).
184
    (4): OPAMP3 availability depends on device category.
185
 
186
    Table 2.  OPAMPs outputs for STM32L1 devices:
187
    +--------------------------------------------------------+
188
    |                 |   OPAMP1   |   OPAMP2   |  OPAMP3(4) |
189
    |-----------------|------------|------------|------------|
190
    | Output          |    PA3     |    PB0     |    PC3     |
191
    +--------------------------------------------------------+
192
    (4) : OPAMP3 availability depends on device category
193
*/
194
 
195
/* Includes ------------------------------------------------------------------*/
196
#include "stm32l1xx_hal.h"
197
 
198
/** @addtogroup STM32L1xx_HAL_Driver
199
  * @{
200
  */
201
 
202
/** @defgroup OPAMP OPAMP
203
  * @brief OPAMP module driver
204
  * @{
205
  */
206
 
207
#ifdef HAL_OPAMP_MODULE_ENABLED
208
 
209
#if defined (STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L151xE) || defined (STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX) || defined (STM32L162xC) || defined (STM32L152xC) || defined (STM32L151xC)
210
 
211
/* Private typedef -----------------------------------------------------------*/
212
/* Private define ------------------------------------------------------------*/
213
/* Private macro -------------------------------------------------------------*/
214
/* Private variables ---------------------------------------------------------*/
215
/* Private constants ---------------------------------------------------------*/
216
/* Private function prototypes -----------------------------------------------*/
217
/* Private functions ---------------------------------------------------------*/
218
/* Exported functions --------------------------------------------------------*/
219
 
220
/** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
221
  * @{
222
  */
223
 
224
/** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions
225
 *  @brief    Initialization and Configuration functions
226
 *
227
@verbatim    
228
  ==============================================================================
229
              ##### Initialization and de-initialization functions #####
230
  ==============================================================================
231
   [..]  This section provides functions allowing to:
232
 
233
@endverbatim
234
  * @{
235
  */
236
 
237
/**
238
  * @brief  Initializes the OPAMP according to the specified
239
  *         parameters in the OPAMP_InitTypeDef and create the associated handle.
240
  * @note   If the selected opamp is locked, initialization can't be performed.
241
  *         To unlock the configuration, perform a system reset.
242
  * @param  hopamp OPAMP handle
243
  * @retval HAL status
244
  */
245
HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef* hopamp)
246
{
247
  HAL_StatusTypeDef status = HAL_OK;
248
  uint32_t tmp_csr;       /* Temporary variable to update register CSR, except bits ANAWSSELx, S7SEL2, OPA_RANGE, OPAxCALOUT */
249
 
250
  /* Check the OPAMP handle allocation and lock status */
251
  /* Init not allowed if calibration is ongoing */
252
  if(hopamp == NULL)
253
  {
254
    return HAL_ERROR;
255
  }
256
  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
257
  {
258
    return HAL_ERROR;
259
  }
260
  else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
261
  {
262
    return HAL_ERROR;
263
  }
264
  else
265
  {
266
    /* Check the parameter */
267
    assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
268
 
269
    /* Set OPAMP parameters */
270
    assert_param(IS_OPAMP_POWER_SUPPLY_RANGE(hopamp->Init.PowerSupplyRange));
271
    assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode));
272
    assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode));
273
    assert_param(IS_OPAMP_NONINVERTING_INPUT_CHECK_INSTANCE(hopamp, hopamp->Init.NonInvertingInput));
274
    assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming));
275
 
276
    if(hopamp->State == HAL_OPAMP_STATE_RESET)
277
    {  
278
#if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
279
    if(hopamp->MspInitCallback == NULL)
280
    {
281
      hopamp->MspInitCallback               = HAL_OPAMP_MspInit;
282
    }
283
#endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
284
    }
285
 
286
 
287
    if (hopamp->Init.Mode != OPAMP_FOLLOWER_MODE)
288
    {
289
      assert_param(IS_OPAMP_INVERTING_INPUT(hopamp->Init.InvertingInput));
290
    }
291
 
292
    if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER)
293
    {
294
      if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL)
295
      {
296
        assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP));
297
        assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN));
298
      }
299
      else
300
      {
301
        assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValuePLowPower));
302
        assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueNLowPower));
303
      }
304
    }
305
 
306
    if(hopamp->State == HAL_OPAMP_STATE_RESET)
307
    {
308
      /* Allocate lock resource and initialize it */
309
      hopamp->Lock = HAL_UNLOCKED;
310
    }
311
 
312
#if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
313
    hopamp->MspInitCallback(hopamp);    
314
#else
315
    /* Call MSP init function */
316
    HAL_OPAMP_MspInit(hopamp);
317
#endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
318
 
319
    /* Set OPAMP parameters                                                   */
320
    /* - Set internal switches in function of:                                */
321
    /*   - OPAMP selected mode: standalone or follower.                       */
322
    /*   - Non-inverting input connection                                     */
323
    /*   - Inverting input connection                                         */
324
    /* - Set power supply range                                               */
325
    /* - Set power mode and associated calibration parameters                 */
326
 
327
    /* Get OPAMP CSR register into temporary variable */
328
    /* Note: OPAMP register CSR is written directly, independently of OPAMP   */
329
    /*       instance, because all OPAMP settings are dispatched in the same  */
330
    /*       register.                                                        */
331
    /*       Settings of bits for each OPAMP instances are managed case by    */
332
    /*       case using macro (OPAMP_CSR_S3SELX(), OPAMP_CSR_ANAWSELX(), ...) */
333
    tmp_csr = OPAMP->CSR;
334
 
335
    /* Open all switches on non-inverting input, inverting input and output   */
336
    /* feedback.                                                              */
337
    CLEAR_BIT(tmp_csr, OPAMP_CSR_ALL_SWITCHES(hopamp));
338
 
339
    /* Set internal switches in function of OPAMP mode selected: standalone   */
340
    /* or follower.                                                           */
341
    /* If follower mode is selected, feedback switch S3 is closed and         */
342
    /* inverting inputs switches are let opened.                              */
343
    /* If standalone mode is selected, feedback switch S3 is let opened and   */
344
    /* the selected inverting inputs switch is closed.                        */
345
    if (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE)
346
    {
347
      /* Follower mode: Close switches S3 and SanB */
348
      SET_BIT(tmp_csr, OPAMP_CSR_S3SELX(hopamp));
349
    }
350
    else
351
    {
352
      /* Set internal switches in function of inverting input selected:       */
353
      /* Close switch to connect OPAMP inverting input to the selected        */
354
      /* input: dedicated IO pin or alternative IO pin available on some      */
355
      /* device packages.                                                     */
356
      if (hopamp->Init.InvertingInput == OPAMP_INVERTINGINPUT_IO0)
357
      {
358
        /* Close switch to connect OPAMP non-inverting input to               */
359
        /* dedicated IO pin low-leakage.                                      */
360
        SET_BIT(tmp_csr, OPAMP_CSR_S4SELX(hopamp));
361
      }
362
      else
363
      {
364
        /* Close switch to connect OPAMP inverting input to alternative       */
365
        /* IO pin available on some device packages.                          */
366
        SET_BIT(tmp_csr, OPAMP_CSR_ANAWSELX(hopamp));
367
      }
368
    }
369
 
370
    /* Set internal switches in function of non-inverting input selected:     */
371
    /* Close switch to connect OPAMP non-inverting input to the selected      */
372
    /* input: dedicated IO pin or DAC channel.                                */
373
    if (hopamp->Init.NonInvertingInput == OPAMP_NONINVERTINGINPUT_IO0)
374
    {
375
      /* Close switch to connect OPAMP non-inverting input to                 */
376
      /* dedicated IO pin low-leakage.                                        */
377
      SET_BIT(tmp_csr, OPAMP_CSR_S5SELX(hopamp));
378
    }
379
    else if (hopamp->Init.NonInvertingInput == OPAMP_NONINVERTINGINPUT_DAC_CH1)
380
    {
381
 
382
      /* Particular case for connection to DAC channel 1:                     */
383
      /* OPAMP_NONINVERTINGINPUT_DAC_CH1 available on OPAMP1 and OPAMP2 only  */
384
      /* (OPAMP3 availability depends on device category).                    */
385
      if ((hopamp->Instance == OPAMP1) || (hopamp->Instance == OPAMP2))
386
      {
387
        /* Close switch to connect OPAMP non-inverting input to               */
388
        /* DAC channel 1.                                                     */
389
        SET_BIT(tmp_csr, OPAMP_CSR_S6SELX(hopamp));
390
      }
391
      else
392
      {
393
        /* Set HAL status to error if another OPAMP instance as OPAMP1 or     */
394
        /* OPAMP2 is intended to be connected to DAC channel 2.               */
395
        status = HAL_ERROR;
396
      }
397
    }
398
    else /* if (hopamp->Init.NonInvertingInput ==                             */
399
         /*     OPAMP_NONINVERTINGINPUT_DAC_CH2  )                            */
400
    {
401
      /* Particular case for connection to DAC channel 2:                     */
402
      /* OPAMP_NONINVERTINGINPUT_DAC_CH2 available on OPAMP2 and OPAMP3 only  */
403
      /* (OPAMP3 availability depends on device category).                    */
404
      if (hopamp->Instance == OPAMP2)
405
      {
406
        /* Close switch to connect OPAMP non-inverting input to               */
407
        /* DAC channel 2.                                                     */
408
        SET_BIT(tmp_csr, OPAMP_CSR_S7SEL2);
409
      }
410
      /* If OPAMP3 is selected (if available) */
411
      else if (hopamp->Instance != OPAMP1)
412
      {
413
        /* Close switch to connect OPAMP non-inverting input to               */
414
        /* DAC channel 2.                                                     */
415
        SET_BIT(tmp_csr, OPAMP_CSR_S6SELX(hopamp));
416
      }
417
      else
418
      {
419
        /* Set HAL status to error if another OPAMP instance as OPAMP2 or     */
420
        /* OPAMP3 (if available) is intended to be connected to DAC channel 2.*/
421
        status = HAL_ERROR;
422
      }
423
    }
424
 
425
    /* Continue OPAMP configuration if settings of switches are correct */
426
    if (status != HAL_ERROR)
427
    {
428
      /* Set power mode and associated calibration parameters */
429
      if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER)
430
      {
431
        /* Set normal mode */
432
        CLEAR_BIT(tmp_csr, OPAMP_CSR_OPAXLPM(hopamp));
433
 
434
        if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER)
435
        {
436
          /* Set calibration mode (factory or user) and values for            */
437
          /* transistors differential pair high (PMOS) and low (NMOS) for     */
438
          /* normal mode.                                                     */
439
          MODIFY_REG(OPAMP->OTR, OPAMP_OTR_OT_USER                                                                     |
440
                                 OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, OPAMP_TRIM_VALUE_MASK)       |
441
                                 OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, OPAMP_TRIM_VALUE_MASK)        ,
442
                                 hopamp->Init.UserTrimming                                                             |
443
                                 OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, hopamp->Init.TrimmingValueN) |
444
                                 OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, hopamp->Init.TrimmingValueP)  );
445
        }
446
        else
447
        {
448
          /* Set calibration mode to factory */
449
          CLEAR_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER);
450
        }
451
 
452
      }
453
      else
454
      {
455
        /* Set low power mode */
456
        SET_BIT(tmp_csr, OPAMP_CSR_OPAXLPM(hopamp));
457
 
458
        if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER)
459
        {
460
          /* Set calibration mode to user trimming */
461
          SET_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER);
462
 
463
          /* Set values for transistors differential pair high (PMOS) and low */
464
          /* (NMOS) for low power mode.                                       */
465
          MODIFY_REG(OPAMP->LPOTR, OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, OPAMP_TRIM_VALUE_MASK)               |
466
                                   OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, OPAMP_TRIM_VALUE_MASK)                ,
467
                                   OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_N, hopamp->Init.TrimmingValueNLowPower) |
468
                                   OPAMP_OFFSET_TRIM_SET(hopamp, OPAMP_FACTORYTRIMMING_P, hopamp->Init.TrimmingValuePLowPower)  );
469
        }
470
        else
471
        {
472
          /* Set calibration mode to factory trimming */
473
          CLEAR_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER);
474
        }
475
 
476
      }
477
 
478
 
479
      /* Configure the power supply range */
480
      MODIFY_REG(tmp_csr, OPAMP_CSR_AOP_RANGE,
481
                          hopamp->Init.PowerSupplyRange);
482
 
483
      /* Set OPAMP CSR register from temporary variable */
484
      /* This allows to apply all changes on one time, in case of update on   */
485
      /* the fly with OPAMP previously set and running:                       */
486
      /*  - to avoid hazardous transient switches settings (risk of short     */
487
      /*    circuit)                                                          */
488
      /*  - to avoid interruption of input signal                             */
489
      OPAMP->CSR = tmp_csr;
490
 
491
 
492
      /* Update the OPAMP state */
493
      /* If coming from state reset: Update from state RESET to state READY */
494
      if (hopamp->State == HAL_OPAMP_STATE_RESET)
495
      {
496
        hopamp->State = HAL_OPAMP_STATE_READY;
497
      }
498
      /* else: OPAMP state remains READY or BUSY state (no update) */
499
    }
500
  }
501
 
502
  return status;
503
}
504
 
505
/**
506
  * @brief  DeInitializes the OPAMP peripheral
507
  * @note   Deinitialization can be performed if the OPAMP configuration is locked.
508
  *         (the OPAMP lock is SW in STM32L1)
509
  * @param  hopamp OPAMP handle
510
  * @retval HAL status
511
  */
512
HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef* hopamp)
513
{
514
  HAL_StatusTypeDef status = HAL_OK;
515
 
516
  /* Check the OPAMP handle allocation */
517
  /* DeInit not allowed if calibration is ongoing */
518
  if(hopamp == NULL)
519
  {
520
    status = HAL_ERROR;
521
  }
522
  else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
523
  {
524
    status = HAL_ERROR;
525
  }
526
  else
527
  {
528
    /* Check the parameter */
529
    assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
530
 
531
    /* Disable the selected opamp */
532
    SET_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp));
533
 
534
    /* Open all switches on non-inverting input, inverting input and output   */
535
    /* feedback.                                                              */
536
    /* Note: OPAMP register CSR is written directly, independently of OPAMP   */
537
    /*       instance, because all OPAMP settings are dispatched in the same  */
538
    /*       register.                                                        */
539
    /*       Settings of bits for each OPAMP instances are managed case by    */
540
    /*       case using macro (OPAMP_CSR_S3SELX(), OPAMP_CSR_ANAWSELX(), ...) */
541
    CLEAR_BIT(OPAMP->CSR, OPAMP_CSR_ALL_SWITCHES(hopamp));
542
 
543
    /* Note: Registers and bits shared with other OPAMP instances are kept    */
544
    /*       unchanged, to not impact other OPAMP while operating on the      */
545
    /*       selected OPAMP.                                                  */
546
    /*       Unchanged: bit OPAMP_OTR_OT_USER (parameter "UserTrimming")      */
547
    /*                  bit OPAMP_CSR_AOP_RANGE (parameter "PowerSupplyRange")*/
548
 
549
#if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
550
  if(hopamp->MspDeInitCallback == NULL)
551
  {
552
    hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
553
  }
554
  /* DeInit the low level hardware */
555
  hopamp->MspDeInitCallback(hopamp);
556
#else
557
    /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
558
    HAL_OPAMP_MspDeInit(hopamp);
559
#endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
560
 
561
    /* Update the OPAMP state*/
562
    hopamp->State = HAL_OPAMP_STATE_RESET;
563
  }
564
 
565
  /* Process unlocked */
566
  __HAL_UNLOCK(hopamp);
567
 
568
  return status;
569
}
570
 
571
/**
572
  * @brief  Initialize the OPAMP MSP.
573
  * @param  hopamp OPAMP handle
574
  * @retval None
575
  */
576
__weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef* hopamp)
577
{
578
  /* Prevent unused argument(s) compilation warning */
579
  UNUSED(hopamp);
580
 
581
  /* NOTE : This function should not be modified, when the callback is needed,
582
            the function "HAL_OPAMP_MspInit()" must be implemented in the user file.
583
  */
584
}
585
 
586
/**
587
  * @brief  DeInitialize OPAMP MSP.
588
  * @param  hopamp OPAMP handle
589
  * @retval None
590
  */
591
__weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef* hopamp)
592
{
593
  /* Prevent unused argument(s) compilation warning */
594
  UNUSED(hopamp);
595
 
596
  /* NOTE : This function should not be modified, when the callback is needed,
597
            the function "HAL_OPAMP_MspDeInit()" must be implemented in the user file.
598
  */
599
}
600
 
601
/**
602
  * @}
603
  */
604
 
605
 
606
/** @defgroup OPAMP_Exported_Functions_Group2 IO operation functions
607
  * @brief   IO operation functions
608
  *
609
@verbatim  
610
 ===============================================================================
611
                        ##### IO operation functions #####
612
 ===============================================================================
613
    [..]
614
    This subsection provides a set of functions allowing to manage the OPAMP
615
    start, stop and calibration actions.
616
 
617
@endverbatim
618
  * @{
619
  */
620
 
621
/**
622
  * @brief  Start the OPAMP.
623
  * @param  hopamp OPAMP handle
624
  * @retval HAL status
625
  */
626
 
627
HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef* hopamp)
628
{
629
  HAL_StatusTypeDef status = HAL_OK;
630
 
631
  /* Check the OPAMP handle allocation */
632
  /* Check if OPAMP locked */
633
  if(hopamp == NULL)
634
  {
635
    status = HAL_ERROR;
636
  }
637
  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
638
  {
639
    status = HAL_ERROR;
640
  }
641
  else
642
  {
643
    /* Check the parameter */
644
    assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
645
 
646
    if(hopamp->State == HAL_OPAMP_STATE_READY)
647
    {
648
      /* Enable the selected opamp */
649
      CLEAR_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp));
650
 
651
      /* Update the OPAMP state */
652
      /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
653
      hopamp->State = HAL_OPAMP_STATE_BUSY;  
654
    }
655
    else
656
    {
657
      status = HAL_ERROR;
658
    }
659
 
660
   }
661
  return status;
662
}
663
 
664
/**
665
  * @brief  Stop the OPAMP.
666
  * @param  hopamp OPAMP handle
667
  * @retval HAL status
668
  */
669
HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef* hopamp)
670
{
671
  HAL_StatusTypeDef status = HAL_OK;
672
 
673
  /* Check the OPAMP handle allocation */
674
  /* Check if OPAMP locked */
675
  /* Check if OPAMP calibration ongoing */
676
  if(hopamp == NULL)
677
  {
678
    status = HAL_ERROR;
679
  }
680
  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
681
  {
682
    status = HAL_ERROR;
683
  }
684
  else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
685
  {
686
    status = HAL_ERROR;
687
  }
688
  else
689
  {
690
    /* Check the parameter */
691
    assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
692
 
693
    if(hopamp->State == HAL_OPAMP_STATE_BUSY)
694
    {
695
      /* Disable the selected opamp */
696
      SET_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp));
697
 
698
      /* Update the OPAMP state*/    
699
      /* From  HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
700
      hopamp->State = HAL_OPAMP_STATE_READY;
701
    }
702
    else
703
    {
704
      status = HAL_ERROR;
705
    }
706
  }
707
  return status;
708
}
709
 
710
/**
711
  * @brief  Run the self calibration of one OPAMP.
712
  * @note   Trimming values (PMOS & NMOS) are updated and user trimming is
713
  *         enabled if calibration is succesful.
714
  * @note   Calibration is performed in the mode specified in OPAMP init
715
  *         structure (mode normal or low-power). To perform calibration for
716
  *         both modes, repeat this function twice after OPAMP init structure
717
  *         accordingly updated.
718
  * @note   Calibration runs about 10 ms.
719
  * @param  hopamp handle
720
  * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled
721
  * @retval HAL status
722
  */
723
HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef* hopamp)
724
{
725
  HAL_StatusTypeDef status = HAL_OK;
726
 
727
  uint32_t* opamp_trimmingvalue;
728
  uint32_t opamp_trimmingvaluen = 0;
729
  uint32_t opamp_trimmingvaluep = 0;
730
 
731
  uint32_t trimming_diff_pair;               /* Selection of differential transistors pair high or low */
732
 
733
  __IO uint32_t* tmp_opamp_reg_trimming;     /* Selection of register of trimming depending on power mode: OTR or LPOTR */
734
  uint32_t tmp_opamp_otr_otuser;             /* Selection of bit OPAMP_OTR_OT_USER depending on trimming register pointed: OTR or LPOTR */
735
 
736
  uint32_t tmp_Opaxcalout_DefaultSate;       /* Bit OPAMP_CSR_OPAXCALOUT default state when trimming value is 00000b. Used to detect the bit toggling */
737
 
738
  uint32_t tmp_OpaxSwitchesContextBackup;
739
 
740
  uint8_t trimming_diff_pair_iteration_count;          /* For calibration loop algorithm: to repeat the calibration loop for both differential transistors pair high and low */
741
  uint8_t delta;                                       /* For calibration loop algorithm: Variable for dichotomy steps value */
742
  uint8_t final_step_check = 0x0U;                        /* For calibration loop algorithm: Flag for additional check of last trimming step */
743
 
744
  /* Check the OPAMP handle allocation */
745
  /* Check if OPAMP locked */
746
  if(hopamp == NULL)
747
  {
748
    status = HAL_ERROR;
749
  }
750
  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
751
  {
752
    status = HAL_ERROR;
753
  }
754
  else
755
  {
756
 
757
    /* Check if OPAMP in calibration mode and calibration not yet enable */
758
    if(hopamp->State == HAL_OPAMP_STATE_READY)
759
    {
760
      /* Check the parameter */
761
      assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
762
      assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode));
763
 
764
      /* Update OPAMP state */
765
      hopamp->State = HAL_OPAMP_STATE_CALIBBUSY;
766
 
767
      /* Backup of switches configuration to restore it at the end of the     */
768
      /* calibration.                                                         */
769
      tmp_OpaxSwitchesContextBackup = READ_BIT(OPAMP->CSR, OPAMP_CSR_ALL_SWITCHES(hopamp));
770
 
771
      /* Open all switches on non-inverting input, inverting input and output */
772
      /* feedback.                                                            */
773
      CLEAR_BIT(OPAMP->CSR, OPAMP_CSR_ALL_SWITCHES(hopamp));
774
 
775
      /* Set calibration mode to user programmed trimming values */
776
      SET_BIT(OPAMP->OTR, OPAMP_OTR_OT_USER);
777
 
778
 
779
      /* Select trimming settings depending on power mode */
780
      if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL)
781
      {
782
        tmp_opamp_otr_otuser = OPAMP_OTR_OT_USER;
783
        tmp_opamp_reg_trimming = &OPAMP->OTR;
784
      }
785
      else
786
      {
787
        tmp_opamp_otr_otuser = 0x00000000U;
788
        tmp_opamp_reg_trimming = &OPAMP->LPOTR;
789
      }
790
 
791
 
792
      /* Enable the selected opamp */
793
      CLEAR_BIT (OPAMP->CSR, OPAMP_CSR_OPAXPD(hopamp));
794
 
795
      /* Perform trimming for both differential transistors pair high and low */
796
      for (trimming_diff_pair_iteration_count = 0U; trimming_diff_pair_iteration_count <=1U; trimming_diff_pair_iteration_count++)
797
      {
798
        if (trimming_diff_pair_iteration_count == 0U)
799
        {
800
          /* Calibration of transistors differential pair high (NMOS) */
801
          trimming_diff_pair = OPAMP_FACTORYTRIMMING_N;
802
          opamp_trimmingvalue = &opamp_trimmingvaluen;
803
 
804
          /* Set bit OPAMP_CSR_OPAXCALOUT default state when trimming value   */
805
          /* is 00000b. Used to detect the bit toggling during trimming.      */
806
          tmp_Opaxcalout_DefaultSate = 0U;
807
 
808
          /* Enable calibration for N differential pair */
809
          MODIFY_REG(OPAMP->CSR, OPAMP_CSR_OPAXCAL_L(hopamp),
810
                                 OPAMP_CSR_OPAXCAL_H(hopamp) );
811
        }
812
        else /* (trimming_diff_pair_iteration_count == 1) */
813
        {
814
          /* Calibration of transistors differential pair low (PMOS) */
815
          trimming_diff_pair = OPAMP_FACTORYTRIMMING_P;
816
          opamp_trimmingvalue = &opamp_trimmingvaluep;
817
 
818
          /* Set bit OPAMP_CSR_OPAXCALOUT default state when trimming value   */
819
          /* is 00000b. Used to detect the bit toggling during trimming.      */
820
          tmp_Opaxcalout_DefaultSate = OPAMP_CSR_OPAXCALOUT(hopamp);
821
 
822
          /* Enable calibration for P differential pair */
823
          MODIFY_REG(OPAMP->CSR, OPAMP_CSR_OPAXCAL_H(hopamp),
824
                                 OPAMP_CSR_OPAXCAL_L(hopamp) );
825
        }
826
 
827
 
828
        /* Perform calibration parameter search by dichotomy sweep */
829
        /*  - Delta initial value 16: for 5 dichotomy steps: 16 for the       */
830
        /*    initial range, then successive delta sweeps (8, 4, 2, 1).       */
831
        /*    can extend the search range to +/- 15 units.                    */
832
        /*  - Trimming initial value 15: search range will go from 0 to 30    */
833
        /*    (Trimming value 31 is forbidden).                               */
834
        /* Note: After dichotomy sweep, the trimming result is determined.    */
835
        /*       However, the final trimming step is deduced from previous    */
836
        /*       trimming steps tested but is not effectively tested.         */
837
        /*       An additional test step (using variable "final_step_check")  */
838
        /*       allow to Test the final trimming step.                       */
839
        *opamp_trimmingvalue = 15U;
840
        delta = 16U;
841
 
842
        while ((delta != 0U) || (final_step_check == 1U))
843
        {
844
          /* Set candidate trimming */
845
          MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, OPAMP_TRIM_VALUE_MASK) ,
846
                                              OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, *opamp_trimmingvalue) | tmp_opamp_otr_otuser);
847
 
848
          /* Offset trimming time: during calibration, minimum time needed    */
849
          /* between two steps to have 1 mV accuracy.                         */
850
          HAL_Delay(OPAMP_TRIMMING_DELAY);
851
 
852
          /* Set flag for additional check of last trimming step equal to     */
853
          /* dichotomy step before its division by 2 (equivalent to previous  */
854
          /* value of dichotomy step).                                        */
855
          final_step_check = delta;
856
 
857
          /* Divide range by 2 to continue dichotomy sweep */
858
          delta >>= 1;
859
 
860
          /* Set trimming values for next iteration in function of trimming   */
861
          /* result toggle (versus initial state).                            */
862
          /* Note: on the last trimming loop, delta is equal to 0 and         */
863
          /*       therefore has no effect.                                   */
864
          if (READ_BIT(OPAMP->CSR, OPAMP_CSR_OPAXCALOUT(hopamp)) != tmp_Opaxcalout_DefaultSate)
865
          {
866
            /* If calibration output is has toggled, try lower trimming */
867
            *opamp_trimmingvalue -= delta;
868
          }
869
          else
870
          {
871
            /* If calibration output is has not toggled, try higher trimming */
872
            *opamp_trimmingvalue += delta;
873
          }
874
 
875
        }
876
 
877
        /* Check trimming result of the selected step and perform final fine  */
878
        /* trimming.                                                          */
879
        /*  - If calibration output is has toggled: the current step is       */
880
        /*    already optimized.                                              */
881
        /*  - If calibration output is has not toggled: the current step can  */
882
        /*    be optimized by incrementing it of one step.                    */
883
        if (READ_BIT(OPAMP->CSR, OPAMP_CSR_OPAXCALOUT(hopamp)) == tmp_Opaxcalout_DefaultSate)
884
        {
885
          *opamp_trimmingvalue += 1U;
886
 
887
          /* Set final fine trimming */
888
          MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, OPAMP_TRIM_VALUE_MASK) ,
889
                                              OPAMP_OFFSET_TRIM_SET(hopamp, trimming_diff_pair, *opamp_trimmingvalue) | tmp_opamp_otr_otuser);
890
        }
891
 
892
      }
893
 
894
 
895
      /* Disable calibration for P and N differential pairs */
896
      /* Disable the selected opamp */
897
      CLEAR_BIT (OPAMP->CSR, (OPAMP_CSR_OPAXCAL_H(hopamp) |
898
                              OPAMP_CSR_OPAXCAL_L(hopamp) |
899
                              OPAMP_CSR_OPAXPD(hopamp))    );
900
 
901
      /* Backup of switches configuration to restore it at the end of the     */
902
      /* calibration.                                                         */
903
      SET_BIT(OPAMP->CSR, tmp_OpaxSwitchesContextBackup);
904
 
905
      /* Self calibration is successful */
906
      /* Store calibration (user trimming) results in init structure. */
907
 
908
      /* Set user trimming mode */  
909
      hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER;
910
 
911
      /* Affect calibration parameters depending on mode normal/low power */
912
      if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER)
913
      {
914
        /* Write calibration result N */
915
        hopamp->Init.TrimmingValueN = opamp_trimmingvaluen;
916
        /* Write calibration result P */
917
        hopamp->Init.TrimmingValueP = opamp_trimmingvaluep;
918
      }
919
      else
920
      {
921
        /* Write calibration result N */
922
        hopamp->Init.TrimmingValueNLowPower = opamp_trimmingvaluen;
923
        /* Write calibration result P */
924
        hopamp->Init.TrimmingValuePLowPower = opamp_trimmingvaluep;
925
      }
926
 
927
      /* Update OPAMP state */
928
      hopamp->State = HAL_OPAMP_STATE_READY;
929
 
930
    }
931
 
932
    else
933
    {
934
      /* OPAMP can not be calibrated from this mode */
935
      status = HAL_ERROR;
936
    }
937
  }
938
 
939
  return status;
940
}
941
 
942
/**
943
  * @}
944
  */
945
 
946
/**
947
  * @}
948
  */
949
 
950
/** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions
951
 *  @brief   Peripheral Control functions
952
 *
953
@verbatim  
954
 ===============================================================================
955
                      ##### Peripheral Control functions #####
956
 ===============================================================================  
957
    [..]
958
    This subsection provides a set of functions allowing to control the OPAMP data
959
    transfers.
960
 
961
 
962
 
963
@endverbatim
964
  * @{
965
  */
966
 
967
/**
968
  * @brief  Lock the selected opamp configuration.
969
  *         Caution: On STM32L1, HAL OPAMP lock is software lock only
970
  *         (not hardware lock as available on some other STM32 devices)
971
  * @param  hopamp OPAMP handle
972
  * @retval HAL status
973
  */
974
HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef* hopamp)
975
{
976
  HAL_StatusTypeDef status = HAL_OK;
977
 
978
  /* Check the OPAMP handle allocation */
979
  /* Check if OPAMP locked */
980
  /* OPAMP can be locked when enabled and running in normal mode */
981
  /*   It is meaningless otherwise */
982
  if(hopamp == NULL)
983
  {
984
    status = HAL_ERROR;
985
  }
986
  else if(hopamp->State == HAL_OPAMP_STATE_BUSY)
987
  {
988
    /* Check the parameter */
989
    assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
990
 
991
   /* OPAMP state changed to locked */
992
    hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED;
993
  }
994
  else
995
  {
996
    status = HAL_ERROR;
997
  }
998
  return status;
999
}
1000
 
1001
/**
1002
  * @brief  Return the OPAMP factory trimming value
1003
  *         Caution: On STM32L1 OPAMP, user can retrieve factory trimming if
1004
  *                  OPAMP has never been set to user trimming before.
1005
  *                  Therefore, this fonction must be called when OPAMP init  
1006
  *                  parameter "UserTrimming" is set to trimming factory,
1007
  *                  and before OPAMP  calibration (function
1008
  *                  "HAL_OPAMP_SelfCalibrate()").
1009
  *                  Otherwise, factory triming value cannot be retrieved and
1010
  *                  error status is returned.
1011
  * @param  hopamp  OPAMP handle
1012
  * @param  trimmingoffset  Trimming offset (P or N)
1013
  *         This parameter must be a value of @ref OPAMP_FactoryTrimming
1014
  * @note   Calibration parameter retrieved is corresponding to the mode
1015
  *         specified in OPAMP init structure (mode normal or low-power).
1016
  *         To retrieve calibration parameters for both modes, repeat this
1017
  *         function after OPAMP init structure accordingly updated.
1018
  * @retval Trimming value (P or N) range: 0->31
1019
  *         or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available
1020
  *
1021
  */
1022
HAL_OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset (OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset)
1023
{
1024
  HAL_OPAMP_TrimmingValueTypeDef trimmingvalue;
1025
  __IO uint32_t* tmp_opamp_reg_trimming;  /* Selection of register of trimming depending on power mode: OTR or LPOTR */
1026
 
1027
  /* Check the OPAMP handle allocation */
1028
  /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
1029
  if(hopamp == NULL)
1030
  {
1031
    return OPAMP_FACTORYTRIMMING_DUMMY;
1032
  }
1033
 
1034
  /* Check the OPAMP handle allocation */
1035
  /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
1036
  if(hopamp->State == HAL_OPAMP_STATE_READY)
1037
  {
1038
    /* Check the parameter */
1039
    assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
1040
    assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset));
1041
    assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode));
1042
 
1043
    /* Check the trimming mode */
1044
    if (hopamp->Init.UserTrimming == OPAMP_TRIMMING_USER)
1045
    {
1046
      /* This fonction must called when OPAMP init parameter "UserTrimming"   */
1047
      /* is set to trimming factory, and before OPAMP calibration (function   */
1048
      /* "HAL_OPAMP_SelfCalibrate()").                                        */
1049
      /* Otherwise, factory triming value cannot be retrieved and error       */
1050
      /* status is returned.                                                  */
1051
      trimmingvalue = OPAMP_FACTORYTRIMMING_DUMMY;
1052
    }
1053
    else
1054
    {
1055
      /* Select trimming settings depending on power mode */
1056
      if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL)
1057
      {
1058
        tmp_opamp_reg_trimming = &OPAMP->OTR;
1059
      }
1060
      else
1061
      {
1062
        tmp_opamp_reg_trimming = &OPAMP->LPOTR;
1063
      }
1064
 
1065
      /* Get factory trimming  */
1066
      trimmingvalue = ((*tmp_opamp_reg_trimming >> OPAMP_OFFSET_TRIM_BITSPOSITION(hopamp, trimmingoffset)) & OPAMP_TRIM_VALUE_MASK);
1067
    }
1068
  }
1069
  else
1070
  {
1071
    return OPAMP_FACTORYTRIMMING_DUMMY;
1072
  }  
1073
  return trimmingvalue;
1074
}
1075
 
1076
/**
1077
  * @}
1078
  */
1079
 
1080
 
1081
/** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions
1082
 *  @brief   Peripheral State functions
1083
 *
1084
@verbatim  
1085
 ===============================================================================
1086
                      ##### Peripheral State functions #####
1087
 ===============================================================================
1088
    [..]
1089
    This subsection permits to get in run-time the status of the peripheral.
1090
 
1091
@endverbatim
1092
  * @{
1093
  */
1094
 
1095
/**
1096
  * @brief  Return the OPAMP handle state.
1097
  * @param  hopamp OPAMP handle
1098
  * @retval HAL state
1099
  */
1100
HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef* hopamp)
1101
{
1102
  /* Check the OPAMP handle allocation */
1103
  if(hopamp == NULL)
1104
  {
1105
    return HAL_OPAMP_STATE_RESET;
1106
  }
1107
 
1108
  /* Check the parameter */
1109
  assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
1110
 
1111
  return hopamp->State;
1112
}
1113
 
1114
#if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
1115
/**
1116
  * @brief  Register a User OPAMP Callback
1117
  *         To be used instead of the weak (surcharged) predefined callback
1118
  * @param hopamp OPAMP handle
1119
  * @param CallbackID ID of the callback to be registered
1120
  *        This parameter can be one of the following values:
1121
  *          @arg @ref HAL_OPAMP_MSPINIT_CB_ID       OPAMP MspInit callback ID
1122
  *          @arg @ref HAL_OPAMP_MSPDEINIT_CB_ID     OPAMP MspDeInit callback ID  
1123
  * @param pCallback pointer to the Callback function
1124
  * @retval status
1125
  */
1126
HAL_StatusTypeDef HAL_OPAMP_RegisterCallback (OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID, pOPAMP_CallbackTypeDef pCallback)
1127
{
1128
  HAL_StatusTypeDef status = HAL_OK;
1129
 
1130
  if(pCallback == NULL)
1131
  {
1132
    return HAL_ERROR;
1133
  }
1134
 
1135
  /* Process locked */
1136
  __HAL_LOCK(hopamp);
1137
 
1138
  if(hopamp->State == HAL_OPAMP_STATE_READY)
1139
  {
1140
    switch (CallbackID)
1141
    {
1142
    case HAL_OPAMP_MSPINIT_CB_ID :
1143
      hopamp->MspInitCallback = pCallback;
1144
      break;
1145
    case HAL_OPAMP_MSPDEINIT_CB_ID :
1146
      hopamp->MspDeInitCallback = pCallback;
1147
      break;
1148
    default :
1149
      /* Update the error code */
1150
      // hopamp->ErrorCode |= HAL_OPAMP_ERROR_INVALID_CALLBACK; 
1151
      /* update return status */
1152
      status =  HAL_ERROR;
1153
      break;
1154
    }
1155
  }
1156
  else if (hopamp->State == HAL_OPAMP_STATE_RESET)
1157
  {
1158
    switch (CallbackID)
1159
    {
1160
    case HAL_OPAMP_MSPINIT_CB_ID :
1161
      hopamp->MspInitCallback = pCallback;
1162
      break;
1163
    case HAL_OPAMP_MSPDEINIT_CB_ID :
1164
      hopamp->MspDeInitCallback = pCallback;
1165
      break;
1166
    default :
1167
      /* Update the error code */
1168
      // hopamp->ErrorCode |= HAL_OPAMP_ERROR_INVALID_CALLBACK; 
1169
      /* update return status */
1170
      status =  HAL_ERROR;
1171
      break;
1172
    }
1173
  }
1174
  else
1175
  {
1176
    /* update return status */
1177
    status =  HAL_ERROR;
1178
  }
1179
 
1180
  /* Release Lock */
1181
  __HAL_UNLOCK(hopamp);
1182
  return status;
1183
}
1184
 
1185
/**
1186
  * @brief  Unregister a User OPAMP Callback
1187
  *         OPAMP Callback is redirected to the weak (surcharged) predefined callback
1188
  * @param hopamp OPAMP handle
1189
  * @param CallbackID ID of the callback to be unregistered
1190
  *        This parameter can be one of the following values:
1191
  *          @arg @ref HAL_OPAMP_MSPINIT_CB_ID              OPAMP MSP Init Callback ID
1192
  *          @arg @ref HAL_OPAMP_MSPDEINIT_CB_ID            OPAMP MSP DeInit Callback ID
1193
  *          @arg @ref HAL_OPAMP_ALL_CB_ID                   OPAMP All Callbacks
1194
  * @retval status
1195
  */
1196
 
1197
HAL_StatusTypeDef HAL_OPAMP_UnRegisterCallback (OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackID)
1198
{
1199
  HAL_StatusTypeDef status = HAL_OK;
1200
 
1201
  /* Process locked */
1202
  __HAL_LOCK(hopamp);
1203
 
1204
  if(hopamp->State == HAL_OPAMP_STATE_READY)
1205
  {
1206
    switch (CallbackID)
1207
    {    
1208
      case HAL_OPAMP_MSPINIT_CB_ID :
1209
      hopamp->MspInitCallback = HAL_OPAMP_MspInit;
1210
      break;
1211
    case HAL_OPAMP_MSPDEINIT_CB_ID :
1212
      hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
1213
      break;
1214
    case HAL_OPAMP_ALL_CB_ID :
1215
      hopamp->MspInitCallback = HAL_OPAMP_MspInit;
1216
      hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
1217
      break;
1218
    default :
1219
      /* update return status */
1220
      status =  HAL_ERROR;
1221
      break;
1222
    }
1223
  }
1224
  else if (hopamp->State == HAL_OPAMP_STATE_RESET)
1225
  {
1226
    switch (CallbackID)
1227
    {
1228
    case HAL_OPAMP_MSPINIT_CB_ID :
1229
      hopamp->MspInitCallback = HAL_OPAMP_MspInit;
1230
      break;
1231
    case HAL_OPAMP_MSPDEINIT_CB_ID :
1232
      hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
1233
      break;
1234
    default :
1235
      /* update return status */
1236
      status =  HAL_ERROR;
1237
      break;
1238
    }
1239
  }
1240
  else
1241
  {
1242
    /* update return status */
1243
    status =  HAL_ERROR;
1244
  }
1245
 
1246
  /* Release Lock */
1247
  __HAL_UNLOCK(hopamp);
1248
  return status;
1249
}
1250
 
1251
#endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
1252
/**
1253
  * @}
1254
  */
1255
 
1256
/**
1257
  * @}
1258
  */
1259
 
1260
#endif /* STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX || STM32L162xC || STM32L152xC || STM32L151xC */
1261
 
1262
#endif /* HAL_OPAMP_MODULE_ENABLED */
1263
/**
1264
  * @}
1265
  */
1266
 
1267
/**
1268
  * @}
1269
  */
1270
 
1271
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/