Rev 710 |
Rev 720 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
// ######################## SPI - FlightCtrl ###################
#include "main.h"
struct str_ToNaviCtrl_Version ToNaviCtrl_Version
;
struct str_FromNaviCtrl_Version FromNaviCtrl_Version
;
struct str_ToNaviCtrl_Value ToNaviCtrl_Value
;
struct str_FromNaviCtrl_Value FromNaviCtrl_Value
;
unsigned char SPI_BufferIndex
;
unsigned char SPI_RxBufferIndex
;
volatile unsigned char SPI_Buffer
[sizeof(FromNaviCtrl_Value
)];
unsigned char *SPI_TX_Buffer
, *ToNaviCtrl_Chksum
;
unsigned char SPITransferCompleted
, SPI_ChkSum
;
#ifdef USE_SPI_COMMUNICATION
//------------------------------------------------------
void SPI_MasterInit
(void)
{
DDR_SPI
|= (1<<DD_MOSI
)|(1<<DD_SCK
); // Set MOSI and SCK output, all others input
SLAVE_SELECT_DDR_PORT
|= (1 << SPI_SLAVE_SELECT
);
SPCR
= (1<<SPE
)|(1<<MSTR
)|(1<<SPR1
)|(0<<SPR0
)|(0<<SPIE
); // Enable SPI, Master, set clock rate fck/64
SPSR
= 0; // SPI2X
SLAVE_SELECT_PORT
|= (1 << SPI_SLAVE_SELECT
);
SPITransferCompleted
= 1;
//SPDR = 0x00; // dummy write
ToNaviCtrl_Value.
Sync1 = 0x81;
ToNaviCtrl_Value.
Sync2 = 0x55;
ToNaviCtrl_Value.
Command = SPI_CMD_VALUE
;
ToNaviCtrl_Value.
IntegralNick = 12345;
ToNaviCtrl_Value.
IntegralRoll = 56789;
ToNaviCtrl_Value.
StickNick = 100;
ToNaviCtrl_Value.
StickRoll = 150;//(char) StickRoll;
ToNaviCtrl_Value.
StickGier = 200;//(char) StickGier;
}
//------------------------------------------------------
void SPI_StartTransmitPacket
(unsigned char command
)
{
//if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed
if (!SPITransferCompleted
) return;
//if (!(SPSR & (1 << SPIF))) return;
_delay_us
(30);
SLAVE_SELECT_PORT
&= ~
(1 << SPI_SLAVE_SELECT
); // SelectSlave
switch(command
)
{
case SPI_CMD_VERSION
: SPI_TX_Buffer
= (unsigned char *) &ToNaviCtrl_Version
;
ToNaviCtrl_Chksum
= (unsigned char *) &ToNaviCtrl_Version.
Chksum;
break;
case SPI_CMD_VALUE
: SPI_TX_Buffer
= (unsigned char *) &ToNaviCtrl_Value
;
ToNaviCtrl_Chksum
= (unsigned char *) &ToNaviCtrl_Value.
Chksum;
break;
}
SPITransferCompleted
= 0;
UpdateSPI_Buffer
(); // update buffer
SPI_BufferIndex
= 1;
//ebugOut.Analog[16]++;
// -- Debug-Output ---
//----
asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop");
asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop");
asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop");
SPDR
= ToNaviCtrl_Value.
Sync1; // Start transmission
*ToNaviCtrl_Chksum
= ToNaviCtrl_Value.
Sync1;
// SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave
}
//------------------------------------------------------
//SIGNAL(SIG_SPI)
void SPI_TransmitByte
(void)
{
static unsigned char SPI_RXState
= 0;
unsigned char rxdata
;
static unsigned char rxchksum
;
if (SPITransferCompleted
) return;
if (!(SPSR
& (1 << SPIF
))) return;
_delay_us
(30);
SLAVE_SELECT_PORT
|= (1 << SPI_SLAVE_SELECT
); // DeselectSlave
rxdata
= SPDR
;
switch ( SPI_RXState
)
{
case 0:
SPI_RxBufferIndex
= 0;
//DebugOut.Analog[17]++;
rxchksum
= rxdata
;
if (rxdata
== 0x81 ) { SPI_RXState
= 1; } // 1. Syncbyte ok
break;
case 1:
if (rxdata
== 0x55) { rxchksum
+= rxdata
; SPI_RXState
= 2; } // 2. Syncbyte ok
else SPI_RXState
= 0;
//DebugOut.Analog[18]++;
break;
case 2:
SPI_Buffer
[SPI_RxBufferIndex
++]= rxdata
; // get data
//DebugOut.Analog[19]++;
if (SPI_RxBufferIndex
>= sizeof(FromNaviCtrl_Value
))
{
if (rxdata
== rxchksum
)
{
unsigned char *ptr
= (unsigned char *)&FromNaviCtrl_Value
;
memcpy(ptr
, (unsigned char *) SPI_Buffer
, sizeof(SPI_Buffer
));
DebugOut.
Analog[20]++;
}
else DebugOut.
Analog[21]++;
SPI_RXState
= 0;
}
else rxchksum
+= rxdata
;
break;
}
if (SPI_BufferIndex
< sizeof(ToNaviCtrl_Value
))
{
SLAVE_SELECT_PORT
&= ~
(1 << SPI_SLAVE_SELECT
); // SelectSlave
asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop");
asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop");
asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop"); asm
volatile ("nop");
SPDR
= SPI_TX_Buffer
[SPI_BufferIndex
];
*ToNaviCtrl_Chksum
+= SPI_TX_Buffer
[SPI_BufferIndex
];
// SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave
}
else SPITransferCompleted
= 1;
SPI_BufferIndex
++;
}
//------------------------------------------------------
void UpdateSPI_Buffer
(void)
{
static unsigned char i
=0;
cli
();
ToNaviCtrl_Value.
Command = SPI_CMD_VALUE
;
ToNaviCtrl_Value.
IntegralNick = (int) ( IntegralNick
/ 1024);
ToNaviCtrl_Value.
IntegralRoll = (int) (IntegralRoll
/ 1024);
ToNaviCtrl_Value.
StickNick = 4;
ToNaviCtrl_Value.
StickRoll = 5;//(char) StickRoll;
ToNaviCtrl_Value.
StickGier = 6;//(char) StickGier;
ToNaviCtrl_Value.
GyroCompass = ErsatzKompass
/ GIER_GRAD_FAKTOR
;
ToNaviCtrl_Value.
User1 = EE_Parameter.
UserParam1;
ToNaviCtrl_Value.
User2 = EE_Parameter.
UserParam2;
ToNaviCtrl_Value.
User3 = EE_Parameter.
UserParam3;
ToNaviCtrl_Value.
User4 = EE_Parameter.
UserParam4;
ToNaviCtrl_Value.
User5 = EE_Parameter.
UserParam5;
ToNaviCtrl_Value.
User6 = EE_Parameter.
UserParam6;
ToNaviCtrl_Value.
User7 = EE_Parameter.
UserParam7;
ToNaviCtrl_Value.
User8 = EE_Parameter.
UserParam8;
sei
();
DebugOut.
Analog[30] = FromNaviCtrl_Value.
GPS_Nick;
DebugOut.
Analog[31] = FromNaviCtrl_Value.
GPS_Roll;
KompassValue
= FromNaviCtrl_Value.
CompassValue;
KompassRichtung
= ((540 + KompassValue
- KompassStartwert
) % 360) - 180;
}
#endif