Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TivaGPIO.hxx
Go to the documentation of this file.
1
36#ifndef _FREERTOS_DRIVERS_TI_TIVAGPIO_HXX_
37#define _FREERTOS_DRIVERS_TI_TIVAGPIO_HXX_
38
39#include "os/Gpio.hxx"
40#include "driverlib/gpio.h"
41#include "driverlib/pin_map.h"
42#include "driverlib/sysctl.h"
43#include "inc/hw_types.h"
44#include "inc/hw_memmap.h"
45#include "inc/hw_gpio.h"
46#include "driverlib/rom_map.h"
47
51#define DECL_PIN(NAME, PORT, NUM) \
52 static const auto NAME##_PERIPH = SYSCTL_PERIPH_GPIO##PORT; \
53 static const auto NAME##_BASE = GPIO_PORT##PORT##_BASE; \
54 static const auto NAME##_PIN = GPIO_PIN_##NUM; \
55
60#define DECL_HWPIN(NAME, PORT, NUM, CONFIG, Type) \
61 DECL_PIN(NAME, PORT, NUM); \
62 static void NAME##_SetPinType() \
63 { \
64 MAP_GPIOPinType##Type(NAME##_BASE, NAME##_PIN); \
65 } \
66 static const auto NAME##_CONFIG = GPIO_P##PORT##NUM##_##CONFIG
67
68template <class Defs, bool SAFE_VALUE> struct GpioOutputPin;
69template <class Defs, uint32_t GPIO_PULL> struct GpioInputPin;
70
72template <unsigned GPIO_BASE, unsigned GPIO_PIN> class TivaGpio : public Gpio
73{
74public:
77 constexpr TivaGpio()
78 {
79 }
80
81 void write(Value new_state) const OVERRIDE
82 {
83 *pin_address() = (new_state ? 0xff : 0);
84 }
85
86 void set() const OVERRIDE
87 {
88 *pin_address() = 0xff;
89 }
90
91 void clr() const OVERRIDE
92 {
93 *pin_address() = 0;
94 }
95
97 {
98 return *pin_address() ? VHIGH : VLOW;
99 }
100
102 {
103 if (dir == Direction::DOUTPUT)
104 {
105 GPIOPinTypeGPIOOutput(GPIO_BASE, GPIO_PIN);
106 }
107 else
108 {
109 GPIOPinTypeGPIOInput(GPIO_BASE, GPIO_PIN);
110 }
111 }
112
114 {
115 uint32_t mode = GPIODirModeGet(GPIO_BASE, GPIO_PIN);
116 switch (mode)
117 {
118 default:
119 HASSERT(0);
120 case GPIO_DIR_MODE_IN:
121 return Direction::DINPUT;
122 case GPIO_DIR_MODE_OUT:
123 return Direction::DOUTPUT;
124 }
125 }
126
127private:
128 template <class Defs> friend struct GpioShared;
134 static const TivaGpio instance_;
135
142 constexpr volatile uint8_t *pin_address() const
143 {
144 return reinterpret_cast<volatile uint8_t *>(
145 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
146 }
147};
148
150template <unsigned GPIO_BASE, unsigned GPIO_PIN>
152
155template<class Defs> struct GpioShared : public Defs {
156public:
157 using Defs::GPIO_PERIPH;
158 using Defs::GPIO_BASE;
159 using Defs::GPIO_PIN;
160
162 static void unlock()
163 {
164 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
165 MAP_SysCtlDelay(26);
166 HWREG(GPIO_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
167 HWREG(GPIO_BASE + GPIO_O_CR) |= GPIO_PIN;
168 HWREG(GPIO_BASE + GPIO_O_LOCK) = 0;
169 }
171 static void lock()
172 {
173 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
174 HWREG(GPIO_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
175 HWREG(GPIO_BASE + GPIO_O_CR) &= ~GPIO_PIN;
176 HWREG(GPIO_BASE + GPIO_O_LOCK) = 0;
177 }
178
181 static void set(bool value)
182 {
183 HWREGB(ptr_address()) = value ? 0xff : 0;
184 }
186 static bool get()
187 {
188 return HWREGB(ptr_address()) != 0;
189 }
191 static void toggle()
192 {
193 set(!get());
194 }
195
197 static constexpr uint32_t ptr_address()
198 {
199 return GPIO_BASE + (((unsigned)GPIO_PIN) << 2);
200 }
201
203 static constexpr const Gpio *instance()
204 {
206 }
207};
208
216template <class Defs, bool SAFE_VALUE>
217struct GpioOutputPin : public GpioShared<Defs>
218{
219public:
220 using Defs::GPIO_PERIPH;
221 using Defs::GPIO_BASE;
222 using Defs::GPIO_PIN;
223 using GpioShared<Defs>::set;
225 static void hw_init()
226 {
227 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
228 MAP_GPIOPinTypeGPIOOutput(GPIO_BASE, GPIO_PIN);
229 set(SAFE_VALUE);
230 }
232 static void hw_set_to_safe()
233 {
234 hw_init();
235 }
236
238 static bool is_output()
239 {
240 return true;
241 }
242};
243
247template <class Defs>
248struct GpioOutputSafeLow : public GpioOutputPin<Defs, false>
249{
250};
251
255template <class Defs>
256struct GpioOutputSafeHigh : public GpioOutputPin<Defs, true>
257{
258};
259
264template <class Defs, bool SAFE_VALUE> struct GpioOutputOD : public GpioOutputPin<Defs, SAFE_VALUE>
265{
266public:
267 using Defs::GPIO_PERIPH;
268 using Defs::GPIO_BASE;
269 using Defs::GPIO_PIN;
271 static void hw_init()
272 {
274 MAP_GPIOPadConfigSet(
275 GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
276 }
277};
278
282template <class Defs>
284
289template <class Defs>
291
292
297template <class Defs> struct LedPin : public GpioOutputPin<Defs, false>
298{
299public:
300 using Defs::GPIO_PERIPH;
301 using Defs::GPIO_BASE;
302 using Defs::GPIO_PIN;
304 static void hw_init()
305 {
307 MAP_GPIOPadConfigSet(
308 GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_8MA_SC, GPIO_PIN_TYPE_STD);
309 }
310};
311
329#define GPIO_PIN(NAME, BaseClass, PORT, NUM) \
330 struct NAME##Defs \
331 { \
332 DECL_PIN(GPIO, PORT, NUM); \
333 }; \
334 typedef BaseClass<NAME##Defs> NAME##_Pin
335
337template <class Defs, uint32_t GPIO_PULL>
338struct GpioInputPin : public GpioShared<Defs>
339{
340public:
341 using Defs::GPIO_PERIPH;
342 using Defs::GPIO_BASE;
343 using Defs::GPIO_PIN;
345 static void hw_init()
346 {
347 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
348 MAP_GPIOPinTypeGPIOInput(GPIO_BASE, GPIO_PIN);
349 MAP_GPIOPadConfigSet(GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_2MA, GPIO_PULL);
350 }
352 static void hw_set_to_safe()
353 {
354 hw_init();
355 }
357 static bool is_output()
358 {
359 return false;
360 }
361};
362
366template <class Defs>
367struct GpioInputPU : public GpioInputPin<Defs, GPIO_PIN_TYPE_STD_WPU>
368{
369};
370
374template <class Defs>
375struct GpioInputPD : public GpioInputPin<Defs, GPIO_PIN_TYPE_STD_WPD>
376{
377};
378
382template <class Defs>
383struct GpioInputNP : public GpioInputPin<Defs, GPIO_PIN_TYPE_STD>
384{
385};
386
388template <class Defs> struct GpioInputOutputPin : public GpioShared<Defs>
389{
390public:
391 using Defs::GPIO_PERIPH;
392 using Defs::GPIO_BASE;
393 using Defs::GPIO_PIN;
395 static void hw_init()
396 {
397 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
398 MAP_GPIOPinTypeGPIOInput(GPIO_BASE, GPIO_PIN);
399 MAP_GPIOPadConfigSet(GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_2MA,
400 GPIO_PIN_TYPE_STD);
401 }
403 static void hw_set_to_safe()
404 {
405 hw_init();
406 }
409 static void set_direction(Gpio::Direction direction)
410 {
411 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN,
412 direction == Gpio::Direction::DINPUT ?
413 GPIO_DIR_MODE_IN : GPIO_DIR_MODE_OUT);
414 }
416 static bool is_output()
417 {
418 return (MAP_GPIODirModeGet(GPIO_BASE, GPIO_PIN) == GPIO_DIR_MODE_OUT);
419 }
420};
421
427template <class Defs> struct GpioADCPin : public Defs
428{
429 using Defs::GPIO_PERIPH;
430 using Defs::GPIO_BASE;
431 using Defs::GPIO_PIN;
433 static void hw_init()
434 {
435 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
436 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN, GPIO_DIR_MODE_HW);
437 MAP_GPIOPadConfigSet(
438 GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_ANALOG);
439 }
441 static void hw_set_to_safe()
442 {
445 hw_init();
446 }
447};
448
454template <class Defs> struct GpioUSBAPin : public Defs
455{
456 using Defs::GPIO_PERIPH;
457 using Defs::GPIO_BASE;
458 using Defs::GPIO_PIN;
460 static void hw_init()
461 {
462 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
463 MAP_GPIOPinTypeUSBAnalog(GPIO_BASE, GPIO_PIN);
464 }
466 static void hw_set_to_safe()
467 {
468 hw_init();
469 }
470};
471
479template <class Defs> struct GpioHwPin : public GpioShared<Defs>
480{
481 using Defs::GPIO_PERIPH;
482 using Defs::GPIO_BASE;
483 using Defs::GPIO_PIN;
484 using Defs::GPIO_CONFIG;
485 using Defs::GPIO_SetPinType;
486
488 static void hw_init()
489 {
490 MAP_SysCtlPeripheralEnable(GPIO_PERIPH);
491 MAP_GPIOPinConfigure(GPIO_CONFIG);
492 set_hw();
493 }
494
496 static void hw_set_to_safe()
497 {
501 hw_init();
502 }
503
505 static void set_hw()
506 {
507 Defs::GPIO_SetPinType();
508 }
509
512 static void set_output()
513 {
514 MAP_GPIOPinTypeGPIOOutput(GPIO_BASE, GPIO_PIN);
515 MAP_GPIOPadConfigSet(
516 GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
517 }
518
525 static void set_input(uint32_t drive_type = GPIO_PIN_TYPE_STD)
526 {
527 MAP_GPIOPinTypeGPIOInput(GPIO_BASE, GPIO_PIN);
528 MAP_GPIOPadConfigSet(
529 GPIO_BASE, GPIO_PIN, GPIO_STRENGTH_2MA, drive_type);
530 }
531};
532
552#define GPIO_HWPIN(NAME, BaseClass, PORT, NUM, CONFIG, TYPE) \
553 struct NAME##Defs \
554 { \
555 DECL_HWPIN(GPIO, PORT, NUM, CONFIG, TYPE); \
556 }; \
557 typedef BaseClass<NAME##Defs> NAME##_Pin
558
559#endif //_FREERTOS_DRIVERS_TI_TIVAGPIO_HXX_
#define GPIO_PIN(NAME, BaseClass, NUM)
Helper macro for defining GPIO pins with Arduino.
#define GPIO_PIN(NAME, BaseClass, NUM)
Helper macro for defining GPIO pins on Linux-based microcontrollers (like the Raspberry Pi or Bagle B...
#define GPIO_PIN(NAME, BaseClass, PORT, NUM)
Helper macro for defining GPIO pins on the Tiva microcontrollers.
Definition TivaGPIO.hxx:329
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
Value
Defines the options for GPIO level.
Definition Gpio.hxx:62
Direction
Defines the options for GPIO direction.
Definition Gpio.hxx:73
Generic GPIO class implementation.
Definition TivaGPIO.hxx:73
constexpr TivaGpio()
This constructor is constexpr which ensures that the object can be initialized in the data section.
Definition TivaGPIO.hxx:77
void set_direction(Direction dir) const OVERRIDE
Sets the GPIO direction.
Definition TivaGPIO.hxx:101
void clr() const OVERRIDE
Clears the GPIO output pin to low.
Definition TivaGPIO.hxx:91
constexpr volatile uint8_t * pin_address() const
Computes the memory address where the bit referring to this pin can be accessed.
Definition TivaGPIO.hxx:142
Value read() const OVERRIDE
Retrieves the current Value of a GPIO input pin.
Definition TivaGPIO.hxx:96
void write(Value new_state) const OVERRIDE
Writes a GPIO output pin (set or clear to a specific state).
Definition TivaGPIO.hxx:81
static const TivaGpio instance_
Static instance variable that can be used for libraries expectiong a generic Gpio pointer.
Definition TivaGPIO.hxx:134
Direction direction() const OVERRIDE
Gets the GPIO direction.
Definition TivaGPIO.hxx:113
void set() const OVERRIDE
Sets the GPIO output pin to high.
Definition TivaGPIO.hxx:86
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
GPIO Input pin in ADC configuration (analog).
Definition TivaGPIO.hxx:428
static void hw_init()
Implements hw_init functionality for this pin only.
Definition TivaGPIO.hxx:433
static void hw_set_to_safe()
Implements hw_set_to_safe functionality for this pin only.
Definition TivaGPIO.hxx:441
GPIO pin in a hardware configuration (via pinmux: UART, I2C, CAN, etc).
Definition TivaGPIO.hxx:480
static void set_input(uint32_t drive_type=GPIO_PIN_TYPE_STD)
Switches the GPIO pin to an input pin.
Definition TivaGPIO.hxx:525
static void set_output()
Switches the GPIO pin to an output pin.
Definition TivaGPIO.hxx:512
static void set_hw()
Switches the GPIO pin to the hardware peripheral.
Definition TivaGPIO.hxx:505
static void hw_init()
Implements hw_init functionality for this pin only.
Definition TivaGPIO.hxx:488
static void hw_set_to_safe()
Implements hw_set_to_safe functionality for this pin only.
Definition TivaGPIO.hxx:496
Defines a GPIO input pin.
Common class for GPIO input pins.
static bool is_output()
Definition TivaGPIO.hxx:416
static void set_direction(Gpio::Direction direction)
Sets the direction of the I/O pin.
Definition TivaGPIO.hxx:409
static void hw_init()
Initializes the hardware pin.
Definition TivaGPIO.hxx:395
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
Definition TivaGPIO.hxx:403
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()
Definition TivaGPIO.hxx:357
static void hw_init()
Initializes the hardware pin.
Definition TivaGPIO.hxx:345
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
Definition TivaGPIO.hxx:352
Defines a GPIO output pin with high-current drive and a defined initialization level.
Definition TivaGPIO.hxx:265
static void hw_init()
Initializes the hardware pin.
Definition TivaGPIO.hxx:271
Parametric GPIO output class.
static void hw_init()
Initializes the hardware pin.
Definition TivaGPIO.hxx:225
static void set(bool value)
Sets the output pinm.
static bool is_output()
Definition TivaGPIO.hxx:238
static void hw_set_to_safe()
Sets the hardware pin to a safe value.
Definition TivaGPIO.hxx:232
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.
Shared class that defines static functions used by both GpioInputPin, GpioOutputPin and GpioHwPin.
Definition TivaGPIO.hxx:155
static bool get()
Definition TivaGPIO.hxx:186
static void lock()
Used to lock special consideration pins such as JTAG or NMI pins.
Definition TivaGPIO.hxx:171
static constexpr uint32_t ptr_address()
Definition TivaGPIO.hxx:197
static void unlock()
Used to unlock special consideration pins such as JTAG or NMI pins.
Definition TivaGPIO.hxx:162
static constexpr const Gpio * instance()
Definition TivaGPIO.hxx:203
static void toggle()
Changes the value of an output pin.
Definition TivaGPIO.hxx:191
static void set(bool value)
Sets the output pin to a specified value;.
Definition TivaGPIO.hxx:181
GPIO Input pin for USB.
Definition TivaGPIO.hxx:455
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
Definition TivaGPIO.hxx:466
static void hw_init()
Initializes the hardware pin.
Definition TivaGPIO.hxx:460
Defines a GPIO output pin with high-current drive and low initialization level.
static void hw_init()
Initialized the hardware pin.
Definition TivaGPIO.hxx:304