Rev 1852 | Rev 1856 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1852 | Rev 1855 | ||
---|---|---|---|
1 | // ######################## SPI - FlightCtrl ################### |
1 | // ######################## SPI - FlightCtrl ################### |
2 | #ifndef _SPI_H |
2 | #ifndef _SPI_H |
3 | #define _SPI_H |
3 | #define _SPI_H |
4 | 4 | ||
5 | #include <util/delay.h> |
5 | #include <util/delay.h> |
6 | #include "vector.h" |
6 | #include "vector.h" |
7 | 7 | ||
8 | #define USE_SPI_COMMUNICATION |
8 | #define USE_SPI_COMMUNICATION |
9 | 9 | ||
10 | //----------------------------------------- |
10 | //----------------------------------------- |
11 | #define DDR_SPI DDRB |
11 | #define DDR_SPI DDRB |
12 | #define SLAVE_SELECT_DDR_PORT DDRC |
12 | #define SLAVE_SELECT_DDR_PORT DDRC |
13 | #define SLAVE_SELECT_PORT PORTC |
13 | #define SLAVE_SELECT_PORT PORTC |
14 | 14 | ||
15 | #if defined(__AVR_ATmega1284P__) |
15 | #if defined(__AVR_ATmega1284P__) |
16 | #define DD_SS PORTB4 |
16 | #define DD_SS PORTB4 |
17 | #define DD_SCK PORTB7 |
17 | #define DD_SCK PORTB7 |
18 | #define DD_MOSI PORTB5 |
18 | #define DD_MOSI PORTB5 |
19 | #define DD_MISO PORTB6 |
19 | #define DD_MISO PORTB6 |
20 | #define SPI_SLAVE_SELECT PORTC5 |
20 | #define SPI_SLAVE_SELECT PORTC5 |
21 | #else |
21 | #else |
22 | #define DD_SS PB4 |
22 | #define DD_SS PB4 |
23 | #define DD_SCK PB7 |
23 | #define DD_SCK PB7 |
24 | #define DD_MOSI PB5 |
24 | #define DD_MOSI PB5 |
25 | #define DD_MISO PB6 |
25 | #define DD_MISO PB6 |
26 | #define SPI_SLAVE_SELECT PC5 |
26 | #define SPI_SLAVE_SELECT PC5 |
27 | #endif |
27 | #endif |
28 | 28 | ||
29 | // for compatibility reasons gcc3.x <-> gcc4.x |
29 | // for compatibility reasons gcc3.x <-> gcc4.x |
30 | #ifndef SPCR |
30 | #ifndef SPCR |
31 | #define SPCR SPCR0 |
31 | #define SPCR SPCR0 |
32 | #endif |
32 | #endif |
33 | #ifndef SPE |
33 | #ifndef SPE |
34 | #define SPE SPE0 |
34 | #define SPE SPE0 |
35 | #endif |
35 | #endif |
36 | #ifndef MSTR |
36 | #ifndef MSTR |
37 | #define MSTR MSTR0 |
37 | #define MSTR MSTR0 |
38 | #endif |
38 | #endif |
39 | #ifndef SPR1 |
39 | #ifndef SPR1 |
40 | #define SPR1 SPR01 |
40 | #define SPR1 SPR01 |
41 | #endif |
41 | #endif |
42 | #ifndef SPR0 |
42 | #ifndef SPR0 |
43 | #define SPR0 SPR00 |
43 | #define SPR0 SPR00 |
44 | #endif |
44 | #endif |
45 | #ifndef SPIE |
45 | #ifndef SPIE |
46 | #define SPIE SPIE0 |
46 | #define SPIE SPIE0 |
47 | #endif |
47 | #endif |
48 | #ifndef SPDR |
48 | #ifndef SPDR |
49 | #define SPDR SPDR0 |
49 | #define SPDR SPDR0 |
50 | #endif |
50 | #endif |
51 | #ifndef SPIF |
51 | #ifndef SPIF |
52 | #define SPIF SPIF0 |
52 | #define SPIF SPIF0 |
53 | #endif |
53 | #endif |
54 | #ifndef SPSR |
54 | #ifndef SPSR |
55 | #define SPSR SPSR0 |
55 | #define SPSR SPSR0 |
56 | #endif |
56 | #endif |
57 | // ------------------------- |
57 | // ------------------------- |
58 | 58 | ||
59 | #define SPI_FCCMD_USER 10 |
59 | #define SPI_FCCMD_USER 10 |
60 | #define SPI_FCCMD_STICK 11 |
60 | #define SPI_FCCMD_STICK 11 |
61 | #define SPI_FCCMD_MISC 12 |
61 | #define SPI_FCCMD_MISC 12 |
62 | #define SPI_FCCMD_PARAMETER1 13 |
62 | #define SPI_FCCMD_PARAMETER1 13 |
63 | #define SPI_FCCMD_VERSION 14 |
63 | #define SPI_FCCMD_VERSION 14 |
64 | #define SPI_FCCMD_SERVOS 15 |
64 | #define SPI_FCCMD_SERVOS 15 |
65 | #define SPI_FCCMD_ACCU 16 |
65 | #define SPI_FCCMD_ACCU 16 |
66 | 66 | ||
67 | struct str_ToNaviCtrl |
67 | struct str_ToNaviCtrl |
68 | { |
68 | { |
69 | unsigned char Sync1, Sync2; |
69 | unsigned char Sync1, Sync2; |
70 | unsigned char Command; |
70 | unsigned char Command; |
71 | signed int IntegralNick; |
71 | signed int IntegralNick; |
72 | signed int IntegralRoll; |
72 | signed int IntegralRoll; |
73 | signed int AccNick; |
73 | signed int AccNick; |
74 | signed int AccRoll; |
74 | signed int AccRoll; |
75 | signed int GyroCompass; |
75 | signed int GyroCompass; |
76 | signed int GyroNick; |
76 | signed int GyroNick; |
77 | signed int GyroRoll; |
77 | signed int GyroRoll; |
78 | signed int GyroGier; |
78 | signed int GyroGier; |
79 | unsigned int FCStatus; |
79 | unsigned int FCStatus; |
80 | union |
80 | union |
81 | { |
81 | { |
82 | unsigned char Byte[12]; |
82 | unsigned char Byte[12]; |
83 | char sByte[12]; |
83 | char sByte[12]; |
84 | unsigned int Int[6]; |
84 | unsigned int Int[6]; |
85 | int sInt[6]; |
85 | int sInt[6]; |
86 | unsigned long Long[3]; |
86 | unsigned long Long[3]; |
87 | long sLong[3]; |
87 | long sLong[3]; |
88 | float Float[3]; |
88 | float Float[3]; |
89 | } Param; |
89 | } Param; |
90 | unsigned char Chksum; |
90 | unsigned char Chksum; |
91 | }; |
91 | }; |
92 | 92 | ||
93 | #define SPI_NCCMD_KALMAN 103 |
93 | #define SPI_NCCMD_KALMAN 103 |
94 | #define SPI_NCCMD_VERSION 104 |
94 | #define SPI_NCCMD_VERSION 104 |
95 | #define SPI_NCCMD_GPSINFO 105 |
95 | #define SPI_NCCMD_GPSINFO 105 |
96 | 96 | ||
97 | struct str_FromNaviCtrl |
97 | struct str_FromNaviCtrl |
98 | { |
98 | { |
99 | unsigned char Command; |
99 | unsigned char Command; |
100 | signed int GPS_Nick; |
100 | signed int GPS_Nick; |
101 | signed int GPS_Roll; |
101 | signed int GPS_Roll; |
102 | signed int GPS_Gier; |
102 | signed int GPS_Gier; |
103 | signed int CompassValue; |
103 | signed int CompassValue; |
104 | signed int MagVecX; |
104 | // signed int MagVecX; |
105 | signed int MagVecY; |
105 | // signed int MagVecY; |
106 | signed int MagVecZ; |
106 | // signed int MagVecZ; |
107 | signed int Status; |
107 | signed int Status; |
108 | unsigned int BeepTime; |
108 | unsigned int BeepTime; |
109 | union |
109 | union |
110 | { |
110 | { |
111 | unsigned char Byte[12]; |
111 | unsigned char Byte[12]; |
112 | char sByte[12]; |
112 | char sByte[12]; |
113 | unsigned int Int[6]; |
113 | unsigned int Int[6]; |
114 | int sInt[6]; |
114 | int sInt[6]; |
115 | unsigned long Long[3]; |
115 | unsigned long Long[3]; |
116 | long sLong[3]; |
116 | long sLong[3]; |
117 | float Float[3]; |
117 | float Float[3]; |
118 | } Param; |
118 | } Param; |
119 | unsigned char Chksum; |
119 | unsigned char Chksum; |
120 | }; |
120 | }; |
121 | 121 | ||
122 | struct str_FromNaviCtrl_Value |
122 | struct str_FromNaviCtrl_Value |
123 | { |
123 | { |
124 | signed char Kalman_K; |
124 | signed char Kalman_K; |
125 | signed char Kalman_MaxDrift; |
125 | signed char Kalman_MaxDrift; |
126 | signed char Kalman_MaxFusion; |
126 | signed char Kalman_MaxFusion; |
127 | unsigned char SerialDataOkay; |
127 | unsigned char SerialDataOkay; |
128 | signed char GpsZ; |
128 | signed char GpsZ; |
129 | }; |
129 | }; |
130 | 130 | ||
131 | struct str_SPI_VersionInfo |
131 | struct str_SPI_VersionInfo |
132 | { |
132 | { |
133 | unsigned char Major; |
133 | unsigned char Major; |
134 | unsigned char Minor; |
134 | unsigned char Minor; |
135 | unsigned char Patch; |
135 | unsigned char Patch; |
136 | unsigned char Compatible; |
136 | unsigned char Compatible; |
137 | unsigned char Hardware; |
137 | unsigned char Hardware; |
138 | }; |
138 | }; |
139 | 139 | ||
140 | // Satfix types for GPSData.SatFix |
140 | // Satfix types for GPSData.SatFix |
141 | #define SATFIX_NONE 0x00 |
141 | #define SATFIX_NONE 0x00 |
142 | #define SATFIX_DEADRECKOING 0x01 |
142 | #define SATFIX_DEADRECKOING 0x01 |
143 | #define SATFIX_2D 0x02 |
143 | #define SATFIX_2D 0x02 |
144 | #define SATFIX_3D 0x03 |
144 | #define SATFIX_3D 0x03 |
145 | #define SATFIX_GPS_DEADRECKOING 0x04 |
145 | #define SATFIX_GPS_DEADRECKOING 0x04 |
146 | #define SATFIX_TIMEONLY 0x05 |
146 | #define SATFIX_TIMEONLY 0x05 |
147 | // Flags for interpretation of the GPSData.Flags |
147 | // Flags for interpretation of the GPSData.Flags |
148 | #define FLAG_GPSFIXOK 0x01 // (i.e. within DOP & ACC Masks) |
148 | #define FLAG_GPSFIXOK 0x01 // (i.e. within DOP & ACC Masks) |
149 | #define FLAG_DIFFSOLN 0x02 // (is DGPS used) |
149 | #define FLAG_DIFFSOLN 0x02 // (is DGPS used) |
150 | #define FLAG_WKNSET 0x04 // (is Week Number valid) |
150 | #define FLAG_WKNSET 0x04 // (is Week Number valid) |
151 | #define FLAG_TOWSET 0x08 // (is Time of Week valid) |
151 | #define FLAG_TOWSET 0x08 // (is Time of Week valid) |
152 | 152 | ||
153 | struct str_GPSInfo |
153 | struct str_GPSInfo |
154 | { |
154 | { |
155 | unsigned char Flags; // Status Flags |
155 | unsigned char Flags; // Status Flags |
156 | unsigned char NumOfSats; // number of satelites |
156 | unsigned char NumOfSats; // number of satelites |
157 | unsigned char SatFix; // type of satfix |
157 | unsigned char SatFix; // type of satfix |
158 | unsigned char Speed; // m/sek |
158 | unsigned char Speed; // m/sek |
159 | unsigned int HomeDistance; // distance to Home in dm |
159 | unsigned int HomeDistance; // distance to Home in dm |
160 | int HomeBearing; // bearing to home in deg |
160 | int HomeBearing; // bearing to home in deg |
161 | }; |
161 | }; |
162 | 162 | ||
163 | 163 | ||
164 | #ifdef USE_SPI_COMMUNICATION |
164 | #ifdef USE_SPI_COMMUNICATION |
165 | extern struct str_GPSInfo GPSInfo; |
165 | extern struct str_GPSInfo GPSInfo; |
166 | extern struct str_SPI_VersionInfo NC_Version; |
166 | extern struct str_SPI_VersionInfo NC_Version; |
167 | extern struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
167 | extern struct str_FromNaviCtrl_Value FromNaviCtrl_Value; |
168 | extern struct str_ToNaviCtrl ToNaviCtrl; |
168 | extern struct str_ToNaviCtrl ToNaviCtrl; |
169 | extern struct str_FromNaviCtrl FromNaviCtrl; |
169 | extern struct str_FromNaviCtrl FromNaviCtrl; |
170 | extern unsigned char SPI_CommandCounter,NaviDataOkay; |
170 | extern unsigned char SPI_CommandCounter,NaviDataOkay; |
171 | extern signed char FromNC_Rotate_C, FromNC_Rotate_S; |
171 | extern signed char FromNC_Rotate_C, FromNC_Rotate_S; |
172 | extern unsigned char NC_ErrorCode; |
172 | extern unsigned char NC_ErrorCode; |
173 | extern void SPI_MasterInit(void); |
173 | extern void SPI_MasterInit(void); |
174 | extern void SPI_StartTransmitPacket(void); |
174 | extern void SPI_StartTransmitPacket(void); |
175 | extern void UpdateSPI_Buffer(void); |
175 | extern void UpdateSPI_Buffer(void); |
176 | extern void SPI_TransmitByte(void); |
176 | extern void SPI_TransmitByte(void); |
177 | extern signed int POI_KameraNick; |
177 | extern signed int POI_KameraNick; |
178 | extern vector16_t MagVec; |
178 | extern vector16_t MagVec; |
179 | #else |
179 | #else |
180 | 180 | ||
181 | 181 | ||
182 | // -------------------------------- Dummy ----------------------------------------- |
182 | // -------------------------------- Dummy ----------------------------------------- |
183 | #define SPI_MasterInit() ; |
183 | #define SPI_MasterInit() ; |
184 | #define SPI_StartTransmitPacket() ; |
184 | #define SPI_StartTransmitPacket() ; |
185 | #define UpdateSPI_Buffer() ; |
185 | #define UpdateSPI_Buffer() ; |
186 | #define SPI_TransmitByte() ; |
186 | #define SPI_TransmitByte() ; |
187 | #endif |
187 | #endif |
188 | 188 | ||
189 | 189 | ||
190 | #endif |
190 | #endif |
191 | 191 |