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

📄 i2c.h

📁 Discription: This multi-master driver provides the software interface to the I2C Bus hardware of th
💻 H
字号:
// ./i2c.h start
//
/*------------------------------------------------------------------------------**
** Driver: I2C Bus Device Driver Package for M16C/62 serires MCUs.
**
** (c) 1999 Mitsubishi Electronics America, Inc.
**
** Three Diamond Lane
**
** Durham, North Carolina 27704 USA
**
** Ph 919-479-3333
**
** File: i2c.h
**
** Discription: This multi-master driver provides the software interface to the
**
** I2C Bus hardware of the M3062x series of Mitsubishi抯 MCU.
**
**------------------------------------------------------------------------------*/
/*-----------------------------------------------**
** Please make the following settings. **
**-----------------------------------------------*/
#ifndef I2C_H
#define I2C_H
#include "Rtos.h"
#define MAX_NR_MULTIPLEXERS (16)

/* temperature thresholds for cards */
#define LCT_TEMP_THRESHOLD		85
#define LCD_TEMP_THRESHOLD		85
#define YXE_LCD_TEMP_THRESHOLD	85
#define LBC_TEMP_THRESHOLD		85
#define PS_TEMP_THRESHOLD		100
#define FBC_TEMP_THRESHOLD		85
#define FBC_CHASSIS_TEMP_THRESHOLD	85
#define SC_TEMP_THRESHOLD		85
#define FC_TEMP_THRESHOLD		85
#define DX_TEMP_THRESHOLD		85
#define XC_TEMP_THRESHOLD		85
#define EXC_TEMP_THRESHOLD		85
#define OSW_TEMP_THRESHOLD		85
#define INGOING_TEMP_THRESHOLD	50
#define EFC_TEMP_THRESHOLD		85
#define TLCC_TEMP_THRESHOLD		85

#define I2C_EEPROM_SIZE 256


enum en_I2CBus
{
	e_busActiveMux1		=	0x04,
	e_busActiveMux2		=	0x05,
	e_busActiveMux3		=	0x06,
	e_busActiveMux4		=	0x07,
	e_busNoneActive     =	0x00
};

// --------------------------------------------------------------------------
// I2c-adresses :  	Bit  0- 7	device addres
//  				Bit	 8-11	bus number
//					Bit 12-15	mux number
// --------------------------------------------------------------------------

enum en_I2CAdresses
{
	e_I2cMstAdr1				= 0x78,
	e_I2cMstAdr2				= 0x79, 

	e_I2cMxDvcAdr1				= 0x70,
	e_I2cMxDvcAdr2				= 0x71,
	e_I2c_YXE_LCDQuadMxDvcAdr	= 0x70,

	e_I2cEEPROMDvcAdr 			= 0x0050,
	e_I2cLcLcEEPROMDvcAdr		= 0x0057,
	e_I2cLCDEEPROMDvcAdr		= 0x0057,
	e_I2cBcfBplEEPROMDvcAdr		= 0x0051,
	e_I2cBclEEPROMDvcAdr		= 0x0450,
	e_I2cBplEEPROMDvcAdr		= 0x0550,
	e_I2cFanEEPROMDvcAdr		= 0x0552,
	e_I2cPs1EEPROMDvcAdr		= 0x0554,
	e_I2cPs2EEPROMDvcAdr		= 0x0555,
	e_I2cLcLcSFPDvcAdr			= 0x0050,
	e_I2c_YXE_LCD_EEPROM0DvcAdr	= 0x0550,
	e_I2c_YXE_LCD_EEPROM1DvcAdr	= 0x0557,

	e_I2cTempDvcAdr 			= 0x0048,
	e_I2cLCDTempDvcAdr			= 0x004f,
	e_I2cFanTempDvcAdr			= 0x054A,
	e_I2cPs1TempDvcAdr			= 0x054C,
	e_I2cPs2TempDvcAdr			= 0x054D,
	e_I2cBcfBplTempDvcAdr		= 0x0049,
	e_I2c_YXE_LCD_TempDvcAdr	= 0x054f,

	e_I2cFanAStat1DvcAdr		= 0x0522,
	e_I2cFanAStat2DvcAdr		= 0x0523,

	e_I2cFanBSpeedReg1DvcAdr	= 0x051B,
	e_I2cFanBSpeedReg2DvcAdr	= 0x054B	

};


#define SLV_MEM_SIZE 0x08
#define SLV_CNT_MASK ( SLV_MEM_SIZE - 1 ) 

#define MAIN_CLOCK 16e6 // Main Clock speed of MCU.
#define CLOCK_DIV 2 // Main Clock divide ratio.

#ifdef I2C_25
#define I2C_BUS_SPEED 25e3 // I2C Bits per second rate.
#else
#ifdef I2C_50
#define I2C_BUS_SPEED 50e3 // I2C Bits per second rate.
#else
#define I2C_BUS_SPEED 100e3 // I2C Bits per second rate.
#endif
#endif

#define GET_ERROR_MES_RET 0 // 0 for beggining of message "Use for debugging"
// 1 for error value.
//------- Do not edit below this line. --------------------------------------------
// The following 7 functions should be called by the user抯 application.
// iic_ini is called to initialize slave operations.
extern void iic_ini(byte);

extern void iic_ini_lowlevel( void );
extern void init_i2c_mas_func(void);
// iic_stop is called to disable I2C bus communications.
extern byte iic_stop(void);

byte masterWriteI2C(byte len,void* data, word devAdr);
byte masterReadI2C( byte len, void* pdata, word devAdr);
//byte readEEProm16(byte adr, byte len, void* pdata, word devAdr);
byte readEEProm(word adr, word len, void *pdata, word devAdr, byte eepromType);
//byte writeEEProm16(byte adr, byte len, void* pdata, word devAdr);
byte writeEEProm(word adr, word len, void* pdata, word devAdr, byte eepromType );
byte scanAddressI2C( word devAdr );
void doScanI2C( byte* data,byte bus );
byte eepromRecognition( word devAdr, byte *enablePort, byte enablePortMask );
void registerI2CMux( byte mdvcadr ,byte mux_nr);
int switchMux(byte adr,byte val);

extern char _far* Get_error_mass(void);

extern bool i2cBusy( void );

// To get the temperature from LM75
byte getTemp( word i2cAddress );
// To get the temperature from LM83
byte getTemp83( word i2cAddress, byte commandReg );

// read card info
byte loadCardInfo(byte * pCardInfo, word devAdr, word num, byte eepromType);
void checkSoftwareTag( byte *enablePort,byte enablePortMask, word devAdr, byte eepromType );
void checkFPGATag( byte *enablePort,byte enablePortMask, word devAdr, byte eepromType );

// install UART interrupts for I2C programming. If called with enablePort
// pointing to ZERO, flash rom of the microprocessor is used instead of
// EEPROM.
void UART_INIT (byte *enablePort,byte enablePortMask);

void sendI2cContent( byte len, byte* data );

void initCRC( void );
word buildCRC( void *pData, word numData );
word buildSuccCRC( void* pData, word numData, word oldCrc);

word buildCRC_far( _far void *pData, word numData );
word buildSuccCRC_far( _far void* pData, word numData, word oldCrc);


extern unsigned char lng; // Number of data bytes transfered by Master.
extern unsigned char I2C_bus_busy; // Bus busy flag.
//extern unsigned char status; // Current status of bus.
extern unsigned char iic_ram[128]; // Slave Ram.
// The next two functions listed are for internal processing only.
_asmfunc
void _iic_end(unsigned char MW_STATUS, unsigned char MW_LNG);
//_asmfunc
//void _iic_mr_end(unsigned char MR_STATUS,unsigned char MR_LNG);
// --------------------------------------------------------------------------------
// The following defines set up static values used in assembley langage code.
// Original times "DELAY_2USEC" are based on 16MHZ main clock, clock divide ratio
// of 1, and
// BAUD rate of 100k bits per second. The delay values and baudrate will
// automatically
// be properly programmed when this library is compiled for the target system.
#define I2C_BUS_BAUDRATE ((unsigned char) ((MAIN_CLOCK / (2 * CLOCK_DIV * I2C_BUS_SPEED)) - 3))
#define COUNTER_RATIO ((float)((CLOCK_DIV * 5.0)/MAIN_CLOCK)) // Delay_counter executes in 5 clock cycles.
#define BAUD_RATE_ADJUST ((float)(100e3 / I2C_BUS_SPEED ))
#define DELAY_1USEC ((unsigned int)(1e-6 * BAUD_RATE_ADJUST / COUNTER_RATIO ))
#define DELAY_2USEC ((unsigned int)(2e-6 * BAUD_RATE_ADJUST / COUNTER_RATIO ))
#define DELAY_4USEC ((unsigned int)(4e-6 * BAUD_RATE_ADJUST / COUNTER_RATIO ))
#define DELAY_5USEC ((unsigned int)(5e-6 * BAUD_RATE_ADJUST / COUNTER_RATIO ))
#define DELAY_15USEC ((unsigned int)(15e-6 * BAUD_RATE_ADJUST / COUNTER_RATIO ))
#define DELAY_20USEC ((unsigned int)(20e-6 * BAUD_RATE_ADJUST / COUNTER_RATIO ))
// --------------------------------------------------------------------------------
//#endif
/*-----------------------------------------------------------**
** Use enum data type for both MW_END and MR_END functions. **
**-----------------------------------------------------------*/

