Subversion Repositories NaviCtrl

Rev

Rev 480 | Rev 483 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 480 Rev 482
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;
-
 
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 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 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
        {
Line 309... Line 328...
309
        if(EEPROM_SUCCESS == eres) i = 1;
328
        if(EEPROM_SUCCESS == eres) i = 1;
310
        else i = 0;
329
        else i = 0;
311
        return(i);     
330
        return(i);     
312
}
331
}
Line 313... Line 332...
313
 
332
 
-
 
333
// read calibration data for external and internal sensor seperately
314
u8 NCMag_CalibrationRead(u8 intern)     // two calibrtion sets for extern and intern sensor
334
u8 NCMag_CalibrationRead(I2C_TypeDef* I2Cx)
-
 
335
{
315
{
336
        u8 address;
316
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
337
        u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE;
317
        u8 *pBuff = (u8*)&Calibration;
-
 
Line 318... Line 338...
318
        u16 address;
338
        u8 *pBuff = (u8*)&Calibration;
319
 
339
 
-
 
340
        if (I2Cx == NCMAG_PORT_EXTERN)          address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
Line 320... Line 341...
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
                }
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);
Line 332... Line 353...
332
        }
353
        }
333
        return(0);
354
        return(0);
334
}
355
}
335
 
356
 
336
 
357
 
