Skip to content

Commit 15ed250

Browse files
committed
[gpio] Extract port shim into common file
1 parent d9ada32 commit 15ed250

File tree

5 files changed

+92
-151
lines changed

5 files changed

+92
-151
lines changed

src/modm/platform/gpio/at90_tiny_mega/module.lb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def validate(env):
109109
bprops["ranges"] = port_ranges(driver["gpio"])
110110
bprops["ports"] = {k:i for i, k in enumerate([p["name"] for p in bprops["ranges"]])}
111111
bprops["gpios"] = [bprops[g["port"] + g["pin"]] for g in driver["gpio"]]
112+
bprops["port_width"] = 8
112113

113114
def build(env):
114115
device = env[":target"]
@@ -154,5 +155,6 @@ def build(env):
154155
env.template("../common/unused.hpp.in", "unused.hpp")
155156

156157
env.copy("../common/inverted.hpp", "inverted.hpp")
158+
env.template("../common/port_shim.hpp.in", "port_shim.hpp")
157159
env.template("../common/connector.hpp.in", "connector.hpp",
158160
filters={"formatPeripheral": "", "printSignalMap": ""})

src/modm/platform/gpio/at90_tiny_mega/port.hpp.in

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,20 @@
99
*/
1010
// ----------------------------------------------------------------------------
1111

12-
#ifndef MODM_AVR_GPIO_PORT_HPP
13-
#define MODM_AVR_GPIO_PORT_HPP
12+
#pragma once
1413

15-
#include "base.hpp"
14+
#include "port_shim.hpp"
1615
#include "set.hpp"
1716
#include <modm/math/utils/bit_operation.hpp>
1817

19-
namespace modm
18+
namespace modm::platform
2019
{
2120

22-
namespace platform
23-
{
24-
25-
/// @cond
26-
template< Gpio::Port port >
27-
struct GpioPortInfo;
28-
template< class StartGpio, int8_t Width >
29-
struct GpioSetShim
30-
{
31-
static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, Width> check;
32-
};
33-
/// @endcond
34-
3521
/// @ingroup modm_platform_gpio
3622
template< class StartGpio, int8_t Width >
37-
class GpioPort : public ::modm::GpioPort/** @cond */, public GpioSetShim<StartGpio, Width>/** @endcond */
23+
class GpioPort : public ::modm::GpioPort/** @cond */, public detail::GpioSetShim<StartGpio, Width>/** @endcond */
3824
{
39-
using PinSet = GpioSetShim<StartGpio, Width>;
25+
using PinSet = detail::GpioSetShim<StartGpio, Width>;
4026
public:
4127
using PinSet::width;
4228
static_assert(width <= 8, "Only a maximum of 8 pins are supported by GpioPort!");
@@ -83,61 +69,4 @@ public:
8369
}
8470
};
8571

86-
/// @cond
87-
// ------ Port Width Information ------
88-
%% for port in ranges
89-
template<>
90-
struct GpioPortInfo<Gpio::Port::{{port["name"]}}>
91-
{
92-
static constexpr int8_t Width = {{port["width"]}};
93-
static constexpr int8_t StartPin = {{port["start"]}};
94-
static constexpr int8_t EndPin = StartPin + Width;
95-
96-
template< uint8_t QueryStartPin, int8_t QueryWidth >
97-
struct check
98-
{
99-
static constexpr bool isNormal = QueryWidth > 0;
100-
static constexpr bool isReversed = QueryWidth < 0;
101-
static constexpr int8_t width = isNormal ? QueryWidth : -QueryWidth;
102-
static_assert(isReversed or width <= Width,
103-
"GpioPort Width out of bounds! Maximum is {{port["width"]}}.");
104-
105-
static_assert(isReversed or (QueryStartPin + QueryWidth <= EndPin),
106-
"GpioPort StartPin + Width out of bounds! Maximum is {{port["start"] + port["width"] - 1}}.");
107-
static_assert(isNormal or (StartPin <= QueryStartPin + QueryWidth + 1),
108-
"GpioPort StartPin - Width out of bounds! Minimum is {{port["start"]}}.");
109-
};
110-
};
111-
%% endfor
112-
113-
// ------ Translator classes from Gpio + Width -> GpioSet ------
114-
template< class StartGpio, int8_t offset >
115-
struct GpioShim
116-
{
117-
static constexpr typename StartGpio::Port port = StartGpio::port;
118-
static constexpr uint8_t mask = (1ul << (StartGpio::pin + offset));
119-
static constexpr bool isInverted = StartGpio::isInverted;
120-
};
121-
%% for width in range(-8, 0)
122-
template< class StartGpio >
123-
struct GpioSetShim<StartGpio, {{width}}> : public GpioSet<
124-
%% for offset in (range(width + 1, 1) | reverse)
125-
GpioShim<StartGpio, {{offset}}>{% if not loop.last %},{% endif %}
126-
%% endfor
127-
> { static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, {{width}}> check; };
128-
%% endfor
129-
%% for width in range(0, 9)
130-
template< class StartGpio >
131-
struct GpioSetShim<StartGpio, {{width}}> : public GpioSet<
132-
%% for offset in (range(width) | reverse)
133-
GpioShim<StartGpio, {{offset}}>{% if not loop.last %},{% endif %}
134-
%% endfor
135-
> { static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, {{width}}> check; };
136-
%% endfor
137-
/// @endcond
138-
139-
} // namespace platform
140-
141-
} // namespace modm
142-
143-
#endif // MODM_AVR_GPIO_PORT_HPP
72+
} // namespace modm::platform
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2016-2018, Niklas Hauser
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+
12+
#pragma once
13+
14+
#include "base.hpp"
15+
16+
/// @cond
17+
namespace modm::platform::detail
18+
{
19+
20+
template< Gpio::Port port >
21+
struct GpioPortInfo;
22+
template< class StartGpio, int8_t Width >
23+
struct GpioSetShim
24+
{
25+
static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, Width> check;
26+
};
27+
28+
// ------ Port Width Information ------
29+
%% for port in ranges
30+
template<>
31+
struct GpioPortInfo<Gpio::Port::{{port.name}}>
32+
{
33+
static constexpr int8_t Width = {{port.width}};
34+
static constexpr int8_t StartPin = {{port.start}};
35+
static constexpr int8_t EndPin = StartPin + Width;
36+
37+
template< uint8_t QueryStartPin, int8_t QueryWidth >
38+
struct check
39+
{
40+
static constexpr bool isNormal = QueryWidth > 0;
41+
static constexpr bool isReversed = QueryWidth < 0;
42+
static constexpr int8_t width = isNormal ? QueryWidth : -QueryWidth;
43+
static_assert(isReversed or width <= Width,
44+
"GpioPort Width out of bounds! Maximum is {{port.width}}.");
45+
46+
static_assert(isReversed or (QueryStartPin + QueryWidth <= EndPin),
47+
"GpioPort StartPin + Width out of bounds! Maximum is {{port.start + port.width - 1}}.");
48+
static_assert(isNormal or (StartPin <= QueryStartPin + QueryWidth + 1),
49+
"GpioPort StartPin - Width out of bounds! Minimum is {{port.start}}.");
50+
};
51+
};
52+
%% endfor
53+
54+
// ------ Translator classes from Gpio + Width -> GpioSet ------
55+
template< class StartGpio, int8_t offset >
56+
struct GpioShim
57+
{
58+
static constexpr typename StartGpio::Port port = StartGpio::port;
59+
static constexpr uint16_t mask = (1ul << (StartGpio::pin + offset));
60+
static constexpr bool isInverted = StartGpio::isInverted;
61+
};
62+
%% for width in range(-port_width, 0)
63+
template< class StartGpio >
64+
struct GpioSetShim<StartGpio, {{width}}> : public GpioSet<
65+
%% for offset in (range(width + 1, 1) | reverse)
66+
GpioShim<StartGpio, {{offset}}>{% if not loop.last %},{% endif %}
67+
%% endfor
68+
> { static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, {{width}}> check; };
69+
%% endfor
70+
%% for width in range(0, port_width+1)
71+
template< class StartGpio >
72+
struct GpioSetShim<StartGpio, {{width}}> : public GpioSet<
73+
%% for offset in (range(width) | reverse)
74+
GpioShim<StartGpio, {{offset}}>{% if not loop.last %},{% endif %}
75+
%% endfor
76+
> { static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, {{width}}> check; };
77+
%% endfor
78+
79+
} // namespace modm::platform::detail
80+
/// @endcond

src/modm/platform/gpio/stm32/module.lb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def validate(env):
253253
bprops["all_peripherals"] = sorted(list(set(all_peripherals)))
254254
bprops["all_signals"] = sorted(list(set(s["name"] for s in all_signals.values())))
255255
bprops["gpios"] = [bprops[g["port"] + g["pin"]] for g in driver["gpio"]]
256+
bprops["port_width"] = 16
256257

