SPI hal: Winning combo

pull/11/head
Alex Voinea 2021-05-17 09:01:22 +03:00
parent ac4fdd30ad
commit dc9528b4ea
5 changed files with 27 additions and 53 deletions

View File

@ -186,7 +186,7 @@ target_compile_options(firmware PRIVATE -Wdouble-promotion)
# target_link_libraries( firmware PRIVATE A3idesHeaders ) # target_link_libraries( firmware PRIVATE A3idesHeaders )
target_sources(firmware PRIVATE src/main.cpp src/hal/avr/cpu.cpp src/hal/avr/spi.cpp) target_sources(firmware PRIVATE src/main.cpp src/hal/avr/cpu.cpp)
set_property( set_property(
SOURCE src/version.c SOURCE src/version.c

View File

@ -1,21 +0,0 @@
#include "../spi.h"
namespace hal {
namespace spi {
// void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf)
// {
// using namespace hal;
// // gpio::Init(conf->miso_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none));
// // gpio::Init(conf->mosi_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
// // gpio::Init(conf->sck_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
// // gpio::Init(conf->ss_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::high));
// const uint8_t spi2x = (conf->prescaler == 7) ? 0 : (conf->prescaler & 0x01);
// 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->SPSRx = (spi2x << SPI2X);
// }
}
}

View File

@ -47,22 +47,22 @@ namespace gpio {
, pin(pin) {}; , pin(pin) {};
}; };
inline void WritePin(const GPIO_pin portPin, Level level) { __attribute__((always_inline)) inline void WritePin(const GPIO_pin portPin, Level level) {
if (level == Level::high) if (level == Level::high)
portPin.port->PORTx |= (1 << portPin.pin); portPin.port->PORTx |= (1 << portPin.pin);
else else
portPin.port->PORTx &= ~(1 << portPin.pin); portPin.port->PORTx &= ~(1 << portPin.pin);
} }
inline Level ReadPin(const GPIO_pin portPin) { __attribute__((always_inline)) inline Level ReadPin(const GPIO_pin portPin) {
return (Level)(portPin.port->PINx & (1 << portPin.pin)); return (Level)(portPin.port->PINx & (1 << portPin.pin));
} }
inline void TogglePin(const GPIO_pin portPin) { __attribute__((always_inline)) inline void TogglePin(const GPIO_pin portPin) {
portPin.port->PINx |= (1 << portPin.pin); portPin.port->PINx |= (1 << portPin.pin);
} }
inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) { __attribute__((always_inline)) inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) {
if (GPIO_Init.mode == Mode::output) { if (GPIO_Init.mode == Mode::output) {
WritePin(portPin, GPIO_Init.level); WritePin(portPin, GPIO_Init.level);
portPin.port->DDRx |= (1 << portPin.pin); portPin.port->DDRx |= (1 << portPin.pin);

View File

@ -13,29 +13,26 @@ namespace spi {
}; };
struct SPI_InitTypeDef { struct SPI_InitTypeDef {
// hal::gpio::GPIO_pin miso_pin; hal::gpio::GPIO_pin miso_pin;
// hal::gpio::GPIO_pin mosi_pin; hal::gpio::GPIO_pin mosi_pin;
// 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;
}; };
// void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf); __attribute__((always_inline)) inline void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf) {
inline void Init(SPI_TypeDef *const hspi, uint8_t prescaler) {
using namespace hal; using namespace hal;
// gpio::Init(conf->miso_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none)); gpio::Init(conf->miso_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none));
// gpio::Init(conf->mosi_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); gpio::Init(conf->mosi_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
// gpio::Init(conf->sck_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); gpio::Init(conf->sck_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
// gpio::Init(conf->ss_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::high)); gpio::Init(conf->ss_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::high));
const uint8_t spi2x = (prescaler == 7) ? 0 : (prescaler & 0x01); const uint8_t spi2x = (conf->prescaler == 7) ? 0 : (conf->prescaler & 0x01);
const uint8_t spr = ((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) | (0 << CPOL) | (0 << CPHA) | (spr << SPR0);
hspi->SPSRx = (spi2x << SPI2X); hspi->SPSRx = (spi2x << SPI2X);
} }
} }
} }

View File

@ -4,18 +4,17 @@
/// One-time setup of HW and SW components /// One-time setup of HW and SW components
/// Called before entering the loop() function /// Called before entering the loop() function
void setup(){ void setup() {
using namespace hal; using namespace hal;
gpio::Init(gpio::GPIO_pin(GPIOB, 3), gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none)); spi::SPI_InitTypeDef spi_conf = {
gpio::Init(gpio::GPIO_pin(GPIOB, 2), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); .miso_pin = gpio::GPIO_pin(GPIOB, 3),
gpio::Init(gpio::GPIO_pin(GPIOB, 1), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); .mosi_pin = gpio::GPIO_pin(GPIOB, 2),
// gpio::Init(gpio::GPIO_pin(GPIOB, 0), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); .sck_pin = gpio::GPIO_pin(GPIOB, 1),
.ss_pin = gpio::GPIO_pin(GPIOB, 0),
// spi::SPI_InitTypeDef spi_conf = { .prescaler = 2, //4mhz
// .prescaler = 2, //4mhz };
// }; spi::Init(SPI0, &spi_conf);
spi::Init(SPI0, 2);
} }
/// Main loop of the firmware /// Main loop of the firmware
@ -32,13 +31,12 @@ void setup(){
/// StepWhateverElseNeedsStepping(); /// StepWhateverElseNeedsStepping();
/// The idea behind the Step* routines is to keep each automaton non-blocking allowing for some “concurrency”. /// The idea behind the Step* routines is to keep each automaton non-blocking allowing for some “concurrency”.
/// Some FW components will leverage ISR to do their stuff (UART, motor stepping?, etc.) /// Some FW components will leverage ISR to do their stuff (UART, motor stepping?, etc.)
void loop(){ void loop() {
} }
int main() { int main() {
setup(); setup();
for(;;){ for (;;) {
using namespace hal::gpio; using namespace hal::gpio;
WritePin(GPIO_pin(GPIOB, 5), Level::low); WritePin(GPIO_pin(GPIOB, 5), Level::low);
TogglePin(GPIO_pin(GPIOB, 6)); TogglePin(GPIO_pin(GPIOB, 6));