337
void NCMAG_Calibrate(void)
358
void NCMAG_Calibrate(void)
338
{
359
{
339
        u8 msg[64];
360
        u8 msg[64];
Line 340... Line 361...
340
        static u8 speak = 0;
361
        static u8 speak = 0;
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
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
384
                        if(Compass_CalState != OldCalState) // only once per state
364
                        {
385
                        {
365
                         UART1_PutString("\r\nStarting compass calibration");
386
                                UART1_PutString("\r\nStarting compass calibration");
366
                         if(I2C_CompassPort == I2C_EXTERN_0)
387
                                if(Compass_I2CPort == NCMAG_PORT_EXTERN)
367
                         {
388
                                {
368
                          if(!ExtCompassOrientation) ExtCompassOrientation = GetExtCompassOrientation();
389
                                        if(!NCMAG_Orientation) NCMAG_Orientation = NCMAG_GetOrientationFromAcc();
369
                          UART1_PutString(" - External sensor ");
390
                                        UART1_PutString(" - External sensor ");
370
                          sprintf(msg, "with orientation: %d ",ExtCompassOrientation);
391
                                        sprintf(msg, "with orientation: %d ", NCMAG_Orientation);
371
                          UART1_PutString(msg);
392
                                        UART1_PutString(msg);
372
                         }
393
                                }
373
                         else UART1_PutString(" - Internal sensor ");
394
                                else UART1_PutString(" - Internal sensor ");
374
                        }
395
                        }
375
                        break;
396
                        break;
-
 
397
               
376
                case 2: // 2nd step of calibration
398
                case 2: // 2nd step of calibration
377
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
399
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
378
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
400
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
379
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
401
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
380
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
402
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
381
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
403
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
382
                        if(Z < Zmin)        { Zmin = Z; } // silent
404
                        if(Z < Zmin)        { Zmin = Z; } // silent
383
                        else if(Z > Zmax)   { Zmax = Z; }
405
                        else if(Z > Zmax)   { Zmax = Z; }
384
 
-
 
385
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
406
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
386
                        break;
407
                        break;
Line 387... Line 408...
387
 
408
 
388
                case 3: // 3rd step of calibration
409
                case 3: // 3rd step of calibration
389
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
410
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
390
                        speak = 1;
411
                        speak = 1;
Line 391... Line 412...
391
                        break;
412
                        break;
392
 
413
 
393
                case 4:
414
                case 4:
394
                        // find Min and Max of the Z-Sensor
415
                        // find Min and Max of the Z-Sensor
395
                        if(Z < Zmin2)     { Zmin2 = Z; BeepTime = 80;}
-
 
396
                        else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;}
416
                        if(Z < Zmin2)           { Zmin2 = Z; BeepTime = 80;}
397
                       
417
                        else if(Z > Zmax2)      { Zmax2 = Z; BeepTime = 80;}
398
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
418
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
399
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
419
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
400
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
-
 
401
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
420
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
402
 
421
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
Line 403... Line 422...
403
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
422
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
404
                        break;
423
                        break;
405
               
424
               
406
                case 5:
425
                case 5:
407
                        // Save values
426
                        // Save values
408
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
427
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
409
                        {
428
                        {
410
                                switch(NCMAG_SensorType)
429
                                switch(NCMAG_SensorType)
411
                                {
430
                                {
412
                                        case TYPE_HMC5843:
431
                                        case TYPE_HMC5843:
Line 413... Line 432...
413
                                                UART1_PutString("\r\nFinished: HMC5843 calibration\n\r");
432
                                                UART1_PutString("\r\nFinished: HMC5843 calibration\n\r");
414
                                                MinCalibration = HMC5843_CALIBRATION_RANGE;
433
                                                MinCalibration = HMC5843_CALIBRATION_RANGE;
415
                                                break;
434
                                                break;
416
 
435
 
417
                                        case TYPE_LSM303DLH:
436
                                        case TYPE_LSM303DLH:
418
                                        case TYPE_LSM303DLM:
437
                                        case TYPE_LSM303DLM:
419
                                                UART1_PutString("\r\nFinished: LSM303 calibration\n\r");
438
                                                UART1_PutString("\r\nFinished: LSM303 calibration\n\r");
420
                                                MinCalibration = LSM303_CALIBRATION_RANGE;
439
                                                MinCalibration = LSM303_CALIBRATION_RANGE;
421
                                        break;
440
                                                break;
422
                                }
441
                                }
423
                                if(EarthMagneticStrengthTheoretic)
442
                                if(EarthMagneticStrengthTheoretic)
424
                                 {
443
                                {
425
                                  MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50;
444
                                        MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50;
Line 426... Line 445...
426
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
445
                                        sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
427
                                  UART1_PutString(msg);
446
                                        UART1_PutString(msg);
428
                                 }
-
 
429
                            else UART1_PutString("without GPS\n\r");
447
                                }
430
 
448
                            else UART1_PutString("without GPS\n\r");
431
                                if(Zmin2 < Zmin)          { Zmin = Zmin2; }
449
 
432
                                else if(Zmax2 > Zmax) { Zmax = Zmax2; }
450
                                if(Zmin2 < Zmin) Zmin = Zmin2;
433
 
451
                                if(Zmax2 > Zmax) Zmax = Zmax2;
434
                                Calibration.MagX.Range = Xmax - Xmin;
452
                                Calibration.MagX.Range = Xmax - Xmin;
435
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
453
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
436
                                Calibration.MagY.Range = Ymax - Ymin;
454
                                Calibration.MagY.Range = Ymax - Ymin;
437
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
455
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
438
                                Calibration.MagZ.Range = Zmax - Zmin;
456
                                Calibration.MagZ.Range = Zmax - Zmin;
439
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
457
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
440
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
458
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
441
                                {
459
                                {
442
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort);
460
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite(Compass_I2CPort);
Line 452... Line 470...
452
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
470
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
453
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
471
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
454
                                        UART1_PutString("\r\n");
472
                                        UART1_PutString("\r\n");
Line 455... Line 473...
455
 
473
 
456
                                        // restore old calibration data from eeprom
474
                                        // restore old calibration data from eeprom
457
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
475
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort);
458
                                }
476
                                }
459
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
477
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
460
                                        UART1_PutString(msg);
478
                                        UART1_PutString(msg);
461
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
479
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
Line 513... Line 531...
513
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
531
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
514
                {
532
                {
515
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  Y = raw; // here Z and Y are exchanged
533
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  Y = raw; // here Z and Y are exchanged
516
                        else                                                                    Z = raw;
534
                        else                                                                    Z = raw;
517
                }
535
                }
-
 
536
                // correct compass orientation
518
          switch(ExtCompassOrientation)
537
                switch(NCMAG_Orientation)
519
                {
538
                {
520
                 case 0:
539
                        case 0:
521
                 case 1:
540
                        case 1:
522
                 default:
541
                        default:
-
 
542
                                // 1:1 Mapping
523
                                MagRawVector.X = X;
543
                                MagRawVector.X = X;
524
                                MagRawVector.Y = Y;
544
                                MagRawVector.Y = Y;
525
                                MagRawVector.Z = Z;
545
                                MagRawVector.Z = Z;
526
                                break;
546
                                break;
527
                 case 2:
547
                        case 2:
528
                                MagRawVector.X = -X;
548
                                MagRawVector.X = -X;
529
                                MagRawVector.Y = Y;
549
                                MagRawVector.Y = Y;
530
                                MagRawVector.Z = -Z;
550
                                MagRawVector.Z = -Z;
531
                                break;
551
                                break;
532
                 case 3:
552
                        case 3:
533
                                MagRawVector.X = -Z;
553
                                MagRawVector.X = -Z;
534
                                MagRawVector.Y = Y;
554
                                MagRawVector.Y = Y;
535
                                MagRawVector.Z = X;
555
                                MagRawVector.Z = X;
536
                                break;
556
                                break;
537
                 case 4:
557
                        case 4:
538
                                MagRawVector.X = Z;
558
                                MagRawVector.X = Z;
539
                                MagRawVector.Y = Y;
559
                                MagRawVector.Y = Y;
540
                                MagRawVector.Z = -X;
560
                                MagRawVector.Z = -X;
541
                                break;
561
                                break;
542
                 case 5:
562
                        case 5:
543
                                MagRawVector.X = X;
563
                                MagRawVector.X = X;
544
                                MagRawVector.Y = -Z;
564
                                MagRawVector.Y = -Z;
545
                                MagRawVector.Z = Y;
565
                                MagRawVector.Z = Y;
546
                                break;
566
                                break;
547
                 case 6:
567
                        case 6:
548
                                MagRawVector.X = -X;
568
                                MagRawVector.X = -X;
549
                                MagRawVector.Y = -Z;
569
                                MagRawVector.Y = -Z;
550
                                MagRawVector.Z = -Y;
570
                                MagRawVector.Z = -Y;
551
                                break;
571
                                break;
552
                }
572
                }
Line 567... Line 587...
567
                Compass_CalcHeading();
587
                Compass_CalcHeading();
568
        }
588
        }
569
}
589
}
570
// rx data handler  for acceleration raw data
590
// rx data handler  for acceleration raw data
571
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
591
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
572
{       // if number of byte are matching
592
{       // if number of bytes are matching
573
        if(RxBufferSize == sizeof(AccRawVector) )
593
        if(RxBufferSize == sizeof(AccRawVector) )
574
        {
594
        {
-
 
595
                // copy from I2C buffer 
575
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
596
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
-
 
597
                // scale and update Acc Vector, at the moment simply 1:1
-
 
598
                memcpy((u8*)&AccVector, (u8*)&AccRawVector,sizeof(AccRawVector));
576
        }
599
        }
577
}
600
}
578
 
-
 
579
u8 GetExtCompassOrientation(void)
-
 
