mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/cmsis/TARGET_STM/TARGET_NUCLEO_F334R8/stm32f3xx_hal_opamp_ex.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 237:f3da66175598
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 237:f3da66175598 | 1 | /** |
mbed_official | 237:f3da66175598 | 2 | ****************************************************************************** |
mbed_official | 237:f3da66175598 | 3 | * @file stm32f3xx_hal_opamp_ex.c |
mbed_official | 237:f3da66175598 | 4 | * @author MCD Application Team |
mbed_official | 237:f3da66175598 | 5 | * @version V1.0.1 |
mbed_official | 237:f3da66175598 | 6 | * @date 18-June-2014 |
mbed_official | 237:f3da66175598 | 7 | * @brief Extended OPAMP HAL module driver. |
mbed_official | 237:f3da66175598 | 8 | * |
mbed_official | 237:f3da66175598 | 9 | * This file provides firmware functions to manage the following |
mbed_official | 237:f3da66175598 | 10 | * functionalities of the Power Controller (OPAMP) peripheral: |
mbed_official | 237:f3da66175598 | 11 | * + Extended Initialization and de-initialization functions |
mbed_official | 237:f3da66175598 | 12 | * + Extended Peripheral Control functions |
mbed_official | 237:f3da66175598 | 13 | * |
mbed_official | 237:f3da66175598 | 14 | @verbatim |
mbed_official | 237:f3da66175598 | 15 | ****************************************************************************** |
mbed_official | 237:f3da66175598 | 16 | * @attention |
mbed_official | 237:f3da66175598 | 17 | * |
mbed_official | 237:f3da66175598 | 18 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
mbed_official | 237:f3da66175598 | 19 | * |
mbed_official | 237:f3da66175598 | 20 | * Redistribution and use in source and binary forms, with or without modification, |
mbed_official | 237:f3da66175598 | 21 | * are permitted provided that the following conditions are met: |
mbed_official | 237:f3da66175598 | 22 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 237:f3da66175598 | 23 | * this list of conditions and the following disclaimer. |
mbed_official | 237:f3da66175598 | 24 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 237:f3da66175598 | 25 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 237:f3da66175598 | 26 | * and/or other materials provided with the distribution. |
mbed_official | 237:f3da66175598 | 27 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 237:f3da66175598 | 28 | * may be used to endorse or promote products derived from this software |
mbed_official | 237:f3da66175598 | 29 | * without specific prior written permission. |
mbed_official | 237:f3da66175598 | 30 | * |
mbed_official | 237:f3da66175598 | 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 237:f3da66175598 | 32 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 237:f3da66175598 | 33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 237:f3da66175598 | 34 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 237:f3da66175598 | 35 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 237:f3da66175598 | 36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 237:f3da66175598 | 37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 237:f3da66175598 | 38 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 237:f3da66175598 | 39 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 237:f3da66175598 | 40 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 237:f3da66175598 | 41 | * |
mbed_official | 237:f3da66175598 | 42 | ****************************************************************************** |
mbed_official | 237:f3da66175598 | 43 | */ |
mbed_official | 237:f3da66175598 | 44 | |
mbed_official | 237:f3da66175598 | 45 | /* Includes ------------------------------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 46 | #include "stm32f3xx_hal.h" |
mbed_official | 237:f3da66175598 | 47 | |
mbed_official | 237:f3da66175598 | 48 | /** @addtogroup STM32F3xx_HAL_Driver |
mbed_official | 237:f3da66175598 | 49 | * @{ |
mbed_official | 237:f3da66175598 | 50 | */ |
mbed_official | 237:f3da66175598 | 51 | |
mbed_official | 237:f3da66175598 | 52 | /** @defgroup OPAMPEx |
mbed_official | 237:f3da66175598 | 53 | * @brief OPAMP Extended HAL module driver. |
mbed_official | 237:f3da66175598 | 54 | * @{ |
mbed_official | 237:f3da66175598 | 55 | */ |
mbed_official | 237:f3da66175598 | 56 | |
mbed_official | 237:f3da66175598 | 57 | #ifdef HAL_OPAMP_MODULE_ENABLED |
mbed_official | 237:f3da66175598 | 58 | |
mbed_official | 237:f3da66175598 | 59 | /* Private typedef -----------------------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 60 | /* Private define ------------------------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 61 | /* Private macro -------------------------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 62 | /* Private variables ---------------------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 63 | /* Private function prototypes -----------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 64 | /* Private functions ---------------------------------------------------------*/ |
mbed_official | 237:f3da66175598 | 65 | |
mbed_official | 237:f3da66175598 | 66 | /** @defgroup OPAMPEx_Private_Functions |
mbed_official | 237:f3da66175598 | 67 | * @{ |
mbed_official | 237:f3da66175598 | 68 | */ |
mbed_official | 237:f3da66175598 | 69 | |
mbed_official | 237:f3da66175598 | 70 | /** @defgroup OPAMPEx_Group1 Extended I/O operation functions |
mbed_official | 237:f3da66175598 | 71 | * @brief Extended Self calibration functions |
mbed_official | 237:f3da66175598 | 72 | * |
mbed_official | 237:f3da66175598 | 73 | @verbatim |
mbed_official | 237:f3da66175598 | 74 | =============================================================================== |
mbed_official | 237:f3da66175598 | 75 | ##### Extended IO operation functions ##### |
mbed_official | 237:f3da66175598 | 76 | =============================================================================== |
mbed_official | 237:f3da66175598 | 77 | [..] |
mbed_official | 237:f3da66175598 | 78 | |
mbed_official | 237:f3da66175598 | 79 | @endverbatim |
mbed_official | 237:f3da66175598 | 80 | * @{ |
mbed_official | 237:f3da66175598 | 81 | */ |
mbed_official | 237:f3da66175598 | 82 | |
mbed_official | 237:f3da66175598 | 83 | #if defined (STM32F302xC) |
mbed_official | 237:f3da66175598 | 84 | /* 2 OPAMPS available */ |
mbed_official | 237:f3da66175598 | 85 | /* 2 OPAMPS can be calibrated in parallel */ |
mbed_official | 237:f3da66175598 | 86 | |
mbed_official | 237:f3da66175598 | 87 | /** |
mbed_official | 237:f3da66175598 | 88 | * @brief Run the self calibration of 2 OPAMPs in parallel. |
mbed_official | 237:f3da66175598 | 89 | * @param hopamp1 handle |
mbed_official | 237:f3da66175598 | 90 | * @param hopamp2 handle |
mbed_official | 237:f3da66175598 | 91 | * @retval HAL status |
mbed_official | 237:f3da66175598 | 92 | * @note Updated offset trimming values (PMOS & NMOS), user trimming is enabled |
mbed_official | 237:f3da66175598 | 93 | * @note Calibration runs about 25 ms. |
mbed_official | 237:f3da66175598 | 94 | */ |
mbed_official | 237:f3da66175598 | 95 | |
mbed_official | 237:f3da66175598 | 96 | HAL_StatusTypeDef HAL_OPAMPEx_SelfCalibrateAll(OPAMP_HandleTypeDef *hopamp1, OPAMP_HandleTypeDef *hopamp2) |
mbed_official | 237:f3da66175598 | 97 | { |
mbed_official | 237:f3da66175598 | 98 | HAL_StatusTypeDef status = HAL_OK; |
mbed_official | 237:f3da66175598 | 99 | |
mbed_official | 237:f3da66175598 | 100 | uint32_t trimmingvaluen1 = 0; |
mbed_official | 237:f3da66175598 | 101 | uint32_t trimmingvaluep1 = 0; |
mbed_official | 237:f3da66175598 | 102 | uint32_t trimmingvaluen2 = 0; |
mbed_official | 237:f3da66175598 | 103 | uint32_t trimmingvaluep2 = 0; |
mbed_official | 237:f3da66175598 | 104 | |
mbed_official | 237:f3da66175598 | 105 | uint32_t delta; |
mbed_official | 237:f3da66175598 | 106 | |
mbed_official | 237:f3da66175598 | 107 | if((hopamp1 == NULL) || (hopamp1->State == HAL_OPAMP_STATE_BUSYLOCKED) || \ |
mbed_official | 237:f3da66175598 | 108 | (hopamp2 == NULL) || (hopamp2->State == HAL_OPAMP_STATE_BUSYLOCKED)) |
mbed_official | 237:f3da66175598 | 109 | { |
mbed_official | 237:f3da66175598 | 110 | status = HAL_ERROR; |
mbed_official | 237:f3da66175598 | 111 | } |
mbed_official | 237:f3da66175598 | 112 | |
mbed_official | 237:f3da66175598 | 113 | if(status == HAL_OK) |
mbed_official | 237:f3da66175598 | 114 | { |
mbed_official | 237:f3da66175598 | 115 | /* Check if OPAMP in calibration mode and calibration not yet enable */ |
mbed_official | 237:f3da66175598 | 116 | if((hopamp1->State == HAL_OPAMP_STATE_READY) && (hopamp2->State == HAL_OPAMP_STATE_READY)) |
mbed_official | 237:f3da66175598 | 117 | { |
mbed_official | 237:f3da66175598 | 118 | /* Check the parameter */ |
mbed_official | 237:f3da66175598 | 119 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp1->Instance)); |
mbed_official | 237:f3da66175598 | 120 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp2->Instance)); |
mbed_official | 237:f3da66175598 | 121 | |
mbed_official | 237:f3da66175598 | 122 | /* Set Calibration mode */ |
mbed_official | 237:f3da66175598 | 123 | /* Non-inverting input connected to calibration reference voltage. */ |
mbed_official | 237:f3da66175598 | 124 | SET_BIT(hopamp1->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 125 | SET_BIT(hopamp2->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 126 | |
mbed_official | 237:f3da66175598 | 127 | /* user trimming values are used for offset calibration */ |
mbed_official | 237:f3da66175598 | 128 | SET_BIT(hopamp1->Instance->CSR, OPAMP_CSR_USERTRIM); |
mbed_official | 237:f3da66175598 | 129 | SET_BIT(hopamp2->Instance->CSR, OPAMP_CSR_USERTRIM); |
mbed_official | 237:f3da66175598 | 130 | |
mbed_official | 237:f3da66175598 | 131 | /* Enable calibration */ |
mbed_official | 237:f3da66175598 | 132 | SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 133 | SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 134 | |
mbed_official | 237:f3da66175598 | 135 | /* 1st calibration - N */ |
mbed_official | 237:f3da66175598 | 136 | /* Select 90% VREF */ |
mbed_official | 237:f3da66175598 | 137 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA); |
mbed_official | 237:f3da66175598 | 138 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA); |
mbed_official | 237:f3da66175598 | 139 | |
mbed_official | 237:f3da66175598 | 140 | /* Enable the opamps */ |
mbed_official | 237:f3da66175598 | 141 | SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 142 | SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 143 | |
mbed_official | 237:f3da66175598 | 144 | /* Init trimming counter */ |
mbed_official | 237:f3da66175598 | 145 | /* Medium value */ |
mbed_official | 237:f3da66175598 | 146 | trimmingvaluen1 = 16; |
mbed_official | 237:f3da66175598 | 147 | trimmingvaluen2 = 16; |
mbed_official | 237:f3da66175598 | 148 | delta = 8; |
mbed_official | 237:f3da66175598 | 149 | |
mbed_official | 237:f3da66175598 | 150 | while (delta != 0) |
mbed_official | 237:f3da66175598 | 151 | { |
mbed_official | 237:f3da66175598 | 152 | // Set candidate trimming */ |
mbed_official | 237:f3da66175598 | 153 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 154 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 155 | |
mbed_official | 237:f3da66175598 | 156 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 157 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 158 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 159 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 160 | |
mbed_official | 237:f3da66175598 | 161 | if (hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 162 | { |
mbed_official | 237:f3da66175598 | 163 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 164 | trimmingvaluen1 += delta; |
mbed_official | 237:f3da66175598 | 165 | } |
mbed_official | 237:f3da66175598 | 166 | else |
mbed_official | 237:f3da66175598 | 167 | { |
mbed_official | 237:f3da66175598 | 168 | /* OPAMP_CSR_OUTCAL is LOW try lower trimming */ |
mbed_official | 237:f3da66175598 | 169 | trimmingvaluen1 -= delta; |
mbed_official | 237:f3da66175598 | 170 | } |
mbed_official | 237:f3da66175598 | 171 | |
mbed_official | 237:f3da66175598 | 172 | if (hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 173 | { |
mbed_official | 237:f3da66175598 | 174 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 175 | trimmingvaluen2 += delta; |
mbed_official | 237:f3da66175598 | 176 | } |
mbed_official | 237:f3da66175598 | 177 | else |
mbed_official | 237:f3da66175598 | 178 | { |
mbed_official | 237:f3da66175598 | 179 | /* OPAMP_CSR_OUTCAL is LOW try lower trimming */ |
mbed_official | 237:f3da66175598 | 180 | trimmingvaluen2 -= delta; |
mbed_official | 237:f3da66175598 | 181 | } |
mbed_official | 237:f3da66175598 | 182 | |
mbed_official | 237:f3da66175598 | 183 | delta >>= 1; |
mbed_official | 237:f3da66175598 | 184 | } |
mbed_official | 237:f3da66175598 | 185 | |
mbed_official | 237:f3da66175598 | 186 | // Still need to check if righ calibration is current value or un step below |
mbed_official | 237:f3da66175598 | 187 | // Indeed the first value that causes the OUTCAL bit to change from 1 to 0 |
mbed_official | 237:f3da66175598 | 188 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 189 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 190 | |
mbed_official | 237:f3da66175598 | 191 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 192 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 193 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 194 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 195 | |
mbed_official | 237:f3da66175598 | 196 | if (hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 197 | { |
mbed_official | 237:f3da66175598 | 198 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 199 | trimmingvaluen1++; |
mbed_official | 237:f3da66175598 | 200 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 201 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 202 | } |
mbed_official | 237:f3da66175598 | 203 | |
mbed_official | 237:f3da66175598 | 204 | if (hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 205 | { |
mbed_official | 237:f3da66175598 | 206 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 207 | trimmingvaluen2++; |
mbed_official | 237:f3da66175598 | 208 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 209 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 210 | } |
mbed_official | 237:f3da66175598 | 211 | |
mbed_official | 237:f3da66175598 | 212 | |
mbed_official | 237:f3da66175598 | 213 | /* 2nd calibration - P */ |
mbed_official | 237:f3da66175598 | 214 | /* Select 10% VREF */ |
mbed_official | 237:f3da66175598 | 215 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA); |
mbed_official | 237:f3da66175598 | 216 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA); |
mbed_official | 237:f3da66175598 | 217 | |
mbed_official | 237:f3da66175598 | 218 | /* Init trimming counter */ |
mbed_official | 237:f3da66175598 | 219 | /* Medium value */ |
mbed_official | 237:f3da66175598 | 220 | trimmingvaluep1 = 16; |
mbed_official | 237:f3da66175598 | 221 | trimmingvaluep2 = 16; |
mbed_official | 237:f3da66175598 | 222 | delta = 8; |
mbed_official | 237:f3da66175598 | 223 | |
mbed_official | 237:f3da66175598 | 224 | while (delta != 0) |
mbed_official | 237:f3da66175598 | 225 | { |
mbed_official | 237:f3da66175598 | 226 | // Set candidate trimming */ |
mbed_official | 237:f3da66175598 | 227 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 228 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 229 | |
mbed_official | 237:f3da66175598 | 230 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 231 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 232 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 233 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 234 | |
mbed_official | 237:f3da66175598 | 235 | if (hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 236 | { |
mbed_official | 237:f3da66175598 | 237 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 238 | trimmingvaluep1 += delta; |
mbed_official | 237:f3da66175598 | 239 | } |
mbed_official | 237:f3da66175598 | 240 | else |
mbed_official | 237:f3da66175598 | 241 | { |
mbed_official | 237:f3da66175598 | 242 | trimmingvaluep1 -= delta; |
mbed_official | 237:f3da66175598 | 243 | } |
mbed_official | 237:f3da66175598 | 244 | |
mbed_official | 237:f3da66175598 | 245 | if (hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 246 | { |
mbed_official | 237:f3da66175598 | 247 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 248 | trimmingvaluep2 += delta; |
mbed_official | 237:f3da66175598 | 249 | } |
mbed_official | 237:f3da66175598 | 250 | else |
mbed_official | 237:f3da66175598 | 251 | { |
mbed_official | 237:f3da66175598 | 252 | trimmingvaluep2 -= delta; |
mbed_official | 237:f3da66175598 | 253 | } |
mbed_official | 237:f3da66175598 | 254 | |
mbed_official | 237:f3da66175598 | 255 | delta >>= 1; |
mbed_official | 237:f3da66175598 | 256 | } |
mbed_official | 237:f3da66175598 | 257 | |
mbed_official | 237:f3da66175598 | 258 | // Still need to check if righ calibration is current value or un step below |
mbed_official | 237:f3da66175598 | 259 | // Indeed the first value that causes the OUTCAL bit to change from 1 to 0 |
mbed_official | 237:f3da66175598 | 260 | // Set candidate trimming */ |
mbed_official | 237:f3da66175598 | 261 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 262 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 263 | |
mbed_official | 237:f3da66175598 | 264 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 265 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 266 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 267 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 268 | |
mbed_official | 237:f3da66175598 | 269 | if (hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 270 | { |
mbed_official | 237:f3da66175598 | 271 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 272 | trimmingvaluep1++; |
mbed_official | 237:f3da66175598 | 273 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 274 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 275 | } |
mbed_official | 237:f3da66175598 | 276 | |
mbed_official | 237:f3da66175598 | 277 | if (hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) |
mbed_official | 237:f3da66175598 | 278 | { |
mbed_official | 237:f3da66175598 | 279 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 280 | trimmingvaluep2++; |
mbed_official | 237:f3da66175598 | 281 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 282 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 283 | } |
mbed_official | 237:f3da66175598 | 284 | |
mbed_official | 237:f3da66175598 | 285 | /* Disable calibration */ |
mbed_official | 237:f3da66175598 | 286 | CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 287 | CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 288 | |
mbed_official | 237:f3da66175598 | 289 | /* Disable the OPAMPs */ |
mbed_official | 237:f3da66175598 | 290 | CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 291 | CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 292 | |
mbed_official | 237:f3da66175598 | 293 | /* Set normale operating mode back */ |
mbed_official | 237:f3da66175598 | 294 | CLEAR_BIT(hopamp1->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 295 | CLEAR_BIT(hopamp2->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 296 | |
mbed_official | 237:f3da66175598 | 297 | /* Self calibration is successful */ |
mbed_official | 237:f3da66175598 | 298 | /* Store calibration(user timming) results in init structure. */ |
mbed_official | 237:f3da66175598 | 299 | /* Select user timming mode */ |
mbed_official | 237:f3da66175598 | 300 | |
mbed_official | 237:f3da66175598 | 301 | /* Write calibration result N */ |
mbed_official | 237:f3da66175598 | 302 | hopamp1->Init.TrimmingValueN = trimmingvaluen1; |
mbed_official | 237:f3da66175598 | 303 | hopamp2->Init.TrimmingValueN = trimmingvaluen2; |
mbed_official | 237:f3da66175598 | 304 | |
mbed_official | 237:f3da66175598 | 305 | /* Write calibration result P */ |
mbed_official | 237:f3da66175598 | 306 | hopamp1->Init.TrimmingValueP = trimmingvaluep1; |
mbed_official | 237:f3da66175598 | 307 | hopamp2->Init.TrimmingValueP = trimmingvaluep2; |
mbed_official | 237:f3da66175598 | 308 | |
mbed_official | 237:f3da66175598 | 309 | /* Calibration */ |
mbed_official | 237:f3da66175598 | 310 | hopamp1->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 311 | hopamp2->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 312 | |
mbed_official | 237:f3da66175598 | 313 | /* Select user timming mode */ |
mbed_official | 237:f3da66175598 | 314 | /* And updated with calibrated settings */ |
mbed_official | 237:f3da66175598 | 315 | hopamp1->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 316 | hopamp2->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 317 | |
mbed_official | 237:f3da66175598 | 318 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 319 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 320 | |
mbed_official | 237:f3da66175598 | 321 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 322 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 323 | |
mbed_official | 237:f3da66175598 | 324 | } |
mbed_official | 237:f3da66175598 | 325 | |
mbed_official | 237:f3da66175598 | 326 | else |
mbed_official | 237:f3da66175598 | 327 | { |
mbed_official | 237:f3da66175598 | 328 | /* At least one OPAMP can not be calibrated */ |
mbed_official | 237:f3da66175598 | 329 | status = HAL_ERROR; |
mbed_official | 237:f3da66175598 | 330 | } |
mbed_official | 237:f3da66175598 | 331 | } |
mbed_official | 237:f3da66175598 | 332 | |
mbed_official | 237:f3da66175598 | 333 | return status; |
mbed_official | 237:f3da66175598 | 334 | } |
mbed_official | 237:f3da66175598 | 335 | #endif /* STM32F302xC */ |
mbed_official | 237:f3da66175598 | 336 | |
mbed_official | 237:f3da66175598 | 337 | #if defined (STM32F303xC) || defined (STM32F358xx) |
mbed_official | 237:f3da66175598 | 338 | /* 4 OPAMPS available */ |
mbed_official | 237:f3da66175598 | 339 | /* 4 OPAMPS can be calibrated in parallel */ |
mbed_official | 237:f3da66175598 | 340 | |
mbed_official | 237:f3da66175598 | 341 | /** |
mbed_official | 237:f3da66175598 | 342 | * @brief Run the self calibration of 4 OPAMPs in parallel. |
mbed_official | 237:f3da66175598 | 343 | * @param hopamp1 handle |
mbed_official | 237:f3da66175598 | 344 | * @param hopamp2 handle |
mbed_official | 237:f3da66175598 | 345 | * @param hopamp3 handle |
mbed_official | 237:f3da66175598 | 346 | * @param hopamp4 handle |
mbed_official | 237:f3da66175598 | 347 | * @retval HAL status |
mbed_official | 237:f3da66175598 | 348 | * @note Updated offset trimming values (PMOS & NMOS), user trimming is enabled |
mbed_official | 237:f3da66175598 | 349 | * @note Calibration runs about 25 ms. |
mbed_official | 237:f3da66175598 | 350 | */ |
mbed_official | 237:f3da66175598 | 351 | |
mbed_official | 237:f3da66175598 | 352 | HAL_StatusTypeDef HAL_OPAMPEx_SelfCalibrateAll(OPAMP_HandleTypeDef *hopamp1, OPAMP_HandleTypeDef *hopamp2, OPAMP_HandleTypeDef *hopamp3, OPAMP_HandleTypeDef *hopamp4) |
mbed_official | 237:f3da66175598 | 353 | { |
mbed_official | 237:f3da66175598 | 354 | HAL_StatusTypeDef status = HAL_OK; |
mbed_official | 237:f3da66175598 | 355 | |
mbed_official | 237:f3da66175598 | 356 | uint32_t trimmingvaluen1 = 0; |
mbed_official | 237:f3da66175598 | 357 | uint32_t trimmingvaluep1 = 0; |
mbed_official | 237:f3da66175598 | 358 | uint32_t trimmingvaluen2 = 0; |
mbed_official | 237:f3da66175598 | 359 | uint32_t trimmingvaluep2 = 0; |
mbed_official | 237:f3da66175598 | 360 | uint32_t trimmingvaluen3 = 0; |
mbed_official | 237:f3da66175598 | 361 | uint32_t trimmingvaluep3 = 0; |
mbed_official | 237:f3da66175598 | 362 | uint32_t trimmingvaluen4 = 0; |
mbed_official | 237:f3da66175598 | 363 | uint32_t trimmingvaluep4 = 0; |
mbed_official | 237:f3da66175598 | 364 | |
mbed_official | 237:f3da66175598 | 365 | uint32_t delta; |
mbed_official | 237:f3da66175598 | 366 | |
mbed_official | 237:f3da66175598 | 367 | if((hopamp1 == NULL) || (hopamp1->State == HAL_OPAMP_STATE_BUSYLOCKED) || \ |
mbed_official | 237:f3da66175598 | 368 | (hopamp2 == NULL) || (hopamp2->State == HAL_OPAMP_STATE_BUSYLOCKED) || \ |
mbed_official | 237:f3da66175598 | 369 | (hopamp3 == NULL) || (hopamp3->State == HAL_OPAMP_STATE_BUSYLOCKED) || \ |
mbed_official | 237:f3da66175598 | 370 | (hopamp4 == NULL) || (hopamp4->State == HAL_OPAMP_STATE_BUSYLOCKED)) |
mbed_official | 237:f3da66175598 | 371 | { |
mbed_official | 237:f3da66175598 | 372 | status = HAL_ERROR; |
mbed_official | 237:f3da66175598 | 373 | } |
mbed_official | 237:f3da66175598 | 374 | |
mbed_official | 237:f3da66175598 | 375 | if(status == HAL_OK) |
mbed_official | 237:f3da66175598 | 376 | { |
mbed_official | 237:f3da66175598 | 377 | /* Check if OPAMP in calibration mode and calibration not yet enable */ |
mbed_official | 237:f3da66175598 | 378 | if((hopamp1->State == HAL_OPAMP_STATE_READY) && (hopamp2->State == HAL_OPAMP_STATE_READY) && \ |
mbed_official | 237:f3da66175598 | 379 | (hopamp3->State == HAL_OPAMP_STATE_READY) && (hopamp4->State == HAL_OPAMP_STATE_READY)) |
mbed_official | 237:f3da66175598 | 380 | { |
mbed_official | 237:f3da66175598 | 381 | /* Check the parameter */ |
mbed_official | 237:f3da66175598 | 382 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp1->Instance)); |
mbed_official | 237:f3da66175598 | 383 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp2->Instance)); |
mbed_official | 237:f3da66175598 | 384 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp3->Instance)); |
mbed_official | 237:f3da66175598 | 385 | assert_param(IS_OPAMP_ALL_INSTANCE(hopamp4->Instance)); |
mbed_official | 237:f3da66175598 | 386 | |
mbed_official | 237:f3da66175598 | 387 | /* Set Calibration mode */ |
mbed_official | 237:f3da66175598 | 388 | /* Non-inverting input connected to calibration reference voltage. */ |
mbed_official | 237:f3da66175598 | 389 | SET_BIT(hopamp1->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 390 | SET_BIT(hopamp2->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 391 | SET_BIT(hopamp3->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 392 | SET_BIT(hopamp4->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 393 | |
mbed_official | 237:f3da66175598 | 394 | /* user trimming values are used for offset calibration */ |
mbed_official | 237:f3da66175598 | 395 | SET_BIT(hopamp1->Instance->CSR, OPAMP_CSR_USERTRIM); |
mbed_official | 237:f3da66175598 | 396 | SET_BIT(hopamp2->Instance->CSR, OPAMP_CSR_USERTRIM); |
mbed_official | 237:f3da66175598 | 397 | SET_BIT(hopamp3->Instance->CSR, OPAMP_CSR_USERTRIM); |
mbed_official | 237:f3da66175598 | 398 | SET_BIT(hopamp4->Instance->CSR, OPAMP_CSR_USERTRIM); |
mbed_official | 237:f3da66175598 | 399 | |
mbed_official | 237:f3da66175598 | 400 | /* Enable calibration */ |
mbed_official | 237:f3da66175598 | 401 | SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 402 | SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 403 | SET_BIT (hopamp3->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 404 | SET_BIT (hopamp4->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 405 | |
mbed_official | 237:f3da66175598 | 406 | /* 1st calibration - N */ |
mbed_official | 237:f3da66175598 | 407 | /* Select 90% VREF */ |
mbed_official | 237:f3da66175598 | 408 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA); |
mbed_official | 237:f3da66175598 | 409 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA); |
mbed_official | 237:f3da66175598 | 410 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA); |
mbed_official | 237:f3da66175598 | 411 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA); |
mbed_official | 237:f3da66175598 | 412 | |
mbed_official | 237:f3da66175598 | 413 | /* Enable the opamps */ |
mbed_official | 237:f3da66175598 | 414 | SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 415 | SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 416 | SET_BIT (hopamp3->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 417 | SET_BIT (hopamp4->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 418 | |
mbed_official | 237:f3da66175598 | 419 | /* Init trimming counter */ |
mbed_official | 237:f3da66175598 | 420 | /* Medium value */ |
mbed_official | 237:f3da66175598 | 421 | trimmingvaluen1 = 16; |
mbed_official | 237:f3da66175598 | 422 | trimmingvaluen2 = 16; |
mbed_official | 237:f3da66175598 | 423 | trimmingvaluen3 = 16; |
mbed_official | 237:f3da66175598 | 424 | trimmingvaluen4 = 16; |
mbed_official | 237:f3da66175598 | 425 | delta = 8; |
mbed_official | 237:f3da66175598 | 426 | |
mbed_official | 237:f3da66175598 | 427 | while (delta != 0) |
mbed_official | 237:f3da66175598 | 428 | { |
mbed_official | 237:f3da66175598 | 429 | /* Set candidate trimming */ |
mbed_official | 237:f3da66175598 | 430 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 431 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 432 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen3<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 433 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen4<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 434 | |
mbed_official | 237:f3da66175598 | 435 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 436 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 437 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 438 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 439 | |
mbed_official | 237:f3da66175598 | 440 | if ((hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 441 | { |
mbed_official | 237:f3da66175598 | 442 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 443 | trimmingvaluen1 += delta; |
mbed_official | 237:f3da66175598 | 444 | } |
mbed_official | 237:f3da66175598 | 445 | else |
mbed_official | 237:f3da66175598 | 446 | { |
mbed_official | 237:f3da66175598 | 447 | /* OPAMP_CSR_OUTCAL is LOW try lower trimming */ |
mbed_official | 237:f3da66175598 | 448 | trimmingvaluen1 -= delta; |
mbed_official | 237:f3da66175598 | 449 | } |
mbed_official | 237:f3da66175598 | 450 | |
mbed_official | 237:f3da66175598 | 451 | if ((hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 452 | { |
mbed_official | 237:f3da66175598 | 453 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 454 | trimmingvaluen2 += delta; |
mbed_official | 237:f3da66175598 | 455 | } |
mbed_official | 237:f3da66175598 | 456 | else |
mbed_official | 237:f3da66175598 | 457 | { |
mbed_official | 237:f3da66175598 | 458 | /* OPAMP_CSR_OUTCAL is LOW try lower trimming */ |
mbed_official | 237:f3da66175598 | 459 | trimmingvaluen2 -= delta; |
mbed_official | 237:f3da66175598 | 460 | } |
mbed_official | 237:f3da66175598 | 461 | |
mbed_official | 237:f3da66175598 | 462 | if ((hopamp3->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 463 | { |
mbed_official | 237:f3da66175598 | 464 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 465 | trimmingvaluen3 += delta; |
mbed_official | 237:f3da66175598 | 466 | } |
mbed_official | 237:f3da66175598 | 467 | else |
mbed_official | 237:f3da66175598 | 468 | { |
mbed_official | 237:f3da66175598 | 469 | /* OPAMP_CSR_OUTCAL is LOW try lower trimming */ |
mbed_official | 237:f3da66175598 | 470 | trimmingvaluen3 -= delta; |
mbed_official | 237:f3da66175598 | 471 | } |
mbed_official | 237:f3da66175598 | 472 | |
mbed_official | 237:f3da66175598 | 473 | if ((hopamp4->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 474 | { |
mbed_official | 237:f3da66175598 | 475 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 476 | trimmingvaluen4 += delta; |
mbed_official | 237:f3da66175598 | 477 | } |
mbed_official | 237:f3da66175598 | 478 | else |
mbed_official | 237:f3da66175598 | 479 | { |
mbed_official | 237:f3da66175598 | 480 | /* OPAMP_CSR_OUTCAL is LOW try lower trimming */ |
mbed_official | 237:f3da66175598 | 481 | trimmingvaluen4 -= delta; |
mbed_official | 237:f3da66175598 | 482 | } |
mbed_official | 237:f3da66175598 | 483 | |
mbed_official | 237:f3da66175598 | 484 | delta >>= 1; |
mbed_official | 237:f3da66175598 | 485 | } |
mbed_official | 237:f3da66175598 | 486 | |
mbed_official | 237:f3da66175598 | 487 | /* Still need to check if righ calibration is current value or un step below */ |
mbed_official | 237:f3da66175598 | 488 | /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */ |
mbed_official | 237:f3da66175598 | 489 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 490 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 491 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen3<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 492 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen4<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 493 | |
mbed_official | 237:f3da66175598 | 494 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 495 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 496 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 497 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 498 | |
mbed_official | 237:f3da66175598 | 499 | if ((hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 500 | { |
mbed_official | 237:f3da66175598 | 501 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 502 | trimmingvaluen1++; |
mbed_official | 237:f3da66175598 | 503 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 504 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 505 | } |
mbed_official | 237:f3da66175598 | 506 | |
mbed_official | 237:f3da66175598 | 507 | if ((hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 508 | { |
mbed_official | 237:f3da66175598 | 509 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 510 | trimmingvaluen2++; |
mbed_official | 237:f3da66175598 | 511 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 512 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 513 | } |
mbed_official | 237:f3da66175598 | 514 | |
mbed_official | 237:f3da66175598 | 515 | if ((hopamp3->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 516 | { |
mbed_official | 237:f3da66175598 | 517 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 518 | trimmingvaluen3++; |
mbed_official | 237:f3da66175598 | 519 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 520 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen3<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 521 | } |
mbed_official | 237:f3da66175598 | 522 | |
mbed_official | 237:f3da66175598 | 523 | if ((hopamp4->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 524 | { |
mbed_official | 237:f3da66175598 | 525 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 526 | trimmingvaluen4++; |
mbed_official | 237:f3da66175598 | 527 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 528 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen4<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 529 | } |
mbed_official | 237:f3da66175598 | 530 | |
mbed_official | 237:f3da66175598 | 531 | /* 2nd calibration - P */ |
mbed_official | 237:f3da66175598 | 532 | /* Select 10% VREF */ |
mbed_official | 237:f3da66175598 | 533 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA); |
mbed_official | 237:f3da66175598 | 534 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA); |
mbed_official | 237:f3da66175598 | 535 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA); |
mbed_official | 237:f3da66175598 | 536 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA); |
mbed_official | 237:f3da66175598 | 537 | |
mbed_official | 237:f3da66175598 | 538 | /* Init trimming counter */ |
mbed_official | 237:f3da66175598 | 539 | /* Medium value */ |
mbed_official | 237:f3da66175598 | 540 | trimmingvaluep1 = 16; |
mbed_official | 237:f3da66175598 | 541 | trimmingvaluep2 = 16; |
mbed_official | 237:f3da66175598 | 542 | trimmingvaluep3 = 16; |
mbed_official | 237:f3da66175598 | 543 | trimmingvaluep4 = 16; |
mbed_official | 237:f3da66175598 | 544 | |
mbed_official | 237:f3da66175598 | 545 | delta = 8; |
mbed_official | 237:f3da66175598 | 546 | |
mbed_official | 237:f3da66175598 | 547 | while (delta != 0) |
mbed_official | 237:f3da66175598 | 548 | { |
mbed_official | 237:f3da66175598 | 549 | /* Set candidate trimming */ |
mbed_official | 237:f3da66175598 | 550 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 551 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 552 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep3<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 553 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep4<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 554 | |
mbed_official | 237:f3da66175598 | 555 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 556 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 557 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 558 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 559 | |
mbed_official | 237:f3da66175598 | 560 | if ((hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 561 | { |
mbed_official | 237:f3da66175598 | 562 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 563 | trimmingvaluep1 += delta; |
mbed_official | 237:f3da66175598 | 564 | } |
mbed_official | 237:f3da66175598 | 565 | else |
mbed_official | 237:f3da66175598 | 566 | { |
mbed_official | 237:f3da66175598 | 567 | trimmingvaluep1 -= delta; |
mbed_official | 237:f3da66175598 | 568 | } |
mbed_official | 237:f3da66175598 | 569 | |
mbed_official | 237:f3da66175598 | 570 | if ((hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 571 | { |
mbed_official | 237:f3da66175598 | 572 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 573 | trimmingvaluep2 += delta; |
mbed_official | 237:f3da66175598 | 574 | } |
mbed_official | 237:f3da66175598 | 575 | else |
mbed_official | 237:f3da66175598 | 576 | { |
mbed_official | 237:f3da66175598 | 577 | trimmingvaluep2 -= delta; |
mbed_official | 237:f3da66175598 | 578 | } |
mbed_official | 237:f3da66175598 | 579 | |
mbed_official | 237:f3da66175598 | 580 | if ((hopamp3->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 581 | { |
mbed_official | 237:f3da66175598 | 582 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 583 | trimmingvaluep3 += delta; |
mbed_official | 237:f3da66175598 | 584 | } |
mbed_official | 237:f3da66175598 | 585 | else |
mbed_official | 237:f3da66175598 | 586 | { |
mbed_official | 237:f3da66175598 | 587 | trimmingvaluep3 -= delta; |
mbed_official | 237:f3da66175598 | 588 | } |
mbed_official | 237:f3da66175598 | 589 | |
mbed_official | 237:f3da66175598 | 590 | if ((hopamp4->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 591 | { |
mbed_official | 237:f3da66175598 | 592 | /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */ |
mbed_official | 237:f3da66175598 | 593 | trimmingvaluep4 += delta; |
mbed_official | 237:f3da66175598 | 594 | } |
mbed_official | 237:f3da66175598 | 595 | else |
mbed_official | 237:f3da66175598 | 596 | { |
mbed_official | 237:f3da66175598 | 597 | trimmingvaluep4 -= delta; |
mbed_official | 237:f3da66175598 | 598 | } |
mbed_official | 237:f3da66175598 | 599 | |
mbed_official | 237:f3da66175598 | 600 | delta >>= 1; |
mbed_official | 237:f3da66175598 | 601 | } |
mbed_official | 237:f3da66175598 | 602 | |
mbed_official | 237:f3da66175598 | 603 | /* Still need to check if righ calibration is current value or un step below */ |
mbed_official | 237:f3da66175598 | 604 | /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */ |
mbed_official | 237:f3da66175598 | 605 | /* Set candidate trimming */ |
mbed_official | 237:f3da66175598 | 606 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 607 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 608 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep3<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 609 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep4<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 610 | |
mbed_official | 237:f3da66175598 | 611 | /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */ |
mbed_official | 237:f3da66175598 | 612 | /* Offset trim time: during calibration, minimum time needed between */ |
mbed_official | 237:f3da66175598 | 613 | /* two steps to have 1 mV accuracy */ |
mbed_official | 237:f3da66175598 | 614 | HAL_Delay(2); |
mbed_official | 237:f3da66175598 | 615 | |
mbed_official | 237:f3da66175598 | 616 | if ((hopamp1->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 617 | { |
mbed_official | 237:f3da66175598 | 618 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 619 | trimmingvaluep1++; |
mbed_official | 237:f3da66175598 | 620 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 621 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 622 | } |
mbed_official | 237:f3da66175598 | 623 | |
mbed_official | 237:f3da66175598 | 624 | if ((hopamp2->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 625 | { |
mbed_official | 237:f3da66175598 | 626 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 627 | trimmingvaluep2++; |
mbed_official | 237:f3da66175598 | 628 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 629 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 630 | } |
mbed_official | 237:f3da66175598 | 631 | |
mbed_official | 237:f3da66175598 | 632 | if ((hopamp3->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 633 | { |
mbed_official | 237:f3da66175598 | 634 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 635 | trimmingvaluep3++; |
mbed_official | 237:f3da66175598 | 636 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 637 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep3<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 638 | } |
mbed_official | 237:f3da66175598 | 639 | |
mbed_official | 237:f3da66175598 | 640 | if ((hopamp4->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET) |
mbed_official | 237:f3da66175598 | 641 | { |
mbed_official | 237:f3da66175598 | 642 | /* OPAMP_CSR_OUTCAL is actually one value more */ |
mbed_official | 237:f3da66175598 | 643 | trimmingvaluep4++; |
mbed_official | 237:f3da66175598 | 644 | /* Set right trimming */ |
mbed_official | 237:f3da66175598 | 645 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep4<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 646 | } |
mbed_official | 237:f3da66175598 | 647 | |
mbed_official | 237:f3da66175598 | 648 | /* Disable calibration */ |
mbed_official | 237:f3da66175598 | 649 | CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 650 | CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 651 | CLEAR_BIT (hopamp3->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 652 | CLEAR_BIT (hopamp4->Instance->CSR, OPAMP_CSR_CALON); |
mbed_official | 237:f3da66175598 | 653 | |
mbed_official | 237:f3da66175598 | 654 | /* Disable the OPAMPs */ |
mbed_official | 237:f3da66175598 | 655 | CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 656 | CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 657 | CLEAR_BIT (hopamp3->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 658 | CLEAR_BIT (hopamp4->Instance->CSR, OPAMP_CSR_OPAMPxEN); |
mbed_official | 237:f3da66175598 | 659 | |
mbed_official | 237:f3da66175598 | 660 | /* Set normale operating mode back */ |
mbed_official | 237:f3da66175598 | 661 | CLEAR_BIT(hopamp1->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 662 | CLEAR_BIT(hopamp2->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 663 | CLEAR_BIT(hopamp3->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 664 | CLEAR_BIT(hopamp4->Instance->CSR, OPAMP_CSR_FORCEVP); |
mbed_official | 237:f3da66175598 | 665 | |
mbed_official | 237:f3da66175598 | 666 | /* Self calibration is successful */ |
mbed_official | 237:f3da66175598 | 667 | /* Store calibration(user timming) results in init structure. */ |
mbed_official | 237:f3da66175598 | 668 | /* Select user timming mode */ |
mbed_official | 237:f3da66175598 | 669 | |
mbed_official | 237:f3da66175598 | 670 | /* Write calibration result N */ |
mbed_official | 237:f3da66175598 | 671 | hopamp1->Init.TrimmingValueN = trimmingvaluen1; |
mbed_official | 237:f3da66175598 | 672 | hopamp2->Init.TrimmingValueN = trimmingvaluen2; |
mbed_official | 237:f3da66175598 | 673 | hopamp3->Init.TrimmingValueN = trimmingvaluen3; |
mbed_official | 237:f3da66175598 | 674 | hopamp4->Init.TrimmingValueN = trimmingvaluen4; |
mbed_official | 237:f3da66175598 | 675 | |
mbed_official | 237:f3da66175598 | 676 | /* Write calibration result P */ |
mbed_official | 237:f3da66175598 | 677 | hopamp1->Init.TrimmingValueP = trimmingvaluep1; |
mbed_official | 237:f3da66175598 | 678 | hopamp2->Init.TrimmingValueP = trimmingvaluep2; |
mbed_official | 237:f3da66175598 | 679 | hopamp3->Init.TrimmingValueP = trimmingvaluep3; |
mbed_official | 237:f3da66175598 | 680 | hopamp4->Init.TrimmingValueP = trimmingvaluep4; |
mbed_official | 237:f3da66175598 | 681 | |
mbed_official | 237:f3da66175598 | 682 | /* Select user timming mode */ |
mbed_official | 237:f3da66175598 | 683 | /* And updated with calibrated settings */ |
mbed_official | 237:f3da66175598 | 684 | hopamp1->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 685 | hopamp2->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 686 | hopamp3->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 687 | hopamp4->Init.UserTrimming = OPAMP_TRIMMING_USER; |
mbed_official | 237:f3da66175598 | 688 | |
mbed_official | 237:f3da66175598 | 689 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen1<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 690 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen2<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 691 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen3<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 692 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen4<<OPAMP_INPUT_INVERTING); |
mbed_official | 237:f3da66175598 | 693 | |
mbed_official | 237:f3da66175598 | 694 | MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep1<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 695 | MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep2<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 696 | MODIFY_REG(hopamp3->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep3<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 697 | MODIFY_REG(hopamp4->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep4<<OPAMP_INPUT_NONINVERTING); |
mbed_official | 237:f3da66175598 | 698 | |
mbed_official | 237:f3da66175598 | 699 | } |
mbed_official | 237:f3da66175598 | 700 | |
mbed_official | 237:f3da66175598 | 701 | else |
mbed_official | 237:f3da66175598 | 702 | { |
mbed_official | 237:f3da66175598 | 703 | /* At least one OPAMP can not be calibrated */ |
mbed_official | 237:f3da66175598 | 704 | status = HAL_ERROR; |
mbed_official | 237:f3da66175598 | 705 | } |
mbed_official | 237:f3da66175598 | 706 | } |
mbed_official | 237:f3da66175598 | 707 | |
mbed_official | 237:f3da66175598 | 708 | return status; |
mbed_official | 237:f3da66175598 | 709 | } |
mbed_official | 237:f3da66175598 | 710 | #endif /* STM32F303xC || STM32F358xx */ |
mbed_official | 237:f3da66175598 | 711 | |
mbed_official | 237:f3da66175598 | 712 | /** |
mbed_official | 237:f3da66175598 | 713 | * @} |
mbed_official | 237:f3da66175598 | 714 | */ |
mbed_official | 237:f3da66175598 | 715 | |
mbed_official | 237:f3da66175598 | 716 | #endif /* HAL_OPAMP_MODULE_ENABLED */ |
mbed_official | 237:f3da66175598 | 717 | /** |
mbed_official | 237:f3da66175598 | 718 | * @} |
mbed_official | 237:f3da66175598 | 719 | */ |
mbed_official | 237:f3da66175598 | 720 | |
mbed_official | 237:f3da66175598 | 721 | /** |
mbed_official | 237:f3da66175598 | 722 | * @} |
mbed_official | 237:f3da66175598 | 723 | */ |
mbed_official | 237:f3da66175598 | 724 | |
mbed_official | 237:f3da66175598 | 725 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |