STM32L4xx_HAL_Driver  1.14.0

IO operation functions. More...

Functions

HAL_StatusTypeDef HAL_OPAMP_Start (OPAMP_HandleTypeDef *hopamp)
 Start the OPAMP. More...
 
HAL_StatusTypeDef HAL_OPAMP_Stop (OPAMP_HandleTypeDef *hopamp)
 Stop the OPAMP. More...
 
HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate (OPAMP_HandleTypeDef *hopamp)
 Run the self calibration of one OPAMP. More...
 

Detailed Description

IO operation functions.

 ===============================================================================
                        ##### IO operation functions #####
 ===============================================================================
    [..]
    This subsection provides a set of functions allowing to manage the OPAMP
    start, stop and calibration actions.

Function Documentation

◆ HAL_OPAMP_SelfCalibrate()

HAL_StatusTypeDef HAL_OPAMP_SelfCalibrate ( OPAMP_HandleTypeDef hopamp)

Run the self calibration of one OPAMP.

Note
Calibration is performed in the mode specified in OPAMP init structure (mode normal or low-power). To perform calibration for both modes, repeat this function twice after OPAMP init structure accordingly updated.
Calibration runs about 10 ms.
Parameters
hopamphandle
Return values
Updatedoffset trimming values (PMOS & NMOS), user trimming is enabled
HALstatus

Definition at line 641 of file stm32l4xx_hal_opamp.c.

642 {
643 
644  HAL_StatusTypeDef status = HAL_OK;
645 
646  uint32_t trimmingvaluen;
647  uint32_t trimmingvaluep;
648  uint32_t delta;
649  uint32_t opampmode;
650 
651  __IO uint32_t* tmp_opamp_reg_trimming; /* Selection of register of trimming depending on power mode: OTR or LPOTR */
652 
653  /* Check the OPAMP handle allocation */
654  /* Check if OPAMP locked */
655  if(hopamp == NULL)
656  {
657  status = HAL_ERROR;
658  }
659  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
660  {
661  status = HAL_ERROR;
662  }
663  else
664  {
665  /* Check if OPAMP in calibration mode and calibration not yet enable */
666  if(hopamp->State == HAL_OPAMP_STATE_READY)
667  {
668  /* Check the parameter */
669  assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
670  assert_param(IS_OPAMP_POWERMODE(hopamp->Init.PowerMode));
671 
672  /* Save OPAMP mode as in */
673  /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx */
674  /* the calibration is not working in PGA mode */
675  opampmode = READ_BIT(hopamp->Instance->CSR,OPAMP_CSR_OPAMODE);
676 
677  /* Use of standalone mode */
678  MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_OPAMODE, OPAMP_STANDALONE_MODE);
679 
680  /* user trimming values are used for offset calibration */
681  SET_BIT(hopamp->Instance->CSR, OPAMP_CSR_USERTRIM);
682 
683  /* Select trimming settings depending on power mode */
684  if (hopamp->Init.PowerMode == OPAMP_POWERMODE_NORMAL)
685  {
686  tmp_opamp_reg_trimming = &hopamp->Instance->OTR;
687  }
688  else
689  {
690  tmp_opamp_reg_trimming = &hopamp->Instance->LPOTR;
691  }
692 
693  /* Enable calibration */
694  SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON);
695 
696  /* 1st calibration - N */
697  CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALSEL);
698 
699  /* Enable the selected opamp */
700  SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
701 
702  /* Init trimming counter */
703  /* Medium value */
704  trimmingvaluen = 16U;
705  delta = 8U;
706 
707  while (delta != 0U)
708  {
709  /* Set candidate trimming */
710  /* OPAMP_POWERMODE_NORMAL */
711  MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen);
712 
713  /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
714  /* Offset trim time: during calibration, minimum time needed between */
715  /* two steps to have 1 mV accuracy */
716  HAL_Delay(OPAMP_TRIMMING_DELAY);
717 
718  if (READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT) != 0U)
719  {
720  /* OPAMP_CSR_CALOUT is HIGH try higher trimming */
721  trimmingvaluen -= delta;
722  }
723  else
724  {
725  /* OPAMP_CSR_CALOUT is LOW try lower trimming */
726  trimmingvaluen += delta;
727  }
728  /* Divide range by 2 to continue dichotomy sweep */
729  delta >>= 1U;
730  }
731 
732  /* Still need to check if right calibration is current value or one step below */
733  /* Indeed the first value that causes the OUTCAL bit to change from 0 to 1 */
734  /* Set candidate trimming */
735  MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen);
736 
737  /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
738  /* Offset trim time: during calibration, minimum time needed between */
739  /* two steps to have 1 mV accuracy */
740  HAL_Delay(OPAMP_TRIMMING_DELAY);
741 
742  if ((READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT)) == 0U)
743  {
744  /* Trimming value is actually one value more */
745  trimmingvaluen++;
746  /* Set right trimming */
747  MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETN, trimmingvaluen);
748  }
749 
750  /* 2nd calibration - P */
751  SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALSEL);
752 
753  /* Init trimming counter */
754  /* Medium value */
755  trimmingvaluep = 16U;
756  delta = 8U;
757 
758  while (delta != 0U)
759  {
760  /* Set candidate trimming */
761  /* OPAMP_POWERMODE_NORMAL */
762  MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep<<OPAMP_INPUT_NONINVERTING));
763 
764  /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
765  /* Offset trim time: during calibration, minimum time needed between */
766  /* two steps to have 1 mV accuracy */
767  HAL_Delay(OPAMP_TRIMMING_DELAY);
768 
769  if (READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT) != 0U)
770  {
771  /* OPAMP_CSR_CALOUT is HIGH try higher trimming */
772  trimmingvaluep += delta;
773  }
774  else
775  {
776  /* OPAMP_CSR_CALOUT is LOW try lower trimming */
777  trimmingvaluep -= delta;
778  }
779 
780  /* Divide range by 2 to continue dichotomy sweep */
781  delta >>= 1U;
782  }
783 
784  /* Still need to check if right calibration is current value or one step below */
785  /* Indeed the first value that causes the OUTCAL bit to change from 1 to 0 */
786  /* Set candidate trimming */
787  MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep<<OPAMP_INPUT_NONINVERTING));
788 
789  /* OFFTRIMmax delay 1 ms as per datasheet (electrical characteristics */
790  /* Offset trim time: during calibration, minimum time needed between */
791  /* two steps to have 1 mV accuracy */
792  HAL_Delay(OPAMP_TRIMMING_DELAY);
793 
794  if (READ_BIT(hopamp->Instance->CSR, OPAMP_CSR_CALOUT) != 0U)
795  {
796  /* Trimming value is actually one value more */
797  trimmingvaluep++;
798  MODIFY_REG(*tmp_opamp_reg_trimming, OPAMP_OTR_TRIMOFFSETP, (trimmingvaluep<<OPAMP_INPUT_NONINVERTING));
799  }
800 
801  /* Disable the OPAMP */
802  CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
803 
804  /* Disable calibration & set normal mode (operating mode) */
805  CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_CALON);
806 
807  /* Self calibration is successful */
808  /* Store calibration(user trimming) results in init structure. */
809 
810  /* Set user trimming mode */
811  hopamp->Init.UserTrimming = OPAMP_TRIMMING_USER;
812 
813  /* Affect calibration parameters depending on mode normal/low power */
814  if (hopamp->Init.PowerMode != OPAMP_POWERMODE_LOWPOWER)
815  {
816  /* Write calibration result N */
817  hopamp->Init.TrimmingValueN = trimmingvaluen;
818  /* Write calibration result P */
819  hopamp->Init.TrimmingValueP = trimmingvaluep;
820  }
821  else
822  {
823  /* Write calibration result N */
824  hopamp->Init.TrimmingValueNLowPower = trimmingvaluen;
825  /* Write calibration result P */
826  hopamp->Init.TrimmingValuePLowPower = trimmingvaluep;
827  }
828 
829  /* Restore OPAMP mode after calibration */
830  MODIFY_REG(hopamp->Instance->CSR, OPAMP_CSR_OPAMODE, opampmode);
831  }
832  else
833  {
834  /* OPAMP can not be calibrated from this mode */
835  status = HAL_ERROR;
836  }
837  }
838  return status;
839 }
void HAL_Delay(uint32_t Delay)
This function provides minimum delay (in milliseconds) based on variable incremented.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_OPAMP_Start()

