Subversion Repositories NaviCtrl

Rev

Rev 330 | Rev 339 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
242 killagreg 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2010 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
254 killagreg 57
#include <math.h>
292 killagreg 58
#include <stdio.h>
242 killagreg 59
#include <string.h>
60
#include "91x_lib.h"
253 killagreg 61
#include "ncmag.h"
242 killagreg 62
#include "i2c.h"
63
#include "timer1.h"
64
#include "led.h"
65
#include "uart1.h"
254 killagreg 66
#include "eeprom.h"
256 killagreg 67
#include "mymath.h"
292 killagreg 68
#include "main.h"
242 killagreg 69
 
253 killagreg 70
u8 NCMAG_Present = 0;
254 killagreg 71
u8 NCMAG_IsCalibrated = 0;
242 killagreg 72
 
253 killagreg 73
#define MAG_TYPE_NONE           0
74
#define MAG_TYPE_HMC5843        1
75
#define MAG_TYPE_LSM303DLH      2
254 killagreg 76
u8 NCMAG_MagType = MAG_TYPE_NONE;
242 killagreg 77
 
338 holgerb 78
#define CALIBRATION_VERSION                     1
79
#define EEPROM_ADR_MAG_CALIBRATION              50
80
#define MAG_CALIBRATION_COMPATIBEL              0xA1
254 killagreg 81
 
256 killagreg 82
#define NCMAG_MIN_RAWVALUE -2047
83
#define NCMAG_MAX_RAWVALUE  2047
84
#define NCMAG_INVALID_DATA -4096
85
 
254 killagreg 86
typedef struct
87
{
88
        s16 Range;
89
        s16 Offset;
256 killagreg 90
} __attribute__((packed)) Scaling_t;
254 killagreg 91
 
92
typedef struct
93
{
94
        Scaling_t MagX;
95
        Scaling_t MagY;
96
        Scaling_t MagZ;
97
        u8 Version;
98
        u8 crc;
256 killagreg 99
} __attribute__((packed)) Calibration_t;
254 killagreg 100
 
101
Calibration_t Calibration;              // calibration data in RAM 
102
 
253 killagreg 103
// i2c MAG interface
104
#define MAG_SLAVE_ADDRESS       0x3C    // i2C slave address mag. sensor registers
242 killagreg 105
 
253 killagreg 106
// register mapping
107
#define REG_MAG_CRA                     0x00
108
#define REG_MAG_CRB                     0x01
109
#define REG_MAG_MODE            0x02
110
#define REG_MAG_DATAX_MSB       0x03
111
#define REG_MAG_DATAX_LSB       0x04
112
#define REG_MAG_DATAY_MSB       0x05
113
#define REG_MAG_DATAY_LSB       0x06
114
#define REG_MAG_DATAZ_MSB       0x07
115
#define REG_MAG_DATAZ_LSB       0x08
116
#define REG_MAG_STATUS          0x09
329 holgerb 117
 
253 killagreg 118
#define REG_MAG_IDA                     0x0A
119
#define REG_MAG_IDB                     0x0B
120
#define REG_MAG_IDC                     0x0C
329 holgerb 121
#define REG_MAG_IDF                     0x0F
242 killagreg 122
 
253 killagreg 123
// bit mask for configuration mode
124
#define CRA_MODE_MASK           0x03
125
#define CRA_MODE_NORMAL         0x00    //default
126
#define CRA_MODE_POSBIAS        0x01
127
#define CRA_MODE_NEGBIAS        0x02
128
#define CRA_MODE_SELFTEST       0x03
242 killagreg 129
 
253 killagreg 130
// bit mask for measurement mode
131
#define MODE_MASK                       0xFF
132
#define MODE_CONTINUOUS         0x00
133
#define MODE_SINGLE                     0x01    // default
134
#define MODE_IDLE                       0x02
135
#define MODE_SLEEP                      0x03
136
 
242 killagreg 137
// bit mask for rate
253 killagreg 138
#define CRA_RATE_MASK           0x1C
139
 
140
// bit mask for gain
141
#define CRB_GAIN_MASK           0xE0
142
 
143
// ids
144
#define MAG_IDA         0x48
145
#define MAG_IDB         0x34
146
#define MAG_IDC         0x33
147
 
