Цитата(Сергей Борщ @ Jun 21 2017, 14:07)
Да чего уж там стыдиться, показывайте. ваше "это все" можно передавать параметром в шаблон?
Можно и так, но я в таких случаях передаю не сам шаблонный класс пина, а лишь индекс пина, на котором он должен "висеть".
И уже внутри шаблонного класса, где этот пин должен использоваться, создаю экземпляр этого пина.
Оно и логично - пин должен принадлежать только тому, кто его пользует. Он же и должен создавать этот пин.
Тогда этот пин защищен от других посягателей.
CODE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Project: Any
// Description: Pin (STM32L1xx)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stm32l1xx_rcc.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef enum
{
PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15,
PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15,
PC0, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15,
PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10, PD11, PD12, PD13, PD14, PD15,
PE0, PE1, PE2, PE3, PE4, PE5, PE6, PE7, PE8, PE9, PE10, PE11, PE12, PE13, PE14, PE15,
PF0, PF1, PF2, PF3, PF4, PF5, PF6, PF7, PF8, PF9, PF10, PF11, PF12, PF13, PF14, PF15,
PH0, PH1, PH2, PH3, PH4, PH5, PH6, PH7, PH8, PH9, PH10, PH11, PH12, PH13, PH14, PH15,
} PIN;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class PinBase
{
public:
PinBase(PIN pin)
{
portIndex = pin / 16;
pinIndex = pin % 16;
pinMask = (((int32_t)1) << pinIndex);
switch (portIndex)
{
#ifdef GPIOA_BASE
case (0): port = GPIOA; RCC->AHBENR |= RCC_AHBPeriph_GPIOA; break;
#endif
#ifdef GPIOB_BASE
case (1): port = GPIOB; RCC->AHBENR |= RCC_AHBPeriph_GPIOB; break;
#endif
#ifdef GPIOC_BASE
case (2): port = GPIOC; RCC->AHBENR |= RCC_AHBPeriph_GPIOC; break;
#endif
#ifdef GPIOD_BASE
case (3): port = GPIOD; RCC->AHBENR |= RCC_AHBPeriph_GPIOD; break;
#endif
#ifdef GPIOE_BASE
case (4): port = GPIOE; RCC->AHBENR |= RCC_AHBPeriph_GPIOE; break;
#endif
#ifdef GPIOF_BASE
case (5): port = GPIOF; RCC->AHBENR |= RCC_AHBPeriph_GPIOF; break;
#endif
#ifdef GPIOH_BASE
case (6): port = GPIOH; RCC->AHBENR |= RCC_AHBPeriph_GPIOH; break;
#endif
default : break;
}
disablePullUpAndPullDown();
}
void setAsDigitalInput() // MODER = 0
{
port->MODER &= ~(((int32_t)0x03) << (pinIndex * 2));
port->MODER |= (((int32_t)0x00) << (pinIndex * 2));
}
void setAsDigitalOutput() // MODER = 1
{
port->MODER &= ~(((int32_t)0x03) << (pinIndex * 2));
port->MODER |= (((int32_t)0x01) << (pinIndex * 2));
}
typedef int8_t AlternativeFunction;
void setAsAlternative(AlternativeFunction function) // MODER = 2
{
port->MODER &= ~(((int32_t)0x03) << (pinIndex * 2));
port->MODER |= (((int32_t)0x02) << (pinIndex * 2));
if (pinIndex < 8)
{
port->AFR[0] &= ~(((int32_t)0x0F) << (pinIndex * 4));
port->AFR[0] |= (((int32_t)function & 0x0F) << (pinIndex * 4));
}
else
{
port->AFR[1] &= ~(((int32_t)0x0F) << ((pinIndex - 8) * 4));
port->AFR[1] |= (((int32_t)function & 0x0F) << ((pinIndex - 8) * 4));
}
}
void setAsAnalogInput() // MODER = 3
{
disablePullUpAndPullDown();
port->MODER &= ~(((int32_t)0x03) << (pinIndex * 2));
port->MODER |= (((int32_t)0x03) << (pinIndex * 2));
}
void setAsAnalogOutput() // MODER = 3
{
disablePullUpAndPullDown();
port->MODER &= ~(((int32_t)0x03) << (pinIndex * 2));
port->MODER |= (((int32_t)0x03) << (pinIndex * 2));
}
void setAsPushPull() { port->OTYPER &= ~(1 << pinIndex); } // OTYPER = 0
void setAsOpenDrain() { port->OTYPER |= (1 << pinIndex); } // OTYPER = 1
void setOutputSpeed400kHz() { port->OSPEEDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->OSPEEDR |= (((int32_t)0x00) << (pinIndex * 2)); } // OSPEEDR = 0
void setOutputSpeed2MHz() { port->OSPEEDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->OSPEEDR |= (((int32_t)0x01) << (pinIndex * 2)); } // OSPEEDR = 1
void setOutputSpeed10MHz() { port->OSPEEDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->OSPEEDR |= (((int32_t)0x02) << (pinIndex * 2)); } // OSPEEDR = 2
void setOutputSpeed40MHz() { port->OSPEEDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->OSPEEDR |= (((int32_t)0x03) << (pinIndex * 2)); } // OSPEEDR = 3
void disablePullUpAndPullDown() { port->PUPDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->PUPDR |= (((int32_t)0x00) << (pinIndex * 2)); } // PUPDR = 0
void enablePullUp() { port->PUPDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->PUPDR |= (((int32_t)0x01) << (pinIndex * 2)); } // PUPDR = 1
void enablePullDown() { port->PUPDR &= ~(((int32_t)0x03) << (pinIndex * 2)); port->PUPDR |= (((int32_t)0x02) << (pinIndex * 2)); } // PUPDR = 2
inline void setToHigh() __attribute__((always_inline)) { port->BSRRL = pinMask; }
inline void setToLow() __attribute__((always_inline)) { port->BSRRH = pinMask; }
inline bool isHigh() __attribute__((always_inline)) { return (((port->IDR) & pinMask) != 0); }
inline bool isLow() __attribute__((always_inline)) { return (((port->IDR) & pinMask) == 0); }
inline void toggle() __attribute__((always_inline)) { if (isLow()) setToHigh(); else setToLow(); }
void lock()
{
volatile int32_t tempRegister = ((int32_t)1 << pinIndex);
GPIOA->LCKR = tempRegister + GPIO_LCKR_LCKK; // (1) Write LCKK bit to 1 and set the pin bits to lock
GPIOA->LCKR = tempRegister; // (2) Write LCKK bit to 0 and set the pin bits to lock
GPIOA->LCKR = tempRegister + GPIO_LCKR_LCKK ; // (3) Write LCKK bit to 1 and set the pin bits to lock
tempRegister = GPIOA->LCKR; // (4) Read the Lock register
if ((GPIOA->LCKR & GPIO_LCKR_LCKK) == 0) // (5) Check the Lock register (optional)
{
// TODO: manage an error
}
}
protected:
volatile GPIO_TypeDef * port;
volatile int8_t portIndex;
volatile int8_t pinIndex;
volatile int32_t pinMask;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <PIN pin>
class Pin : public PinBase
{
public:
Pin() : PinBase(pin) { }
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <PIN pin>
class DigitalOutputPin : public PinBase
{
public:
DigitalOutputPin() : PinBase(pin)
{
setAsDigitalOutput();
setAsPushPull();
setOutputSpeed40MHz();
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <PIN pin>
class LedOpenDrain : public PinBase
{
public:
LedOpenDrain() : PinBase(pin)
{
setAsDigitalOutput();
setAsOpenDrain();
setOutputSpeed10MHz();
off();
}
void on() { setToLow(); }
void off() { setToHigh(); }
};
......