Skip to content

Commit

Permalink
[examples] Add RTT with OpenOCD example
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 10, 2021
1 parent 24bafbb commit 2298de1
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/avr/block_device_mirror/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace modm::platform;

// Create a new UART object and configure it to a baudrate of 115200
Uart0 uart;
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > loggerDevice(uart);
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > loggerDevice;

// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
Expand Down
49 changes: 49 additions & 0 deletions examples/stm32f469_discovery/rtt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2021, 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>
#include <modm/processing.hpp>

using namespace Board;

Rtt rtt(1);
modm::IODeviceObjectWrapper< Rtt, modm::IOBuffer::DiscardIfFull > device(rtt);
modm::IOStream stream(device);

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

uint32_t counter(0);
modm::PeriodicTimer tmr(100ms);

char data;
while (true)
{
stream.get(data);
switch(data)
{
case '1'...'9':
tmr.restart(std::chrono::milliseconds((data - '0') * 100));
break;
}
if (tmr.execute())
{
LedBlue::toggle();

stream << "loop: " << counter++ << modm::endl;
}
}

return 0;
}
12 changes: 12 additions & 0 deletions examples/stm32f469_discovery/rtt/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:disco-f469ni</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f469_discovery/rtt</option>
<option name="modm:platform:rtt:buffer.rx">16</option>
</options>
<modules>
<module>modm:platform:rtt</module>
<module>modm:processing:timer</module>
<module>modm:build:scons</module>
</modules>
</library>
43 changes: 37 additions & 6 deletions src/modm/io/iodevice_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,18 @@ template< class Device, IOBuffer behavior >
class IODeviceWrapper : public IODevice
{
public:
IODeviceWrapper(const Device&) {}
IODeviceWrapper() = default;
using IODevice::write;

void
write(char c) override
{
if constexpr (behavior == IOBuffer::DiscardIfFull) {
Device::write(static_cast<uint8_t>(c));
}
else {
while(not Device::write(static_cast<uint8_t>(c))) ;
bool written;
do
{
written = Device::write(uint8_t(c));
}
while(behavior == IOBuffer::BlockIfFull and not written);
}

void
Expand All @@ -68,6 +67,38 @@ class IODeviceWrapper : public IODevice
}
};

template< class Device, IOBuffer behavior >
class IODeviceObjectWrapper : public IODevice
{
Device &device;
public:
IODeviceObjectWrapper(Device& device) : device{device} {}
using IODevice::write;

void
write(char c) override
{
bool written;
do
{
written = device.write(uint8_t(c));
}
while(behavior == IOBuffer::BlockIfFull and not written);
}

void
flush() override
{
device.flushWriteBuffer();
}

bool
read(char& c) override
{
return device.read(reinterpret_cast<uint8_t&>(c));
}
};

}

#endif // MODM_IODEVICE_WRAPPER_HPP

0 comments on commit 2298de1

Please sign in to comment.