You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_opamp.c 47KB

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