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,42 +23,46 @@ 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); for (uint16_t m = 0x8000; m; m >>= 1) {
for (uint16_t m = 0x8000; m; m >>= 1) { WritePin(SHR16_DATA, (Level)((m & v) != 0));
WritePin(SHR16_DATA, (Level)((m & v) != 0)); asm("nop");
asm("nop"); WritePin(SHR16_CLOCK, Level::high);
WritePin(SHR16_CLOCK, Level::high); asm("nop");
asm("nop"); WritePin(SHR16_CLOCK, Level::low);
WritePin(SHR16_CLOCK, Level::low); asm("nop");
asm("nop");
}
WritePin(SHR16_LATCH, Level::high);
shr16_v = v;
} }
WritePin(SHR16_LATCH, Level::high);
shr16_v = v;
} }
void SHR16::SetLED(uint16_t led) { void SHR16::SetLED(uint16_t led) {
led = ((led & 0x00ff) << 8) | ((led & 0x0300) >> 2); ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
Write((shr16_v & ~SHR16_LED_MSK) | led); led = ((led & 0x00ff) << 8) | ((led & 0x0300) >> 2);
Write((shr16_v & ~SHR16_LED_MSK) | led);
}
} }
void SHR16::SetTMCEnabled(uint8_t index, bool ena) { void SHR16::SetTMCEnabled(uint8_t index, bool ena) {
const uint16_t mask = 1 << (2 * index + 1); ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (ena) const uint16_t mask = 1 << (2 * index + 1);
Write(shr16_v & ~mask); if (ena)
else Write(shr16_v & ~mask);
Write(shr16_v | mask); else
Write(shr16_v | mask);
}
} }
void SHR16::SetTMCDir(uint8_t index, bool dir) { void SHR16::SetTMCDir(uint8_t index, bool dir) {
const uint16_t mask = 1 << (2 * index); ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (dir) const uint16_t mask = 1 << (2 * index);
Write(shr16_v & ~mask); if (dir)
else Write(shr16_v & ~mask);
Write(shr16_v | mask); else
Write(shr16_v | mask);
}
} }
} // namespace shr16 } // namespace shr16