Make all SHR16 public functions "thread-safe"

pull/134/head
D.R.racer 2021-10-21 08:55:15 +02:00 committed by DRracer
parent 19dcfcef79
commit 04348b2d86
1 changed files with 30 additions and 26 deletions

View File

@ -23,7 +23,6 @@ void SHR16::Init() {
} }
void SHR16::Write(uint16_t v) { void SHR16::Write(uint16_t v) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
using namespace hal::gpio; using namespace hal::gpio;
WritePin(SHR16_LATCH, Level::low); WritePin(SHR16_LATCH, Level::low);
_delay_us(1); _delay_us(1);
@ -38,28 +37,33 @@ void SHR16::Write(uint16_t v) {
WritePin(SHR16_LATCH, Level::high); WritePin(SHR16_LATCH, Level::high);
shr16_v = v; shr16_v = v;
} }
}
void SHR16::SetLED(uint16_t led) { void SHR16::SetLED(uint16_t led) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
led = ((led & 0x00ff) << 8) | ((led & 0x0300) >> 2); led = ((led & 0x00ff) << 8) | ((led & 0x0300) >> 2);
Write((shr16_v & ~SHR16_LED_MSK) | led); Write((shr16_v & ~SHR16_LED_MSK) | led);
} }
}
void SHR16::SetTMCEnabled(uint8_t index, bool ena) { void SHR16::SetTMCEnabled(uint8_t index, bool ena) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
const uint16_t mask = 1 << (2 * index + 1); const uint16_t mask = 1 << (2 * index + 1);
if (ena) if (ena)
Write(shr16_v & ~mask); Write(shr16_v & ~mask);
else else
Write(shr16_v | mask); Write(shr16_v | mask);
} }
}
void SHR16::SetTMCDir(uint8_t index, bool dir) { void SHR16::SetTMCDir(uint8_t index, bool dir) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
const uint16_t mask = 1 << (2 * index); const uint16_t mask = 1 << (2 * index);
if (dir) if (dir)
Write(shr16_v & ~mask); Write(shr16_v & ~mask);
else else
Write(shr16_v | mask); Write(shr16_v | mask);
} }
}
} // namespace shr16 } // namespace shr16
} // namespace hal } // namespace hal