148
// the special HMC5843 interface
149
// bit mask for rate
242 killagreg 150
#define HMC5843_CRA_RATE_0_5HZ          0x00
151
#define HMC5843_CRA_RATE_1HZ            0x04
152
#define HMC5843_CRA_RATE_2HZ            0x08
153
#define HMC5843_CRA_RATE_5HZ            0x0C
154
#define HMC5843_CRA_RATE_10HZ           0x10    //default
155
#define HMC5843_CRA_RATE_20HZ           0x14
156
#define HMC5843_CRA_RATE_50HZ           0x18
157
// bit mask for gain
158
#define HMC5843_CRB_GAIN_07GA           0x00
159
#define HMC5843_CRB_GAIN_10GA           0x20    //default
160
#define HMC5843_CRB_GAIN_15GA           0x40
161
#define HMC5843_CRB_GAIN_20GA           0x60
162
#define HMC5843_CRB_GAIN_32GA           0x80
163
#define HMC5843_CRB_GAIN_38GA           0xA0
164
#define HMC5843_CRB_GAIN_45GA           0xC0
165
#define HMC5843_CRB_GAIN_65GA           0xE0
253 killagreg 166
// self test value
167
#define HMC5843_TEST_XSCALE             715
168
#define HMC5843_TEST_YSCALE             715
169
#define HMC5843_TEST_ZSCALE             715
242 killagreg 170
 
171
 
253 killagreg 172
// the special LSM302DLH interface
173
// bit mask for rate
174
#define LSM303DLH_CRA_RATE_0_75HZ       0x00
175
#define LSM303DLH_CRA_RATE_1_5HZ        0x04
176
#define LSM303DLH_CRA_RATE_3_0HZ        0x08
177
#define LSM303DLH_CRA_RATE_7_5HZ        0x0C
178
#define LSM303DLH_CRA_RATE_15HZ         0x10    //default
179
#define LSM303DLH_CRA_RATE_30HZ         0x14
180
#define LSM303DLH_CRA_RATE_75HZ         0x18
338 holgerb 181
 
253 killagreg 182
// bit mask for gain
183
#define LSM303DLH_CRB_GAIN_XXGA         0x00
184
#define LSM303DLH_CRB_GAIN_13GA         0x20    //default
185
#define LSM303DLH_CRB_GAIN_19GA         0x40
186
#define LSM303DLH_CRB_GAIN_25GA         0x60
187
#define LSM303DLH_CRB_GAIN_40GA         0x80
188
#define LSM303DLH_CRB_GAIN_47GA         0xA0
189
#define LSM303DLH_CRB_GAIN_56GA         0xC0
190
#define LSM303DLH_CRB_GAIN_81GA         0xE0
191
// self test value
338 holgerb 192
#define LSM303DLH_TEST_XSCALE   495
193
#define LSM303DLH_TEST_YSCALE   495
194
#define LSM303DLH_TEST_ZSCALE   470
253 killagreg 195
 
196
// the i2c ACC interface
197
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
198
// register mapping
199
#define REG_ACC_CTRL1                   0x20
200
#define REG_ACC_CTRL2                   0x21
201
#define REG_ACC_CTRL3                   0x22
202
#define REG_ACC_CTRL4                   0x23
203
#define REG_ACC_CTRL5                   0x24
204
#define REG_ACC_HP_FILTER_RESET 0x25
205
#define REG_ACC_REFERENCE               0x26
206
#define REG_ACC_STATUS                  0x27
207
#define REG_ACC_X_LSB                   0x28
208
#define REG_ACC_X_MSB                   0x29
209
#define REG_ACC_Y_LSB                   0x2A
210
#define REG_ACC_Y_MSB                   0x2B
211
#define REG_ACC_Z_LSB                   0x2C
212
#define REG_ACC_Z_MSB                   0x2D
213
 
214
 
215
 
242 killagreg 216
typedef struct
217
{
253 killagreg 218
        u8 A;
219
        u8 B;
220
        u8 C;
221
} __attribute__((packed)) Identification_t;
222
volatile Identification_t NCMAG_Identification;
242 killagreg 223
 
