API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
db_i2c.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 db_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 ***** Debug Board SENSOR *****)
*/
/* Device address. User needs to define the device address */
#define DEVICE_ADDRESS 0x39<<1
/* Write register address */
#define WRITE_REGISTER_ADDRESS 0x80
/* Data to write in the device */
#define WRITE_REGISTER_VALUE 0x03
/* Read register address */
#define READ_REGISTER_ADDRESS_ID 0x8A
#define READ_REGISTER_ADDRESS_DATA0LOW 0x8C
/*
* 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 */
int8_t data_read;
int8_t valid;
/* 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;
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;
/* Write in a device register. User must provide the device address, as well as the write register address */
valid = TSMART_I2C_WriteDeviceRegister(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, WRITE_REGISTER_ADDRESS, WRITE_REGISTER_VALUE, 1000/portTICK_RATE_MS);
/* Read from a device register. User must provide the device address, as well as the read register address */
valid = TSMART_I2C_ReadDeviceRegisters(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, READ_REGISTER_ADDRESS_ID, 1, &data_read, 1000/portTICK_RATE_MS);
/* Read from a device register. User must provide the device address, as well as the read register address */
valid = TSMART_I2C_ReadDeviceRegisters(&tsmart_i2c1, &tsmart_i2c_config, DEVICE_ADDRESS, READ_REGISTER_ADDRESS_DATA0LOW, 1, &data_read, 1000/portTICK_RATE_MS);
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;
}