All DLN-series adapters support I2C master interface. Some of them can have several independent I2C ports. To know the number of available I2C master ports, use the DlnI2cMasterGetPortCount() function.Before using the I2C bus for transmitting data, you need to configure the I2C master port and enable it (see Configuring the I2C Master Interface). To stop using the I2C master port, you can disable it by the DlnI2cMasterDisable() function.
To start using the I2C master port, you need to configure the I2C master interface:
Configure the I2C frequency. This parameter influences the speed of data transmission. For details, read I2C Speed and Frequency.
Configure the number of attempts to resend data if Not Acknowledgement is received. For details, read Reply Count.
Enable the I2C master port. If the pins of the I2C port are not used by other modules, you can enable the I2C master port by the DlnI2cMasterEnable() function.
I2C bus specification describes four operating speed categories for bidirectional data transmission:
Standard-mode (Sm) | a bit rate up to 100 kbit/s |
Fast-mode (Fm) | a bit rate up to 400 kbit/s |
Fast-mode Plus (Fm+) | a bit rate up to 1 Mbit/s |
High-speed mode (Hs) | a bit rate up to 3.4 Mbit/s |
One more speed category, Ultra-fast mode (UFm), stands for unidirectional data transmission up to 5 Mbit/s.
Configuring the I2C master interface, you can specify the frequency value by calling the DlnI2cMasterSetFrequency() function.
The range of supported frequency values depends on the DLN adapter:
DLN-1 and DLN-2 adapters support frequency from 1kHz up to 4MHz.
DLN-4 adapters support frequency from 1.47kHz up to 1MHz.
The quality of I2C lines, the values of pull-up resistors and the number of slaves connected to the I2C bus may influence the working frequency of the I2C bus. Besides, the frequency reflects the speed of a single byte transmission, but not the speed of transmitting all data. It is a fact that the time of data processing can exceed significantly the time of data transmission. That is why data transmitted at high speed can have no effect on the speed of the I2C bus if delays between bytes are longer than the bytes themselves.
The I2C transmission expects an Acknowledge bit after every byte. This bit is sent by a slave and by a receiver:
A slave sends an Acknowledge bit after the slave address and direction bit to signal that the device with the specified address is present on the I2C bus;
A receiver sends an Acknowledge bit after a data byte to signal that the byte was successfully received and another byte may be sent.
The Acknowledge signal is LOW on the SDA line that remains stable during the HIGH period of the ninth pulse on the SCL line. If the SDA line remains HIGH during this clock pulse, this is defined as Not Acknowledge signal. In this case, the master has the following options:
Generate a STOP (P) condition to abort the transmission;
Generate a repeated START (Sr) condition to start a new transmission.
DLN-1 and DLN-2 adapters provide one more option for the I2C master:
Generate a STOP (P) condition followed by a START (S) condition to start the same transmission from the very beginning.
This option allows to repeat transmission if acknowledgement was not received. By default, transmissions can repeat 10 times. If all these times acknowledgement was not received, the transmission is supposed to fail. If acknowledgement was received, the transmission is successful.
Using the DlnI2cMasterSetMaxReplyCount() function, you can change the maximum number of attempts to transmit data. The DlnI2cMasterGetMaxReplyCount() function allows to check the currently specified number of attempts.
DLN-series adapters support only 7-bit addressing. To start transmission, the I2C master generates the START (S) condition followed by seven bits of a slave address and an eighth bit which is a data direction bit.
7-bit addressing allows 127 different addresses. Some addresses are reserved (See Slave Address and Data Direction), only 112 devices can actually be connected to the I2C bus. To scan all possible addresses and to find devices connected to the I2C bus, us the DlnI2cMasterScanDevices() function. It returns the number of connected devices and the list of their addresses.You can use these addresses for I2C transmission in one of the following functions:
Receives data from the specified slave. Internal address can be specified (See READ Operation for details).
Sends data to the specified slave. Internal address can be specified (See WRITE Operation for details).
Sends data to the specified slave, then reads data from the same slave (only DLN-1 and DLN-2 adapters support this function).
The following example shows how to operate with I2C master module. You can find the complete example in the “..\Program Files\Diolan\DLN\examples\c_cpp\examples\simple
” folder after DLN setup package installation.
#include "..\..\..\common\dln_generic.h" #include "..\..\..\common\dln_i2c_master.h" #pragma comment(lib, "..\\..\\..\\bin\\dln.lib") int _tmain(int argc, _TCHAR* argv[]) { // Open device HDLN device; DlnOpenUsbDevice(&device); // Set frequency uint32_t frequency; DlnI2cMasterSetFrequency(device, 0, 100000, &frequency); // Enable I2C master uint16_t conflict; DlnI2cMasterEnable(device, 0, &conflict); // Prepare output buffer uint8_t output[8], input[8]; for (int i = 0; i < 8; i++) output[i] = i; // Write bytes DlnI2cMasterWrite(device, 0, 0x50, 1, 0, 8, output); // Read bytes DlnI2cMasterRead(device, 0, 0x50, 1, 0, 8, input); // Print input data for (int i = 0; i < 8; i++) printf("%02x ", input[i]); // Disable I2C master DlnI2cMasterDisable(device, 0); // Close device DlnCloseHandle(device); return 0; }
Line 1:#include "..\..\..\common\dln_generic.h"
The dln_generic..h
header file declares functions and data structures for the generic interface.
Line 2:#include "..\..\..\common\dln_i2c_master.h"
The dln_i2c_master.h
header file declares functions and data structures for the I2C master interface.
Line 3:#pragma comment(lib, "..\\..\\..\\bin\\dln.lib")
Use dln.lib
library while project linking.
Line 10:DlnOpenUsbDevice(&device);
The function establishes the connection with the DLN adapter. This application uses the USB connectivity of the adapter. For additional options, refer to the Device Opening & Identification section.
Line 14:DlnI2cMasterSetFrequency(device, 0, 100000, &frequency);
This function sets frequency on the I2C bus. It is set in Hertz. Any frequency value can be provided to the function, but only device compatible frequency will be set. You can read the actual frequency value by providing pointer to the unsigned 32-bit integer variable. You can read more about I2C bus speed and frequency by navigating to I2C Speed and Frequency section.
Line 17:DlnI2cMasterEnable(device, 0, &conflict);
This function enables I2C master module.
Line 21:for (int i = 0; i < 8; i++) output[i] = i;
Fill output array with the values from 0 to 8. It will be used as data buffer for sending it via I2C bus.
Line 23: DlnI2cMasterWrite(device, 0, 0x50, 1, 0, 8, output);
This function sends provided data buffer via I2C bus to connected I2C slave device. To send data properly to slave device it is required to provide also slave device address and memory address. You can read more about I2C addressing at I2C Addresses.
Line 26: DlnI2cMasterRead(device, 0, 0x50, 1, 0, 8, &input);
This function reads data from the I2C slave device. The parameters are almost similar to the data writing process.
Line 28: for (int i = 0; i < 8; i++) printf("%02x ", input[i]);
Print to console data, which was read from the I2C slave device.
Line 31: DlnI2cMasterDisable(device, 0);
Disable I2C master port.
Line 33: DlnCloseHandle(device);
Closing handle to the previously opened DLN-series adapter.
Use the I2C Master Interface functions to control and monitor the I2C Master module of a DLN-series adapter. The dln_i2c_master.h
file declares the I2C Master Interface functions.
General port information:
Retrieves the total number of I2C master ports available at your DLN-series adapter.
Assigns a port to the I2C Master module.
Releases a port from the I2C Master module.
Retrieves whether a port is assigned to the I2C Master module.
Scans all slave addresses searching for connected I2C slave devices.
I2C Master module configuration functions:
Configures frequency for the specified I2C master port.
Retrieves frequency configuration for an I2C Master port.
Configures the maximum reply count for an I2C master port.
Retrieves the maximum reply count configuration.
Transmission functions:
Receives data from the specified slave. Internal address can be specified.
Sends data to the specified slave. Internal address can be specified.
Sends data to the specified slave, then reads data from the same slave (only DLN-1 and DLN-2 adapters support this function).
The DlnI2cMasterDisable()
function releases the specified port from the I2C Master module.
The DlnI2cMasterEnable()
function assigns the specified port to the I2C Master module.
The DlnI2cMasterGetFrequency()
function retrieves the current I2C bus clock frequency.
The DlnI2cMasterGetMaxReplyCount()
function retrieves maximum reply count for I2C master port.
The DlnI2cMasterGetPortCount()
function retrieves the total number of I2C master ports available at your DLN-series adapter.
The DlnI2cMasterIsEnabled()
function checks whether the specified I2C master port is active or not.
The DlnI2cMasterRead()
function reads data from the specified I2C slave device.
The DlnI2cMasterScanDevices()
function scans all the 127 slave addresses searching for connected I2C slave devices.
The DlnI2cMasterSetFrequency()
function configures the clock frequency for the specified I2C port.
The DlnI2cMasterSetMaxReplyCount()
function sets maximum reply count for I2C master port.
DLN-1 and DLN-2 adapters cannot send a single slave address and direction bit without data bytes. This is a firmware driver limitation. Therefore, an adapter cannot read a single byte at all possible addresses. Retrying every read/write operation 10 times is set by default in firmware I2C driver, but using the DlnI2cMasterSetMaxReplyCount()
function you can modify this parameter.
The DlnI2cMasterTransfer()
function sends and receives data via the I2C bus. The data is sent and received as an array of 1-byte elements.
The DlnI2cMasterWrite()
function sends data to the specified I2C slave device.