ST / BSP_DISCO_L4R9I

Dependents:   DISCO_L4R9I-LCD-demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4r9i_discovery_idd.c Source File

stm32l4r9i_discovery_idd.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4r9i_discovery_idd.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of firmware functions to manage the
00006   *          Idd measurement driver for STM32L4R9I_DISCOVERY board.
00007   ******************************************************************************
00008   * @attention
00009   *
00010   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
00011   * All rights reserved.</center></h2>
00012   *
00013   * This software component is licensed by ST under BSD 3-Clause license,
00014   * the "License"; You may not use this file except in compliance with the
00015   * License. You may obtain a copy of the License at:
00016   *                        opensource.org/licenses/BSD-3-Clause
00017   *
00018   ******************************************************************************
00019   */
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32l4r9i_discovery_idd.h"
00023 #include "stm32l4r9i_discovery_io.h"
00024 
00025 /** @addtogroup BSP
00026   * @{
00027   */
00028 
00029 /** @addtogroup STM32L4R9I_DISCOVERY
00030   * @{
00031   */
00032 
00033 /** @defgroup STM32L4R9I_DISCOVERY_IDD STM32L4R9I_DISCOVERY IDD
00034   * @brief This file includes the Idd driver for STM32L4R9I_DISCOVERY board.
00035   *        It allows user to measure MCU Idd current on board, especially in
00036   *        different low power modes.
00037   * @{
00038   */
00039 
00040 /** @defgroup STM32L4R9I_DISCOVERY_IDD_Private_Variables Private Variables
00041   * @{
00042   */
00043 static IDD_DrvTypeDef *IddDrv;
00044 
00045 /**
00046   * @}
00047   */
00048 
00049 /** @defgroup STM32L4R9I_DISCOVERY_IDD_Exported_Functions Exported Functions
00050   * @{
00051   */
00052 
00053 /**
00054   * @brief  Configures IDD measurement component.
00055   * @retval IDD_OK if no problem during initialization
00056   */
00057 uint8_t BSP_IDD_Init(void)
00058 {
00059   IDD_ConfigTypeDef iddconfig = {0};
00060   uint8_t mfxstm32l152_id = 0;
00061   uint8_t ret = 0;
00062 
00063   /* Initialize IO functionalities (MFX) used by control amp */
00064   BSP_IO_Init();
00065 
00066   /* wake up mfx component in case it went to standby mode */
00067   mfxstm32l152_idd_drv.WakeUp(IDD_I2C_ADDRESS);
00068   HAL_Delay(5);
00069 
00070   /* Read ID and verify if the MFX is ready */
00071   mfxstm32l152_id = mfxstm32l152_idd_drv.ReadID(IDD_I2C_ADDRESS);
00072 
00073   if((mfxstm32l152_id == MFXSTM32L152_ID_1) || (mfxstm32l152_id == MFXSTM32L152_ID_2))
00074   {
00075     /* Initialize the Idd driver structure */
00076     IddDrv = &mfxstm32l152_idd_drv;
00077 
00078     /* Initialize the Idd driver */
00079     if(IddDrv->Init != NULL)
00080     {
00081       IddDrv->Init(IDD_I2C_ADDRESS);
00082     }
00083 
00084     /* Configure Idd component with default values */
00085     iddconfig.AmpliGain = DISCOVERY_IDD_AMPLI_GAIN;
00086     iddconfig.VddMin = DISCOVERY_IDD_VDD_MIN;
00087     iddconfig.Shunt0Value = DISCOVERY_IDD_SHUNT0_VALUE;
00088     iddconfig.Shunt1Value = DISCOVERY_IDD_SHUNT1_VALUE;
00089     iddconfig.Shunt2Value = DISCOVERY_IDD_SHUNT2_VALUE;
00090     iddconfig.Shunt3Value = 0;
00091     iddconfig.Shunt4Value = DISCOVERY_IDD_SHUNT4_VALUE;
00092     iddconfig.Shunt0StabDelay = DISCOVERY_IDD_SHUNT0_STABDELAY;
00093     iddconfig.Shunt1StabDelay = DISCOVERY_IDD_SHUNT1_STABDELAY;
00094     iddconfig.Shunt2StabDelay = DISCOVERY_IDD_SHUNT2_STABDELAY;
00095     iddconfig.Shunt3StabDelay = 0;
00096     iddconfig.Shunt4StabDelay = DISCOVERY_IDD_SHUNT4_STABDELAY;
00097     iddconfig.ShuntNbOnBoard = MFXSTM32L152_IDD_SHUNT_NB_4;
00098     iddconfig.ShuntNbUsed = MFXSTM32L152_IDD_SHUNT_NB_4;
00099     iddconfig.VrefMeasurement = MFXSTM32L152_IDD_VREF_AUTO_MEASUREMENT_ENABLE;
00100     iddconfig.Calibration = MFXSTM32L152_IDD_AUTO_CALIBRATION_ENABLE;
00101     iddconfig.PreDelayUnit = MFXSTM32L152_IDD_PREDELAY_20_MS;
00102     iddconfig.PreDelayValue = 0x7F;
00103     iddconfig.MeasureNb = 100;
00104     iddconfig.DeltaDelayUnit= MFXSTM32L152_IDD_DELTADELAY_0_5_MS;
00105     iddconfig.DeltaDelayValue = 10;
00106     BSP_IDD_Config(iddconfig);
00107 
00108     ret = IDD_OK;
00109   }
00110   else
00111   {
00112     ret = IDD_ERROR;
00113   }
00114 
00115   return ret;
00116 }
00117 
00118 /**
00119   * @brief  Unconfigures IDD measurement component.
00120   * @retval IDD_OK if no problem during deinitialization
00121   */
00122 void BSP_IDD_DeInit(void)
00123 {
00124   if(IddDrv->DeInit!= NULL)
00125   {
00126     IddDrv->DeInit(IDD_I2C_ADDRESS);
00127   }
00128 }
00129 
00130 /**
00131   * @brief  Reset Idd measurement component.
00132   * @retval None
00133   */
00134 void BSP_IDD_Reset(void)
00135 {
00136   if(IddDrv->Reset != NULL)
00137   {
00138     IddDrv->Reset(IDD_I2C_ADDRESS);
00139   }
00140 }
00141 
00142 /**
00143   * @brief  Turn Idd measurement component in low power (standby/sleep) mode
00144   * @retval None
00145   */
00146 void BSP_IDD_LowPower(void)
00147 {
00148   if(IddDrv->LowPower != NULL)
00149   {
00150     IddDrv->LowPower(IDD_I2C_ADDRESS);
00151   }
00152 }
00153 
00154 /**
00155   * @brief  Start Measurement campaign
00156   * @retval None
00157   */
00158 void BSP_IDD_StartMeasure(void)
00159 {
00160 
00161   /* Activate the OPAMP used ny the MFX to measure the current consumption */
00162   BSP_IO_ConfigPin(IDD_AMP_CONTROL_PIN, IO_MODE_OUTPUT);
00163   BSP_IO_WritePin(IDD_AMP_CONTROL_PIN, GPIO_PIN_RESET);
00164 
00165   if(IddDrv->Start != NULL)
00166   {
00167     IddDrv->Start(IDD_I2C_ADDRESS);
00168   }
00169 }
00170 
00171 /**
00172   * @brief  Configure Idd component
00173   * @param  IddConfig: structure of idd parameters
00174   * @retval None
00175   */
00176 void BSP_IDD_Config(IDD_ConfigTypeDef IddConfig)
00177 {
00178   if(IddDrv->Config != NULL)
00179   {
00180     IddDrv->Config(IDD_I2C_ADDRESS, IddConfig);
00181   }
00182 }
00183 
00184 /**
00185   * @brief  Get Idd current value.
00186   * @param  IddValue: Pointer on u32 to store Idd. Value unit is 10 nA.
00187   * @retval None
00188   */
00189 void BSP_IDD_GetValue(uint32_t *IddValue)
00190 {
00191   /* De-activate the OPAMP used ny the MFX to measure the current consumption */
00192   BSP_IO_ConfigPin(IDD_AMP_CONTROL_PIN, IO_MODE_OUTPUT);
00193   BSP_IO_WritePin(IDD_AMP_CONTROL_PIN, GPIO_PIN_RESET);
00194 
00195   if(IddDrv->GetValue != NULL)
00196   {
00197     IddDrv->GetValue(IDD_I2C_ADDRESS, IddValue);
00198   }
00199 }
00200 
00201 /**
00202   * @brief  Enable Idd interrupt that warn end of measurement
00203   * @retval None
00204   */
00205 void BSP_IDD_EnableIT(void)
00206 {
00207   if(IddDrv->EnableIT != NULL)
00208   {
00209     IddDrv->EnableIT(IDD_I2C_ADDRESS);
00210   }
00211 }
00212 
00213 /**
00214   * @brief  Clear Idd interrupt that warn end of measurement
00215   * @retval None
00216   */
00217 void BSP_IDD_ClearIT(void)
00218 {
00219   if(IddDrv->ClearIT != NULL)
00220   {
00221     IddDrv->ClearIT(IDD_I2C_ADDRESS);
00222   }
00223 }
00224 
00225 /**
00226   * @brief  Get Idd interrupt status
00227   * @retval status
00228   */
00229 uint8_t BSP_IDD_GetITStatus(void)
00230 {
00231   if(IddDrv->GetITStatus != NULL)
00232   {
00233     return (IddDrv->GetITStatus(IDD_I2C_ADDRESS));
00234   }
00235   else
00236   {
00237     return IDD_ERROR;
00238   }
00239 }
00240 
00241 /**
00242   * @brief  Disable Idd interrupt that warn end of measurement
00243   * @retval None
00244   */
00245 void BSP_IDD_DisableIT(void)
00246 {
00247   if(IddDrv->DisableIT != NULL)
00248   {
00249     IddDrv->DisableIT(IDD_I2C_ADDRESS);
00250   }
00251 }
00252 
00253 /**
00254   * @brief  Get Error Code .
00255   * @retval Error code or error status
00256   */
00257 uint8_t BSP_IDD_ErrorGetCode(void)
00258 {
00259   if(IddDrv->ErrorGetSrc != NULL)
00260   {
00261     if((IddDrv->ErrorGetSrc(IDD_I2C_ADDRESS) & MFXSTM32L152_IDD_ERROR_SRC) != RESET)
00262     {
00263       if(IddDrv->ErrorGetCode != NULL)
00264       {
00265        return IddDrv->ErrorGetCode(IDD_I2C_ADDRESS);
00266       }
00267       else
00268       {
00269         return IDD_ERROR;
00270       }
00271     }
00272     else
00273     {
00274       return IDD_ERROR;
00275     }
00276   }
00277   else
00278   {
00279     return IDD_ERROR;
00280   }
00281 }
00282 
00283 
00284 /**
00285   * @brief  Enable error interrupt that warn end of measurement
00286   * @retval None
00287   */
00288 void BSP_IDD_ErrorEnableIT(void)
00289 {
00290   if(IddDrv->ErrorEnableIT != NULL)
00291   {
00292     IddDrv->ErrorEnableIT(IDD_I2C_ADDRESS);
00293   }
00294 }
00295 
00296 /**
00297   * @brief  Clear Error interrupt that warn end of measurement
00298   * @retval None
00299   */
00300 void BSP_IDD_ErrorClearIT(void)
00301 {
00302   if(IddDrv->ErrorClearIT != NULL)
00303   {
00304     IddDrv->ErrorClearIT(IDD_I2C_ADDRESS);
00305   }
00306 }
00307 
00308 /**
00309   * @brief  Get Error interrupt status
00310   * @retval Status
00311   */
00312 uint8_t BSP_IDD_ErrorGetITStatus(void)
00313 {
00314   if(IddDrv->ErrorGetITStatus != NULL)
00315   {
00316     return (IddDrv->ErrorGetITStatus(IDD_I2C_ADDRESS));
00317   }
00318   else
00319   {
00320     return 0;
00321   }
00322 }
00323 
00324 /**
00325   * @brief  Disable Error interrupt
00326   * @retval None
00327   */
00328 void BSP_IDD_ErrorDisableIT(void)
00329 {
00330   if(IddDrv->ErrorDisableIT != NULL)
00331   {
00332     IddDrv->ErrorDisableIT(IDD_I2C_ADDRESS);
00333   }
00334 }
00335 
00336 /**
00337   * @brief  Wake up Idd measurement component.
00338   * @retval None
00339   */
00340 void BSP_IDD_WakeUp(void)
00341 {
00342   if(IddDrv->WakeUp != NULL)
00343   {
00344     IddDrv->WakeUp(IDD_I2C_ADDRESS);
00345   }
00346 }
00347 
00348 /**
00349   * @}
00350   */
00351 
00352 /**
00353   * @}
00354   */
00355 
00356 /**
00357   * @}
00358   */
00359 
00360 /**
00361   * @}
00362   */
00363 
00364 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00365