253 killagreg 224
typedef struct
225
{
329 holgerb 226
        u8 Sub;
227
} __attribute__((packed)) Identification2_t;
228
volatile Identification2_t NCMAG_Identification2;
229
 
230
typedef struct
231
{
253 killagreg 232
        u8 cra;
233
        u8 crb;
234
        u8 mode;
235
} __attribute__((packed)) MagConfig_t;
242 killagreg 236
 
253 killagreg 237
volatile MagConfig_t MagConfig;
242 killagreg 238
 
253 killagreg 239
typedef struct
240
{
241
        u8 ctrl_1;
242
        u8 ctrl_2;
243
        u8 ctrl_3;
244
        u8 ctrl_4;
245
        u8 ctrl_5;
246
} __attribute__((packed)) AccConfig_t;
247
 
248
volatile AccConfig_t AccConfig;
249
 
254 killagreg 250
volatile s16vec_t AccRawVector;
251
volatile s16vec_t MagRawVector;
253 killagreg 252
 
253
 
254 killagreg 254
u8 NCMag_CalibrationWrite(void)
255
{
338 holgerb 256
        u8 i, crc = MAG_CALIBRATION_COMPATIBEL;
254 killagreg 257
        EEPROM_Result_t eres;
258
        u8 *pBuff = (u8*)&Calibration;
259
 
260
        Calibration.Version = CALIBRATION_VERSION;
256 killagreg 261
        for(i = 0; i<(sizeof(Calibration)-1); i++)
254 killagreg 262
        {
263
                crc += pBuff[i];        
264
        }
265
        Calibration.crc = ~crc;
266
        eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration));
267
        if(EEPROM_SUCCESS == eres) i = 1;
268
        else i = 0;
269
        return(i);     
270
}
271
 
272
u8 NCMag_CalibrationRead(void)
273
{
338 holgerb 274
        u8 i, crc = MAG_CALIBRATION_COMPATIBEL;
254 killagreg 275
        u8 *pBuff = (u8*)&Calibration;
276
 
277
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
278
        {
256 killagreg 279
                for(i = 0; i<(sizeof(Calibration)-1); i++)
254 killagreg 280
                {
281
                        crc += pBuff[i];        
282
                }
283
                crc = ~crc;
284
                if(Calibration.crc != crc) return(0); // crc mismatch
257 killagreg 285
                if(Calibration.Version == CALIBRATION_VERSION) return(1);
254 killagreg 286
        }
287
        return(0);
288
}
289
 
290
 
