stm32l4xx_hal_opamp_ex.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_opamp_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended OPAMP HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the operational amplifier(s)(OPAMP1, OPAMP2 etc)
  8. * peripheral:
  9. * + Extended Initialization and de-initialization functions
  10. * + Extended Peripheral Control functions
  11. *
  12. @verbatim
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. * 1. Redistributions of source code must retain the above copyright notice,
  21. * this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  26. * may be used to endorse or promote products derived from this software
  27. * without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32l4xx_hal.h"
  44. /** @addtogroup STM32L4xx_HAL_Driver
  45. * @{
  46. */
  47. /** @defgroup OPAMPEx OPAMPEx
  48. * @brief OPAMP Extended HAL module driver
  49. * @{
  50. */
  51. #ifdef HAL_OPAMP_MODULE_ENABLED
  52. /* Private typedef -----------------------------------------------------------*/
  53. /* Private define ------------------------------------------------------------*/
  54. /* Private macro -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private function prototypes -----------------------------------------------*/
  57. /* Exported functions --------------------------------------------------------*/
  58. /** @defgroup OPAMP_Exported_Functions OPAMP Exported Functions
  59. * @{
  60. */
  61. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  62. defined (STM32L496xx) || defined (STM32L4A6xx) || \
  63. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  64. /** @addtogroup OPAMPEx_Exported_Functions_Group1
  65. * @brief Extended operation functions
  66. *
  67. @verbatim
  68. ===============================================================================
  69. ##### Extended IO operation functions #####
  70. ===============================================================================
  71. [..]
  72. (+) OPAMP Self calibration.
  73. @endverbatim
  74. * @{
  75. */
  76. /* 2 OPAMPS available */
  77. /* 2 OPAMPS can be calibrated in parallel */
  78. /* Not available on STM32L43x/STM32L44x where only one OPAMP available */
  79. /**
  80. * @brief Run the self calibration of the 2 OPAMPs in parallel.
  81. * @note Trimming values (PMOS & NMOS) are updated and user trimming is
  82. * enabled is calibration is successful.
  83. * @note Calibration is performed in the mode specified in OPAMP init
  84. * structure (mode normal or low-power). To perform calibration for
  85. * both modes, repeat this function twice after OPAMP init structure
  86. * accordingly updated.
  87. * @note Calibration runs about 10 ms (5 dichotomy steps, repeated for P
  88. * and N transistors: 10 steps with 1 ms for each step).
  89. * @param hopamp1 handle
  90. * @param hopamp2 handle
  91. * @retval HAL status
  92. */
  93. HAL_StatusTypeDef HAL_OPAMPEx_SelfCalibrateAll(OPAMP_HandleTypeDef *hopamp1, OPAMP_HandleTypeDef *hopamp2)
  94. {
  95. HAL_StatusTypeDef status = HAL_OK;
  96. uint32_t trimmingvaluen1 = 0;
  97. uint32_t trimmingvaluep1 = 0;
  98. uint32_t trimmingvaluen2 = 0;
  99. uint32_t trimmingvaluep2 = 0;
  100. /* Selection of register of trimming depending on power mode: OTR or LPOTR */
  101. __IO uint32_t* tmp_opamp1_reg_trimming;
  102. __IO uint32_t* tmp_opamp2_reg_trimming;
  103. uint32_t delta;
  104. uint32_t opampmode1;
  105. uint32_t opampmode2;
  106. if((hopamp1 == NULL) || (hopamp1->State == HAL_OPAMP_STATE_BUSYLOCKED) || \
  107. (hopamp2 == NULL) || (hopamp2->State == HAL_OPAMP_STATE_BUSYLOCKED))
  108. {
  109. status = HAL_ERROR;
  110. }
  111. else
  112. {
  113. /* Check if OPAMP in calibration mode and calibration not yet enable */
  114. if((hopamp1->State == HAL_OPAMP_STATE_READY) && (hopamp2->State == HAL_OPAMP_STATE_READY))
  115. {
  116. /* Check the parameter */
  117. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp1->Instance));
  118. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp2->Instance));
  119. assert_param(IS_OPAMP_POWERMODE(hopamp1->Init.PowerMode));
  120. assert_param(IS_OPAMP_POWERMODE(hopamp2->Init.PowerMode));
  121. /* Save OPAMP mode as in */
  122. /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx */
  123. /* the calibration is not working in PGA mode */
  124. opampmode1 = READ_BIT(hopamp1->Instance->CSR,OPAMP_CSR_OPAMODE);
  125. opampmode2 = READ_BIT(hopamp2->Instance->CSR,OPAMP_CSR_OPAMODE);
  126. /* Use of standalone mode */
  127. MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_OPAMODE, OPAMP_STANDALONE_MODE);
  128. MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_OPAMODE, OPAMP_STANDALONE_MODE);
  129. /* user trimming values are used for offset calibration */
  130. SET_BIT(hopamp1->Instance->CSR, OPAMP_CSR_USERTRIM);
  131. SET_BIT(hopamp2->Instance->CSR, OPAMP_CSR_USERTRIM);
  132. /* Select trimming settings depending on power mode */
  133. if (hopamp1->Init.PowerMode == OPAMP_POWERMODE_NORMAL)
  134. {
  135. tmp_opamp1_reg_trimming = &OPAMP1->OTR;
  136. }
  137. else
  138. {
  139. tmp_opamp1_reg_trimming = &OPAMP1->LPOTR;
  140. }
  141. if (hopamp2->Init.PowerMode == OPAMP_POWERMODE_NORMAL)
  142. {
  143. tmp_opamp2_reg_trimming = &OPAMP2->OTR;
  144. }
  145. else
  146. {
  147. tmp_opamp2_reg_trimming = &OPAMP2->LPOTR;
  148. }
  149. /* Enable calibration */
  150. SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALON);
  151. SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALON);
  152. /* 1st calibration - N */
  153. CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALSEL);
  154. CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALSEL);
  155. /* Enable the selected opamp */
  156. SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  157. SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  158. /* Init trimming counter */
  159. /* Medium value */
  160. trimmingvaluen1 = 16;
  161. trimmingvaluen2 = 16;
  162. delta = 8;
  163. while (delta != 0)
  164. {
  165. /* Set candidate trimming */
  166. /* OPAMP_POWERMODE_NORMAL */
  167. MODIFY_REG(*tmp_opamp1_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen1);
  168. MODIFY_REG(*tmp_opamp2_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen2);
  169. /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
  170. /* Offset trim time: during calibration, minimum time needed between */
  171. /* two steps to have 1 mV accuracy */
  172. HAL_Delay(OPAMP_TRIMMING_DELAY);
  173. if (READ_BIT(hopamp1->Instance->CSR, OPAMP_CSR_CALOUT) != RESET)
  174. {
  175. /* OPAMP_CSR_CALOUT is HIGH try lower trimming */
  176. trimmingvaluen1 -= delta;
  177. }
  178. else
  179. {
  180. /* OPAMP_CSR_CALOUT is LOW try higher trimming */
  181. trimmingvaluen1 += delta;
  182. }
  183. if (READ_BIT(hopamp2->Instance->CSR, OPAMP_CSR_CALOUT) != RESET)
  184. {
  185. /* OPAMP_CSR_CALOUT is HIGH try lower trimming */
  186. trimmingvaluen2 -= delta;
  187. }
  188. else
  189. {
  190. /* OPAMP_CSR_CALOUT is LOW try higher trimming */
  191. trimmingvaluen2 += delta;
  192. }
  193. /* Divide range by 2 to continue dichotomy sweep */
  194. delta >>= 1;
  195. }
  196. /* Still need to check if right calibration is current value or one step below */
  197. /* Indeed the first value that causes the OUTCAL bit to change from 0 to 1 */
  198. /* Set candidate trimming */
  199. MODIFY_REG(*tmp_opamp1_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen1);
  200. MODIFY_REG(*tmp_opamp2_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen2);
  201. /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
  202. /* Offset trim time: during calibration, minimum time needed between */
  203. /* two steps to have 1 mV accuracy */
  204. HAL_Delay(OPAMP_TRIMMING_DELAY);
  205. if ((READ_BIT(hopamp1->Instance->CSR, OPAMP_CSR_CALOUT)) == 0)
  206. {
  207. /* Trimming value is actually one value more */
  208. trimmingvaluen1++;
  209. MODIFY_REG(*tmp_opamp1_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen1);
  210. }
  211. if ((READ_BIT(hopamp2->Instance->CSR, OPAMP_CSR_CALOUT)) == 0)
  212. {
  213. /* Trimming value is actually one value more */
  214. trimmingvaluen2++;
  215. MODIFY_REG(*tmp_opamp2_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen2);
  216. }
  217. /* 2nd calibration - P */
  218. SET_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALSEL);
  219. SET_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALSEL);
  220. /* Init trimming counter */
  221. /* Medium value */
  222. trimmingvaluep1 = 16;
  223. trimmingvaluep2 = 16;
  224. delta = 8;
  225. while (delta != 0)
  226. {
  227. /* Set candidate trimming */
  228. /* OPAMP_POWERMODE_NORMAL */
  229. MODIFY_REG(*tmp_opamp1_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep1<<OPAMP_INPUT_NONINVERTING));
  230. MODIFY_REG(*tmp_opamp2_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep2<<OPAMP_INPUT_NONINVERTING));
  231. /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
  232. /* Offset trim time: during calibration, minimum time needed between */
  233. /* two steps to have 1 mV accuracy */
  234. HAL_Delay(OPAMP_TRIMMING_DELAY);
  235. if (READ_BIT(hopamp1->Instance->CSR, OPAMP_CSR_CALOUT) != RESET)
  236. {
  237. /* OPAMP_CSR_CALOUT is HIGH try higher trimming */
  238. trimmingvaluep1 += delta;
  239. }
  240. else
  241. {
  242. /* OPAMP_CSR_CALOUT is HIGH try lower trimming */
  243. trimmingvaluep1 -= delta;
  244. }
  245. if (READ_BIT(hopamp2->Instance->CSR, OPAMP_CSR_CALOUT) != RESET)
  246. {
  247. /* OPAMP_CSR_CALOUT is HIGH try higher trimming */
  248. trimmingvaluep2 += delta;
  249. }
  250. else
  251. {
  252. /* OPAMP_CSR_CALOUT is LOW try lower trimming */
  253. trimmingvaluep2 -= delta;
  254. }
  255. /* Divide range by 2 to continue dichotomy sweep */
  256. delta >>= 1;
  257. }
  258. /* Still need to check if right calibration is current value or one step below */
  259. /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
  260. /* Set candidate trimming */
  261. MODIFY_REG(*tmp_opamp1_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep1<<OPAMP_INPUT_NONINVERTING));
  262. MODIFY_REG(*tmp_opamp2_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep2<<OPAMP_INPUT_NONINVERTING));
  263. /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
  264. /* Offset trim time: during calibration, minimum time needed between */
  265. /* two steps to have 1 mV accuracy */
  266. HAL_Delay(OPAMP_TRIMMING_DELAY);
  267. if (READ_BIT(hopamp1->Instance->CSR, OPAMP_CSR_CALOUT) != RESET)
  268. {
  269. /* Trimming value is actually one value more */
  270. trimmingvaluep1++;
  271. MODIFY_REG(*tmp_opamp1_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep1<<OPAMP_INPUT_NONINVERTING));
  272. }
  273. if (READ_BIT(hopamp2->Instance->CSR, OPAMP_CSR_CALOUT) != RESET)
  274. {
  275. /* Trimming value is actually one value more */
  276. trimmingvaluep2++;
  277. MODIFY_REG(*tmp_opamp2_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep2<<OPAMP_INPUT_NONINVERTING));
  278. }
  279. /* Disable the OPAMPs */
  280. CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  281. CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_OPAMPxEN);
  282. /* Disable calibration & set normal mode (operating mode) */
  283. CLEAR_BIT (hopamp1->Instance->CSR, OPAMP_CSR_CALON);
  284. CLEAR_BIT (hopamp2->Instance->CSR, OPAMP_CSR_CALON);
  285. /* Self calibration is successful */
  286. /* Store calibration (user trimming) results in init structure. */
  287. /* Set user trimming mode */
  288. hopamp1->Init.UserTrimming = OPAMP_TRIMMING_USER;
  289. hopamp2->Init.UserTrimming = OPAMP_TRIMMING_USER;
  290. /* Affect calibration parameters depending on mode normal/low power */
  291. if (hopamp1->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER)
  292. {
  293. /* Write calibration result N */
  294. hopamp1->Init.TrimmingValueN = trimmingvaluen1;
  295. /* Write calibration result P */
  296. hopamp1->Init.TrimmingValueP = trimmingvaluep1;
  297. }
  298. else
  299. {
  300. /* Write calibration result N */
  301. hopamp1->Init.TrimmingValueNLowPower = trimmingvaluen1;
  302. /* Write calibration result P */
  303. hopamp1->Init.TrimmingValuePLowPower = trimmingvaluep1;
  304. }
  305. if (hopamp2->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER)
  306. {
  307. /* Write calibration result N */
  308. hopamp2->Init.TrimmingValueN = trimmingvaluen2;
  309. /* Write calibration result P */
  310. hopamp2->Init.TrimmingValueP = trimmingvaluep2;
  311. }
  312. else
  313. {
  314. /* Write calibration result N */
  315. hopamp2->Init.TrimmingValueNLowPower = trimmingvaluen2;
  316. /* Write calibration result P */
  317. hopamp2->Init.TrimmingValuePLowPower = trimmingvaluep2;
  318. }
  319. /* Update OPAMP state */
  320. hopamp1->State = HAL_OPAMP_STATE_READY;
  321. hopamp2->State = HAL_OPAMP_STATE_READY;
  322. /* Restore OPAMP mode after calibration */
  323. MODIFY_REG(hopamp1->Instance->CSR, OPAMP_CSR_OPAMODE, opampmode1);
  324. MODIFY_REG(hopamp2->Instance->CSR, OPAMP_CSR_OPAMODE, opampmode2);
  325. }
  326. else
  327. {
  328. /* At least one OPAMP can not be calibrated */
  329. status = HAL_ERROR;
  330. }
  331. }
  332. return status;
  333. }
  334. /**
  335. * @}
  336. */
  337. #endif
  338. /** @defgroup OPAMPEx_Exported_Functions_Group2 Peripheral Control functions
  339. * @brief Peripheral Control functions
  340. *
  341. @verbatim
  342. ===============================================================================
  343. ##### Peripheral Control functions #####
  344. ===============================================================================
  345. [..]
  346. (+) OPAMP unlock.
  347. @endverbatim
  348. * @{
  349. */
  350. /**
  351. * @brief Unlock the selected OPAMP configuration.
  352. * @note This function must be called only when OPAMP is in state "locked".
  353. * @param hopamp: OPAMP handle
  354. * @retval HAL status
  355. */
  356. HAL_StatusTypeDef HAL_OPAMPEx_Unlock(OPAMP_HandleTypeDef* hopamp)
  357. {
  358. HAL_StatusTypeDef status = HAL_OK;
  359. /* Check the OPAMP handle allocation */
  360. /* Check if OPAMP locked */
  361. if((hopamp == NULL) || (hopamp->State == HAL_OPAMP_STATE_RESET)
  362. || (hopamp->State == HAL_OPAMP_STATE_READY)
  363. || (hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
  364. || (hopamp->State == HAL_OPAMP_STATE_BUSY))
  365. {
  366. status = HAL_ERROR;
  367. }
  368. else
  369. {
  370. /* Check the parameter */
  371. assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
  372. /* OPAMP state changed to locked */
  373. hopamp->State = HAL_OPAMP_STATE_BUSY;
  374. }
  375. return status;
  376. }
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @}
  382. */
  383. #endif /* HAL_OPAMP_MODULE_ENABLED */
  384. /**
  385. * @}
  386. */
  387. /**
  388. * @}
  389. */
  390. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/