Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 860 → Rev 861

/trunk/i2c.c
79,6 → 79,103
}
 
//--------------------------------------------------------------
void I2CBus_StateReset(I2C_TypeDef* I2Cx)
{
volatile I2C_Bus_t *pBus = NULL;
I2C_InitTypeDef I2C_Struct;
GPIO_InitTypeDef GPIO_InitStructure;
u8 SCL_Pin = 0;
u8 SDA_Pin = 0;
u32 SCL_Clock = 0;
u32 APBPeriph = 0;
u8 VIC_Priority = 0;
 
if (I2Cx == I2C0)
{
UART1_PutString("\r\n I2C0 Reset");
SCL_Pin = GPIO_Pin_0;
SDA_Pin = GPIO_Pin_1;
SCL_Clock = I2C0_CLOCK;
APBPeriph = __I2C0;
VIC_Priority = PRIORITY_I2C0;
 
pBus = &I2C0_Bus;
pBus->pData = I2C0_Buffer;
pBus->VIC_Source = I2C0_ITLine;
}
if (I2Cx == I2C1)
{
UART1_PutString("\r\n I2C1 Reset");
SCL_Pin = GPIO_Pin_2;
SDA_Pin = GPIO_Pin_3;
SCL_Clock = I2C1_CLOCK;
APBPeriph = __I2C1;
VIC_Priority = PRIORITY_I2C1;
 
pBus = &I2C1_Bus;
pBus->pData = I2C1_Buffer;
pBus->VIC_Source = I2C1_ITLine;
}
if(pBus == NULL) return;
pBus->State = I2C_STATE_UNDEF;
pBus->Error = I2C_ERROR_UNKNOWN;
pBus->Timeout = 0;
pBus->TxBufferSize = 0;
pBus->RxBufferSize = 0;
pBus->Direction = 0;
pBus->SlaveAddr = 0;
pBus->pRxHandler = NULL;
 
// enable Port 2 peripherie
SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
// disable a reset state
SCU_APBPeriphReset(__GPIO2, DISABLE);
 
u8 i;
u32 delay;
 
// reconfigure I2C_CLKOUT and I2C_DOUT
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = SCL_Pin | SDA_Pin;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C_CLKOUT, I2C_DOUT
GPIO_Init(GPIO2, &GPIO_InitStructure);
 
// enable I2C peripherie
SCU_APBPeriphClockConfig(APBPeriph, ENABLE);
// reset I2C peripherie
SCU_APBPeriphReset(APBPeriph, ENABLE);
SCU_APBPeriphReset(APBPeriph, DISABLE);
 
I2C_DeInit(I2Cx);
I2C_StructInit(&I2C_Struct);
I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
I2C_Struct.I2C_Ack = I2C_Ack_Enable;
I2C_Struct.I2C_CLKSpeed = SCL_Clock;
I2C_Struct.I2C_OwnAddress = 0x00;
I2C_Init(I2Cx, &I2C_Struct);
 
I2C_Cmd(I2Cx, ENABLE);
I2C_ITConfig(I2Cx, ENABLE);
 
VIC_Config(pBus->VIC_Source, VIC_IRQ , VIC_Priority);
pBus->Timeout = SetDelay(2*I2C_TIMEOUT);
I2C_GenerateSTOP(I2Cx, ENABLE);
pBus->State = I2C_STATE_IDLE;
 
// start some dummy transmissions cycles
// to get the irq routine to work
for(i = 0; i < 2; i++)
{
pBus->State = I2C_STATE_BUFFBUSY;
I2CBus_Transmission(I2Cx, 0, NULL, 1, 0, 0); // transfer 1 byte in the isr
if(I2CBus_WaitForEndOfTransmission(I2Cx, 2)) break;
}
}
 
void I2CBus_Init(I2C_TypeDef* I2Cx)
{
volatile I2C_Bus_t *pBus = NULL;
655,7 → 752,7
if(I2CBus_WaitForEndOfTransmission(I2Cx, timeout))
{
pBus->State = I2C_STATE_BUFFBUSY;
pBus->State = I2C_STATE_BUFFBUSY;
pBus->Error = I2C_ERROR_UNKNOWN;
return(1);
}
/trunk/i2c.h
44,6 → 44,8
// initialize the I2C bus
void I2CBus_Init(I2C_TypeDef* I2Cx);
// deinitialize the I2C bus
void I2CBus_StateReset(I2C_TypeDef* I2Cx);
 
void I2CBus_Deinit(I2C_TypeDef* I2Cx);
// try to allocate the I2C_Buffer within the timeout limit
// returns 1 on success