⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c_framing_layer.h

📁 BlackFin处理器视频演示代码
💻 H
字号:
/**
 * @file i2c_framing_layer.h
 * @author Zlatan Stanojevic
 * 
 * Define I2C_BLOCKING if You want to enable blocking behaviour (the functions block execution until the 
 * requested I2C operation has either succeeded or failed) of the @ref I2CRead, @ref I2CWrite,
 * @ref I2CGet and @ref I2CSet functions. The @ref I2CScanbus function is always blocking.
 * 
 * The use of the @a Framing @a Layer is not mandatory although it greatly simplifies the use of the 
 * I2C emulator (@ref i2c_timing_layer.h and i2c_timing_layer.c).
 * 
 * @note As the I2C emulator doesn't support slave mode all I2C operations are limited to master mode only.
 */



#ifndef I2C_FRAMING_LAYER_H
#define I2C_FRAMING_LAYER_H

#define I2C_BLOCKING

#include "i2c_timing_layer.h"

/*
typedef enum 
{
    I2CSTAT_OK, 
    I2CSTAT_ERR
} I2CStatus;
*/


/**
 * Initializes the module. This function must be called before any other function in this module.
 */
void I2CInitFraming( void );


/**
 * @typedef I2CCallback
 * 
 * The callbacks supplied to the @ref I2CRead, @ref I2CWrite, @ref I2CGet and @ref I2CSet functions
 * in non blocking mode (!) should accept unsigned char as their first argument, unsigned char* as their
 * second and should have void return type.
 */
typedef void(*I2CCallback)( unsigned char, unsigned char* );


#if 0
/* this is a dummy section only needed for doxygen to work properly.
   it will be omitted by the preprocessor */

/**
 * @brief Performs master read
 * 
 * This function initiates a master read operation (Firts byte's LSB is high). 
 * 
 * Depending upon whether 
 * I2C_BLOCKING is defined before including the @ref i2c_framing_layer.h header this function blocks
 * execution until the I2C operation has finished. In non-blocking mode the function returns immediately
 * and a callback is called when the I2C operation has finished.
 * 
 * @note The Framing Layer does not queue messages. A request for an I2C operation will wait (block execution)
 * until a possibly pending operation has finished.
 * 
 * @param hwid The remote device's Hardware ID. Only the lower 7 bits are relevant.
 * @param message Pointer to array where the received bytes are to be stored.
 * @param msg_length Number of bytes to be requested from remote device.
 * @param callback Function to be called upon operation finish. 
 *        @b Applies @b only @b in @b non-blocking @b mode.
 * 
 */
void I2CRead( unsigned char hwid, unsigned char *message,
			unsigned char msg_length, I2CCallback callback );

/**
 * @brief Performs master write
 * 
 * This function initiates a master write operation (Firts byte's LSB is low). 
 * 
 * Depending upon whether 
 * I2C_BLOCKING is defined before including the @ref i2c_framing_layer.h header this function blocks
 * execution until the I2C operation has finished. In non-blocking mode the function returns immediately
 * and a callback is called when the I2C operation has finished.
 * 
 * @note The Framing Layer does not queue messages. A request for an I2C operation will wait (block execution)
 * until a possibly pending operation has finished.
 * 
 * @param hwid The remote device's Hardware ID. Only the lower 7 bits are relevant.
 * @param message Pointer to array of bytes to be sent.
 * @param msg_length Number of bytes to be read from remote device's registers.
 * @param callback Function to be called upon operation finish. 
 *        @b Applies @b only @b in @b non-blocking @b mode.
 * 
 */
void I2CWrite( unsigned char hwid, unsigned char *message,
			unsigned char msg_length, I2CCallback callback );


/**
 * @brief Wrties data to remote device's registers
 * 
 * Most devices expose a set of addressable registers through I2C. This requires a more complex I2C operation
 * which would first write HW ID then one byte as the start address and finally a sequence of bytes to be
 * written to the registers.
 * 
 * Depending upon whether 
 * I2C_BLOCKING is defined before including the @ref i2c_framing_layer.h header this function blocks
 * execution until the I2C operation has finished. In non-blocking mode the function returns immediately
 * and a callback is called when the I2C operation has finished.
 * 
 * @note The Framing Layer does not queue messages. A request for an I2C operation will wait (block execution)
 * until a possibly pending operation has finished.
 * 
 * @param hwid The remote device's Hardware ID. Only the lower 7 bits are relevant.
 * @param start_addr Address of first remote device's register to be transferred to.
 * @param values Pointer to array where the register values are stored sequentially.
 * @param vals_length Number of bytes to be witten to remote device's registers.
 * @param callback Function to be called upon operation finish. 
 *        @b Applies @b only @b in @b non-blocking @b mode.
 * 
 */
void I2CSet( unsigned char hwid, unsigned char start_addr, unsigned char *values,
			unsigned char vals_length, I2CCallback callback );


/**
 * @brief Fetches data from remote device's registers
 * 
 * Most devices expose a set of addressable registers through I2C. This requires a more complex I2C operation
 * which would first write HW ID and one byte as the start address then restart the transmission and read
 * an arbitrary sequence of bytes representing the remote device's register status.
 * 
 * Depending upon whether 
 * I2C_BLOCKING is defined before including the @ref i2c_framing_layer.h header this function blocks
 * execution until the I2C operation has finished. In non-blocking mode the function returns immediately
 * and a callback is called when the I2C operation has finished.
 * 
 * @note The Framing Layer does not queue messages. A request for an I2C operation will wait (block execution)
 * until a possibly pending operation has finished.
 * 
 * @param hwid The remote device's Hardware ID. Only the lower 7 bits are relevant.
 * @param start_addr Address of first remote device's register to be transferred.
 * @param values Pointer to array where the register values are to be stored sequentially.
 * @param vals_length Number of bytes to read from remote device.
 * @param callback Function to be called upon operation finish. 
 *        @b Applies @b only @b in @b non-blocking @b mode.
 * 
 */
void I2CGet( unsigned char hwid, unsigned char start_addr, unsigned char *values,
			unsigned char vals_length, I2CCallback callback );


#endif



#ifdef I2C_BLOCKING
#define I2CRead  _I2CRead_blocking
#define I2CWrite _I2CWrite_blocking
#define I2CSet   _I2CSet_blocking
#define I2CGet   _I2CGet_blocking
#else
#define I2CRead  _I2CRead_non_blocking
#define I2CWrite _I2CWrite_non_blocking
#define I2CSet   _I2CSet_non_blocking
#define I2CGet   _I2CGet_non_blocking
#endif


void _I2CRead_non_blocking( unsigned char hwid, unsigned char *message,
		    unsigned char msg_length, I2CCallback callback );

void _I2CWrite_non_blocking( unsigned char hwid, unsigned char *message,
			unsigned char msg_length, I2CCallback callback );

void _I2CSet_non_blocking( unsigned char hwid, unsigned char start_addr, 
		   unsigned char *values, unsigned char vals_length, I2CCallback callback );

void _I2CGet_non_blocking( unsigned char hwid, unsigned char start_addr, 
		   unsigned char *values, unsigned char vals_length, I2CCallback callback );


void _I2CRead_blocking( unsigned char hwid, unsigned char *message,
		    unsigned char msg_length );

void _I2CWrite_blocking( unsigned char hwid, unsigned char *message,
			unsigned char msg_length );

void _I2CSet_blocking( unsigned char hwid, unsigned char start_addr, 
		   unsigned char *values, unsigned char vals_length );

void _I2CGet_blocking( unsigned char hwid, unsigned char start_addr, 
		   unsigned char *values, unsigned char vals_length );



/**
 * @typedef I2CScanCallback
 * 
 * The callbacks supplied to the @ref I2CScanbus function
 * should accept unsigned char as argument
 * and should have a void return type.
 */
typedef void (*I2CScanCallback)( unsigned char );


/**
 * @brief Scans bus for devices.
 * 
 * This function scans the I2C bus by probing every single HW ID. This may take some time.
 * Meanwhile execution is blocked.
 * 
 * @param callback A valid function pointer of type @ref I2CScanCallback must be supplied here.
 *                 Each time a device responds, @a callback is called an the device's HW ID is
 *                 passed as argument.
 */
void I2CScanbus( I2CScanCallback callback );



void I2CInitHAL( unsigned long pa_lFrequency, unsigned long pa_lSystemClk );



#if defined( __ADSPBF537__ )

inline void I2CInit( unsigned long bitrate, unsigned long systemclk ) { I2CInitHAL( bitrate, systemclk ); }

#elif defined( __ADSPBF533__ ) || defined( __ADSPBF561__ )

inline void I2CInit( unsigned long bitrate, unsigned char scl_pin,
					 unsigned char sda_pin, unsigned char timer, unsigned long systemclk ) 
{ 
	I2CInitTiming( scl_pin, sda_pin, timer, bitrate );//, systemclk ); 
	I2CInitFraming();
}

#else
#error Target not supported
#endif

typedef struct
{
    unsigned char address, value;
} I2CConfig;

static inline void I2CControl( unsigned char hwid, I2CConfig *config, short len )
{
    short i;
    for( i = 0; i < len; i++ )
    {
        _I2CSet_blocking( hwid, config[i].address, &config[i].value, 1 );
    }
}

static inline void I2CStatus( unsigned char hwid, I2CConfig *config, short len )
{
    short i;
    for( i = 0; i < len; i++ )
    {
        _I2CGet_blocking( hwid, config[i].address, &config[i].value, 1 );
    }
}


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -