Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
304 | ingob | 1 | // ######################## SPI - FlightCtrl ################### |
2 | #include "main.h" |
||
3 | |||
4 | |||
708 | ingob | 5 | struct str_ToNaviCtrl_Version ToNaviCtrl_Version; |
6 | struct str_FromNaviCtrl_Version FromNaviCtrl_Version; |
||
7 | struct str_ToNaviCtrl_Value ToNaviCtrl_Value; |
||
8 | struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
||
9 | |||
304 | ingob | 10 | unsigned char SPI_BufferIndex; |
708 | ingob | 11 | unsigned char SPI_RxBufferIndex; |
606 | ingob | 12 | |
708 | ingob | 13 | volatile unsigned char SPI_Buffer[sizeof(FromNaviCtrl_Value)]; |
14 | unsigned char *SPI_TX_Buffer, *ToNaviCtrl_Chksum; |
||
15 | |||
617 | ingob | 16 | unsigned char SPITransferCompleted, SPI_ChkSum; |
597 | ingob | 17 | #ifdef USE_SPI_COMMUNICATION |
691 | ingob | 18 | |
304 | ingob | 19 | //------------------------------------------------------ |
20 | void SPI_MasterInit(void) |
||
21 | { |
||
22 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
||
23 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); |
||
24 | |||
691 | ingob | 25 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
26 | SPSR = 0; // SPI2X |
||
27 | |||
304 | ingob | 28 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); |
606 | ingob | 29 | SPITransferCompleted = 1; |
30 | |||
708 | ingob | 31 | //SPDR = 0x00; // dummy write |
606 | ingob | 32 | |
708 | ingob | 33 | ToNaviCtrl_Value.Sync1 = 0x81; |
34 | ToNaviCtrl_Value.Sync2 = 0x55; |
||
606 | ingob | 35 | |
708 | ingob | 36 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
37 | ToNaviCtrl_Value.IntegralNick = 12345; |
||
38 | ToNaviCtrl_Value.IntegralRoll = 56789; |
||
39 | ToNaviCtrl_Value.StickNick = 100; |
||
40 | ToNaviCtrl_Value.StickRoll = 150;//(char) StickRoll; |
||
41 | ToNaviCtrl_Value.StickGier = 200;//(char) StickGier; |
||
42 | |||
304 | ingob | 43 | } |
44 | |||
45 | //------------------------------------------------------ |
||
708 | ingob | 46 | void SPI_StartTransmitPacket(unsigned char command) |
304 | ingob | 47 | { |
606 | ingob | 48 | //if ((SLAVE_SELECT_PORT & (1 << SPI_SLAVE_SELECT)) == 0) return; // transfer of prev. packet not completed |
49 | if (!SPITransferCompleted) return; |
||
708 | ingob | 50 | //if (!(SPSR & (1 << SPIF))) return; |
51 | _delay_us(30); |
||
304 | ingob | 52 | |
53 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
617 | ingob | 54 | |
708 | ingob | 55 | switch(command) |
56 | { |
||
57 | case SPI_CMD_VERSION: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Version; |
||
58 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Version.Chksum; |
||
59 | break; |
||
60 | |||
61 | case SPI_CMD_VALUE: SPI_TX_Buffer = (unsigned char *) &ToNaviCtrl_Value; |
||
62 | ToNaviCtrl_Chksum = (unsigned char *) &ToNaviCtrl_Value.Chksum; |
||
63 | break; |
||
64 | |||
65 | } |
||
66 | |||
67 | |||
606 | ingob | 68 | SPITransferCompleted = 0; |
304 | ingob | 69 | UpdateSPI_Buffer(); // update buffer |
70 | SPI_BufferIndex = 1; |
||
708 | ingob | 71 | //ebugOut.Analog[16]++; |
304 | ingob | 72 | // -- Debug-Output --- |
73 | //---- |
||
691 | ingob | 74 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
75 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
76 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
708 | ingob | 77 | SPDR = ToNaviCtrl_Value.Sync1; // Start transmission |
78 | *ToNaviCtrl_Chksum = ToNaviCtrl_Value.Sync1; |
||
691 | ingob | 79 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
80 | |||
304 | ingob | 81 | } |
82 | |||
83 | //------------------------------------------------------ |
||
84 | //SIGNAL(SIG_SPI) |
||
85 | void SPI_TransmitByte(void) |
||
86 | { |
||
708 | ingob | 87 | static unsigned char SPI_RXState = 0; |
88 | unsigned char rxdata; |
||
89 | static unsigned char rxchksum; |
||
691 | ingob | 90 | |
91 | if (SPITransferCompleted) return; |
||
304 | ingob | 92 | if (!(SPSR & (1 << SPIF))) return; |
691 | ingob | 93 | |
708 | ingob | 94 | _delay_us(30); |
95 | |||
606 | ingob | 96 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
304 | ingob | 97 | |
708 | ingob | 98 | rxdata = SPDR; |
99 | switch ( SPI_RXState) |
||
100 | { |
||
101 | case 0: |
||
102 | |||
103 | SPI_RxBufferIndex = 0; |
||
104 | //DebugOut.Analog[17]++; |
||
105 | rxchksum = rxdata; |
||
106 | if (rxdata == 0x81 ) { SPI_RXState = 1; } // 1. Syncbyte ok |
||
107 | |||
108 | break; |
||
109 | |||
110 | case 1: |
||
111 | if (rxdata == 0x55) { rxchksum += rxdata; SPI_RXState = 2; } // 2. Syncbyte ok |
||
112 | else SPI_RXState = 0; |
||
113 | //DebugOut.Analog[18]++; |
||
114 | break; |
||
115 | |||
116 | case 2: |
||
117 | SPI_Buffer[SPI_RxBufferIndex++]= rxdata; // get data |
||
118 | //DebugOut.Analog[19]++; |
||
119 | if (SPI_RxBufferIndex >= sizeof(FromNaviCtrl_Value)) |
||
120 | { |
||
121 | |||
122 | if (rxdata == rxchksum) |
||
123 | { |
||
124 | unsigned char *ptr = (unsigned char *)&FromNaviCtrl_Value; |
||
125 | |||
126 | memcpy(ptr, (unsigned char *) SPI_Buffer, sizeof(SPI_Buffer)); |
||
127 | |||
128 | DebugOut.Analog[20]++; |
||
129 | } |
||
130 | else DebugOut.Analog[21]++; |
||
131 | |||
132 | SPI_RXState = 0; |
||
133 | } |
||
134 | else rxchksum += rxdata; |
||
135 | break; |
||
136 | |||
137 | } |
||
138 | |||
139 | if (SPI_BufferIndex < sizeof(ToNaviCtrl_Value)) |
||
606 | ingob | 140 | { |
141 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
691 | ingob | 142 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
143 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
144 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
617 | ingob | 145 | |
708 | ingob | 146 | SPDR = SPI_TX_Buffer[SPI_BufferIndex]; |
147 | *ToNaviCtrl_Chksum += SPI_TX_Buffer[SPI_BufferIndex]; |
||
691 | ingob | 148 | // SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
149 | |||
606 | ingob | 150 | } |
708 | ingob | 151 | else SPITransferCompleted = 1; |
152 | |||
304 | ingob | 153 | SPI_BufferIndex++; |
154 | } |
||
155 | |||
691 | ingob | 156 | |
304 | ingob | 157 | //------------------------------------------------------ |
158 | void UpdateSPI_Buffer(void) |
||
159 | { |
||
691 | ingob | 160 | static unsigned char i =0; |
304 | ingob | 161 | cli(); |
708 | ingob | 162 | ToNaviCtrl_Value.Command = SPI_CMD_VALUE; |
709 | ingob | 163 | ToNaviCtrl_Value.IntegralNick = (int) ( IntegralNick / 1024); |
164 | ToNaviCtrl_Value.IntegralRoll = (int) (IntegralRoll / 1024); |
||
708 | ingob | 165 | ToNaviCtrl_Value.StickNick = 4; |
166 | ToNaviCtrl_Value.StickRoll = 5;//(char) StickRoll; |
||
167 | ToNaviCtrl_Value.StickGier = 6;//(char) StickGier; |
||
717 | hbuss | 168 | ToNaviCtrl_Value.GyroCompass = ErsatzKompass / GIER_GRAD_FAKTOR; |
709 | ingob | 169 | ToNaviCtrl_Value.User1 = EE_Parameter.UserParam1; |
170 | ToNaviCtrl_Value.User2 = EE_Parameter.UserParam2; |
||
171 | ToNaviCtrl_Value.User3 = EE_Parameter.UserParam3; |
||
172 | ToNaviCtrl_Value.User4 = EE_Parameter.UserParam4; |
||
173 | ToNaviCtrl_Value.User5 = EE_Parameter.UserParam5; |
||
174 | ToNaviCtrl_Value.User6 = EE_Parameter.UserParam6; |
||
175 | ToNaviCtrl_Value.User7 = EE_Parameter.UserParam7; |
||
176 | ToNaviCtrl_Value.User8 = EE_Parameter.UserParam8; |
||
304 | ingob | 177 | sei(); |
691 | ingob | 178 | |
709 | ingob | 179 | DebugOut.Analog[30] = FromNaviCtrl_Value.GPS_Nick; |
180 | DebugOut.Analog[31] = FromNaviCtrl_Value.GPS_Roll; |
||
181 | KompassValue = FromNaviCtrl_Value.CompassValue; |
||
182 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
304 | ingob | 183 | } |
184 | |||
597 | ingob | 185 | #endif |
304 | ingob | 186 | |
187 |