Fix formatting
parent
65e45b2cc2
commit
5b2a7d0cf2
|
|
@ -4,73 +4,68 @@
|
||||||
namespace hal {
|
namespace hal {
|
||||||
namespace gpio {
|
namespace gpio {
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
|
||||||
volatile uint8_t PINx;
|
|
||||||
volatile uint8_t DDRx;
|
|
||||||
volatile uint8_t PORTx;
|
|
||||||
} GPIO_TypeDef;
|
|
||||||
|
|
||||||
enum class Mode
|
|
||||||
{
|
{
|
||||||
|
volatile uint8_t PINx;
|
||||||
|
volatile uint8_t DDRx;
|
||||||
|
volatile uint8_t PORTx;
|
||||||
|
} GPIO_TypeDef;
|
||||||
|
|
||||||
|
enum class Mode {
|
||||||
input = 0,
|
input = 0,
|
||||||
output,
|
output,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Pull
|
enum class Pull {
|
||||||
{
|
|
||||||
none = 0,
|
none = 0,
|
||||||
up,
|
up,
|
||||||
down, //not available on the AVR
|
down, //not available on the AVR
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Level
|
enum class Level {
|
||||||
{
|
|
||||||
low = 0,
|
low = 0,
|
||||||
high,
|
high,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GPIO_InitTypeDef
|
struct GPIO_InitTypeDef {
|
||||||
{
|
Mode mode;
|
||||||
Mode mode;
|
Pull pull;
|
||||||
Pull pull;
|
Level level;
|
||||||
Level level;
|
inline GPIO_InitTypeDef()
|
||||||
inline GPIO_InitTypeDef() : mode(Mode::input), pull(Pull::none){};
|
: mode(Mode::input)
|
||||||
inline GPIO_InitTypeDef(Mode mode, Pull pull) : mode(mode), pull(pull) {};
|
, pull(Pull::none) {};
|
||||||
inline GPIO_InitTypeDef(Mode mode, Level level) : mode(mode), level(level) {};
|
inline GPIO_InitTypeDef(Mode mode, Pull pull)
|
||||||
};
|
: mode(mode)
|
||||||
|
, pull(pull) {};
|
||||||
|
inline GPIO_InitTypeDef(Mode mode, Level level)
|
||||||
|
: mode(mode)
|
||||||
|
, level(level) {};
|
||||||
|
};
|
||||||
|
|
||||||
inline void WritePin(GPIO_TypeDef * const port, const uint8_t pin, Level level)
|
inline void WritePin(GPIO_TypeDef *const port, const uint8_t pin, Level level) {
|
||||||
{
|
if (level == Level::high)
|
||||||
if (level == Level::high)
|
port->PORTx |= (1 << pin);
|
||||||
port->PORTx |= (1 << pin);
|
else
|
||||||
else
|
port->PORTx &= ~(1 << pin);
|
||||||
port->PORTx &= ~(1 << pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Level ReadPin(GPIO_TypeDef * const port, const uint8_t pin)
|
|
||||||
{
|
|
||||||
return (Level)(port->PINx & (1 << pin));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TogglePin(GPIO_TypeDef * const port, const uint8_t pin)
|
|
||||||
{
|
|
||||||
port->PINx |= (1 << pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Init(GPIO_TypeDef * const port, const uint8_t pin, GPIO_InitTypeDef GPIO_Init)
|
|
||||||
{
|
|
||||||
if (GPIO_Init.mode == Mode::output)
|
|
||||||
{
|
|
||||||
WritePin(port, pin, GPIO_Init.level);
|
|
||||||
port->DDRx |= (1 << pin);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
inline Level ReadPin(GPIO_TypeDef *const port, const uint8_t pin) {
|
||||||
port->DDRx &= ~(1 << pin);
|
return (Level)(port->PINx & (1 << pin));
|
||||||
WritePin(port, pin, (Level)GPIO_Init.pull);
|
}
|
||||||
|
|
||||||
|
inline void TogglePin(GPIO_TypeDef *const port, const uint8_t pin) {
|
||||||
|
port->PINx |= (1 << pin);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Init(GPIO_TypeDef *const port, const uint8_t pin, GPIO_InitTypeDef GPIO_Init) {
|
||||||
|
if (GPIO_Init.mode == Mode::output) {
|
||||||
|
WritePin(port, pin, GPIO_Init.level);
|
||||||
|
port->DDRx |= (1 << pin);
|
||||||
|
} else {
|
||||||
|
port->DDRx &= ~(1 << pin);
|
||||||
|
WritePin(port, pin, (Level)GPIO_Init.pull);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue