Skip to content

Instantly share code, notes, and snippets.

@triffid
Created January 31, 2014 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triffid/8724763 to your computer and use it in GitHub Desktop.
Save triffid/8724763 to your computer and use it in GitHub Desktop.
smoothie: bitbanded gpio
diff --git a/src/libs/Pin.cpp b/src/libs/Pin.cpp
index bc234d2..3190935 100644
--- a/src/libs/Pin.cpp
+++ b/src/libs/Pin.cpp
@@ -4,6 +4,7 @@
Pin::Pin(){
this->inverting= false;
+ bitband = NULL;
}
// Make a new pin object from a string
@@ -33,6 +34,8 @@ Pin* Pin::from_string(std::string value){
if ((cn > cs) && (pin < 32)){
this->port->FIOMASK &= ~(1 << this->pin);
+ bitband = (uint32_t*) (0x22000000 + ((((uintptr_t) &(port->FIOPIN)) - 0x20000000) * 32 + pin * 4));
+
// now check for modifiers:-
// ! = invert pin
// o = set pin to open drain
@@ -76,6 +79,7 @@ Pin* Pin::from_string(std::string value){
port = gpios[0];
pin = 255;
inverting = false;
+ bitband = NULL;
return this;
}
diff --git a/src/libs/Pin.h b/src/libs/Pin.h
index d64592f..89e76a6 100644
--- a/src/libs/Pin.h
+++ b/src/libs/Pin.h
@@ -47,17 +47,17 @@ class Pin {
inline void set(bool value)
{
- if (this->pin >= 32) return;
- if ( this->inverting ^ value )
- this->port->FIOSET = 1 << this->pin;
- else
- this->port->FIOCLR = 1 << this->pin;
+ if (bitband == NULL)
+ return;
+ *bitband = inverting ^ value;
}
LPC_GPIO_TypeDef* port;
bool inverting;
char port_number;
unsigned char pin;
+
+ uint32_t* bitband;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment