Skip to content

Instantly share code, notes, and snippets.

@visusnet
Created January 26, 2021 10:01
Show Gist options
  • Save visusnet/f90a60b668c39db9cf1c31a5ba1358e0 to your computer and use it in GitHub Desktop.
Save visusnet/f90a60b668c39db9cf1c31a5ba1358e0 to your computer and use it in GitHub Desktop.
seeed Azure Sphere MT3620 + Grove Shield I2C Scanner
#include "../../MT3620_Grove_Shield_Library/HAL/GroveI2C.h" // for SC18IM700_ReadReg
#include "../../MT3620_Grove_Shield_Library/HAL/GroveUART.h" // for GroveUART_Write
#include <stdlib.h> // for NULL
#include <time.h> // for nanosleep + timespec
#include <applibs/log.h> // for Log_Debug
// SC18IM700 data sheet:
// https://www.nxp.com/docs/en/data-sheet/SC18IM700.pdf
void i2cScanner(int i2cFd) {
uint8_t i2cState;
const struct timespec sleepTime = {0, 10000000};
for (uint8_t addr = 1; addr < 127; addr++) {
uint8_t send[4];
// See "Write N bytes to slave device" in SC18IM700 data sheet
send[0] = 'S'; // start byte
send[1] = (uint8_t)((addr << 1) & 0xfe);
send[2] = (uint8_t)0; // no data, i.e. size = 0
send[3] = 'P'; // end byte
Log_Debug("0x%02X ", addr);
GroveUART_Write(i2cFd, send, (int)sizeof(send));
Log_Debug("W");
SC18IM700_ReadReg(i2cFd, 0x0A /* SC18IM700 I2C status register */, &i2cState);
Log_Debug("R");
if (i2cState == I2C_OK) {
Log_Debug(" = I2C_OK\r\n");
} else if (i2cState == I2C_TIME_OUT) {
Log_Debug(" = I2C_TIME_OUT\r\n");
} else if (i2cState == I2C_NACK_ON_ADDRESS) {
Log_Debug(" = I2C_NACK_ON_ADDRESS\r\n");
} else if (i2cState == I2C_NACK_ON_DATA) {
Log_Debug(" = I2C_NACK_ON_DATA\r\n");
}
nanosleep(&sleepTime, NULL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment