stm32g4xx_hal_opamp.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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 amplifiers peripheral:
  8. * + Initialization/de-initialization functions
  9. * + I/O operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2019 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ================================================================================
  26. ##### OPAMP Peripheral Features #####
  27. ================================================================================
  28. [..] The device integrates up to 6 operational amplifiers OPAMP1, OPAMP2,
  29. OPAMP3, OPAMP4, OPAMP5 and OPAMP6:
  30. (#) The OPAMP(s) provides several exclusive running modes.
  31. (++) Standalone mode
  32. (++) Programmable Gain Amplifier (PGA) mode (Resistor feedback output)
  33. (++) Follower mode
  34. (#) The OPAMP(s) provide(s) calibration capabilities.
  35. (++) Calibration aims at correcting some offset for running mode.
  36. (++) The OPAMP uses either factory calibration settings OR user defined
  37. calibration (trimming) settings (i.e. trimming mode).
  38. (++) The user defined settings can be figured out using self calibration
  39. handled by HAL_OPAMP_SelfCalibrate, HAL_OPAMPEx_SelfCalibrateAll
  40. (++) HAL_OPAMP_SelfCalibrate:
  41. (++) Runs automatically the calibration in 2 steps.
  42. (90% of VDDA for NMOS transistors, 10% of VDDA for PMOS transistors).
  43. (As OPAMP is Rail-to-rail input/output, these 2 steps calibration is
  44. appropriate and enough in most cases).
  45. (++) Enables the user trimming mode
  46. (++) Updates the init structure with trimming values with fresh calibration
  47. results.
  48. The user may store the calibration results for larger
  49. (ex monitoring the trimming as a function of temperature
  50. for instance)
  51. (++) for STM32G4 devices having 6 OPAMPs
  52. HAL_OPAMPEx_SelfCalibrateAll
  53. runs calibration of 6 OPAMPs in parallel.
  54. (#) For any running mode, an additional Timer-controlled Mux (multiplexer)
  55. mode can be set on top.
  56. (++) Timer-controlled Mux mode allows Automatic switching of inputs
  57. configuration (inverting and non inverting).
  58. (++) Hence on top of defaults (primary) inverting and non-inverting inputs,
  59. the user shall select secondary inverting and non inverting inputs.
  60. (++) TIM1 OC6, TIM8 OC6 and TIM20 OC6 provides the alternate switching
  61. tempo between defaults (primary) and secondary inputs.
  62. (++) These 3 timers (TIM1, TIM8 and TIM20) can be combined to design a more
  63. complex switching scheme. So that any of the selected channel can initiate
  64. the configuration switch.
  65. (#) Running mode: Standalone mode
  66. (++) Gain is set externally (gain depends on external loads).
  67. (++) Follower mode also possible externally by connecting the inverting input to
  68. the output.
  69. (#) Running mode: Follower mode
  70. (++) Inverting Input is not connected.
  71. (#) Running mode: Programmable Gain Amplifier (PGA) mode
  72. (Resistor feedback output)
  73. (++) The OPAMP(s) output(s) can be internally connected to resistor feedback
  74. output.
  75. (++) The OPAMP inverting input can be "not" connected, signal to amplify is
  76. connected to non inverting input and gain is positive (2,4,8,16,32 or 64)
  77. (++) The OPAMP inverting input can be connected to VINM0:
  78. If signal is applied to non inverting input, gain is positive (2,4,8,16,32 or 64).
  79. If signal is applied to inverting input, gain is negative (-1,-3,-7,-15-,31 or -63).
  80. In both cases, the other input can be used as bias.
  81. ##### How to use this driver #####
  82. ================================================================================
  83. [..]
  84. *** High speed / normal power mode ***
  85. ============================================
  86. [..] To run in high speed mode:
  87. (#) Configure the OPAMP using HAL_OPAMP_Init() function:
  88. (++) Select OPAMP_POWERMODE_HIGHSPEED
  89. (++) Otherwise select OPAMP_POWERMODE_NORMALSPEED
  90. *** Calibration ***
  91. ============================================
  92. [..] To run the OPAMP calibration self calibration:
  93. (#) Start calibration using HAL_OPAMP_SelfCalibrate.
  94. Store the calibration results.
  95. *** Running mode ***
  96. ============================================
  97. [..] To use the OPAMP, perform the following steps:
  98. (#) Fill in the HAL_OPAMP_MspInit() to
  99. (++) Configure the OPAMP input AND output in analog mode using
  100. HAL_GPIO_Init() to map the OPAMP output to the GPIO pin.
  101. (#) Registrate Callbacks
  102. (++) The compilation define USE_HAL_OPAMP_REGISTER_CALLBACKS when set to 1
  103. allows the user to configure dynamically the driver callbacks.
  104. (++) Use Functions HAL_OPAMP_RegisterCallback() to register a user callback,
  105. it allows to register following callbacks:
  106. (+++) MspInitCallback : OPAMP MspInit.
  107. (+++) MspDeInitCallback : OPAMP MspDeInit.
  108. This function takes as parameters the HAL peripheral handle, the Callback ID
  109. and a pointer to the user callback function.
  110. (++) Use function HAL_OPAMP_UnRegisterCallback() to reset a callback to the default
  111. weak (surcharged) function. It allows to reset following callbacks:
  112. (+++) MspInitCallback : OPAMP MspInit.
  113. (+++) MspDeInitCallback : OPAMP MspDeInit.
  114. (+++) All Callbacks
  115. (#) Configure the OPAMP using HAL_OPAMP_Init() function:
  116. (++) Select the mode
  117. (++) Select the inverting input
  118. (++) Select the non-inverting input
  119. (++) Select if the internal output should be enabled/disabled (if enabled, regular I/O output is disabled)
  120. (++) Select if the Timer controlled Mux is disabled or enabled and controlled by specified timer(s)
  121. (++) If the Timer controlled Mux mode is enabled, select the secondary inverting input
  122. (++) If the Timer controlled Mux mode is enabled, Select the secondary non-inverting input
  123. (++) If PGA mode is enabled, Select if inverting input is connected.
  124. (++) If PGA mode is enabled, Select PGA gain to be used.
  125. (++) Select either factory or user defined trimming mode.
  126. (++) If the user defined trimming mode is enabled, select PMOS & NMOS trimming values
  127. (typ. settings returned by HAL_OPAMP_SelfCalibrate function).
  128. (#) Enable the OPAMP using HAL_OPAMP_Start() function.
  129. (#) Disable the OPAMP using HAL_OPAMP_Stop() function.
  130. (#) Lock the OPAMP in running mode using HAL_OPAMP_Lock() & HAL_OPAMP_TimerMuxLock functions.
  131. From then the configuration can only be modified
  132. (++) After HW reset
  133. (++) OR thanks to HAL_OPAMP_MspDeInit called (user defined) from HAL_OPAMP_DeInit.
  134. *** Running mode: change of configuration while OPAMP ON ***
  135. ============================================
  136. [..] To Re-configure OPAMP when OPAMP is ON (change on the fly)
  137. (#) If needed, fill in the HAL_OPAMP_MspInit()
  138. (++) This is the case for instance if you wish to use new OPAMP I/O
  139. (#) Configure the OPAMP using HAL_OPAMP_Init() function:
  140. (++) As in configure case, selects first the parameters you wish to modify.
  141. (++) If OPAMP control register is locked, it is not possible to modify any values
  142. on the fly (even the timer controlled mux parameters).
  143. (++) If OPAMP timer controlled mux mode register is locked, it is possible to modify any values
  144. of the control register but none on the timer controlled mux mode one.
  145. (#) Change from high speed mode to normal power mode (& vice versa) requires
  146. first HAL_OPAMP_DeInit() (force OPAMP OFF) and then HAL_OPAMP_Init().
  147. In other words, of OPAMP is ON, HAL_OPAMP_Init can NOT change power mode
  148. alone.
  149. @endverbatim
  150. ******************************************************************************
  151. */
  152. /*
  153. Additional Tables:
  154. The OPAMPs non inverting input (both default and secondary) can be
  155. selected among the list shown by table below.
  156. The OPAMPs non inverting input (both default and secondary) can be
  157. selected among the list shown by table below.
  158. Table 1. OPAMPs inverting/non-inverting inputs for the STM32G4 devices:
  159. +-----------------------------------------------------------------------------------------------+
  160. | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 | OPAMP5 | OPAMP6 |
  161. |-----------------|--------|----------|----------|-------------|----------|----------|----------|
  162. | | No conn| X | X | X | X | X | X |
  163. | Inverting Input | VM0 | PA3 | PA5 | PB2 | PB10 | PB15 | PA1 |
  164. | (1) | VM1 | PC5 | PC5 | PB10 | PD8 | PA3 | PB1 |
  165. |-----------------|--------|----------|----------|-------------|----------|----------|----------|
  166. | | VP0 | PA1 | PA7 | PB0 | PB13 | PB14 | PB12 |
  167. | Non Inverting | VP1 | PA3 | PB14 | PB13 | PD11 | PD12 | PD9 |
  168. | Input | VP2 | PA7 | PB0 | PA1 | PB11 | PC3 | PB13 |
  169. | | VP3 | DAC3_CH1 | PD14 | DAC3_CH2(2) | DAC4_CH1 | DAC4_CH2 | DAC3_CH1 |
  170. +-----------------------------------------------------------------------------------------------+
  171. (1): No connection in follower mode.
  172. (2): Available for STM32G47x/ STM32G48x devices only
  173. Table 2. OPAMPs outputs for the STM32G4 devices:
  174. +------------------------------------------------------------------------------------+
  175. | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 | OPAMP5 | OPAMP6 |
  176. |-----------------|--------|--------|--------|----------|--------|--------|----------|
  177. | Output | | PA2 | PA6 | PB1 | PB12 | PA8 | PB11 |
  178. |-----------------|--------|--------|--------|----------|--------|--------|----------+
  179. | Internal output | | ADC1 | ADC2 | ADC2 | ADC5 | ADC5 | ADC4 |
  180. | to ADCs | | CH13 | CH16 | CH18 | CH5 | CH3 | CH17(2) |
  181. | (1) | | | | ADC3 | | | ADC3 |
  182. | | | | | CH13(2) | | | CH17(3) |
  183. |-----------------|--------|--------|--------|----------|------ -|--------|----------|
  184. | Internal output | | ADC1 | ADC2 | ADC3 | ADC4 | ADC5 | ADC1 |
  185. | to ADCs input | | CH3 | CH3 | CH1(2) | CH3 | CH1 | CH14 |
  186. | on GPIO | | | | ADC1 | ADC1 | | ADC2 |
  187. | | | | | CH12 | CH11 | | CH14 |
  188. +------------------------------------------------------------------------------------+
  189. (1): This ADC channel is connected internally to the OPAMPx_VOUT when OPAINTOEN
  190. bit is set.
  191. In this case, the I/O on which the OPAMPx_VOUT is available, can be used for
  192. another purpose.
  193. (2): Available for STM32G47x/ STM32G48x devices only.
  194. (3): Available for STM32G491/STM32G4A1 devices only.
  195. */
  196. /* Includes ------------------------------------------------------------------*/
  197. #include "stm32g4xx_hal.h"
  198. /** @addtogroup STM32G4xx_HAL_Driver
  199. * @{
  200. */
  201. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4) || defined (OPAMP5) || defined (OPAMP6)
  202. #ifdef HAL_OPAMP_MODULE_ENABLED
  203. /** @defgroup OPAMP OPAMP
  204. * @brief OPAMP HAL module driver
  205. * @{
  206. */
  207. /* Private typedef -----------------------------------------------------------*/
  208. /* Private define ------------------------------------------------------------*/
  209. /** @defgroup OPAMP_Private_Define OPAMP Private Define
  210. * @{
  211. */
  212. /* CSR register reset value */
  213. #define OPAMP_CSR_RESET_VALUE (0x00000000UL)
  214. /* CSR register TRIM value upon reset are factory ones, filter them out from CSR register check */
  215. #define OPAMP_CSR_RESET_CHECK_MASK (~(OPAMP_CSR_TRIMOFFSETN | OPAMP_CSR_TRIMOFFSETP))
  216. /* CSR init register Mask */
  217. #define OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK (OPAMP_CSR_TRIMOFFSETN | OPAMP_CSR_TRIMOFFSETP \
  218. | OPAMP_CSR_HIGHSPEEDEN | OPAMP_CSR_OPAMPINTEN \
  219. | OPAMP_CSR_PGGAIN | OPAMP_CSR_VPSEL \
  220. | OPAMP_CSR_VMSEL | OPAMP_CSR_FORCEVP)
  221. /* TCMR init register Mask */
  222. #define OPAMP_TCMR_UPDATE_PARAMETERS_INIT_MASK (OPAMP_TCMR_T20CMEN | OPAMP_TCMR_T8CMEN \
  223. | OPAMP_TCMR_T1CMEN | OPAMP_TCMR_VPSSEL \
  224. | OPAMP_TCMR_VMSSEL)
  225. /**
  226. * @}
  227. */
  228. /* Private macro -------------------------------------------------------------*/
  229. /* Private variables ---------------------------------------------------------*/
  230. /* Private function prototypes -----------------------------------------------*/
  231. /* Exported functions ---------------------------------------------------------*/
  232. /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
  233. * @{
  234. */
  235. /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions
  236. * @brief Initialization and Configuration functions
  237. *
  238. @verbatim
  239. ===============================================================================
  240. ##### Initialization and de-initialization functions #####
  241. ===============================================================================
  242. [..] This section provides functions allowing to:
  243. @endverbatim
  244. * @{
  245. */
  246. /**
  247. * @brief Initializes the OPAMP according to the specified
  248. * parameters in the OPAMP_InitTypeDef and initialize the associated handle.
  249. * @note If the selected opamp is locked, initialization can't be performed.
  250. * To unlock the configuration, perform a system reset.
  251. * @param hopamp OPAMP handle
  252. * @retval HAL status
  253. */
  254. HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp)
  255. {
  256. HAL_StatusTypeDef status = HAL_OK;
  257. /* Check the OPAMP handle allocation and lock status */
  258. /* Init not allowed if calibration is ongoing */
  259. if (hopamp == NULL)
  260. {
  261. return HAL_ERROR;
  262. }
  263. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  264. {
  265. return HAL_ERROR;
  266. }
  267. else if (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  268. {
  269. return HAL_ERROR;
  270. }
  271. else
  272. {
  273. /* Check the parameter */
  274. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  275. /* Set OPAMP parameters */
  276. assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode));
  277. assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode));
  278. assert_param(IS_OPAMP_NONINVERTING_INPUT(hopamp->Init.NonInvertingInput));
  279. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  280. if (hopamp->State == HAL_OPAMP_STATE_RESET)
  281. {
  282. if (hopamp->MspInitCallback == NULL)
  283. {
  284. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  285. }
  286. }
  287. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  288. if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
  289. {
  290. assert_param(IS_OPAMP_INVERTING_INPUT(hopamp->Init.InvertingInput));
  291. }
  292. assert_param(IS_FUNCTIONAL_STATE(hopamp->Init.InternalOutput));
  293. assert_param(IS_OPAMP_TIMERCONTROLLED_MUXMODE(hopamp->Init.TimerControlledMuxmode));
  294. if ((hopamp->Init.TimerControlledMuxmode) != OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE)
  295. {
  296. assert_param(IS_OPAMP_SEC_NONINVERTING_INPUT(hopamp->Init.NonInvertingInputSecondary));
  297. assert_param(IS_OPAMP_SEC_INVERTING_INPUT(hopamp->Init.InvertingInputSecondary));
  298. }
  299. if ((hopamp->Init.Mode) == OPAMP_PGA_MODE)
  300. {
  301. assert_param(IS_OPAMP_PGACONNECT(hopamp->Init.PgaConnect));
  302. assert_param(IS_OPAMP_PGA_GAIN(hopamp->Init.PgaGain));
  303. }
  304. assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming));
  305. if ((hopamp->Init.UserTrimming) == OPAMP_TRIMMING_USER)
  306. {
  307. assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP));
  308. assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN));
  309. }
  310. /* Init SYSCFG and the low level hardware to access opamp */
  311. __HAL_RCC_SYSCFG_CLK_ENABLE();
  312. if (hopamp->State == HAL_OPAMP_STATE_RESET)
  313. {
  314. /* Allocate lock resource and initialize it */
  315. hopamp->Lock = HAL_UNLOCKED;
  316. }
  317. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  318. hopamp->MspInitCallback(hopamp);
  319. #else
  320. /* Call MSP init function */
  321. HAL_OPAMP_MspInit(hopamp);
  322. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  323. /* Set OPAMP parameters */
  324. /* Set bits according to hopamp->hopamp->Init.Mode value */
  325. /* Set bits according to hopamp->hopamp->Init.InvertingInput value */
  326. /* Set bits according to hopamp->hopamp->Init.NonInvertingInput value */
  327. /* Set bits according to hopamp->hopamp->Init.InternalOutput value */
  328. /* Set bits according to hopamp->hopamp->Init.TimerControlledMuxmode value */
  329. /* Set bits according to hopamp->hopamp->Init.InvertingInputSecondary value */
  330. /* Set bits according to hopamp->hopamp->Init.NonInvertingInputSecondary value */
  331. /* Set bits according to hopamp->hopamp->Init.PgaConnect value */
  332. /* Set bits according to hopamp->hopamp->Init.PgaGain value */
  333. /* Set bits according to hopamp->hopamp->Init.UserTrimming value */
  334. /* Set bits according to hopamp->hopamp->Init.TrimmingValueP value */
  335. /* Set bits according to hopamp->hopamp->Init.TrimmingValueN value */
  336. /* check if OPAMP_PGA_MODE & in Follower mode */
  337. /* - InvertingInput */
  338. /* is Not Applicable */
  339. if ((hopamp->Init.Mode == OPAMP_PGA_MODE) || (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE))
  340. {
  341. /* Update User Trim config first to be able to modify trimming value afterwards */
  342. MODIFY_REG(hopamp->Instance->CSR,
  343. OPAMP_CSR_USERTRIM,
  344. hopamp->Init.UserTrimming);
  345. MODIFY_REG(hopamp->Instance->CSR,
  346. OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK,
  347. hopamp->Init.PowerMode |
  348. hopamp->Init.Mode |
  349. hopamp->Init.NonInvertingInput |
  350. ((hopamp->Init.InternalOutput == ENABLE) ? OPAMP_CSR_OPAMPINTEN : 0UL) |
  351. hopamp->Init.PgaConnect |
  352. hopamp->Init.PgaGain |
  353. (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) |
  354. (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
  355. }
  356. else /* OPAMP_STANDALONE_MODE */
  357. {
  358. /* Update User Trim config first to be able to modify trimming value afterwards */
  359. MODIFY_REG(hopamp->Instance->CSR,
  360. OPAMP_CSR_USERTRIM,
  361. hopamp->Init.UserTrimming);
  362. MODIFY_REG(hopamp->Instance->CSR,
  363. OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK,
  364. hopamp->Init.PowerMode |
  365. hopamp->Init.Mode |
  366. hopamp->Init.InvertingInput |
  367. hopamp->Init.NonInvertingInput |
  368. ((hopamp->Init.InternalOutput == ENABLE) ? OPAMP_CSR_OPAMPINTEN : 0UL) |
  369. hopamp->Init.PgaConnect |
  370. hopamp->Init.PgaGain |
  371. (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) |
  372. (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
  373. }
  374. if ((READ_BIT(hopamp->Instance->TCMR, OPAMP_TCMR_LOCK)) == 0UL)
  375. {
  376. MODIFY_REG(hopamp->Instance->TCMR,
  377. OPAMP_TCMR_UPDATE_PARAMETERS_INIT_MASK,
  378. hopamp->Init.TimerControlledMuxmode |
  379. hopamp->Init.InvertingInputSecondary |
  380. hopamp->Init.NonInvertingInputSecondary);
  381. }
  382. /* Update the OPAMP state*/
  383. if (hopamp->State == HAL_OPAMP_STATE_RESET)
  384. {
  385. /* From RESET state to READY State */
  386. hopamp->State = HAL_OPAMP_STATE_READY;
  387. }
  388. /* else: remain in READY or BUSY state (no update) */
  389. return status;
  390. }
  391. }
  392. /**
  393. * @brief DeInitializes the OPAMP peripheral
  394. * @note Deinitialization can't be performed if the OPAMP configuration is locked.
  395. * To unlock the configuration, perform a system reset.
  396. * @param hopamp OPAMP handle
  397. * @retval HAL status
  398. */
  399. HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp)
  400. {
  401. HAL_StatusTypeDef status = HAL_OK;
  402. /* Check the OPAMP handle allocation */
  403. /* DeInit not allowed if calibration is ongoing */
  404. if (hopamp == NULL)
  405. {
  406. status = HAL_ERROR;
  407. }
  408. else if (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  409. {
  410. status = HAL_ERROR;
  411. }
  412. else
  413. {
  414. /* Check the parameter */
  415. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  416. /* Set OPAMP_CSR register to reset value */
  417. WRITE_REG(hopamp->Instance->CSR, OPAMP_CSR_RESET_VALUE);
  418. /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
  419. /* When OPAMP is locked, unlocking can be achieved thanks to */
  420. /* __HAL_RCC_SYSCFG_CLK_DISABLE() call within HAL_OPAMP_MspDeInit */
  421. /* Note that __HAL_RCC_SYSCFG_CLK_DISABLE() also disables comparator */
  422. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  423. if (hopamp->MspDeInitCallback == NULL)
  424. {
  425. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  426. }
  427. /* DeInit the low level hardware */
  428. hopamp->MspDeInitCallback(hopamp);
  429. #else
  430. HAL_OPAMP_MspDeInit(hopamp);
  431. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  432. if (OPAMP_CSR_RESET_VALUE == (hopamp->Instance->CSR & OPAMP_CSR_RESET_CHECK_MASK))
  433. {
  434. /* Update the OPAMP state */
  435. hopamp->State = HAL_OPAMP_STATE_RESET;
  436. }
  437. else /* RESET STATE */
  438. {
  439. /* DeInit not complete */
  440. /* It can be the case if OPAMP was formerly locked */
  441. status = HAL_ERROR;
  442. /* The OPAMP state is NOT updated */
  443. }
  444. /* Process unlocked */
  445. __HAL_UNLOCK(hopamp);
  446. }
  447. return status;
  448. }
  449. /**
  450. * @brief Initialize the OPAMP MSP.
  451. * @param hopamp OPAMP handle
  452. * @retval None
  453. */
  454. __weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp)
  455. {
  456. /* Prevent unused argument(s) compilation warning */
  457. UNUSED(hopamp);
  458. /* NOTE : This function should not be modified, when the callback is needed,
  459. the HAL_OPAMP_MspInit could be implemented in the user file
  460. */
  461. /* Example */
  462. }
  463. /**
  464. * @brief DeInitialize OPAMP MSP.
  465. * @param hopamp OPAMP handle
  466. * @retval None
  467. */
  468. __weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp)
  469. {
  470. /* Prevent unused argument(s) compilation warning */
  471. UNUSED(hopamp);
  472. /* NOTE : This function should not be modified, when the callback is needed,
  473. the HAL_OPAMP_MspDeInit could be implemented in the user file
  474. */
  475. }
  476. /**
  477. * @}
  478. */
  479. /** @defgroup OPAMP_Exported_Functions_Group2 Input and Output operation functions
  480. * @brief Data transfers functions
  481. *
  482. @verbatim
  483. ===============================================================================
  484. ##### IO operation functions #####
  485. ===============================================================================
  486. [..]
  487. This subsection provides a set of functions allowing to manage the OPAMP data
  488. transfers.
  489. @endverbatim
  490. * @{
  491. */
  492. /**
  493. * @brief Start the opamp
  494. * @param hopamp OPAMP handle
  495. * @retval HAL status
  496. */
  497. HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp)
  498. {
  499. HAL_StatusTypeDef status = HAL_OK;
  500. /* Check the OPAMP handle allocation */
  501. /* Check if OPAMP locked */
  502. if (hopamp == NULL)
  503. {
  504. status = HAL_ERROR;
  505. }
  506. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  507. {
  508. status = HAL_ERROR;
  509. }
  510. else
  511. {
  512. /* Check the parameter */
  513. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  514. if (hopamp->State == HAL_OPAMP_STATE_READY)
  515. {
  516. /* Enable the selected opamp */
  517. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  518. /* Update the OPAMP state*/
  519. /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
  520. hopamp->State = HAL_OPAMP_STATE_BUSY;
  521. }
  522. else
  523. {
  524. status = HAL_ERROR;
  525. }
  526. }
  527. return status;
  528. }
  529. /**
  530. * @brief Stop the opamp
  531. * @param hopamp OPAMP handle
  532. * @retval HAL status
  533. */
  534. HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp)
  535. {
  536. HAL_StatusTypeDef status = HAL_OK;
  537. /* Check the OPAMP handle allocation */
  538. /* Check if OPAMP locked */
  539. /* Check if OPAMP calibration ongoing */
  540. if (hopamp == NULL)
  541. {
  542. status = HAL_ERROR;
  543. }
  544. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  545. {
  546. status = HAL_ERROR;
  547. }
  548. else if (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  549. {
  550. status = HAL_ERROR;
  551. }
  552. else
  553. {
  554. /* Check the parameter */
  555. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  556. if (hopamp->State == HAL_OPAMP_STATE_BUSY)
  557. {
  558. /* Disable the selected opamp */
  559. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  560. /* Update the OPAMP state*/
  561. /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
  562. hopamp->State = HAL_OPAMP_STATE_READY;
  563. }
  564. else
  565. {
  566. status = HAL_ERROR;
  567. }
  568. }
  569. return status;
  570. }
  571. /**
  572. * @brief Run the self calibration of one OPAMP
  573. * @note Calibration is performed in the mode specified in OPAMP init
  574. * structure (mode normal or high-speed).
  575. * @param hopamp handle
  576. * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled
  577. * @retval HAL status
  578. * @note Calibration runs about 25 ms.
  579. */
  580. HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp)
  581. {
  582. HAL_StatusTypeDef status = HAL_OK;
  583. uint32_t trimmingvaluen;
  584. uint32_t trimmingvaluep;
  585. uint32_t delta;
  586. /* Check the OPAMP handle allocation */
  587. /* Check if OPAMP locked */
  588. if (hopamp == NULL)
  589. {
  590. status = HAL_ERROR;
  591. }
  592. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  593. {
  594. status = HAL_ERROR;
  595. }
  596. else
  597. {
  598. /* Check if OPAMP in calibration mode and calibration not yet enable */
  599. if (hopamp->State == HAL_OPAMP_STATE_READY)
  600. {
  601. /* Check the parameter */
  602. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  603. /* Set Calibration mode */
  604. /* Non-inverting input connected to calibration reference voltage. */
  605. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
  606. /* user trimming values are used for offset calibration */
  607. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
  608. /* Enable calibration */
  609. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALON);
  610. /* 1st calibration - N */
  611. /* Select 90% VREF */
  612. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA);
  613. /* Enable the selected opamp */
  614. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  615. /* Init trimming counter */
  616. /* Medium value */
  617. trimmingvaluen = 16UL;
  618. delta = 8UL;
  619. while (delta != 0UL)
  620. {
  621. /* Set candidate trimming */
  622. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  623. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  624. /* Offset trim time: during calibration, minimum time needed between */
  625. /* two steps to have 1 mV accuracy */
  626. HAL_Delay(2);
  627. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != 0UL)
  628. {
  629. /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
  630. trimmingvaluen += delta;
  631. }
  632. else
  633. {
  634. /* OPAMP_CSR_OUTCAL is LOW try lower trimming */
  635. trimmingvaluen -= delta;
  636. }
  637. delta >>= 1;
  638. }
  639. /* Still need to check if righ calibration is current value or un step below */
  640. /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
  641. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  642. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  643. /* Offset trim time: during calibration, minimum time needed between */
  644. /* two steps to have 1 mV accuracy */
  645. HAL_Delay(2);
  646. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != 0UL)
  647. {
  648. /* OPAMP_CSR_OUTCAL is actually one value more */
  649. trimmingvaluen++;
  650. /* Set right trimming */
  651. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  652. }
  653. /* 2nd calibration - P */
  654. /* Select 10% VREF */
  655. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA);
  656. /* Init trimming counter */
  657. /* Medium value */
  658. trimmingvaluep = 16UL;
  659. delta = 8UL;
  660. while (delta != 0UL)
  661. {
  662. /* Set candidate trimming */
  663. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  664. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  665. /* Offset trim time: during calibration, minimum time needed between */
  666. /* two steps to have 1 mV accuracy */
  667. HAL_Delay(2);
  668. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != 0UL)
  669. {
  670. /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
  671. trimmingvaluep += delta;
  672. }
  673. else
  674. {
  675. trimmingvaluep -= delta;
  676. }
  677. delta >>= 1;
  678. }
  679. /* Still need to check if righ calibration is current value or un step below */
  680. /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0U */
  681. /* Set candidate trimming */
  682. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  683. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  684. /* Offset trim time: during calibration, minimum time needed between */
  685. /* two steps to have 1 mV accuracy */
  686. HAL_Delay(2);
  687. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != 0UL)
  688. {
  689. /* OPAMP_CSR_OUTCAL is actually one value more */
  690. trimmingvaluep++;
  691. /* Set right trimming */
  692. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  693. }
  694. /* Disable calibration */
  695. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALON);
  696. /* Disable the OPAMP */
  697. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  698. /* Set operating mode */
  699. /* Non-inverting input connected to calibration reference voltage. */
  700. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
  701. /* Self calibration is successful */
  702. /* Store calibration(user timing) results in init structure. */
  703. /* Write calibration result N */
  704. hopamp->Init.TrimmingValueN = trimmingvaluen;
  705. /* Write calibration result P */
  706. hopamp->Init.TrimmingValueP = trimmingvaluep;
  707. /* Select user timing mode */
  708. /* And updated with calibrated settings */
  709. hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER;
  710. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  711. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  712. }
  713. else
  714. {
  715. /* OPAMP can not be calibrated from this mode */
  716. status = HAL_ERROR;
  717. }
  718. }
  719. return status;
  720. }
  721. /**
  722. * @}
  723. */
  724. /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions
  725. * @brief Peripheral Control functions
  726. *
  727. @verbatim
  728. ===============================================================================
  729. ##### Peripheral Control functions #####
  730. ===============================================================================
  731. [..]
  732. This subsection provides a set of functions allowing to control the OPAMP data
  733. transfers.
  734. @endverbatim
  735. * @{
  736. */
  737. /**
  738. * @brief Lock the selected opamp configuration.
  739. * @param hopamp OPAMP handle
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp)
  743. {
  744. HAL_StatusTypeDef status = HAL_OK;
  745. /* Check the OPAMP handle allocation */
  746. /* Check if OPAMP locked */
  747. /* OPAMP can be locked when enabled and running in normal mode */
  748. /* It is meaningless otherwise */
  749. if (hopamp == NULL)
  750. {
  751. status = HAL_ERROR;
  752. }
  753. else if (hopamp->State != HAL_OPAMP_STATE_BUSY)
  754. {
  755. status = HAL_ERROR;
  756. }
  757. else
  758. {
  759. /* Check the parameter */
  760. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  761. /* Lock OPAMP */
  762. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_LOCK);
  763. /* OPAMP state changed to locked */
  764. hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED;
  765. }
  766. return status;
  767. }
  768. /**
  769. * @}
  770. */
  771. /**
  772. * @brief Lock the selected opamp timer controlled mux configuration.
  773. * @param hopamp OPAMP handle
  774. * @retval HAL status
  775. */
  776. HAL_StatusTypeDef HAL_OPAMP_LockTimerMux(OPAMP_HandleTypeDef *hopamp)
  777. {
  778. HAL_StatusTypeDef status = HAL_OK;
  779. /* Check the OPAMP handle allocation */
  780. /* Check if OPAMP timer controlled mux is locked */
  781. /* OPAMP timer controlled mux can be locked when enabled */
  782. /* It is meaningless otherwise */
  783. if (hopamp == NULL)
  784. {
  785. status = HAL_ERROR;
  786. }
  787. else if (hopamp->State == HAL_OPAMP_STATE_RESET)
  788. {
  789. status = HAL_ERROR;
  790. }
  791. else if (READ_BIT(hopamp->Instance->TCMR, OPAMP_TCMR_LOCK) == OPAMP_TCMR_LOCK)
  792. {
  793. status = HAL_ERROR;
  794. }
  795. else
  796. {
  797. /* Check the parameter */
  798. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  799. /* Lock OPAMP */
  800. SET_BIT(hopamp->Instance->TCMR, OPAMP_TCMR_LOCK);
  801. }
  802. return status;
  803. }
  804. /**
  805. * @}
  806. */
  807. /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions
  808. * @brief Peripheral State functions
  809. *
  810. @verbatim
  811. ===============================================================================
  812. ##### Peripheral State functions #####
  813. ===============================================================================
  814. [..]
  815. This subsection permit to get in run-time the status of the peripheral
  816. and the data flow.
  817. @endverbatim
  818. * @{
  819. */
  820. /**
  821. * @brief Return the OPAMP state
  822. * @param hopamp OPAMP handle
  823. * @retval HAL state
  824. */
  825. HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef *hopamp)
  826. {
  827. /* Check the OPAMP handle allocation */
  828. if (hopamp == NULL)
  829. {
  830. return HAL_OPAMP_STATE_RESET;
  831. }
  832. /* Check the parameter */
  833. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  834. return hopamp->State;
  835. }
  836. /**
  837. * @brief Return the OPAMP factory trimming value
  838. * @param hopamp OPAMP handle
  839. * @param trimmingoffset Trimming offset (P or N)
  840. * @retval Trimming value (P or N): range: 0->31
  841. * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available
  842. */
  843. OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset(OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset)
  844. {
  845. uint32_t oldusertrimming = 0UL;
  846. OPAMP_TrimmingValueTypeDef oldtrimmingvaluep = 0UL, oldtrimmingvaluen = 0UL, trimmingvalue;
  847. /* Check the OPAMP handle allocation */
  848. /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
  849. if (hopamp == NULL)
  850. {
  851. return OPAMP_FACTORYTRIMMING_DUMMY;
  852. }
  853. else if (hopamp->State != HAL_OPAMP_STATE_READY)
  854. {
  855. return OPAMP_FACTORYTRIMMING_DUMMY;
  856. }
  857. else
  858. {
  859. /* Check the parameter */
  860. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  861. assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset));
  862. /* Check the trimming mode */
  863. if ((READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM)) != 0UL)
  864. {
  865. /* User trimming is used */
  866. oldusertrimming = OPAMP_TRIMMING_USER;
  867. /* Store the TrimmingValueP & TrimmingValueN */
  868. oldtrimmingvaluep = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING;
  869. oldtrimmingvaluen = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING;
  870. }
  871. /* Set factory timing mode */
  872. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
  873. /* Get factory trimming */
  874. if (trimmingoffset == OPAMP_FACTORYTRIMMING_P)
  875. {
  876. /* Return TrimOffsetP */
  877. trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING);
  878. }
  879. else
  880. {
  881. /* Return TrimOffsetN */
  882. trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING);
  883. }
  884. /* Restore user trimming configuration if it was formerly set */
  885. /* Check if user trimming was used */
  886. if (oldusertrimming == OPAMP_TRIMMING_USER)
  887. {
  888. /* Restore user trimming */
  889. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
  890. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, oldtrimmingvaluep << OPAMP_INPUT_NONINVERTING);
  891. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, oldtrimmingvaluen << OPAMP_INPUT_INVERTING);
  892. }
  893. }
  894. return trimmingvalue;
  895. }
  896. /**
  897. * @}
  898. */
  899. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  900. /**
  901. * @brief Register a User OPAMP Callback
  902. * To be used instead of the weak (surcharged) predefined callback
  903. * @param hopamp : OPAMP handle
  904. * @param CallbackID : ID of the callback to be registered
  905. * This parameter can be one of the following values:
  906. * @arg @ref HAL_OPAMP_MSPINIT_CB_ID OPAMP MspInit callback ID
  907. * @arg @ref HAL_OPAMP_MSPDEINIT_CB_ID OPAMP MspDeInit callback ID
  908. * @param pCallback : pointer to the Callback function
  909. * @retval status
  910. */
  911. HAL_StatusTypeDef HAL_OPAMP_RegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackId,
  912. pOPAMP_CallbackTypeDef pCallback)
  913. {
  914. HAL_StatusTypeDef status = HAL_OK;
  915. if (pCallback == NULL)
  916. {
  917. return HAL_ERROR;
  918. }
  919. /* Process locked */
  920. __HAL_LOCK(hopamp);
  921. if (hopamp->State == HAL_OPAMP_STATE_READY)
  922. {
  923. switch (CallbackId)
  924. {
  925. case HAL_OPAMP_MSPINIT_CB_ID :
  926. hopamp->MspInitCallback = pCallback;
  927. break;
  928. case HAL_OPAMP_MSPDEINIT_CB_ID :
  929. hopamp->MspDeInitCallback = pCallback;
  930. break;
  931. default :
  932. /* update return status */
  933. status = HAL_ERROR;
  934. break;
  935. }
  936. }
  937. else if (hopamp->State == HAL_OPAMP_STATE_RESET)
  938. {
  939. switch (CallbackId)
  940. {
  941. case HAL_OPAMP_MSPINIT_CB_ID :
  942. hopamp->MspInitCallback = pCallback;
  943. break;
  944. case HAL_OPAMP_MSPDEINIT_CB_ID :
  945. hopamp->MspDeInitCallback = pCallback;
  946. break;
  947. default :
  948. /* update return status */
  949. status = HAL_ERROR;
  950. break;
  951. }
  952. }
  953. else
  954. {
  955. /* update return status */
  956. status = HAL_ERROR;
  957. }
  958. /* Release Lock */
  959. __HAL_UNLOCK(hopamp);
  960. return status;
  961. }
  962. /**
  963. * @brief Unregister a User OPAMP Callback
  964. * OPAMP Callback is redirected to the weak (surcharged) predefined callback
  965. * @param hopamp : OPAMP handle
  966. * @param CallbackID : ID of the callback to be unregistered
  967. * This parameter can be one of the following values:
  968. * @arg @ref HAL_OPAMP_MSPINIT_CB_ID OPAMP MSP Init Callback ID
  969. * @arg @ref HAL_OPAMP_MSPDEINIT_CB_ID OPAMP MSP DeInit Callback ID
  970. * @arg @ref HAL_OPAMP_ALL_CB_ID OPAMP All Callbacks
  971. * @retval status
  972. */
  973. HAL_StatusTypeDef HAL_OPAMP_UnRegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackId)
  974. {
  975. HAL_StatusTypeDef status = HAL_OK;
  976. /* Process locked */
  977. __HAL_LOCK(hopamp);
  978. if (hopamp->State == HAL_OPAMP_STATE_READY)
  979. {
  980. switch (CallbackId)
  981. {
  982. case HAL_OPAMP_MSPINIT_CB_ID :
  983. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  984. break;
  985. case HAL_OPAMP_MSPDEINIT_CB_ID :
  986. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  987. break;
  988. case HAL_OPAMP_ALL_CB_ID :
  989. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  990. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  991. break;
  992. default :
  993. /* update return status */
  994. status = HAL_ERROR;
  995. break;
  996. }
  997. }
  998. else if (hopamp->State == HAL_OPAMP_STATE_RESET)
  999. {
  1000. switch (CallbackId)
  1001. {
  1002. case HAL_OPAMP_MSPINIT_CB_ID :
  1003. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  1004. break;
  1005. case HAL_OPAMP_MSPDEINIT_CB_ID :
  1006. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  1007. break;
  1008. default :
  1009. /* update return status */
  1010. status = HAL_ERROR;
  1011. break;
  1012. }
  1013. }
  1014. else
  1015. {
  1016. /* update return status */
  1017. status = HAL_ERROR;
  1018. }
  1019. /* Release Lock */
  1020. __HAL_UNLOCK(hopamp);
  1021. return status;
  1022. }
  1023. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  1024. /**
  1025. * @}
  1026. */
  1027. /**
  1028. * @}
  1029. */
  1030. #endif /* HAL_OPAMP_MODULE_ENABLED */
  1031. #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 || OPAMP5 || OPAMP6 */
  1032. /**
  1033. * @}
  1034. */