API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
i2c_master.c

Example showing how to use the I2C bus communication. This example shows how to write and read the device registers using I2C communication.

/*
* @file i2c.c
* @author TST
* @version V0.0
* @date 21/06/2012
* @brief Inter Integrated Circuit (I2C) example. In this example user will be able to write and read registers
* associated to an specific device (user must specify the correct address referred to the device).
*
*/
/* This file includes all the necessary headers (System, STM32, FreeRTOS, TS_Drivers, TS_Boards, TS_Devices
* and TS_Utils) so the program can work properly */
#include "tsmart.h"
/*
* The defines below must be filled by the user. Each device using I2C needs an address and a register address.
* (The values set for this example are referred to the accelerometer MMA7455)
*/
/* Device address. User needs to define the device address */
#define DEVICE_ADDRESS 0x28
/* Write register address */
#define WRITE_REGISTER_ADDRESS_0 0x00
#define WRITE_REGISTER_ADDRESS_1 0x01
#define WRITE_REGISTER_ADDRESS_2 0x02
#define WRITE_REGISTER_ADDRESS_3 0x03
#define WRITE_REGISTER_ADDRESS_4 0x04
#define WRITE_REGISTER_ADDRESS_5 0x05
#define WRITE_REGISTER_ADDRESS_6 0x06
#define WRITE_REGISTER_ADDRESS_7 0x07
/* Data to write in the device */
#define WRITE_REGISTER_VALUE 0x37
/*
* Task function. Main process to use the I2C protocol with an specific device. Write and read registers functions
* are implemented showing how to access the device using its own address.
*
*/
void vI2C_EXAMPLE(void *pvParameters){
/* Variables */
uint8_t data_write_buf[5]={0x10,0x11,0x12,0x13,0x14};
uint8_t data_read_buf[10];
uint8_t i;
/* I2C configuration. User must fill this field with the correct configuration for each device. For this example
* a default configuration is loaded */
tsmart_i2c_config_t tsmart_i2c_config;
ts_i2c_cfg_t ts_i2c_cfg;
tsmart_i2c_config.clock_speed = TSMART_I2C_SPEED;
tsmart_i2c_config.mode = TSMART_I2C_MODE;
tsmart_i2c_config.duty_cycle = TSMART_I2C_DUTYCYCLE_2;
tsmart_i2c_config.own_address = 0x00;
tsmart_i2c_config.ack_mode = TSMART_I2C_ACK_ENABLE;
/* Read */
memset(data_read_buf, 0xFF, sizeof(data_read_buf));
for(i=0; i<8; i++){
/* Read from a device register. User must provide the device address, as well as the read register address */
if(TSMART_I2C_ReadDeviceRegister(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, i, &data_read_buf[i], 1000/portTICK_RATE_MS)!= TSMART_PASS){
for(;;);
}
}
memset(data_read_buf, 0xFF, sizeof(data_read_buf));
/* Read from a device register. User must provide the device address, as well as the read register address */
if(TSMART_I2C_ReadDeviceRegisters(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, 2, 3, data_read_buf, 1000/portTICK_RATE_MS)!= TSMART_PASS){
for(;;);
}
for(i=0; i<5; i++){
/* Read from a device register. User must provide the device address, as well as the read register address */
if(TSMART_I2C_WriteDeviceRegister(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, i, data_write_buf[i], 1000/portTICK_RATE_MS)!= TSMART_PASS){
for(;;);
}
}
/* Read from a device register. User must provide the device address, as well as the read register address */
if(TSMART_I2C_ReadDeviceRegisters(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, 0, 8, data_read_buf, 1000/portTICK_RATE_MS)!= TSMART_PASS){
for(;;);
}
memset(data_read_buf, 0xFF, sizeof(data_read_buf));
/* Write in a device register. User must provide the device address, as well as the write register address */
if( TSMART_I2C_WriteDeviceRegisters(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, 0, data_write_buf, 5, 1000/portTICK_RATE_MS) != TSMART_PASS){
for(;;);
}
/* Read from a device register. User must provide the device address, as well as the read register address */
if(TSMART_I2C_ReadDeviceRegisters(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, 0, 8, data_read_buf, 1000/portTICK_RATE_MS)!= TSMART_PASS){
for(;;);
}
while(1);
}
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Initialize I2C. Interface I2C1 is selected */
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create I2C task */
if(xTaskCreate(vI2C_EXAMPLE, "I2C", 256, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}