Skip to content

Commit

Permalink
Fix SAMD GPIO bug for port B
Browse files Browse the repository at this point in the history
Support correct port B register access, and return the register as volatile
  • Loading branch information
mcbridejc committed Sep 8, 2021
1 parent 2c22fae commit 463d9d4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/modm/platform/gpio/sam/pin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,23 @@ class GpioSet : protected PinCfgMixin<PinConfigs...>
return (((PinConfigs::port == port) ? 1u << PinConfigs::pin : 0u) | ...);
}

inline static constexpr uint32_t*
template<PortName port>
inline static constexpr volatile uint32_t*
getPortReg(size_t offset)
{
return (uint32_t*)(&PORT->Group[0]) + offset / sizeof(uint32_t);
if constexpr (port == PortName::A) {
return (uint32_t*)(&PORT->Group[0]) + offset / sizeof(uint32_t);
}
if constexpr (port == PortName::B) {
return (uint32_t*)(&PORT->Group[1]) + offset / sizeof(uint32_t);
}
}

inline static constexpr void
setPortReg(size_t offset)
{
if constexpr (mask(PortName::A) != 0) { *getPortReg(offset) = mask(PortName::A); }
if constexpr (mask(PortName::B) != 0) { *getPortReg(offset) = mask(PortName::B); }
if constexpr (mask(PortName::A) != 0) { *getPortReg<PortName::A>(offset) = mask(PortName::A); }
if constexpr (mask(PortName::B) != 0) { *getPortReg<PortName::B>(offset) = mask(PortName::B); }
}

template<PortName port>
Expand All @@ -160,12 +166,12 @@ class GpioSet : protected PinCfgMixin<PinConfigs...>
{
static_assert(mask(PortName::A) != 0,
"Trying to read port which is not in the GpioSet!");
return *getPortReg(offset) & mask(PortName::A);
return *getPortReg<PortName::A>(offset) & mask(PortName::A);
} else if constexpr (port == PortName::B)
{
static_assert(mask(PortName::B) != 0,
"Trying to read port which is not in the GpioSet!");
return *getPortReg(offset) & mask(PortName::A);
return *getPortReg<PortName::B>(offset) & mask(PortName::B);
}
}

Expand Down

0 comments on commit 463d9d4

Please sign in to comment.