enum I2C_status_Responses {
	stat_success,
	stat_arb_lost_in_address,
	stat_arb_lost_in_write,
	stat_arb_lost_in_nack,
	stat_timeout,
	stat_no_ack_from_device,
	stat_data_error_write,
	stat_data_error_read,
	stat_bus_broken,
	stat_timeout_at_address,
	stat_timeout_during_write,
	stat_timeout_at_stop,
	stat_timeout_in_start,
	stat_timeout_in_init,
	stat_timeout_after_address,
	stat_timeout_in_stop,
	stat_timeout_in_write,
	stat_timeout_in_read,
	stat_timeout_slave_write,
	stat_timeout_slave_read,
	stat_timeout_slave_dataddr,
	stat_timeout_decouple,
	stat_timeout_slave_addr,
	stat_timeout_in_write_arblost,
	stat_script_syntax_error=254,
	stat_in_progress=255
};

/*-------------------------------------------**
** Use enum data tyype for status of bus. **
**-------------------------------------------*/
enum I2C_status_flags {
	Not_busy = 0,
	Busy,
	Set_flag
};

enum eeprom_Types
{
	eeprom_Small,
	eeprom_Large,
	eeprom_NVRam
};

enum ack_Answers
{
	ack_OK,
	ack_NoAck,
	ack_Timeout
};

#define I2C_TIMEOUT 1000
#define I2C_TIMEOUT_MASTER 50
#define I2C_IRQ_TIMEOUT 0

#ifdef I2C_25
#define NUM_NOPS 50
#define NUM_NOPS_2 20
#define NUM_NOPS_3 12
#else
#ifdef I2C_50
#define NUM_NOPS 20
#define NUM_NOPS_2 9
#define NUM_NOPS_3 5
#else
#define NUM_NOPS 9
#define NUM_NOPS_2 4
#define NUM_NOPS_3 2
#endif
#endif

extern volatile word loops_per_usec;

#define udelay(usecs) { \
	word j;\
	for(j=0;j<usecs;j++) {\
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
		_nop(); \
	}  \
}

_inline
void waitCycles( byte cycles ) {
	byte i;
	for(i=0;i<cycles;i++) {
		udelay(10);
	}		
}

_inline
void waitHalfCycle( void ) {
	udelay(5);
}

_inline
void waitQuarterCycle( void ) {
	udelay(3);
}

#define M16C_OFFSET ( entryAddrMIB[e_Cmn_M16CSoftwareTag] - entryAddrMIB[ e_CmnModuleID ] )
#define FPGA_OFFSET ( entryAddrMIB[e_Cmn_FPGATag] - entryAddrMIB[ e_CmnModuleID ] )
#define M16C_TAG_OFFSET 11

#define MAX_I2C_DEVICES 8
#define TEMP_NOT_AVAILABLE 0x80

#define INITIAL_CRC 0xFFFF

#define MAX_I2C_SCRIPTLEN 64 

#define CMD_I2C_WRITEB              (0) // len =2
#define CMD_I2C_READDATA            (1)
#define CMD_I2C_WAKEUP              (2)
#define CMD_I2C_LOOP                (4) // len=3 (word count)
#define CMD_I2C_SCRIPT_END          (5) 
#define CMD_I2C_TRIGGER_RX          (6) 
#define CMD_I2C_ENDLOOP             (7)
#define CMD_I2C_CHECK_ACK           (8) 
#define CMD_I2C_WRITEDATA           (9) 
#define CMD_I2C_CHECK_ARB           (10) 
#define CMD_I2C_TRIGGER_RX_NOACK    (11) 
#define CMD_I2C_CHECK_ADDR_ARB      (12) 
#define CMD_I2C_SEND_ACKBIT         (13) 
#define CMD_I2C_SEND_NACKBIT        (17) 
#define CMD_I2C_NOCHECK_ACK	    (14)
#define CMD_I2C_REPSTART            (15) 
#define CMD_I2C_STOP                (16) 

typedef struct I2C_Context {
	OS_RSEMA i2cLockSema;
	OS_CSEMA i2cWaitSema;
	word state;
        word instrP;
	signed char muxAddrs[MAX_NR_MULTIPLEXERS];
	signed int currBus;
        byte *data; // has to be set up before interrupt is enabled;
        byte dataP;
        byte scriptstack[9]; // three levels of LOOPs should be enough
        byte stackP;
        byte result;
        byte script[MAX_I2C_SCRIPTLEN];
	byte my_i2cAddr;
} I2C_Context;

extern I2C_Context i2cContext;

#define I2C_IDLE 	   (0)
#define I2C_SLAVE_WAITADDR (1)
#define	I2C_SLAVE_WRITE    (2)
#define I2C_SLAVE_READ	   (4)
#define I2C_DECOUPLED	   (5)
#define I2C_MASTER	   (6)

_inline void i2c_clear_script(I2C_Context *ctxt) 
{
	ctxt->script[0]=CMD_I2C_SCRIPT_END;
	ctxt->instrP=0;
	ctxt->dataP=0;
	ctxt->stackP=0;
	ctxt->result=stat_in_progress;
}
_inline void i2c_reset_script(I2C_Context *ctxt) 
{
	ctxt->instrP=ctxt->stackP=ctxt->dataP=0;	
	ctxt->result=stat_in_progress;
}
#endif
// ./i2c.h end

⌨️ 快捷键说明

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