291
void NCMAG_Calibrate(void)
292
{
330 holgerb 293
        u8 msg[64];
254 killagreg 294
        static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
256 killagreg 295
        static s16 X = 0, Y = 0, Z = 0;
254 killagreg 296
        static u8 OldCalState = 0;     
297
 
256 killagreg 298
        X = (4*X + MagRawVector.X + 3)/5;
299
        Y = (4*Y + MagRawVector.Y + 3)/5;
300
        Z = (4*Z + MagRawVector.Z + 3)/5;
301
 
254 killagreg 302
        switch(Compass_CalState)
303
        {
304
                case 1:
305
                        // 1st step of calibration
306
                        // initialize ranges
307
                        // used to change the orientation of the NC in the horizontal plane
308
                        Xmin =  10000;
309
                        Xmax = -10000;
310
                        Ymin =  10000;
311
                        Ymax = -10000;
312
                        Zmin =  10000;
313
                        Zmax = -10000;
314
                        break;
315
 
316
                case 2: // 2nd step of calibration
317
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
275 killagreg 318
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
319
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
320
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
321
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
254 killagreg 322
                        break;
323
 
324
                case 3: // 3rd step of calibration
325
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
326
                        break;
327
 
328
                case 4:
329
                        // find Min and Max of the Z-Sensor
275 killagreg 330
                        if(Z < Zmin)      { Zmin = Z; BeepTime = 80;}
331
                        else if(Z > Zmax) { Zmax = Z; BeepTime = 80;}
254 killagreg 332
                        break;
333
 
334
                case 5:
335
                        // Save values
336
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
337
                        {
338 holgerb 338
//                              #define MIN_CALIBRATION    256
339
                                #define MIN_CALIBRATION    450
254 killagreg 340
                                Calibration.MagX.Range = Xmax - Xmin;
341
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
342
                                Calibration.MagY.Range = Ymax - Ymin;
343
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
344
                                Calibration.MagZ.Range = Zmax - Zmin;
345
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
265 holgerb 346
                                if((Calibration.MagX.Range > MIN_CALIBRATION) && (Calibration.MagY.Range > MIN_CALIBRATION) && (Calibration.MagZ.Range > MIN_CALIBRATION))
254 killagreg 347
                                {
348
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
270 killagreg 349
                                        BeepTime = 2500;
330 holgerb 350
                                        UART1_PutString("\r\n Calibration okay\n\r");
254 killagreg 351
                                }
352
                                else
353
                                {
330 holgerb 354
                                        UART1_PutString("\r\n Calibration FAILED - Values too low");
355
                                    if(Calibration.MagX.Range < MIN_CALIBRATION) UART1_PutString("X! ");
356
                                    if(Calibration.MagY.Range < MIN_CALIBRATION) UART1_PutString("y! ");
357
                                    if(Calibration.MagZ.Range < MIN_CALIBRATION) UART1_PutString("Z! ");
358
                                        UART1_PutString("\r\n");
254 killagreg 359
                                        // restore old calibration data from eeprom
360
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
361
                                }
330 holgerb 362
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
363
                                        UART1_PutString(msg);
364
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
365
                                        UART1_PutString(msg);
366
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
367
                                        UART1_PutString(msg);
254 killagreg 368
                        }
369
                        break;
370
 
371
                default:
372
                        break; 
373
        }
374
        OldCalState = Compass_CalState;
375
}
376
 
242 killagreg 377
// ---------- call back handlers -----------------------------------------
378
 
379
// rx data handler for id info request
253 killagreg 380
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
254 killagreg 381
{       // if number of bytes are matching
253 killagreg 382
        if(RxBufferSize == sizeof(NCMAG_Identification) )
242 killagreg 383
        {
253 killagreg 384
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
385
        }
242 killagreg 386
}
329 holgerb 387
 
388
void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize)
389
{       // if number of bytes are matching
390
        if(RxBufferSize == sizeof(NCMAG_Identification2))
391
        {
392
                memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2));
393
        }
394
}
395
 
254 killagreg 396
// rx data handler for magnetic sensor raw data
253 killagreg 397
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
254 killagreg 398
{       // if number of bytes are matching
399
        if(RxBufferSize == sizeof(MagRawVector) )
243 killagreg 400
        {       // byte order from big to little endian
256 killagreg 401
                s16 raw;
402
                raw = pRxBuffer[0]<<8;
403
                raw+= pRxBuffer[1];
404
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
405
                raw = pRxBuffer[2]<<8;
406
                raw+= pRxBuffer[3];
330 holgerb 407
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
408
                {
409
                  if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged
410
                  else MagRawVector.Y = raw;
411
                }
256 killagreg 412
                raw = pRxBuffer[4]<<8;
413
                raw+= pRxBuffer[5];
330 holgerb 414
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
415
                {
416
                  if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged
417
                  else MagRawVector.Z = raw;
418
                }
419
 
242 killagreg 420
        }
254 killagreg 421
        if(Compass_CalState || !NCMAG_IsCalibrated)
284 killagreg 422
        {       // mark out data invalid
289 killagreg 423
                MagVector.X = MagRawVector.X;
424
                MagVector.Y = MagRawVector.Y;
425
                MagVector.Z = MagRawVector.Z;
254 killagreg 426
                Compass_Heading = -1;
427
        }
428
        else
429
        {
430
                // update MagVector from MagRaw Vector by Scaling
431
                MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range);
432
                MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range);
433
                MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range);
292 killagreg 434
                Compass_CalcHeading();
254 killagreg 435
        }
