Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn on and off HSI clock if system clock use MSI clock. #131

Merged
merged 3 commits into from
Feb 19, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 76 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 nad off HSI clock if system clock use MSI clock.

*/

#ifdef ARDUINO_ARCH_STM32
Expand Down Expand Up @@ -112,6 +115,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 +193,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 +232,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 +274,30 @@ static bool AdcCalibrate(void)
// return false;
}

uTime = millis();
if ((uTime - s_uLastCalibTime) < 2000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2000 should be a constexpr defined at the top of the module.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will add ADC_CALIBRATE_TIME.

return true;

s_uLastCalibTime = uTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for now, but long term we should not use statics in C++ code. This belongs in the parent object (which is either the Catena or "cAdc" class that's embedded. AdcCalibrate should be a private method of the enclosing object, possibly with a friend class (for the individual sense points, if we decide to have a ADC measurement class so that we write gCatena.Vbus.Read() instead of the current replication of names).


/* 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 +325,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
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These printfs() add a lot of code to the image. We're getting close to needing a macro or something for this. We don't have a lot of flash space. But OK for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added for debugging purpose. I think we don't need right now. I will comment out all debug message.

return false;
}
}
return true;
}
Expand All @@ -291,6 +350,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 +377,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;
terrillmoore marked this conversation as resolved.
Show resolved Hide resolved
return false;
Expand Down