📄 lib_i2c.c
字号:
I2C_pt->I2CTCHandler = AT91_I2CRxReceiveByte;
break;
default :
//* clear SCL line
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_CODR = I2C_pt->SCL_line;
else
CLEARSCL
//* change interrupt handler to
I2C_pt->I2CTCHandler = AT91_I2CTxSendByte;
break;
} //* End switch
}
else {
I2C_pt->nb_ACK--;
if(I2C_pt->nb_ACK == 0)
//* change interrupt handler to AT91_I2C
I2C_pt->I2CTCHandler = AT91_I2CError;
}
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CRxDoACK (I2Cdesc *I2C_pt)
//* Object : clear SCL line and load RxReceiveByte
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CRxDoACK (I2Cdesc *I2C_pt)
{
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_CODR = I2C_pt->SCL_line; //* clear SCL line
else
CLEARSCL
//* change interrupt handler to
I2C_pt->I2CTCHandler = AT91_I2CRxReceiveByte;
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CTxSetSCL (I2Cdesc *I2C_pt)
//* Object : set SCL line
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CTxSetSCL (I2Cdesc *I2C_pt)
{
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_SODR = I2C_pt->SCL_line; //* set SCL line
else
SETSCL
//* change interrupt handler to AT91_I2CTxSendBit
I2C_pt->I2CTCHandler = AT91_I2CTxSendBit;
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CTxClearSCL (I2Cdesc *I2C_pt)
//* Object : Clear SCL line
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CTxClearSCL (I2Cdesc *I2C_pt)
{
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_CODR = I2C_pt->SCL_line; //* Clear SCL line
else
CLEARSCL
if(I2C_pt->countBit == 0) {
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_ODR = I2C_pt->SDA_line; //* set SDA line as an input
else
SETSDATOINPUT
}
//* change interrupt handler to AT91_I2CTxSendBit
I2C_pt->I2CTCHandler = AT91_I2CTxSendBit;
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CRxSetSCL (I2Cdesc *I2C_pt)
//* Object : set SCL line
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CRxSetSCL (I2Cdesc *I2C_pt)
{
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_SODR = I2C_pt->SCL_line; //* set SCL line
else
SETSCL
//* change interrupt handler to AT91_I2CRxReceiveBit
I2C_pt->I2CTCHandler = AT91_I2CRxReceiveBit;
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CRxClearSCL (I2Cdesc *I2C_pt)
//* Object : Clear SCL line
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CRxClearSCL (I2Cdesc *I2C_pt)
{
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_CODR = I2C_pt->SCL_line; //* Clear SCL line
else
CLEARSCL
if (I2C_pt->countBit == 0) { //* 所有的字节发送完毕
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_OER = I2C_pt->SDA_line; //* set SDA line as an output
else
SETSDATOOUTPUT
if(I2C_pt->nbI2CByte == 0) { //* If last byte,最后一个字节
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_SODR = I2C_pt->SDA_line; //* set SDA line for NACK
else
SETSDA
}
else {
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_CODR = I2C_pt->SDA_line; //* clear SDA line for ACK
else
CLEARSDA
} //* End if last Byte
//* change interrupt handler to AT91_I2CI2CRxPrepareACK
I2C_pt->I2CTCHandler = AT91_I2CRxPrepareACK;
}//* End countBit
else {
//* change interrupt handler to AT91_I2CRx
I2C_pt->I2CTCHandler = AT91_I2CRxReceiveBit;
}
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CRxReceiveByte (I2Cdesc *I2C_pt)
//* Object : clear SCL line
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CRxReceiveByte (I2Cdesc *I2C_pt)
{
if (I2C_pt->I2CType==I2CFLASH) {
MY_PIO_ODR = I2C_pt->SDA_line; //* set SDA line as an input
MY_PIO_CODR = I2C_pt->SCL_line; //* clear SCL line
}
else {
SETSDATOINPUT
CLEARSCL
}
I2C_pt->countBit = 8; //* initialize countBit
I2C_pt->nbI2CByte--; //* decrement byte
if(I2C_pt->nbI2CByte == 0)
I2C_pt->mode = STOP; //* next mode
//* change interrupt handler to AT91_I2C
I2C_pt->I2CTCHandler = AT91_I2CRxReceiveBit;
}
//*-----------------------------------------------------------------------------
//* Function Name : _BYTE AT91_I2CRxReceiveBit (I2Cdesc *I2C_pt)
//* Object : set SCL line
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CRxReceiveBit (I2Cdesc *I2C_pt)
{
_BYTE bit_val;
_DWORD dwData;
if (I2C_pt->I2CType==I2CFLASH)
MY_PIO_SODR = I2C_pt->SCL_line; //* set SCL line
else
SETSCL
I2C_pt->countBit--; //* decrement countBit
if (I2C_pt->I2CType==I2CFLASH)
dwData = MY_PIO_PDSR & I2C_pt->SDA_line;
else
dwData = (_DWORD)(pCPLD[8] &0x01);
if (dwData != 0) //* get bit
bit_val = 1;
else
bit_val = 0;
if(I2C_pt->countBit > 0) //* if bit counter > 0
*I2C_pt->RxEnd |= (bit_val << I2C_pt->countBit); //* update working byte
else
*(I2C_pt->RxEnd++) |= (bit_val << I2C_pt->countBit) ;
//* change interrupt handler to AT91_I2CRxClearSCL
I2C_pt->I2CTCHandler = AT91_I2CRxClearSCL;
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CWait (I2Cdesc *I2C_pt)
//* Object : Waite Handler
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CWait (I2Cdesc *I2C_pt)
{
I2C_pt->countBit--; //* decrement countBit for time
if(I2C_pt->countBit == 0) { //* if bit counter = 0
I2C_pt->timerBase->TC_CCR = TC_CLKDIS ;
//AT91_I2C_LineOpen(I2C_pt,I2C_pt->timerMem,0,0);
}
}
//*-----------------------------------------------------------------------------
//* Function Name : _BYTE AT91_I2CRxReceiveBit (I2Cdesc *I2C_pt)
//* Object : C Interrupt Handler called by assembly handler
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void I2C_lineHandlerInt (I2Cdesc *I2C_pt)
{
_BYTE dummy;
dummy = I2C_pt->timerBase->TC_SR; //* acknowledge interrupt status
(*(I2C_pt->I2CTCHandler))(I2C_pt); //* call automate state handler
}
//*-----------------------------------------------------------------------------
//* Function Name : (I2Cdesc *I2C_pt)
//* Object : Open I2C
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* RCValue Timer value for gererate I2C SCL alf periode
//* RCValue = ( (MCKI / 2) /2 ) * SCL
//* TCCLKS =0
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2C_LineOpen (I2Cdesc *I2C_pt, _DWORD dwI2CSpeed,_BYTE cChip,_BYTE cI2CType)
{
_DWORD RCValue;
I2C_pt->I2CType = cI2CType;
if (I2C_pt->I2CType == I2CFLASH) {
I2C_pt->dwI2CSpeed = I2CFLASHSPEED;
I2C_pt->wPageSize = AT24C256_PAGE_SIZE;
if (cChip==FLASH_CHIP1)
I2C_pt->DeviceAddress = AT24C256_CHIP1_ADDRESS;
else
I2C_pt->DeviceAddress = AT24C256_CHIP2_ADDRESS;
}
else {
I2C_pt->dwI2CSpeed = I2CICCARDSPEED;
I2C_pt->wPageSize = AT24C64_PAGE_SIZE;
//I2C_pt->DeviceAddress = AT24C64_I2C_ADDRESS;
}
I2C_pt->state = OK;
if (I2C_pt->I2CType==I2CFLASH) {
MY_PIO_OER = I2C_pt->SDA_line; //* set SDA line as an output
MY_PIO_SODR = I2C_pt->SDA_line; //* set SDA line
MY_PIO_OER = I2C_pt->SCL_line; //* set SCL line as an output
MY_PIO_SODR = I2C_pt->SCL_line; //* set SCL line
}
else {
SETSDATOOUTPUT
SETSDA
SETSCL
}
//MY_PS_PCER = (1<<8) ;
//* TIMER configuration
//* Set the mode of the timer
//* Open the clock of the timer
MY_PS_PCER = (1<<I2C_pt->channelId) ;
//* 0x0000C 4 0 0
//* 1100 01 00 0 0 00 0 000
//* MCK/2
//* CLKI: 0 = Counter is incremented on rising edge of the clock.
//* BURST: The clock is not gated by an external signal.
//* CPCSTOP: Counter clock is not stopped when counter reaches RC.
//* CPCDIS:Counter clock is not disabled when counter reaches RC.
//* EEVTEDG:0 1 XC0 Output
//* ENETRG:The external event resets the counter and starts the counter clock.
//*
//* CPCTRG:RC Compare resets the counter and starts the counter clock.
//* WAVE = 1
I2C_pt->timerBase->TC_CMR = 0x0000C400;
//* Set timer
RCValue = MCK/(4*I2C_pt->dwI2CSpeed);
I2C_pt->timerBase->TC_RC = RCValue; //*
I2C_pt->timerMem = RCValue; //* Save Timer value
//* modificationn set in LEVEL not EDGE
AT91_irq_open(I2C_pt->channelId, 7, AIC_SRCTYPE_INT_LEVEL_SENSITIVE, I2C_pt->AICHandler);
//* Enable the clock
I2C_pt->timerBase->TC_CCR = TC_CLKEN;
}
//*-----------------------------------------------------------------------------
//* Function Name : I2C_lineClose(I2Cdesc *I2C_pt)
//* Object : Close I2C
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2C_LineClose(I2Cdesc *I2C_pt)
{
//* Sate
I2C_pt->state = OK;
//* set line
if (I2C_pt->I2CType==I2CFLASH) {
MY_PIO_OER = I2C_pt->SDA_line; //* set SDA line as an output
MY_PIO_SODR = I2C_pt->SDA_line; //* set SDA line
MY_PIO_OER = I2C_pt->SCL_line; //* set SCL line as an output
MY_PIO_SODR = I2C_pt->SCL_line; //* set SCL line
}
else {
SETSDATOOUTPUT
SETSDA
SETSCL
}
//* change interrupt handler to
I2C_pt->I2CTCHandler = no_handler_tc;
//MY_PS_PCDR = (1<<8) ;
//* TIMER configuration
MY_PS_PCDR = (1<<I2C_pt->channelId) ;
//* Disable the clock and interrupts
I2C_pt->timerBase->TC_CCR = TC_CLKDIS ;
I2C_pt->timerBase->TC_IDR = 0xFFFFFFFF ;
}
//*-----------------------------------------------------------------------------
//* Function Name : AT91_I2CWrite (I2Cdesc *I2C_pt, _WORD loadAddress, _BYTE nbByte)
//* Object : I2C Write Byte & Write Page
//* Input Parameters : <I2C_pt> = I2C Peripheral Descriptor pointer
//* : loadAddress Address on slave
//* : nbByte Write to bytes
//* Output Parameters : none
//*-----------------------------------------------------------------------------
void AT91_I2CWrite (I2Cdesc *I2C_pt, _WORD loadAddress, _DWORD nbByte )
{
_BYTE K;
//* initialize state
I2C_pt->state = NOK;
if (I2C_pt->I2CType==I2CFLASH) {
MY_PIO_OER = I2C_pt->SDA_line; //* set SDA line as an output
MY_PIO_OER = I2C_pt->SCL_line; //* set SCL line as an output
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -