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

Add STM32G0 ADC driver #318

Merged
merged 6 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,18 @@ can easily configure them for you specific needs.
<td align="center">SIEMENS-S65</td>
<td align="center">SIEMENS-S75</td>
<td align="center">SK6812</td>
<td align="center">SK9822</td>
<td align="center">SSD1306</td>
<td align="center">TCS3414</td>
<td align="center">TCS3472</td>
</tr><tr>
<td align="center">TCS3472</td>
<td align="center">TLC594X</td>
<td align="center">TMP102</td>
<td align="center">TMP175</td>
<td align="center">VL53L0</td>
<td align="center">VL6180</td>
<td align="center">WS2812</td>
</tr><tr>
<td align="center">WS2812</td>
</tr>
</table>
<!--/drivertable-->
Expand Down
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(Vref);
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(Vref);
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
15 changes: 10 additions & 5 deletions src/modm/board/nucleo_g071rb/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def prepare(module, options):
return False

module.depends(":platform:core", ":platform:gpio", ":platform:clock", ":platform:uart:2",
":debug", ":architecture:clock", ":architecture:clock")
":debug", ":architecture:clock")
return True

def build(env):
Expand All @@ -32,7 +32,12 @@ def build(env):
env.template("../board.cpp.in", "board.cpp")
env.copy('.')

# Waiting on this patch to OpenOCD http://openocd.zylin.com/#/c/4807
# env.outbasepath = "modm/openocd/modm/board/"
# env.copy(repopath("tools/openocd/modm/st_nucleo_g0.cfg"), "st_nucleo_g0.cfg")
# env.collect(":build:openocd.source", "modm/board/st_nucleo_g0.cfg")
# Waiting on OpenOCD to add support for STM32G0
env.log.warning("\nUploading firmware and debugging for STM32G0 via OpenOCD requires this patch: http://openocd.zylin.com/#/c/4807"
"\nAlternatively you may copy the compiled binary onto the Nucleo USB Mass-Storage Device manually:"
"\n $ scons bin && cp path/to/build/release/project.bin /Volumes/NODE_G071RB")

# The patch doesn't include board config file though
env.outbasepath = "modm/openocd/modm/board/"
env.copy(repopath("tools/openocd/modm/st_nucleo_g0.cfg"), "st_nucleo_g0.cfg")
env.collect(":build:openocd.source", "modm/board/st_nucleo_g0.cfg")
4 changes: 2 additions & 2 deletions src/modm/driver/pwm/apa102.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class Apa102
Apa102() : data{0,0,0,0}
{
clear();
// set the flushing bits correctly
// using 0x00 as flushing bits makes this compatible with sk9822
for (size_t ii=4 + 4*LEDs; ii < length; ii++)
data[ii] = 0xff;
data[ii] = 0x00;
}

void
Expand Down
22 changes: 22 additions & 0 deletions src/modm/driver/pwm/sk9822.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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/.
*/
// ----------------------------------------------------------------------------

#pragma once
#include "apa102.hpp"

namespace modm
{

/// @ingroup modm_driver_sk9822
template< class SpiMaster, size_t LEDs >
using Sk9822 = Apa102<SpiMaster, LEDs>;

} // namespace modm
40 changes: 40 additions & 0 deletions src/modm/driver/pwm/sk9822.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# 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/.
# -----------------------------------------------------------------------------


def init(module):
module.name = ":driver:sk9822"
module.description = """\
# SK9822 RGB LED Driver

Drives any number of chained SK9822 RGB LEDs using SPI signals SCK and MOSI up
to a few dozen MHz. Due to the synchronous clock, there are no special
restrictions on protocol timing, making this driver safe to use with interrupts
enabled and/or with an RTOS.

The internal data buffer size is 4B for start of frame, 4B for every LED and 1B
for every 16 LEDs as end of frame.

References:

- ["SK9822 – a clone of the APA102?"][led]

[led]: https://cpldcpu.wordpress.com/2016/12/13/sk9822-a-clone-of-the-apa102/
"""

def prepare(module, options):
module.depends(":driver:apa102")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/pwm"
env.copy("sk9822.hpp")
15 changes: 15 additions & 0 deletions src/modm/math/algorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include "algorithm/enumerate.hpp"
#include "algorithm/prescaler.hpp"
45 changes: 45 additions & 0 deletions src/modm/math/algorithm/enumerate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2018, Nathan Reed
* 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/.
*/
// ----------------------------------------------------------------------------

#pragma once

#include <stdint.h>
#include <tuple>

namespace modm
{

// From: http://reedbeta.com/blog/python-like-enumerate-in-cpp17/
/// @ingroup modm_math_algorithm
template <typename T,
typename TIter = decltype(std::begin(std::declval<T>())),
typename = decltype(std::end(std::declval<T>()))>
constexpr auto enumerate(T && iterable)
{
struct iterator
{
size_t i;
TIter iter;
bool operator != (const iterator & other) const { return iter != other.iter; }
void operator ++ () { ++i; ++iter; }
auto operator * () const { return std::tie(i, *iter); }
};
struct iterable_wrapper
{
T iterable;
auto begin() { return iterator{ 0, std::begin(iterable) }; }
auto end() { return iterator{ 0, std::end(iterable) }; }
};
return iterable_wrapper{ std::forward<T>(iterable) };
}

} // namespace modm
25 changes: 25 additions & 0 deletions src/modm/math/algorithm/module.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2017, Fabian Greif
#
# 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/.
# -----------------------------------------------------------------------------

def init(module):
module.name = ":math:algorithm"
module.description = "Math Algorithms"

def prepare(module, options):
module.depends(":math:units")
return True

def build(env):
env.outbasepath = "modm/src/modm/math/algorithm"
env.copy(".")
env.copy("../algorithm.hpp")
Loading