-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 */ | ||
|
||
/* | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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(); | ||
|
@@ -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) | ||
|
@@ -238,16 +274,30 @@ static bool AdcCalibrate(void) | |
// return false; | ||
} | ||
|
||
uTime = millis(); | ||
if ((uTime - s_uLastCalibTime) < 2000) | ||
return true; | ||
|
||
s_uLastCalibTime = uTime; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
/* 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; | ||
|
@@ -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 | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -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; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2000
should be aconstexpr
defined at the top of the module.There was a problem hiding this comment.
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.