Skip to content

Commit

Permalink
WIP Add custom STM32C031 board examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehaenger authored and salkinium committed Sep 22, 2024
1 parent db85cec commit 021fa61
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ jobs:
if: always()
run: |
(cd examples && ../tools/scripts/examples_compile.py nucleo_h743zi nucleo_h723zg stm32h750vbt6_devebox)
- name: Examples STM32C0 Series
if: always()
run: |
(cd examples && ../tools/scripts/examples_compile.py stm32c0x)
stm32f4-examples-1:
runs-on: ubuntu-22.04
Expand Down
81 changes: 81 additions & 0 deletions examples/stm32c0x/adc-sequence/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2024, Jörg Ebeling
*
* 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 "../custom-board.hpp"

using namespace Board;
using namespace std::chrono_literals;

#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper<proto_uart::Uart, modm::IOBuffer::BlockIfFull> loggerDevice;
// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);

std::array<uint16_t, 4> adc_data_buf = {};

static void
adc_handler()
{
static uint8_t adc_data_idx = 0;

if (Adc1::getInterruptFlags() & Adc1::InterruptFlag::EndOfConversion)
{
Adc1::acknowledgeInterruptFlags(Adc1::InterruptFlag::EndOfConversion);
adc_data_buf.at(adc_data_idx) = Adc1::getValue();
adc_data_idx++;
}
if (Adc1::getInterruptFlags() & Adc1::InterruptFlag::EndOfSequence)
{
Adc1::acknowledgeInterruptFlags(Adc1::InterruptFlag::EndOfSequence);
adc_data_idx = 0;
}
}

// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
MODM_LOG_INFO << "Board initialized" << modm::endl << modm::flush;

Adc1::connect<GpioA1::In1, GpioA2::In2>();
Adc1::initialize<Board::SystemClock, Adc1::ClockMode::Asynchronous,
24_MHz>(); // 24MHz/160.5 sample time=6.6us (fulfill Ts_temp of 5us)
Adc1::setResolution(Adc1::Resolution::Bits12);
Adc1::setRightAdjustResult();
Adc1::setSampleTime(Adc1::SampleTime::Cycles160_5);

const Adc1::Channel sequence[4] = {Adc1::Channel::InternalReference, Adc1::Channel::In2,
Adc1::Channel::In1, Adc1::Channel::Temperature};
Adc1::setChannels(sequence);

Adc1::enableInterruptVector(15);
Adc1::enableInterrupt(Adc1::Interrupt::EndOfConversion | Adc1::Interrupt::EndOfSequence);
AdcInterrupt1::attachInterruptHandler(adc_handler);
Adc1::enableFreeRunningMode();
Adc1::startConversion();

while (true)
{
LedGn::toggle();
modm::delay(500ms);

MODM_LOG_INFO << "ADC data ";
for (size_t i = 0; auto value : adc_data_buf) { MODM_LOG_INFO << i << "=" << value << " "; }
MODM_LOG_INFO << modm::endl << modm::flush;
}
return 0;
}
27 changes: 27 additions & 0 deletions examples/stm32c0x/adc-sequence/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<library>
<repositories>
<repository>
<path>../../../repo.lb</path>
</repository>
</repositories>
<options>
<option name="modm:target">stm32c011f6p6</option>
<option name="modm:build:build.path">../../../build/stm32c0x/adc-sequence</option>
</options>
<collectors>
<!-- You need to install https://xpack-dev-tools.github.io/openocd-xpack/ >= 0.12.0-3 for STM32C0x support -->
<collect name="modm:build:openocd.source">interface/stlink.cfg</collect>
<collect name="modm:build:openocd.source">target/stm32c0x.cfg</collect>
</collectors>
<modules>
<module>modm:architecture:clock</module>
<module>modm:build:scons</module>
<module>modm:debug</module>
<module>modm:platform:clock</module>
<module>modm:platform:core</module>
<module>modm:platform:gpio</module>
<module>modm:platform:adc</module>
<module>modm:platform:uart:1</module>
<module>modm:platform:uart:2</module>
</modules>
</library>
57 changes: 57 additions & 0 deletions examples/stm32c0x/adc-simple/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 "../custom-board.hpp"

using namespace Board;
using namespace std::chrono_literals;

#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper<proto_uart::Uart, modm::IOBuffer::BlockIfFull> loggerDevice;
// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);

// ----------------------------------------------------------------------------
int
main()
{
uint16_t vref;

Board::initialize();
Adc1::connect<GpioA1::In1, GpioA2::In2>();
Adc1::initialize<Board::SystemClock, Adc1::ClockMode::Asynchronous,
24_MHz>(); // 24MHz/160.5 sample time=6.6us (fulfill Ts_temp of 5us)

Adc1::setResolution(Adc1::Resolution::Bits12);
Adc1::setSampleTime(Adc1::SampleTime::Cycles160_5);
Adc1::setRightAdjustResult();
Adc1::enableOversampling(Adc1::OversampleRatio::x16, Adc1::OversampleShift::Div16);

while (true)
{
LedGn::toggle();
modm::delay(500ms);

vref = Adc1::readInternalVoltageReference();
MODM_LOG_INFO << "vref=" << vref << "mV "
<< "temperature=" << Adc1::readTemperature(vref) << "deg.C "
<< "ch1=" << (vref * Adc1::readChannel(Adc1::Channel::In1) / 4096U) << "mV "
<< "ch2=" << (vref * Adc1::readChannel(Adc1::Channel::In2) / 4096U) << "mV "
<< modm::endl
<< modm::flush;
}
return 0;
}
27 changes: 27 additions & 0 deletions examples/stm32c0x/adc-simple/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<library>
<repositories>
<repository>
<path>../../../repo.lb</path>
</repository>
</repositories>
<options>
<option name="modm:target">stm32c011f6p6</option>
<option name="modm:build:build.path">../../../build/stm32c0x/adc-simple</option>
</options>
<collectors>
<!-- You need to install https://xpack-dev-tools.github.io/openocd-xpack/ >= 0.12.0-3 for STM32C0x support -->
<collect name="modm:build:openocd.source">interface/stlink.cfg</collect>
<collect name="modm:build:openocd.source">target/stm32c0x.cfg</collect>
</collectors>
<modules>
<module>modm:architecture:clock</module>
<module>modm:build:scons</module>
<module>modm:debug</module>
<module>modm:platform:clock</module>
<module>modm:platform:core</module>
<module>modm:platform:gpio</module>
<module>modm:platform:adc</module>
<module>modm:platform:uart:1</module>
<module>modm:platform:uart:2</module>
</modules>
</library>
36 changes: 36 additions & 0 deletions examples/stm32c0x/blink-clock/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024, Jörg Ebeling
*
* 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/platform.hpp>

#include "../custom-board.hpp"

#define BLINK_MILLIS 1000

using namespace modm::platform;
using namespace std::chrono_literals;

uint32_t next_blink = modm::Clock::now().time_since_epoch().count() + BLINK_MILLIS;

int
main()
{
Board::initialize();
Board::LedGn::set();
while (true)
{
if (modm::Clock::now().time_since_epoch().count() >= next_blink)
{
next_blink += BLINK_MILLIS;
Board::LedGn::toggle();
Board::LedRd::toggle();
}
}
}
21 changes: 21 additions & 0 deletions examples/stm32c0x/blink-clock/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<library>
<repositories>
<repository>
<path>../../../repo.lb</path>
</repository>
</repositories>
<options>
<option name="modm:target">stm32c011f6p6</option>
<option name="modm:build:build.path">../../../build/stm32c0x/blink-clock</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:core</module>
<module>modm:platform:rcc</module>
<module>modm:platform:gpio</module>
<module>modm:platform:uart:1</module>
<module>modm:platform:uart:2</module>
<module>modm:architecture:clock</module>
<module>modm:build:scons</module>
</modules>
</library>
28 changes: 28 additions & 0 deletions examples/stm32c0x/blink-delay/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, Jörg Ebeling
*
* 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/platform.hpp>
#include "../custom-board.hpp"

using namespace modm::platform;
using namespace std::chrono_literals;

int
main()
{
Board::initialize();
Board::LedGn::set();
while (true)
{
Board::LedGn::toggle();
Board::LedRd::toggle();
modm::delay(0.5s);
}
}
21 changes: 21 additions & 0 deletions examples/stm32c0x/blink-delay/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<library>
<repositories>
<repository>
<path>../../../repo.lb</path>
</repository>
</repositories>
<options>
<option name="modm:target">stm32c011f6p6</option>
<option name="modm:build:build.path">../../../build/stm32c0x/blink-delay</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:core</module>
<module>modm:platform:rcc</module>
<module>modm:platform:gpio</module>
<module>modm:platform:uart:1</module>
<module>modm:platform:uart:2</module>
<module>modm:architecture:delay</module>
<module>modm:build:scons</module>
</modules>
</library>
73 changes: 73 additions & 0 deletions examples/stm32c0x/blink-timer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2024, Jörg Ebeling
*
* 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/debug/logger.hpp>
#include <modm/platform.hpp>

#include "../custom-board.hpp"

using namespace Board;
using namespace std::chrono_literals;

// --- UART Debugging ---
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG
// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper<proto_uart::Uart, modm::IOBuffer::BlockIfFull> loggerDevice;
// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);


MODM_ISR(TIM14)
{
Timer14::acknowledgeInterruptFlags(Timer14::InterruptFlag::Update);
MODM_LOG_DEBUG << "Toggle green LED" << modm::endl;
LedGn::toggle();
}

MODM_ISR(TIM16)
{
Timer16::acknowledgeInterruptFlags(Timer16::InterruptFlag::Update);
MODM_LOG_DEBUG << "Toggle red LED" << modm::endl;
LedRd::toggle();
}

int
main()
{
Board::initialize();

// Initialize prototyping-UART for MODM_LOG_*
proto_uart::Uart::connect<proto_uart::Tx::Tx, proto_uart::Rx::Rx>();
proto_uart::Uart::initialize<SystemClock, 115200_Bd>();

MODM_LOG_INFO << "Board & Logger initialized" << modm::endl;

Timer14::enable();
Timer14::setMode(Timer14::Mode::UpCounter);
Timer14::setPeriod<Board::SystemClock>(1000ms);
Timer14::applyAndReset();
Timer14::start();
Timer14::enableInterrupt(Timer14::Interrupt::Update);
Timer14::enableInterruptVector(true, 5);

Timer16::enable();
Timer16::setMode(Timer16::Mode::UpCounter);
Timer16::setPeriod<Board::SystemClock>(250ms);
Timer16::applyAndReset();
Timer16::start();
Timer16::enableInterrupt(Timer16::Interrupt::Update);
Timer16::enableInterruptVector(true, 5);

while (true) {}
}
Loading

0 comments on commit 021fa61

Please sign in to comment.