Skip to content

Commit 65e0c73

Browse files
committed
fix(GPIO): Revert previous fix, now invert AF mapping only on assignment
1 parent 1a1f8ca commit 65e0c73

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

Inc/HALAL/Models/GPIO.hpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,25 @@ 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,
76-
AF0 = 0,
77-
AF1 = 1,
78-
AF2 = 2,
79-
AF3 = 3,
80-
AF4 = 4,
81-
AF5 = 5,
82-
AF6 = 6,
83-
AF7 = 7,
84-
AF8 = 8,
85-
AF9 = 9,
86-
AF10 = 10,
87-
AF11 = 11,
88-
AF12 = 12,
89-
AF13 = 13,
90-
AF14 = 14,
91-
AF15 = 15
77+
AF0 = 15,
78+
AF1 = 14,
79+
AF2 = 13,
80+
AF3 = 12,
81+
AF4 = 11,
82+
AF5 = 10,
83+
AF6 = 9,
84+
AF7 = 8,
85+
AF8 = 7,
86+
AF9 = 6,
87+
AF10 = 5,
88+
AF11 = 4,
89+
AF12 = 3,
90+
AF13 = 2,
91+
AF14 = 1,
92+
AF15 = 0
9293
};
9394
enum class Port : uint8_t { A, B, C, D, E, F, G, H };
9495
static inline GPIO_TypeDef *port_to_reg(Port p) {
@@ -189,6 +190,10 @@ 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

194199
template <class Ctx> consteval void inscribe(Ctx &ctx) const {
@@ -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);

0 commit comments

Comments
 (0)