Change the pin definitions in order to be valid types

Remove the constructor from GPIO_pin so that we can use brace
initialization at compile time.

Rewrite the contents of pins.h to construct GPIO_pin types directly by
the use of a simple preprocessor macro.

Makes the code type-check and easier to read/extend.
pull/41/head
Yuri D'Elia 2021-07-02 19:11:54 +02:00 committed by DRracer
parent e9627ad0d4
commit 169665331e
4 changed files with 27 additions and 24 deletions

View File

@ -13,22 +13,22 @@ SHR16 shr16;
void SHR16::Init() {
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));
gpio::Init(SHR16_DATA, GPIO_InitTypeDef(Mode::output, Level::low));
gpio::Init(SHR16_LATCH, GPIO_InitTypeDef(Mode::output, Level::high));
gpio::Init(SHR16_CLOCK, GPIO_InitTypeDef(Mode::output, Level::low));
Write(0);
}
void SHR16::Write(uint16_t v) {
using namespace hal::gpio;
WritePin(GPIO_pin(SHR16_LATCH), Level::low);
WritePin(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(SHR16_DATA, (Level)((m & v) != 0));
WritePin(SHR16_CLOCK, Level::high);
WritePin(SHR16_CLOCK, Level::low);
}
WritePin(GPIO_pin(SHR16_LATCH), Level::high);
WritePin(SHR16_LATCH, Level::high);
shr16_v = v;
}

View File

@ -40,11 +40,10 @@ struct GPIO_InitTypeDef {
};
struct GPIO_pin {
// No constructor here in order to allow brace-initialization in old
// gcc versions/standards
GPIO_TypeDef *const port;
const uint8_t pin;
inline GPIO_pin(GPIO_TypeDef *const port, const uint8_t pin)
: port(port)
, pin(pin) {};
};
__attribute__((always_inline)) inline void WritePin(const GPIO_pin portPin, Level level) {

View File

@ -110,8 +110,8 @@ void setup() {
// @@TODO if the shift register doesn't work we really can't signalize anything, only internal variables will be accessible if the UART works
hu::USART::USART_InitTypeDef usart_conf = {
.rx_pin = gpio::GPIO_pin(GPIOD, 2),
.tx_pin = gpio::GPIO_pin(GPIOD, 3),
.rx_pin = USART_RX,
.tx_pin = USART_TX,
.baudrate = 115200,
};
hu::usart1.Init(&usart_conf);
@ -121,10 +121,10 @@ void setup() {
// @@TODO if both shift register and the UART are dead, we are sitting ducks :(
spi::SPI_InitTypeDef spi_conf = {
.miso_pin = gpio::GPIO_pin(TMC2130_SPI_MISO_PIN),
.mosi_pin = gpio::GPIO_pin(TMC2130_SPI_MOSI_PIN),
.sck_pin = gpio::GPIO_pin(TMC2130_SPI_SCK_PIN),
.ss_pin = gpio::GPIO_pin(TMC2130_SPI_SS_PIN),
.miso_pin = TMC2130_SPI_MISO_PIN,
.mosi_pin = TMC2130_SPI_MOSI_PIN,
.sck_pin = TMC2130_SPI_SCK_PIN,
.ss_pin = TMC2130_SPI_SS_PIN,
.prescaler = 2, //4mhz
.cpha = 1,
.cpol = 1,

View File

@ -2,12 +2,16 @@
#include "hal/gpio.h"
/// pin definitions
#define GPIO_PIN(port, pin) hal::gpio::GPIO_pin{port, pin}
#define TMC2130_SPI_MISO_PIN GPIOB, 3
#define TMC2130_SPI_MOSI_PIN GPIOB, 2
#define TMC2130_SPI_SCK_PIN GPIOB, 1
#define TMC2130_SPI_SS_PIN GPIOB, 0
#define TMC2130_SPI_MISO_PIN GPIO_PIN(GPIOB, 3)
#define TMC2130_SPI_MOSI_PIN GPIO_PIN(GPIOB, 2)
#define TMC2130_SPI_SCK_PIN GPIO_PIN(GPIOB, 1)
#define TMC2130_SPI_SS_PIN GPIO_PIN(GPIOB, 0)
#define SHR16_DATA GPIOB, 5 ///DS
#define SHR16_LATCH GPIOB, 6 ///STCP
#define SHR16_CLOCK GPIOC, 7 ///SHCP
#define SHR16_DATA GPIO_PIN(GPIOB, 5) ///DS
#define SHR16_LATCH GPIO_PIN(GPIOB, 6) ///STCP
#define SHR16_CLOCK GPIO_PIN(GPIOC, 7) ///SHCP
#define USART_RX GPIO_PIN(GPIOD, 2)
#define USART_TX GPIO_PIN(GPIOD, 3)