diff --git a/src/hal/avr/shr16.cpp b/src/hal/avr/shr16.cpp index c024be1..5ac52a3 100644 --- a/src/hal/avr/shr16.cpp +++ b/src/hal/avr/shr16.cpp @@ -1,5 +1,6 @@ #include "../shr16.h" #include "../gpio.h" +#include "../../pins.h" namespace hal { namespace shr16 { @@ -7,34 +8,24 @@ namespace shr16 { SHR16 shr16; void SHR16::Init() { - // DDRC |= 0x80; - // DDRB |= 0x40; - // DDRB |= 0x20; - // PORTC &= ~0x80; - // PORTB &= ~0x40; - // PORTB &= ~0x20; - // shr16_v = 0; - // Write(shr16_v); - // Write(shr16_v); + using namespace hal::gpio; + gpio::Init(GPIO_pin(SHR16_DATA), GPIO_InitTypeDef(Mode::output, Level::low)); + gpio::Init(GPIO_pin(SHR16_LATCH), GPIO_InitTypeDef(Mode::output, Level::high)); + gpio::Init(GPIO_pin(SHR16_CLOCK), GPIO_InitTypeDef(Mode::output, Level::low)); + Write(0); } void SHR16::Write(uint16_t v) { - // PORTB &= ~0x40; - // asm("nop"); - // for (uint16_t m = 0x8000; m; m >>= 1) - // { - // if (m & v) - // PORTB |= 0x20; - // else - // PORTB &= ~0x20; - // PORTC |= 0x80; - // asm("nop"); - // PORTC &= ~0x80; - // asm("nop"); - // } - // PORTB |= 0x40; - // asm("nop"); - // shr16_v = v; + using namespace hal::gpio; + WritePin(GPIO_pin(SHR16_LATCH), Level::low); + for (uint16_t m = 0x8000; m; m >>= 1) + { + WritePin(GPIO_pin(SHR16_DATA), (Level)((m & v) != 0)); + WritePin(GPIO_pin(SHR16_CLOCK), Level::high); + WritePin(GPIO_pin(SHR16_CLOCK), Level::low); + } + WritePin(GPIO_pin(SHR16_LATCH), Level::high); + shr16_v = v; } void SHR16::SetLED(uint16_t led) { diff --git a/src/pins.h b/src/pins.h index 154436f..be9853a 100644 --- a/src/pins.h +++ b/src/pins.h @@ -7,3 +7,7 @@ #define TMC2130_SPI_MOSI_PIN GPIOB, 2 #define TMC2130_SPI_SCK_PIN GPIOB, 1 #define TMC2130_SPI_SS_PIN GPIOB, 0 + +#define SHR16_DATA GPIOB, 5 ///DS +#define SHR16_LATCH GPIOB, 6 ///STCP +#define SHR16_CLOCK GPIOB, 7 ///SHCP