580
{
-
 
581
        if(I2C_CompassPort != I2C_EXTERN_0) return(0);
-
 
582
 
-
 
583
        if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300))
-
 
584
         {
-
 
585
//        UART1_PutString("\r\nTilted"); 
-
 
586
          return(0);
-
 
587
         }
-
 
588
        if(AccRawVector.Z >  3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn
-
 
589
        else
-
 
590
        if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn
-
 
591
        else
-
 
592
        if(AccRawVector.X >  3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn
-
 
593
        else
-
 
594
        if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn
-
 
595
        else
-
 
596
        if(AccRawVector.Y >  3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn
-
 
597
        else
-
 
598
        if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten  - 'front' nach vorn
-
 
599
        return(0);
-
 
600
}
-
 
601
 
-
 
602
// rx data handler for reading magnetic sensor configuration
601
// rx data handler for reading magnetic sensor configuration
603
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
602
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
604
{       // if number of byte are matching
603
{       // if number of byte are matching
605
        if(RxBufferSize == sizeof(MagConfig) )
604
        if(RxBufferSize == sizeof(MagConfig) )
606
        {
605
        {
Line 615... Line 614...
615
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
614
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
616
        }
615
        }
617
}
616
}
618
//----------------------------------------------------------------------
617
//----------------------------------------------------------------------
Line -... Line 618...
-
 
618
 
-
 
619
u8 NCMAG_GetOrientationFromAcc(void)
-
 
620
{
-
 
621
        // only if external compass connected
-
 
622
        if(Compass_I2CPort != NCMAG_PORT_EXTERN) return(0);
-
 
623
         // MK must not be tilted
-
 
624
        if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300))
-
 
625
        {
-
 
626
//              UART1_PutString("\r\nTilted");          
-
 
627
                return(0);
-
 
628
        }
-
 
629
        // select orientation
-
 
630
        if(AccRawVector.Z >  3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn
-
 
631
        else
-
 
632
        if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn
-
 
633
        else
-
 
634
        if(AccRawVector.X >  3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn
-
 
635
        else
-
 
636
        if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn
-
 
637
        else
-
 
638
        if(AccRawVector.Y >  3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn
-
 
639
        else
-
 
640
        if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten  - 'front' nach vorn
-
 
641
        return(0);
Line 619... Line 642...
619
 
642
}
620
 
643
 
621
// ---------------------------------------------------------------------
644
// ---------------------------------------------------------------------
622
u8 NCMAG_SetMagConfig(void)
645
u8 NCMAG_SetMagConfig(void)
-
 
646
{
623
{
647
        u8 retval = 0;
624
        u8 retval = 0;
648
 
625
        // try to catch the i2c buffer within 100 ms timeout
649
        // try to catch the i2c buffer within 100 ms timeout
626
        if(I2C_LockBufferFunc(100))
650
        if(I2CBus_LockBuffer(Compass_I2CPort, 100))
-
 
651
        {
-
 
652
                u8 TxBytes = 0;
627
        {
653
                u8 TxData[sizeof(MagConfig) + 3];
628
                u8 TxBytes = 0;
654
               
629
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;        
655
                TxData[TxBytes++] = REG_MAG_CRA;       
630
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
656
                memcpy(&TxData[TxBytes], (u8*)&MagConfig, sizeof(MagConfig));
631
                TxBytes += sizeof(MagConfig);
657
                TxBytes += sizeof(MagConfig);
632
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
658
                if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, 0, 0))
633
                {
659
                {
634
                        if(I2C_WaitForEndOfTransmissionFunc(100))
660
                        if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
635
                        {
661
                        {
636
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
662
                                if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
637
                        }
663
                        }
638
                }
664
                }
639
        }
665
        }
Line 643... Line 669...
643
// ----------------------------------------------------------------------------------------
669
// ----------------------------------------------------------------------------------------
644
u8 NCMAG_GetMagConfig(void)
670
u8 NCMAG_GetMagConfig(void)
645
{
671
{
646
        u8 retval = 0;
672
        u8 retval = 0;
647
        // try to catch the i2c buffer within 100 ms timeout
673
        // try to catch the i2c buffer within 100 ms timeout
648
        if(I2C_LockBufferFunc(100))
674
        if(I2CBus_LockBuffer(Compass_I2CPort, 100))
649
        {
675
        {
650
                u8 TxBytes = 0;
676
                u8 TxBytes = 0;
-
 
677
                u8 TxData[3];
651
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
678
                TxData[TxBytes++] = REG_MAG_CRA;
652
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
679
                if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
653
                {
680
                {
654
                        if(I2C_WaitForEndOfTransmissionFunc(100))
681
                        if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
655
                        {
682
                        {
656
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
683
                                if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
657
                        }
684
                        }
658
                }
685
                }
659
        }
686
        }
660
        return(retval);        
687
        return(retval);        
661
}
688
}
Line 662... Line 689...
662
 
689
 
663
// ----------------------------------------------------------------------------------------
690
// ----------------------------------------------------------------------------------------
664
u8 NCMAG_SetAccConfig(void)
691
u8 NCMAG_SetAccConfig(void)
665
{
692
{
666
        u8 retval = 0;
693
        u8 retval = 0;
667
        // try to catch the i2c buffer within 100 ms timeout
694
        // try to catch the i2c buffer within 50 ms timeout
668
        if(I2C_LockBufferFunc(50))
695
        if(I2CBus_LockBuffer(Compass_I2CPort, 50))
669
        {
696
        {
-
 
697
                u8 TxBytes = 0;
670
                u8 TxBytes = 0;
698
                u8 TxData[sizeof(AccConfig) + 3];
671
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;    
699
                TxData[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;  
672
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
700
                memcpy(&TxData[TxBytes], (u8*)&AccConfig, sizeof(AccConfig));
673
                TxBytes += sizeof(AccConfig);
701
                TxBytes += sizeof(AccConfig);
674
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
702
                if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, 0, 0))
675
                {
703
                {
676
                        if(I2C_WaitForEndOfTransmissionFunc(50))
704
                        if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 50))
677
                        {
705
                        {
678
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
706
                                if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
679
                        }
707
                        }
680
                }
708
                }
681
        }
709
        }
682
        return(retval);        
710
        return(retval);        
Line 685... Line 713...
685
// ----------------------------------------------------------------------------------------
713
// ----------------------------------------------------------------------------------------
686
u8 NCMAG_GetAccConfig(void)
714
u8 NCMAG_GetAccConfig(void)
687
{
715
{
688
        u8 retval = 0;
716
        u8 retval = 0;
689
        // try to catch the i2c buffer within 100 ms timeout
717
        // try to catch the i2c buffer within 100 ms timeout
690
        if(I2C_LockBufferFunc(100))
718
        if(I2CBus_LockBuffer(Compass_I2CPort, 100))
691
        {
719
        {
692
                u8 TxBytes = 0;
720
                u8 TxBytes = 0;
-
 
721
                u8 TxData[3];
693
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
722
                TxData[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
694
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
723
                if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
695
                {
724
                {
696
                        if(I2C_WaitForEndOfTransmissionFunc(100))
725
                        if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
697
                        {
726
                        {
698
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
727
                                if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
699
                        }
728
                        }
700
                }
729
                }
701
        }
730
        }
702
        return(retval);        
731
        return(retval);        
703
}
732
}
Line 705... Line 734...
705
// ----------------------------------------------------------------------------------------
734
// ----------------------------------------------------------------------------------------
706
u8 NCMAG_GetIdentification(void)
735
u8 NCMAG_GetIdentification(void)
707
{
736
{
708
        u8 retval = 0;
737
        u8 retval = 0;
709
        // try to catch the i2c buffer within 100 ms timeout
738
        // try to catch the i2c buffer within 100 ms timeout
710
        if(I2C_LockBufferFunc(100))
739
        if(I2CBus_LockBuffer(Compass_I2CPort, 100))
711
        {
740
        {
712
                u16 TxBytes = 0;
741
                u8 TxBytes = 0;
-
 
742
                u8 TxData[3];
713
                NCMAG_Identification.A = 0xFF;
743
                NCMAG_Identification.A = 0xFF;
714
                NCMAG_Identification.B = 0xFF;
744
                NCMAG_Identification.B = 0xFF;
715
                NCMAG_Identification.C = 0xFF;
745
                NCMAG_Identification.C = 0xFF;
716
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDA;
746
                TxData[TxBytes++] = REG_MAG_IDA;
717
                // initiate transmission
747
                // initiate transmission
718
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
748
                if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
719
                {
749
                {
720
                        if(I2C_WaitForEndOfTransmissionFunc(100))
750
                        if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
721
                        {
751
                        {
722
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
752
                                if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
723
                        }
753
                        }
724
                }
754
                }
725
        }
755
        }
726
        return(retval);
756
        return(retval);
727
}
757
}
Line 728... Line 758...
728
 
758
 
729
u8 NCMAG_GetIdentification_Sub(void)
759
u8 NCMAG_GetIdentification_Sub(void)
730
{
760
{
731
        u8 retval = 0;
761
        u8 retval = 0;
732
        // try to catch the i2c buffer within 100 ms timeout
762
        // try to catch the i2c buffer within 100 ms timeout
733
        if(I2C_LockBufferFunc(100))
763
        if(I2CBus_LockBuffer(Compass_I2CPort, 100))
734
        {
764
        {
-
 
765
                u8 TxBytes = 0;
735
                u16 TxBytes = 0;
766
                u8 TxData[3];
736
                NCMAG_Identification2.Sub = 0xFF;
767
                NCMAG_Identification2.Sub = 0xFF;
737
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDF;
768
                TxData[TxBytes++] = REG_MAG_IDF;
738
                // initiate transmission
769
                // initiate transmission
739
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
770
                if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
740
                {
771
                {
741
                        if(I2C_WaitForEndOfTransmissionFunc(100))
772
                        if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
742
                        {
773
                        {
743
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
774
                                if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
744
                        }
775
                        }
745
                }
776
                }
746
        }
777
        }
747
        return(retval);
778
        return(retval);
Line 748... Line 779...
748
}
779
}
749
 
780
 
750
 
781
 
751
// ----------------------------------------------------------------------------------------
782
// ----------------------------------------------------------------------------------------
752
void NCMAG_GetMagVector(void)
783
void NCMAG_GetMagVector(u8 timeout)
753
{
784
{
754
        // try to catch the I2C buffer within 0 ms
785
        // try to catch the I2C buffer within timeout ms
-
 
786
        if(I2CBus_LockBuffer(Compass_I2CPort, timeout))
755
        if(I2C_LockBufferFunc(5))
787
        {
756
        {
788
                u8 TxBytes = 0;
757
                u16 TxBytes = 0;
789
                u8 TxData[3];
758
                // set register pointer
790
                // set register pointer
759
                I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB;
791
                TxData[TxBytes++] = REG_MAG_DATAX_MSB;
760
                // initiate transmission
792
                // initiate transmission
Line 761... Line 793...
761
                I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
793
                I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
762
        }
794
        }
763
}
795
}
764
 
796
 
765
//----------------------------------------------------------------
797
//----------------------------------------------------------------
766
void NCMAG_GetAccVector(u8 timeout)
798
void NCMAG_GetAccVector(u8 timeout)
767
{
799
{
-
 
800
        // try to catch the I2C buffer within timeout ms
768
        // try to catch the I2C buffer within 0 ms
801
        if(I2CBus_LockBuffer(Compass_I2CPort, timeout))
769
        if(I2C_LockBufferFunc(timeout))
802
        {
770
        {
803
                u8 TxBytes = 0;
771
                u16 TxBytes = 0;
804
                u8 TxData[3];
772
                // set register pointer
-
 
773
                I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
-
 
774
                // initiate transmission
-
 
775
                I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
805
                // set register pointer
776
//DebugOut.Analog[16] = AccRawVector.X;
806
                TxData[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
Line 777... Line 807...
777
//DebugOut.Analog[17] = AccRawVector.Y;
807
                // initiate transmission
778
//DebugOut.Analog[18] = AccRawVector.Z;
808
                I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
779
        }
809
        }
780
}
810
}
Line 781... Line 811...
781
 
811
 
782
//----------------------------------------------------------------
812
//----------------------------------------------------------------
Line 822... Line 852...
822
void NCMAG_Update(u8 init)
852
void NCMAG_Update(u8 init)
823
{
853
{
824
        static u32 TimerUpdate = 0;
854
        static u32 TimerUpdate = 0;
825
        static s8 send_config = 0;
855
        static s8 send_config = 0;
826
        u32 delay = 20;
856
        u32 delay = 20;
-
 
857
       
827
        if(init) TimerUpdate = SetDelay(10);
858
        if(init) TimerUpdate = SetDelay(10);
Line 828... Line -...
828
 
-
 
829
        // todo State Handling for both busses  !!
859
 
830
        if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF)/* || !NCMAG_Present*/ )
860
        if( (I2CBus(Compass_I2CPort)->State == I2C_STATE_UNDEF) /*|| !NCMAG_Present*/ )
831
        {
861
        {
832
                Compass_Heading = -1;
862
                Compass_Heading = -1;
833
                DebugOut.Analog[14]++; // count I2C error
863
                DebugOut.Analog[14]++; // count I2C error
834
                TimerUpdate = SetDelay(10);
864
                TimerUpdate = SetDelay(10);
835
                return;
865
                return;
836
        }
866
        }
837
        if(CheckDelay(TimerUpdate))// && I2C0_State == I2C_STATE_IDLE && I2C1_State == I2C_STATE_IDLE)
867
        if(CheckDelay(TimerUpdate))
838
        {
868
        {
839
                if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
869
                if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
840
        if(++send_config == 25)   // 500ms
870
        if(++send_config == 25)   // 500ms
841
                {
871
                {
842
                        send_config = -25;    // next try after 1 second
872
                        send_config = -25;    // next try after 1 second
843
                InitNC_MagnetSensor();
873
                NCMAG_ConfigureSensor();
844
                        TimerUpdate = SetDelay(20);    // back into the old time-slot
874
                        TimerUpdate = SetDelay(20);    // back into the old time-slot
845
                }
875
                }
846
                else
876
                else
847
                {
877
                {
Line 851... Line 881...
851
                        if(Compass_CalState) NCMAG_Calibrate();
881
                        if(Compass_CalState) NCMAG_Calibrate();
Line 852... Line 882...
852
                       
882
                       
853
                        // in case of LSM303 type
883
                        // in case of LSM303 type
854
                        switch(NCMAG_SensorType)
884
                        switch(NCMAG_SensorType)
855
                        {
885
                        {
-
 
886
                                case TYPE_HMC5843:
856
                                case TYPE_HMC5843:                             
887
                                        delay = 20;      // next cycle after 20 ms                      
857
                                        NCMAG_GetMagVector();
-
 
858
                                        delay = 20;
888
                                        NCMAG_GetMagVector(5);
859
                                        break;
889
                                        break;
860
                                case TYPE_LSM303DLH:
890
                                case TYPE_LSM303DLH:
861
                                case TYPE_LSM303DLM:
-
 
862
                                        delay = 20;
891
                                case TYPE_LSM303DLM:
863
//delay = 2;
892
                                       
864
                                        if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector();
893
                                        if(s-- || (Compass_I2CPort == NCMAG_PORT_INTERN))
-
 
894
                                        {
-
 
895
                                                delay = 20;      // next cycle after 20 ms
865
                                        else
896
                                                NCMAG_GetMagVector(5);
-
 
897
                                        }
-
 
898
                                        else // having an external compass, read every 50th cycle the ACC vec 
866
                                         {
899
                                        {       // try to initialize if no data are there 
-
 
900
                                                if((AccRawVector.X + AccRawVector.Y + AccRawVector.Z) == 0) NCMAG_Init_ACCSensor();
867
                                          if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor();
901
                                                // get new data
868
                                          NCMAG_GetAccVector(5);
902
                                                NCMAG_GetAccVector(5);
869
                                          delay = 10;
903
                                                delay = 10; // next cycle after 10 ms
870
                                          s = 40;  // about 0,8 sec
904
                                                s = 40; //reset downconter about 0,8 sec
871
                                         };
-
 
872
                                        if(!s) delay = 10; // ACC-Reading in the next step after 10ms
-
 
873
//if(!s) delay = 2; // ACC-Reading in the next step after 10ms
905
                                        }
874
                                        break;                           
906
                                        break;                           
875
                        }
907
                        }
876
                        if(send_config == 24) TimerUpdate = SetDelay(15);    // next event is the re-configuration
908
                        if(send_config == 24) TimerUpdate = SetDelay(15);    // next event is the re-configuration
877
                        else TimerUpdate = SetDelay(delay);    // every 20 ms are 50 Hz
909
                        else TimerUpdate = SetDelay(delay);    // every 20 ms are 50 Hz
Line 933... Line 965...
933
        while(!CheckDelay(time));
965
        while(!CheckDelay(time));
934
        // averaging
966
        // averaging
935
        #define AVERAGE 20
967
        #define AVERAGE 20
936
        for(i = 0; i<AVERAGE; i++)
968
        for(i = 0; i<AVERAGE; i++)
937
        {
969
        {
938
                NCMAG_GetMagVector();
970
                NCMAG_GetMagVector(5);
939
                time = SetDelay(20);
971
                time = SetDelay(20);
940
        while(!CheckDelay(time));
972
        while(!CheckDelay(time));
941
                XMax += MagRawVector.X;
973
                XMax += MagRawVector.X;
942
                YMax += MagRawVector.Y;
974
                YMax += MagRawVector.Y;
943
                ZMax += MagRawVector.Z;
975
                ZMax += MagRawVector.Z;
Line 949... Line 981...
949
        time = SetDelay(50);
981
        time = SetDelay(50);
950
        while(!CheckDelay(time));
982
        while(!CheckDelay(time));
951
        // averaging
983
        // averaging
952
        for(i = 0; i < AVERAGE; i++)
984
        for(i = 0; i < AVERAGE; i++)
953
        {
985
        {
954
                NCMAG_GetMagVector();
986
                NCMAG_GetMagVector(5);
955
                time = SetDelay(20);
987
                time = SetDelay(20);
956
        while(!CheckDelay(time));
988
        while(!CheckDelay(time));
957
                XMin += MagRawVector.X;
989
                XMin += MagRawVector.X;
958
                YMin += MagRawVector.Y;
990
                YMin += MagRawVector.Y;
959
                ZMin += MagRawVector.Z;
991
                ZMin += MagRawVector.Z;
Line 991... Line 1023...
991
        done = retval;
1023
        done = retval;
992
        return(retval);
1024
        return(retval);
993
}
1025
}
Line 994... Line -...
994
 
-
 
995
 
1026
 
996
//----------------------------------------------------------------
-
 
997
void NCMAG_SelectI2CBus(u8 busno)
-
 
998
{
-
 
999
  if (busno == 0)
-
 
1000
  {
-
 
1001
    I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission;
-
 
1002
        I2C_LockBufferFunc = &I2C0_LockBuffer;
1027
 
1003
        I2C_TransmissionFunc = &I2C0_Transmission;
1028
void NCMAG_CheckOrientation(void)
1004
        I2C_BufferPnt = I2C0_Buffer;
-
 
1005
        I2C_ErrorPnt = &I2C0_Error;
-
 
1006
  }
1029
{       // only for external sensor
1007
   else
1030
        if(Compass_I2CPort == NCMAG_PORT_EXTERN)
1008
  {
-
 
1009
    I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission;
1031
        {
1010
        I2C_LockBufferFunc = &I2C1_LockBuffer;
1032
                NCMAG_Orientation = NCMAG_GetOrientationFromAcc();
1011
        I2C_TransmissionFunc = &I2C1_Transmission;
-
 
1012
        I2C_BufferPnt = I2C1_Buffer;
1033
                if(NCMAG_Orientation != (Calibration.Version>>4)) NCMAG_IsCalibrated = 0;
1013
        I2C_ErrorPnt = &I2C1_Error;
1034
                else NCMAG_IsCalibrated = 1;
1014
  }
-
 
1015
}
1035
        }
1016
 
1036
}
1017
//----------------------------------------------------------------
1037
//----------------------------------------------------------------
1018
u8 NCMAG_Init(void)
1038
u8 NCMAG_Init(void)
1019
{
1039
{
1020
        u8 msg[64];
1040
        MagRawVector.X = 0;
1021
        u8 retval = 0;
-
 
1022
        u8 repeat = 0;
-
 
1023
 
1041
    MagRawVector.Y = 0;
1024
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1025
// Search external sensor
1042
    MagRawVector.Z = 0;
1026
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1043
        AccRawVector.X = 0;
1027
        I2C_CompassPort = I2C_EXTERN_0;
1044
        AccRawVector.Y = 0;
1028
        NCMAG_SelectI2CBus(I2C_CompassPort);
-
 
1029
 
-
 
1030
        // get id bytes
-
 
1031
        retval = 0;
-
 
1032
    for(repeat = 0; repeat < 5; repeat++)
-
 
1033
        {
1045
        AccRawVector.Z = 0;
1034
//              retval = NCMAG_GetIdentification();
-
 
1035
                retval = NCMAG_GetAccConfig();            // only the sensor with ACC is supported
-
 
1036
                if(retval) break; // break loop on success
-
 
1037
                UART1_PutString("_");
-
 
1038
        }
-
 
1039
//retval = 1;
-
 
1040
        if(!retval)
-
 
1041
        {
-
 
1042
         UART1_PutString(" internal sensor");
-
 
1043
         I2C_CompassPort = I2C_INTERN_1;
-
 
1044
         NCMAG_SelectI2CBus(I2C_CompassPort);
-
 
1045
        }
1046
               
-
 
1047
        if(NCMAG_Present) // do only short init ! , full init was called before
-
 
1048
        {
-
 
1049
                // reset I2C Bus
-
 
1050
                I2CBus_Deinit(Compass_I2CPort);
-
 
1051
                I2CBus_Init(Compass_I2CPort);
-
 
1052
                // try to reconfigure senor
-
 
1053
                NCMAG_ConfigureSensor();
-
 
1054
                NCMAG_Update(1);
-
 
1055
        }
-
 
1056
        else  // full init
-
 
1057
        {
-
 
1058
                u8 msg[64];
-
 
1059
                u8 retval = 0;
-
 
1060
                u8 repeat = 0;
-
 
1061
       
-
 
1062
                //--------------------------------------------
-
 
1063
                // search external sensor first
-
 
1064
                //--------------------------------------------
-
 
1065
                Compass_I2CPort = NCMAG_PORT_EXTERN;
-
 
1066
                // get id bytes
-
 
1067
                retval = 0;
-
 
1068
                for(repeat = 0; repeat < 5; repeat++)
-
 
1069
                {
-
 
1070
                        //retval = NCMAG_GetIdentification();
-
 
1071
                        retval = NCMAG_GetAccConfig();            // only the external sensor with ACC is supported
-
 
1072
                        if(retval) break; // break loop on success
-
 
1073
                        UART1_PutString("_");
-
 
1074
                }
-
 
1075
                // Extenal sensor not found?
-
 
1076
                if(!retval)
1046
        else
1077
                {
-
 
1078
                        // search internal sensor afterwards
1047
        {
1079
                        UART1_PutString(" internal sensor");
-
 
1080
                        Compass_I2CPort = NCMAG_PORT_INTERN;
-
 
1081
                        NCMAG_Orientation = 0;
-
 
1082
                }
-
 
1083
                else
Line -... Line 1084...
-
 
1084
                {
-
 
1085
                        UART1_PutString(" external sensor ");
-
 
1086
 
1048
         UART1_PutString(" external sensor ");
1087
                        // initialize ACC sensor of the IC
1049
         NCMAG_Init_ACCSensor();
1088
                        NCMAG_Init_ACCSensor();
1050
 
1089
                        // try to get orientation by acc sensor values  
1051
         for(repeat = 0; repeat < 100; repeat++)
1090
                        for(repeat = 0; repeat < 100; repeat++)
1052
          {
-
 
1053
           NCMAG_GetAccVector(10); // only the sensor with ACC is supported
1091
                        {
1054
       ExtCompassOrientation = GetExtCompassOrientation();       
1092
                                NCMAG_GetAccVector(10); // only the sensor with ACC is supported
1055
           if(ExtCompassOrientation && (ExtCompassOrientation == Calibration.Version / 16)) break;
1093
                                NCMAG_Orientation = NCMAG_GetOrientationFromAcc();       
1056
           //UART1_Putchar('-');
-
 
1057
          }
1094
                                if(NCMAG_Orientation) break;
1058
//DebugOut.Analog[19] = repeat;
-
 
1059
 
1095
                        }
1060
     if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)");
-
 
1061
         else
1096
                        // check orientation result
1062
         {
1097
                        if(NCMAG_Orientation)
1063
          NCMag_CalibrationRead(I2C_CompassPort);
-
 
1064
          sprintf(msg, "with orientation: %d ",ExtCompassOrientation );
1098
                        {
1065
          UART1_PutString(msg);
-
 
1066
          if(ExtCompassOrientation != Calibration.Version / 16)
1099
                                sprintf(msg, "with orientation: %d ", NCMAG_Orientation );
1067
           {
1100
                                UART1_PutString(msg);
1068
            sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version / 16);
1101
                        }
1069
            UART1_PutString(msg);
1102
                        else
1070
           }
-
 
1071
          else UART1_PutString("ok ");
1103
                        {      
1072
         }
1104
                                UART1_PutString(" (Orientation unknown!)");
1073
 
1105
                        }
1074
        }
1106
                }
1075
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1107
                //-------------------------------------------
1076
 
1108
       
1077
        NCMAG_Present = 0;
-
 
1078
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
-
 
1079
        // polling for LSM302DLH/DLM option by ACC address ack
-
 
1080
    for(repeat = 0; repeat < 3; repeat++)
-
 
1081
        {
-
 
1082
                retval = NCMAG_GetAccConfig();
1109
                NCMAG_Present = 0;
1083
                if(retval) break; // break loop on success
-
 
1084
        }
-
 
1085
        if(retval)
-
 
1086
        {
-
 
1087
                // initialize ACC sensor
-
 
1088
                NCMAG_Init_ACCSensor();
-
 
1089
 
1110
                NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
1090
                NCMAG_SensorType = TYPE_LSM303DLH;     
1111
                // polling for LSM302DLH/DLM option by ACC address ack
1091
                // polling of sub identification
1112
                repeat = 0;
1092
            for(repeat = 0; repeat < 12; repeat++)
1113
                for(repeat = 0; repeat < 3; repeat++)
1093
                {
1114
                {
1094
                        retval = NCMAG_GetIdentification_Sub();
1115
                        retval = NCMAG_GetAccConfig();
1095
                        if(retval) break; // break loop on success
1116
                        if(retval) break; // break loop on success
1096
                }
1117
                }
1097
                if(retval)
1118
                if(retval)
1098
                {
1119
                {
-
 
1120
                        // initialize ACC sensor
-
 
1121
                        NCMAG_Init_ACCSensor();
1099
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
1122
       
1100
                }      
1123
                        NCMAG_SensorType = TYPE_LSM303DLH;     
1101
        }
1124
                        // polling of sub identification
1102
        // get id bytes
1125
                        repeat = 0;
1103
    for(repeat = 0; repeat < 3; repeat++)
1126
                        for(repeat = 0; repeat < 12; repeat++)
1104
        {
1127
                        {
1105
                retval = NCMAG_GetIdentification();
-
 
1106
                if(retval) break; // break loop on success
-
 
1107
        }
1128
                                retval = NCMAG_GetIdentification_Sub();
1108
 
1129
                                if(retval) break; // break loop on success              
1109
        // if we got an answer to id request
1130
                        }
1110
        if(retval)
1131
                        if(retval)
-
 
1132
                        {
1111
        {
1133
                                if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
1112
                u8 n1[] = "\n\r HMC5843";
1134
                        }      
1113
                u8 n2[] = "\n\r LSM303DLH";
-
 
1114
                u8 n3[] = "\n\r LSM303DLM";
1135
                }
1115
                u8* pn = n1;
1136
                // get id bytes
1116
               
-
 
1117
                switch(NCMAG_SensorType)
-
 
1118
                {
-
 
1119
                        case TYPE_HMC5843:
1137
                retval = 0;
1120
                                pn = n1;
-
 
1121
                                break;
-
 
1122
                        case TYPE_LSM303DLH:
1138
                for(repeat = 0; repeat < 3; repeat++)
1123
                                pn = n2;
-
 
1124
                                break;
-
 
1125
                        case TYPE_LSM303DLM:
1139
                {
1126
                                pn = n3;
1140
                        retval = NCMAG_GetIdentification();
1127
                                break;
-
 
1128
                }
-
 
1129
 
1141
                        if(retval) break; // break loop on success
1130
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
1142
                }
1131
                UART1_PutString(msg);
-
 
1132
                if (    (NCMAG_Identification.A == MAG_IDA)
1143
       
-
 
1144
                // if we got an answer to id request
-
 
1145
                if(retval)
-
 
1146
                {
1133
                     && (NCMAG_Identification.B == MAG_IDB)
1147
                        u8 n1[] = "\n\r HMC5843";
1134
                         && (NCMAG_Identification.C == MAG_IDC))
1148
                        u8 n2[] = "\n\r LSM303DLH";
1135
                {
1149
                        u8 n3[] = "\n\r LSM303DLM";
1136
                        NCMAG_Present = 1;
1150
                        u8* pn = n1;
-
 
1151
                       
-
 
1152
                        switch(NCMAG_SensorType)
-
 
1153
                        {
1137
 
1154
                                case TYPE_HMC5843:
-
 
1155
                                        pn = n1;
-
 
1156
                                        break;
1138
                        if(EEPROM_Init())
1157
                                case TYPE_LSM303DLH:
-
 
1158
                                        pn = n2;
-
 
1159
                                        break;
1139
                        {
1160
                                case TYPE_LSM303DLM:
-
 
1161
                                        pn = n3;
-
 
1162
                                        break;
-
 
1163
                        }
-
 
1164
       
-
 
1165
                        sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
-
 
1166
                        UART1_PutString(msg);
-
 
1167
                        if (    (NCMAG_Identification.A == MAG_IDA)
-
 
1168
                             && (NCMAG_Identification.B == MAG_IDB)
-
 
1169
                                 && (NCMAG_Identification.C == MAG_IDC))
-
 
1170
                        {
-
 
1171
                                NCMAG_Present = 1;
-
 
1172
       
-
 
1173
                                if(EEPROM_Init())
-
 
1174
                                {
-
 
1175
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort);
-
 
1176
                                        if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
-
 
1177
                                        else // valid calibration data in EEPROM
-
 
1178
                                        {       // check current orientation
-
 
1179
                                                if(NCMAG_Orientation != Calibration.Version >> 4)
-
 
1180
                                                {
-
 
1181
                                                        sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version >> 4);
-
 
1182
                                                        UART1_PutString(msg);
-
 
1183
                                                }
1140
                                NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
1184
                                                else UART1_PutString("ok ");   
1141
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
1185
                                        }
1142
                        }
1186
                                }
1143
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
1187
                                else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
1144
                        // perform self test
1188
                                // perform self test
-
 
1189
                                if(!NCMAG_SelfTest())
-
 
1190
                                {
-
 
1191
                                        UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
-
 
1192
                                        LED_RED_ON;
-
 
1193
                                        //NCMAG_IsCalibrated = 0;
-
 
1194
                                }
-
 
1195
                                else UART1_PutString("\r\n Selftest ok");
-
 
1196
       
-
 
1197
                                // initialize magnetic sensor configuration
-
 
1198
                                NCMAG_ConfigureSensor();
-
 
1199
                        }
-
 
1200
                        else
1145
                        if(!NCMAG_SelfTest())
1201
                        {
1146
                        {
-
 
1147
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
1202
                                UART1_PutString("\n\r Not compatible!");
1148
                                LED_RED_ON;
-
 
1149
//                              NCMAG_IsCalibrated = 0;
-
 
1150
                        }
-
 
1151
                        else UART1_PutString("\r\n Selftest ok");
-
 
1152
 
1203
                                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
1153
                        // initialize magnetic sensor configuration
1204
                                LED_RED_ON;
1154
                        InitNC_MagnetSensor();
1205
                        }
-
 
1206
                }
1155
                }
1207
                else // nothing found
1156
                else
-
 
1157
                {
-
 
1158
                        UART1_PutString("\n\r Not compatible!");
1208
                {
1159
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
1209
                        NCMAG_SensorType = TYPE_NONE;
1160
                        LED_RED_ON;
-
 
1161
                }
-
 
1162
        }
-
 
1163
        else // nothing found
-
 
1164
        {
-
 
1165
                NCMAG_SensorType = TYPE_NONE;
1210
                        UART1_PutString("not found!");  
1166
                UART1_PutString("not found!");  
1211
                }