HAL_StatusTypeDef HAL_OPAMP_Start ( OPAMP_HandleTypeDef hopamp)

Start the OPAMP.

Parameters
hopampOPAMP handle
Return values
HALstatus

Definition at line 545 of file stm32l4xx_hal_opamp.c.

546 {
547  HAL_StatusTypeDef status = HAL_OK;
548 
549  /* Check the OPAMP handle allocation */
550  /* Check if OPAMP locked */
551  if(hopamp == NULL)
552  {
553  status = HAL_ERROR;
554  }
555  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
556  {
557  status = HAL_ERROR;
558  }
559  else
560  {
561  /* Check the parameter */
562  assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
563 
564  if(hopamp->State == HAL_OPAMP_STATE_READY)
565  {
566  /* Enable the selected opamp */
567  SET_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
568 
569  /* Update the OPAMP state*/
570  /* From HAL_OPAMP_STATE_READY to HAL_OPAMP_STATE_BUSY */
571  hopamp->State = HAL_OPAMP_STATE_BUSY;
572  }
573  else
574  {
575  status = HAL_ERROR;
576  }
577 
578  }
579  return status;
580 }
return HAL_OK
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_OPAMP_Stop()

HAL_StatusTypeDef HAL_OPAMP_Stop ( OPAMP_HandleTypeDef hopamp)

Stop the OPAMP.

Parameters
hopampOPAMP handle
Return values
HALstatus

Definition at line 587 of file stm32l4xx_hal_opamp.c.

588 {
589  HAL_StatusTypeDef status = HAL_OK;
590 
591  /* Check the OPAMP handle allocation */
592  /* Check if OPAMP locked */
593  /* Check if OPAMP calibration ongoing */
594  if(hopamp == NULL)
595  {
596  status = HAL_ERROR;
597  }
598  else if(hopamp->State == HAL_OPAMP_STATE_BUSYLOCKED)
599  {
600  status = HAL_ERROR;
601  }
602  else if(hopamp->State == HAL_OPAMP_STATE_CALIBBUSY)
603  {
604  status = HAL_ERROR;
605  }
606  else
607  {
608  /* Check the parameter */
609  assert_param(IS_OPAMP_ALL_INSTANCE(hopamp->Instance));
610 
611  if(hopamp->State == HAL_OPAMP_STATE_BUSY)
612  {
613  /* Disable the selected opamp */
614  CLEAR_BIT (hopamp->Instance->CSR, OPAMP_CSR_OPAMPxEN);
615 
616  /* Update the OPAMP state*/
617  /* From HAL_OPAMP_STATE_BUSY to HAL_OPAMP_STATE_READY*/
618  hopamp->State = HAL_OPAMP_STATE_READY;
619  }
620  else
621  {
622  status = HAL_ERROR;
623  }
624  }
625  return status;
626 }
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
return HAL_OK
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))