Skip to content

Commit

Permalink
[examples] Adapt STM32F0/STM32G0 ADC examples
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Dec 11, 2019
1 parent bcf1241 commit ae64cc2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
25 changes: 19 additions & 6 deletions examples/nucleo_f042k6/adc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,30 @@ main()
LedD13::setOutput();

Adc::connect<AdcIn1::In0>();
Adc::initialize(Adc::ClockMode::PCLKDividedBy4, Adc::CalibrationMode::DoNotCalibrate);
Adc::initialize<Board::SystemClock, Adc::ClockMode::Synchronous, 12_MHz>();

uint16_t Vref = Adc::readInternalVoltageReference();
int16_t Temp = Adc::readTemperature();
MODM_LOG_INFO << "Vref=" << Vref << modm::endl;
MODM_LOG_INFO << "Temp=" << Temp << modm::endl;

MODM_LOG_INFO << "TS_CAL1=" << uint16_t(*((volatile uint16_t *)0x1FFF77B8)) << modm::endl;
MODM_LOG_INFO << "TS_CAL2=" << uint16_t(*((volatile uint16_t *)0x1FFF77C2)) << modm::endl;
MODM_LOG_INFO << "VREFINT_CAL=" << uint16_t(*((volatile uint16_t *)0x1FFFF7BA)) << modm::endl;

Adc::setPinChannel<AdcIn1>();
Adc::setResolution(Adc::Resolution::Bits12);
Adc::setRightAdjustResult();
Adc::setSampleTime(Adc::SampleTime::Cycles239_5);
Adc::enableFreeRunningMode();
Adc::startConversion();

while (true)
{
LedD13::toggle();
modm::delayMilliseconds(250);
modm::delayMilliseconds(100);

Adc::setPinChannel<AdcIn1>();
Adc::startConversion();
while(!Adc::isConversionFinished()) { }
MODM_LOG_INFO << "ADC A0: " << Adc::getValue() << modm::endl;
MODM_LOG_INFO << "mV=" << (Vref * Adc::getValue() / 4095ul) << modm::endl;

}

Expand Down
52 changes: 52 additions & 0 deletions examples/nucleo_g071rb/adc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <modm/board.hpp>

using namespace Board;

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
LedD13::setOutput();
Adc1::connect<GpioB10::In11>();

Adc1::initialize<Board::SystemClock, Adc1::ClockMode::Asynchronous, 1_MHz>();

uint16_t Vref = Adc1::readInternalVoltageReference();
int16_t Temp = Adc1::readTemperature();
MODM_LOG_INFO << "Vref=" << Vref << modm::endl;
MODM_LOG_INFO << "Temp=" << Temp << modm::endl;

MODM_LOG_INFO << "TS_CAL1=" << uint16_t(*((volatile uint16_t *)0x1FFF75A8)) << modm::endl;
MODM_LOG_INFO << "TS_CAL2=" << uint16_t(*((volatile uint16_t *)0x1FFF75CA)) << modm::endl;
MODM_LOG_INFO << "VREFINT_CAL=" << uint16_t(*((volatile uint16_t *)0x1FFF75AA)) << modm::endl;
MODM_LOG_INFO << "ADC_CALFACT=" << uint16_t(ADC1->CALFACT) << modm::endl;

Adc1::setPinChannel<GpioB10>();
Adc1::setResolution(Adc1::Resolution::Bits12);
Adc1::setRightAdjustResult();
Adc1::setSampleTime(Adc1::SampleTime::Cycles160_5);
Adc1::enableFreeRunningMode();
Adc1::enableOversampling(Adc1::OversampleRatio::x256, Adc1::OversampleShift::Div256);
Adc1::startConversion();

while (true)
{
LedD13::toggle();
modm::delayMilliseconds(100);

MODM_LOG_INFO << "mV=" << (Vref * Adc1::getValue() / 4095ul) << modm::endl;
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_g071rb/adc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-g071rb</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g071rb/adc</option>
</options>
<modules>
<module>modm:platform:gpio</module>
<module>modm:platform:adc</module>
<module>modm:build:scons</module>
</modules>
</library>
2 changes: 1 addition & 1 deletion src/modm/board/nucleo_g071rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct SystemClock {
static constexpr uint32_t Spi1 = Apb;
static constexpr uint32_t I2s1 = Apb;
static constexpr uint32_t Timer1 = Apb;
static constexpr uint32_t Adc = Apb;
static constexpr uint32_t Adc1 = Apb;
static constexpr uint32_t Comp = Apb;
static constexpr uint32_t ItLine = Apb;
static constexpr uint32_t VrefBuf = Apb;
Expand Down

0 comments on commit ae64cc2

Please sign in to comment.