Subversion Repositories NaviCtrl

Rev

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

Rev 360 Rev 394
Line 67... Line 67...
67
#include "main.h"
67
#include "main.h"
Line 68... Line 68...
68
 
68
 
69
u8 NCMAG_Present = 0;
69
u8 NCMAG_Present = 0;
Line -... Line 70...
-
 
70
u8 NCMAG_IsCalibrated = 0;
70
u8 NCMAG_IsCalibrated = 0;
71
 
71
 
72
// supported magnetic sensor types
72
#define MAG_TYPE_NONE           0
73
#define TYPE_NONE                       0
-
 
74
#define TYPE_HMC5843            1
-
 
75
#define TYPE_LSM303DLH          2
73
#define MAG_TYPE_HMC5843        1
76
#define TYPE_LSM303DLM          3
Line 74... Line -...
74
#define MAG_TYPE_LSM303DLH      2
-
 
75
u8 NCMAG_MagType = MAG_TYPE_NONE;
77
 
-
 
78
u8 NCMAG_SensorType = TYPE_NONE;
76
 
79
 
Line 77... Line 80...
77
#define CALIBRATION_VERSION                     1
80
#define EEPROM_ADR_MAG_CALIBRATION              50
78
#define EEPROM_ADR_MAG_CALIBRATION              50
81
#define CALIBRATION_VERSION                     1
79
#define MAG_CALIBRATION_COMPATIBEL              0xA2
82
#define MAG_CALIBRATION_COMPATIBLE              0xA2
Line 117... Line 120...
117
#define REG_MAG_STATUS          0x09
120
#define REG_MAG_STATUS          0x09
Line 118... Line 121...
118
 
121
 
119
#define REG_MAG_IDA                     0x0A
122
#define REG_MAG_IDA                     0x0A
120
#define REG_MAG_IDB                     0x0B
123
#define REG_MAG_IDB                     0x0B
121
#define REG_MAG_IDC                     0x0C
124
#define REG_MAG_IDC                     0x0C
Line 122... Line 125...
122
#define REG_MAG_IDF                     0x0F
125
#define REG_MAG_IDF                     0x0F  // WHO_AM_I _M = 0x03c when LSM303DLM is connected
123
 
126
 
124
// bit mask for configuration mode
127
// bit mask for configuration mode
125
#define CRA_MODE_MASK           0x03
128
#define CRA_MODE_MASK           0x03
Line 143... Line 146...
143
 
146
 
144
// ids
147
// ids
145
#define MAG_IDA         0x48
148
#define MAG_IDA         0x48
146
#define MAG_IDB         0x34
149
#define MAG_IDB         0x34
-
 
150
#define MAG_IDC         0x33
Line 147... Line 151...
147
#define MAG_IDC         0x33
151
#define MAG_IDF_LSM303DLM       0x3C
148
 
152
 
149
// the special HMC5843 interface
153
// the special HMC5843 interface
150
// bit mask for rate
154
// bit mask for rate
Line 166... Line 170...
166
#define HMC5843_CRB_GAIN_65GA           0xE0
170
#define HMC5843_CRB_GAIN_65GA           0xE0
167
// self test value
171
// self test value
168
#define HMC5843_TEST_XSCALE             555
172
#define HMC5843_TEST_XSCALE             555
169
#define HMC5843_TEST_YSCALE             555
173
#define HMC5843_TEST_YSCALE             555
170
#define HMC5843_TEST_ZSCALE             555
174
#define HMC5843_TEST_ZSCALE             555
171
// clibration range
175
// calibration range
172
#define HMC5843_CALIBRATION_RANGE   600
176
#define HMC5843_CALIBRATION_RANGE   600
Line 173... Line 177...
173
 
177
 
174
// the special LSM302DLH interface
178
// the special LSM302DLH interface
175
// bit mask for rate
179
// bit mask for rate
Line 188... Line 192...
188
#define LSM303DLH_CRB_GAIN_25GA         0x60
192
#define LSM303DLH_CRB_GAIN_25GA         0x60
189
#define LSM303DLH_CRB_GAIN_40GA         0x80
193
#define LSM303DLH_CRB_GAIN_40GA         0x80
190
#define LSM303DLH_CRB_GAIN_47GA         0xA0
194
#define LSM303DLH_CRB_GAIN_47GA         0xA0
191
#define LSM303DLH_CRB_GAIN_56GA         0xC0
195
#define LSM303DLH_CRB_GAIN_56GA         0xC0
192
#define LSM303DLH_CRB_GAIN_81GA         0xE0
196
#define LSM303DLH_CRB_GAIN_81GA         0xE0
-
 
