VL6180x at low SPI clock speeds.
I am using a VL6180x with an Atmega8a in a lower-power application. The Atmega8a runs at a clock frequency of 1 MHz. After struggling for a few weeks to get the sensor working, I discovered an issue with the VL6180x. After writing 0x01 to register 0x038, to initiate a single-shot measurement, the value of register 0x04F remained 0 forever.
I then switched from the Atmega8a to an ESP32 and the problem was solved. I then discovered that the problem reappeared when I set the SPI clock speed on the ESP32 to less than 20kHz.
Is this a known issue with the VL6180x?
I then wrote the following inline assembler code that does bit-banging. The VL6180x now works fine with the Atmega8a.
void startSingleShot(){
// SDA = PC4
// SCL = PC5
// Port C address = 0x15
__asm__ __volatile__(
// Set pin PC4 as output
"SBI 0x14, 4 \n\t"
// Send start
"CBI 0x15, 4 \n\t" // Take SDA low to indicate start
"CBI 0x15, 5 \n\t" // Take SCL low
// Send address
"CBI 0x15, 4 \n\t" // bit 7 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t" // bit 6 is high
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 5 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t" // bit 4 is high
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 3 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 2 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t" // bit 1 is high
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 0 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as input
"CBI 0x14, 4 \n\t"
// Clock for acknowledge
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as output
"SBI 0x14, 4 \n\t"
// Send first index: 0x00
"CBI 0x15, 4 \n\t" // bit 7 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 6 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 5 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 4 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 3 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 2 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 1 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 0 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as input
"CBI 0x14, 4 \n\t"
// Clock for acknowledge
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as output
"SBI 0x14, 4 \n\t"
// Send second index: 0x18
"CBI 0x15, 4 \n\t" // bit 7 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 6 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 5 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t" // bit 4 is high
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t" // bit 3 is high
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 2 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 1 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 0 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as input
"CBI 0x14, 4 \n\t"
// Clock for acknowledge
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as output
"SBI 0x14, 4 \n\t"
// Set value to 0x01
// Set pin PC4 as output
"SBI 0x14, 4 \n\t"
// Send data: 0x01
"CBI 0x15, 4 \n\t" // bit 7 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 6 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 5 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 4 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 3 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 2 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"CBI 0x15, 4 \n\t" // bit 1 is low
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t" // bit 0 is high
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as input
"CBI 0x14, 4 \n\t"
// Clock for acknowledge
"SBI 0x15, 5 \n\t"
"CBI 0x15, 5 \n\t"
// Set pin PC4 as output
"SBI 0x14, 4 \n\t"
// Send stop
"CBI 0x15, 4 \n\t"
"SBI 0x15, 5 \n\t"
"SBI 0x15, 4 \n\t"
// Set pin PC4 as input
"CBI 0x14, 4 \n\t"
);
}