Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 32 → Rev 33

/branches/V0.1 killagreg/i2c.c
88,7 → 88,7
volatile u8 I2C_ReadRequest = 0;
volatile u32 I2C1_Timeout = 0;
 
volatile u8 I2C_PrimRxBuffer[10];
volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
 
 
//--------------------------------------------------------------
222,7 → 222,6
//--------------------------------------------------------------
void I2C1_IRQHandler(void)
{
u8 data;
u16 status;
static u8 crc;
// detemine I2C State
239,11 → 238,11
LED_GRN_OFF;
}
else
{
{ // depending on current i2c state
switch (status)
{
// the start condition was initiated on the bus
case I2C_EVENT_MASTER_MODE_SELECT: // EV5
case I2C_EVENT_MASTER_MODE_SELECT:
LED_GRN_ON;
// update current bus state variable
switch(I2C_Direction)
265,12 → 264,13
}
break;
default:
default: // invalid direction
I2C_GenerateSTOP (I2C1, ENABLE);
I2C_State = I2C_IDLE;
LED_GRN_OFF;
return;
}
// enable acknowledge
I2C_AcknowledgeConfig (I2C1, ENABLE);
// send address/direction byte on the bus
I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
277,9 → 277,10
break;
// the address byte was send
case I2C_EVENT_MASTER_MODE_SELECTED: // EV6
case I2C_EVENT_MASTER_MODE_SELECTED:
// Clear EV6 by set again the PE bit
I2C1->CR |= 0x20;
I2C_Cmd(I2C1, ENABLE);
// reset checksum
crc = 0;
switch(I2C_State)
{
293,15 → 294,10
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);
}
Rx_Idx = 0;
break;
default:
default: // unknown I2C state
// should never happen
I2C_GenerateSTOP (I2C1, ENABLE);
I2C_State = I2C_IDLE;
310,13 → 306,28
break;
// 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
case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
// some bytes have to be transmitted
if(Tx_Idx < I2C_TxBufferSize)
{
if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
if(I2C_TxBuffer != NULL)
{
I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
crc += I2C_TxBuffer[Tx_Idx];
}
else
{
I2C_SendData(I2C1, 0x00);
}
}
else // the last tx buffer byte was send
{
// send crc byte at the end
I2C_SendData(I2C1, crc);
// generate stop or repeated start condition
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
}
325,61 → 336,48
I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
I2C_State = I2C_IDLE; // ready for new actions
LED_GRN_OFF;
DebugOut.Analog[15]++;
}
}
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++;
}
Tx_Idx++;
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
case I2C_EVENT_MASTER_BYTE_RECEIVED:
// some bytes have to be received
if (Rx_Idx < I2C_RxBufferSize)
{ // copy received byte from the data register to the rx-buffer
I2C_PrimRxBuffer[Rx_Idx] = data;
crc += data;
I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
// update checksum
crc += I2C_PrimRxBuffer[Rx_Idx];
}
Rx_Idx++;
// if the 2nd last byte was received disable acknowledge for the last one
if ( Rx_Idx == I2C_RxBufferSize )
// if the last byte (crc) was received
else if ( Rx_Idx == I2C_RxBufferSize)
{
I2C_AcknowledgeConfig (I2C1, DISABLE);
}
// if the last byte was received
if ( Rx_Idx == (I2C_RxBufferSize + 1))
{
if(data == crc)
{
// generate a STOP condition on the bus before reading data register
I2C_GenerateSTOP(I2C1, ENABLE);
// compare last byte with checksum
if(crc == I2C_ReceiveData(I2C1))
{ // copy primary rx buffer content to rx buffer if exist
if(I2C_RxBuffer != NULL)
{
memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize);
}
DebugOut.Analog[15]++;
}
else // crc error
else // checksum error detected
{
DebugOut.Analog[15]++;
DebugOut.Analog[14]++;
}
// generate a STOP condition on the bus
I2C_GenerateSTOP(I2C1, ENABLE);
I2C_State = I2C_IDLE;
LED_GRN_OFF;
}
Rx_Idx++;
// if the 2nd last byte was received disable acknowledge for the last one
if ( Rx_Idx == I2C_RxBufferSize )
{
I2C_AcknowledgeConfig (I2C1, DISABLE);
}
break;
default:
409,8 → 407,8
case I2C_CMD_WRITE_CAL:
I2C_RxBuffer = NULL;
I2C_RxBufferSize = 0;
// update CalByte from spi input queue
fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
DebugOut.Analog[14] = I2C_WriteCal.CalByte;
I2C_TxBuffer = (u8 *)&I2C_WriteCal;
I2C_TxBufferSize = sizeof(I2C_WriteCal);
break;
424,6 → 422,9
DebugOut.Analog[26] = I2C_Heading.Heading;
I2C_RxBuffer = (u8 *)&I2C_Heading;
I2C_RxBufferSize = sizeof(I2C_Heading);
// updat atitude from spi rx buffer
I2C_WriteAttitude.Roll = FromFlightCtrl.IntegralRoll;
I2C_WriteAttitude.Nick = FromFlightCtrl.IntegralNick;
I2C_TxBuffer = (u8 *)&I2C_WriteAttitude;
I2C_TxBufferSize = sizeof(I2C_WriteAttitude);
break;
438,7 → 439,9
I2C_ITConfig(I2C1, ENABLE);
// set direction to master transmitter
I2C_Direction = I2C_MODE_TRANSMITTER;
// initiale start condition on the bus
// test on busy flag and clear it
I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
// initiale start condition on the bus
I2C_GenerateStart(I2C1, ENABLE);
// to be continued in the I2C1_IRQHandler() above
}
/branches/V0.1 killagreg/spi_slave.c
382,11 → 382,6
break;
}
 
 
//------------
I2C_WriteAttitude.Roll = FromFlightCtrl.IntegralRoll;
I2C_WriteAttitude.Nick = FromFlightCtrl.IntegralNick;
 
// every time we got new data from the FC via SPI call the navigation routine
GPS_Navigation();
ClearMKFlags = 1;
/branches/V0.1 killagreg/uart1.c
120,9 → 120,9
" ",
"SPI Error ",
"SPI Okay ",
"CompassCalState ",
"I2C CRC Error ", //15
"I2C_ReadByte ",
"I2C Error ",
"I2C Okay ", //15
" ",
"ACC_Speed_N ",
"ACC_Speed_E ",
" ",