Example of i2c scanner for RHOMBIO_L476DMW1K

This example is known to work well on the following platforms: RHOMBIO_L476DMW1K

The example code scans any I2C peripheral attached to the kit, whether it is any rhomb.io slave module attached to Deimos carrier board or any external peripheral wired to SDA, SCL and GND pins available on Deimos pin header H2.

The code also prints out the result, showing the address of any device found on the I2C bus.

This example code should give you an output similar to this:


Rhomb.io example code - I2C Scanner

Searching for I2C devices...

I2C device found at address 0x60 (0xC0 in 8-bit)

1 device. found


PS: RHOMBIO_L476DMW1K IoT kit comes with a ATECC608A crypto authentication memory on board, which is connected to the I2C bus. Its I2C slave address is 0x60, that's the device found by the example code when no other I2C device has been connected to the kit.

Committer:
galonso@rhomb.io
Date:
Tue Sep 10 15:17:47 2019 +0200
Revision:
1:9281da0f55a2
Parent:
0:dc5bc0efc11d
added MIT license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
galonso@rhomb.io 1:9281da0f55a2 1 /* i2c-scanner-rhombio-l476dmw1k
galonso@rhomb.io 1:9281da0f55a2 2 *
galonso@rhomb.io 1:9281da0f55a2 3 * Copyright (c) 2019 rhomb.io
galonso@rhomb.io 1:9281da0f55a2 4 *
galonso@rhomb.io 1:9281da0f55a2 5 * MIT License
galonso@rhomb.io 1:9281da0f55a2 6 *
galonso@rhomb.io 1:9281da0f55a2 7 * Permission is hereby granted, free of charge, to any person
galonso@rhomb.io 1:9281da0f55a2 8 * obtaining a copy of this software and associated documentation
galonso@rhomb.io 1:9281da0f55a2 9 * files (the "Software"), to deal in the Software without
galonso@rhomb.io 1:9281da0f55a2 10 * restriction, including without limitation the rights to use,
galonso@rhomb.io 1:9281da0f55a2 11 * copy, modify, merge, publish, distribute, sublicense, and/or sell
galonso@rhomb.io 1:9281da0f55a2 12 * copies of the Software, and to permit persons to whom the
galonso@rhomb.io 1:9281da0f55a2 13 * Software is furnished to do so, subject to the following
galonso@rhomb.io 1:9281da0f55a2 14 * conditions:
galonso@rhomb.io 1:9281da0f55a2 15 *
galonso@rhomb.io 1:9281da0f55a2 16 * The above copyright notice and this permission notice shall be
galonso@rhomb.io 1:9281da0f55a2 17 * included in all copies or substantial portions of the Software.
galonso@rhomb.io 1:9281da0f55a2 18 *
galonso@rhomb.io 1:9281da0f55a2 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
galonso@rhomb.io 1:9281da0f55a2 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
galonso@rhomb.io 1:9281da0f55a2 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
galonso@rhomb.io 1:9281da0f55a2 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
galonso@rhomb.io 1:9281da0f55a2 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
galonso@rhomb.io 1:9281da0f55a2 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
galonso@rhomb.io 1:9281da0f55a2 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
galonso@rhomb.io 1:9281da0f55a2 26 * OTHER DEALINGS IN THE SOFTWARE.
galonso@rhomb.io 1:9281da0f55a2 27 */
galonso@rhomb.io 1:9281da0f55a2 28
galonso@rhomb.io 0:dc5bc0efc11d 29 #include "mbed.h"
galonso@rhomb.io 0:dc5bc0efc11d 30
galonso@rhomb.io 0:dc5bc0efc11d 31
galonso@rhomb.io 0:dc5bc0efc11d 32 I2C i2c(I2C_SDA, I2C_SCL);
galonso@rhomb.io 0:dc5bc0efc11d 33 DigitalOut led(LED1);
galonso@rhomb.io 0:dc5bc0efc11d 34
galonso@rhomb.io 0:dc5bc0efc11d 35
galonso@rhomb.io 0:dc5bc0efc11d 36 int main()
galonso@rhomb.io 0:dc5bc0efc11d 37 {
galonso@rhomb.io 0:dc5bc0efc11d 38 led = 1;
galonso@rhomb.io 0:dc5bc0efc11d 39
galonso@rhomb.io 0:dc5bc0efc11d 40 i2c.frequency(100000);
galonso@rhomb.io 0:dc5bc0efc11d 41 printf("Rhomb.io example code - I2C Scanner\n\r");
galonso@rhomb.io 0:dc5bc0efc11d 42 printf("-----------------------------------\n\r");
galonso@rhomb.io 0:dc5bc0efc11d 43
galonso@rhomb.io 0:dc5bc0efc11d 44 while (1) {
galonso@rhomb.io 0:dc5bc0efc11d 45 printf("Searching for I2C devices...\n\r");
galonso@rhomb.io 0:dc5bc0efc11d 46
galonso@rhomb.io 0:dc5bc0efc11d 47 int count = 0;
galonso@rhomb.io 0:dc5bc0efc11d 48 for (int address = 0; address < 255; address +=2) { // check only for device's read addres
galonso@rhomb.io 0:dc5bc0efc11d 49 if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
galonso@rhomb.io 0:dc5bc0efc11d 50 printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n\r", address >> 1, address); // the address is without LSB, which is R/W flag. shoft it right once
galonso@rhomb.io 0:dc5bc0efc11d 51 count++;
galonso@rhomb.io 0:dc5bc0efc11d 52 led = 1;
galonso@rhomb.io 0:dc5bc0efc11d 53 }
galonso@rhomb.io 0:dc5bc0efc11d 54 wait(0.02);
galonso@rhomb.io 0:dc5bc0efc11d 55 }
galonso@rhomb.io 0:dc5bc0efc11d 56 if (count)
galonso@rhomb.io 0:dc5bc0efc11d 57 printf("%d", count);
galonso@rhomb.io 0:dc5bc0efc11d 58 else
galonso@rhomb.io 0:dc5bc0efc11d 59 printf("No");
galonso@rhomb.io 0:dc5bc0efc11d 60 printf(" device%c found\n\r\n", count == 1?'\0':'s');
galonso@rhomb.io 0:dc5bc0efc11d 61 led = 0;
galonso@rhomb.io 0:dc5bc0efc11d 62 wait (1.8);
galonso@rhomb.io 0:dc5bc0efc11d 63 }
galonso@rhomb.io 0:dc5bc0efc11d 64 }