197
 
-
 
198
typedef struct
-
 
199
{
-
 
200
        u8 A;
-
 
201
        u8 B;
-
 
202
        u8 C;
-
 
203
} __attribute__((packed)) Identification_t;
-
 
204
volatile Identification_t NCMAG_Identification;
-
 
205
 
-
 
206
typedef struct
-
 
207
{
-
 
208
        u8 Sub;
-
 
209
} __attribute__((packed)) Identification2_t;
-
 
210
volatile Identification2_t NCMAG_Identification2;
-
 
211
 
-
 
212
typedef struct
-
 
213
{
-
 
214
        u8 cra;
-
 
215
        u8 crb;
-
 
216
        u8 mode;
-
 
217
} __attribute__((packed)) MagConfig_t;
-
 
218
 
-
 
219
volatile MagConfig_t MagConfig;
-
 
220
 
-
 
221
 
193
// self test value
222
// self test value
194
#define LSM303DLH_TEST_XSCALE   495
223
#define LSM303DLH_TEST_XSCALE   495
195
#define LSM303DLH_TEST_YSCALE   495
224
#define LSM303DLH_TEST_YSCALE   495
196
#define LSM303DLH_TEST_ZSCALE   470
225
#define LSM303DLH_TEST_ZSCALE   470
197
// clibration range
226
// clibration range
198
#define LSM303_CALIBRATION_RANGE   550
227
#define LSM303_CALIBRATION_RANGE   550
Line 199... Line 228...
199
 
228
 
200
// the i2c ACC interface
229
// the i2c ACC interface
-
 
230
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
-
 
231
 
-
 
232
// multiple byte read/write mask
-
 
233
#define REG_ACC_MASK_AUTOINCREMENT 0x80
201
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
234
 
202
// register mapping
235
// register mapping
203
#define REG_ACC_CTRL1                   0x20
236
#define REG_ACC_CTRL1                   0x20
204
#define REG_ACC_CTRL2                   0x21
237
#define REG_ACC_CTRL2                   0x21
205
#define REG_ACC_CTRL3                   0x22
238
#define REG_ACC_CTRL3                   0x22
Line 213... Line 246...
213
#define REG_ACC_Y_LSB                   0x2A
246
#define REG_ACC_Y_LSB                   0x2A
214
#define REG_ACC_Y_MSB                   0x2B
247
#define REG_ACC_Y_MSB                   0x2B
215
#define REG_ACC_Z_LSB                   0x2C
248
#define REG_ACC_Z_LSB                   0x2C
216
#define REG_ACC_Z_MSB                   0x2D
249
#define REG_ACC_Z_MSB                   0x2D
Line -... Line 250...
-
 
250
 
-
 
251
#define ACC_CRTL1_PM_DOWN               0x00
-
 
252
#define ACC_CRTL1_PM_NORMAL             0x20
-
 
253
#define ACC_CRTL1_PM_LOW_0_5HZ  0x40
-
 
254
#define ACC_CRTL1_PM_LOW_1HZ    0x60
-
 
255
#define ACC_CRTL1_PM_LOW_2HZ    0x80
-
 
256
#define ACC_CRTL1_PM_LOW_5HZ    0xA0
-
 
257
#define ACC_CRTL1_PM_LOW_10HZ   0xC0
-
 
258
// Output data rate in normal power mode
-
 
259
#define ACC_CRTL1_DR_50HZ               0x00
-
 
260
#define ACC_CRTL1_DR_100HZ              0x08
-
 
261
#define ACC_CRTL1_DR_400HZ              0x10
-
 
262
#define ACC_CRTL1_DR_1000HZ             0x18
-
 
263
// axis anable flags                    
-
 
264
#define ACC_CRTL1_XEN                   0x01
-
 
265
#define ACC_CRTL1_YEN                   0x02
-
 
266
#define ACC_CRTL1_ZEN                   0x04
-
 
267
 
-
 
268
#define ACC_CTRL4_BDU                   0x80 // Block data update, (0: continuos update; 1: output registers not updated between MSB and LSB reading)
-
 
269
#define ACC_CTRL4_BLE                   0x40 // Big/little endian, (0: data LSB @ lower address; 1: data MSB @ lower address)
-
 
270
#define ACC_CTRL4_FS_2G                 0x00
-
 
271
#define ACC_CTRL4_FS_4G                 0x10
-
 
272
#define ACC_CTRL4_FS_8G                 0x30
-
 
273
#define ACC_CTRL4_STSIGN_PLUS   0x00
-
 
274
#define ACC_CTRL4_STSIGN_MINUS  0x08
Line 217... Line -...
217
 
