Skip to content

Commit

Permalink
[examples] Add examples for same70 xplained
Browse files Browse the repository at this point in the history
  • Loading branch information
lgili authored and salkinium committed Mar 14, 2023
1 parent ac61d41 commit a38feca
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/same70_xplained/adc/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <modm/board.hpp>
#include <modm/io/iostream.hpp>

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<Board::SystemClock>();
Afec0::connect<Ad2::Ad, Ad3::Ad>();

while (true)
{
MODM_LOG_INFO << "ADC Readings: ";
MODM_LOG_INFO.printf("%5d ", Afec0::readChannel(Afec0::getPinChannel<Ad2>()));
MODM_LOG_INFO.printf("%5d ", Afec0::readChannel(Afec0::getPinChannel<Ad3>()));
MODM_LOG_INFO << modm::endl;

modm::delay(500ms);
}
}
10 changes: 10 additions & 0 deletions examples/same70_xplained/adc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:same70-xplained</extends>
<options>
<option name="modm:build:build.path">../../../build/same70_xplained/adc</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:adc:0</module>
</modules>
</library>
44 changes: 44 additions & 0 deletions examples/same70_xplained/blink/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <modm/board.hpp>
#include <modm/architecture/interface/delay.hpp>
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;
}
9 changes: 9 additions & 0 deletions examples/same70_xplained/blink/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<library>
<extends>modm:same70-xplained</extends>
<options>
<option name="modm:build:build.path">../../../build/same70_xplained/blink</option>
</options>
<modules>
<module>modm:build:scons</module>
</modules>
</library>
66 changes: 66 additions & 0 deletions examples/same70_xplained/pwm/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <modm/board.hpp>

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<OutH2::PwmH2, OutL2::PwmL2>();

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;
}
10 changes: 10 additions & 0 deletions examples/same70_xplained/pwm/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:same70-xplained</extends>
<options>
<option name="modm:build:build.path">../../../build/same70_xplained/pwm</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:pwm:*</module>
</modules>
</library>
72 changes: 72 additions & 0 deletions examples/same70_xplained/timer/main.cpp
Original file line number Diff line number Diff line change
@@ -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 <modm/board.hpp>
#include <modm/platform/timer/timer_channel_0.hpp>
#include <modm/platform/timer/timer_channel_10.hpp>

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<Tioa0::Tioa>();

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;
}
10 changes: 10 additions & 0 deletions examples/same70_xplained/timer/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:same70-xplained</extends>
<options>
<option name="modm:build:build.path">../../../build/same70_xplained/timer</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:timer:*</module>
</modules>
</library>

0 comments on commit a38feca

Please sign in to comment.