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

[fiber] Adds a fiber processing library #439

Closed
wants to merge 8 commits into from
Closed
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
9 changes: 0 additions & 9 deletions examples/blue_pill_f103/adns_9800/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart2, 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);

class BlinkThread : public modm::pt::Protothread
{
public:
Expand Down
3 changes: 0 additions & 3 deletions examples/blue_pill_f103/can/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#include <modm/board.hpp>
#include <modm/debug/logger.hpp>

modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
modm::log::Logger modm::log::info(loggerDevice);

// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO
Expand Down
11 changes: 0 additions & 11 deletions examples/blue_pill_f103/environment/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ Bme280Thread bme280Thread;
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
modm::IOStream stream(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);

// ----------------------------------------------------------------------------

using namespace Board;

Expand Down
4 changes: 4 additions & 0 deletions examples/blue_pill_f103/logger/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

/*** The following code is included in the modm:board module (board.hpp),
* but might be useful if the BSP is not used.

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;

Expand All @@ -30,6 +33,7 @@ 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);
*/


class BlinkThread : public modm::pt::Protothread
Expand Down
10 changes: 0 additions & 10 deletions examples/blue_pill_f103/weight_scale_hx711/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ using namespace Board;
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart1, 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);


struct hx711_config : public modm::hx711::Config
{
using Sck = GpioOutputA9;
Expand Down
56 changes: 56 additions & 0 deletions examples/generic/fiber/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Erik Henriksson
*
* 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/debug/logger.hpp>
#include <modm/processing.hpp>

using namespace Board;
using namespace std::chrono_literals;

constexpr uint32_t cycles = 1000'000;
volatile uint32_t f1counter = 0, f2counter = 0;

void
f1()
{
while (++f1counter < cycles / 2) { modm::yield(); }
}

void
f2()
{
while (++f2counter < cycles / 2) { modm::yield(); }
}

modm::fiber::Stack<2048> stack1, stack2;
modm::Fiber fiber1(stack1, &f1), fiber2(stack2, &f2);

// Blue pill (M3 72MHz): Executed 1000000 in 1098591us (910256.88 yields per second)
// Feather M0 (M0+ 48MHz): Executed 1000000 in 1944692us (514220.25 yields per second)
int
main(int argc, char* argv[])
{
Board::initialize();
MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl;
MODM_LOG_INFO.flush();
const modm::PreciseTimestamp start = modm::PreciseClock::now();
modm::fiber::scheduler.start();
const auto diff = (modm::PreciseClock::now() - start);
MODM_LOG_INFO << "Benchmark done!" << modm::endl;
MODM_LOG_INFO << "Executed " << cycles << " in " << diff << " (";
MODM_LOG_INFO.printf("%.2f", cycles * 1'000'000.0 / std::chrono::microseconds(diff).count());
MODM_LOG_INFO << " yields per second)" << modm::endl;
MODM_LOG_INFO.flush();
while (1)
;
return 0;
}
3 changes: 3 additions & 0 deletions examples/generic/fiber/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source [find interface/stlink-v2.cfg]

set CPUTAPID 0x2ba01477
13 changes: 13 additions & 0 deletions examples/generic/fiber/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<library>
<extends>modm:blue-pill</extends>
<!-- <extends>modm:feather-m0</extends> -->
<options>
<option name="modm:build:build.path">../../../build/generic/fiber</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:processing:timer</module>
<module>modm:processing:fiber</module>
</modules>
</library>
56 changes: 56 additions & 0 deletions examples/linux/fiber/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2020, Erik Henriksson
*
* 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 <chrono>
#include <cmath>
#include <modm/debug/logger.hpp>
#include <modm/processing.hpp>

using namespace std::chrono_literals;

#define STACK_SIZE 1024

void
f1()
{
MODM_LOG_INFO << "f1" << modm::endl;
modm::yield();
MODM_LOG_INFO << "f1 done" << modm::endl;
}

void
f2()
{
MODM_LOG_INFO << "f2" << modm::endl;
modm::yield();
MODM_LOG_INFO << "f2 done" << modm::endl;
}

void
idle()
{
MODM_LOG_INFO << "idle" << modm::endl;
modm::yield();
MODM_LOG_INFO << "all done" << modm::endl;
}

modm::fiber::Stack<STACK_SIZE> stack1, stack2, stack_idle;
modm::Fiber fiber1(stack1, &f1), fiber2(stack2, &f2), fiber_idle(stack_idle, &idle);

int
main(void)
{
MODM_LOG_INFO << "Start" << modm::endl;
modm::fiber::scheduler.start();
// Will never get here.
MODM_LOG_INFO << "End" << modm::endl;
return 0;
}
12 changes: 12 additions & 0 deletions examples/linux/fiber/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<options>
<option name="modm:target">hosted-linux</option>
<option name="modm:build:build.path">../../../build/linux/fiber</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:core</module>
<module>modm:processing:fiber</module>
<module>modm:build:scons</module>
</modules>
</library>
2 changes: 1 addition & 1 deletion examples/nucleo_f031k6/sk6812/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using namespace Board;

using Output = Board::D11;
modm::Sk6812w<SpiMaster1, Output, 8*8> leds;
modm::Sk6812w<SpiMaster1, Output, 4*4> leds;
modm::ShortPeriodicTimer tmr{33ms};

int
Expand Down
64 changes: 64 additions & 0 deletions examples/samd/context/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2020, Erik Henriksson
*
* 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/debug/logger.hpp>
#include <modm/processing.hpp>

#define MODM_LOG_LEVEL modm::log::INFO
#undef MODM_BOARD_HAS_LOGGER

using namespace Board;
using namespace std::chrono_literals;

modm_fastdata uint32_t stack1[64], stack2[64]; // 64 bytes stack
modm_fastdata modm_context f1_ctx, f2_ctx, m_ctx;

modm_naked void
f1()
{
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "f1: entered" << modm::endl;
#endif
while (1)
{
A0::set();
modm_jumpcontext(&f1_ctx, f2_ctx);
}
}

modm_naked void
f2()
{
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "f2: entered" << modm::endl;
#endif
while (1)
{
A0::reset();
modm_jumpcontext(&f2_ctx, f1_ctx);
}
}

// Frequency of A0 is 766.5kHz, resulting in ~36 CPU cycles per context switch (incl. overhead).
int
main()
{
Board::initialize();
A0::setOutput();
f1_ctx = modm_makecontext((void**)stack1, sizeof(stack1), &f1, nullptr);
f2_ctx = modm_makecontext((void**)stack2, sizeof(stack2), &f2, nullptr);
MODM_LOG_DEBUG << "main: Jumping to f1 with sp-addr: " << modm::hex << f1_ctx.sp
<< " and f-addr: " << modm::hex << *(uint32_t*)(f1_ctx.sp + 0x10) << modm::endl;
modm_jumpcontext(&m_ctx, f1_ctx);
// Will never get here.
return 0;
}
10 changes: 10 additions & 0 deletions examples/samd/context/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:feather-m0</extends>
<options>
<option name="modm:build:build.path">../../../build/samd/context</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:processing:fiber</module>
</modules>
</library>
73 changes: 73 additions & 0 deletions examples/samd/fiber/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2020, Erik Henriksson
*
* 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/debug/logger.hpp>
#include <modm/processing.hpp>

using namespace Board;
using namespace std::chrono_literals;

#ifdef MODM_BOARD_HAS_LOGGER
#define STACK_SIZE 512
#else
// Without logging, we only need 4 byte (storing PC when performing a jump), plus space for
// another 8 registers (32 bytes), which are pushed to the stack by hardware when an interrupt
// happens (i.e. SysTick).
#define STACK_SIZE 40
#endif

void
f1();

void
f2();

modm::fiber::Stack<STACK_SIZE> stack1, stack2;
modm::Fiber fiber1(stack1, &f1), fiber2(stack2, &f2);

void
f1()
{
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "f1: entered" << modm::endl;
#endif
while (1)
{
A0::set();
modm::yield();
}
}

void
f2()
{
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "f2: entered" << modm::endl;
#endif
while (1)
{
A0::reset();
modm::yield();
}
}

// Frequency of A0 is 625.3kHz, resulting in ~45 CPU cycles per context switch (incl. GPIO
// overhead).
int
main(int argc, char* argv[])
{
Board::initialize();
A0::setOutput();
modm::fiber::scheduler.start();
// Will never get here.
return 0;
}
10 changes: 10 additions & 0 deletions examples/samd/fiber/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:feather-m0</extends>
<options>
<option name="modm:build:build.path">../../../build/samd/fiber</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:processing:fiber</module>
</modules>
</library>
Loading