Skip to content

Commit

Permalink
Merge pull request #131 from mcci-catena/issue125
Browse files Browse the repository at this point in the history
Fix #125: Turn on and off HSI clock if system clock use MSI clock.
  • Loading branch information
terrillmoore authored Feb 19, 2019
2 parents 684833b + 06dfb76 commit f4c5107
Showing 1 changed file with 77 additions and 5 deletions.
82 changes: 77 additions & 5 deletions src/lib/stm32/stm32l0/CatenaStm32L0_ReadAnalog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* CatenaStm32L0_ReadAnalog.cpp Tue Dec 18 2018 15:41:52 chwon */
/* CatenaStm32L0_ReadAnalog.cpp Mon Feb 18 2019 09:56:32 chwon */

/*
Expand All @@ -8,10 +8,10 @@ Module: CatenaStm32L0_ReadAnalog.cpp
CatenaStm32L0::ReadAnalog()
Version:
V0.13.0 Tue Dec 18 2018 15:41:52 chwon Edit level 1
V0.14.0 Mon Feb 18 2019 09:56:32 chwon Edit level 2
Copyright notice:
This file copyright (C) 2018 by
This file copyright (C) 2018-2019 by
MCCI Corporation
3520 Krums Corners Road
Expand All @@ -29,6 +29,9 @@ Revision history:
0.13.0 Tue Dec 18 2018 15:41:52 chwon
Module created.
0.14.0 Mon Feb 18 2019 09:56:32 chwon
Turn on and off HSI clock if system clock use MSI clock.
*/

#ifdef ARDUINO_ARCH_STM32
Expand All @@ -50,6 +53,7 @@ using namespace McciCatena;
\****************************************************************************/

constexpr unsigned long ADC_TIMEOUT = 10;
constexpr unsigned long ADC_CALIBRATE_TIME = 2000;
constexpr int STM32L0_ADC_CHANNEL_VREFINT = 17;
constexpr int STM32L0_ADC_CHANNEL_TEMPSENSOR = 18;

Expand Down Expand Up @@ -112,6 +116,18 @@ bool McciCatena::CatenaStm32L0_ReadAnalog(
/* make sure that the RCC is generating the low-frequency clock */
__HAL_RCC_ADC1_CLK_ENABLE();

#if CATENA_CFG_SYSCLK < 16
/* Set the Low Frequency Mode */
/* ADC->CCR = ADC_CCR_LFMEN; -- it's not working with MSI clock */

/* ADC requires HSI clock, so enable it now */
LL_RCC_HSI_Enable();
while (LL_RCC_HSI_IsReady() != 1U)
{
/* Wait for HSI ready */
}
#endif

fStatusOk = true;

if (fStatusOk)
Expand Down Expand Up @@ -178,6 +194,10 @@ bool McciCatena::CatenaStm32L0_ReadAnalog(
ADC1->CR &= ~ADC_CR_ADVREGEN;
ADC->CCR &= ~ADC_CCR_VREFEN;

#if CATENA_CFG_SYSCLK < 16
LL_RCC_HSI_Disable();
#endif

__HAL_RCC_ADC1_FORCE_RESET();
__HAL_RCC_ADC1_RELEASE_RESET();
__HAL_RCC_ADC1_CLK_DISABLE();
Expand Down Expand Up @@ -213,21 +233,38 @@ static bool AdcDisable(void)
while (ADC1->CR & ADC_CR_ADSTP)
{
if ((millis() - uTime) > ADC_TIMEOUT)
{
// gLog.printf(
// gLog.kError,
// "?AdcDisable:"
// " CR=%x\n",
// ADC1->CR
// );
return false;
}
}

ADC1->CR |= ADC_CR_ADDIS;
uTime = millis();
while (ADC1->CR & ADC_CR_ADEN)
{
if ((millis() - uTime) > ADC_TIMEOUT)
{
// gLog.printf(
// gLog.kError,
// "?AdcDisable:"
// " CR=%x\n",
// ADC1->CR
// );
return false;
}
}
return true;
}

static bool AdcCalibrate(void)
{
static uint32_t s_uLastCalibTime = 0;
uint32_t uTime;

if (ADC1->CR & ADC_CR_ADEN)
Expand All @@ -238,16 +275,30 @@ static bool AdcCalibrate(void)
// return false;
}

uTime = millis();
if ((uTime - s_uLastCalibTime) < ADC_CALIBRATE_TIME)
return true;

s_uLastCalibTime = uTime;

/* turn on the cal bit */
ADC1->ISR = ADC_ISR_EOCAL;
ADC1->CR |= ADC_CR_ADCAL;

/* wait for ADCAL to be reset */
uTime = millis();
while (! (ADC1->ISR & ADC_ISR_EOCAL))
{
if ((millis() - uTime) > ADC_TIMEOUT)
{
// gLog.printf(
// gLog.kError,
// "?AdcCalibrate:"
// " CCR=%x CR=%x ISR=%x\n",
// ADC->CCR,
// ADC1->CR,
// ADC1->ISR
// );
return false;
}
}

// uint32_t calData = ADC1->DR;
Expand Down Expand Up @@ -275,7 +326,16 @@ static bool AdcEnable(void)
while (!(ADC1->ISR & ADC_ISR_ADRDY))
{
if ((millis() - uTime) > ADC_TIMEOUT)
{
// gLog.printf(
// gLog.kError,
// "?AdcEnable:"
// " CR=%x ISR=%x\n",
// ADC1->CR,
// ADC1->ISR
// );
return false;
}
}
return true;
}
Expand All @@ -291,6 +351,12 @@ static bool AdcGetValue(uint32_t *value)
if ((millis() - uTime) > ADC_TIMEOUT)
{
*value = 0x0FFFu;
// gLog.printf(
// gLog.kError,
// "?AdcGetValue:"
// " ISR=%x\n",
// rAdcIsr
// );
return false;
}
}
Expand All @@ -312,6 +378,12 @@ static bool AdcGetValue(uint32_t *value)
return true;
}

// gLog.printf(
// gLog.kError,
// "?AdcGetValue:"
// " ISR=%x\n",
// rAdcIsr
// );
// no more data in this sequence
*value = 0x0FFFu;
return false;
Expand Down

0 comments on commit f4c5107

Please sign in to comment.