From 97862f34a6ba15b0332233d534f5eee0a0a8b769 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Mon, 5 Jul 2021 15:56:14 +0200 Subject: [PATCH] I2c tx/rx blocking --- Core/Src/main.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 084b369..2bda898 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -35,17 +35,18 @@ int main(void) MX_USART2_UART_Init(); MX_I2C1_Init(); - printf("\r\nStarting I2C Scanning:\r\n"); - for(uint8_t i = 1; i < 128; i++) { - uint8_t ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 3, 5); - if (ret != HAL_OK) { /* No ACK Received At That Address */ - printf(" - "); - } else if(ret == HAL_OK) { - printf("0x%X", i); + uint8_t data[2] = {0}; + data[0] = 1; // Config register + if(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)(0x48 << 1), data,1, 1000) != HAL_OK) { + printf("I2c tx failed.\r\n"); + } else { + if(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)(0x48 << 1), data, 2, 1000) != HAL_OK) { + printf("I2c rx failed.\r\n"); + } else { + uint16_t config = (uint16_t)(data[0] << 8) | data[1]; + printf("ADS1115 config register: 0x%04x\r\n", config); } } - printf("\r\nDone! \r\n\r\n"); - while(1) {}; }