242 killagreg 436
}
254 killagreg 437
// rx data handler  for acceleration raw data
253 killagreg 438
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
439
{       // if number of byte are matching
254 killagreg 440
        if(RxBufferSize == sizeof(AccRawVector) )
253 killagreg 441
        {
254 killagreg 442
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
253 killagreg 443
        }
444
}
254 killagreg 445
// rx data handler for reading magnetic sensor configuration
253 killagreg 446
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
447
{       // if number of byte are matching
448
        if(RxBufferSize == sizeof(MagConfig) )
449
        {
450
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
451
        }
452
}
254 killagreg 453
// rx data handler for reading acceleration sensor configuration
253 killagreg 454
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
455
{       // if number of byte are matching
456
        if(RxBufferSize == sizeof(AccConfig) )
457
        {
458
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
459
        }
460
}
254 killagreg 461
//----------------------------------------------------------------------
253 killagreg 462
 
254 killagreg 463
 
464
// ---------------------------------------------------------------------
253 killagreg 465
u8 NCMAG_SetMagConfig(void)
466
{
467
        u8 retval = 0;
468
        // try to catch the i2c buffer within 100 ms timeout
469
        if(I2C_LockBuffer(100))
470
        {
471
                u8 TxBytes = 0;
472
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;    
473
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
474
                TxBytes += sizeof(MagConfig);
475
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
476
                {
477
                        if(I2C_WaitForEndOfTransmission(100))
478
                        {
479
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
480
                        }
481
                }
482
        }
483
        return(retval);        
484
}
242 killagreg 485
 
253 killagreg 486
// ----------------------------------------------------------------------------------------
487
u8 NCMAG_GetMagConfig(void)
242 killagreg 488
{
253 killagreg 489
        u8 retval = 0;
252 killagreg 490
        // try to catch the i2c buffer within 100 ms timeout
248 killagreg 491
        if(I2C_LockBuffer(100))
242 killagreg 492
        {
253 killagreg 493
                u8 TxBytes = 0;
494
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;
495
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
248 killagreg 496
                {
252 killagreg 497
                        if(I2C_WaitForEndOfTransmission(100))
498
                        {
499
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
500
                        }
248 killagreg 501
                }
242 killagreg 502
        }
253 killagreg 503
        return(retval);        
242 killagreg 504
}
505
 
506
// ----------------------------------------------------------------------------------------
253 killagreg 507
u8 NCMAG_SetAccConfig(void)
242 killagreg 508
{
252 killagreg 509
        u8 retval = 0;
253 killagreg 510
        // try to catch the i2c buffer within 100 ms timeout
248 killagreg 511
        if(I2C_LockBuffer(100))
242 killagreg 512
        {
253 killagreg 513
                u8 TxBytes = 0;
514
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;  
515
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
516
                TxBytes += sizeof(AccConfig);
517
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
518
                {
519
                        if(I2C_WaitForEndOfTransmission(100))
520
                        {
521
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
522
                        }
523
                }
524
        }
525
        return(retval);        
526
}
527
 
528
// ----------------------------------------------------------------------------------------
529
u8 NCMAG_GetAccConfig(void)
530
{
531
        u8 retval = 0;
532
        // try to catch the i2c buffer within 100 ms timeout
533
        if(I2C_LockBuffer(100))
534
        {
535
                u8 TxBytes = 0;
536
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;
537
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
538
                {
539
                        if(I2C_WaitForEndOfTransmission(100))
540
                        {
541
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
542
                        }
543
                }
544
        }
545
        return(retval);        
546
}
547
 
548
// ----------------------------------------------------------------------------------------
549
u8 NCMAG_GetIdentification(void)
550
{
551
        u8 retval = 0;
552
        // try to catch the i2c buffer within 100 ms timeout
553
        if(I2C_LockBuffer(100))
554
        {
555
                u16 TxBytes = 0;
556
                NCMAG_Identification.A = 0xFF;
557
                NCMAG_Identification.B = 0xFF;
558
                NCMAG_Identification.C = 0xFF;
559
                I2C_Buffer[TxBytes++] = REG_MAG_IDA;
248 killagreg 560
                // initiate transmission
253 killagreg 561
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
248 killagreg 562
                {
253 killagreg 563
                        if(I2C_WaitForEndOfTransmission(100))
252 killagreg 564
                        {
565
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
566
                        }
248 killagreg 567
                }
242 killagreg 568
        }
253 killagreg 569
        return(retval);
242 killagreg 570
}
571
 
