Permalink
Browse files

split GPIO class out

  • Loading branch information...
1 parent 7d03b55 commit 72759d329bb8b853a5652f7d13cf9e084a944e29 @triffid committed Apr 17, 2012
Showing with 68 additions and 43 deletions.
  1. +54 −0 gpio.cpp
  2. +14 −43 gpio.hpp
View
@@ -0,0 +1,54 @@
+#include "gpio.hpp"
+
+#include "LPC17xx.h"
+#include "lpc17xx_pinsel.h"
+#include "lpc17xx_gpio.h"
+
+GPIO::GPIO(uint8_t prt, uint8_t pn) {
+ port = prt;
+ pin = pn;
+ io = 0;
+ value = 0;
+
+ PINSEL_CFG_Type PinCfg;
+ PinCfg.Funcnum = 0;
+ PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
+ PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;
+ PinCfg.Portnum = prt;
+ PinCfg.Pinnum = pn;
+ PINSEL_ConfigPin(&PinCfg);
+}
+
+GPIO::~GPIO() {}
+
+void GPIO::set_direction(uint8_t direction) {
+ FIO_SetDir(port, pin, direction);
+ io = direction;
+}
+
+void GPIO::output() {
+ set_direction(1);
+}
+
+void GPIO::input() {
+ set_direction(0);
+}
+
+void GPIO::write(uint8_t value) {
+ if (value)
+ FIO_SetValue(port, 1UL << pin);
+ else
+ FIO_ClearValue(port, 1UL << pin);
+}
+
+void GPIO::set() {
+ FIO_SetValue(port, 1UL << pin);
+}
+
+void GPIO::clear() {
+ FIO_ClearValue(port, 1UL << pin);
+}
+
+uint8_t GPIO::get() {
+ return (FIO_ReadValue(port) & (1UL << pin))?255:0;
+}
View
@@ -1,6 +1,7 @@
-#include "LPC17xx.h"
-#include "lpc17xx_pinsel.h"
-#include "lpc17xx_gpio.h"
+#ifndef _GPIO_HPP
+#define _GPIO_HPP
+
+#include <stdint.h>
#define GPIO_DIR_INPUT 0
#define GPIO_DIR_OUTPUT 1
@@ -11,45 +12,15 @@ class GPIO {
uint8_t io;
uint8_t value;
public:
- GPIO(uint8_t prt, uint8_t pn) {
- port = prt;
- pin = pn;
- io = 0;
- value = 0;
-
- PINSEL_CFG_Type PinCfg;
- PinCfg.Funcnum = 0;
- PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
- PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;
- PinCfg.Portnum = prt;
- PinCfg.Pinnum = pn;
- PINSEL_ConfigPin(&PinCfg);
- };
- ~GPIO() {};
- void set_direction(uint8_t direction) {
- FIO_SetDir(port, pin, direction);
- io = direction;
- };
- void output() {
- set_direction(1);
- };
- void input() {
- set_direction(0);
- }
- void write(uint8_t value) {
- if (value)
- FIO_SetValue(port, 1UL << pin);
- else
- FIO_ClearValue(port, 1UL << pin);
- };
- void set() {
- FIO_SetValue(port, 1UL << pin);
- };
- void clear() {
- FIO_ClearValue(port, 1UL << pin);
- };
- uint8_t get() {
- return (FIO_ReadValue(port) & (1UL << pin))?255:0;
- };
+ GPIO(uint8_t prt, uint8_t pn);
+ ~GPIO();
+ void set_direction(uint8_t direction);
+ void output();
+ void input();
+ void write(uint8_t value);
+ void set();
+ void clear();
+ uint8_t get();
};
+#endif /* _GPIO_HPP */

0 comments on commit 72759d3

Please sign in to comment.