55,6 → 55,7 |
// + POSSIBILITY OF SUCH DAMAGE. |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
#include <stdlib.h> |
#include <string.h> |
#include "91x_lib.h" |
#include "i2c.h" |
#include "uart1.h" |
61,6 → 62,7 |
#include "timer.h" |
#include "main.h" |
#include "led.h" |
#include "spi_slave.h" |
|
|
volatile I2C_State_t I2C_State; |
86,9 → 88,9 |
volatile u8 I2C_ReadRequest = 0; |
volatile u32 I2C1_Timeout = 0; |
|
volatile u8 I2C_PrimRxBuffer[10]; |
|
|
|
//-------------------------------------------------------------- |
void I2C1_Init(void) |
{ |
222,6 → 224,7 |
{ |
u8 data; |
u16 status; |
static u8 crc; |
// detemine I2C State |
status = I2C_GetLastEvent(I2C1); |
|
237,140 → 240,158 |
} |
else |
{ |
switch (status) |
{ |
// the start condition was initiated on the bus |
case I2C_EVENT_MASTER_MODE_SELECT: // EV5 |
LED_GRN_ON; |
// update current bus state variable |
switch(I2C_Direction) |
{ |
case I2C_MODE_TRANSMITTER: |
I2C_State = I2C_TX_PROGRESS; |
break; |
|
case I2C_MODE_RECEIVER: |
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
{ |
switch (status) |
{ |
// the start condition was initiated on the bus |
case I2C_EVENT_MASTER_MODE_SELECT: // EV5 |
LED_GRN_ON; |
// update current bus state variable |
switch(I2C_Direction) |
{ |
case I2C_MODE_TRANSMITTER: |
I2C_State = I2C_TX_PROGRESS; |
break; |
|
case I2C_MODE_RECEIVER: |
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; |
} |
I2C_AcknowledgeConfig (I2C1, ENABLE); |
// send address/direction byte on the bus |
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
break; |
|
// the address byte was send |
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6 |
// Clear EV6 by set again the PE bit |
I2C1->CR |= 0x20; |
crc = 0; |
switch(I2C_State) |
{ |
case I2C_TX_PROGRESS: |
// send command 1st data byte (allways the command id) |
I2C_SendData(I2C1, I2C_Command); |
crc += 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: |
// if only one byte should be received |
if (I2C_RxBufferSize == 1) |
{ // send no acknowledge after reception of the first byte |
I2C_AcknowledgeConfig (I2C1, DISABLE); |
} |
else |
{ |
I2C_State = I2C_RX_PROGRESS; |
} |
Rx_Idx = 0; |
break; |
|
default: |
|
default: |
// should never happen |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
return; |
} |
I2C_AcknowledgeConfig (I2C1, ENABLE); |
// send address/direction byte on the bus |
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
break; |
|
// the address byte was send |
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6 |
// Clear EV6 by set again the PE bit |
I2C1->CR |= 0x20; |
switch(I2C_State) |
{ |
case I2C_TX_PROGRESS: |
// 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: |
// if only one byte should be received |
if (I2C_RxBufferSize == 1) |
{ // send no acknowledge after reception of the first byte |
I2C_AcknowledgeConfig (I2C1, DISABLE); |
break; |
} |
Rx_Idx = 0; |
break; |
|
default: |
// should never happen |
I2C_GenerateSTOP (I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
break; |
} |
break; |
|
// 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 |
{ |
|
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
|
// the master has transmitted a byte and slave has been acknowledged |
case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8 |
// if all bytes incl. crc are transmitted |
if ( (Tx_Idx >= I2C_TxBufferSize + 1) || (I2C_TxBuffer == NULL) ) // all bytes transmitted |
{ |
I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
|
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
{ |
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 |
{ // stop communication |
I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
I2C_State = I2C_IDLE; // ready for new actions |
LED_GRN_OFF; |
} |
} |
else |
{ // stop communication |
I2C_GenerateSTOP (I2C1, ENABLE); // generate stop condition to free the bus |
I2C_State = I2C_IDLE; // ready for new actions |
LED_GRN_OFF; |
else // some bytes have to be transmitted |
{ |
if(Tx_Idx < I2C_TxBufferSize) |
{ |
if(I2C_TxBuffer != NULL) data = I2C_TxBuffer[Tx_Idx]; |
else data = 0x00; |
crc += data; |
} |
else // the last tx buffer byte was send |
{ |
data = crc; |
} |
I2C_SendData(I2C1, data); // send next byte |
Tx_Idx++; |
} |
} |
else // some bytes have to be transmitted |
{ |
if(I2C_TxBuffer != NULL) |
break; |
|
// the master has received a byte from the slave |
case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7 |
data = I2C_ReceiveData(I2C1); |
DebugOut.Analog[16]++; |
DebugOut.Analog[6] = Rx_Idx; |
// as long as not all bytes are collected |
if (Rx_Idx < I2C_RxBufferSize) |
{ // copy received byte from the data register to the rx-buffer |
I2C_PrimRxBuffer[Rx_Idx] = data; |
crc += data; |
} |
Rx_Idx++; |
// if the 2nd last byte was received disable acknowledge for the last one |
if ( Rx_Idx == I2C_RxBufferSize ) |
{ |
I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx++]); // send next byte |
I2C_AcknowledgeConfig (I2C1, DISABLE); |
} |
else |
{ // send a dummy byte (should never happen) |
I2C_SendData(I2C1, 0x00); |
// if the last byte was received |
if ( Rx_Idx == (I2C_RxBufferSize + 1)) |
{ |
if(data == crc) |
{ |
if(I2C_RxBuffer != NULL) |
{ |
memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
} |
} |
else // crc error |
{ |
DebugOut.Analog[15]++; |
} |
// generate a STOP condition on the bus |
I2C_GenerateSTOP(I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
} |
} |
break; |
|
// the master has received a byte from the slave |
case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7 |
data = I2C_ReceiveData(I2C1); |
DebugOut.Analog[16]++; |
|
// as long as there is some space in the rx-buffer |
if ((I2C_RxBuffer != NULL) && (Rx_Idx < I2C_RxBufferSize)) |
{ // copy received byte from the data register to the rx-buffer |
I2C_RxBuffer[Rx_Idx] = data; |
} |
Rx_Idx++; |
// if the 2nd last byte was received disable acknowledge for the last one |
if ( (Rx_Idx+1) >= I2C_RxBufferSize ) |
{ |
I2C_AcknowledgeConfig (I2C1, DISABLE); |
} |
// if the last byte was received |
if ( Rx_Idx >= I2C_RxBufferSize ) |
{ |
// generate a STOP condition on the bus |
I2C_GenerateSTOP(I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
} |
break; |
|
default: |
break; |
break; |
|
default: |
break; |
} |
} |
} |
} |
//---------------------------------------------------------------- |
void I2C1_SendCommand(u8 command) |
{ |
// If I2C transmission is in progress |
if (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
while (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); |
388,7 → 409,8 |
case I2C_CMD_WRITE_CAL: |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_WriteCal.CalByte = CompassCalState; |
fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
DebugOut.Analog[14] = I2C_WriteCal.CalByte; |
I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
I2C_TxBufferSize = sizeof(I2C_WriteCal); |
break; |
399,7 → 421,7 |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_READ_HEADING: |
DebugOut.Analog[26] = I2C_Heading.Heading; |
DebugOut.Analog[26] = I2C_Heading.Heading; |
I2C_RxBuffer = (u8 *)&I2C_Heading; |
I2C_RxBufferSize = sizeof(I2C_Heading); |
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
420,16 → 442,3 |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} |
/* |
void I2C1_ReadAnswer(void) |
{ |
// only if the bus state is matching |
if (I2C_State == I2C_RX_PENDING) |
{ |
// set direction to master receiver |
I2C_Direction = I2C_MODE_RECEIVER; |
// initiale start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} |
} */ |