Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Stm32Gpio.hxx
Go to the documentation of this file.
1
36#ifndef _FREERTOS_DRIVERS_ST_STM32GPIO_HXX_
37#define _FREERTOS_DRIVERS_ST_STM32GPIO_HXX_
38
39#include "os/Gpio.hxx"
40#include "GpioWrapper.hxx"
41
42#include "stm32f_hal_conf.hxx"
43
49template <uint32_t GPIOx, uint16_t PIN, uint8_t PIN_NUM> struct Stm32GpioDefs
50{
52 static GPIO_TypeDef *port()
53 {
54 return (GPIO_TypeDef *)GPIOx;
55 }
56
58 static uint16_t pin()
59 {
60 return PIN;
61 }
62
64 static void set(bool value)
65 {
66 if (value)
67 {
68// These alternatives are needed for old releases of STM32F HAL library.
69#if 0 && (defined(STM32F303xC) || defined(STM32F303xE))
70 port()->BSRRL = pin();
71#else
72 port()->BSRR = pin();
73#endif
74 }
75 else
76 {
77#if 0 && (defined(STM32F303xC) || defined(STM32F303xE))
78 port()->BSRRH = pin();
79#else
80 port()->BSRR = pin() << 16;
81#endif
82 }
83 }
84
86 static bool get()
87 {
88 return port()->IDR & pin();
89 }
90
92 static void toggle()
93 {
94 set(!get());
95 }
96
99 static constexpr const Gpio *instance()
100 {
102 }
103
105 static bool is_output()
106 {
107 uint8_t* mode = (uint8_t*)port();
108 uint8_t m = mode[PIN_NUM >> 1];
109 if (PIN_NUM & 1) { m >>= 4; }
110 return (m & 3) != 0;
111 }
112
113};
114
115template <class Defs, bool SAFE_VALUE> struct GpioOutputPin : public Defs
116{
117 using Defs::port;
118 using Defs::pin;
119 using Defs::set;
121 static void hw_init()
122 {
123 GPIO_InitTypeDef gpio_init = {0};
124 gpio_init.Pin = pin();
125 gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
126 gpio_init.Pull = GPIO_NOPULL;
127 gpio_init.Speed = GPIO_SPEED_FREQ_LOW;
128 HAL_GPIO_Init(port(), &gpio_init);
129 set(SAFE_VALUE);
130 }
132 static void hw_set_to_safe()
133 {
134 hw_init();
135 set(SAFE_VALUE);
136 }
137};
138
142template <class Defs>
143struct GpioOutputSafeLow : public GpioOutputPin<Defs, false>
144{
145};
146
150template <class Defs>
151struct GpioOutputSafeHigh : public GpioOutputPin<Defs, true>
152{
153};
154
159template <class Defs> struct LedPin : public GpioOutputPin<Defs, false>
160{
161};
162
163template <class Defs, uint32_t PULL_MODE> struct GpioInputPin : public Defs
164{
165 using Defs::port;
166 using Defs::pin;
167 using Defs::set;
169 static void hw_init()
170 {
171 GPIO_InitTypeDef gpio_init = {0};
172 gpio_init.Pin = pin();
173 gpio_init.Mode = GPIO_MODE_INPUT;
174 gpio_init.Pull = PULL_MODE;
175 gpio_init.Speed = GPIO_SPEED_FREQ_LOW;
176 HAL_GPIO_Init(port(), &gpio_init);
177 }
179 static void hw_set_to_safe()
180 {
181 hw_init();
182 }
184 static bool is_output()
185 {
186 return false;
187 }
188};
189
193template <class Defs>
194struct GpioInputPU : public GpioInputPin<Defs, GPIO_PULLUP>
195{
196};
197
201template <class Defs>
202struct GpioInputPD : public GpioInputPin<Defs, GPIO_PULLDOWN>
203{
204};
205
209template <class Defs>
210struct GpioInputNP : public GpioInputPin<Defs, GPIO_NOPULL>
211{
212};
213
232#define GPIO_PIN(NAME, BaseClass, PORTNAME, NUM) \
233 typedef BaseClass<Stm32GpioDefs<(uint32_t)(GPIO ## PORTNAME ## _BASE), GPIO_PIN_ ## NUM, NUM>> NAME##_Pin
234
235#endif // _FREERTOS_DRIVERS_ST_STM32GPIO_HXX_
Creates an implementation of an os-independent Gpio object from a hardware-specific static Gpio struc...
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
Defines a GPIO input pin.
Defines a GPIO input pin with pull-down enabled.
Defines a GPIO input pin with pull-up.
Parametric GPIO input class.
static bool is_output()
static void hw_init()
Initializes the hardware pin.
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
Parametric GPIO output class.
static void hw_init()
Initializes the hardware pin.
static void set(bool value)
Sets the output pinm.
static void hw_set_to_safe()
Sets the output pin to a safe value.
Defines a GPIO output pin, initialized to be an output pin with high level.
Defines a GPIO output pin, initialized to be an output pin with low level.
Defines a GPIO output pin with high-current drive and low initialization level.
Static GPIO implementation for the STM32 microcontrollers.
Definition Stm32Gpio.hxx:50
static bool get()
Sets the output pin to a given level.
Definition Stm32Gpio.hxx:86
static void set(bool value)
Sets the output pin to a given level.
Definition Stm32Gpio.hxx:64
static uint16_t pin()
Definition Stm32Gpio.hxx:58
static constexpr const Gpio * instance()
Definition Stm32Gpio.hxx:99
static bool is_output()
static void toggle()
Changes the output pin level.
Definition Stm32Gpio.hxx:92
static GPIO_TypeDef * port()
Definition Stm32Gpio.hxx:52