stm32f3xx_hal_opamp.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_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) 2016 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 4 operational amplifiers OPAMP1, OPAMP2,
  29. OPAMP3 and OPAMP4:
  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. (90U% of VDDA for NMOS transistors, 10U% 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 STM32F3 devices having 2 or 4 OPAMPs
  52. HAL_OPAMPEx_SelfCalibrateAll
  53. runs calibration of 2 or 4 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 between inverting
  57. and non-inverting input.
  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 CC6 provides the alternate switching tempo between defaults
  61. (primary) and secondary inputs.
  62. (#) Running mode: Standalone mode
  63. (++) Gain is set externally (gain depends on external loads).
  64. (++) Follower mode also possible externally by connecting the inverting input to
  65. the output.
  66. (#) Running mode: Follower mode
  67. (++) No Inverting Input is connected.
  68. (#) Running mode: Programmable Gain Amplifier (PGA) mode
  69. (Resistor feedback output)
  70. (++) The OPAMP(s) output(s) can be internally connected to resistor feedback
  71. output.
  72. (++) OPAMP gain is either 2U, 4U, 8 or 16.
  73. ##### How to use this driver #####
  74. ================================================================================
  75. [..]
  76. *** Calibration ***
  77. ============================================
  78. [..]
  79. To run the opamp calibration self calibration:
  80. (#) Start calibration using HAL_OPAMP_SelfCalibrate.
  81. Store the calibration results.
  82. *** Running mode ***
  83. ============================================
  84. [..]
  85. To use the opamp, perform the following steps:
  86. (#) Fill in the HAL_OPAMP_MspInit() to
  87. (++) Configure the opamp input AND output in analog mode using
  88. HAL_GPIO_Init() to map the opamp output to the GPIO pin.
  89. (#) Registrate Callbacks
  90. (++) The compilation define USE_HAL_OPAMP_REGISTER_CALLBACKS when set to 1
  91. allows the user to configure dynamically the driver callbacks.
  92. (++) Use Functions HAL_OPAMP_RegisterCallback() to register a user callback,
  93. it allows to register following callbacks:
  94. (+++) MspInitCallback : OPAMP MspInit.
  95. (+++) MspDeInitCallback : OPAMP MspFeInit.
  96. This function takes as parameters the HAL peripheral handle, the Callback ID
  97. and a pointer to the user callback function.
  98. (++) Use function HAL_OPAMP_UnRegisterCallback() to reset a callback to the default
  99. weak (surcharged) function. It allows to reset following callbacks:
  100. (+++) MspInitCallback : OPAMP MspInit.
  101. (+++) MspDeInitCallback : OPAMP MspdeInit.
  102. (+++) All Callbacks
  103. (#) Configure the opamp using HAL_OPAMP_Init() function:
  104. (++) Select the mode
  105. (++) Select the inverting input
  106. (++) Select the non-inverting input
  107. (++) Select if the Timer controlled Mux mode is enabled/disabled
  108. (++) If the Timer controlled Mux mode is enabled, select the secondary inverting input
  109. (++) If the Timer controlled Mux mode is enabled, Select the secondary non-inverting input
  110. (++) If PGA mode is enabled, Select if inverting input is connected.
  111. (++) Select either factory or user defined trimming mode.
  112. (++) If the user defined trimming mode is enabled, select PMOS & NMOS trimming values
  113. (typ. settings returned by HAL_OPAMP_SelfCalibrate function).
  114. (#) Enable the opamp using HAL_OPAMP_Start() function.
  115. (#) Disable the opamp using HAL_OPAMP_Stop() function.
  116. (#) Lock the opamp in running mode using HAL_OPAMP_Lock() function. From then The configuration
  117. can be modified
  118. (++) After HW reset
  119. (++) OR thanks to HAL_OPAMP_MspDeInit called (user defined) from HAL_OPAMP_DeInit.
  120. *** Running mode: change of configuration while OPAMP ON ***
  121. ============================================
  122. [..]
  123. To Re-configure OPAMP when OPAMP is ON (change on the fly)
  124. (#) If needed, Fill in the HAL_OPAMP_MspInit()
  125. (++) This is the case for instance if you wish to use new OPAMP I/O
  126. (#) Configure the opamp using HAL_OPAMP_Init() function:
  127. (++) As in configure case, selects first the parameters you wish to modify.
  128. @endverbatim
  129. ******************************************************************************
  130. */
  131. /*
  132. Additional Tables:
  133. The OPAMPs non inverting input (both default and secondary) can be
  134. selected among the list shown by table below.
  135. The OPAMPs non inverting input (both default and secondary) can be
  136. selected among the list shown by table below.
  137. Table 1. OPAMPs inverting/non-inverting inputs for the STM32F3 devices:
  138. +--------------------------------------------------------------+
  139. | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
  140. |-----------------|--------|--------|--------|--------|--------|
  141. | | No conn| X | X | X | X |
  142. | Inverting Input | VM0 | PC5 | PC5 | PB10 | PB10 |
  143. | (1) | VM1 | PA3 | PA5 | PB2 | PD8 |
  144. |-----------------|--------|--------|--------|--------|--------|
  145. | | VP0 | PA1 | PA7 | PB0 | PB13 |
  146. | Non Inverting | VP1 | PA7 | PD14 | PB13 | PD11 |
  147. | Input | VP2 | PA3 | PB0 | PA1 | PA4 |
  148. | | VP3 | PA5 | PB14 | PA5 | PB11 |
  149. +--------------------------------------------------------------+
  150. (1): NA in follower mode.
  151. Table 2. OPAMPs outputs for the STM32F3 devices:
  152. +--------------------------------------------------------------+
  153. | | | OPAMP1 | OPAMP2 | OPAMP3 | OPAMP4 |
  154. |-----------------|--------|--------|--------|--------|--------|
  155. | Output | | PA2 | PA6 | PB1 | PB12 |
  156. |-----------------|--------|--------|--------|--------|--------|
  157. */
  158. /* Includes ------------------------------------------------------------------*/
  159. #include "stm32f3xx_hal.h"
  160. /** @addtogroup STM32F3xx_HAL_Driver
  161. * @{
  162. */
  163. #ifdef HAL_OPAMP_MODULE_ENABLED
  164. #if defined (OPAMP1) || defined (OPAMP2) || defined (OPAMP3) || defined (OPAMP4)
  165. /** @defgroup OPAMP OPAMP
  166. * @brief OPAMP HAL module driver
  167. * @{
  168. */
  169. /* Private typedef -----------------------------------------------------------*/
  170. /* Private define ------------------------------------------------------------*/
  171. /** @defgroup OPAMP_Private_Define OPAMP Private Define
  172. * @{
  173. */
  174. /* CSR register reset value */
  175. #define OPAMP_CSR_RESET_VALUE (0x00000000U)
  176. /**
  177. * @}
  178. */
  179. /* Private macro -------------------------------------------------------------*/
  180. /* Private variables ---------------------------------------------------------*/
  181. /* Private function prototypes -----------------------------------------------*/
  182. /* Exported functions ---------------------------------------------------------*/
  183. /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
  184. * @{
  185. */
  186. /** @defgroup OPAMP_Exported_Functions_Group1 Initialization and de-initialization functions
  187. * @brief Initialization and Configuration functions
  188. *
  189. @verbatim
  190. ===============================================================================
  191. ##### Initialization and de-initialization functions #####
  192. ===============================================================================
  193. [..] This section provides functions allowing to:
  194. @endverbatim
  195. * @{
  196. */
  197. /**
  198. * @brief Initializes the OPAMP according to the specified
  199. * parameters in the OPAMP_InitTypeDef and create the associated handle.
  200. * @note If the selected opamp is locked, initialization can't be performed.
  201. * To unlock the configuration, perform a system reset.
  202. * @param hopamp OPAMP handle
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef HAL_OPAMP_Init(OPAMP_HandleTypeDef *hopamp)
  206. {
  207. HAL_StatusTypeDef status = HAL_OK;
  208. /* Check the OPAMP handle allocation and lock status */
  209. /* Init not allowed if calibration is ongoing */
  210. if (hopamp == NULL)
  211. {
  212. return HAL_ERROR;
  213. }
  214. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  215. {
  216. return HAL_ERROR;
  217. }
  218. else if (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  219. {
  220. return HAL_ERROR;
  221. }
  222. else
  223. {
  224. /* Check the parameter */
  225. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  226. /* Set OPAMP parameters */
  227. assert_param(IS_OPAMP_FUNCTIONAL_NORMALMODE(hopamp->Init.Mode));
  228. assert_param(IS_OPAMP_NONINVERTING_INPUT(hopamp->Init.NonInvertingInput));
  229. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  230. if (hopamp->State == HAL_OPAMP_STATE_RESET)
  231. {
  232. if (hopamp->MspInitCallback == NULL)
  233. {
  234. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  235. }
  236. }
  237. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  238. if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
  239. {
  240. assert_param(IS_OPAMP_INVERTING_INPUT(hopamp->Init.InvertingInput));
  241. }
  242. assert_param(IS_OPAMP_TIMERCONTROLLED_MUXMODE(hopamp->Init.TimerControlledMuxmode));
  243. if ((hopamp->Init.TimerControlledMuxmode) == OPAMP_TIMERCONTROLLEDMUXMODE_ENABLE)
  244. {
  245. assert_param(IS_OPAMP_SEC_NONINVERTINGINPUT(hopamp->Init.NonInvertingInputSecondary));
  246. if ((hopamp->Init.Mode) == OPAMP_STANDALONE_MODE)
  247. {
  248. assert_param(IS_OPAMP_SEC_INVERTINGINPUT(hopamp->Init.InvertingInputSecondary));
  249. }
  250. }
  251. if ((hopamp->Init.Mode) == OPAMP_PGA_MODE)
  252. {
  253. assert_param(IS_OPAMP_PGACONNECT(hopamp->Init.PgaConnect));
  254. assert_param(IS_OPAMP_PGA_GAIN(hopamp->Init.PgaGain));
  255. }
  256. assert_param(IS_OPAMP_TRIMMING(hopamp->Init.UserTrimming));
  257. if ((hopamp->Init.UserTrimming) == OPAMP_TRIMMING_USER)
  258. {
  259. assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueP));
  260. assert_param(IS_OPAMP_TRIMMINGVALUE(hopamp->Init.TrimmingValueN));
  261. }
  262. /* Init SYSCFG and the low level hardware to access opamp */
  263. __HAL_RCC_SYSCFG_CLK_ENABLE();
  264. if (hopamp->State == HAL_OPAMP_STATE_RESET)
  265. {
  266. /* Allocate lock resource and initialize it */
  267. hopamp->Lock = HAL_UNLOCKED;
  268. }
  269. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  270. hopamp->MspInitCallback(hopamp);
  271. #else
  272. /* Call MSP init function */
  273. HAL_OPAMP_MspInit(hopamp);
  274. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  275. /* Set OPAMP parameters */
  276. /* Set bits according to hopamp->hopamp->Init.Mode value */
  277. /* Set bits according to hopamp->hopamp->Init.InvertingInput value */
  278. /* Set bits according to hopamp->hopamp->Init.NonInvertingInput value */
  279. /* Set bits according to hopamp->hopamp->Init.TimerControlledMuxmode value */
  280. /* Set bits according to hopamp->hopamp->Init.InvertingInputSecondary value */
  281. /* Set bits according to hopamp->hopamp->Init.NonInvertingInputSecondary value */
  282. /* Set bits according to hopamp->hopamp->Init.PgaConnect value */
  283. /* Set bits according to hopamp->hopamp->Init.PgaGain value */
  284. /* Set bits according to hopamp->hopamp->Init.UserTrimming value */
  285. /* Set bits according to hopamp->hopamp->Init.TrimmingValueP value */
  286. /* Set bits according to hopamp->hopamp->Init.TrimmingValueN value */
  287. /* check if OPAMP_PGA_MODE & in Follower mode */
  288. /* - InvertingInput */
  289. /* - InvertingInputSecondary */
  290. /* are Not Applicable */
  291. if ((hopamp->Init.Mode == OPAMP_PGA_MODE) || (hopamp->Init.Mode == OPAMP_FOLLOWER_MODE))
  292. {
  293. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK, \
  294. hopamp->Init.Mode | \
  295. hopamp->Init.NonInvertingInput | \
  296. hopamp->Init.TimerControlledMuxmode | \
  297. hopamp->Init.NonInvertingInputSecondary | \
  298. hopamp->Init.PgaConnect | \
  299. hopamp->Init.PgaGain | \
  300. hopamp->Init.UserTrimming | \
  301. (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) | \
  302. (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
  303. }
  304. else /* OPAMP_STANDALONE_MODE */
  305. {
  306. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_UPDATE_PARAMETERS_INIT_MASK, \
  307. hopamp->Init.Mode | \
  308. hopamp->Init.InvertingInput | \
  309. hopamp->Init.NonInvertingInput | \
  310. hopamp->Init.TimerControlledMuxmode | \
  311. hopamp->Init.InvertingInputSecondary | \
  312. hopamp->Init.NonInvertingInputSecondary | \
  313. hopamp->Init.PgaConnect | \
  314. hopamp->Init.PgaGain | \
  315. hopamp->Init.UserTrimming | \
  316. (hopamp->Init.TrimmingValueP << OPAMP_INPUT_NONINVERTING) | \
  317. (hopamp->Init.TrimmingValueN << OPAMP_INPUT_INVERTING));
  318. }
  319. /* Update the OPAMP state*/
  320. if (hopamp->State == HAL_OPAMP_STATE_RESET)
  321. {
  322. /* From RESET state to READY State */
  323. hopamp->State = HAL_OPAMP_STATE_READY;
  324. }
  325. /* else: remain in READY or BUSY state (no update) */
  326. return status;
  327. }
  328. }
  329. /**
  330. * @brief DeInitializes the OPAMP peripheral
  331. * @note Deinitialization can't be performed if the OPAMP configuration is locked.
  332. * To unlock the configuration, perform a system reset.
  333. * @param hopamp OPAMP handle
  334. * @retval HAL status
  335. */
  336. HAL_StatusTypeDef HAL_OPAMP_DeInit(OPAMP_HandleTypeDef *hopamp)
  337. {
  338. HAL_StatusTypeDef status = HAL_OK;
  339. /* Check the OPAMP handle allocation */
  340. /* DeInit not allowed if calibration is ongoing */
  341. if (hopamp == NULL)
  342. {
  343. status = HAL_ERROR;
  344. }
  345. else if (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  346. {
  347. status = HAL_ERROR;
  348. }
  349. else
  350. {
  351. /* Check the parameter */
  352. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  353. /* Set OPAMP_CSR register to reset value */
  354. WRITE_REG(hopamp->Instance->CSR, OPAMP_CSR_RESET_VALUE);
  355. /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
  356. /* When OPAMP is locked, unlocking can be achieved thanks to */
  357. /* __HAL_RCC_SYSCFG_CLK_DISABLE() call within HAL_OPAMP_MspDeInit */
  358. /* Note that __HAL_RCC_SYSCFG_CLK_DISABLE() also disables comparator */
  359. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  360. if (hopamp->MspDeInitCallback == NULL)
  361. {
  362. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  363. }
  364. /* DeInit the low level hardware */
  365. hopamp->MspDeInitCallback(hopamp);
  366. #else
  367. /* DeInit the low level hardware: GPIO, CLOCK and NVIC */
  368. HAL_OPAMP_MspDeInit(hopamp);
  369. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  370. if (OPAMP_CSR_RESET_VALUE == hopamp->Instance->CSR)
  371. {
  372. /* Update the OPAMP state */
  373. hopamp->State = HAL_OPAMP_STATE_RESET;
  374. }
  375. else /* RESET STATE */
  376. {
  377. /* DeInit not complete */
  378. /* It can be the case if OPAMP was formerly locked */
  379. status = HAL_ERROR;
  380. /* The OPAMP state is NOT updated */
  381. }
  382. /* Process unlocked */
  383. __HAL_UNLOCK(hopamp);
  384. }
  385. return status;
  386. }
  387. /**
  388. * @brief Initializes the OPAMP MSP.
  389. * @param hopamp OPAMP handle
  390. * @retval None
  391. */
  392. __weak void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *hopamp)
  393. {
  394. /* Prevent unused argument(s) compilation warning */
  395. UNUSED(hopamp);
  396. /* NOTE : This function should not be modified, when the callback is needed,
  397. the HAL_OPAMP_MspInit could be implemented in the user file
  398. */
  399. /* Example */
  400. }
  401. /**
  402. * @brief DeInitializes OPAMP MSP.
  403. * @param hopamp OPAMP handle
  404. * @retval None
  405. */
  406. __weak void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *hopamp)
  407. {
  408. /* Prevent unused argument(s) compilation warning */
  409. UNUSED(hopamp);
  410. /* NOTE : This function should not be modified, when the callback is needed,
  411. the HAL_OPAMP_MspDeInit could be implemented in the user file
  412. */
  413. }
  414. /**
  415. * @}
  416. */
  417. /** @defgroup OPAMP_Exported_Functions_Group2 Input and Output operation functions
  418. * @brief Data transfers functions
  419. *
  420. @verbatim
  421. ===============================================================================
  422. ##### IO operation functions #####
  423. ===============================================================================
  424. [..]
  425. This subsection provides a set of functions allowing to manage the OPAMP data
  426. transfers.
  427. @endverbatim
  428. * @{
  429. */
  430. /**
  431. * @brief Start the opamp
  432. * @param hopamp OPAMP handle
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_OPAMP_Start(OPAMP_HandleTypeDef *hopamp)
  436. {
  437. HAL_StatusTypeDef status = HAL_OK;
  438. /* Check the OPAMP handle allocation */
  439. /* Check if OPAMP locked */
  440. if (hopamp == NULL)
  441. {
  442. status = HAL_ERROR;
  443. }
  444. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  445. {
  446. status = HAL_ERROR;
  447. }
  448. else
  449. {
  450. /* Check the parameter */
  451. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  452. if (hopamp->State == HAL_OPAMP_STATE_READY)
  453. {
  454. /* Enable the selected opamp */
  455. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  456. /* Update the OPAMP state*/
  457. /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
  458. hopamp->State = HAL_OPAMP_STATE_BUSY;
  459. }
  460. else
  461. {
  462. status = HAL_ERROR;
  463. }
  464. }
  465. return status;
  466. }
  467. /**
  468. * @brief Stop the opamp
  469. * @param hopamp OPAMP handle
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_OPAMP_Stop(OPAMP_HandleTypeDef *hopamp)
  473. {
  474. HAL_StatusTypeDef status = HAL_OK;
  475. /* Check the OPAMP handle allocation */
  476. /* Check if OPAMP locked */
  477. /* Check if OPAMP calibration ongoing */
  478. if (hopamp == NULL)
  479. {
  480. status = HAL_ERROR;
  481. }
  482. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  483. {
  484. status = HAL_ERROR;
  485. }
  486. else if (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  487. {
  488. status = HAL_ERROR;
  489. }
  490. else
  491. {
  492. /* Check the parameter */
  493. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  494. if (hopamp->State == HAL_OPAMP_STATE_BUSY)
  495. {
  496. /* Disable the selected opamp */
  497. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  498. /* Update the OPAMP state*/
  499. /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
  500. hopamp->State = HAL_OPAMP_STATE_READY;
  501. }
  502. else
  503. {
  504. status = HAL_ERROR;
  505. }
  506. }
  507. return status;
  508. }
  509. /**
  510. * @brief Run the self calibration of one OPAMP
  511. * @param hopamp handle
  512. * @retval Updated offset trimming values (PMOS & NMOS), user trimming is enabled
  513. * @retval HAL status
  514. * @note Calibration runs about 25 ms.
  515. */
  516. HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate(OPAMP_HandleTypeDef *hopamp)
  517. {
  518. HAL_StatusTypeDef status = HAL_OK;
  519. uint32_t trimmingvaluen;
  520. uint32_t trimmingvaluep;
  521. uint32_t delta;
  522. /* Check the OPAMP handle allocation */
  523. /* Check if OPAMP locked */
  524. if (hopamp == NULL)
  525. {
  526. status = HAL_ERROR;
  527. }
  528. else if (hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
  529. {
  530. status = HAL_ERROR;
  531. }
  532. else
  533. {
  534. /* Check if OPAMP in calibration mode and calibration not yet enable */
  535. if (hopamp->State == HAL_OPAMP_STATE_READY)
  536. {
  537. /* Check the parameter */
  538. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  539. /* Set Calibration mode */
  540. /* Non-inverting input connected to calibration reference voltage. */
  541. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
  542. /* user trimming values are used for offset calibration */
  543. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
  544. /* Enable calibration */
  545. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALON);
  546. /* 1st calibration - N */
  547. /* Select 90U% VREF */
  548. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_90VDDA);
  549. /* Enable the selected opamp */
  550. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  551. /* Init trimming counter */
  552. /* Medium value */
  553. trimmingvaluen = 16U;
  554. delta = 8U;
  555. while (delta != 0U)
  556. {
  557. /* Set candidate trimming */
  558. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  559. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  560. /* Offset trim time: during calibration, minimum time needed between */
  561. /* two steps to have 1 mV accuracy */
  562. HAL_Delay(2U);
  563. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
  564. {
  565. /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
  566. trimmingvaluen += delta;
  567. }
  568. else
  569. {
  570. /* OPAMP_CSR_OUTCAL is LOW try lower trimming */
  571. trimmingvaluen -= delta;
  572. }
  573. delta >>= 1U;
  574. }
  575. /* Still need to check if right calibration is current value or un step below */
  576. /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
  577. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  578. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  579. /* Offset trim time: during calibration, minimum time needed between */
  580. /* two steps to have 1 mV accuracy */
  581. HAL_Delay(2U);
  582. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
  583. {
  584. /* OPAMP_CSR_OUTCAL is actually one value more */
  585. trimmingvaluen++;
  586. /* Set right trimming */
  587. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  588. }
  589. /* 2nd calibration - P */
  590. /* Select 10U% VREF */
  591. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_CALSEL, OPAMP_VREF_10VDDA);
  592. /* Init trimming counter */
  593. /* Medium value */
  594. trimmingvaluep = 16U;
  595. delta = 8U;
  596. while (delta != 0U)
  597. {
  598. /* Set candidate trimming */
  599. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  600. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  601. /* Offset trim time: during calibration, minimum time needed between */
  602. /* two steps to have 1 mV accuracy */
  603. HAL_Delay(2U);
  604. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
  605. {
  606. /* OPAMP_CSR_OUTCAL is HIGH try higher trimming */
  607. trimmingvaluep += delta;
  608. }
  609. else
  610. {
  611. trimmingvaluep -= delta;
  612. }
  613. delta >>= 1U;
  614. }
  615. /* Still need to check if right calibration is current value or un step below */
  616. /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0U */
  617. /* Set candidate trimming */
  618. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  619. /* OFFTRIMmax delay 2 ms as per datasheet (electrical characteristics */
  620. /* Offset trim time: during calibration, minimum time needed between */
  621. /* two steps to have 1 mV accuracy */
  622. HAL_Delay(2U);
  623. if ((hopamp->Instance->CSR & OPAMP_CSR_OUTCAL) != RESET)
  624. {
  625. /* OPAMP_CSR_OUTCAL is actually one value more */
  626. trimmingvaluep++;
  627. /* Set right trimming */
  628. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  629. }
  630. /* Disable calibration */
  631. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALON);
  632. /* Disable the OPAMP */
  633. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  634. /* Set operating mode */
  635. /* Non-inverting input connected to calibration reference voltage. */
  636. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_FORCEVP);
  637. /* Self calibration is successful */
  638. /* Store calibration(user trimming) results in init structure. */
  639. /* Write calibration result N */
  640. hopamp->Init.TrimmingValueN = trimmingvaluen;
  641. /* Write calibration result P */
  642. hopamp->Init.TrimmingValueP = trimmingvaluep;
  643. /* Select user trimming mode */
  644. /* And updated with calibrated settings */
  645. hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER;
  646. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, trimmingvaluep << OPAMP_INPUT_NONINVERTING);
  647. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, trimmingvaluen << OPAMP_INPUT_INVERTING);
  648. }
  649. else
  650. {
  651. /* OPAMP can not be calibrated from this mode */
  652. status = HAL_ERROR;
  653. }
  654. }
  655. return status;
  656. }
  657. /**
  658. * @}
  659. */
  660. /** @defgroup OPAMP_Exported_Functions_Group3 Peripheral Control functions
  661. * @brief management functions
  662. *
  663. @verbatim
  664. ===============================================================================
  665. ##### Peripheral Control functions #####
  666. ===============================================================================
  667. [..]
  668. This subsection provides a set of functions allowing to control the OPAMP data
  669. transfers.
  670. @endverbatim
  671. * @{
  672. */
  673. /**
  674. * @brief Lock the selected opamp configuration.
  675. * @param hopamp OPAMP handle
  676. * @retval HAL status
  677. */
  678. HAL_StatusTypeDef HAL_OPAMP_Lock(OPAMP_HandleTypeDef *hopamp)
  679. {
  680. HAL_StatusTypeDef status = HAL_OK;
  681. /* Check the OPAMP handle allocation */
  682. /* Check if OPAMP locked */
  683. /* OPAMP can be locked when enabled and running in normal mode */
  684. /* It is meaningless otherwise */
  685. if (hopamp == NULL)
  686. {
  687. status = HAL_ERROR;
  688. }
  689. else if (hopamp->State == HAL_OPAMP_STATE_BUSY)
  690. {
  691. /* Check the parameter */
  692. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  693. /* Lock OPAMP */
  694. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_LOCK);
  695. /* OPAMP state changed to locked */
  696. hopamp->State = HAL_OPAMP_STATE_BUSYLOCKED;
  697. }
  698. else
  699. {
  700. status = HAL_ERROR;
  701. }
  702. return status;
  703. }
  704. /**
  705. * @}
  706. */
  707. /** @defgroup OPAMP_Exported_Functions_Group4 Peripheral State functions
  708. * @brief Peripheral State functions
  709. *
  710. @verbatim
  711. ===============================================================================
  712. ##### Peripheral State functions #####
  713. ===============================================================================
  714. [..]
  715. This subsection permit to get in run-time the status of the peripheral
  716. and the data flow.
  717. @endverbatim
  718. * @{
  719. */
  720. /**
  721. * @brief Return the OPAMP state
  722. * @param hopamp OPAMP handle
  723. * @retval HAL state
  724. */
  725. HAL_OPAMP_StateTypeDef HAL_OPAMP_GetState(OPAMP_HandleTypeDef *hopamp)
  726. {
  727. /* Check the OPAMP handle allocation */
  728. if (hopamp == NULL)
  729. {
  730. return HAL_OPAMP_STATE_RESET;
  731. }
  732. /* Check the parameter */
  733. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  734. return hopamp->State;
  735. }
  736. /**
  737. * @brief Return the OPAMP factory trimming value
  738. * @param hopamp OPAMP handle
  739. * @param trimmingoffset Trimming offset (P or N)
  740. * @retval Trimming value (P or N): range: 0->31
  741. * or OPAMP_FACTORYTRIMMING_DUMMY if trimming value is not available
  742. */
  743. OPAMP_TrimmingValueTypeDef HAL_OPAMP_GetTrimOffset(OPAMP_HandleTypeDef *hopamp, uint32_t trimmingoffset)
  744. {
  745. uint32_t oldusertrimming = 0U;
  746. OPAMP_TrimmingValueTypeDef oldtrimmingvaluep = 0U, oldtrimmingvaluen = 0U, trimmingvalue = 0U;
  747. /* Check the OPAMP handle allocation */
  748. /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
  749. if (hopamp == NULL)
  750. {
  751. return OPAMP_FACTORYTRIMMING_DUMMY;
  752. }
  753. /* Check the OPAMP handle allocation */
  754. /* Value can be retrieved in HAL_OPAMP_STATE_READY state */
  755. if (hopamp->State == HAL_OPAMP_STATE_READY)
  756. {
  757. /* Check the parameter */
  758. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  759. assert_param(IS_OPAMP_FACTORYTRIMMING(trimmingoffset));
  760. /* Check the trimming mode */
  761. if ((READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM)) != RESET)
  762. {
  763. /* User trimming is used */
  764. oldusertrimming = OPAMP_TRIMMING_USER;
  765. /* Store the TrimmingValueP & TrimmingValueN */
  766. oldtrimmingvaluep = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING;
  767. oldtrimmingvaluen = (hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING;
  768. }
  769. /* Set factory trimming mode */
  770. CLEAR_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
  771. /* Get factory trimming */
  772. if (trimmingoffset == OPAMP_FACTORYTRIMMING_P)
  773. {
  774. /* Return TrimOffsetP */
  775. trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETP) >> OPAMP_INPUT_NONINVERTING);
  776. }
  777. else
  778. {
  779. /* Return TrimOffsetN */
  780. trimmingvalue = ((hopamp->Instance->CSR & OPAMP_CSR_TRIMOFFSETN) >> OPAMP_INPUT_INVERTING);
  781. }
  782. /* Restore user trimming configuration if it was formerly set */
  783. /* Check if user trimming was used */
  784. if (oldusertrimming == OPAMP_TRIMMING_USER)
  785. {
  786. /* Restore user trimming */
  787. SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
  788. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETP, oldtrimmingvaluep << OPAMP_INPUT_NONINVERTING);
  789. MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_TRIMOFFSETN, oldtrimmingvaluen << OPAMP_INPUT_INVERTING);
  790. }
  791. }
  792. else
  793. {
  794. return OPAMP_FACTORYTRIMMING_DUMMY;
  795. }
  796. return trimmingvalue;
  797. }
  798. /**
  799. * @}
  800. */
  801. #if (USE_HAL_OPAMP_REGISTER_CALLBACKS == 1)
  802. /**
  803. * @brief Register a User OPAMP Callback
  804. * To be used instead of the weak (surcharged) predefined callback
  805. * @param hopamp : OPAMP handle
  806. * @param CallbackId : ID of the callback to be registered
  807. * This parameter can be one of the following values:
  808. * @arg @ref HAL_OPAMP_MSP_INIT_CB_ID OPAMP MspInit callback ID
  809. * @arg @ref HAL_OPAMP_MSP_DEINIT_CB_ID OPAMP MspDeInit callback ID
  810. * @param pCallback : pointer to the Callback function
  811. * @retval status
  812. */
  813. HAL_StatusTypeDef HAL_OPAMP_RegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackId, pOPAMP_CallbackTypeDef pCallback)
  814. {
  815. HAL_StatusTypeDef status = HAL_OK;
  816. if (pCallback == NULL)
  817. {
  818. return HAL_ERROR;
  819. }
  820. /* Process locked */
  821. __HAL_LOCK(hopamp);
  822. if (hopamp->State == HAL_OPAMP_STATE_READY)
  823. {
  824. switch (CallbackId)
  825. {
  826. case HAL_OPAMP_MSP_INIT_CB_ID :
  827. hopamp->MspInitCallback = pCallback;
  828. break;
  829. case HAL_OPAMP_MSP_DEINIT_CB_ID :
  830. hopamp->MspDeInitCallback = pCallback;
  831. break;
  832. default :
  833. /* update return status */
  834. status = HAL_ERROR;
  835. break;
  836. }
  837. }
  838. else if (hopamp->State == HAL_OPAMP_STATE_RESET)
  839. {
  840. switch (CallbackId)
  841. {
  842. case HAL_OPAMP_MSP_INIT_CB_ID :
  843. hopamp->MspInitCallback = pCallback;
  844. break;
  845. case HAL_OPAMP_MSP_DEINIT_CB_ID :
  846. hopamp->MspDeInitCallback = pCallback;
  847. break;
  848. default :
  849. /* update return status */
  850. status = HAL_ERROR;
  851. break;
  852. }
  853. }
  854. else
  855. {
  856. /* update return status */
  857. status = HAL_ERROR;
  858. }
  859. /* Release Lock */
  860. __HAL_UNLOCK(hopamp);
  861. return status;
  862. }
  863. /**
  864. * @brief Unregister a User OPAMP Callback
  865. * OPAMP Callback is redirected to the weak (surcharged) predefined callback
  866. * @param hopamp : OPAMP handle
  867. * @param CallbackId : ID of the callback to be unregistered
  868. * This parameter can be one of the following values:
  869. * @arg @ref HAL_OPAMP_MSP_INIT_CB_ID OPAMP MSP Init Callback ID
  870. * @arg @ref HAL_OPAMP_MSP_DEINIT_CB_ID OPAMP MSP DeInit Callback ID
  871. * @arg @ref HAL_OPAMP_ALL_CB_ID OPAMP All Callbacks
  872. * @retval status
  873. */
  874. HAL_StatusTypeDef HAL_OPAMP_UnRegisterCallback(OPAMP_HandleTypeDef *hopamp, HAL_OPAMP_CallbackIDTypeDef CallbackId)
  875. {
  876. HAL_StatusTypeDef status = HAL_OK;
  877. /* Process locked */
  878. __HAL_LOCK(hopamp);
  879. if (hopamp->State == HAL_OPAMP_STATE_READY)
  880. {
  881. switch (CallbackId)
  882. {
  883. case HAL_OPAMP_MSP_INIT_CB_ID :
  884. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  885. break;
  886. case HAL_OPAMP_MSP_DEINIT_CB_ID :
  887. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  888. break;
  889. case HAL_OPAMP_ALL_CB_ID :
  890. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  891. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  892. break;
  893. default :
  894. /* update return status */
  895. status = HAL_ERROR;
  896. break;
  897. }
  898. }
  899. else if (hopamp->State == HAL_OPAMP_STATE_RESET)
  900. {
  901. switch (CallbackId)
  902. {
  903. case HAL_OPAMP_MSP_INIT_CB_ID :
  904. hopamp->MspInitCallback = HAL_OPAMP_MspInit;
  905. break;
  906. case HAL_OPAMP_MSP_DEINIT_CB_ID :
  907. hopamp->MspDeInitCallback = HAL_OPAMP_MspDeInit;
  908. break;
  909. default :
  910. /* update return status */
  911. status = HAL_ERROR;
  912. break;
  913. }
  914. }
  915. else
  916. {
  917. /* update return status */
  918. status = HAL_ERROR;
  919. }
  920. /* Release Lock */
  921. __HAL_UNLOCK(hopamp);
  922. return status;
  923. }
  924. #endif /* USE_HAL_OPAMP_REGISTER_CALLBACKS */
  925. /**
  926. * @}
  927. */
  928. /**
  929. * @}
  930. */
  931. #endif /* OPAMP1 || OPAMP2 || OPAMP3 || OPAMP4 */
  932. #endif /* HAL_OPAMP_MODULE_ENABLED */
  933. /**
  934. * @}
  935. */