Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 30 → Rev 31

/branches/V0.1 killagreg/i2c.c
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
}
}
} */
/branches/V0.1 killagreg/i2c.h
8,8 → 8,6
#define I2C_CMD_READ_MAG 0x02
#define I2C_CMD_READ_HEADING 0x03
#define I2C_CMD_WRITE_CAL 0x04
#define I2C_CMD_WRITE_EEPROM 0x0A
#define I2C_CMD_READ_EEPROM 0x0B
 
 
typedef struct
16,22 → 14,15
{
u8 Major;
u8 Minor;
u8 Patch;
u8 Compatible;
} __attribute__((packed)) I2C_Version_t;
 
 
typedef struct
{
u8 Address;
u16 Content;
} __attribute__((packed)) I2C_EEPROM_t;
 
 
typedef struct
{
u16 MagX;
u16 MagY;
u16 MagZ;
s16 MagX;
s16 MagY;
s16 MagZ;
} __attribute__((packed)) I2C_Mag_t;
 
 
51,7 → 42,7
 
typedef struct
{
u16 Heading;
s16 Heading;
} __attribute__((packed)) I2C_Heading_t;
 
typedef enum
63,7 → 54,6
I2C_RX_PROGRESS,
} I2C_State_t;
 
extern u8 CompassCalState;
extern volatile I2C_State_t I2C_State;
extern volatile u8 I2C_Direction;
extern volatile u32 I2C1_Timeout;
71,7 → 61,6
extern volatile I2C_Heading_t I2C_Heading;
extern volatile I2C_WriteAttitude_t I2C_WriteAttitude;
extern volatile I2C_Mag_t I2C_Mag;
extern volatile I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM;
extern volatile I2C_Version_t I2C_Version;
extern volatile I2C_WriteCal_t I2C_WriteCal;
 
80,7 → 69,7
void I2C1_Init(void);
void I2C1_Deinit(void);
void I2C1_SendCommand(u8 command);
void I2C1_ReadAnswer(void);
//void I2C1_ReadAnswer(void);
 
#endif // I2C_H
 
/branches/V0.1 killagreg/main.c
74,10 → 74,11
#include "main.h"
 
u32 TimerCompassUpdate;
u32 TimerI2CReadDelay;
u32 TimerDebugDataDelay;
u32 TimerKmlAddPointDelay;
 
u8 CompassCalState = 0;
 
u16 BeepTime;
 
u8 ClearMKFlags = 0;
121,7 → 122,7
//----------------------------------------------------------------------------------------------------
int main(void)
{
u8 oldCompassCalState = 0;
u8 OldCompassCalState = 0;
u8 kml_state = 0;
u8 kml_count = 0;
KML_Document_t mydocument;
166,15 → 167,11
// get version from MK3MAG
I2C_Version.Major = 0xFF;
I2C_Version.Minor = 0xFF;
I2C_Version.Patch = 0xFF;
I2C_Version.Compatible = 0xFF;
 
TimerCompassUpdate = SetDelay(50);
while (!CheckDelay(TimerCompassUpdate));
I2C1_SendCommand(I2C_CMD_VERSION);
 
TimerCompassUpdate = SetDelay(5);
TimerI2CReadDelay = SetDelay(5);
TimerKmlAddPointDelay = SetDelay(250);
 
SerialPutString("\r\n---------------------------------------------");
204,26 → 201,20
}
else
{
if(oldCompassCalState != CompassCalState)
{
oldCompassCalState = CompassCalState;
TimerCompassUpdate = SetDelay(25); // every 25 ms
if(OldCompassCalState != CompassCalState)
{
I2C1_SendCommand(I2C_CMD_WRITE_CAL);
OldCompassCalState = CompassCalState;
}
else
{
TimerCompassUpdate = SetDelay(25); // every 25 ms
I2C1_SendCommand(I2C_CMD_READ_HEADING);
}
TimerCompassUpdate = SetDelay(25); // every 25 ms
}
}
if (CheckDelay(TimerI2CReadDelay))
{
I2C1_ReadAnswer();
TimerI2CReadDelay = SetDelay(1000);
}
// ---------------- Debug Timing ----------------------------------
if (CheckDelay(TimerDebugDataDelay))
{
/branches/V0.1 killagreg/main.h
7,7 → 7,7
#define VERSION_PATCH 7
 
extern u32 TimerCompassUpdate;
extern u32 TimerI2CReadDelay;
extern u8 CompassCalState;
extern u16 BeepTime;
extern u8 ClearMKFlags;
void Interrupt_Init(void);
/branches/V0.1 killagreg/menu.c
275,7 → 275,11
LCD_printfxy(0,3,"UP7:%3i UP8:%3i ",Parameter.User7,Parameter.User8);
break;
case 12: // MK3MAG
LCD_printfxy(0,0,"MK3MAG V%i.%i.%i",I2C_Version.Major,I2C_Version.Minor, I2C_Version.Patch);
I2C1_SendCommand(I2C_CMD_READ_MAG);
LCD_printfxy(0,0,"MK3MAG V%i.%i",I2C_Version.Major,I2C_Version.Minor);
LCD_printfxy(0,1,"MagX: %4i ",I2C_Mag.MagX);
LCD_printfxy(0,2,"MagY: %4i ",I2C_Mag.MagY);
LCD_printfxy(0,3,"MagZ: %4i ",I2C_Mag.MagZ);
break;
default:
MaxMenuItem = MenuItem - 1;
/branches/V0.1 killagreg/spi_slave.c
272,7 → 272,6
ToFlightCtrl.GPS_Nick = GPS_Stick.Nick;
ToFlightCtrl.GPS_Roll = GPS_Stick.Roll;
ToFlightCtrl.GPS_Yaw = GPS_Stick.Yaw;
DebugOut.Analog[26] = I2C_Heading.Heading;
// cycle spi commands
ToFlightCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++];
if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0;
364,6 → 363,7
 
case SPI_CMD_CAL_COMPASS:
CompassCalState = FromFlightCtrl.Param.Byte[0];
DebugOut.Analog[4] = CompassCalState;
break;
 
default: