📄 hal_spear_i2c.c
字号:
#include"HAL_SPEAR_I2C.h"#include"HAL_SPEAR_I2C_REG.h"void I2C_Error_Clear (void);const volatile unsigned char* I2C_TxAdd ; /* Global address variable *///=======================================================================================/* I2C_Init *///=======================================================================================// Description - This function is called for initializing the I2C communication,// This function must be called before calling any other I2C function.//// Arguments - Initial_Value : Loads the I2CCR register with Initial value.//// Return value - void///*======================================================================================*/int I2C_Init (I2C_InitParam Init_Value){ /* Loads reset value in all I2C registers */ I2CCR = I2C_DEFAULT_PARAM1 ; I2C_Select_Speed (I2C_FASTSPEED, (unsigned int)200); /* Selects fast speed mode, Speed is 200KHz */ I2CCR = PE |(unsigned char)Init_Value ; /* Enables I2C */ I2CCR = PE |(unsigned char)Init_Value ; /* Enables I2C */ return 0;}//=======================================================================================/* I2C_Select_Speed *///=======================================================================================// Description - This function is called for selecting the I2C clock speed.//// Arguments - I2C_Speed_Mode - Sets Standard speed mode OR Fast speed mode// I2C_Speed - The speed of clock can be selected from following range//// Standard speed mode range - 0 to 100 KHz// Fast speed mode range - 101 to 400 KHz//// Return value - void////======================================================================================void I2C_Select_Speed (I2C_SpeedMode Speed_Value, unsigned int I2C_Speed){ /*unsigned long Temp ; I2CCCR &= (unsigned char)~(unsigned char)I2C_FASTSPEED ; if (Speed_Value == I2C_FASTSPEED ) // Fast speed mode is selected { Temp = (unsigned long)((Fcpu /(3000 * (unsigned long)I2C_Speed))- 2); I2CCCR = (unsigned char)(Temp); // Speed value calculated from input parameters I2CCCR |= I2C_FASTSPEED ; // FM/SM bit is enabled } else { // Standard speed mode selected Temp = (unsigned long)((Fcpu /(2000 * (unsigned long)I2C_Speed))-2); I2CCCR = (unsigned char)(Temp) ; // Speed value calculated from input parameters }*/ I2CCCR= 0xff;}//=======================================================================================/* I2C_Load_Address *///=======================================================================================// Description - Transmit the address byte for the slave device on I2C bus//// Arguments - Input Parameter 1 -// Addr_Byte - Address of the slave I2C slave device//// Input Parameter 2-// I2C_TX_MODE - Enters into transmitter mode after slave address is// transmitted.//// I2C_RX_MODE - Enters into receiver mode after slave address is transmitted//// Return value - 0 for success, non-zero for failure////======================================================================================int I2C_Load_Address (unsigned char Addr_Byte, I2C_Mode Mode_Value){ I2C_TxErrCode_t Temp1 ; unsigned int i; if ( Mode_Value == I2C_RX_MODE) { /* Checking for transmitter or receiver mode */ Addr_Byte |= I2C_RX_MODE; /* LSB of slave address made 1 */ } else if ( Mode_Value == I2C_TX_MODE) { Addr_Byte &= (unsigned char)~(unsigned char)I2C_RX_MODE; /* LSB of slave address made 0 */ } I2CDR = (unsigned int)Addr_Byte; Temp1 = I2C_IsTransmitCompleted() ; Timeoutcount = 0 ; while ((User_Timeout_Function()) && (Temp1 != I2C_DATA_TX_OK)) { Temp1 = I2C_IsTransmitCompleted() ; } switch (Temp1) /* To check transmission status */ { case I2C_DATA_TX_OK: //Insert some delay for(i=0;i<200;i++); return 0; break; case I2C_TX_AF: return -1; break; default: return -1; break; }}//=======================================================================================/* I2C_Error_Clear *///=======================================================================================// Description - Reads the I2C status registers to clear Error flags// Arguments - void//// Return value - void////======================================================================================void I2C_Error_Clear (void){ unsigned char Temp ; Temp = I2CSR2; Temp = I2CSR1 ; /* Clearing sequence for errors */}//=======================================================================================/* I2C_ACK *///=======================================================================================// Description - Enables/Disbles the ACK bit in the I2CCR.// Arguments - I2C_ACK_Param//// Return value - void////======================================================================================void I2C_ACK (I2C_ACK_Param ACK_Value){ if ( ACK_Value == I2C_ACK_ENABLE) { I2CCR |= I2C_ACK_ENABLE; /* Enables ACK bit */ } else { I2CCR &= (unsigned char)~(unsigned char)ACK; /* Disables ACK bit */ }}//=======================================================================================/* I2C_Generate_Stop *///=======================================================================================// Description - Generate the Stop condition on I2C bus//// Arguments - None//// Return value - 0 for success, non-zero for failure////======================================================================================int I2C_Generate_Stop (void){ unsigned char Temp ; unsigned int i; Temp = I2CSR1 ; if (Temp & TRA) /* Checking for transmission */ { while (!(Temp & BTF)) /* Checking for transmission completion */ { Temp = I2CSR1 ; } I2CCR |= STOP ; /* Enables STOP bit */ } else { if (!(I2CCR & STOP)) { I2CCR |= STOP ; } Temp = I2CDR; } //I2C_Status_Byte = 1 ; while (!(I2C_IsStopGen ())); //Insert Some delay for(i=0;i<9000;i++); for(i=0;i<9000;i++); for(i=0;i<9000;i++); for(i=0;i<9000;i++); for(i=0;i<9000;i++); return 0;}//=======================================================================================/* I2C_Generate_Start *///=======================================================================================// Description - Generates the Start condition on I2C bus//// Arguments - None//// Return value - 0 for success, non-zero for failure////======================================================================================int I2C_Generate_Start (){ unsigned int i; I2CCR |= 0x08; while(!(I2C_IsTransmitCompleted()== I2C_START_OK)); //Insert some delay for(i=0;i<200;i++); return 0;}int I2C_IsStopGen (void){ if (I2CCR & STOP) /* Checking if STOP condition is generated */ { return (0) ; /* STOP condition is not generated */ } else { return (1) ; /* STOP condition is generated */ }}//=======================================================================================/* I2C_SendByte *///=======================================================================================// Description - Transmits a single byte of data on I2C bus, . This function will be// called from I2C_SPEAR_xfer function//// Arguments - Tx_Data - Data byte to be transmitted//// Return value - 0 for success, non-zero for failure////======================================================================================int I2C_SendByte (unsigned char Tx_Data){ unsigned char Temp3; unsigned int i; I2C_TxErrCode_t Temp1 ; I2CDR = (unsigned int)Tx_Data ; /* Loads data to be transmitted in data register */ Temp3 = I2CDR; Timeoutcount = 0 ; Temp1 = I2C_IsTransmitCompleted() ; while ((User_Timeout_Function()) && (Temp1 != I2C_DATA_TX_OK )) { Temp1 = I2C_IsTransmitCompleted() ; } switch (Temp1) /* To check transmission status */ { case I2C_DATA_TX_OK: //Insert Some delay for(i=0;i<200;i++); return 0 ; /* Error Management */ break; case I2C_TX_AF: case I2C_TX_ARLO: case I2C_TX_BERR: return -1 ; /* Error Management */ break; default: /* None of the above condition is true */ return -1; }}//=======================================================================================/* I2C_SendBuffer *///=======================================================================================// Description - Transmits data buffer on I2C bus, This function will be// called from I2C_SPEAR_xfer function//// Arguments - Input Parameter 1// *PtrToBuffer - start address of the buffer// Input Parameter 2//// NbOfBytes - Number of bytes to be transmitted.//// Return value - 0 for success, non-zero for failure////======================================================================================int I2C_SendBuffer (const unsigned char *PtrToBuffer, int NbOfBytes){ int i,j; int status ; for (i=0; i< NbOfBytes; i++) { status = I2C_SendByte(*(PtrToBuffer + i)); if( status ) { return -1; } } //Insert some delay for(j=0;j<9000;j++); for(j=0;j<9000;j++); return 0; /* Transmission is successful */}//=======================================================================================/* I2C_RecieveByte *///=======================================================================================// Description - Returns the most recent Byte received, This function will be// called from I2C_SPEAR_xfer function//// Arguments - None//// Return value - Unsigned char - Returns the received byte.////======================================================================================unsigned char I2C_ReceiveByte(){ I2C_RxErrCode_t Temp2 ; unsigned char Rx_Data; int i; Timeoutcount = 0 ; Temp2 = I2C_IsReceptionCompleted() ; while ((User_Timeout_Function()) && ( Temp2 == I2C_RX_DATA_EMPTY)) { Temp2 = I2C_IsReceptionCompleted() ; } /* Waits for data byte reception */ if (User_Timeout_Function()) { switch (Temp2) { case I2C_DATA_RX_OK: /* Reception successful */ Rx_Data = I2CDR; //Insert some delay for(i=0;i<9000;i++); return ( Rx_Data ); break; case I2C_RX_ARLO: case I2C_RX_BERR: return -1; break; default: /* None of the above condition is true */ return -1; break; } } else { //Time out return -1; }}//=======================================================================================/* I2C_RecieveBuffer *///=======================================================================================// Description - Receives the number of data bytes and stores it in user defined area//// Arguments - *PtrToBuffer - start address of user buffer//// NobOfBytes - Number of bytes to be received////// Return value - 0 for success , non zero for failure////======================================================================================I2C_RxErrCode_t I2C_RecieveBuffer(unsigned char *PtrToBuffer, int NbOfBytes){ unsigned char DataByte; int i;// printk("INSIDE I2C_RecieveBuffe NbOfbytes = %d\n", NbOfBytes); for (; NbOfBytes > 1; --NbOfBytes) {// printk("INSIDE FOR LOOP\n"); DataByte = I2C_ReceiveByte(); //Ashiwni - for(i=0;i<9000;i++); if (NbOfBytes == 2) /* Checking for second last byte */ { I2CCR &= (unsigned char)~(unsigned char)ACK ; /* Acknowledge disabled */ } *(PtrToBuffer) = DataByte ; /* Received byte put in user buffer */ PtrToBuffer++; } if (NbOfBytes == 1) /* Checking for last byte */ {// printk("INSIDE if condition"); DataByte = I2C_ReceiveByte(); for(i=0;i<9000;i++); *(PtrToBuffer) = DataByte ; /* Received byte put in user buffer */ // printk("DataByte = %x \n", DataByte); // printk(" *PtrToBuffer = %x\n",*(PtrToBuffer)); *(PtrToBuffer + 0) = DataByte; //printk(" *PtrToBuffer = %x\n",*(PtrToBuffer)); } I2CCR |= ACK ; /* Acknowledge enabled */ return 0; /* Reception is successful */}//=======================================================================================/* I2C_MultiWriteRegister16 *///=======================================================================================// Description - Write multiple bytes to the I2C slave device's registers starting from the given base register//// Arguments - Input Parameter 1 -// SlaveAddress - The I2C chip address.//Input Parameter 2 -// Index - Index of slave device register//Input Parameter 3// PtrToBuffer - Pointer to the block of data bytes to be written to the // Registers//Input Parameter 4// NbOfBytes - the number of data bytes to be transferred////// Return value - 0 for success , non zero for failure////======================================================================================I2C_TxErrCode_t I2C_MultiWriteRegister16( unsigned char SlaveAddress, int Index, char * PtrToBuffer,int NbOfBytes){ int Status; unsigned char LSB,MSB; Status = I2C_Generate_Start(); if(Status) { return -1; } Status = I2C_Load_Address (SlaveAddress,I2C_TX_MODE ); if(Status) { return -1; } //Transmit the LSB first LSB = (unsigned char ) (Index & 0xFF ); MSB = (unsigned char )(Index >> 8 ); Status = I2C_SendByte (LSB ); if( Status) { return -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -