diff --git a/CMakeLists.txt b/CMakeLists.txt index ee55d68..58d59c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,6 +170,8 @@ objcopy(firmware "ihex" ".hex") add_custom_command(TARGET firmware POST_BUILD COMMAND avr-objdump ARGS -CSd firmware > firmware.txt) +add_custom_command(TARGET firmware POST_BUILD COMMAND avr-size ARGS -C --mcu=atmega32u4 firmware) + # generate linker map file target_link_options(firmware PUBLIC -Wl,-Map=firmware.map) @@ -184,7 +186,7 @@ target_compile_options(firmware PRIVATE -Wdouble-promotion) # target_link_libraries( firmware PRIVATE A3idesHeaders ) -target_sources(firmware PRIVATE src/main.cpp src/hal/avr/cpu.cpp) +target_sources(firmware PRIVATE src/main.cpp src/hal/avr/cpu.cpp src/hal/avr/spi.cpp) set_property( SOURCE src/version.c diff --git a/src/hal/avr/spi.cpp b/src/hal/avr/spi.cpp new file mode 100644 index 0000000..73e260a --- /dev/null +++ b/src/hal/avr/spi.cpp @@ -0,0 +1,21 @@ +#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); + // } +} +} diff --git a/src/hal/gpio.h b/src/hal/gpio.h index 80f3545..0ba9687 100644 --- a/src/hal/gpio.h +++ b/src/hal/gpio.h @@ -1,3 +1,4 @@ +#pragma once #include #include @@ -30,9 +31,6 @@ namespace gpio { Mode mode; Pull pull; Level level; - inline GPIO_InitTypeDef() - : mode(Mode::input) - , pull(Pull::none) {}; inline GPIO_InitTypeDef(Mode mode, Pull pull) : mode(mode) , pull(pull) {}; @@ -41,28 +39,36 @@ namespace gpio { , level(level) {}; }; - inline void WritePin(GPIO_TypeDef *const port, const uint8_t pin, Level level) { + struct GPIO_pin { + GPIO_TypeDef *const port; + const uint8_t pin; + inline GPIO_pin(GPIO_TypeDef *const port, const uint8_t pin) + : port(port) + , pin(pin) {}; + }; + + inline void WritePin(const GPIO_pin portPin, Level level) { if (level == Level::high) - port->PORTx |= (1 << pin); + portPin.port->PORTx |= (1 << portPin.pin); else - port->PORTx &= ~(1 << pin); + portPin.port->PORTx &= ~(1 << portPin.pin); } - inline Level ReadPin(GPIO_TypeDef *const port, const uint8_t pin) { - return (Level)(port->PINx & (1 << pin)); + inline Level ReadPin(const GPIO_pin portPin) { + return (Level)(portPin.port->PINx & (1 << portPin.pin)); } - inline void TogglePin(GPIO_TypeDef *const port, const uint8_t pin) { - port->PINx |= (1 << pin); + inline void TogglePin(const GPIO_pin portPin) { + portPin.port->PINx |= (1 << portPin.pin); } - inline void Init(GPIO_TypeDef *const port, const uint8_t pin, GPIO_InitTypeDef GPIO_Init) { + inline void Init(const GPIO_pin portPin, GPIO_InitTypeDef GPIO_Init) { if (GPIO_Init.mode == Mode::output) { - WritePin(port, pin, GPIO_Init.level); - port->DDRx |= (1 << pin); + WritePin(portPin, GPIO_Init.level); + portPin.port->DDRx |= (1 << portPin.pin); } else { - port->DDRx &= ~(1 << pin); - WritePin(port, pin, (Level)GPIO_Init.pull); + portPin.port->DDRx &= ~(1 << portPin.pin); + WritePin(portPin, (Level)GPIO_Init.pull); } } diff --git a/src/hal/spi.h b/src/hal/spi.h new file mode 100644 index 0000000..3c22ec4 --- /dev/null +++ b/src/hal/spi.h @@ -0,0 +1,42 @@ +#pragma once +#include +#include "gpio.h" + +/// SPI interface + +namespace hal { +namespace spi { + struct SPI_TypeDef { + volatile uint8_t SPCRx; + volatile uint8_t SPSRx; + volatile uint8_t SPDRx; + }; + + struct SPI_InitTypeDef { + // hal::gpio::GPIO_pin miso_pin; + // hal::gpio::GPIO_pin mosi_pin; + // hal::gpio::GPIO_pin sck_pin; + // hal::gpio::GPIO_pin ss_pin; + uint8_t prescaler; + }; + + // void Init(SPI_TypeDef *const hspi, SPI_InitTypeDef *const conf); + + inline void Init(SPI_TypeDef *const hspi, uint8_t prescaler) { + 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 = (prescaler == 7) ? 0 : (prescaler & 0x01); + const uint8_t spr = ((prescaler - 1) >> 1) & 0x03; + + hspi->SPCRx = (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR) | (0 << CPOL) | (0 << CPHA) | (spr << SPR0); + hspi->SPSRx = (spi2x << SPI2X); + } + +} +} + +#define SPI0 ((hal::spi::SPI_TypeDef *)&SPCR) diff --git a/src/main.cpp b/src/main.cpp index f26c6b1..ca2ab7f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,21 @@ #include "logic/mm_control.h" #include "hal/gpio.h" +#include "hal/spi.h" /// One-time setup of HW and SW components /// Called before entering the loop() function void setup(){ - using namespace hal::gpio; - hal::gpio::Init(GPIOB, 1, GPIO_InitTypeDef(Mode::output, Level::low)); + using namespace hal; + gpio::Init(gpio::GPIO_pin(GPIOB, 3), gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Pull::none)); + gpio::Init(gpio::GPIO_pin(GPIOB, 2), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); + gpio::Init(gpio::GPIO_pin(GPIOB, 1), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); + // gpio::Init(gpio::GPIO_pin(GPIOB, 0), gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); + + // spi::SPI_InitTypeDef spi_conf = { + // .prescaler = 2, //4mhz + // }; + spi::Init(SPI0, 2); } /// Main loop of the firmware @@ -31,9 +40,9 @@ int main() { setup(); for(;;){ using namespace hal::gpio; - WritePin(GPIOB, 5, Level::low); - TogglePin(GPIOB, 6); - if (hal::gpio::ReadPin(GPIOB, 7) == hal::gpio::Level::low) + WritePin(GPIO_pin(GPIOB, 5), Level::low); + TogglePin(GPIO_pin(GPIOB, 6)); + if (hal::gpio::ReadPin(GPIO_pin(GPIOB, 7)) == hal::gpio::Level::low) break; loop(); }