329 holgerb 572
u8 NCMAG_GetIdentification_Sub(void)
573
{
574
        u8 retval = 0;
575
        // try to catch the i2c buffer within 100 ms timeout
576
        if(I2C_LockBuffer(100))
577
        {
578
                u16 TxBytes = 0;
579
                NCMAG_Identification2.Sub = 0xFF;
580
                I2C_Buffer[TxBytes++] = REG_MAG_IDF;
581
                // initiate transmission
582
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
583
                {
584
                        if(I2C_WaitForEndOfTransmission(100))
585
                        {
586
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
587
                        }
588
                }
589
        }
590
        return(retval);
591
}
592
 
593
 
253 killagreg 594
// ----------------------------------------------------------------------------------------
595
void NCMAG_GetMagVector(void)
596
{
597
        // try to catch the I2C buffer within 0 ms
598
        if(I2C_LockBuffer(0))
599
        {
330 holgerb 600
//       s16 tmp;
253 killagreg 601
                u16 TxBytes = 0;
602
                // set register pointer
603
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
604
                // initiate transmission
605
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
606
        }
607
}
608
 
242 killagreg 609
//----------------------------------------------------------------
253 killagreg 610
void NCMAG_GetAccVector(void)
243 killagreg 611
{
252 killagreg 612
        // try to catch the I2C buffer within 0 ms
613
        if(I2C_LockBuffer(0))
243 killagreg 614
        {
248 killagreg 615
                u16 TxBytes = 0;
243 killagreg 616
                // set register pointer
253 killagreg 617
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB;
243 killagreg 618
                // initiate transmission
254 killagreg 619
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
243 killagreg 620
        }
621
}
622
 
330 holgerb 623
//----------------------------------------------------------------
624
void InitNC_MagnetSensor(void)
625
{
626
        s16 xscale, yscale, zscale;
627
        u8 crb_gain, cra_rate;
338 holgerb 628
//      u8  retval = 1;
330 holgerb 629
 
630
        switch(NCMAG_MagType)
631
        {
632
                case MAG_TYPE_HMC5843:
633
                        crb_gain = HMC5843_CRB_GAIN_10GA;
634
                        cra_rate = HMC5843_CRA_RATE_50HZ;
635
                        xscale = HMC5843_TEST_XSCALE;
636
                        yscale = HMC5843_TEST_YSCALE;
637
                        zscale = HMC5843_TEST_ZSCALE;
638
                        break;
639
 
640
                case MAG_TYPE_LSM303DLH:
338 holgerb 641
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
330 holgerb 642
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
643
                        xscale = LSM303DLH_TEST_XSCALE;
644
                        yscale = LSM303DLH_TEST_YSCALE;
645
                        zscale = LSM303DLH_TEST_ZSCALE;
646
                        break;
647
 
648
                default:
338 holgerb 649
                return;
330 holgerb 650
        }
651
 
652
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
653
        MagConfig.crb = crb_gain;
654
        MagConfig.mode = MODE_CONTINUOUS;
655
        NCMAG_SetMagConfig();
656
}
657
 
658
 
253 killagreg 659
// --------------------------------------------------------
292 killagreg 660
void NCMAG_Update(void)
243 killagreg 661
{
292 killagreg 662
        static u32 TimerUpdate = 0;
321 holgerb 663
        static u8 send_config = 0;
243 killagreg 664
 
254 killagreg 665
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
666
        {
667
                Compass_Heading = -1;
326 holgerb 668
                DebugOut.Analog[14]++; // count I2C error
254 killagreg 669
                return;
670
        }
292 killagreg 671
        if(CheckDelay(TimerUpdate))
243 killagreg 672
        {
326 holgerb 673
           if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
674
       if(++send_config == 25)   // 500ms
321 holgerb 675
            {
676
                 send_config = 0;
330 holgerb 677
             InitNC_MagnetSensor();
321 holgerb 678
                 TimerUpdate = SetDelay(15);    // back into the old time-slot
679
            }
680
                else
681
                {
254 killagreg 682
                // check for new calibration state
683
                Compass_UpdateCalState();
684
                if(Compass_CalState) NCMAG_Calibrate();
685
                NCMAG_GetMagVector(); //Get new data;
326 holgerb 686
                if(send_config == 24) TimerUpdate = SetDelay(5);    // next event is the re-configuration
321 holgerb 687
                else TimerUpdate = SetDelay(20);    // every 20 ms are 50 Hz
688
                }
243 killagreg 689
        }
690
}
691
 
