80,12 → 80,9 |
volatile I2C_Heading_t I2C_Heading; |
volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
volatile I2C_Mag_t I2C_Mag; |
volatile I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM; |
volatile I2C_Version_t I2C_Version; |
volatile I2C_WriteCal_t I2C_WriteCal; |
|
u8 CompassCalState = 0; |
|
volatile u8 I2C_ReadRequest = 0; |
volatile u32 I2C1_Timeout = 0; |
|
214,7 → 211,7 |
I2C_State = I2C_UNDEF; |
|
I2C1_Timeout = 0; |
I2C_Heading.Heading = 0; |
I2C_Heading.Heading = -1; |
|
SerialPutString("ok"); |
} |
224,9 → 221,24 |
void I2C1_IRQHandler(void) |
{ |
u8 data; |
u16 status; |
// detemine I2C State |
status = I2C_GetLastEvent(I2C1); |
|
switch (I2C_GetLastEvent(I2C1)) |
if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
{ // Set and subsequently clear the STOP bit while BTF is set. |
while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
{ |
I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
} |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
} |
else |
{ |
switch (status) |
{ |
// the start condition was initiated on the bus |
case I2C_EVENT_MASTER_MODE_SELECT: // EV5 |
LED_GRN_ON; |
238,33 → 250,30 |
break; |
|
case I2C_MODE_RECEIVER: |
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
{ |
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
{ |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
return; |
} |
else |
{ |
I2C_State = I2C_RX_PROGRESS; |
} |
break; |
|
default: |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
return; |
} |
else |
{ |
I2C_State = I2C_RX_PROGRESS; |
} |
break; |
|
default: |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
return; |
|
break; |
} |
I2C_AcknowledgeConfig (I2C1, ENABLE); |
// send address/direction byte on the bus |
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
// reset timeout |
I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
break; |
// the address byte was send and slave has been acknowledged |
|
// the address byte was send |
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6 |
// Clear EV6 by set again the PE bit |
I2C1->CR |= 0x20; |
274,6 → 283,8 |
// send command 1st data byte (allways the command id) |
I2C_SendData(I2C1, I2C_Command); |
Tx_Idx = 0; |
// reset timeout |
I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
break; |
|
case I2C_RX_PROGRESS: |
293,21 → 304,21 |
} |
break; |
|
// the master has transmitted a byte to the slave |
// the master has transmitted a byte and slave has been acknowledged |
case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8 |
// if all bytes are transmitted |
if ( (Tx_Idx >= I2C_TxBufferSize) || (I2C_TxBuffer == NULL) ) // all bytes transmitted |
{ |
// generate a STOP condition on the bus |
I2C_GenerateSTOP (I2C1, ENABLE); // generate stop condition to free the bus |
|
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
{ |
I2C_State = I2C_RX_PENDING; // data can be read back |
TimerI2CReadDelay = SetDelay(10); // start master receiver sequence in 10 ms |
I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
} |
else |
{ |
I2C_State = I2C_IDLE; // ready for new actions |
{ // stop communication |
I2C_GenerateSTOP (I2C1, ENABLE); // generate stop condition to free the bus |
I2C_State = I2C_IDLE; // ready for new actions |
LED_GRN_OFF; |
} |
} |
353,12 → 364,13 |
default: |
break; |
} |
} |
} |
//---------------------------------------------------------------- |
void I2C1_SendCommand(u8 command) |
{ |
// If I2C transmission is in progress |
if (I2C_State != I2C_IDLE) return; // return |
if (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
// disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during |
// update of that buffer pointers and length |
I2C_ITConfig(I2C1, DISABLE); |
373,12 → 385,6 |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_WRITE_EEPROM: |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_TxBuffer = (u8 *)&I2C_WriteEEPROM; |
I2C_TxBufferSize = sizeof(I2C_WriteEEPROM); |
break; |
case I2C_CMD_WRITE_CAL: |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
386,12 → 392,6 |
I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
I2C_TxBufferSize = sizeof(I2C_WriteCal); |
break; |
case I2C_CMD_READ_EEPROM: |
I2C_RxBuffer = (u8 *)&I2C_ReadEEPROM.Content; |
I2C_RxBufferSize = 2; |
I2C_TxBuffer = (u8 *)&I2C_ReadEEPROM; |
I2C_TxBufferSize = 1; |
break; |
case I2C_CMD_READ_MAG: |
I2C_RxBuffer = (u8 *)&I2C_Mag; |
I2C_RxBufferSize = sizeof(I2C_Mag); |
399,6 → 399,7 |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_READ_HEADING: |
DebugOut.Analog[26] = I2C_Heading.Heading; |
I2C_RxBuffer = (u8 *)&I2C_Heading; |
I2C_RxBufferSize = sizeof(I2C_Heading); |
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
415,13 → 416,11 |
I2C_ITConfig(I2C1, ENABLE); |
// set direction to master transmitter |
I2C_Direction = I2C_MODE_TRANSMITTER; |
// enable acknowledge |
//I2C_AcknowledgeConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} |
|
/* |
void I2C1_ReadAnswer(void) |
{ |
// only if the bus state is matching |
429,10 → 428,8 |
{ |
// set direction to master receiver |
I2C_Direction = I2C_MODE_RECEIVER; |
// enable acknowledge |
//I2C_AcknowledgeConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} |
} |
} */ |