54,7 → 54,6 |
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// + POSSIBILITY OF SUCH DAMAGE. |
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
#include <stdlib.h> |
#include <string.h> |
#include "91x_lib.h" |
#include "i2c.h" |
65,22 → 64,29 |
#include "led.h" |
#include "spi_slave.h" |
|
#define I2C_SLAVE_ADDRESS 0x50 |
|
volatile I2C_State_t I2C_State = I2C_OFF; |
volatile u8 I2C_StopPolling = 1; |
// I2C states |
#define I2C_UNDEF 0 |
#define I2C_IDLE 1 |
#define I2C_TX_PROGRESS 2 |
#define I2C_RX_PENDING 3 |
#define I2C_RX_PROGRESS 4 |
#define I2C_OFF 5 |
|
volatile u8 I2C_State = I2C_OFF; // only on byte! because of sync by nesting irqs |
u8 I2C_StopPolling = 1; |
|
// rxbuffer |
volatile u8 I2C_RxBufferSize; |
volatile u8 *I2C_RxBuffer; |
volatile u8 Rx_Idx = 0; |
// txbuffer |
volatile u8 I2C_TxBufferSize; |
volatile u8 *I2C_TxBuffer; |
volatile u8 Tx_Idx = 0; |
|
volatile u8 I2C_Direction; |
volatile u8 I2C_Command; |
|
// I2C Transfer buffers |
volatile I2C_Heading_t I2C_Heading; |
volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
volatile I2C_Mag_t I2C_Mag; |
90,7 → 96,6 |
|
#define I2C1_TIMEOUT 500 // 500 ms |
volatile u32 I2C1_Timeout = 0; |
volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers |
|
//-------------------------------------------------------------- |
void I2C1_Init(void) |
172,11 → 177,9 |
I2C_Init(I2C1, &I2C_Struct); |
|
I2C_TxBuffer = NULL; |
Tx_Idx = 0; |
I2C_TxBufferSize = 0; |
|
I2C_RxBuffer = NULL; |
Rx_Idx = 0; |
I2C_RxBufferSize = 0; |
|
I2C_Cmd(I2C1, ENABLE); |
183,9 → 186,8 |
I2C_ITConfig(I2C1, ENABLE); |
|
VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
VIC_ITCmd(I2C1_ITLine, ENABLE); |
|
I2C1_Timeout = SetDelay(2*I2C1_TIMEOUT); |
I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
I2C_Heading.Heading = -1; |
I2C_GenerateSTOP(I2C1, ENABLE); |
I2C_State = I2C_IDLE; |
222,14 → 224,12 |
GPIO_Init(GPIO2, &GPIO_InitStructure); |
|
I2C_TxBuffer = NULL; |
Tx_Idx = 0; |
I2C_TxBufferSize = 0; |
|
I2C_RxBuffer = NULL; |
Rx_Idx = 0; |
I2C_RxBufferSize = 0; |
|
I2C1_Timeout = 0; |
I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
I2C_Heading.Heading = -1; |
|
UART1_PutString("ok"); |
239,8 → 239,10 |
//-------------------------------------------------------------- |
void I2C1_IRQHandler(void) |
{ |
static u8 Rx_Idx = 0, Tx_Idx = 0, crc = 0; |
static u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers |
u16 status; |
static u8 crc; |
|
// detemine I2C State |
status = I2C_GetLastEvent(I2C1); |
|
248,11 → 250,13 |
{ // 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, ENABLE); // free the bus |
I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
} |
I2C_State = I2C_IDLE; |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
LED_GRN_OFF; |
return; |
} |
else |
{ // depending on current i2c state |
272,6 → 276,8 |
if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
{ |
I2C_GenerateSTOP (I2C1, ENABLE); |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
LED_GRN_OFF; |
I2C_State = I2C_IDLE; |
return; |
} |
283,8 → 289,9 |
|
default: // invalid direction |
I2C_GenerateSTOP (I2C1, ENABLE); |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
LED_GRN_OFF; |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
return; |
} |
// enable acknowledge |
307,7 → 314,7 |
crc += I2C_Command; |
Tx_Idx = 0; |
// reset timeout |
I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
break; |
|
case I2C_RX_PROGRESS: |
317,7 → 324,10 |
default: // unknown I2C state |
// should never happen |
I2C_GenerateSTOP (I2C1, ENABLE); |
LED_GRN_OFF; |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
I2C_State = I2C_IDLE; |
return; |
break; |
} |
break; |
338,11 → 348,18 |
I2C_SendData(I2C1, 0x00); |
} |
} |
else // the last tx buffer byte was send |
else if(Tx_Idx == I2C_TxBufferSize) // the last tx buffer byte was send |
{ |
// send crc byte at the end |
crc = ~crc; // flip all bits in the checksum |
I2C_SendData(I2C1, crc); |
} |
else if(Tx_Idx == (I2C_TxBufferSize+1) ) |
{ |
I2C_SendData(I2C1, 0xAA); // send a dummybyte |
} |
else // last byte was send |
{ |
// generate stop or repeated start condition |
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
{ |
352,9 → 369,11 |
else |
{ // stop communication |
I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
I2C_State = I2C_IDLE; // ready for new actions |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
LED_GRN_OFF; |
DebugOut.Analog[15]++; |
I2C_State = I2C_IDLE; // ready for new actions |
|
} |
} |
Tx_Idx++; |
377,20 → 396,23 |
// compare last byte with checksum |
crc = ~crc;// flip all bits in calulated checksum |
if(crc == I2C_ReceiveData(I2C1)) |
{ // copy primary rx buffer content to rx buffer if exist |
{ |
// copy primary rx buffer content to rx buffer if exist |
if(I2C_RxBuffer != NULL) |
{ |
memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
} |
I2C1_Timeout = SetDelay(500); |
I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
DebugOut.Analog[15]++; |
} |
else // checksum error detected |
{ |
// DebugOut.Analog[14]++; |
DebugOut.Analog[14]++; |
} |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
LED_GRN_OFF; |
I2C_State = I2C_IDLE; |
LED_GRN_OFF; |
return; |
} |
Rx_Idx++; |
// if the 2nd last byte was received disable acknowledge for the last one |
408,60 → 430,67 |
//---------------------------------------------------------------- |
void I2C1_SendCommand(u8 command) |
{ |
// disable I2C IRQ to check state |
VIC_ITCmd(I2C1_ITLine, DISABLE); |
// If I2C transmission is in progress |
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); |
// update current command id |
I2C_Command = command; |
// set pointers to data area with respect to the command id |
switch (command) |
if(I2C_State == I2C_IDLE) |
{ |
case I2C_CMD_VERSION: |
I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
I2C_RxBufferSize = sizeof(MK3MAG_Version); |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_WRITE_CAL: |
I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
I2C_RxBufferSize = sizeof(I2C_ReadCal); |
I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
I2C_TxBufferSize = sizeof(I2C_WriteCal); |
break; |
case I2C_CMD_READ_MAG: |
I2C_RxBuffer = (u8 *)&I2C_Mag; |
I2C_RxBufferSize = sizeof(I2C_Mag); |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_READ_HEADING: |
DebugOut.Analog[10] = I2C_Heading.Heading; |
I2C_RxBuffer = (u8 *)&I2C_Heading; |
I2C_RxBufferSize = sizeof(I2C_Heading); |
// updat atitude from spi rx buffer |
I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
break; |
default: // unknown command id |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
} |
// set direction to master transmitter |
I2C_Direction = I2C_MODE_TRANSMITTER; |
// test on busy flag and clear it |
I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
// enable I2C IRQ again |
I2C_ITConfig(I2C1, ENABLE); |
// initiale start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
// update current command id |
I2C_Command = command; |
// set pointers to data area with respect to the command id |
switch (command) |
{ |
case I2C_CMD_VERSION: |
I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
I2C_RxBufferSize = sizeof(MK3MAG_Version); |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_WRITE_CAL: |
I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
I2C_RxBufferSize = sizeof(I2C_ReadCal); |
I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
I2C_TxBufferSize = sizeof(I2C_WriteCal); |
break; |
case I2C_CMD_READ_MAG: |
I2C_RxBuffer = (u8 *)&I2C_Mag; |
I2C_RxBufferSize = sizeof(I2C_Mag); |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
case I2C_CMD_READ_HEADING: |
I2C_RxBuffer = (u8 *)&I2C_Heading; |
I2C_RxBufferSize = sizeof(I2C_Heading); |
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
// update attitude from spi rx buffer |
VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy |
I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
VIC_ITCmd(SSP0_ITLine, ENABLE); |
break; |
default: // unknown command id |
I2C_RxBuffer = NULL; |
I2C_RxBufferSize = 0; |
I2C_TxBuffer = NULL; |
I2C_TxBufferSize = 0; |
break; |
} |
// set direction to master transmitter |
I2C_Direction = I2C_MODE_TRANSMITTER; |
// test on busy flag and clear it |
I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
// enable I2C IRQ again |
VIC_ITCmd(I2C1_ITLine, ENABLE); |
// initiate start condition on the bus |
I2C_GenerateStart(I2C1, ENABLE); |
// to be continued in the I2C1_IRQHandler() above |
} // EOF I2C_State == I2C_IDLE |
else // I2C_State != I2C_IDLE |
{ |
// re-enable I2C IRQ again |
VIC_ITCmd(I2C1_ITLine, ENABLE); |
} |
} |
|
//---------------------------------------------------------------- |
527,6 → 556,6 |
{ |
I2C1_SendCommand(I2C_CMD_READ_HEADING); |
} |
TimerCompassUpdate = SetDelay(25); // every 25 ms |
TimerCompassUpdate = SetDelay(40); // every 40 ms are 25 Hz |
} |
} |