-
 
218
 
-
 
219
 
-
 
220
typedef struct
-
 
221
{
-
 
222
        u8 A;
-
 
223
        u8 B;
-
 
224
        u8 C;
-
 
225
} __attribute__((packed)) Identification_t;
-
 
226
volatile Identification_t NCMAG_Identification;
-
 
227
 
-
 
228
typedef struct
-
 
229
{
-
 
230
        u8 Sub;
-
 
231
} __attribute__((packed)) Identification2_t;
-
 
232
volatile Identification2_t NCMAG_Identification2;
-
 
233
 
-
 
234
typedef struct
-
 
235
{
-
 
236
        u8 cra;
-
 
237
        u8 crb;
275
#define ACC_CTRL4_ST_ENABLE             0x02
238
        u8 mode;
-
 
239
} __attribute__((packed)) MagConfig_t;
276
 
Line 240... Line 277...
240
 
277
#define ACC_CTRL5_STW_ON                0x03
241
volatile MagConfig_t MagConfig;
278
#define ACC_CTRL5_STW_OFF               0x00
242
 
279
 
243
typedef struct
280
typedef struct
Line 251... Line 288...
251
 
288
 
Line 252... Line 289...
252
volatile AccConfig_t AccConfig;
289
volatile AccConfig_t AccConfig;
253
 
290
 
254
u8 NCMag_CalibrationWrite(void)
291
u8 NCMag_CalibrationWrite(void)
255
{
292
{
256
        u8 i, crc = MAG_CALIBRATION_COMPATIBEL;
293
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
Line 257... Line 294...
257
        EEPROM_Result_t eres;
294
        EEPROM_Result_t eres;
258
        u8 *pBuff = (u8*)&Calibration;
295
        u8 *pBuff = (u8*)&Calibration;
Line 269... Line 306...
269
        return(i);     
306
        return(i);     
270
}
307
}
Line 271... Line 308...
271
 
308
 
272
u8 NCMag_CalibrationRead(void)
309
u8 NCMag_CalibrationRead(void)
273
{
310
{
274
        u8 i, crc = MAG_CALIBRATION_COMPATIBEL;
311
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
Line 275... Line 312...
275
        u8 *pBuff = (u8*)&Calibration;
312
        u8 *pBuff = (u8*)&Calibration;
276
 
313
 
277
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
314
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
Line 292... Line 329...
292
{
329
{
293
        u8 msg[64];
330
        u8 msg[64];
294
        static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
331
        static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
295
        static s16 X = 0, Y = 0, Z = 0;
332
        static s16 X = 0, Y = 0, Z = 0;
296
        static u8 OldCalState = 0;     
333
        static u8 OldCalState = 0;     
297
        s16 MinCaclibration = 450;
334
        s16 MinCalibration = 450;
Line 298... Line 335...
298
 
335
 
299
        X = (4*X + MagRawVector.X + 3)/5;
336
        X = (4*X + MagRawVector.X + 3)/5;
300
        Y = (4*Y + MagRawVector.Y + 3)/5;
337
        Y = (4*Y + MagRawVector.Y + 3)/5;
Line 334... Line 371...
334
               
371
               
335
                case 5:
372
                case 5:
336
                        // Save values
373
                        // Save values
337
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
374
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
338
                        {
-
 
339
//                              #define MIN_CALIBRATION    256
375
                        {
340
                                if(NCMAG_MagType == MAG_TYPE_HMC5843)
376
                                switch(NCMAG_SensorType)
-
 
377
                                {
341
                                 {
378
                                        case TYPE_HMC5843:
342
                                  UART1_PutString("\r\nHMC5843 calibration\n\r");
379
                                                UART1_PutString("\r\nHMC5843 calibration\n\r");
343
                                  MinCaclibration = HMC5843_CALIBRATION_RANGE;
380
                                                MinCalibration = HMC5843_CALIBRATION_RANGE;
-
 
381
                                                break;
344
                                 }
382
 
345
                                if(NCMAG_MagType == MAG_TYPE_LSM303DLH)
383
                                        case TYPE_LSM303DLH:
346
                                 {
384
                                        case TYPE_LSM303DLM:
347
                                  UART1_PutString("\r\n\r\nLSM303 calibration\n\r");
385
                                                UART1_PutString("\r\n\r\nLSM303 calibration\n\r");
-
 
386
                                                MinCalibration = LSM303_CALIBRATION_RANGE;
348
                                  MinCaclibration =LSM303_CALIBRATION_RANGE;
387
                                        break;
349
                                 }
388
                                }
350
                                if(EarthMagneticStrengthTheoretic)
389
                                if(EarthMagneticStrengthTheoretic)
351
                                 {
390
                                 {
352
                                  MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50;
391
                                  MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50;
353
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
392
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
354
                                  UART1_PutString(msg);
393
                                  UART1_PutString(msg);
355
                                 }
394
                                 }
Line 359... Line 398...
359
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
398
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
360
                                Calibration.MagY.Range = Ymax - Ymin;
399
                                Calibration.MagY.Range = Ymax - Ymin;
361
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
400
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
362
                                Calibration.MagZ.Range = Zmax - Zmin;
401
                                Calibration.MagZ.Range = Zmax - Zmin;
363
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
402
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
364
                                if((Calibration.MagX.Range > MinCaclibration) && (Calibration.MagY.Range > MinCaclibration) && (Calibration.MagZ.Range > MinCaclibration))
403
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
365
                                {
404
                                {
366
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
405
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
367
                                        BeepTime = 2500;
406
                                        BeepTime = 2500;
368
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
407
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
369
                                }
408
                                }
370
                                else
409
                                else
371
                                {
410
                                {
372
                                        UART1_PutString("\r\nCalibration FAILED - Values too low: ");
411
                                        UART1_PutString("\r\nCalibration FAILED - Values too low: ");
373
                                    if(Calibration.MagX.Range < MinCaclibration) UART1_PutString("X! ");
412
                                    if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! ");
374
                                    if(Calibration.MagY.Range < MinCaclibration) UART1_PutString("Y! ");
413
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
375
                                    if(Calibration.MagZ.Range < MinCaclibration) UART1_PutString("Z! ");
414
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
376
                                        UART1_PutString("\r\n");
415
                                        UART1_PutString("\r\n");
Line 377... Line 416...
377
 
416
 
378
                                        // restore old calibration data from eeprom
417
                                        // restore old calibration data from eeprom
379
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
418
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
Line 382... Line 421...
382
                                        UART1_PutString(msg);
421
                                        UART1_PutString(msg);
383
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
422
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
384
                                        UART1_PutString(msg);
423
                                        UART1_PutString(msg);
385
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
424
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
386
                                        UART1_PutString(msg);
425
                                        UART1_PutString(msg);
387
                                        sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration);
426
                                        sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration);
388
                                        UART1_PutString(msg);
427
                                        UART1_PutString(msg);
389
                        }
428
                        }
390
                        break;
429
                        break;
Line 391... Line 430...
391
                       
430
                       
Line 425... Line 464...
425
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
464
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
426
                raw = pRxBuffer[2]<<8;
465
                raw = pRxBuffer[2]<<8;
427
                raw+= pRxBuffer[3];
466
                raw+= pRxBuffer[3];
428
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
467
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
429
                {
468
                {
430
                  if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged
469
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  MagRawVector.Z = raw; // here Z and Y are exchanged
431
                  else MagRawVector.Y = raw;
470
                        else                                                                    MagRawVector.Y = raw;
432
                }
471
                }
433
                raw = pRxBuffer[4]<<8;
472
                raw = pRxBuffer[4]<<8;
434
                raw+= pRxBuffer[5];
473
                raw+= pRxBuffer[5];
435
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
474
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
436
                {
475
                {
437
                  if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged
476
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  MagRawVector.Y = raw; // here Z and Y are exchanged
438
                  else MagRawVector.Z = raw;
477
                        else                                                                    MagRawVector.Z = raw;
439
                }
478
                }
440
//MagRawVector.X += 2 * ((s32) FC.Poti[7] - 128);
-
 
441
        }
479
        }
442
        if(Compass_CalState || !NCMAG_IsCalibrated)
480
        if(Compass_CalState || !NCMAG_IsCalibrated)
443
        {       // mark out data invalid
481
        {       // mark out data invalid
444
                MagVector.X = MagRawVector.X;
482
                MagVector.X = MagRawVector.X;
445
                MagVector.Y = MagRawVector.Y;
483
                MagVector.Y = MagRawVector.Y;
Line 460... Line 498...
460
{       // if number of byte are matching
498
{       // if number of byte are matching
461
        if(RxBufferSize == sizeof(AccRawVector) )
499
        if(RxBufferSize == sizeof(AccRawVector) )
462
        {
500
        {
463
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
501
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
464
        }
502
        }
-
 
503
        DebugOut.Analog[16] = AccRawVector.X;
-
 
504
        DebugOut.Analog[17] = AccRawVector.Y;
-
 
505
        DebugOut.Analog[18] = AccRawVector.Z;
465
}
506
}
466
// rx data handler for reading magnetic sensor configuration
507
// rx data handler for reading magnetic sensor configuration
467
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
508
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
468
{       // if number of byte are matching
509
{       // if number of byte are matching
469
        if(RxBufferSize == sizeof(MagConfig) )
510
        if(RxBufferSize == sizeof(MagConfig) )
Line 530... Line 571...
530
        u8 retval = 0;
571
        u8 retval = 0;
531
        // try to catch the i2c buffer within 100 ms timeout
572
        // try to catch the i2c buffer within 100 ms timeout
532
        if(I2C_LockBuffer(100))
573
        if(I2C_LockBuffer(100))
533
        {
574
        {
534
                u8 TxBytes = 0;
575
                u8 TxBytes = 0;
535
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;  
576
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;      
536
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
577
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
537
                TxBytes += sizeof(AccConfig);
578
                TxBytes += sizeof(AccConfig);
538
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
579
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
539
                {
580
                {
540
                        if(I2C_WaitForEndOfTransmission(100))
581
                        if(I2C_WaitForEndOfTransmission(100))
Line 552... Line 593...
552
        u8 retval = 0;
593
        u8 retval = 0;
553
        // try to catch the i2c buffer within 100 ms timeout
594
        // try to catch the i2c buffer within 100 ms timeout
554
        if(I2C_LockBuffer(100))
595
        if(I2C_LockBuffer(100))
555
        {
596
        {
556
                u8 TxBytes = 0;
597
                u8 TxBytes = 0;
557
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;
598
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
558
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
599
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
559
                {
600
                {
560
                        if(I2C_WaitForEndOfTransmission(100))
601
                        if(I2C_WaitForEndOfTransmission(100))
561
                        {
602
                        {
562
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
603
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
Line 616... Line 657...
616
void NCMAG_GetMagVector(void)
657
void NCMAG_GetMagVector(void)
617
{
658
{
618
        // try to catch the I2C buffer within 0 ms
659
        // try to catch the I2C buffer within 0 ms
619
        if(I2C_LockBuffer(0))
660
        if(I2C_LockBuffer(0))
620
        {
661
        {
621
//       s16 tmp;
-
 
622
                u16 TxBytes = 0;
662
                u16 TxBytes = 0;
623
                // set register pointer
663
                // set register pointer
624
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
664
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
625
                // initiate transmission
665
                // initiate transmission
626
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
666
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
Line 633... Line 673...
633
        // try to catch the I2C buffer within 0 ms
673
        // try to catch the I2C buffer within 0 ms
634
        if(I2C_LockBuffer(0))
674
        if(I2C_LockBuffer(0))
635
        {
675
        {
636
                u16 TxBytes = 0;
676
                u16 TxBytes = 0;
637
                // set register pointer
677
                // set register pointer
638
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB;
678
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
639
                // initiate transmission
679
                // initiate transmission
640
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
680
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
641
        }
681
        }
642
}
682
}
Line 643... Line 683...
643
 
683
 
644
//----------------------------------------------------------------
684
//----------------------------------------------------------------
645
void InitNC_MagnetSensor(void)
685
u8 InitNC_MagnetSensor(void)
646
{
-
 
647
        s16 xscale, yscale, zscale;
686
{
648
        u8 crb_gain, cra_rate;
-
 
Line 649... Line 687...
649
//      u8  retval = 1;
687
        u8 crb_gain, cra_rate;
650
 
688
 
651
        switch(NCMAG_MagType)
689
        switch(NCMAG_SensorType)
652
        {
690
        {
653
                case MAG_TYPE_HMC5843:
691
                case TYPE_HMC5843:
654
                        crb_gain = HMC5843_CRB_GAIN_15GA;
-
 
655
                        cra_rate = HMC5843_CRA_RATE_50HZ;
-
 
656
                        xscale = HMC5843_TEST_XSCALE;
-
 
657
                        yscale = HMC5843_TEST_YSCALE;
692
                        crb_gain = HMC5843_CRB_GAIN_15GA;
Line 658... Line 693...
658
                        zscale = HMC5843_TEST_ZSCALE;
693
                        cra_rate = HMC5843_CRA_RATE_50HZ;
-
 
694
                        break;
659
                        break;
695
 
660
 
696
                case TYPE_LSM303DLH:
661
                case MAG_TYPE_LSM303DLH:
-
 
662
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
-
 
663
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
-
 
664
                        xscale = LSM303DLH_TEST_XSCALE;
697
                case TYPE_LSM303DLM:
Line 665... Line 698...
665
                        yscale = LSM303DLH_TEST_YSCALE;
698
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
666
                        zscale = LSM303DLH_TEST_ZSCALE;
699
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
667
                        break;
700
                        break;
Line 668... Line 701...
668
 
701
 
669
                default:
702
                default:
670
                return;
703
                return(0);
671
        }
704
        }
672
 
705
 
Line -... Line 706...
-
 
706
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
-
 
707
        MagConfig.crb = crb_gain;
673
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
708
        MagConfig.mode = MODE_CONTINUOUS;
-
 
709
        return(NCMAG_SetMagConfig());
-
 
710
}
-
 
711
 
-
 
712
//----------------------------------------------------------------
-
 
713
u8 NCMAG_Init_ACCSensor(void)
-
 
714
{
-
 
715
        AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_400HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN;
674
        MagConfig.crb = crb_gain;
716
        AccConfig.ctrl_2 = 0x00;
675
        MagConfig.mode = MODE_CONTINUOUS;
717
        AccConfig.ctrl_3 = 0x00;
676
        NCMAG_SetMagConfig();
718
        AccConfig.ctrl_4 = ACC_CTRL4_BDU|ACC_CTRL4_FS_2G;
677
}
719
        AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF;
678
 
720
        return(NCMAG_SetAccConfig());
-
 
721
}
Line 679... Line 722...
679
 
722
// --------------------------------------------------------
680
// --------------------------------------------------------
723
void NCMAG_Update(void)
681
void NCMAG_Update(void)
724
{
682
{
725
        static u32 TimerUpdate = 0;
683
        static u32 TimerUpdate = 0;
726
        static u8 send_config = 0;
684
        static u8 send_config = 0;
727
        u32 delay = 20;
685
 
728
 
686
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
729
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
687
        {
730
        {
688
                Compass_Heading = -1;
731
                Compass_Heading = -1;
689
                DebugOut.Analog[14]++; // count I2C error
732
                DebugOut.Analog[14]++; // count I2C error
690
                return;
733
                return;
691
        }
734
        }
692
        if(CheckDelay(TimerUpdate))
735
        if(CheckDelay(TimerUpdate))
693
        {
736
        {
694
           if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
737
                if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
695
       if(++send_config == 25)   // 500ms
738
        if(++send_config == 25)   // 500ms
-
 
739
                {
696
            {
740
                        send_config = 0;
697
                 send_config = 0;
741
                InitNC_MagnetSensor();
698
             InitNC_MagnetSensor();
742
                        TimerUpdate = SetDelay(15);    // back into the old time-slot
-
 
743
                }
-
 
744
                else
-
 
745
                {
-
 
746
                        static u8 s = 0;
-
 
747
                        // check for new calibration state
-
 
748
                        Compass_UpdateCalState();
-
 
749
                        if(Compass_CalState) NCMAG_Calibrate();
-
 
750
                       
-
 
751
                        // in case of LSM303 type
-
 
752
                        switch(NCMAG_SensorType)
699
                 TimerUpdate = SetDelay(15);    // back into the old time-slot
753
                        {
-
 
754
                                case TYPE_HMC5843:                             
-
 
755
                                        NCMAG_GetMagVector();
-
 
756
                                        delay = 20;
-
 
757
                                        break;
700
            }
758
                                case TYPE_LSM303DLH:
701
                else
759
                                case TYPE_LSM303DLM:
702
                {
760
                                        if(s){ NCMAG_GetMagVector(); s = 0;}
703
                // check for new calibration state
761
                                        else { NCMAG_GetAccVector(); s = 1;}
704
                Compass_UpdateCalState();
762
                                        delay = 10;
Line 724... Line 782...
724
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
782
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
725
        s16 xscale, yscale, zscale, scale_min, scale_max;
783
        s16 xscale, yscale, zscale, scale_min, scale_max;
726
        u8 crb_gain, cra_rate;
784
        u8 crb_gain, cra_rate;
727
        u8 i = 0, retval = 1;
785
        u8 i = 0, retval = 1;
Line 728... Line 786...
728
 
786
 
729
        switch(NCMAG_MagType)
787
        switch(NCMAG_SensorType)
730
        {
788
        {
731
                case MAG_TYPE_HMC5843:
789
                case TYPE_HMC5843:
732
                        crb_gain = HMC5843_CRB_GAIN_15GA;
790
                        crb_gain = HMC5843_CRB_GAIN_15GA;
733
                        cra_rate = HMC5843_CRA_RATE_50HZ;
791
                        cra_rate = HMC5843_CRA_RATE_50HZ;
734
                        xscale = HMC5843_TEST_XSCALE;
792
                        xscale = HMC5843_TEST_XSCALE;
735
                        yscale = HMC5843_TEST_YSCALE;
793
                        yscale = HMC5843_TEST_YSCALE;
736
                        zscale = HMC5843_TEST_ZSCALE;
794
                        zscale = HMC5843_TEST_ZSCALE;
Line 737... Line 795...
737
                        break;
795
                        break;
738
 
796
 
739
                case MAG_TYPE_LSM303DLH:
797
                case TYPE_LSM303DLH:
740
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
798
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
741
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
799
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
742
                        xscale = LSM303DLH_TEST_XSCALE;
800
                        xscale = LSM303DLH_TEST_XSCALE;
743
                        yscale = LSM303DLH_TEST_YSCALE;
801
                        yscale = LSM303DLH_TEST_YSCALE;
Line -... Line 802...
-
 
802
                        zscale = LSM303DLH_TEST_ZSCALE;
-
 
803
                        break;
-
 
804
 
-
 
805
                case TYPE_LSM303DLM:
-
 
806
                        // does not support self test feature 
-
 
807
                        done = retval;
744
                        zscale = LSM303DLH_TEST_ZSCALE;
808
                        return(retval);
745
                        break;
809
                        break;
746
 
810
 
Line 747... Line 811...
747
                default:
811
                default:
748
                return(0);
812
                        return(0);
749
        }
813
        }
Line 790... Line 854...
790
        // check scale for all axes
854
        // check scale for all axes
791
        // prepare scale limits
855
        // prepare scale limits
792
        LIMITS(xscale, scale_min, scale_max);
856
        LIMITS(xscale, scale_min, scale_max);
793
        xscale = (XMax - XMin)/(2*AVERAGE);
857
        xscale = (XMax - XMin)/(2*AVERAGE);
794
        if((xscale > scale_max) || (xscale < scale_min))
858
        if((xscale > scale_max) || (xscale < scale_min))
795
     {
859
    {
796
          retval = 0;
860
                retval = 0;
797
      sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
861
        sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
798
          UART1_PutString(msg);
862
                UART1_PutString(msg);
799
     }
863
    }
800
        LIMITS(yscale, scale_min, scale_max);
864
        LIMITS(yscale, scale_min, scale_max);
801
        yscale = (YMax - YMin)/(2*AVERAGE);
865
        yscale = (YMax - YMin)/(2*AVERAGE);
802
        if((yscale > scale_max) || (yscale < scale_min))
866
        if((yscale > scale_max) || (yscale < scale_min))
803
     {
867
    {
804
          retval = 0;
868
                retval = 0;
805
      sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
869
        sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
806
          UART1_PutString(msg);
870
                UART1_PutString(msg);
807
     }
871
    }
808
        LIMITS(zscale, scale_min, scale_max);
872
        LIMITS(zscale, scale_min, scale_max);
809
        zscale = (ZMax - ZMin)/(2*AVERAGE);
873
        zscale = (ZMax - ZMin)/(2*AVERAGE);
810
        if((zscale > scale_max) || (zscale < scale_min))      
874
        if((zscale > scale_max) || (zscale < scale_min))      
811
         {
875
        {
812
          retval = 0;
876
                retval = 0;
813
      sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
877
        sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
814
          UART1_PutString(msg);
878
                UART1_PutString(msg);
815
     }
879
    }
816
        done = retval;
880
        done = retval;
817
        return(retval);
881
        return(retval);
818
}
882
}
Line 824... Line 888...
824
        u8 msg[64];
888
        u8 msg[64];
825
        u8 retval = 0;
889
        u8 retval = 0;
826
        u8 repeat;
890
        u8 repeat;
Line 827... Line 891...
827
 
891
 
828
        NCMAG_Present = 0;
892
        NCMAG_Present = 0;
829
        NCMAG_MagType = MAG_TYPE_HMC5843;       // assuming having an HMC5843
893
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
830
        // polling for LSM302DLH option
894
        // polling for LSM302DLH/DLM option by ACC address ack
831
        repeat = 0;
895
        repeat = 0;
832
        do
896
        do
833
        {
897
        {
834
                retval = NCMAG_GetAccConfig();
898
                retval = NCMAG_GetAccConfig();
835
                if(retval) break; // break loop on success
899
                if(retval) break; // break loop on success
836
                UART1_PutString(".");
900
                UART1_PutString(".");
837
                repeat++;
901
                repeat++;
838
        }while(repeat < 3);
-
 
839
        if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH
-
 
840
        // polling of identification
902
        }while(repeat < 3);
841
        repeat = 0;
-
 
842
        do
903
        if(retval)
-
 
904
        {
-
 
905
                // initialize ACC sensor
-
 
906
                NCMAG_Init_ACCSensor();
-
 
907
 
-
 
908
                NCMAG_SensorType = TYPE_LSM303DLH;     
-
 
909
                // polling of sub identification
-
 
910
                repeat = 0;
-
 
911
                do
843
        {
912
                {
844
                retval = NCMAG_GetIdentification_Sub();
913
                        retval = NCMAG_GetIdentification_Sub();
845
                if(retval) break; // break loop on success
914
                        if(retval) break; // break loop on success
846
                UART1_PutString(".");
915
                        UART1_PutString(".");
847
                repeat++;
916
                        repeat++;
-
 
917
                }while(repeat < 12);
-
 
918
                if(retval)
-
 
919
                {
-
 
920
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
-
 
921
                }      
-
 
922
        }
848
        }while(repeat < 12);
923
        // get id bytes
849
        retval = 0;
924
        retval = 0;
850
        do
925
        do
851
        {
926
        {
852
                retval = NCMAG_GetIdentification();
927
                retval = NCMAG_GetIdentification();
Line 859... Line 934...
859
        if(retval)
934
        if(retval)
860
        {
935
        {
861
                u8 n1[] = "\n\r HMC5843";
936
                u8 n1[] = "\n\r HMC5843";
862
                u8 n2[] = "\n\r LSM303DLH";
937
                u8 n2[] = "\n\r LSM303DLH";
863
                u8 n3[] = "\n\r LSM303DLM";
938
                u8 n3[] = "\n\r LSM303DLM";
864
                u8* pn;
939
                u8* pn = n1;
Line 865... Line -...
865
               
-
 
866
                pn = n1;
940
               
867
                if(NCMAG_MagType == MAG_TYPE_LSM303DLH)
941
                switch(NCMAG_SensorType)
-
 
942
                {
-
 
943
                        case TYPE_HMC5843:
-
 
944
                                pn = n1;
868
                {
945
                                break;
869
                 if(NCMAG_Identification2.Sub == 0x3c) pn = n3;
946
                        case TYPE_LSM303DLH:
-
 
947
                                pn = n2;
-
 
948
                                break;
-
 
949
                        case TYPE_LSM303DLM:
-
 
950
                                pn = n3;
870
                 else pn = n2;
951
                                break;
Line 871... Line 952...
871
                }
952
                }
872
 
953
 
873
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
954
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
Line 877... Line 958...
877
                         && (NCMAG_Identification.C == MAG_IDC))
958
                         && (NCMAG_Identification.C == MAG_IDC))
878
                {
959
                {
879
                        NCMAG_Present = 1;
960
                        NCMAG_Present = 1;
Line 880... Line 961...
880
 
961
 
881
                        if(EEPROM_Init())
-
 
882
                                {
-
 
883
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
-
 
884
                                        if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
-
 
885
                                }
-
 
886
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
-
 
887
                       
-
 
888
                        if(NCMAG_Identification2.Sub == 0x00)
962
                        if(EEPROM_Init())
-
 
963
                        {
-
 
964
                                NCMAG_IsCalibrated = NCMag_CalibrationRead();
-
 
965
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
-
 
966
                        }
-
 
967
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
889
                        {
968
                        // perform self test
890
                         if(!NCMAG_SelfTest())
969
                        if(!NCMAG_SelfTest())
891
                         {
970
                        {
892
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
971
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
893
                                LED_RED_ON;
972
                                LED_RED_ON;
-
 
973
                                NCMAG_IsCalibrated = 0;
894
                                NCMAG_IsCalibrated = 0;
974
                        }
895
                         }      else UART1_PutString("\r\n Selftest ok");
975
                        else UART1_PutString("\r\n Selftest ok");
-
 
976
 
896
                        }
977
                        // initialize magnetic sensor configuration
897
                        else InitNC_MagnetSensor();
978
                        InitNC_MagnetSensor();
898
                }
979
                }
899
                else
980
                else
900
                {
981
                {
901
                        UART1_PutString("\n\r Not compatible!");
982
                        UART1_PutString("\n\r Not compatible!");
902
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
983
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
903
                        LED_RED_ON;
984
                        LED_RED_ON;
904
                }
985
                }
905
        }
986
        }
906
        else // nothing found
987
        else // nothing found
907
        {
988
        {
908
                NCMAG_MagType = MAG_TYPE_NONE;
989
                NCMAG_SensorType = TYPE_NONE;
909
                UART1_PutString("not found!");  
990
                UART1_PutString("not found!");  
910
        }
991
        }
911
        return(NCMAG_Present);
992
        return(NCMAG_Present);