Rev 287 | Rev 292 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 287 | Rev 288 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "mk3mag.h" |
59 | #include "mk3mag.h" |
60 | #include "i2c.h" |
60 | #include "i2c.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | #include "led.h" |
62 | #include "led.h" |
63 | #include "spi_slave.h" |
63 | #include "spi_slave.h" |
64 | #include "main.h" |
64 | #include "main.h" |
65 | #include "uart1.h" |
65 | #include "uart1.h" |
66 | #include "compass.h" |
66 | #include "compass.h" |
67 | 67 | ||
68 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
68 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
69 | 69 | ||
70 | u8 MK3MAG_Present = 0; |
70 | u8 MK3MAG_Present = 0; |
71 | 71 | ||
72 | typedef struct |
72 | typedef struct |
73 | { |
73 | { |
74 | s16 Nick; |
74 | s16 Nick; |
75 | s16 Roll; |
75 | s16 Roll; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
77 | 77 | ||
78 | // Transfer buffers |
78 | // Transfer buffers |
79 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
79 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
80 | volatile MK3MAG_Version_t MK3MAG_Version; |
80 | volatile MK3MAG_Version_t MK3MAG_Version; |
81 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
81 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
82 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
82 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
83 | 83 | ||
84 | // ------------------------------------------------------------------------------- |
84 | // ------------------------------------------------------------------------------- |
85 | // the calculation of the MK3MAG packet checksum |
85 | // the calculation of the MK3MAG packet checksum |
86 | 86 | ||
87 | // calculate the crc of bytecount bytes in buffer |
87 | // calculate the crc of bytecount bytes in buffer |
88 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
88 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
89 | { |
89 | { |
90 | u8 i, crc = 0; |
90 | u8 i, crc = 0; |
91 | for(i=0; i < bytecount; i++) |
91 | for(i=0; i < bytecount; i++) |
92 | { |
92 | { |
93 | crc += pBuffer[i]; |
93 | crc += pBuffer[i]; |
94 | } |
94 | } |
95 | crc = ~crc; |
95 | crc = ~crc; |
96 | return crc; |
96 | return crc; |
97 | } |
97 | } |
98 | 98 | ||
99 | // assuming the last byte in buffer is the crc byte |
99 | // assuming the last byte in buffer is the crc byte |
100 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
100 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
101 | { |
101 | { |
102 | u8 crc = 0, retval = 0; |
102 | u8 crc = 0, retval = 0; |
103 | if(BuffLen == 0) return(retval); |
103 | if(BuffLen == 0) return(retval); |
104 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
104 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
105 | if(crc == pBuffer[BuffLen-1]) |
105 | if(crc == pBuffer[BuffLen-1]) |
106 | { |
106 | { |
107 | retval = 1; |
107 | retval = 1; |
108 | DebugOut.Analog[15]++; // count mk3mag ok |
108 | DebugOut.Analog[15]++; // count mk3mag ok |
109 | } |
109 | } |
110 | else |
110 | else |
111 | { |
111 | { |
112 | retval = 0; |
112 | retval = 0; |
113 | DebugOut.Analog[14]++; // count mk3mag crc error |
113 | DebugOut.Analog[14]++; // count mk3mag crc error |
114 | } |
114 | } |
115 | return(retval); |
115 | return(retval); |
116 | } |
116 | } |
117 | 117 | ||
118 | // ------------------------------------------------------------------------------- |
118 | // ------------------------------------------------------------------------------- |
119 | // the I2C rx data handler functions |
119 | // the I2C rx data handler functions |
120 | 120 | ||
121 | // rx data handler for version info request |
121 | // rx data handler for version info request |
122 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
122 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
123 | { // if crc is ok and number of byte are matching |
123 | { // if crc is ok and number of byte are matching |
124 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
124 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
125 | { |
125 | { |
126 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
126 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
127 | } |
127 | } |
128 | } |
128 | } |
129 | 129 | ||
130 | // rx data handler for calibration request |
130 | // rx data handler for calibration request |
131 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
131 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
132 | { // if crc is ok and number of byte are matching |
132 | { // if crc is ok and number of byte are matching |
133 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
133 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
134 | { |
134 | { |
135 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
135 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
136 | } |
136 | } |
137 | } |
137 | } |
138 | 138 | ||
139 | // rx data handler for magnetic vector request |
139 | // rx data handler for magnetic vector request |
140 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
140 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
141 | { // if crc is ok and number of byte are matching |
141 | { // if crc is ok and number of byte are matching |
142 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
142 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
143 | { |
143 | { |
144 | memcpy((u8 *)&MagVector, pRxBuffer, sizeof(MagVector)); |
144 | memcpy((u8 *)&MagVector, pRxBuffer, sizeof(MagVector)); |
145 | } |
145 | } |
146 | } |
146 | } |
147 | 147 | ||
148 | // rx data handler for heading request |
148 | // rx data handler for heading request |
149 | void MK3MAG_UpdateHeading(u8* pRxBuffer, u8 RxBufferSize) |
149 | void MK3MAG_UpdateHeading(u8* pRxBuffer, u8 RxBufferSize) |
150 | { // if crc is ok and number of byte are matching |
150 | { // if crc is ok and number of byte are matching |
151 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(Compass_Heading)+1)) ) |
151 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(Compass_Heading)+1)) ) |
152 | { |
152 | { |
153 | memcpy((u8 *)&Compass_Heading, pRxBuffer, sizeof(Compass_Heading)); |
153 | memcpy((u8 *)&Compass_Heading, pRxBuffer, sizeof(Compass_Heading)); |
154 | } |
154 | } |
155 | } |
155 | } |
156 | 156 | ||
157 | //---------------------------------------------------------------- |
157 | //---------------------------------------------------------------- |
158 | void MK3MAG_SendCommand(u8 command) |
158 | void MK3MAG_SendCommand(u8 command) |
159 | { |
159 | { |
160 | // trya to catch the I2C buffer |
160 | // try to catch the I2C buffer |
161 | if(I2C_LockBuffer(0)) |
161 | if(I2C_LockBuffer(0)) |
162 | { |
162 | { |
163 | u16 TxBytes = 0; |
163 | u16 TxBytes = 0; |
164 | u16 RxBytes = 0; |
164 | u16 RxBytes = 0; |
165 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
165 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
166 | 166 | ||
167 | // update current command id |
167 | // update current command id |
168 | I2C_Buffer[TxBytes++] = command; |
168 | I2C_Buffer[TxBytes++] = command; |
169 | 169 | ||
170 | // set pointers to data area with respect to the command id |
170 | // set pointers to data area with respect to the command id |
171 | switch (command) |
171 | switch (command) |
172 | { |
172 | { |
173 | case MK3MAG_CMD_VERSION: |
173 | case MK3MAG_CMD_VERSION: |
174 | RxBytes = sizeof(MK3MAG_Version)+1; |
174 | RxBytes = sizeof(MK3MAG_Version)+1; |
175 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
175 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
176 | break; |
176 | break; |
177 | case MK3MAG_CMD_WRITE_CAL: |
177 | case MK3MAG_CMD_WRITE_CAL: |
178 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
178 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
179 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
179 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
180 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
180 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
181 | TxBytes += sizeof(MK3MAG_WriteCal); |
181 | TxBytes += sizeof(MK3MAG_WriteCal); |
182 | break; |
182 | break; |
183 | case MK3MAG_CMD_READ_MAGVECT: |
183 | case MK3MAG_CMD_READ_MAGVECT: |
184 | RxBytes = sizeof(MagVector)+1; |
184 | RxBytes = sizeof(MagVector)+1; |
185 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
185 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
186 | break; |
186 | break; |
187 | case MK3MAG_CMD_READ_HEADING: |
187 | case MK3MAG_CMD_READ_HEADING: |
188 | RxBytes = sizeof(Compass_Heading)+1; |
188 | RxBytes = sizeof(Compass_Heading)+1; |
189 | pRxHandlerFunc = &MK3MAG_UpdateHeading; |
189 | pRxHandlerFunc = &MK3MAG_UpdateHeading; |
190 | // update attitude from spi rx buffer |
190 | // update attitude from spi rx buffer |
191 | VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy |
191 | VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy |
192 | MK3MAG_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
192 | MK3MAG_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
193 | MK3MAG_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
193 | MK3MAG_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
194 | VIC_ITCmd(SSP0_ITLine, ENABLE); |
194 | VIC_ITCmd(SSP0_ITLine, ENABLE); |
195 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteAttitude, sizeof(MK3MAG_WriteAttitude)); |
195 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteAttitude, sizeof(MK3MAG_WriteAttitude)); |
196 | TxBytes += sizeof(MK3MAG_WriteAttitude); |
196 | TxBytes += sizeof(MK3MAG_WriteAttitude); |
197 | break; |
197 | break; |
198 | default: // unknown command id |
198 | default: // unknown command id |
199 | RxBytes = 0; |
199 | RxBytes = 0; |
200 | pRxHandlerFunc = NULL; |
200 | pRxHandlerFunc = NULL; |
201 | break; |
201 | break; |
202 | } |
202 | } |
203 | // update packet checksum |
203 | // update packet checksum |
204 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
204 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
205 | TxBytes++; |
205 | TxBytes++; |
206 | // initiate I2C transmission |
206 | // initiate I2C transmission |
207 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
207 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
208 | } // EOF I2C_State == I2C_IDLE |
208 | } // EOF I2C_State == I2C_IDLE |
209 | } |
209 | } |
210 | 210 | ||
211 | 211 | ||
212 | //---------------------------------------------------------------- |
212 | //---------------------------------------------------------------- |
213 | u8 MK3MAG_Init(void) |
213 | u8 MK3MAG_Init(void) |
214 | { |
214 | { |
215 | u8 msg[64]; |
215 | u8 msg[64]; |
216 | u8 repeat; |
216 | u8 repeat; |
217 | u32 timeout; |
217 | u32 timeout; |
218 | 218 | ||
219 | MK3MAG_Present = 0; |
219 | MK3MAG_Present = 0; |
220 | 220 | ||
221 | MK3MAG_Version.Major = 0xFF; |
221 | MK3MAG_Version.Major = 0xFF; |
222 | MK3MAG_Version.Minor = 0xFF; |
222 | MK3MAG_Version.Minor = 0xFF; |
223 | MK3MAG_Version.Patch = 0xFF; |
223 | MK3MAG_Version.Patch = 0xFF; |
224 | MK3MAG_Version.Compatible = 0xFF; |
224 | MK3MAG_Version.Compatible = 0xFF; |
225 | 225 | ||
226 | Compass_Heading = -1; |
226 | Compass_Heading = -1; |
227 | 227 | ||
228 | // polling of version info |
228 | // polling of version info |
229 | repeat = 0; |
229 | repeat = 0; |
230 | do |
230 | do |
231 | { |
231 | { |
232 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
232 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
233 | timeout = SetDelay(250); |
233 | timeout = SetDelay(250); |
234 | do |
234 | do |
235 | { |
235 | { |
236 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
236 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
237 | }while (!CheckDelay(timeout)); |
237 | }while (!CheckDelay(timeout)); |
238 | UART1_PutString("."); |
238 | UART1_PutString("."); |
239 | repeat++; |
239 | repeat++; |
240 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
240 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
241 | // if we got it |
241 | // if we got it |
242 | if (MK3MAG_Version.Major != 0xFF) |
242 | if (MK3MAG_Version.Major != 0xFF) |
243 | { |
243 | { |
244 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
244 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
245 | UART1_PutString(msg); |
245 | UART1_PutString(msg); |
246 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
246 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
247 | { |
247 | { |
248 | UART1_PutString("\n\r MK3MAG not compatible!"); |
248 | UART1_PutString("\n\r MK3MAG not compatible!"); |
249 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
249 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
250 | LED_RED_ON; |
250 | LED_RED_ON; |
251 | } |
251 | } |
252 | else |
252 | else |
253 | { // version ok |
253 | { // version ok |
254 | MK3MAG_Present = 1; |
254 | MK3MAG_Present = 1; |
255 | } |
255 | } |
256 | } |
256 | } |
257 | return(MK3MAG_Present); |
257 | return(MK3MAG_Present); |
258 | } |
258 | } |
259 | 259 | ||
260 | //---------------------------------------------------------------- |
260 | //---------------------------------------------------------------- |
261 | void MK3MAG_UpdateCompass(void) |
261 | void MK3MAG_UpdateCompass(void) |
262 | { |
262 | { |
263 | static u32 TimerCompassUpdate = 0; |
263 | static u32 TimerCompassUpdate = 0; |
264 | static s32 x_max,y_max,z_max,x_min,y_min,z_min; |
264 | static s32 x_max,y_max,z_max,x_min,y_min,z_min; |
265 | 265 | ||
266 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
266 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
267 | 267 | ||
268 | if(CheckDelay(TimerCompassUpdate)) |
268 | if(CheckDelay(TimerCompassUpdate)) |
269 | { |
269 | { |
270 | // check for incomming compass calibration request |
270 | // check for incomming compass calibration request |
271 | Compass_UpdateCalState(); |
271 | Compass_UpdateCalState(); |
272 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
272 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
273 | // send new calstate |
273 | // send new calstate |
274 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
274 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
275 | { |
275 | { |
276 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
276 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
277 | x_max = -30000; y_max = -30000; z_max = -30000; |
277 | x_max = -30000; y_max = -30000; z_max = -30000; |
278 | x_min = 30000; y_min = 30000; z_min = 30000; |
278 | x_min = 30000; y_min = 30000; z_min = 30000; |
279 | } |
279 | } |
280 | if(MK3MAG_WriteCal.CalByte == 2) |
280 | if(MK3MAG_WriteCal.CalByte == 2) |
281 | { |
281 | { |
282 | Compass_UpdateMagVector(); |
282 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); |
283 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 30; }; |
283 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 30; }; |
284 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 30; }; |
284 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 30; }; |
285 | if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 30; }; |
285 | if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 30; }; |
286 | if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 30; }; |
286 | if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 30; }; |
287 | } |
287 | } |
288 | if(MK3MAG_WriteCal.CalByte == 4) |
288 | if(MK3MAG_WriteCal.CalByte == 4) |
289 | { |
289 | { |
290 | Compass_UpdateMagVector(); |
290 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); |
291 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 30; }; |
291 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 30; }; |
292 | if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 30; }; |
292 | if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 30; }; |
293 | } |
293 | } |
294 | else // request current heading |
294 | else // request current heading |
295 | { |
295 | { |
296 | MK3MAG_SendCommand(MK3MAG_CMD_READ_HEADING); |
296 | MK3MAG_SendCommand(MK3MAG_CMD_READ_HEADING); |
297 | } |
297 | } |
298 | TimerCompassUpdate = SetDelay(20); // every 20 ms are 50 Hz |
298 | TimerCompassUpdate = SetDelay(20); // every 20 ms are 50 Hz |
299 | } |
299 | } |
300 | } |
300 | } |
301 | 301 |