Rev 720 |
Go to most recent revision |
Blame |
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
;
unsigned char SPI_RxDataValid
;
#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;//(1<<SPI2X);
SLAVE_SELECT_PORT
|= (1 << SPI_SLAVE_SELECT
);
SPITransferCompleted
= 1;
//SPDR = 0x00; // dummy write
ToNaviCtrl_Value.
Sync1 = 0xAA;
ToNaviCtrl_Value.
Sync2 = 0x83;
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;
SPI_RxDataValid
= 0;
}
//------------------------------------------------------
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;
// _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;
SendSPI
= 4;
// _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
));
SPI_RxDataValid
= 1;
}
else SPI_RxDataValid
= 0;
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 = Parameter_UserParam1
;
ToNaviCtrl_Value.
User2 = Parameter_UserParam2
;
ToNaviCtrl_Value.
User3 = Parameter_UserParam3
;
ToNaviCtrl_Value.
User4 = Parameter_UserParam4
;
ToNaviCtrl_Value.
User5 = Parameter_UserParam5
;
ToNaviCtrl_Value.
User6 = Parameter_UserParam6
;
ToNaviCtrl_Value.
User7 = Parameter_UserParam7
;
ToNaviCtrl_Value.
User8 = Parameter_UserParam8
;
sei
();
if (SPI_RxDataValid
)
{
GPS_Nick
= FromNaviCtrl_Value.
GPS_Nick;
GPS_Roll
= FromNaviCtrl_Value.
GPS_Roll;
KompassValue
= FromNaviCtrl_Value.
CompassValue;
KompassRichtung
= ((540 + KompassValue
- KompassStartwert
) % 360) - 180;
if (FromNaviCtrl_Value.
BeepTime > beeptime
) beeptime
= FromNaviCtrl_Value.
BeepTime;
}
else
{
KompassValue
= 0;
KompassRichtung
= 0;
GPS_Nick
= 0;
GPS_Roll
= 0;
}
}
#endif