Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1180 | killagreg | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + Nur für den privaten Gebrauch |
||
4 | // + www.MikroKopter.com |
||
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
11 | // + Verkauf von Luftbildaufnahmen, usw. |
||
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
18 | // + eindeutig als Ursprung verlinkt werden |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
21 | // + Benutzung auf eigene Gefahr |
||
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
25 | // + mit unserer Zustimmung zulässig |
||
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
30 | // + this list of conditions and the following disclaimer. |
||
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
32 | // + from this software without specific prior written permission. |
||
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
34 | // + for non-commercial use (directly or indirectly) |
||
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
36 | // + with our written permission |
||
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
38 | // + clearly linked as origin |
||
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
49 | // + POSSIBILITY OF SUCH DAMAGE. |
||
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
886 | killagreg | 51 | #include <avr/io.h> |
52 | #include <avr/interrupt.h> |
||
53 | #include <string.h> |
||
54 | #include <stdlib.h> |
||
1180 | killagreg | 55 | #include "main.h" |
886 | killagreg | 56 | #include "spi.h" |
57 | #include "fc.h" |
||
58 | #include "rc.h" |
||
59 | #include "eeprom.h" |
||
1180 | killagreg | 60 | #include "uart0.h" |
886 | killagreg | 61 | #include "timer0.h" |
936 | killagreg | 62 | #include "analog.h" |
304 | ingob | 63 | |
1198 | killagreg | 64 | |
65 | //----------------------------------------- |
||
66 | #define DDR_SPI DDRB |
||
67 | #define DD_SS PB4 |
||
68 | #define DD_SCK PB7 |
||
69 | #define DD_MOSI PB5 |
||
70 | #define DD_MISO PB6 |
||
71 | |||
72 | // for compatibility reasons gcc3.x <-> gcc4.x |
||
73 | #ifndef SPCR |
||
74 | #define SPCR SPCR0 |
||
75 | #endif |
||
76 | #ifndef SPIE |
||
77 | #define SPIE SPIE0 |
||
78 | #endif |
||
79 | #ifndef SPE |
||
80 | #define SPE SPE0 |
||
81 | #endif |
||
82 | #ifndef DORD |
||
83 | #define DORD DORD0 |
||
84 | #endif |
||
85 | #ifndef MSTR |
||
86 | #define MSTR MSTR0 |
||
87 | #endif |
||
88 | #ifndef CPOL |
||
89 | #define CPOL CPOL0 |
||
90 | #endif |
||
91 | #ifndef CPHA |
||
92 | #define CPHA CPHA0 |
||
93 | #endif |
||
94 | #ifndef SPR1 |
||
95 | #define SPR1 SPR01 |
||
96 | #endif |
||
97 | #ifndef SPR0 |
||
98 | #define SPR0 SPR00 |
||
99 | #endif |
||
100 | |||
101 | #ifndef SPDR |
||
102 | #define SPDR SPDR0 |
||
103 | #endif |
||
104 | |||
105 | #ifndef SPSR |
||
106 | #define SPSR SPSR0 |
||
107 | #endif |
||
108 | #ifndef SPIF |
||
109 | #define SPIF SPIF0 |
||
110 | #endif |
||
111 | #ifndef WCOL |
||
112 | #define WCOL WCOL0 |
||
113 | #endif |
||
114 | #ifndef SPI2X |
||
115 | #define SPI2X SPI2X0 |
||
116 | #endif |
||
117 | // ------------------------- |
||
118 | |||
119 | #define SLAVE_SELECT_DDR_PORT DDRC |
||
120 | #define SLAVE_SELECT_PORT PORTC |
||
121 | #define SPI_SLAVE_SELECT PC5 |
||
122 | |||
123 | |||
886 | killagreg | 124 | #define SPI_TXSYNCBYTE1 0xAA |
125 | #define SPI_TXSYNCBYTE2 0x83 |
||
126 | #define SPI_RXSYNCBYTE1 0x81 |
||
127 | #define SPI_RXSYNCBYTE2 0x55 |
||
304 | ingob | 128 | |
886 | killagreg | 129 | typedef enum |
130 | { |
||
131 | SPI_SYNC1, |
||
132 | SPI_SYNC2, |
||
133 | SPI_DATA |
||
134 | } SPI_RXState_t; |
||
708 | ingob | 135 | |
606 | ingob | 136 | |
886 | killagreg | 137 | // data exchange packets to and From NaviCtrl |
1078 | killagreg | 138 | ToNaviCtrl_t ToNaviCtrl; |
139 | FromNaviCtrl_t FromNaviCtrl; |
||
708 | ingob | 140 | |
1078 | killagreg | 141 | SPI_VersionInfo_t SPI_VersionInfo; |
142 | |||
886 | killagreg | 143 | // rx packet buffer |
144 | #define SPI_RXBUFFER_LEN sizeof(FromNaviCtrl) |
||
145 | uint8_t SPI_RxBuffer[SPI_RXBUFFER_LEN]; |
||
146 | uint8_t SPI_RxBufferIndex = 0; |
||
147 | uint8_t SPI_RxBuffer_Request = 0; |
||
720 | ingob | 148 | |
886 | killagreg | 149 | // tx packet buffer |
150 | #define SPI_TXBUFFER_LEN sizeof(ToNaviCtrl) |
||
151 | uint8_t *SPI_TxBuffer; |
||
152 | uint8_t SPI_TxBufferIndex = 0; |
||
823 | ingob | 153 | |
886 | killagreg | 154 | uint8_t SPITransferCompleted, SPI_ChkSum; |
155 | uint8_t SPI_RxDataValid; |
||
156 | |||
1078 | killagreg | 157 | uint8_t SPI_CommandSequence[] = { SPI_CMD_USER, SPI_CMD_STICK, SPI_CMD_PARAMETER1, SPI_CMD_STICK, SPI_CMD_MISC, SPI_CMD_VERSION }; |
886 | killagreg | 158 | uint8_t SPI_CommandCounter = 0; |
159 | |||
160 | /*********************************************/ |
||
161 | /* Initialize SPI interface to NaviCtrl */ |
||
162 | /*********************************************/ |
||
304 | ingob | 163 | void SPI_MasterInit(void) |
164 | { |
||
886 | killagreg | 165 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK); // Set MOSI and SCK output, all others input |
166 | SLAVE_SELECT_DDR_PORT |= (1 << SPI_SLAVE_SELECT); // set Slave select port as output port |
||
167 | |||
168 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(0<<SPR0)|(0<<SPIE); // Enable SPI, Master, set clock rate fck/64 |
||
169 | SPSR = 0;//(1<<SPI2X); |
||
170 | |||
171 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // Deselect Slave |
||
172 | |||
173 | SPI_TxBuffer = (uint8_t *) &ToNaviCtrl; // set pointer to tx-buffer |
||
174 | SPITransferCompleted = 1; |
||
175 | // initialize data packet to NaviControl |
||
176 | ToNaviCtrl.Sync1 = SPI_TXSYNCBYTE1; |
||
177 | ToNaviCtrl.Sync2 = SPI_TXSYNCBYTE2; |
||
178 | |||
179 | ToNaviCtrl.Command = SPI_CMD_USER; |
||
911 | killagreg | 180 | ToNaviCtrl.IntegralNick = 0; |
886 | killagreg | 181 | ToNaviCtrl.IntegralRoll = 0; |
1078 | killagreg | 182 | |
886 | killagreg | 183 | SPI_RxDataValid = 0; |
1078 | killagreg | 184 | |
185 | SPI_VersionInfo.Major = VERSION_MAJOR; |
||
186 | SPI_VersionInfo.Minor = VERSION_MINOR; |
||
187 | SPI_VersionInfo.Patch = VERSION_PATCH; |
||
188 | SPI_VersionInfo.Compatible = NC_SPI_COMPATIBLE; |
||
304 | ingob | 189 | } |
190 | |||
886 | killagreg | 191 | |
192 | /**********************************************************/ |
||
193 | /* Update Data transferd by the SPI from/to NaviCtrl */ |
||
194 | /**********************************************************/ |
||
195 | void UpdateSPI_Buffer(void) |
||
304 | ingob | 196 | { |
886 | killagreg | 197 | int16_t tmp; |
198 | cli(); // stop all interrupts to avoid writing of new data during update of that packet. |
||
823 | ingob | 199 | |
886 | killagreg | 200 | // update content of packet to NaviCtrl |
1180 | killagreg | 201 | ToNaviCtrl.IntegralNick = (int16_t)((10 * IntegralGyroNick) / GYRO_DEG_FACTOR); // convert to multiple of 0.1° |
202 | ToNaviCtrl.IntegralRoll = (int16_t)((10 * IntegralGyroRoll) / GYRO_DEG_FACTOR); // convert to multiple of 0.1° |
||
203 | ToNaviCtrl.GyroHeading = (int16_t)((10 * YawGyroHeading) / GYRO_DEG_FACTOR); // convert to multiple of 0.1° |
||
204 | ToNaviCtrl.GyroNick = GyroNick; |
||
205 | ToNaviCtrl.GyroRoll = GyroRoll; |
||
206 | ToNaviCtrl.GyroYaw = GyroYaw; |
||
207 | ToNaviCtrl.AccNick = ((int16_t) 10 * ACC_AMPLIFY * (NaviAccNick / NaviCntAcc)) / ACC_DEG_FACTOR; // convert to multiple of 0.1° |
||
208 | ToNaviCtrl.AccRoll = ((int16_t) 10 * ACC_AMPLIFY * (NaviAccRoll / NaviCntAcc)) / ACC_DEG_FACTOR; // convert to multiple of 0.1° |
||
911 | killagreg | 209 | NaviCntAcc = 0; NaviAccNick = 0; NaviAccRoll = 0; |
691 | ingob | 210 | |
886 | killagreg | 211 | switch(ToNaviCtrl.Command) |
212 | { |
||
213 | case SPI_CMD_USER: |
||
214 | ToNaviCtrl.Param.Byte[0] = FCParam.UserParam1; |
||
215 | ToNaviCtrl.Param.Byte[1] = FCParam.UserParam2; |
||
216 | ToNaviCtrl.Param.Byte[2] = FCParam.UserParam3; |
||
217 | ToNaviCtrl.Param.Byte[3] = FCParam.UserParam4; |
||
218 | ToNaviCtrl.Param.Byte[4] = FCParam.UserParam5; |
||
219 | ToNaviCtrl.Param.Byte[5] = FCParam.UserParam6; |
||
220 | ToNaviCtrl.Param.Byte[6] = FCParam.UserParam7; |
||
221 | ToNaviCtrl.Param.Byte[7] = FCParam.UserParam8; |
||
936 | killagreg | 222 | ToNaviCtrl.Param.Byte[8] = MKFlags; |
223 | MKFlags &= ~(MKFLAG_CALIBRATE | MKFLAG_START); // calibrate and start are temporal states that are cleared immediately after transmitting |
||
224 | ToNaviCtrl.Param.Byte[9] = (uint8_t)UBat; |
||
225 | ToNaviCtrl.Param.Byte[10] = ParamSet.LowVoltageWarning; |
||
226 | ToNaviCtrl.Param.Byte[11] = GetActiveParamSet(); |
||
886 | killagreg | 227 | break; |
228 | |||
936 | killagreg | 229 | case SPI_CMD_PARAMETER1: |
1078 | killagreg | 230 | ToNaviCtrl.Param.Byte[0] = ParamSet.NaviGpsModeControl; // Parameters for the Naviboard |
231 | ToNaviCtrl.Param.Byte[1] = ParamSet.NaviGpsGain; |
||
232 | ToNaviCtrl.Param.Byte[2] = ParamSet.NaviGpsP; |
||
233 | ToNaviCtrl.Param.Byte[3] = ParamSet.NaviGpsI; |
||
234 | ToNaviCtrl.Param.Byte[4] = ParamSet.NaviGpsD; |
||
235 | ToNaviCtrl.Param.Byte[5] = ParamSet.NaviGpsACC; |
||
936 | killagreg | 236 | ToNaviCtrl.Param.Byte[6] = ParamSet.NaviGpsMinSat; |
237 | ToNaviCtrl.Param.Byte[7] = ParamSet.NaviStickThreshold; |
||
1078 | killagreg | 238 | ToNaviCtrl.Param.Byte[8] = ParamSet.NaviOperatingRadius; |
239 | ToNaviCtrl.Param.Byte[9] = ParamSet.NaviWindCorrection; |
||
240 | ToNaviCtrl.Param.Byte[10] = ParamSet.NaviSpeedCompensation; |
||
241 | ToNaviCtrl.Param.Byte[11] = ParamSet.NaviAngleLimitation; |
||
936 | killagreg | 242 | break; |
243 | |||
244 | |||
886 | killagreg | 245 | case SPI_CMD_STICK: |
911 | killagreg | 246 | tmp = PPM_in[ParamSet.ChannelAssignment[CH_GAS]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128; |
886 | killagreg | 247 | ToNaviCtrl.Param.Byte[0] = (int8_t) tmp; |
248 | tmp = PPM_in[ParamSet.ChannelAssignment[CH_YAW]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128; |
||
249 | ToNaviCtrl.Param.Byte[1] = (int8_t) tmp; |
||
250 | tmp = PPM_in[ParamSet.ChannelAssignment[CH_ROLL]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128; |
||
251 | ToNaviCtrl.Param.Byte[2] = (int8_t) tmp; |
||
911 | killagreg | 252 | tmp = PPM_in[ParamSet.ChannelAssignment[CH_NICK]]; if(tmp > 127) tmp = 127; else if(tmp < -128) tmp = -128; |
886 | killagreg | 253 | ToNaviCtrl.Param.Byte[3] = (int8_t) tmp; |
254 | ToNaviCtrl.Param.Byte[4] = (uint8_t) Poti1; |
||
255 | ToNaviCtrl.Param.Byte[5] = (uint8_t) Poti2; |
||
256 | ToNaviCtrl.Param.Byte[6] = (uint8_t) Poti3; |
||
257 | ToNaviCtrl.Param.Byte[7] = (uint8_t) Poti4; |
||
258 | ToNaviCtrl.Param.Byte[8] = (uint8_t) RC_Quality; |
||
259 | break; |
||
260 | |||
1078 | killagreg | 261 | case SPI_CMD_MISC: |
262 | ToNaviCtrl.Param.Byte[0] = CompassCalState; |
||
263 | if(CompassCalState > 4) |
||
264 | { // jump from 5 to 0 |
||
265 | CompassCalState = 0; |
||
886 | killagreg | 266 | } |
1180 | killagreg | 267 | ToNaviCtrl.Param.Byte[1] = ParamSet.NaviPHLoginTime; |
268 | ToNaviCtrl.Param.Int[1] = ReadingHeight; // at address of Byte 2 and 3 |
||
269 | ToNaviCtrl.Param.Byte[4] = ParamSet.NaviGpsPLimit; |
||
270 | ToNaviCtrl.Param.Byte[5] = ParamSet.NaviGpsILimit; |
||
271 | ToNaviCtrl.Param.Byte[6] = ParamSet.NaviGpsDLimit; |
||
886 | killagreg | 272 | break; |
1078 | killagreg | 273 | |
274 | case SPI_CMD_VERSION: |
||
275 | ToNaviCtrl.Param.Byte[0] = SPI_VersionInfo.Major; |
||
276 | ToNaviCtrl.Param.Byte[1] = SPI_VersionInfo.Minor; |
||
277 | ToNaviCtrl.Param.Byte[2] = SPI_VersionInfo.Patch; |
||
278 | ToNaviCtrl.Param.Byte[3] = SPI_VersionInfo.Compatible; |
||
1180 | killagreg | 279 | ToNaviCtrl.Param.Byte[4] = BoardRelease; |
1078 | killagreg | 280 | break; |
281 | |||
282 | default: |
||
283 | break; |
||
886 | killagreg | 284 | } |
285 | |||
286 | |||
287 | sei(); // enable all interrupts |
||
288 | |||
289 | // analyze content of packet from NaviCtrl if valid |
||
290 | if (SPI_RxDataValid) |
||
291 | { |
||
292 | // update gps controls |
||
1180 | killagreg | 293 | if(abs(FromNaviCtrl.GPSStickNick) < 512 && abs(FromNaviCtrl.GPSStickRoll) < 512 && (ParamSet.GlobalConfig & CFG_GPS_ACTIVE)) |
886 | killagreg | 294 | { |
1180 | killagreg | 295 | GPSStickNick = FromNaviCtrl.GPSStickNick; |
296 | GPSStickRoll = FromNaviCtrl.GPSStickRoll; |
||
886 | killagreg | 297 | } |
298 | // update compass readings |
||
299 | if(FromNaviCtrl.CompassHeading <= 360) |
||
300 | { |
||
301 | CompassHeading = FromNaviCtrl.CompassHeading; |
||
302 | } |
||
303 | if(CompassHeading < 0) CompassOffCourse = 0; |
||
304 | else CompassOffCourse = ((540 + CompassHeading - CompassCourse) % 360) - 180; |
||
305 | // NaviCtrl wants to beep? |
||
306 | if (FromNaviCtrl.BeepTime > BeepTime && !CompassCalState) BeepTime = FromNaviCtrl.BeepTime; |
||
307 | |||
308 | switch (FromNaviCtrl.Command) |
||
309 | { |
||
1078 | killagreg | 310 | case SPI_KALMAN: |
1180 | killagreg | 311 | FCParam.KalmanK = FromNaviCtrl.Param.Byte[0]; |
312 | FCParam.KalmanMaxFusion = FromNaviCtrl.Param.Byte[1]; |
||
313 | FCParam.KalmanMaxDrift = FromNaviCtrl.Param.Byte[2]; |
||
1078 | killagreg | 314 | break; |
315 | |||
886 | killagreg | 316 | default: |
317 | break; |
||
318 | } |
||
319 | } |
||
320 | else // no valid data from NaviCtrl |
||
321 | { |
||
322 | // disable GPS control |
||
1180 | killagreg | 323 | GPSStickNick = 0; |
324 | GPSStickRoll = 0; |
||
886 | killagreg | 325 | } |
304 | ingob | 326 | } |
327 | |||
886 | killagreg | 328 | |
329 | |||
330 | /*********************************************/ |
||
331 | /* Start Transmission of packet to NaviCtrl */ |
||
332 | /*********************************************/ |
||
333 | void SPI_StartTransmitPacket(void) |
||
304 | ingob | 334 | { |
708 | ingob | 335 | |
886 | killagreg | 336 | if (!SPITransferCompleted) return; // return immediately if transfer is in progress |
337 | else // transmission was completed |
||
338 | { |
||
339 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // Select slave |
||
691 | ingob | 340 | |
886 | killagreg | 341 | // cyclic commands |
342 | ToNaviCtrl.Command = SPI_CommandSequence[SPI_CommandCounter++]; |
||
343 | if (SPI_CommandCounter >= sizeof(SPI_CommandSequence)) SPI_CommandCounter = 0; |
||
344 | |||
936 | killagreg | 345 | SPITransferCompleted = 0; // transfer is in progress |
886 | killagreg | 346 | UpdateSPI_Buffer(); // update data in ToNaviCtrl |
347 | |||
348 | SPI_TxBufferIndex = 1; //proceed with 2nd byte |
||
349 | |||
350 | // -- Debug-Output --- |
||
351 | //---- |
||
352 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
353 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
354 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
355 | ToNaviCtrl.Chksum = ToNaviCtrl.Sync1; // init checksum |
||
356 | SPDR = ToNaviCtrl.Sync1; // send first byte |
||
357 | } |
||
304 | ingob | 358 | } |
359 | |||
886 | killagreg | 360 | //------------------------------------------------------ |
361 | // This is the spi data transfer between FlightCtrl and NaviCtrl |
||
362 | // Every time this routine is called within the mainloop one byte of the packet to |
||
363 | // the NaviCtrl and one byte of the packet from the NaviCtrl is possible transfered |
||
691 | ingob | 364 | |
886 | killagreg | 365 | void SPI_TransmitByte(void) |
304 | ingob | 366 | { |
886 | killagreg | 367 | static SPI_RXState_t SPI_RXState = SPI_SYNC1; |
368 | uint8_t rxdata; |
||
369 | static uint8_t rxchksum; |
||
823 | ingob | 370 | |
886 | killagreg | 371 | if (SPITransferCompleted) return; // return immediatly if transfer was completed |
372 | if (!(SPSR & (1 << SPIF))) return; // return if no SPI-IRQ pending |
||
373 | SendSPI = 4; // mait 4 * 0.102 ms for the next call of SPI_TransmitByte() in the main loop |
||
823 | ingob | 374 | |
886 | killagreg | 375 | SLAVE_SELECT_PORT |= (1 << SPI_SLAVE_SELECT); // DeselectSlave |
823 | ingob | 376 | |
886 | killagreg | 377 | rxdata = SPDR; // save spi data register |
823 | ingob | 378 | |
886 | killagreg | 379 | switch (SPI_RXState) |
380 | { |
||
381 | case SPI_SYNC1: // first sync byte |
||
382 | SPI_RxBufferIndex = 0; // set pointer to start of rx buffer |
||
383 | rxchksum = rxdata; // initialize checksum |
||
384 | if (rxdata == SPI_RXSYNCBYTE1 ) |
||
385 | { // 1st Syncbyte found |
||
386 | SPI_RXState = SPI_SYNC2; // trigger to state for second sync byte |
||
387 | } |
||
388 | break; |
||
823 | ingob | 389 | |
886 | killagreg | 390 | case SPI_SYNC2: // second sync byte |
391 | if (rxdata == SPI_RXSYNCBYTE2) |
||
392 | { // 2nd Syncbyte found |
||
393 | rxchksum += rxdata; // update checksum |
||
394 | SPI_RXState = SPI_DATA; // trigger to state for second sync byte |
||
395 | } |
||
396 | else // 2nd Syncbyte not found |
||
397 | { |
||
398 | SPI_RXState = SPI_SYNC1; // jump back to 1st sync byte |
||
399 | } |
||
400 | break; |
||
823 | ingob | 401 | |
886 | killagreg | 402 | case SPI_DATA: // data bytes |
403 | SPI_RxBuffer[SPI_RxBufferIndex++] = rxdata; // copy data byte to spi buffer |
||
404 | // if all bytes are received of a packet from the NaviCtrl |
||
405 | if (SPI_RxBufferIndex >= SPI_RXBUFFER_LEN) |
||
406 | { // last byte transfered is the checksum of the packet |
||
407 | if (rxdata == rxchksum) // checksum matching? |
||
408 | { |
||
409 | // copy SPI_RxBuffer -> FromFlightCtrl |
||
410 | uint8_t *ptr = (uint8_t *)&FromNaviCtrl; |
||
411 | cli(); |
||
412 | memcpy(ptr, (uint8_t *) SPI_RxBuffer, sizeof(FromNaviCtrl)); |
||
413 | sei(); |
||
414 | SPI_RxDataValid = 1; |
||
1078 | killagreg | 415 | //DebugOut.Analog[18]++; |
886 | killagreg | 416 | } |
417 | else |
||
418 | { // checksum does not match |
||
1078 | killagreg | 419 | //DebugOut.Analog[17]++; |
886 | killagreg | 420 | SPI_RxDataValid = 0; // reset valid flag |
421 | } |
||
422 | SPI_RXState = SPI_SYNC1; // reset state sync |
||
423 | } |
||
424 | else // not all bytes transfered |
||
425 | { |
||
426 | rxchksum += rxdata; // update checksum |
||
427 | } |
||
428 | break; |
||
429 | }// eof switch(SPI_RXState) |
||
430 | |||
431 | // if still some bytes left for transmission to NaviCtrl |
||
432 | if (SPI_TxBufferIndex < SPI_TXBUFFER_LEN) |
||
433 | { |
||
434 | SLAVE_SELECT_PORT &= ~(1 << SPI_SLAVE_SELECT); // SelectSlave |
||
435 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
436 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
437 | asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); asm volatile ("nop"); |
||
438 | |||
439 | SPDR = SPI_TxBuffer[SPI_TxBufferIndex]; // transmit byte |
||
440 | ToNaviCtrl.Chksum += SPI_TxBuffer[SPI_TxBufferIndex]; // update checksum for everey byte that was sent |
||
441 | SPI_TxBufferIndex++; |
||
442 | } |
||
443 | else |
||
444 | { |
||
445 | //Transfer of all bytes of the packet to NaviCtrl completed |
||
446 | SPITransferCompleted = 1; |
||
447 | } |
||
304 | ingob | 448 | } |
449 | |||
450 | |||
451 |