diff --git a/Inc/DS_I2CScanner.h b/Inc/DS_I2CScanner.h new file mode 100644 index 0000000..ebd55b1 --- /dev/null +++ b/Inc/DS_I2CScanner.h @@ -0,0 +1,16 @@ +#ifndef DS_I2CSCANNER_H +#define DS_I2CSCANNER_H + +#include "main.h" + +#ifdef STM32G0 +#include "stm32g0xx_hal.h" +#endif + +#ifdef STM32F0 +#include "stm32f0xx_hal.h" +#endif + +void DS_I2CScan(I2C_HandleTypeDef *I2C, uint8_t *Result, uint8_t maxNumResults); + +#endif \ No newline at end of file diff --git a/Src/DS_I2CScanner.c b/Src/DS_I2CScanner.c new file mode 100644 index 0000000..e80781a --- /dev/null +++ b/Src/DS_I2CScanner.c @@ -0,0 +1,25 @@ +#include "DS_I2CScanner.h" + +void DS_I2CScan(I2C_HandleTypeDef *I2C, uint8_t *Result, uint8_t maxNumResults) +{ + HAL_StatusTypeDef HAL_result = HAL_ERROR; + uint8_t NumOfResult = 0; + + for (uint8_t i = 0; i < maxNumResults - 1; i++) + { + Result[i] = 0; + } + + for (uint8_t addr = 0; addr < 0xFF; ++addr) + { + HAL_result = HAL_I2C_IsDeviceReady(I2C, addr, 3, 1000); + if (HAL_result == HAL_OK) + { + Result[NumOfResult] = addr; + if (NumOfResult < maxNumResults - 1) + { + NumOfResult++; + } + } + } +} \ No newline at end of file