From a38feca20d909701311494beb839cbadd1d8d88c Mon Sep 17 00:00:00 2001 From: lgili Date: Fri, 10 Mar 2023 16:27:52 -0300 Subject: [PATCH] [examples] Add examples for same70 xplained --- examples/same70_xplained/adc/main.cpp | 38 ++++++++++++ examples/same70_xplained/adc/project.xml | 10 +++ examples/same70_xplained/blink/main.cpp | 44 +++++++++++++ examples/same70_xplained/blink/project.xml | 9 +++ examples/same70_xplained/pwm/main.cpp | 66 ++++++++++++++++++++ examples/same70_xplained/pwm/project.xml | 10 +++ examples/same70_xplained/timer/main.cpp | 72 ++++++++++++++++++++++ examples/same70_xplained/timer/project.xml | 10 +++ 8 files changed, 259 insertions(+) create mode 100644 examples/same70_xplained/adc/main.cpp create mode 100644 examples/same70_xplained/adc/project.xml create mode 100644 examples/same70_xplained/blink/main.cpp create mode 100644 examples/same70_xplained/blink/project.xml create mode 100644 examples/same70_xplained/pwm/main.cpp create mode 100644 examples/same70_xplained/pwm/project.xml create mode 100644 examples/same70_xplained/timer/main.cpp create mode 100644 examples/same70_xplained/timer/project.xml diff --git a/examples/same70_xplained/adc/main.cpp b/examples/same70_xplained/adc/main.cpp new file mode 100644 index 0000000000..338be2dac4 --- /dev/null +++ b/examples/same70_xplained/adc/main.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021, Jeff McBride + * Copyright (c) 2022, Christopher Durand + * Copyright (c) 2023, Luiz Carlos Gili + * + * 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 +#include + +using namespace modm::platform; +using namespace modm::literals; + +using Ad2 = GpioD30::Ad; // channel 0, pin AD2 on board +using Ad3 = GpioA19::Ad; // channel 8, pin AD8 on board + +int main() +{ + Board::initialize(); + + Afec0::initialize(); + Afec0::connect(); + + while (true) + { + MODM_LOG_INFO << "ADC Readings: "; + MODM_LOG_INFO.printf("%5d ", Afec0::readChannel(Afec0::getPinChannel())); + MODM_LOG_INFO.printf("%5d ", Afec0::readChannel(Afec0::getPinChannel())); + MODM_LOG_INFO << modm::endl; + + modm::delay(500ms); + } +} diff --git a/examples/same70_xplained/adc/project.xml b/examples/same70_xplained/adc/project.xml new file mode 100644 index 0000000000..afd9795e40 --- /dev/null +++ b/examples/same70_xplained/adc/project.xml @@ -0,0 +1,10 @@ + + modm:same70-xplained + + + + + modm:build:scons + modm:platform:adc:0 + + diff --git a/examples/same70_xplained/blink/main.cpp b/examples/same70_xplained/blink/main.cpp new file mode 100644 index 0000000000..5ca9c1db06 --- /dev/null +++ b/examples/same70_xplained/blink/main.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016-2017, Niklas Hauser + * Copyright (c) 2023, Luiz Carlos Gili + * + * 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 +#include +using namespace Board; + +int +main() +{ + Board::initialize(); + + // Use the logging streams to print some messages. + // Change MODM_LOG_LEVEL above to enable or disable these messages + MODM_LOG_DEBUG << "debug" << modm::endl; + MODM_LOG_INFO << "info" << modm::endl; + MODM_LOG_WARNING << "warning" << modm::endl; + MODM_LOG_ERROR << "error" << modm::endl; + + uint32_t counter(0); + + while (true) + { + Led0::toggle(); + modm::delay_ms(1000); + + if(ButtonSW0::read()){ + counter = 0; + } + + MODM_LOG_INFO << "loop: " << counter++ << modm::endl; + } + + return 0; +} diff --git a/examples/same70_xplained/blink/project.xml b/examples/same70_xplained/blink/project.xml new file mode 100644 index 0000000000..dcd8d2ee66 --- /dev/null +++ b/examples/same70_xplained/blink/project.xml @@ -0,0 +1,9 @@ + + modm:same70-xplained + + + + + modm:build:scons + + diff --git a/examples/same70_xplained/pwm/main.cpp b/examples/same70_xplained/pwm/main.cpp new file mode 100644 index 0000000000..69e42c2ca9 --- /dev/null +++ b/examples/same70_xplained/pwm/main.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023, Christopher Durand + * Copyright (c) 2023, Luiz Carlos Gili + * + * 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 + +using namespace Board; + +// Complementary output on PWM0 Channel2 High / Low outputs +using OutH2 = GpioC19; +using OutL2 = GpioD26; + +int main() +{ + Board::initialize(); + + MODM_LOG_INFO << "PWM Test" << modm::endl; + + Pwm0::connect(); + + Pwm0::initialize(); + // Period 1500 => MCLK / 1500 = 100 kHz + Pwm0::setPeriod(Pwm0::Channel::Ch2, 1500); + // 500/1500 = 33.3% duty-cycle + Pwm0::setDutyCycle(Pwm0::Channel::Ch2, 500); + + constexpr auto mode = Pwm0::ChannelMode() + .setClock(Pwm0::ChannelClock::Mck) + .setDeadTimeGeneration(Pwm0::DeadTimeGeneration::Enabled); + + Pwm0::setChannelMode(Pwm0::Channel::Ch2, mode); + + // Set 50 ticks dead-time for both high and low output + const auto deadTimeL = 50; // ticks + const auto deadTimeH = 50; // ticks + Pwm0::setDeadTime(Pwm0::Channel::Ch2, deadTimeL, deadTimeH); + + // Set all outputs to low in override mode + Pwm0::configureOutputOverrideValues(Pwm0::Outputs_t{}); + + Pwm0::enableChannelOutput(Pwm0::Channels::Ch2); + + while (true) + { + Led0::toggle(); + modm::delay(500ms); + + // Activate override mode to force outputs to low when button is pressed + const auto outputs = Pwm0::Outputs::Ch2PwmH | Pwm0::Outputs::Ch2PwmL; + if (ButtonSW0::read()) + Pwm0::setOutputOverride(outputs, false); + else + Pwm0::clearOutputOverride(outputs, false); + + } + + return 0; +} diff --git a/examples/same70_xplained/pwm/project.xml b/examples/same70_xplained/pwm/project.xml new file mode 100644 index 0000000000..9062de0783 --- /dev/null +++ b/examples/same70_xplained/pwm/project.xml @@ -0,0 +1,10 @@ + + modm:same70-xplained + + + + + modm:build:scons + modm:platform:pwm:* + + diff --git a/examples/same70_xplained/timer/main.cpp b/examples/same70_xplained/timer/main.cpp new file mode 100644 index 0000000000..bb6446d0bd --- /dev/null +++ b/examples/same70_xplained/timer/main.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023, Christopher Durand + * Copyright (c) 2023, Luiz Carlos Gili + * + * 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 +#include +#include + +using namespace Board; + +// use timer channel 10 to toggle led +MODM_ISR(TC10) +{ + // clear interrupt flags by reading + (void) TimerChannel10::getInterruptFlags(); + Led0::toggle(); +} + +using Tioa0 = GpioA0; + + +int main() +{ + Board::initialize(); + + MODM_LOG_INFO << "Timer / Counter Test" << modm::endl; + + // generate 25% duty-cycle 100 kHz PWM waveform on TIOA0 output (GpioA0) + TimerChannel0::initialize(); + TimerChannel0::connect(); + + TimerChannel0::setClockSource(TimerChannel0::ClockSource::Mck); + TimerChannel0::setWaveformMode(true); + // Up-counter, reset on register C compare match + TimerChannel0::setWaveformSelection(TimerChannel0::WavSel::Up_Rc); + + // Clear output on register A match, set on register C match + TimerChannel0::setTioaEffects(TimerChannel0::TioEffect::Clear, TimerChannel0::TioEffect::Set); + // period MCLK = 150 MHz / 1500 = 100 kHz + // duty-cycle 375 / 1500 = 25% + TimerChannel0::setRegA(375); + TimerChannel0::setRegC(1500); + TimerChannel0::enable(); + TimerChannel0::start(); + + // setup timer channel 10 to run interrupt at ~1 Hz from ~32 kHz internal slow clock + TimerChannel10::initialize(); + TimerChannel10::setClockSource(TimerChannel10::ClockSource::Slck); + // Toggle every 8192 / 32768 kHz = 0.25s => 1 Hz period + TimerChannel10::setRegC(8192); + TimerChannel10::setWaveformMode(true); + TimerChannel10::setWaveformSelection(TimerChannel10::WavSel::Up_Rc); + TimerChannel10::enableInterruptVector(true); + TimerChannel10::enableInterrupt(TimerChannel10::Interrupt::RcCompare); + TimerChannel10::enable(); + TimerChannel10::start(); + + while (true) + { + modm::delay(500ms); + } + + return 0; +} diff --git a/examples/same70_xplained/timer/project.xml b/examples/same70_xplained/timer/project.xml new file mode 100644 index 0000000000..014a5dea1f --- /dev/null +++ b/examples/same70_xplained/timer/project.xml @@ -0,0 +1,10 @@ + + modm:same70-xplained + + + + + modm:build:scons + modm:platform:timer:* + +