API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
i2c_slave.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 configure platform like an i2c 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"
#define OwnAddress1 0x28
#define OwnAddress2 0x30
#define ClockSpeed 400000
#define REGS_SIZE 8
#pragma pack(1)
uint8_t Regs[REGS_SIZE];
#pragma pack()
/*
* 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_SLAVE_EXAMPLE(void *pvParameters){
uint8_t i;
for (i=0;i<=7;i++){
Regs[i]=i;
}
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() {
tsmart_i2c_config_t tsmart_i2c_config;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Initialize I2C. Interface I2C1 is selected */
/* I2C1 Slave configuration */
TSMART_I2C_ConfigInit(&tsmart_i2c_config);
tsmart_i2c_config.own_address = OwnAddress1;
tsmart_i2c_config.regs = Regs;
tsmart_i2c_config.n_regs = REGS_SIZE;
TSMART_I2C_SlaveInit(&tsmart_i2c1, tsmart_i2c_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create I2C task */
if(xTaskCreate(vI2C_SLAVE_EXAMPLE, "I2C_SLAVE", 256, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}