Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gpio.cpp Source File

gpio.cpp

00001 #include "gpio.h"
00002 
00003 #include "LPC17xx.h"
00004 #include "lpc17xx_pinsel.h"
00005 #include "lpc17xx_gpio.h"
00006 
00007 GPIO::GPIO(PinName pin) {
00008     this->port = (pin >> 5) & 7;
00009     this->pin = pin & 0x1F;
00010 
00011     setup();
00012 }
00013 
00014 GPIO::GPIO(uint8_t port, uint8_t pin) {
00015     GPIO::port = port;
00016     GPIO::pin = pin;
00017 
00018     setup();
00019 }
00020 
00021 GPIO::GPIO(uint8_t port, uint8_t pin, uint8_t direction) {
00022     GPIO::port = port;
00023     GPIO::pin = pin;
00024 
00025     setup();
00026 
00027     set_direction(direction);
00028 }
00029 // GPIO::~GPIO() {}
00030 
00031 void GPIO::setup() {
00032     PINSEL_CFG_Type PinCfg;
00033     PinCfg.Funcnum = 0;
00034     PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
00035     PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;
00036     PinCfg.Portnum = GPIO::port;
00037     PinCfg.Pinnum = GPIO::pin;
00038     PINSEL_ConfigPin(&PinCfg);
00039 }
00040 
00041 void GPIO::set_direction(uint8_t direction) {
00042     FIO_SetDir(port, 1UL << pin, direction);
00043 }
00044 
00045 void GPIO::output() {
00046     set_direction(1);
00047 }
00048 
00049 void GPIO::input() {
00050     set_direction(0);
00051 }
00052 
00053 void GPIO::write(uint8_t value) {
00054     output();
00055     if (value)
00056         set();
00057     else
00058         clear();
00059 }
00060 
00061 void GPIO::set() {
00062     FIO_SetValue(port, 1UL << pin);
00063 }
00064 
00065 void GPIO::clear() {
00066     FIO_ClearValue(port, 1UL << pin);
00067 }
00068 
00069 uint8_t GPIO::get() {
00070     return (FIO_ReadValue(port) & (1UL << pin))?255:0;
00071 }
00072 
00073 int GPIO::operator=(int value) {
00074     if (value)
00075         set();
00076     else
00077         clear();
00078     return value;
00079 }