Skip to content

Instantly share code, notes, and snippets.

@technobly
Created January 23, 2014 06:29
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 technobly/8573877 to your computer and use it in GitHub Desktop.
Save technobly/8573877 to your computer and use it in GitHub Desktop.
FAST READ EXAMPLE for SPARK CORE
//-----------------------------------------------
// Fast Read Example
//===============================================
// Copy this into a new application at:
// https://www.spark.io/build and go nuts!
//-----------------------------------------------
// Technobly / BDub - Jan 22nd 2014
//===============================================
#pragma GCC diagnostic ignored "-Wunused-value"
uint8_t digital_input = 0;
void setup() {
pinMode(D7,OUTPUT);
pinMode(D0,OUTPUT);
pinMode(D1,INPUT_PULLUP);
}
void loop() {
// Bit-Bang Digital Output D0 at 18MHz = System Clock 72MHz / 4
// HIGH time = 27.7ns
// LOW time = 27.7ns
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
// Fast Read Digital Input D1
// Read time = 27.7ns * 2 = 55.5ns (read and mask)
PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 ONLY, NO SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 ONLY, NO SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 ONLY, NO SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 ONLY, NO SAVE
// Fast Read Digital Input D1
// Read time = 27.7ns * 2 = 55.5ns (read and mask)
// Save time = 27.7ns (save)
// Total read time and save = 83.3ns
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
digital_input = (uint8_t)PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 AND SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
digital_input = (uint8_t)PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 AND SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
digital_input = (uint8_t)PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 AND SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
digital_input = (uint8_t)PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin; // READ D1 AND SAVE
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
//Turn on D7 LED based on state of D1 (200ns)
if(PIN_MAP[D1].gpio_peripheral->IDR & PIN_MAP[D1].gpio_pin) // READ D1 AND MASK
PIN_MAP[D7].gpio_peripheral->BSRR = PIN_MAP[D7].gpio_pin; // IF D1 IS HIGH, TURN ON LED
else
PIN_MAP[D7].gpio_peripheral->BRR = PIN_MAP[D7].gpio_pin; // IF D1 IS LOW, TURN OFF LOW
PIN_MAP[D0].gpio_peripheral->BSRR = PIN_MAP[D0].gpio_pin; // HIGH
PIN_MAP[D0].gpio_peripheral->BRR = PIN_MAP[D0].gpio_pin; // LOW
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment