i2c.h 3.4 KB

/**@file
 * @brief Declaration for the I2C functionality
 * @author Imanol Barba Sabariego
 * @date 08/06/2014
 *
 * The functions declared in this file manage the I2C functionality of the device to use with the sensors
 */

#ifndef I2C_H
#define I2C_H

#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
#include "sensors.h"
#include "libwismart.h"

//! I2C Response timeout
/*! This variable defines how many milliseconds will the device without obtaining answer from the bus until it considers that there is no I2C device in the address being polled. */
#define I2C_TIMEOUT 100

//! Debug printing definition
/*! This definition is used for debugging purposes, it is a shorthand for printing debug information */
#define DBG_I2C(fmt,...)                printf("%c[1;35mi2c.c:%c[1;00m "fmt,0x1B,0x1B, ##__VA_ARGS__)
//#define DBG_I2C(fmt,...)                printf("")

//! I2C initialization function
/*! This function initializes the I2C bus in use for the sensors, which is I2C1, at a hardware level. */
void I2C_init(void);

//! I2C comunication Start function
/*! This function generates a START sequence in the I2C bus and sends the 7 bit address passed in the arguments along with the R/W bit. */
void I2C_start(I2C_TypeDef* I2Cx /*! I2C device to use */, uint8_t address /*! I2C slave address */, uint8_t direction /*! R/W bit. Possible values are I2C_Direction_Transmitter and I2C_Direction_Receiver */);

//! I2C communication Stop function
/*! This function generaates a STOP sequence in the I2C bus, stopping the communication between master and slave */
void I2C_stop(I2C_TypeDef* I2Cx /*! I2C device to use */);

//! I2C write byte function
/*! This function writes a byte into the bus using the specified I2C device */
void I2C_write(I2C_TypeDef* I2Cx /*! I2C device to use */, uint8_t data /*! Byte to write */);

//! I2C read byte function (with master ACK)
/*! This function fetches a byte from the I2C bus that has been stored into the internal register and sends and ACK afterwards. */
uint8_t I2C_read_ack(I2C_TypeDef* I2Cx /*! I2C device to use */);

//! I2C read byte function (without master ACK)
/*! This function fetches a byte from the I2C bus that has been stored into the internal register without sending and ACK afterwards. */
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx /*! I2C device to use */);

//! I2C bus scan function
/*! This function scans the I2C bus in search of known sensor addresses and returns them in the addresses array passed by argument. */
void I2C_scan(I2C_TypeDef* I2Cx /*! I2C device to use */,uint8_t *addresses /*! Array to be filled */);

//! I2C presence check function
/*! This function checks a specified address to see if there are any slave devices listening. */
uint8_t I2C_check(I2C_TypeDef* I2Cx /*! I2C device to use */, uint8_t address /*! Address to check */);

//! I2C bus reset function
/*! This function resets the I2C bus in case that a timeout occurred, pulling both lines up and resetting the internal registers to a known state. */
void I2C_reset(I2C_TypeDef* I2Cx /*! I2C device to reset */);

//! I2C communication repeated Start function
/*! This function sends a repeated START sequence (a START sequence for which no previous STOP sequence was sent). */
void I2C_restart(I2C_TypeDef* I2Cx /*! I2C device to use */, uint8_t address /*! I2C slave address */, uint8_t direction /*! R/W bit. Possible values are I2C_Direction_Transmitter and I2C_Direction_Receiver */);


#endif