Rev 488 | Rev 492 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 488 | Rev 489 | ||
---|---|---|---|
Line 5... | Line 5... | ||
5 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | // + Software Nutzungsbedingungen (english version: see below) |
7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
22 | // + des Mitverschuldens offen. |
22 | // + des Mitverschuldens offen. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
Line 30... | Line 30... | ||
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Software LICENSING TERMS |
32 | // + Software LICENSING TERMS |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
37 | // + The Software may only be used with the Licensor's products. |
37 | // + The Software may only be used with the Licensor's products. |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
40 | // + agreement shall be the property of the Licensor. |
40 | // + agreement shall be the property of the Licensor. |
Line 53... | Line 53... | ||
53 | // + #### END OF LICENSING TERMS #### |
53 | // + #### END OF LICENSING TERMS #### |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | #include <math.h> |
56 | #include <math.h> |
57 | #include <stdio.h> |
57 | #include <stdio.h> |
- | 58 | #include <stdlib.h> |
|
58 | #include <string.h> |
59 | #include <string.h> |
59 | #include "91x_lib.h" |
60 | #include "91x_lib.h" |
60 | #include "ncmag.h" |
61 | #include "ncmag.h" |
61 | #include "i2c1.h" |
- | |
62 | #include "i2c0.h" |
62 | #include "i2c.h" |
63 | - | ||
64 | #include "timer1.h" |
63 | #include "timer1.h" |
65 | #include "led.h" |
64 | #include "led.h" |
66 | #include "uart1.h" |
65 | #include "uart1.h" |
67 | #include "eeprom.h" |
66 | #include "eeprom.h" |
68 | #include "mymath.h" |
67 | #include "mymath.h" |
Line 70... | Line 69... | ||
70 | #include "spi_slave.h" |
69 | #include "spi_slave.h" |
Line 71... | Line 70... | ||
71 | 70 | ||
72 | u8 NCMAG_Present = 0; |
71 | u8 NCMAG_Present = 0; |
Line 73... | Line -... | ||
73 | u8 NCMAG_IsCalibrated = 0; |
- | |
74 | - | ||
75 | u8 I2C_CompassPort = 1; |
- | |
76 | u8 ExtCompassOrientation = 0; |
- | |
77 | - | ||
78 | u8 *I2C_BufferPnt; |
- | |
79 | u8 *I2C_ErrorPnt; |
- | |
80 | I2C_TransmissionFunc_t I2C_TransmissionFunc; |
- | |
81 | I2C_LockBufferFunc_t I2C_LockBufferFunc; |
- | |
Line 82... | Line 72... | ||
82 | I2C_WaitForEndOfTransmissionFunc_t I2C_WaitForEndOfTransmissionFunc; |
72 | u8 NCMAG_IsCalibrated = 0; |
83 | 73 | ||
84 | 74 | ||
85 | // supported magnetic sensor types |
75 | // supported magnetic sensor types |
86 | #define TYPE_NONE 0 |
76 | #define TYPE_NONE 0 |
Line 87... | Line 77... | ||
87 | #define TYPE_HMC5843 1 |
77 | #define TYPE_HMC5843 1 |
- | 78 | #define TYPE_LSM303DLH 2 |
|
- | 79 | #define TYPE_LSM303DLM 3 |
|
- | 80 | ||
- | 81 | u8 NCMAG_SensorType = TYPE_NONE; |
|
- | 82 | u8 NCMAG_Orientation = 0; // 0 means unknown! |
|
- | 83 | ||
- | 84 | // two calibrtion sets for extern and intern sensor |
|
- | 85 | #define EEPROM_ADR_MAG_CALIBRATION_INTERN 50 |
|
- | 86 | #define EEPROM_ADR_MAG_CALIBRATION_EXTERN 70 |
|
- | 87 | ||
- | 88 | #define CALIBRATION_VERSION 1 |
|
- | 89 | #define MAG_CALIBRATION_COMPATIBLE 0xA2 |
|
- | 90 | ||
- | 91 | #define NCMAG_MIN_RAWVALUE -2047 |
|
- | 92 | #define NCMAG_MAX_RAWVALUE 2047 |
|
- | 93 | #define NCMAG_INVALID_DATA -4096 |
|
- | 94 | ||
- | 95 | typedef struct |
|
- | 96 | { |
|
- | 97 | s16 Range; |
|
- | 98 | s16 Offset; |
|
- | 99 | } __attribute__((packed)) Scaling_t; |
|
- | 100 | ||
- | 101 | typedef struct |
|
- | 102 | { |
|
- | 103 | Scaling_t MagX; |
|
- | 104 | Scaling_t MagY; |
|
Line 88... | Line 105... | ||
88 | #define TYPE_LSM303DLH 2 |
105 | Scaling_t MagZ; |
89 | #define TYPE_LSM303DLM 3 |
106 | u8 Version; |
90 | 107 | u8 crc; |
|
Line 91... | Line 108... | ||
91 | u8 NCMAG_SensorType = TYPE_NONE; |
108 | } __attribute__((packed)) Calibration_t; |
92 | 109 | ||
Line 150... | Line 167... | ||
150 | #define HMC5843_CRA_RATE_20HZ 0x14 |
167 | #define HMC5843_CRA_RATE_20HZ 0x14 |
151 | #define HMC5843_CRA_RATE_50HZ 0x18 |
168 | #define HMC5843_CRA_RATE_50HZ 0x18 |
152 | // bit mask for gain |
169 | // bit mask for gain |
153 | #define HMC5843_CRB_GAIN_07GA 0x00 |
170 | #define HMC5843_CRB_GAIN_07GA 0x00 |
154 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
171 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
155 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
172 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
156 | #define HMC5843_CRB_GAIN_20GA 0x60 |
173 | #define HMC5843_CRB_GAIN_20GA 0x60 |
157 | #define HMC5843_CRB_GAIN_32GA 0x80 |
174 | #define HMC5843_CRB_GAIN_32GA 0x80 |
158 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
175 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
159 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
176 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
160 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
177 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
Line 206... | Line 223... | ||
206 | u8 mode; |
223 | u8 mode; |
207 | } __attribute__((packed)) MagConfig_t; |
224 | } __attribute__((packed)) MagConfig_t; |
Line 208... | Line 225... | ||
208 | 225 | ||
Line 209... | Line -... | ||
209 | volatile MagConfig_t MagConfig; |
- | |
210 | - | ||
211 | - | ||
212 | - | ||
213 | 226 | volatile MagConfig_t MagConfig; |
|
214 | 227 | ||
215 | // self test value |
228 | // self test value |
216 | #define LSM303DLH_TEST_XSCALE 495 |
229 | #define LSM303DLH_TEST_XSCALE 495 |
217 | #define LSM303DLH_TEST_YSCALE 495 |
230 | #define LSM303DLH_TEST_YSCALE 495 |
Line 251... | Line 264... | ||
251 | // Output data rate in normal power mode |
264 | // Output data rate in normal power mode |
252 | #define ACC_CRTL1_DR_50HZ 0x00 |
265 | #define ACC_CRTL1_DR_50HZ 0x00 |
253 | #define ACC_CRTL1_DR_100HZ 0x08 |
266 | #define ACC_CRTL1_DR_100HZ 0x08 |
254 | #define ACC_CRTL1_DR_400HZ 0x10 |
267 | #define ACC_CRTL1_DR_400HZ 0x10 |
255 | #define ACC_CRTL1_DR_1000HZ 0x18 |
268 | #define ACC_CRTL1_DR_1000HZ 0x18 |
256 | // axis anable flags |
269 | // axis anable flags |
257 | #define ACC_CRTL1_XEN 0x01 |
270 | #define ACC_CRTL1_XEN 0x01 |
258 | #define ACC_CRTL1_YEN 0x02 |
271 | #define ACC_CRTL1_YEN 0x02 |
259 | #define ACC_CRTL1_ZEN 0x04 |
272 | #define ACC_CRTL1_ZEN 0x04 |
Line 260... | Line 273... | ||
260 | 273 | ||
Line 284... | Line 297... | ||
284 | u8 ctrl_5; |
297 | u8 ctrl_5; |
285 | } __attribute__((packed)) AccConfig_t; |
298 | } __attribute__((packed)) AccConfig_t; |
Line 286... | Line 299... | ||
286 | 299 | ||
Line 287... | Line 300... | ||
287 | volatile AccConfig_t AccConfig; |
300 | volatile AccConfig_t AccConfig; |
- | 301 | ||
288 | 302 | // write calibration data for external and internal sensor seperately |
|
289 | u8 NCMag_CalibrationWrite(u8 intern) // two calibrtion sets for extern and intern sensor |
303 | u8 NCMag_CalibrationWrite(I2C_TypeDef* I2Cx) |
290 | { |
304 | { |
291 | u16 address; |
305 | u16 address; |
292 | u8 i, crc = MAG_CALIBRATION_COMPATIBLE; |
306 | u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE; |
293 | EEPROM_Result_t eres; |
- | |
Line 294... | Line 307... | ||
294 | u8 *pBuff = (u8*)&Calibration; |
307 | EEPROM_Result_t eres; |
295 | Calibration.Version = CALIBRATION_VERSION; |
- | |
296 | 308 | u8 *pBuff = (u8*)&Calibration; |
|
297 | if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
309 | |
298 | else |
310 | if (I2Cx == NCMAG_PORT_EXTERN) |
299 | { |
311 | { |
- | 312 | address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
|
- | 313 | Calibration.Version = CALIBRATION_VERSION + (NCMAG_Orientation<<4);; |
|
- | 314 | } |
|
- | 315 | else if (I2Cx == NCMAG_PORT_INTERN) |
|
- | 316 | { |
|
- | 317 | address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
|
- | 318 | Calibration.Version = CALIBRATION_VERSION; |
|
300 | address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
319 | } |
301 | Calibration.Version = CALIBRATION_VERSION + ExtCompassOrientation * 16; |
320 | else return(i); |
302 | } |
321 | |
303 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
322 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
304 | { |
323 | { |
305 | crc += pBuff[i]; |
324 | crc += pBuff[i]; |
306 | } |
325 | } |
307 | Calibration.crc = ~crc; |
326 | Calibration.crc = ~crc; |
308 | eres = EEPROM_WriteBlock(address, pBuff, sizeof(Calibration)); |
327 | eres = EEPROM_WriteBlock(address, pBuff, sizeof(Calibration)); |
309 | if(EEPROM_SUCCESS == eres) i = 1; |
328 | if(EEPROM_SUCCESS == eres) i = 1; |
Line 310... | Line 329... | ||
310 | else i = 0; |
329 | else i = 0; |
- | 330 | return(i); |
|
311 | return(i); |
331 | } |
- | 332 | ||
312 | } |
333 | // read calibration data for external and internal sensor seperately |
313 | 334 | u8 NCMag_CalibrationRead(I2C_TypeDef* I2Cx) |
|
314 | u8 NCMag_CalibrationRead(u8 intern) // two calibrtion sets for extern and intern sensor |
- | |
Line 315... | Line 335... | ||
315 | { |
335 | { |
316 | u8 i, crc = MAG_CALIBRATION_COMPATIBLE; |
336 | u8 address; |
- | 337 | u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE; |
|
Line 317... | Line 338... | ||
317 | u8 *pBuff = (u8*)&Calibration; |
338 | u8 *pBuff = (u8*)&Calibration; |
318 | u16 address; |
339 | |
319 | 340 | if (I2Cx == NCMAG_PORT_EXTERN) address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
|
320 | if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
341 | else if (I2Cx == NCMAG_PORT_INTERN) address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
321 | else address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
342 | else return(0); |
322 | 343 | ||
323 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(address, pBuff, sizeof(Calibration))) |
344 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(address, pBuff, sizeof(Calibration))) |
324 | { |
345 | { |
325 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
346 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
326 | { |
347 | { |
327 | crc += pBuff[i]; |
348 | crc += pBuff[i]; |
328 | } |
349 | } |
Line 329... | Line 350... | ||
329 | crc = ~crc; |
350 | crc = ~crc; |
330 | if(Calibration.crc != crc) return(0); // crc mismatch |
351 | if(Calibration.crc != crc) return(0); // crc mismatch |
331 | if((Calibration.Version & 0x0f) == CALIBRATION_VERSION) return(1); |
352 | if((Calibration.Version & 0x0F) == CALIBRATION_VERSION) return(1); |
332 | } |
353 | } |
333 | return(0); |
354 | return(0); |
334 | } |
355 | } |
335 | 356 | ||
336 | 357 | ||
Line 337... | Line 358... | ||
337 | void NCMAG_Calibrate(void) |
358 | void NCMAG_Calibrate(void) |
338 | { |
359 | { |
339 | u8 msg[64]; |
360 | u8 msg[64]; |
Line 357... | Line 378... | ||
357 | Xmax = -10000; |
378 | Xmax = -10000; |
358 | Ymin = 10000; |
379 | Ymin = 10000; |
359 | Ymax = -10000; |
380 | Ymax = -10000; |
360 | Zmin = 10000; |
381 | Zmin = 10000; |
361 | Zmax = -10000; |
382 | Zmax = -10000; |
362 | speak = 1; |
383 | speak = 1; |
363 | CompassValueErrorCount = 0; |
384 | CompassValueErrorCount = 0; |
364 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
385 | if(Compass_CalState != OldCalState) // only once per state |
365 | { |
386 | { |
366 | UART1_PutString("\r\nStarting compass calibration"); |
387 | UART1_PutString("\r\nStarting compass calibration"); |
367 | if(I2C_CompassPort == I2C_EXTERN_0) |
388 | if(Compass_I2CPort == NCMAG_PORT_EXTERN) |
368 | { |
389 | { |
369 | if(!ExtCompassOrientation) ExtCompassOrientation = GetExtCompassOrientation(); |
390 | if(!NCMAG_Orientation) NCMAG_Orientation = NCMAG_GetOrientationFromAcc(); |
370 | UART1_PutString(" - External sensor "); |
391 | UART1_PutString(" - External sensor "); |
371 | sprintf(msg, "with orientation: %d ",ExtCompassOrientation); |
392 | sprintf(msg, "with orientation: %d ", NCMAG_Orientation); |
372 | UART1_PutString(msg); |
393 | UART1_PutString(msg); |
373 | } |
394 | } |
374 | else UART1_PutString(" - Internal sensor "); |
395 | else UART1_PutString(" - Internal sensor "); |
375 | } |
396 | } |
376 | break; |
397 | break; |
- | 398 | ||
377 | case 2: // 2nd step of calibration |
399 | case 2: // 2nd step of calibration |
378 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
400 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
379 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
401 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
380 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
402 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
381 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
403 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
382 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
404 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
383 | if(Z < Zmin) { Zmin = Z; } // silent |
405 | if(Z < Zmin) { Zmin = Z; } // silent |
384 | else if(Z > Zmax) { Zmax = Z; } |
406 | else if(Z > Zmax) { Zmax = Z; } |
385 | - | ||
386 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
407 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
387 | break; |
408 | break; |
Line 388... | Line 409... | ||
388 | 409 | ||
389 | case 3: // 3rd step of calibration |
410 | case 3: // 3rd step of calibration |
390 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
411 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
391 | speak = 1; |
412 | speak = 1; |
Line 392... | Line 413... | ||
392 | break; |
413 | break; |
393 | 414 | ||
394 | case 4: |
415 | case 4: |
395 | // find Min and Max of the Z-Sensor |
416 | // find Min and Max of the Z-Sensor |
396 | if(Z < Zmin2) { Zmin2 = Z; BeepTime = 80;} |
- | |
397 | else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;} |
417 | if(Z < Zmin2) { Zmin2 = Z; BeepTime = 80;} |
398 | 418 | else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;} |
|
399 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
419 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
400 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
420 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
401 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
- | |
402 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
421 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
403 | 422 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
|
404 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
423 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
405 | break; |
424 | break; |
406 | 425 | ||
407 | case 5: |
426 | case 5: |
408 | // Save values |
427 | // Save values |
409 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
428 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
410 | { |
429 | { |
411 | switch(NCMAG_SensorType) |
430 | switch(NCMAG_SensorType) |
412 | { |
431 | { |
413 | case TYPE_HMC5843: |
432 | case TYPE_HMC5843: |
414 | UART1_PutString("\r\nFinished: HMC5843 calibration\n\r"); |
433 | UART1_PutString("\r\nFinished: HMC5843 calibration\n\r"); |
Line 415... | Line 434... | ||
415 | MinCalibration = HMC5843_CALIBRATION_RANGE; |
434 | MinCalibration = HMC5843_CALIBRATION_RANGE; |
416 | break; |
435 | break; |
417 | 436 | ||
418 | case TYPE_LSM303DLH: |
437 | case TYPE_LSM303DLH: |
419 | case TYPE_LSM303DLM: |
438 | case TYPE_LSM303DLM: |
420 | UART1_PutString("\r\nFinished: LSM303 calibration\n\r"); |
439 | UART1_PutString("\r\nFinished: LSM303 calibration\n\r"); |
421 | MinCalibration = LSM303_CALIBRATION_RANGE; |
440 | MinCalibration = LSM303_CALIBRATION_RANGE; |
422 | break; |
441 | break; |
423 | } |
442 | } |
424 | if(EarthMagneticStrengthTheoretic) |
443 | if(EarthMagneticStrengthTheoretic) |
425 | { |
444 | { |
426 | MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50; |
445 | MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50; |
427 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
446 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
Line 428... | Line 447... | ||
428 | UART1_PutString(msg); |
447 | UART1_PutString(msg); |
429 | } |
448 | } |
430 | else UART1_PutString("without GPS\n\r"); |
- | |
431 | 449 | else UART1_PutString("without GPS\n\r"); |
|
432 | if(Zmin2 < Zmin) { Zmin = Zmin2; } |
450 | |
433 | else if(Zmax2 > Zmax) { Zmax = Zmax2; } |
451 | if(Zmin2 < Zmin) Zmin = Zmin2; |
434 | 452 | if(Zmax2 > Zmax) Zmax = Zmax2; |
|
435 | Calibration.MagX.Range = Xmax - Xmin; |
453 | Calibration.MagX.Range = Xmax - Xmin; |
436 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
454 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
437 | Calibration.MagY.Range = Ymax - Ymin; |
455 | Calibration.MagY.Range = Ymax - Ymin; |
438 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
456 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
439 | Calibration.MagZ.Range = Zmax - Zmin; |
457 | Calibration.MagZ.Range = Zmax - Zmin; |
440 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
458 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
Line 441... | Line 459... | ||
441 | if(CompassValueErrorCount) |
459 | if(CompassValueErrorCount) |
442 | { |
460 | { |
443 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
461 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
444 | UART1_PutString("\r\nCalibration FAILED - Compass sensor error !!!!\r\n "); |
462 | UART1_PutString("\r\nCalibration FAILED - Compass sensor error !!!!\r\n "); |
445 | 463 | ||
446 | } |
464 | } |
447 | else |
465 | else |
448 | if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration)) |
466 | if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration)) |
449 | { |
467 | { |
450 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort); |
468 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(Compass_I2CPort); |
451 | BeepTime = 2500; |
469 | BeepTime = 2500; |
452 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
470 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
453 | SpeakHoTT = SPEAK_MIKROKOPTER; |
471 | SpeakHoTT = SPEAK_MIKROKOPTER; |
454 | } |
472 | } |
455 | else |
473 | else |
456 | { |
474 | { |
457 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
475 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
Line 458... | Line 476... | ||
458 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
476 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
459 | if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! "); |
477 | if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! "); |
460 | if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! "); |
478 | if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! "); |
461 | if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! "); |
479 | if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! "); |
462 | UART1_PutString("\r\n"); |
480 | UART1_PutString("\r\n"); |
463 | 481 | ||
464 | // restore old calibration data from eeprom |
482 | // restore old calibration data from eeprom |
Line 472... | Line 490... | ||
472 | UART1_PutString(msg); |
490 | UART1_PutString(msg); |
473 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration); |
491 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration); |
474 | UART1_PutString(msg); |
492 | UART1_PutString(msg); |
475 | } |
493 | } |
476 | break; |
494 | break; |
477 | 495 | ||
478 | default: |
496 | default: |
479 | break; |
497 | break; |
480 | } |
498 | } |
481 | OldCalState = Compass_CalState; |
499 | OldCalState = Compass_CalState; |
482 | } |
500 | } |
Line 483... | Line 501... | ||
483 | 501 | ||
Line 506... | Line 524... | ||
506 | if(RxBufferSize == sizeof(MagRawVector) ) |
524 | if(RxBufferSize == sizeof(MagRawVector) ) |
507 | { // byte order from big to little endian |
525 | { // byte order from big to little endian |
508 | s16 raw, X = 0, Y = 0, Z = 0; |
526 | s16 raw, X = 0, Y = 0, Z = 0; |
509 | raw = pRxBuffer[0]<<8; |
527 | raw = pRxBuffer[0]<<8; |
510 | raw+= pRxBuffer[1]; |
528 | raw+= pRxBuffer[1]; |
511 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw; |
529 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw; |
512 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
530 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
Line 513... | Line 531... | ||
513 | 531 | ||
514 | raw = pRxBuffer[2]<<8; |
532 | raw = pRxBuffer[2]<<8; |
515 | raw+= pRxBuffer[3]; |
533 | raw+= pRxBuffer[3]; |
516 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
534 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
517 | { |
535 | { |
518 | if(NCMAG_SensorType == TYPE_LSM303DLM) Z = raw; // here Z and Y are exchanged |
536 | if(NCMAG_SensorType == TYPE_LSM303DLM) Z = raw; // here Z and Y are exchanged |
519 | else Y = raw; |
537 | else Y = raw; |
520 | } |
538 | } |
Line 521... | Line 539... | ||
521 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
539 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
522 | 540 | ||
523 | raw = pRxBuffer[4]<<8; |
541 | raw = pRxBuffer[4]<<8; |
524 | raw+= pRxBuffer[5]; |
542 | raw+= pRxBuffer[5]; |
525 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
543 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
526 | { |
544 | { |
527 | if(NCMAG_SensorType == TYPE_LSM303DLM) Y = raw; // here Z and Y are exchanged |
545 | if(NCMAG_SensorType == TYPE_LSM303DLM) Y = raw; // here Z and Y are exchanged |
528 | else Z = raw; |
546 | else Z = raw; |
Line -... | Line 547... | ||
- | 547 | } |
|
529 | } |
548 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
530 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
549 | |
531 | 550 | // correct compass orientation |
|
532 | switch(ExtCompassOrientation) |
551 | switch(NCMAG_Orientation) |
533 | { |
552 | { |
- | 553 | case 0: |
|
534 | case 0: |
554 | case 1: |
535 | case 1: |
555 | default: |
536 | default: |
556 | // 1:1 Mapping |
537 | MagRawVector.X = X; |
557 | MagRawVector.X = X; |
538 | MagRawVector.Y = Y; |
558 | MagRawVector.Y = Y; |
539 | MagRawVector.Z = Z; |
559 | MagRawVector.Z = Z; |
540 | break; |
560 | break; |
541 | case 2: |
561 | case 2: |
542 | MagRawVector.X = -X; |
562 | MagRawVector.X = -X; |
543 | MagRawVector.Y = Y; |
563 | MagRawVector.Y = Y; |
544 | MagRawVector.Z = -Z; |
564 | MagRawVector.Z = -Z; |
545 | break; |
565 | break; |
546 | case 3: |
566 | case 3: |
547 | MagRawVector.X = -Z; |
567 | MagRawVector.X = -Z; |
548 | MagRawVector.Y = Y; |
568 | MagRawVector.Y = Y; |
549 | MagRawVector.Z = X; |
569 | MagRawVector.Z = X; |
550 | break; |
570 | break; |
551 | case 4: |
571 | case 4: |
552 | MagRawVector.X = Z; |
572 | MagRawVector.X = Z; |
553 | MagRawVector.Y = Y; |
573 | MagRawVector.Y = Y; |
554 | MagRawVector.Z = -X; |
574 | MagRawVector.Z = -X; |
555 | break; |
575 | break; |
556 | case 5: |
576 | case 5: |
557 | MagRawVector.X = X; |
577 | MagRawVector.X = X; |
558 | MagRawVector.Y = -Z; |
578 | MagRawVector.Y = -Z; |
559 | MagRawVector.Z = Y; |
579 | MagRawVector.Z = Y; |
560 | break; |
580 | break; |
561 | case 6: |
581 | case 6: |
562 | MagRawVector.X = -X; |
582 | MagRawVector.X = -X; |
563 | MagRawVector.Y = -Z; |
583 | MagRawVector.Y = -Z; |
Line 581... | Line 601... | ||
581 | Compass_CalcHeading(); |
601 | Compass_CalcHeading(); |
582 | } |
602 | } |
583 | } |
603 | } |
584 | // rx data handler for acceleration raw data |
604 | // rx data handler for acceleration raw data |
585 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
605 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
586 | { // if number of byte are matching |
606 | { // if number of bytes are matching |
587 | if(RxBufferSize == sizeof(AccRawVector) ) |
607 | if(RxBufferSize == sizeof(AccRawVector) ) |
588 | { |
608 | { |
- | 609 | // copy from I2C buffer |
|
589 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
610 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
- | 611 | // scale and update Acc Vector, at the moment simply 1:1 |
|
- | 612 | memcpy((u8*)&AccVector, (u8*)&AccRawVector,sizeof(AccRawVector)); |
|
590 | } |
613 | } |
591 | } |
614 | } |
592 | - | ||
593 | u8 GetExtCompassOrientation(void) |
- | |
594 | { |
- | |
595 | if(I2C_CompassPort != I2C_EXTERN_0) return(0); |
- | |
596 | - | ||
597 | if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300)) |
- | |
598 | { |
- | |
599 | // UART1_PutString("\r\nTilted"); |
- | |
600 | return(0); |
- | |
601 | } |
- | |
602 | if(AccRawVector.Z > 3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn |
- | |
603 | else |
- | |
604 | if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn |
- | |
605 | else |
- | |
606 | if(AccRawVector.X > 3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn |
- | |
607 | else |
- | |
608 | if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn |
- | |
609 | else |
- | |
610 | if(AccRawVector.Y > 3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn |
- | |
611 | else |
- | |
612 | if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten - 'front' nach vorn |
- | |
613 | return(0); |
- | |
614 | } |
- | |
615 | - | ||
616 | // rx data handler for reading magnetic sensor configuration |
615 | // rx data handler for reading magnetic sensor configuration |
617 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
616 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
618 | { // if number of byte are matching |
617 | { // if number of byte are matching |
619 | if(RxBufferSize == sizeof(MagConfig) ) |
618 | if(RxBufferSize == sizeof(MagConfig) ) |
620 | { |
619 | { |
Line 629... | Line 628... | ||
629 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
628 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
630 | } |
629 | } |
631 | } |
630 | } |
632 | //---------------------------------------------------------------------- |
631 | //---------------------------------------------------------------------- |
Line -... | Line 632... | ||
- | 632 | ||
- | 633 | u8 NCMAG_GetOrientationFromAcc(void) |
|
- | 634 | { |
|
- | 635 | // only if external compass connected |
|
- | 636 | if(Compass_I2CPort != NCMAG_PORT_EXTERN) return(0); |
|
- | 637 | // MK must not be tilted |
|
- | 638 | if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300)) |
|
- | 639 | { |
|
- | 640 | // UART1_PutString("\r\nTilted"); |
|
- | 641 | return(0); |
|
- | 642 | } |
|
- | 643 | // select orientation |
|
- | 644 | if(AccRawVector.Z > 3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn |
|
- | 645 | else |
|
- | 646 | if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn |
|
- | 647 | else |
|
- | 648 | if(AccRawVector.X > 3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn |
|
- | 649 | else |
|
- | 650 | if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn |
|
- | 651 | else |
|
- | 652 | if(AccRawVector.Y > 3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn |
|
- | 653 | else |
|
- | 654 | if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten - 'front' nach vorn |
|
- | 655 | return(0); |
|
Line 633... | Line 656... | ||
633 | 656 | } |
|
634 | 657 | ||
635 | // --------------------------------------------------------------------- |
658 | // --------------------------------------------------------------------- |
636 | u8 NCMAG_SetMagConfig(void) |
659 | u8 NCMAG_SetMagConfig(void) |
- | 660 | { |
|
637 | { |
661 | u8 retval = 0; |
638 | u8 retval = 0; |
662 | |
639 | // try to catch the i2c buffer within 100 ms timeout |
663 | // try to catch the i2c buffer within 100 ms timeout |
640 | if(I2C_LockBufferFunc(100)) |
664 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
- | 665 | { |
|
- | 666 | u8 TxBytes = 0; |
|
641 | { |
667 | u8 TxData[sizeof(MagConfig) + 3]; |
642 | u8 TxBytes = 0; |
668 | |
643 | I2C_BufferPnt[TxBytes++] = REG_MAG_CRA; |
669 | TxData[TxBytes++] = REG_MAG_CRA; |
644 | memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
670 | memcpy(&TxData[TxBytes], (u8*)&MagConfig, sizeof(MagConfig)); |
645 | TxBytes += sizeof(MagConfig); |
671 | TxBytes += sizeof(MagConfig); |
646 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
672 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, 0, 0)) |
647 | { |
673 | { |
648 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
674 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
649 | { |
675 | { |
650 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
676 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
651 | } |
677 | } |
652 | } |
678 | } |
653 | } |
679 | } |
Line 654... | Line 680... | ||
654 | return(retval); |
680 | return(retval); |
655 | } |
681 | } |
656 | 682 | ||
657 | // ---------------------------------------------------------------------------------------- |
683 | // ---------------------------------------------------------------------------------------- |
658 | u8 NCMAG_GetMagConfig(void) |
684 | u8 NCMAG_GetMagConfig(void) |
659 | { |
685 | { |
660 | u8 retval = 0; |
686 | u8 retval = 0; |
661 | // try to catch the i2c buffer within 100 ms timeout |
687 | // try to catch the i2c buffer within 100 ms timeout |
- | 688 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
|
662 | if(I2C_LockBufferFunc(100)) |
689 | { |
663 | { |
690 | u8 TxBytes = 0; |
664 | u8 TxBytes = 0; |
691 | u8 TxData[3]; |
665 | I2C_BufferPnt[TxBytes++] = REG_MAG_CRA; |
692 | TxData[TxBytes++] = REG_MAG_CRA; |
666 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
693 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
667 | { |
694 | { |
668 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
695 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
669 | { |
696 | { |
670 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
697 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
671 | } |
698 | } |
672 | } |
699 | } |
Line 673... | Line 700... | ||
673 | } |
700 | } |
674 | return(retval); |
701 | return(retval); |
675 | } |
702 | } |
676 | 703 | ||
677 | // ---------------------------------------------------------------------------------------- |
704 | // ---------------------------------------------------------------------------------------- |
678 | u8 NCMAG_SetAccConfig(void) |
705 | u8 NCMAG_SetAccConfig(void) |
679 | { |
706 | { |
680 | u8 retval = 0; |
707 | u8 retval = 0; |
- | 708 | // try to catch the i2c buffer within 50 ms timeout |
|
681 | // try to catch the i2c buffer within 100 ms timeout |
709 | if(I2CBus_LockBuffer(Compass_I2CPort, 50)) |
682 | if(I2C_LockBufferFunc(50)) |
710 | { |
683 | { |
711 | u8 TxBytes = 0; |
684 | u8 TxBytes = 0; |
712 | u8 TxData[sizeof(AccConfig) + 3]; |
685 | I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT; |
713 | TxData[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT; |
686 | memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
714 | memcpy(&TxData[TxBytes], (u8*)&AccConfig, sizeof(AccConfig)); |
687 | TxBytes += sizeof(AccConfig); |
715 | TxBytes += sizeof(AccConfig); |
688 | if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
716 | if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, 0, 0)) |
689 | { |
717 | { |
690 | if(I2C_WaitForEndOfTransmissionFunc(50)) |
718 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 50)) |
691 | { |
719 | { |
692 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
720 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
693 | } |
721 | } |
Line 694... | Line 722... | ||
694 | } |
722 | } |
695 | } |
723 | } |
696 | return(retval); |
724 | return(retval); |
697 | } |
725 | } |
698 | 726 | ||
699 | // ---------------------------------------------------------------------------------------- |
727 | // ---------------------------------------------------------------------------------------- |
700 | u8 NCMAG_GetAccConfig(void) |
728 | u8 NCMAG_GetAccConfig(void) |
701 | { |
729 | { |
- | 730 | u8 retval = 0; |
|
702 | u8 retval = 0; |
731 | // try to catch the i2c buffer within 100 ms timeout |
703 | // try to catch the i2c buffer within 100 ms timeout |
732 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
704 | if(I2C_LockBufferFunc(100)) |
733 | { |
705 | { |
734 | u8 TxBytes = 0; |
706 | u8 TxBytes = 0; |
735 | u8 TxData[3]; |
707 | I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT; |
736 | TxData[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT; |
708 | if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
737 | if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
709 | { |
738 | { |
710 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
739 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
711 | { |
740 | { |
712 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
741 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
Line 713... | Line 742... | ||
713 | } |
742 | } |
714 | } |
743 | } |
715 | } |
744 | } |
716 | return(retval); |
745 | return(retval); |
717 | } |
746 | } |
718 | 747 | ||
719 | // ---------------------------------------------------------------------------------------- |
748 | // ---------------------------------------------------------------------------------------- |
720 | u8 NCMAG_GetIdentification(void) |
749 | u8 NCMAG_GetIdentification(void) |
- | 750 | { |
|
721 | { |
751 | u8 retval = 0; |
722 | u8 retval = 0; |
752 | // try to catch the i2c buffer within 100 ms timeout |
723 | // try to catch the i2c buffer within 100 ms timeout |
753 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
724 | if(I2C_LockBufferFunc(100)) |
754 | { |
725 | { |
755 | u8 TxBytes = 0; |
726 | u16 TxBytes = 0; |
756 | u8 TxData[3]; |
727 | NCMAG_Identification.A = 0xFF; |
757 | NCMAG_Identification.A = 0xFF; |
728 | NCMAG_Identification.B = 0xFF; |
758 | NCMAG_Identification.B = 0xFF; |
729 | NCMAG_Identification.C = 0xFF; |
759 | NCMAG_Identification.C = 0xFF; |
730 | I2C_BufferPnt[TxBytes++] = REG_MAG_IDA; |
760 | TxData[TxBytes++] = REG_MAG_IDA; |
731 | // initiate transmission |
761 | // initiate transmission |
732 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
762 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
733 | { |
763 | { |
734 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
764 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
735 | { |
765 | { |
Line 736... | Line 766... | ||
736 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
766 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
737 | } |
767 | } |
738 | } |
768 | } |
739 | } |
769 | } |
740 | return(retval); |
770 | return(retval); |
741 | } |
771 | } |
742 | 772 | ||
- | 773 | u8 NCMAG_GetIdentification_Sub(void) |
|
743 | u8 NCMAG_GetIdentification_Sub(void) |
774 | { |
744 | { |
775 | u8 retval = 0; |
745 | u8 retval = 0; |
776 | // try to catch the i2c buffer within 100 ms timeout |
746 | // try to catch the i2c buffer within 100 ms timeout |
777 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
747 | if(I2C_LockBufferFunc(100)) |
778 | { |
748 | { |
779 | u8 TxBytes = 0; |
749 | u16 TxBytes = 0; |
780 | u8 TxData[3]; |
750 | NCMAG_Identification2.Sub = 0xFF; |
781 | NCMAG_Identification2.Sub = 0xFF; |
751 | I2C_BufferPnt[TxBytes++] = REG_MAG_IDF; |
782 | TxData[TxBytes++] = REG_MAG_IDF; |
752 | // initiate transmission |
783 | // initiate transmission |
753 | if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
784 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
754 | { |
785 | { |
755 | if(I2C_WaitForEndOfTransmissionFunc(100)) |
786 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
Line 756... | Line 787... | ||
756 | { |
787 | { |
757 | if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1; |
788 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
758 | } |
789 | } |
759 | } |
790 | } |
760 | } |
791 | } |
761 | return(retval); |
792 | return(retval); |
762 | } |
793 | } |
- | 794 | ||
763 | 795 | ||
764 | 796 | // ---------------------------------------------------------------------------------------- |
|
765 | // ---------------------------------------------------------------------------------------- |
797 | void NCMAG_GetMagVector(u8 timeout) |
766 | void NCMAG_GetMagVector(void) |
798 | { |
767 | { |
799 | // try to catch the I2C buffer within timeout ms |
768 | // try to catch the I2C buffer within 0 ms |
800 | if(I2CBus_LockBuffer(Compass_I2CPort, timeout)) |
Line 769... | Line 801... | ||
769 | if(I2C_LockBufferFunc(5)) |
801 | { |
770 | { |
802 | u8 TxBytes = 0; |
771 | u16 TxBytes = 0; |
803 | u8 TxData[3]; |
772 | // set register pointer |
804 | // set register pointer |
773 | I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB; |
805 | TxData[TxBytes++] = REG_MAG_DATAX_MSB; |
774 | // initiate transmission |
806 | // initiate transmission |
775 | I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
807 | I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
- | 808 | } |
|
776 | } |
809 | } |
777 | } |
810 | |
778 | 811 | //---------------------------------------------------------------- |
|
779 | //---------------------------------------------------------------- |
812 | void NCMAG_GetAccVector(u8 timeout) |
780 | void NCMAG_GetAccVector(u8 timeout) |
- | |
781 | { |
- | |
782 | // try to catch the I2C buffer within 0 ms |
- | |
783 | if(I2C_LockBufferFunc(timeout)) |
813 | { |
784 | { |
814 | // try to catch the I2C buffer within timeout ms |
Line 785... | Line 815... | ||
785 | u16 TxBytes = 0; |
815 | if(I2CBus_LockBuffer(Compass_I2CPort, timeout)) |
786 | // set register pointer |
816 | { |
787 | I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT; |
817 | u8 TxBytes = 0; |
788 | // initiate transmission |
818 | u8 TxData[3]; |
Line 789... | Line 819... | ||
789 | I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
819 | // set register pointer |
790 | //DebugOut.Analog[16] = AccRawVector.X; |
820 | TxData[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT; |
Line 836... | Line 866... | ||
836 | void NCMAG_Update(u8 init) |
866 | void NCMAG_Update(u8 init) |
837 | { |
867 | { |
838 | static u32 TimerUpdate = 0; |
868 | static u32 TimerUpdate = 0; |
839 | static s8 send_config = 0; |
869 | static s8 send_config = 0; |
840 | u32 delay = 20; |
870 | u32 delay = 20; |
- | 871 | ||
841 | if(init) TimerUpdate = SetDelay(10); |
872 | if(init) TimerUpdate = SetDelay(10); |
Line 842... | Line -... | ||
842 | - | ||
843 | // todo State Handling for both busses !! |
873 | |
844 | if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF)/* || !NCMAG_Present*/ ) |
874 | if( (I2CBus(Compass_I2CPort)->State == I2C_STATE_UNDEF) /*|| !NCMAG_Present*/ ) |
845 | { |
875 | { |
846 | Compass_Heading = -1; |
876 | Compass_Heading = -1; |
847 | DebugOut.Analog[14]++; // count I2C error |
877 | DebugOut.Analog[14]++; // count I2C error |
848 | TimerUpdate = SetDelay(10); |
878 | TimerUpdate = SetDelay(10); |
849 | return; |
879 | return; |
850 | } |
880 | } |
851 | if(CheckDelay(TimerUpdate))// && I2C0_State == I2C_STATE_IDLE && I2C1_State == I2C_STATE_IDLE) |
881 | if(CheckDelay(TimerUpdate)) |
852 | { |
882 | { |
853 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
883 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
854 | if(++send_config == 25) // 500ms |
884 | if(++send_config == 25) // 500ms |
855 | { |
885 | { |
856 | send_config = -25; // next try after 1 second |
886 | send_config = -25; // next try after 1 second |
857 | InitNC_MagnetSensor(); |
887 | NCMAG_ConfigureSensor(); |
858 | TimerUpdate = SetDelay(20); // back into the old time-slot |
888 | TimerUpdate = SetDelay(20); // back into the old time-slot |
859 | } |
889 | } |
860 | else |
890 | else |
861 | { |
891 | { |
862 | static u8 s = 0; |
892 | static u8 s = 0; |
863 | // check for new calibration state |
893 | // check for new calibration state |
864 | Compass_UpdateCalState(); |
894 | Compass_UpdateCalState(); |
865 | if(Compass_CalState) NCMAG_Calibrate(); |
895 | if(Compass_CalState) NCMAG_Calibrate(); |
866 | 896 | ||
867 | // in case of LSM303 type |
897 | // in case of LSM303 type |
868 | switch(NCMAG_SensorType) |
898 | switch(NCMAG_SensorType) |
869 | { |
899 | { |
- | 900 | case TYPE_HMC5843: |
|
870 | case TYPE_HMC5843: |
901 | delay = 20; // next cycle after 20 ms |
871 | NCMAG_GetMagVector(); |
- | |
872 | delay = 20; |
902 | NCMAG_GetMagVector(5); |
873 | break; |
903 | break; |
874 | case TYPE_LSM303DLH: |
904 | case TYPE_LSM303DLH: |
875 | case TYPE_LSM303DLM: |
- | |
876 | delay = 20; |
905 | case TYPE_LSM303DLM: |
877 | //delay = 2; |
906 | |
878 | if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector(); |
907 | if(s-- || (Compass_I2CPort == NCMAG_PORT_INTERN)) |
- | 908 | { |
|
- | 909 | delay = 20; // next cycle after 20 ms |
|
879 | else |
910 | NCMAG_GetMagVector(5); |
- | 911 | } |
|
- | 912 | else // having an external compass, read every 50th cycle the ACC vec |
|
880 | { |
913 | { // try to initialize if no data are there |
- | 914 | if((AccRawVector.X + AccRawVector.Y + AccRawVector.Z) == 0) NCMAG_Init_ACCSensor(); |
|
881 | if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor(); |
915 | // get new data |
882 | NCMAG_GetAccVector(5); |
916 | NCMAG_GetAccVector(5); |
883 | delay = 10; |
917 | delay = 10; // next cycle after 10 ms |
884 | s = 40; // about 0,8 sec |
918 | s = 40; //reset downconter about 0,8 sec |
885 | }; |
- | |
886 | if(!s) delay = 10; // ACC-Reading in the next step after 10ms |
- | |
887 | //if(!s) delay = 2; // ACC-Reading in the next step after 10ms |
919 | } |
888 | break; |
920 | break; |
889 | } |
921 | } |
890 | if(send_config == 24) TimerUpdate = SetDelay(15); // next event is the re-configuration |
922 | if(send_config == 24) TimerUpdate = SetDelay(15); // next event is the re-configuration |
891 | else TimerUpdate = SetDelay(delay); // every 20 ms are 50 Hz |
923 | else TimerUpdate = SetDelay(delay); // every 20 ms are 50 Hz |
892 | } |
924 | } |
Line 899... | Line 931... | ||
899 | { |
931 | { |
900 | u8 msg[64]; |
932 | u8 msg[64]; |
901 | static u8 done = 0; |
933 | static u8 done = 0; |
Line 902... | Line 934... | ||
902 | 934 | ||
903 | if(done) return(1); // just make it once |
935 | if(done) return(1); // just make it once |
904 | 936 | ||
905 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
937 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
906 | u32 time; |
938 | u32 time; |
907 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
939 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
908 | s16 xscale, yscale, zscale, scale_min, scale_max; |
940 | s16 xscale, yscale, zscale, scale_min, scale_max; |
Line 926... | Line 958... | ||
926 | yscale = LSM303DLH_TEST_YSCALE; |
958 | yscale = LSM303DLH_TEST_YSCALE; |
927 | zscale = LSM303DLH_TEST_ZSCALE; |
959 | zscale = LSM303DLH_TEST_ZSCALE; |
928 | break; |
960 | break; |
Line 929... | Line 961... | ||
929 | 961 | ||
930 | case TYPE_LSM303DLM: |
962 | case TYPE_LSM303DLM: |
931 | // does not support self test feature |
963 | // does not support self test feature |
932 | done = retval; |
964 | done = 1; |
933 | return(retval); |
965 | return(1); // always return success |
Line 934... | Line 966... | ||
934 | break; |
966 | break; |
935 | 967 | ||
936 | default: |
968 | default: |
Line 947... | Line 979... | ||
947 | while(!CheckDelay(time)); |
979 | while(!CheckDelay(time)); |
948 | // averaging |
980 | // averaging |
949 | #define AVERAGE 20 |
981 | #define AVERAGE 20 |
950 | for(i = 0; i<AVERAGE; i++) |
982 | for(i = 0; i<AVERAGE; i++) |
951 | { |
983 | { |
952 | NCMAG_GetMagVector(); |
984 | NCMAG_GetMagVector(5); |
953 | time = SetDelay(20); |
985 | time = SetDelay(20); |
954 | while(!CheckDelay(time)); |
986 | while(!CheckDelay(time)); |
955 | XMax += MagRawVector.X; |
987 | XMax += MagRawVector.X; |
956 | YMax += MagRawVector.Y; |
988 | YMax += MagRawVector.Y; |
957 | ZMax += MagRawVector.Z; |
989 | ZMax += MagRawVector.Z; |
Line 963... | Line 995... | ||
963 | time = SetDelay(50); |
995 | time = SetDelay(50); |
964 | while(!CheckDelay(time)); |
996 | while(!CheckDelay(time)); |
965 | // averaging |
997 | // averaging |
966 | for(i = 0; i < AVERAGE; i++) |
998 | for(i = 0; i < AVERAGE; i++) |
967 | { |
999 | { |
968 | NCMAG_GetMagVector(); |
1000 | NCMAG_GetMagVector(5); |
969 | time = SetDelay(20); |
1001 | time = SetDelay(20); |
970 | while(!CheckDelay(time)); |
1002 | while(!CheckDelay(time)); |
971 | XMin += MagRawVector.X; |
1003 | XMin += MagRawVector.X; |
972 | YMin += MagRawVector.Y; |
1004 | YMin += MagRawVector.Y; |
973 | ZMin += MagRawVector.Z; |
1005 | ZMin += MagRawVector.Z; |
Line 978... | Line 1010... | ||
978 | NCMAG_SetMagConfig(); |
1010 | NCMAG_SetMagConfig(); |
979 | // check scale for all axes |
1011 | // check scale for all axes |
980 | // prepare scale limits |
1012 | // prepare scale limits |
981 | LIMITS(xscale, scale_min, scale_max); |
1013 | LIMITS(xscale, scale_min, scale_max); |
982 | xscale = (XMax - XMin)/(2*AVERAGE); |
1014 | xscale = (XMax - XMin)/(2*AVERAGE); |
983 | if((xscale > scale_max) || (xscale < scale_min)) |
1015 | if((xscale > scale_max) || (xscale < scale_min)) |
984 | { |
1016 | { |
985 | retval = 0; |
1017 | retval = 0; |
986 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
1018 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
987 | UART1_PutString(msg); |
1019 | UART1_PutString(msg); |
988 | } |
1020 | } |
989 | LIMITS(yscale, scale_min, scale_max); |
1021 | LIMITS(yscale, scale_min, scale_max); |
990 | yscale = (YMax - YMin)/(2*AVERAGE); |
1022 | yscale = (YMax - YMin)/(2*AVERAGE); |
991 | if((yscale > scale_max) || (yscale < scale_min)) |
1023 | if((yscale > scale_max) || (yscale < scale_min)) |
992 | { |
1024 | { |
993 | retval = 0; |
1025 | retval = 0; |
994 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
1026 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
995 | UART1_PutString(msg); |
1027 | UART1_PutString(msg); |
996 | } |
1028 | } |
997 | LIMITS(zscale, scale_min, scale_max); |
1029 | LIMITS(zscale, scale_min, scale_max); |
998 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
1030 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
999 | if((zscale > scale_max) || (zscale < scale_min)) |
1031 | if((zscale > scale_max) || (zscale < scale_min)) |
1000 | { |
1032 | { |
1001 | retval = 0; |
1033 | retval = 0; |
1002 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
1034 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
1003 | UART1_PutString(msg); |
1035 | UART1_PutString(msg); |
1004 | } |
1036 | } |
1005 | done = retval; |
1037 | done = retval; |
1006 | return(retval); |
1038 | return(retval); |
1007 | } |
1039 | } |
Line 1008... | Line -... | ||
1008 | - | ||
1009 | 1040 | ||
1010 | //---------------------------------------------------------------- |
- | |
1011 | void NCMAG_SelectI2CBus(u8 busno) |
- | |
1012 | { |
- | |
1013 | if (busno == 0) |
- | |
1014 | { |
- | |
1015 | I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission; |
- | |
1016 | I2C_LockBufferFunc = &I2C0_LockBuffer; |
1041 | |
1017 | I2C_TransmissionFunc = &I2C0_Transmission; |
1042 | void NCMAG_CheckOrientation(void) |
1018 | I2C_BufferPnt = I2C0_Buffer; |
- | |
1019 | I2C_ErrorPnt = &I2C0_Error; |
- | |
1020 | } |
1043 | { // only for external sensor |
1021 | else |
1044 | if(Compass_I2CPort == NCMAG_PORT_EXTERN) |
1022 | { |
- | |
1023 | I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission; |
1045 | { |
1024 | I2C_LockBufferFunc = &I2C1_LockBuffer; |
1046 | NCMAG_Orientation = NCMAG_GetOrientationFromAcc(); |
1025 | I2C_TransmissionFunc = &I2C1_Transmission; |
- | |
1026 | I2C_BufferPnt = I2C1_Buffer; |
1047 | if(NCMAG_Orientation != (Calibration.Version>>4)) NCMAG_IsCalibrated = 0; |
1027 | I2C_ErrorPnt = &I2C1_Error; |
1048 | else NCMAG_IsCalibrated = 1; |
1028 | } |
- | |
1029 | } |
1049 | } |
1030 | 1050 | } |
|
1031 | //---------------------------------------------------------------- |
1051 | //---------------------------------------------------------------- |
- | 1052 | u8 NCMAG_Init(void) |
|
- | 1053 | { |
|
- | 1054 | MagRawVector.X = 0; |
|
- | 1055 | MagRawVector.Y = 0; |
|
- | 1056 | MagRawVector.Z = 0; |
|
- | 1057 | AccRawVector.X = 0; |
|
- | 1058 | AccRawVector.Y = 0; |
|
- | 1059 | AccRawVector.Z = 0; |
|
- | 1060 | ||
- | 1061 | if(NCMAG_Present) // do only short init ! , full init was called before |
|
- | 1062 | { |
|
- | 1063 | // reset I2C Bus |
|
- | 1064 | I2CBus_Deinit(Compass_I2CPort); |
|
- | 1065 | I2CBus_Init(Compass_I2CPort); |
|
- | 1066 | // try to reconfigure senor |
|
- | 1067 | NCMAG_ConfigureSensor(); |
|
- | 1068 | NCMAG_Update(1); |
|
- | 1069 | } |
|
1032 | u8 NCMAG_Init(void) |
1070 | else // full init |
1033 | { |
1071 | { |
1034 | u8 msg[64]; |
1072 | u8 msg[64]; |
1035 | u8 retval = 0; |
1073 | u8 retval = 0; |
1036 | u8 repeat = 0; |
1074 | u8 repeat = 0; |
1037 | 1075 | ||
1038 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1076 | //-------------------------------------------- |
1039 | // Search external sensor |
1077 | // search external sensor first |
1040 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
1041 | I2C_CompassPort = I2C_EXTERN_0; |
- | |
1042 | NCMAG_SelectI2CBus(I2C_CompassPort); |
1078 | //-------------------------------------------- |
1043 | 1079 | Compass_I2CPort = NCMAG_PORT_EXTERN; |
|
1044 | // get id bytes |
1080 | // get id bytes |
1045 | retval = 0; |
1081 | retval = 0; |
1046 | for(repeat = 0; repeat < 5; repeat++) |
1082 | for(repeat = 0; repeat < 5; repeat++) |
1047 | { |
1083 | { |
1048 | // retval = NCMAG_GetIdentification(); |
1084 | //retval = NCMAG_GetIdentification(); |
1049 | retval = NCMAG_GetAccConfig(); // only the sensor with ACC is supported |
1085 | retval = NCMAG_GetAccConfig(); // only the external sensor with ACC is supported |
1050 | if(retval) break; // break loop on success |
1086 | if(retval) break; // break loop on success |
1051 | UART1_PutString("_"); |
1087 | UART1_PutString("_"); |
1052 | } |
1088 | } |
1053 | //retval = 1; |
1089 | // Extenal sensor not found? |
- | 1090 | if(!retval) |
|
1054 | if(!retval) |
1091 | { |
1055 | { |
1092 | // search internal sensor afterwards |
1056 | UART1_PutString(" internal sensor "); |
- | |
1057 | I2C_CompassPort = I2C_INTERN_1; |
1093 | UART1_PutString(" internal sensor"); |
1058 | NCMAG_SelectI2CBus(I2C_CompassPort); |
1094 | Compass_I2CPort = NCMAG_PORT_INTERN; |
1059 | } |
1095 | } |
1060 | else |
1096 | else |
1061 | { |
1097 | { |
- | 1098 | UART1_PutString(" external sensor"); |
|
- | 1099 | Compass_I2CPort = NCMAG_PORT_EXTERN; |
|
Line 1062... | Line -... | ||
1062 | UART1_PutString(" external sensor "); |
- | |
1063 | NCMAG_Init_ACCSensor(); |
- | |
1064 | - | ||
1065 | for(repeat = 0; repeat < 100; repeat++) |
- | |
1066 | { |
- | |
1067 | NCMAG_GetAccVector(10); // only the sensor with ACC is supported |
- | |
1068 | ExtCompassOrientation = GetExtCompassOrientation(); |
- | |
1069 | if(ExtCompassOrientation && (ExtCompassOrientation == Calibration.Version / 16)) break; |
- | |
1070 | //UART1_Putchar('-'); |
- | |
1071 | } |
- | |
1072 | //DebugOut.Analog[19] = repeat; |
- | |
1073 | - | ||
1074 | if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)"); |
- | |
1075 | else |
- | |
1076 | { |
- | |
1077 | NCMag_CalibrationRead(I2C_CompassPort); |
- | |
1078 | sprintf(msg, "with orientation: %d ",ExtCompassOrientation ); |
- | |
1079 | UART1_PutString(msg); |
- | |
1080 | if(ExtCompassOrientation != Calibration.Version / 16) |
- | |
1081 | { |
- | |
1082 | sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version / 16); |
- | |
1083 | UART1_PutString(msg); |
- | |
1084 | } |
- | |
1085 | else UART1_PutString("ok "); |
- | |
1086 | } |
- | |
1087 | - | ||
1088 | } |
1100 | } |
1089 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1101 | //------------------------------------------- |
1090 | 1102 | ||
1091 | NCMAG_Present = 0; |
- | |
1092 | NCMAG_SensorType = TYPE_HMC5843; // assuming having an HMC5843 |
- | |
1093 | // polling for LSM302DLH/DLM option by ACC address ack |
- | |
1094 | for(repeat = 0; repeat < 3; repeat++) |
- | |
1095 | { |
- | |
1096 | retval = NCMAG_GetAccConfig(); |
1103 | NCMAG_Present = 0; |
1097 | if(retval) break; // break loop on success |
- | |
1098 | } |
- | |
1099 | if(retval) |
- | |
1100 | { |
- | |
1101 | // initialize ACC sensor |
- | |
1102 | NCMAG_Init_ACCSensor(); |
- | |
1103 | 1104 | NCMAG_SensorType = TYPE_HMC5843; // assuming having an HMC5843 |
|
1104 | NCMAG_SensorType = TYPE_LSM303DLH; |
1105 | // polling for LSM302DLH/DLM option by ACC address ack |
1105 | // polling of sub identification |
1106 | repeat = 0; |
1106 | for(repeat = 0; repeat < 12; repeat++) |
1107 | for(repeat = 0; repeat < 3; repeat++) |
1107 | { |
1108 | { |
1108 | retval = NCMAG_GetIdentification_Sub(); |
1109 | retval = NCMAG_GetAccConfig(); |
1109 | if(retval) break; // break loop on success |
1110 | if(retval) break; // break loop on success |
1110 | } |
1111 | } |
1111 | if(retval) |
1112 | if(retval) |
1112 | { |
1113 | { |
- | 1114 | // initialize ACC sensor |
|
- | 1115 | NCMAG_Init_ACCSensor(); |
|
1113 | if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM) NCMAG_SensorType = TYPE_LSM303DLM; |
1116 | |
1114 | } |
1117 | NCMAG_SensorType = TYPE_LSM303DLH; |
1115 | } |
1118 | // polling of sub identification |
1116 | // get id bytes |
1119 | repeat = 0; |
1117 | for(repeat = 0; repeat < 3; repeat++) |
1120 | for(repeat = 0; repeat < 12; repeat++) |
1118 | { |
1121 | { |
1119 | retval = NCMAG_GetIdentification(); |
- | |
1120 | if(retval) break; // break loop on success |
- | |
1121 | } |
1122 | retval = NCMAG_GetIdentification_Sub(); |
1122 | 1123 | if(retval) break; // break loop on success |
|
1123 | // if we got an answer to id request |
1124 | } |
1124 | if(retval) |
1125 | if(retval) |
- | 1126 | { |
|
1125 | { |
1127 | if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM) NCMAG_SensorType = TYPE_LSM303DLM; |
1126 | u8 n1[] = "\n\r HMC5843"; |
1128 | } |
1127 | u8 n2[] = "\n\r LSM303DLH"; |
- | |
1128 | u8 n3[] = "\n\r LSM303DLM"; |
1129 | } |
1129 | u8* pn = n1; |
1130 | // get id bytes |
1130 | - | ||
1131 | switch(NCMAG_SensorType) |
- | |
1132 | { |
- | |
1133 | case TYPE_HMC5843: |
1131 | retval = 0; |
1134 | pn = n1; |
- | |
1135 | break; |
- | |
1136 | case TYPE_LSM303DLH: |
1132 | for(repeat = 0; repeat < 3; repeat++) |
1137 | pn = n2; |
- | |
1138 | break; |
- | |
1139 | case TYPE_LSM303DLM: |
1133 | { |
Line 1140... | Line -... | ||
1140 | pn = n3; |
- | |
1141 | break; |
- | |
1142 | } |
1134 | retval = NCMAG_GetIdentification(); |
1143 | 1135 | if(retval) break; // break loop on success |
|
1144 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
- | |
1145 | UART1_PutString(msg); |
1136 | } |
- | 1137 | ||
- | 1138 | // if we got an answer to id request |
|
- | 1139 | if(retval) |
|
1146 | if ( (NCMAG_Identification.A == MAG_IDA) |
1140 | { |
Line 1147... | Line 1141... | ||
1147 | && (NCMAG_Identification.B == MAG_IDB) |
1141 | u8 n1[] = "\n\r HMC5843"; |
1148 | && (NCMAG_Identification.C == MAG_IDC)) |
1142 | u8 n2[] = "\n\r LSM303DLH"; |
- | 1143 | u8 n3[] = "\n\r LSM303DLM"; |
|
- | 1144 | u8* pn = n1; |
|
- | 1145 | ||
1149 | { |
1146 | switch(NCMAG_SensorType) |
- | 1147 | { |
|
- | 1148 | case TYPE_HMC5843: |
|
1150 | NCMAG_Present = 1; |
1149 | pn = n1; |
- | 1150 | break; |
|
- | 1151 | case TYPE_LSM303DLH: |
|
1151 | 1152 | pn = n2; |
|
1152 | if(EEPROM_Init()) |
- | |
1153 | { |
- | |
1154 | NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort); |
- | |
1155 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
- | |
1156 | } |
- | |
1157 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
- | |
1158 | // perform self test |
- | |
1159 | if(!NCMAG_SelfTest()) |
- | |
1160 | { |
- | |
Line -... | Line 1153... | ||
- | 1153 | break; |
|
- | 1154 | case TYPE_LSM303DLM: |
|
- | 1155 | pn = n3; |
|
- | 1156 | break; |
|
- | 1157 | } |
|
- | 1158 | ||
- | 1159 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
|
- | 1160 | UART1_PutString(msg); |
|
- | 1161 | if ( (NCMAG_Identification.A == MAG_IDA) |
|
- | 1162 | && (NCMAG_Identification.B == MAG_IDB) |
|
- | 1163 | && (NCMAG_Identification.C == MAG_IDC)) |
|
- | 1164 | { |
|
- | 1165 | NCMAG_Present = 1; |
|
- | 1166 | ||
- | 1167 | if(EEPROM_Init()) |
|
- | 1168 | { |
|
- | 1169 | NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort); |
|
- | 1170 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
|
- | 1171 | } |
|
- | 1172 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
|
- | 1173 | ||
- | 1174 | // in case of an external sensor, try to get the orientation by acc readings |
|
- | 1175 | if(Compass_I2CPort == NCMAG_PORT_EXTERN) |
|
- | 1176 | { |
|
- | 1177 | // try to get orientation by acc sensor values |
|
- | 1178 | for(repeat = 0; repeat < 100; repeat++) |
|
- | 1179 | { |
|
- | 1180 | NCMAG_GetAccVector(10); // only the sensor with ACC is supported |
|
- | 1181 | NCMAG_Orientation = NCMAG_GetOrientationFromAcc(); |
|
- | 1182 | if(NCMAG_Orientation && (NCMAG_Orientation == Calibration.Version >> 4)) break; |
|
- | 1183 | } |
|
- | 1184 | // check orientation result if available |
|
- | 1185 | sprintf(msg, "\r\n Orientation: "); |
|
- | 1186 | UART1_PutString(msg); |
|
- | 1187 | if(NCMAG_Orientation) |
|
- | 1188 | { |
|
- | 1189 | sprintf(msg, "%d ", NCMAG_Orientation); |
|
- | 1190 | UART1_PutString(msg); |
|
- | 1191 | if(NCMAG_IsCalibrated) // check against calibration data orientation |
|
- | 1192 | { |
|
- | 1193 | if(NCMAG_Orientation != Calibration.Version >> 4) |
|
- | 1194 | { |
|
- | 1195 | sprintf(msg, "\n\r Warning: calibrated orientation was %d !",Calibration.Version >> 4); |
|
- | 1196 | UART1_PutString(msg); |
|
- | 1197 | } |
|
- | 1198 | } |
|
- | 1199 | } |
|
- | 1200 | else |
|
- | 1201 | { |
|
- | 1202 | UART1_PutString("unknown!"); |
|
- | 1203 | } |
|
- | 1204 | } |
|
- | 1205 | ||
- | 1206 | ||
- | 1207 | // perform self test |
|
- | 1208 | if(!NCMAG_SelfTest()) |
|
- | 1209 | { |
|
1161 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
1210 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
1162 | LED_RED_ON; |
1211 | LED_RED_ON; |
- | 1212 | //NCMAG_IsCalibrated = 0; |
|
- | 1213 | } |
|
- | 1214 | else UART1_PutString("\r\n Selftest ok"); |
|
- | 1215 | ||
- | 1216 | // initialize magnetic sensor configuration |
|
- | 1217 | NCMAG_ConfigureSensor(); |
|
- | 1218 | } |
|
1163 | // NCMAG_IsCalibrated = 0; |
1219 | else |
1164 | } |
1220 | { |
1165 | else UART1_PutString("\r\n Selftest ok"); |
1221 | UART1_PutString("\n\r Not compatible!"); |
- | 1222 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
|
1166 | 1223 | LED_RED_ON; |
|
1167 | // initialize magnetic sensor configuration |
- | |
1168 | InitNC_MagnetSensor(); |
- | |
1169 | } |
1224 | } |
1170 | else |
1225 | } |
1171 | { |
- | |
1172 | UART1_PutString("\n\r Not compatible!"); |
- | |
1173 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
- | |
1174 | LED_RED_ON; |
- | |
1175 | } |
- | |
1176 | } |
1226 | else // nothing found |
1177 | else // nothing found |
1227 | { |