257258
# Check the max number of ADC instances
258259
max_adc_instance = max(map(int, device.get_driver("adc").get("instance", [1])))
@@ -273,6 +274,7 @@ def build(env):
273274
env.template("set.hpp.in")
274275
env.template("base.hpp.in")
275276
env.template("../common/unused.hpp.in", "unused.hpp")
277+
env.template("../common/port_shim.hpp.in", "port_shim.hpp")
276278
if len(env["enable_ports"]):
277279
env.template("enable.cpp.in")
278280

src/modm/platform/gpio/stm32/port.hpp.in

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,16 @@
99
*/
1010
// ----------------------------------------------------------------------------
1111

12-
#ifndef MODM_STM32_GPIO_PORT_HPP
13-
#define MODM_STM32_GPIO_PORT_HPP
12+
#pragma once
1413

15-
#include "base.hpp"
14+
#include "port_shim.hpp"
1615
#include "set.hpp"
1716
#include <type_traits>
1817
#include <modm/math/utils/bit_operation.hpp>
1918

2019
namespace modm::platform
2120
{
2221

23-
/// @cond
24-
namespace detail
25-
{
26-
template< Gpio::Port port >
27-
struct GpioPortInfo;
28-
template< class StartGpio, int8_t Width >
29-
struct GpioSetShim
30-
{
31-
static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, Width> check;
32-
};
33-
} // namespace detail
34-
/// @endcond
35-
3622
/// @ingroup modm_platform_gpio
3723
template< class StartGpio, int8_t Width >
3824
class GpioPort : public ::modm::GpioPort/** @cond */, public detail::GpioSetShim<StartGpio, Width> /** @endcond */
@@ -89,62 +75,4 @@ public:
8975
}
9076
};
9177

92-
/// @cond
93-
namespace detail
94-
{
95-
// ------ Port Width Information ------
96-
%% for port in ranges
97-
template<>
98-
struct GpioPortInfo<Gpio::Port::{{port.name}}>
99-
{
100-
static constexpr int8_t Width = {{port.width}};
101-
static constexpr int8_t StartPin = {{port.start}};
102-
static constexpr int8_t EndPin = StartPin + Width;
103-
104-
template< uint8_t QueryStartPin, int8_t QueryWidth >
105-
struct check
106-
{
107-
static constexpr bool isNormal = QueryWidth > 0;
108-
static constexpr bool isReversed = QueryWidth < 0;
109-
static constexpr int8_t width = isNormal ? QueryWidth : -QueryWidth;
110-
static_assert(isReversed or width <= Width,
111-
"GpioPort Width out of bounds! Maximum is {{port.width}}.");
112-
113-
static_assert(isReversed or (QueryStartPin + QueryWidth <= EndPin),
114-
"GpioPort StartPin + Width out of bounds! Maximum is {{port.start + port.width - 1}}.");
115-
static_assert(isNormal or (StartPin <= QueryStartPin + QueryWidth + 1),
116-
"GpioPort StartPin - Width out of bounds! Minimum is {{port.start}}.");
117-
};
118-
};
119-
%% endfor
120-
121-
// ------ Translator classes from Gpio + Width -> GpioSet ------
122-
template< class StartGpio, int8_t offset >
123-
struct GpioShim
124-
{
125-
static constexpr typename StartGpio::Port port = StartGpio::port;
126-
static constexpr uint16_t mask = (1ul << (StartGpio::pin + offset));
127-
static constexpr bool isInverted = StartGpio::isInverted;
128-
};
129-
%% for width in range(-16, 0)
130-
template< class StartGpio >
131-
struct GpioSetShim<StartGpio, {{width}}> : public GpioSet<
132-
%% for offset in (range(width + 1, 1) | reverse)
133-
GpioShim<StartGpio, {{offset}}>{% if not loop.last %},{% endif %}
134-
%% endfor
135-
> { static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, {{width}}> check; };
136-
%% endfor
137-
%% for width in range(0, 17)
138-
template< class StartGpio >
139-
struct GpioSetShim<StartGpio, {{width}}> : public GpioSet<
140-
%% for offset in (range(width) | reverse)
141-
GpioShim<StartGpio, {{offset}}>{% if not loop.last %},{% endif %}
142-
%% endfor
143-
> { static typename GpioPortInfo<StartGpio::port>::template check<StartGpio::pin, {{width}}> check; };
144-
%% endfor
145-
} // namespace detail
146-
/// @endcond
147-
14878
} // namespace modm::platform
149-
150-
#endif // MODM_STM32_GPIO_PORT_HPP

0 commit comments

Comments
 (0)