Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CC3200GPIO.hxx
Go to the documentation of this file.
1
48#ifndef _FREERTOS_DRIVERS_TI_CC3200GPIO_HXX_
49#define _FREERTOS_DRIVERS_TI_CC3200GPIO_HXX_
50
51#include "os/Gpio.hxx"
52#include "inc/hw_types.h"
53#include "inc/hw_ints.h"
54#include "driverlib/gpio.h"
55#include "driverlib/prcm.h"
56#include "inc/hw_memmap.h"
57#include "driverlib/rom_map.h"
58
59
63#define DECL_PIN(NAME, PORT, NUM) \
64 static const auto NAME##_PERIPH = PRCM_GPIO##PORT; \
65 static const auto NAME##_BASE = GPIO##PORT##_BASE; \
66 static const auto NAME##_PIN = GPIO_PIN_##NUM; \
67 static const auto NAME##_INT = INT_GPIO##PORT
68
69
70template <class Defs, bool SAFE_VALUE> struct GpioOutputPin;
71template <class Defs> struct GpioInputPin;
72
74template <unsigned GPIO_BASE, unsigned GPIO_PIN> class CC3200Gpio : public Gpio
75{
76public:
79 constexpr CC3200Gpio()
80 {
81 }
82
83 void write(Value new_state) const OVERRIDE
84 {
85 *pin_address() = (new_state ? 0xff : 0);
87 __asm__ volatile("dsb" : : : "memory");
88 }
89
90 void set() const OVERRIDE
91 {
92 *pin_address() = 0xff;
94 __asm__ volatile("dsb" : : : "memory");
95 }
96
97 void clr() const OVERRIDE
98 {
99 *pin_address() = 0;
101 __asm__ volatile("dsb" : : : "memory");
102 }
103
105 {
106 return *pin_address() ? VHIGH : VLOW;
107 }
108
110 {
111 if (dir == Direction::DOUTPUT)
112 {
113 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN, GPIO_DIR_MODE_OUT);
114 }
115 else
116 {
117 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN, GPIO_DIR_MODE_IN);
118 }
119 }
120
122 {
123 uint32_t mode = GPIODirModeGet(GPIO_BASE, GPIO_PIN);
124 switch (mode)
125 {
126 default:
127 HASSERT(0);
128 case GPIO_DIR_MODE_IN:
129 return Direction::DINPUT;
130 case GPIO_DIR_MODE_OUT:
131 return Direction::DOUTPUT;
132 }
133 }
134
135private:
136 template <class Defs, bool SAFE_VALUE> friend struct GpioOutputPin;
137 template <class Defs> friend struct GpioInputPin;
143 static const CC3200Gpio instance_;
144
150 constexpr volatile uint8_t *pin_address() const
151 {
152 return reinterpret_cast<volatile uint8_t *>(
153 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
154 }
155};
156
158template <unsigned GPIO_BASE, unsigned GPIO_PIN>
160
168template <class Defs, bool SAFE_VALUE> struct GpioOutputPin : public Defs
169{
170public:
171 using Defs::GPIO_PERIPH;
172 using Defs::GPIO_BASE;
173 using Defs::GPIO_PIN;
175 static void hw_init()
176 {
177 MAP_PRCMPeripheralClkEnable(GPIO_PERIPH, PRCM_RUN_MODE_CLK);
178 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN, GPIO_DIR_MODE_OUT);
179 set(SAFE_VALUE);
180 }
182 static void hw_set_to_safe()
183 {
184 hw_init();
185 }
188 static void __attribute__((always_inline)) set(bool value)
189 {
190 volatile uint8_t *ptr = reinterpret_cast<uint8_t *>(
191 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
192 *ptr = value ? 0xff : 0;
194 __asm__ volatile("dsb" : : : "memory");
195 }
197 static bool __attribute__((always_inline)) get()
198 {
199 const volatile uint8_t *ptr = reinterpret_cast<const uint8_t *>(
200 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
201 return *ptr;
202 }
204 static void __attribute__((always_inline)) toggle()
205 {
206 set(!get());
207 }
208
210 static constexpr const Gpio *instance()
211 {
213 }
214
216 static bool is_output()
217 {
218 return true;
219 }
220};
221
225template <class Defs>
226struct GpioOutputSafeLow : public GpioOutputPin<Defs, false>
227{
228};
229
233template <class Defs>
234struct GpioOutputSafeHigh : public GpioOutputPin<Defs, true>
235{
236};
237
255#define GPIO_PIN(NAME, BaseClass, PORT, NUM) \
256 struct NAME##Defs \
257 { \
258 DECL_PIN(GPIO, PORT, NUM); \
259 }; \
260 typedef BaseClass<NAME##Defs> NAME##_Pin
261
263template <class Defs> struct GpioInputPin : public Defs
264{
265public:
266 using Defs::GPIO_PERIPH;
267 using Defs::GPIO_BASE;
268 using Defs::GPIO_PIN;
270 static void hw_init()
271 {
272 MAP_PRCMPeripheralClkEnable(GPIO_PERIPH, PRCM_RUN_MODE_CLK);
273 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN, GPIO_DIR_MODE_IN);
274 }
276 static void hw_set_to_safe()
277 {
278 hw_init();
279 }
281 static bool get()
282 {
283 const volatile uint8_t *ptr = reinterpret_cast<const uint8_t *>(
284 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
285 return *ptr;
286 }
288 static bool is_output()
289 {
290 return false;
291 }
293 static constexpr const Gpio *instance()
294 {
296 }
297};
298
300template <class Defs> struct GpioInputOutputPin : public Defs
301{
302public:
303 using Defs::GPIO_PERIPH;
304 using Defs::GPIO_BASE;
305 using Defs::GPIO_PIN;
307 static void hw_init()
308 {
309 MAP_PRCMPeripheralClkEnable(GPIO_PERIPH, PRCM_RUN_MODE_CLK);
310 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN, GPIO_DIR_MODE_IN);
311 }
313 static void hw_set_to_safe()
314 {
315 hw_init();
316 }
319 static void __attribute__((always_inline)) set(bool value)
320 {
321 volatile uint8_t *ptr = reinterpret_cast<uint8_t *>(
322 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
323 *ptr = value ? 0xff : 0;
325 __asm__ volatile("dsb" : : : "memory");
326 }
328 static bool __attribute__((always_inline)) get()
329 {
330 const volatile uint8_t *ptr = reinterpret_cast<const uint8_t *>(
331 GPIO_BASE + (((unsigned)GPIO_PIN) << 2));
332 return *ptr;
333 }
335 static void __attribute__((always_inline)) toggle()
336 {
337 set(!get());
338 }
339
341 static constexpr const Gpio *instance()
342 {
344 }
345
348 static void set_direction(Gpio::Direction direction)
349 {
350 MAP_GPIODirModeSet(GPIO_BASE, GPIO_PIN,
351 direction == Gpio::Direction::DINPUT ?
352 GPIO_DIR_MODE_IN : GPIO_DIR_MODE_OUT);
353 }
354
356 static bool is_output()
357 {
358 return (MAP_GPIODirModeGet(GPIO_BASE, GPIO_PIN) == GPIO_DIR_MODE_OUT);
359 }
360};
361
362#endif //_FREERTOS_DRIVERS_TI_CC3200GPIO_HXX_
#define GPIO_PIN(NAME, BaseClass, PORT, NUM)
Helper macro for defining GPIO pins on the CC3200 microcontrollers.
#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
Generic GPIO class implementation.
Direction direction() const OVERRIDE
Gets the GPIO direction.
static const CC3200Gpio instance_
Static instance variable that can be used for libraries expectiong a generic Gpio pointer.
void write(Value new_state) const OVERRIDE
Writes a GPIO output pin (set or clear to a specific state).
void clr() const OVERRIDE
Clears the GPIO output pin to low.
void set() const OVERRIDE
Sets the GPIO output pin to high.
constexpr volatile uint8_t * pin_address() const
Computes the memory address where the bit referring to this pin can be accessed.
Value read() const OVERRIDE
Retrieves the current Value of a GPIO input pin.
void set_direction(Direction dir) const OVERRIDE
Sets the GPIO direction.
constexpr CC3200Gpio()
This constructor is constexpr which ensures that the object can be initialized in the data section.
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
#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
Common class for GPIO input pins.
static bool is_output()
static void set_direction(Gpio::Direction direction)
Sets the direction of the I/O pin.
static constexpr const Gpio * instance()
static void set(bool value)
Sets the output pin to a defined value.
static void toggle()
Changes the value of an output pin.
static void hw_init()
Initializes the hardware pin.
static bool get()
static void hw_set_to_safe()
Sets the output pin to a safe value.
Parametric GPIO input class.
static bool is_output()
static bool get()
static void hw_init()
Initializes the hardware pin.
static void hw_set_to_safe()
Sets the hardware pin to a safe state.
static constexpr const Gpio * instance()
Parametric GPIO output class.
static constexpr const Gpio * instance()
static void toggle()
Changes the value of an output pin.
static bool get()
static void hw_init()
Initializes the hardware pin.
static void set(bool value)
Sets the output pinm.
static bool is_output()
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.