Bit-Bang
Bit-banging is a process where one emulates an unavailable peripheral using direct port manipulation commands on GPIO pins.
Uses
Code example
// Definitions of Digital pin numbers on the Cortex to use for SPI
#define PIN_SPI_CS 9
#define PIN_SPI_MISO 10
#define PIN_SPI_MOSI 11
#define PIN_SPI_SCK 12
// Write an 8-bit value to SPI most significant bit (MSB) first and return
// the 8-bit response value from the slave
// This function uses clock phase 0, clock polarity 0. See the SPI article
// and hardware datasheet for the different CPHA and CPOL settings.
uint8_t spiWriteRead(uint8_t value) {
uint8_t j, out = 0x00;
// Assert CS to select device (active low)
digitalWrite(PIN_SPI_CS, 0);
for (uint32_t i = 0; i < 8; i++) {
// Set bit
if (value & 0x80)
digitalWrite(PIN_SPI_MOSI, 1);
else
digitalWrite(PIN_SPI_MOSI, 0);
// Move next bit into most significant position
value <<= 1;
// Assert the clock
digitalWrite(PIN_SPI_SCK, 1);
// Delay for a short while
// Due to CPU usage problems, tasks cannot yet delay for less than 1 ms
// As a 1 KHz SPI rate is unacceptably slow, mash this out in software
for (j = 0; j != 63; j++) asm("");
// Read the MISO line for slave data
if (digitalRead(PIN_SPI_MISO))
out |= 0x01;
out <<= 1;
// Negate the clock
digitalWrite(PIN_SPI_SCK, 0);
// Delay for a short while again
for (j = 0; j != 63; j++) asm("");
}
// Negate CS
digitalWrite(PIN_SPI_CS, 1);
return out;
}Teams Contributed to this Article:
Last updated
Was this helpful?
