Skip to content

Instantly share code, notes, and snippets.

@roboter
Created August 20, 2024 17:00
Show Gist options
  • Save roboter/6bf6317e53127b0721fef3f91bd7902a to your computer and use it in GitHub Desktop.
Save roboter/6bf6317e53127b0721fef3f91bd7902a to your computer and use it in GitHub Desktop.
void I2C_Scan(void) {
printf("Scanning I2C bus:\r\n");
HAL_StatusTypeDef result;
uint8_t i;
for (i = 1; i < 128; i++) {
/*
* the HAL wants a left aligned i2c address
* &hi2c1 is the handle
* (uint16_t)(i<<1) is the i2c address left aligned
* retries 2
* timeout 2
*/
result = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t) (i << 1), 2, 2);
if (result != HAL_OK) // HAL_ERROR or HAL_BUSY or HAL_TIMEOUT
{
printf("."); // No ACK received at that address
}
if (result == HAL_OK) {
printf("0x%X", i); // Received an ACK at that address
sprintf(test, "0x%X", i);
HAL_UART_Transmit(&huart2, (uint8_t*) test, strlen(test), 100);
}
}
printf("\r\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment