diff --git a/examples/arduino_nano/encoder_input/main.cpp b/examples/arduino_nano/encoder_input_bitbang/main.cpp similarity index 84% rename from examples/arduino_nano/encoder_input/main.cpp rename to examples/arduino_nano/encoder_input_bitbang/main.cpp index 65bb5e1e8a..e49ff11c64 100644 --- a/examples/arduino_nano/encoder_input/main.cpp +++ b/examples/arduino_nano/encoder_input_bitbang/main.cpp @@ -10,15 +10,14 @@ // ---------------------------------------------------------------------------- #include -#include -#include #include +#include +#include using namespace modm::platform; // Connect the encoders outputs to D7 and D8 Pins (usually the outer pins) // The common third pin (usually in the middle) is connected to GND. -// Don't add any resistors or filters. It's all in the MCU and the driver. modm::BitBangEncoderInput encoder; MODM_ISR(TIMER2_COMPA) @@ -48,22 +47,19 @@ main() Board::initialize(); LedD13::setOutput(); - encoder.connect(); - + encoder.initialize(); init_Timer2(); enableInterrupts(); int value(0); modm::ShortPeriodicTimer heartbeat(500ms); - modm::ShortPeriodicTimer outputValue(1000ms); while (true) { - if (heartbeat.execute()) Board::LedD13::toggle(); - if (outputValue.execute()) - { - value += encoder.getIncrement(); + if (heartbeat.execute()) { + Board::LedD13::toggle(); + value += encoder.getDelta(); MODM_LOG_INFO << "value: " << value << modm::endl; } } diff --git a/examples/arduino_nano/encoder_input/project.xml b/examples/arduino_nano/encoder_input_bitbang/project.xml similarity index 89% rename from examples/arduino_nano/encoder_input/project.xml rename to examples/arduino_nano/encoder_input_bitbang/project.xml index 703c79eff9..6a15971d34 100644 --- a/examples/arduino_nano/encoder_input/project.xml +++ b/examples/arduino_nano/encoder_input_bitbang/project.xml @@ -1,7 +1,7 @@ modm:arduino-nano - + modm:build:scons diff --git a/examples/blue_pill_f103/encoder_input/main.cpp b/examples/blue_pill_f103/encoder_input/main.cpp new file mode 100644 index 0000000000..51a1f7cc1c --- /dev/null +++ b/examples/blue_pill_f103/encoder_input/main.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021, Thomas Sommer + * + * 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 +#include + +// ---------------------------------------------------------------------------- +// Set the log level +#undef MODM_LOG_LEVEL +#define MODM_LOG_LEVEL modm::log::INFO + +// 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); + +int +main() +{ + Board::initialize(); + + Usart2::connect(); + Usart2::initialize(); + + // Each Timer can drive one Encoder + // For Timer2 and Timer3 you have 2 Gpio options + // When using one of PB3 or PB4 you also have to disable JTAG debugging during shared pins + + // Timer1: + modm::EncoderInput encoder; + + // Timer2: + // modm::EncoderInput encoder; + // modm::EncoderInput encoder; + // AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE; + + // Timer3: + // modm::EncoderInput encoder; + // modm::EncoderInput encoder; + // Disable JTAG to make PB3, PB4 available as Gpio + // AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE; + + // Timer4: + // modm::EncoderInput encoder; + + encoder.initialize(true); + + modm::ShortPeriodicTimer heartbeat(500ms); + + while (true) + { + if(heartbeat.execute()) { + Board::LedGreen::toggle(); + MODM_LOG_INFO << "Encoder Delta: " << encoder.getDelta() << modm::endl; + MODM_LOG_INFO << "Encoder Absolut: " << encoder.getValue() << modm::endl; + } + } + + return 0; +} diff --git a/examples/blue_pill_f103/encoder_input/openocd.cfg b/examples/blue_pill_f103/encoder_input/openocd.cfg new file mode 100644 index 0000000000..b157e7e432 --- /dev/null +++ b/examples/blue_pill_f103/encoder_input/openocd.cfg @@ -0,0 +1,2 @@ +# Replace this with your custom programmer +source [find interface/stlink-v2.cfg] diff --git a/examples/blue_pill_f103/encoder_input/project.xml b/examples/blue_pill_f103/encoder_input/project.xml new file mode 100644 index 0000000000..34707ffb99 --- /dev/null +++ b/examples/blue_pill_f103/encoder_input/project.xml @@ -0,0 +1,18 @@ + + modm:blue-pill-f103 + + + + + + modm:debug + modm:platform:uart:2 + modm:processing:timer + modm:driver:encoder_input + modm:platform:timer:1 + modm:platform:timer:2 + modm:platform:timer:3 + modm:platform:timer:4 + modm:build:scons + + diff --git a/examples/blue_pill_f103/encoder_input_bitbang/main.cpp b/examples/blue_pill_f103/encoder_input_bitbang/main.cpp new file mode 100644 index 0000000000..5b32144e22 --- /dev/null +++ b/examples/blue_pill_f103/encoder_input_bitbang/main.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2021, Thomas Sommer + * + * 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 +#include + +// ---------------------------------------------------------------------------- +// Set the log level +#undef MODM_LOG_LEVEL +#define MODM_LOG_LEVEL modm::log::INFO + +// 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); + +// Connect the encoders outputs to D7 and D8 Pins (usually the outer pins) +// The common third pin (usually in the middle) is connected to GND. +modm::BitBangEncoderInput encoder; + +MODM_ISR(TIM2) +{ + Timer2::acknowledgeInterruptFlags(Timer2::InterruptFlag::Update); + encoder.update(); +} + +void +init_Timer2(const uint16_t period) +{ + Timer2::enable(); + Timer2::setMode(Timer2::Mode::UpCounter); + + Timer2::template setPeriod(period); + Timer2::enableInterruptVector(true, 10); + Timer2::enableInterrupt(Timer2::Interrupt::Update); + + Timer2::applyAndReset(); + Timer2::start(); +} + +int +main() +{ + Board::initialize(); + + Usart2::connect(); + Usart2::initialize(); + + encoder.initialize(); + init_Timer2(1000); // 1ms period + + int value(0); + + modm::ShortPeriodicTimer heartbeat(1s); + + while (true) + { + if (heartbeat.execute()) { + Board::LedGreen::toggle(); + + const auto delta = encoder.getDelta(); + MODM_LOG_INFO << "Delta: " << delta << modm::endl; + + value += delta; + MODM_LOG_INFO << "Encoder Absolut: " << value << modm::endl; + } + } +} diff --git a/examples/blue_pill_f103/encoder_input_bitbang/openocd.cfg b/examples/blue_pill_f103/encoder_input_bitbang/openocd.cfg new file mode 100644 index 0000000000..b157e7e432 --- /dev/null +++ b/examples/blue_pill_f103/encoder_input_bitbang/openocd.cfg @@ -0,0 +1,2 @@ +# Replace this with your custom programmer +source [find interface/stlink-v2.cfg] diff --git a/examples/blue_pill_f103/encoder_input_bitbang/project.xml b/examples/blue_pill_f103/encoder_input_bitbang/project.xml new file mode 100644 index 0000000000..717a9f9d8d --- /dev/null +++ b/examples/blue_pill_f103/encoder_input_bitbang/project.xml @@ -0,0 +1,15 @@ + + modm:blue-pill-f103 + + + + + + modm:debug + modm:platform:uart:2 + modm:processing:timer + modm:driver:encoder_input.bitbang + modm:platform:timer:2 + modm:build:scons + +