Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 188 → Rev 189

/trunk/Navi-Ctrl.Uv2
178,7 → 178,7
ALDICDR ()
ALDMISC ()
ALDSCAT (.\scripts\flash_str9.ld)
OPTDL (SARM.DLL)(-cSTR91x)(DARMST9.DLL)(-pSTR910)(SARM.DLL)(-cSTR91x)(TARMST9.DLL)(-pSTR910)
OPTDL (SARM.DLL)(-cSTR91x)(DARMST9.DLL)(-pSTR911)(SARM.DLL)(-cSTR91x)(TARMST9.DLL)(-pSTR911)
OPTDBG 48126,0,()()()()()()()()()() (BIN\UL2ARM.DLL)()()()
FLASH1 { 9,0,0,0,1,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0 }
FLASH2 (BIN\UL2ARM.DLL)
/trunk/config.h
59,8 → 59,8
//<o> UART2 <9600=> 9600 Baud <19200=> 19,2 kBaud <38400=> 38,4 kBaud <57600=> 57,6 kBaud <115200=> 115,2 kBaud
#define UART2_BAUD_RATE 57600 //Baud Rate for the serial interfaces
 
//<o> I2C-Bus1 <20000=> 20 kBit <50000=> 50 kBit <100000=> 100 kBit <200000=> 200 kBit <400000=> 400 kBit
#define I2C1_CLOCK 20000 // 20 kHz
//<o> I2C-Bus1 <22000=> 22 kBit <50000=> 50 kBit <100000=> 100 kBit <200000=> 200 kBit <400000=> 400 kBit
#define I2C1_CLOCK 22000 // Bit Rate for I2C
 
//</h>
//<<< end of configuration section >>>
/trunk/i2c.c
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
}
}
/trunk/i2c.h
1,9 → 1,6
#ifndef __I2C_H
#define __I2C_H
 
 
#define I2C_SLAVE_ADDRESS 0x50
 
#define I2C_CMD_VERSION 0x01
#define I2C_CMD_READ_MAG 0x02
#define I2C_CMD_READ_HEADING 0x03
45,26 → 42,11
s16 Heading;
} __attribute__((packed)) I2C_Heading_t;
 
typedef enum
{
I2C_UNDEF,
I2C_IDLE,
I2C_TX_PROGRESS,
I2C_RX_PENDING,
I2C_RX_PROGRESS,
I2C_OFF
} I2C_State_t;
 
extern volatile I2C_State_t I2C_State;
extern volatile u8 I2C_Direction;
extern volatile u32 I2C1_Timeout;
 
extern volatile I2C_Heading_t I2C_Heading;
extern volatile I2C_WriteAttitude_t I2C_WriteAttitude;
extern volatile I2C_Mag_t I2C_Mag;
extern volatile I2C_Version_t MK3MAG_Version;
extern volatile I2C_Cal_t I2C_WriteCal;
extern volatile I2C_Cal_t I2C_ReadCal;
 
void I2C1_Init(void);
void I2C1_Deinit(void);
/trunk/main.c
282,7 → 282,6
USB_TransmitTxData(); // send answer
 
SPI0_UpdateBuffer(); // handle new SPI Data
 
// ---------------- Error Check Timing ----------------------------
if(CheckDelay(TimerCheckError))
{
/trunk/spi_slave.c
74,13 → 74,6
#define SPI_TXSYNCBYTE1 0x81
#define SPI_TXSYNCBYTE2 0x55
 
typedef enum
{
SPI_SYNC1,
SPI_SYNC2,
SPI_DATA
} SPI_State_t;
 
//communication packets
volatile FromFlightCtrl_t FromFlightCtrl;
volatile ToFlightCtrl_t ToFlightCtrl;
120,32 → 113,16
{
static u8 rxchksum = 0;
u8 rxdata;
static SPI_State_t SPI_State = SPI_SYNC1;
 
#define SPI_SYNC1 0
#define SPI_SYNC2 1
#define SPI_DATA 2
static u8 SPI_State = SPI_SYNC1;
 
// clear pending bits
SSP_ClearITPendingBit(SSP0, SSP_IT_RxTimeOut);
SSP_ClearITPendingBit(SSP0, SSP_IT_RxFifo);
/*
// Fill TxFIFO while its not full or end of packet is reached
while (SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoNotFull) == SET)
{
if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN) // still data to send ?
{
SSP0->DR = SPI_TxBuffer[SPI_TxBufferIndex]; // send a byte
*Ptr_TxChksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum
SPI_TxBufferIndex++; // pointer to next byte
}
else // TxBuffer end is reached then reset and copy data to tx buffer
{
SPI_TxBufferIndex = 0; // reset buffer index
ToFlightCtrl.Chksum = 0; // initialize checksum
ToFlightCtrl.BeepTime = BeepTime; // set beeptime
BeepTime = 0; // reset local beeptime
// copy contents of ToFlightCtrl->SPI_TxBuffer
memcpy( (u8 *) &(SPI_TxBuffer[2]), (u8 *) &ToFlightCtrl, sizeof(ToFlightCtrl));
}
}
*/
 
// while RxFIFO not empty
while (SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == SET)
{
264,11 → 241,8
SSP_InitStructure.SSP_ClockRate = 0;
 
SSP_Init(SSP0, &SSP_InitStructure);
 
//SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_TxFifo | SSP_IT_RxTimeOut, ENABLE);
SSP_ITConfig(SSP0, SSP_IT_RxFifo | SSP_IT_RxTimeOut, ENABLE);
 
 
fifo_init(&CompassCalcStateFiFo, CompassCalStateQueue, sizeof(CompassCalStateQueue));
 
SSP_Cmd(SSP0, ENABLE);
296,8 → 270,8
{
// avoid sending data via SPI during the update of the ToFlightCtrl structure
VIC_ITCmd(SSP0_ITLine, DISABLE); // disable SPI interrupt
 
ToFlightCtrl.CompassHeading = I2C_Heading.Heading;
DebugOut.Analog[10] = ToFlightCtrl.CompassHeading;
ToFlightCtrl.GPS_Nick = GPS_Stick.Nick;
ToFlightCtrl.GPS_Roll = GPS_Stick.Roll;
ToFlightCtrl.GPS_Yaw = GPS_Stick.Yaw;
/trunk/uart1.c
138,7 → 138,7
"GyroHeading ",
"SPI Error ",
"SPI Okay ",
" ",
"I2C Error ",
"I2C Okay ", //15
" ",// "Kalman_K ",
"ACC_Speed_N ",