330 holgerb 692
 
254 killagreg 693
// --------------------------------------------------------
253 killagreg 694
u8 NCMAG_SelfTest(void)
243 killagreg 695
{
266 holgerb 696
        u8 msg[64];
275 killagreg 697
        static u8 done = 0;
266 holgerb 698
 
287 holgerb 699
        if(done) return(1);        // just make it once
275 killagreg 700
 
271 holgerb 701
        #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;}
243 killagreg 702
        u32 time;
253 killagreg 703
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
704
        s16 xscale, yscale, zscale, scale_min, scale_max;
705
        u8 crb_gain, cra_rate;
706
        u8 i = 0, retval = 1;
243 killagreg 707
 
253 killagreg 708
        switch(NCMAG_MagType)
709
        {
710
                case MAG_TYPE_HMC5843:
711
                        crb_gain = HMC5843_CRB_GAIN_10GA;
712
                        cra_rate = HMC5843_CRA_RATE_50HZ;
713
                        xscale = HMC5843_TEST_XSCALE;
714
                        yscale = HMC5843_TEST_YSCALE;
715
                        zscale = HMC5843_TEST_ZSCALE;
716
                        break;
717
 
718
                case MAG_TYPE_LSM303DLH:
338 holgerb 719
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
253 killagreg 720
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
721
                        xscale = LSM303DLH_TEST_XSCALE;
722
                        yscale = LSM303DLH_TEST_YSCALE;
723
                        zscale = LSM303DLH_TEST_ZSCALE;
724
                        break;
725
 
726
                default:
727
                return(0);
728
        }
729
 
730
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
731
        MagConfig.crb = crb_gain;
732
        MagConfig.mode = MODE_CONTINUOUS;
733
        // activate positive bias field
734
        NCMAG_SetMagConfig();
251 killagreg 735
        // wait for stable readings
736
        time = SetDelay(50);
737
        while(!CheckDelay(time));
243 killagreg 738
        // averaging
253 killagreg 739
        #define AVERAGE 20
740
        for(i = 0; i<AVERAGE; i++)
243 killagreg 741
        {
253 killagreg 742
                NCMAG_GetMagVector();
243 killagreg 743
                time = SetDelay(20);
744
        while(!CheckDelay(time));
254 killagreg 745
                XMax += MagRawVector.X;
746
                YMax += MagRawVector.Y;
747
                ZMax += MagRawVector.Z;
243 killagreg 748
        }
253 killagreg 749
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
750
        // activate positive bias field
751
        NCMAG_SetMagConfig();
251 killagreg 752
    // wait for stable readings
753
        time = SetDelay(50);
754
        while(!CheckDelay(time));
243 killagreg 755
        // averaging
253 killagreg 756
        for(i = 0; i < AVERAGE; i++)
243 killagreg 757
        {
253 killagreg 758
                NCMAG_GetMagVector();
243 killagreg 759
                time = SetDelay(20);
760
        while(!CheckDelay(time));
254 killagreg 761
                XMin += MagRawVector.X;
762
                YMin += MagRawVector.Y;
763
                ZMin += MagRawVector.Z;
243 killagreg 764
        }
765
        // setup final configuration
253 killagreg 766
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
767
        // activate positive bias field
768
        NCMAG_SetMagConfig();
266 holgerb 769
        // check scale for all axes
243 killagreg 770
        // prepare scale limits
253 killagreg 771
        LIMITS(xscale, scale_min, scale_max);
267 holgerb 772
        xscale = (XMax - XMin)/(2*AVERAGE);
266 holgerb 773
        if((xscale > scale_max) || (xscale < scale_min))
774
     {
775
          retval = 0;
776
      sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
777
          UART1_PutString(msg);
778
     }
267 holgerb 779
        LIMITS(yscale, scale_min, scale_max);
266 holgerb 780
        yscale = (YMax - YMin)/(2*AVERAGE);
781
        if((yscale > scale_max) || (yscale < scale_min))
782
     {
783
          retval = 0;
784
      sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
785
          UART1_PutString(msg);
786
     }
267 holgerb 787
        LIMITS(zscale, scale_min, scale_max);
266 holgerb 788
        zscale = (ZMax - ZMin)/(2*AVERAGE);
789
        if((zscale > scale_max) || (zscale < scale_min))      
790
         {
791
          retval = 0;
792
      sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
793
          UART1_PutString(msg);
794
     }
275 killagreg 795
        done = retval;
253 killagreg 796
        return(retval);
243 killagreg 797
}
798
 
