Configurable SPI CPHA and CPOL

pull/11/head
Alex Voinea 2021-05-17 17:45:31 +03:00
parent 57abb7ecc4
commit 13ee425352
2 changed files with 5 additions and 1 deletions

View File

@ -18,6 +18,8 @@ namespace spi {
hal::gpio::GPIO_pin sck_pin; hal::gpio::GPIO_pin sck_pin;
hal::gpio::GPIO_pin ss_pin; hal::gpio::GPIO_pin ss_pin;
uint8_t prescaler; uint8_t prescaler;
uint8_t cpha;
uint8_t cpol;
}; };
__attribute__((always_inline)) inline void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf) { __attribute__((always_inline)) inline void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf) {
@ -30,7 +32,7 @@ namespace spi {
const uint8_t spi2x = (conf->prescaler == 7) ? 0 : (conf->prescaler & 0x01); const uint8_t spi2x = (conf->prescaler == 7) ? 0 : (conf->prescaler & 0x01);
const uint8_t spr = ((conf->prescaler - 1) >> 1) & 0x03; const uint8_t spr = ((conf->prescaler - 1) >> 1) & 0x03;
hspi->SPCRx = (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | (0 << CPOL) | (0 << CPHA) | (spr << SPR0); hspi->SPCRx = (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | ((conf->cpol & 0x01) << CPOL) | ((conf->cpha & 0x01) << CPHA) | (spr << SPR0);
hspi->SPSRx = (spi2x << SPI2X); hspi->SPSRx = (spi2x << SPI2X);
} }

View File

@ -14,6 +14,8 @@ void setup() {
.sck_pin = gpio::GPIO_pin(TMC2130_SPI_SCK_PIN), .sck_pin = gpio::GPIO_pin(TMC2130_SPI_SCK_PIN),
.ss_pin = gpio::GPIO_pin(TMC2130_SPI_SS_PIN), .ss_pin = gpio::GPIO_pin(TMC2130_SPI_SS_PIN),
.prescaler = 2, //4mhz .prescaler = 2, //4mhz
.cpha = 1,
.cpol = 1,
}; };
spi::Init(SPI0, &spi_conf); spi::Init(SPI0, &spi_conf);
} }