1 /*
2  * Copyright (c) 2023 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/kernel.h>
11 
12 
13 #if DT_NODE_HAS_STATUS(DT_ALIAS(i2c_0), okay)
14 #define I2C_DEV_NODE	DT_ALIAS(i2c_0)
15 #else
16 #error "Please set the correct I2C device"
17 #endif
18 
main(void)19 int main(void)
20 {
21 	const struct device *const i2c_dev = DEVICE_DT_GET(I2C_DEV_NODE);
22 	uint32_t i2c_cfg;
23 
24 	if (!device_is_ready(i2c_dev)) {
25 		printk("I2C device is not ready\n");
26 		return -1;
27 	}
28 
29 	/* Verify i2c_get_config() */
30 	if (i2c_get_config(i2c_dev, (uint32_t *)&i2c_cfg)) {
31 		printk("I2C get_config failed\n");
32 		return -1;
33 	}
34 
35 	/* I2C BIT RATE */
36 	printk("\nI2C freq.");
37 
38 	if ((I2C_SPEED_GET(i2c_cfg) == 1) &&
39 		(DT_PROP(I2C_DEV_NODE, clock_frequency) == 100000)) {
40 		printk(" I2C_BITRATE_STANDARD\n");
41 	} else if ((I2C_SPEED_GET(i2c_cfg) == 2) &&
42 		(DT_PROP(I2C_DEV_NODE, clock_frequency) == 400000)) {
43 		printk(" I2C_BITRATE_FAST\n");
44 	} else if ((I2C_SPEED_GET(i2c_cfg) == 3) &&
45 		(DT_PROP(I2C_DEV_NODE, clock_frequency) == 1000000)) {
46 		printk(" I2C_SPEED_FAST_PLUS\n");
47 	} else {
48 		printk(" I2C speed mismatch (%d)\n", I2C_SPEED_GET(i2c_cfg));
49 	}
50 
51 	return 0;
52 }
53