Skip to content

Commit 64ace7d

Browse files
authored
fix(GPIO): Alternative Function numbers were inverted + make inscribe return size_t (#543)
* fix(GPIO): Alternative Function numbers were inverted * fix(GPIO): Revert previous fix, now invert AF mapping only on assignment * feat(ST-LIB): Make inscribe() return size_t and use it from higher Domains
1 parent fc00a32 commit 64ace7d

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

Inc/HALAL/Models/GPIO.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct GPIODomain {
7171
return GPIO_SPEED_FREQ_VERY_HIGH;
7272
}
7373
}
74+
// Note: AF mapping is inverted: AF0 -> 15, AF1 -> 14, ..., AF15 -> 0, it is staticly casted and inverted later
7475
enum class AlternateFunction : uint8_t {
7576
NO_AF = 20,
7677
AF0 = 15,
@@ -189,10 +190,14 @@ struct GPIODomain {
189190
if (!pin.valid_af(af)) {
190191
compile_error("Alternate function not valid for this pin");
191192
}
193+
194+
if ((mode == OperationMode::ALT_PP || mode == OperationMode::ALT_OD) && af == AlternateFunction::NO_AF) {
195+
compile_error("Alternate function must be specified for alternate modes");
196+
}
192197
}
193198

194-
template <class Ctx> consteval void inscribe(Ctx &ctx) const {
195-
ctx.template add<GPIODomain>(e, this);
199+
template <class Ctx> consteval std::size_t inscribe(Ctx &ctx) const {
200+
return ctx.template add<GPIODomain>(e, this);
196201
}
197202
};
198203

@@ -221,7 +226,7 @@ struct GPIODomain {
221226
GPIO_InitStruct.Pull = to_hal_pull(e.pull);
222227
GPIO_InitStruct.Speed = to_hal_speed(e.speed);
223228
if (e.mode == OperationMode::ALT_PP || e.mode == OperationMode::ALT_OD) {
224-
GPIO_InitStruct.Alternate = static_cast<uint32_t>(e.af);
229+
GPIO_InitStruct.Alternate = 15 - static_cast<uint32_t>(e.af); // AF mapping inversion
225230
}
226231

227232
cfgs[i].init_data = std::make_tuple(e.port, GPIO_InitStruct);

Inc/ST-LIB_LOW/DigitalInput2.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ struct DigitalInputDomain {
1818
GPIODomain::Speed speed = GPIODomain::Speed::Low)
1919
: gpio{pin, GPIODomain::OperationMode::INPUT, pull, speed} {}
2020

21-
template <class Ctx> consteval void inscribe(Ctx &ctx) const {
22-
const auto gpio_idx = ctx.template add<GPIODomain>(gpio.e, &gpio);
21+
template <class Ctx> consteval std::size_t inscribe(Ctx &ctx) const {
22+
const auto gpio_idx = gpio.inscribe(ctx);
2323
Entry e{.gpio_idx = gpio_idx};
24-
ctx.template add<DigitalInputDomain>(e, this);
24+
return ctx.template add<DigitalInputDomain>(e, this);
2525
}
2626
};
2727

Inc/ST-LIB_LOW/DigitalOutput2.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ struct DigitalOutputDomain {
2626
: gpio{pin, static_cast<GPIODomain::OperationMode>(mode), pull, speed} {
2727
}
2828

29-
template <class Ctx> consteval void inscribe(Ctx &ctx) const {
30-
const auto gpio_idx = ctx.template add<GPIODomain>(gpio.e, &gpio);
29+
template <class Ctx> consteval std::size_t inscribe(Ctx &ctx) const {
30+
const auto gpio_idx = gpio.inscribe(ctx);
3131
Entry e{.gpio_idx = gpio_idx};
32-
ctx.template add<DigitalOutputDomain>(e, this);
32+
return ctx.template add<DigitalOutputDomain>(e, this);
3333
}
3434
};
3535

0 commit comments

Comments
 (0)