I2C MUX 8 Click demo application is developed using the NECTO Studio, ensuring compatibility with mikroSDK's open-source libraries and tools. Designed for plug-and-play implementation and testing, the demo is fully compatible with all development, starter, and mikromedia boards featuring a mikroBUS™ socket.
- Author : Stefan Filipovic
- Date : Feb 2025.
- Type : I2C type
This example demonstrates the use of I2C MUX 8 Click board by reading the device ID of a 6DOF IMU 11 and Compass 3 Click boards connected to the channels 1 and 4 respectfully.
- MikroSDK.Board
- MikroSDK.Log
- Click.I2CMUX8
i2cmux8_cfg_setupThis function initializes Click configuration structure to initial values.
void i2cmux8_cfg_setup ( i2cmux8_cfg_t *cfg );i2cmux8_initThis function initializes all necessary pins and peripherals used for this Click board.
err_t i2cmux8_init ( i2cmux8_t *ctx, i2cmux8_cfg_t *cfg );i2cmux8_set_channelThis function sets the active channel and updates the slave address for communication.
err_t i2cmux8_set_channel ( i2cmux8_t *ctx, uint8_t ch_sel, uint8_t ch_slave_addr );i2cmux8_read_channelThis function reads the currently selected channel.
err_t i2cmux8_read_channel ( i2cmux8_t *ctx, uint8_t *ch_sel );i2cmux8_i2c_read_regThis function reads data from a specific register of the currently active I2C slave.
err_t i2cmux8_i2c_read_reg ( i2cmux8_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );Initializes the driver and resets the device.
void application_init ( void )
{
log_cfg_t log_cfg; /**< Logger config object. */
i2cmux8_cfg_t i2cmux8_cfg; /**< Click config object. */
/**
* Logger initialization.
* Default baud rate: 115200
* Default log level: LOG_LEVEL_DEBUG
* @note If USB_UART_RX and USB_UART_TX
* are defined as HAL_PIN_NC, you will
* need to define them manually for log to work.
* See @b LOG_MAP_USB_UART macro definition for detailed explanation.
*/
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, " Application Init " );
// Click initialization.
i2cmux8_cfg_setup( &i2cmux8_cfg );
I2CMUX8_MAP_MIKROBUS( i2cmux8_cfg, MIKROBUS_POSITION_I2CMUX8 );
if ( I2C_MASTER_ERROR == i2cmux8_init( &i2cmux8, &i2cmux8_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
i2cmux8_reset_device ( &i2cmux8 );
log_info( &logger, " Application Task " );
}Reads the device ID of the connected Click boards. Channel 1 : 6DOF IMU 11 Click [slave address: 0x0E; reg: 0x00; id: 0x2D], Channel 4 : Compass 3 Click [slave address: 0x30; reg: 0x2F; id: 0x0C]. All data is being logged on the USB UART where you can check the device ID.
void application_task ( void )
{
uint8_t channel = 0, device_id = 0;
if ( I2CMUX8_OK == i2cmux8_set_channel ( &i2cmux8, DEVICE0_POSITION, DEVICE0_SLAVE_ADDRESS ) )
{
if ( I2CMUX8_OK == i2cmux8_read_channel ( &i2cmux8, &channel ) )
{
log_printf( &logger, " --- Channel %u --- \r\n", ( uint16_t ) channel );
}
if ( I2CMUX8_OK == i2cmux8_i2c_read_reg ( &i2cmux8, DEVICE0_REG_ID, &device_id, 1 ) )
{
log_printf( &logger, " %s - Device ID: 0x%.2X \r\n\n", ( char * ) DEVICE0_NAME, ( uint16_t ) device_id );
}
Delay_ms ( 1000 );
}
if ( I2CMUX8_OK == i2cmux8_set_channel ( &i2cmux8, DEVICE1_POSITION, DEVICE1_SLAVE_ADDRESS ) )
{
if ( I2CMUX8_OK == i2cmux8_read_channel ( &i2cmux8, &channel ) )
{
log_printf( &logger, " --- Channel %u --- \r\n", ( uint16_t ) channel );
}
if ( I2CMUX8_OK == i2cmux8_i2c_read_reg ( &i2cmux8, DEVICE1_REG_ID, &device_id, 1 ) )
{
log_printf( &logger, " %s - Device ID: 0x%.2X \r\n\n", ( char * ) DEVICE1_NAME, ( uint16_t ) device_id );
}
Delay_ms ( 1000 );
}
}This Click board can be interfaced and monitored in two ways:
- Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.
- UART Terminal - Monitor data via the UART Terminal using a USB to UART converter. For detailed instructions, check out this tutorial.
The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.