799
 
800
//----------------------------------------------------------------
253 killagreg 801
u8 NCMAG_Init(void)
242 killagreg 802
{
803
        u8 msg[64];
252 killagreg 804
        u8 retval = 0;
242 killagreg 805
        u8 repeat;
806
 
253 killagreg 807
        NCMAG_Present = 0;
808
        NCMAG_MagType = MAG_TYPE_HMC5843;       // assuming having an HMC5843
809
        // polling for LSM302DLH option
810
        repeat = 0;
811
        do
812
        {
813
                retval = NCMAG_GetAccConfig();
814
                if(retval) break; // break loop on success
815
                UART1_PutString(".");
816
                repeat++;
817
        }while(repeat < 3);
818
        if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH
242 killagreg 819
        // polling of identification
820
        repeat = 0;
821
        do
822
        {
329 holgerb 823
                retval = NCMAG_GetIdentification_Sub();
824
                if(retval) break; // break loop on success
825
                UART1_PutString(".");
826
                repeat++;
827
        }while(repeat < 12);
828
        retval = 0;
829
        do
830
        {
253 killagreg 831
                retval = NCMAG_GetIdentification();
252 killagreg 832
                if(retval) break; // break loop on success
242 killagreg 833
                UART1_PutString(".");
834
                repeat++;
252 killagreg 835
        }while(repeat < 12);
329 holgerb 836
 
253 killagreg 837
        // if we got an answer to id request
252 killagreg 838
        if(retval)
242 killagreg 839
        {
329 holgerb 840
                u8 n1[] = "\n\r HMC5843";
841
                u8 n2[] = "\n\r LSM303DLH";
842
                u8 n3[] = "\n\r LSM303DLM";
253 killagreg 843
                u8* pn;
329 holgerb 844
 
845
                pn = n1;
846
                if(NCMAG_MagType == MAG_TYPE_LSM303DLH)
847
                {
848
                 if(NCMAG_Identification2.Sub == 0x3c) pn = n3;
849
                 else pn = n2;
850
                }
851
 
852
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
242 killagreg 853
                UART1_PutString(msg);
253 killagreg 854
                if (    (NCMAG_Identification.A == MAG_IDA)
855
                     && (NCMAG_Identification.B == MAG_IDB)
856
                         && (NCMAG_Identification.C == MAG_IDC))
242 killagreg 857
                {
268 killagreg 858
                        NCMAG_Present = 1;
329 holgerb 859
 
860
                        if(EEPROM_Init())
264 killagreg 861
                                {
862
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
863
                                        if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
864
                                }
329 holgerb 865
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
866
 
867
                        if(NCMAG_Identification2.Sub == 0x00)
868
                        {
869
                         if(!NCMAG_SelfTest())
870
                         {
871
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
872
                                LED_RED_ON;
873
                                NCMAG_IsCalibrated = 0;
874
                         }      else UART1_PutString("\r\n Selftest ok");
254 killagreg 875
                        }
330 holgerb 876
                        else InitNC_MagnetSensor();
242 killagreg 877
                }
878
                else
879
                {
254 killagreg 880
                        UART1_PutString("\n\r Not compatible!");
256 killagreg 881
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
242 killagreg 882
                        LED_RED_ON;
883
                }
884
        }
253 killagreg 885
        else // nothing found
886
        {
887
                NCMAG_MagType = MAG_TYPE_NONE;
888
                UART1_PutString("not found!");  
889
        }
890
        return(NCMAG_Present);
242 killagreg 891
}
892