Skip to content

Commit eff9f87

Browse files
committed
Added driver EncoderInput
1 parent 1290c4d commit eff9f87

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, Thomas Sommer
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#pragma once
12+
13+
#include <modm/platform.hpp>
14+
15+
namespace modm
16+
{
17+
/**
18+
* @brief Gray code decoder using STM32 Timer mode
19+
*
20+
* @tparam Timer STM32 General purpose timer
21+
* @tparam SignalA First modm::platform::Gpio pin that connects to Timer::Ch1
22+
* @tparam SignalB Second modm::platform::Gpio pin that connects to Timer::Ch2
23+
*/
24+
template<class Timer, typename SignalA, typename SignalB, uint16_t POSTSCALER = 4, std::signed_integral ValueType = int8_t>
25+
class EncoderInput
26+
{
27+
public:
28+
void
29+
initialize(const bool invert = false, const uint8_t filter = 4)
30+
{
31+
// TODO Pullup optional?
32+
SignalA::setInput(modm::platform::Gpio::InputType::PullUp);
33+
SignalB::setInput(modm::platform::Gpio::InputType::PullUp);
34+
Timer::template connect<typename SignalA::Ch1, typename SignalB::Ch2>();
35+
36+
Timer::enable();
37+
38+
Timer::setMode(invert ? Timer::Mode::UpCounter : Timer::Mode::DownCounter, Timer::SlaveMode::Encoder3);
39+
Timer::setPrescaler(POSTSCALER);
40+
41+
if(filter) {
42+
Timer::configureInputChannel(1, filter);
43+
Timer::configureInputChannel(2, filter);
44+
}
45+
46+
Timer::applyAndReset();
47+
Timer::start();
48+
last = Timer::getValue();
49+
}
50+
51+
uint16_t
52+
getValue()
53+
{
54+
return Timer::getValue();
55+
}
56+
57+
ValueType
58+
getIncrement()
59+
{
60+
uint16_t current = Timer::getValue();
61+
int16_t delta = current - last;
62+
last = current;
63+
return delta;
64+
}
65+
66+
67+
private:
68+
uint16_t last = 0;
69+
};
70+
} // namespace modm
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021, Thomas Sommer
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
def init(module):
14+
module.name = ":driver:encoder_input"
15+
module.description = """
16+
# Quadrature Encoder Input using STM32 general purpose Timer
17+
18+
This driver decodes a AB (incremental) encoder signal.
19+
"""
20+
21+
def prepare(module, options):
22+
# TODO requires STM32 architecture
23+
module.depends(":architecture:atomic")
24+
return True
25+
26+
def build(env):
27+
env.outbasepath = "modm/src/modm/driver/encoder"
28+
env.copy("encoder_input.hpp")

0 commit comments

Comments
 (0)