Subversion Repositories NaviCtrl

Rev

Rev 271 | Rev 284 | 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>
242 killagreg 58
#include <string.h>
59
#include "91x_lib.h"
268 killagreg 60
#include "main.h"
253 killagreg 61
#include "ncmag.h"
242 killagreg 62
#include "i2c.h"
63
#include "timer1.h"
64
#include "led.h"
65
#include "spi_slave.h"
66
#include "uart1.h"
254 killagreg 67
#include "eeprom.h"
256 killagreg 68
#include "mymath.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
 
254 killagreg 78
#define CALIBRATION_VERSION 1
79
#define EEPROM_ADR_MAG_CALIBRATION 50
80
 
256 killagreg 81
#define NCMAG_MIN_RAWVALUE -2047
82
#define NCMAG_MAX_RAWVALUE  2047
83
#define NCMAG_INVALID_DATA -4096
84
 
254 killagreg 85
typedef struct
86
{
87
        s16 Range;
88
        s16 Offset;
256 killagreg 89
} __attribute__((packed)) Scaling_t;
254 killagreg 90
 
91
typedef struct
92
{
93
        Scaling_t MagX;
94
        Scaling_t MagY;
95
        Scaling_t MagZ;
96
        u8 Version;
97
        u8 crc;
256 killagreg 98
} __attribute__((packed)) Calibration_t;
254 killagreg 99
 
100
Calibration_t Calibration;              // calibration data in RAM 
101
 
253 killagreg 102
// i2c MAG interface
103
#define MAG_SLAVE_ADDRESS       0x3C    // i2C slave address mag. sensor registers
242 killagreg 104
 
253 killagreg 105
// register mapping
106
#define REG_MAG_CRA                     0x00
107
#define REG_MAG_CRB                     0x01
108
#define REG_MAG_MODE            0x02
109
#define REG_MAG_DATAX_MSB       0x03
110
#define REG_MAG_DATAX_LSB       0x04
111
#define REG_MAG_DATAY_MSB       0x05
112
#define REG_MAG_DATAY_LSB       0x06
113
#define REG_MAG_DATAZ_MSB       0x07
114
#define REG_MAG_DATAZ_LSB       0x08
115
#define REG_MAG_STATUS          0x09
116
#define REG_MAG_IDA                     0x0A
117
#define REG_MAG_IDB                     0x0B
118
#define REG_MAG_IDC                     0x0C
242 killagreg 119
 
253 killagreg 120
// bit mask for configuration mode
121
#define CRA_MODE_MASK           0x03
122
#define CRA_MODE_NORMAL         0x00    //default
123
#define CRA_MODE_POSBIAS        0x01
124
#define CRA_MODE_NEGBIAS        0x02
125
#define CRA_MODE_SELFTEST       0x03
242 killagreg 126
 
253 killagreg 127
// bit mask for measurement mode
128
#define MODE_MASK                       0xFF
129
#define MODE_CONTINUOUS         0x00
130
#define MODE_SINGLE                     0x01    // default
131
#define MODE_IDLE                       0x02
132
#define MODE_SLEEP                      0x03
133
 
242 killagreg 134
// bit mask for rate
253 killagreg 135
#define CRA_RATE_MASK           0x1C
136
 
137
// bit mask for gain
138
#define CRB_GAIN_MASK           0xE0
139
 
140
// ids
141
#define MAG_IDA         0x48
142
#define MAG_IDB         0x34
143
#define MAG_IDC         0x33
144
 
145
// the special HMC5843 interface
146
// bit mask for rate
242 killagreg 147
#define HMC5843_CRA_RATE_0_5HZ          0x00
148
#define HMC5843_CRA_RATE_1HZ            0x04
149
#define HMC5843_CRA_RATE_2HZ            0x08
150
#define HMC5843_CRA_RATE_5HZ            0x0C
151
#define HMC5843_CRA_RATE_10HZ           0x10    //default
152
#define HMC5843_CRA_RATE_20HZ           0x14
153
#define HMC5843_CRA_RATE_50HZ           0x18
154
// bit mask for gain
155
#define HMC5843_CRB_GAIN_07GA           0x00
156
#define HMC5843_CRB_GAIN_10GA           0x20    //default
157
#define HMC5843_CRB_GAIN_15GA           0x40
158
#define HMC5843_CRB_GAIN_20GA           0x60
159
#define HMC5843_CRB_GAIN_32GA           0x80
160
#define HMC5843_CRB_GAIN_38GA           0xA0
161
#define HMC5843_CRB_GAIN_45GA           0xC0
162
#define HMC5843_CRB_GAIN_65GA           0xE0
253 killagreg 163
// self test value
164
#define HMC5843_TEST_XSCALE             715
165
#define HMC5843_TEST_YSCALE             715
166
#define HMC5843_TEST_ZSCALE             715
242 killagreg 167
 
168
 
253 killagreg 169
// the special LSM302DLH interface
170
// bit mask for rate
171
#define LSM303DLH_CRA_RATE_0_75HZ       0x00
172
#define LSM303DLH_CRA_RATE_1_5HZ        0x04
173
#define LSM303DLH_CRA_RATE_3_0HZ        0x08
174
#define LSM303DLH_CRA_RATE_7_5HZ        0x0C
175
#define LSM303DLH_CRA_RATE_15HZ         0x10    //default
176
#define LSM303DLH_CRA_RATE_30HZ         0x14
177
#define LSM303DLH_CRA_RATE_75HZ         0x18
178
// bit mask for gain
179
#define LSM303DLH_CRB_GAIN_XXGA         0x00
180
#define LSM303DLH_CRB_GAIN_13GA         0x20    //default
181
#define LSM303DLH_CRB_GAIN_19GA         0x40
182
#define LSM303DLH_CRB_GAIN_25GA         0x60
183
#define LSM303DLH_CRB_GAIN_40GA         0x80
184
#define LSM303DLH_CRB_GAIN_47GA         0xA0
185
#define LSM303DLH_CRB_GAIN_56GA         0xC0
186
#define LSM303DLH_CRB_GAIN_81GA         0xE0
187
// self test value
188
#define LSM303DLH_TEST_XSCALE   655
189
#define LSM303DLH_TEST_YSCALE   655
190
#define LSM303DLH_TEST_ZSCALE   630
191
 
192
// the i2c ACC interface
193
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
194
// register mapping
195
#define REG_ACC_CTRL1                   0x20
196
#define REG_ACC_CTRL2                   0x21
197
#define REG_ACC_CTRL3                   0x22
198
#define REG_ACC_CTRL4                   0x23
199
#define REG_ACC_CTRL5                   0x24
200
#define REG_ACC_HP_FILTER_RESET 0x25
201
#define REG_ACC_REFERENCE               0x26
202
#define REG_ACC_STATUS                  0x27
203
#define REG_ACC_X_LSB                   0x28
204
#define REG_ACC_X_MSB                   0x29
205
#define REG_ACC_Y_LSB                   0x2A
206
#define REG_ACC_Y_MSB                   0x2B
207
#define REG_ACC_Z_LSB                   0x2C
208
#define REG_ACC_Z_MSB                   0x2D
209
 
210
 
211
 
242 killagreg 212
typedef struct
213
{
253 killagreg 214
        u8 A;
215
        u8 B;
216
        u8 C;
217
} __attribute__((packed)) Identification_t;
242 killagreg 218
 
253 killagreg 219
volatile Identification_t NCMAG_Identification;
242 killagreg 220
 
253 killagreg 221
typedef struct
222
{
223
        u8 cra;
224
        u8 crb;
225
        u8 mode;
226
} __attribute__((packed)) MagConfig_t;
242 killagreg 227
 
253 killagreg 228
volatile MagConfig_t MagConfig;
242 killagreg 229
 
253 killagreg 230
typedef struct
231
{
232
        u8 ctrl_1;
233
        u8 ctrl_2;
234
        u8 ctrl_3;
235
        u8 ctrl_4;
236
        u8 ctrl_5;
237
} __attribute__((packed)) AccConfig_t;
238
 
239
volatile AccConfig_t AccConfig;
240
 
254 killagreg 241
volatile s16vec_t AccRawVector;
242
volatile s16vec_t MagRawVector;
253 killagreg 243
 
244
 
254 killagreg 245
u8 NCMag_CalibrationWrite(void)
246
{
247
        u8 i, crc = 0xAA;
248
        EEPROM_Result_t eres;
249
        u8 *pBuff = (u8*)&Calibration;
250
 
251
        Calibration.Version = CALIBRATION_VERSION;
256 killagreg 252
        for(i = 0; i<(sizeof(Calibration)-1); i++)
254 killagreg 253
        {
254
                crc += pBuff[i];        
255
        }
256
        Calibration.crc = ~crc;
257
        eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration));
258
        if(EEPROM_SUCCESS == eres) i = 1;
259
        else i = 0;
260
        return(i);     
261
}
262
 
263
u8 NCMag_CalibrationRead(void)
264
{
265
        u8 i, crc = 0xAA;
266
        u8 *pBuff = (u8*)&Calibration;
267
 
268
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
269
        {
256 killagreg 270
                for(i = 0; i<(sizeof(Calibration)-1); i++)
254 killagreg 271
                {
272
                        crc += pBuff[i];        
273
                }
274
                crc = ~crc;
275
                if(Calibration.crc != crc) return(0); // crc mismatch
257 killagreg 276
                if(Calibration.Version == CALIBRATION_VERSION) return(1);
254 killagreg 277
        }
278
        return(0);
279
}
280
 
281
 
282
void NCMAG_Calibrate(void)
283
{
284
        static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
256 killagreg 285
        static s16 X = 0, Y = 0, Z = 0;
254 killagreg 286
        static u8 OldCalState = 0;     
287
 
256 killagreg 288
        X = (4*X + MagRawVector.X + 3)/5;
289
        Y = (4*Y + MagRawVector.Y + 3)/5;
290
        Z = (4*Z + MagRawVector.Z + 3)/5;
291
 
254 killagreg 292
        switch(Compass_CalState)
293
        {
294
                case 1:
295
                        // 1st step of calibration
296
                        // initialize ranges
297
                        // used to change the orientation of the NC in the horizontal plane
298
                        Xmin =  10000;
299
                        Xmax = -10000;
300
                        Ymin =  10000;
301
                        Ymax = -10000;
302
                        Zmin =  10000;
303
                        Zmax = -10000;
304
                        break;
305
 
306
                case 2: // 2nd step of calibration
307
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
275 killagreg 308
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
309
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
310
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
311
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
254 killagreg 312
                        break;
313
 
314
                case 3: // 3rd step of calibration
315
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
316
                        break;
317
 
318
                case 4:
319
                        // find Min and Max of the Z-Sensor
275 killagreg 320
                        if(Z < Zmin)      { Zmin = Z; BeepTime = 80;}
321
                        else if(Z > Zmax) { Zmax = Z; BeepTime = 80;}
254 killagreg 322
                        break;
323
 
324
                case 5:
325
                        // Save values
326
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
327
                        {
267 holgerb 328
#define MIN_CALIBRATION    256
254 killagreg 329
                                Calibration.MagX.Range = Xmax - Xmin;
330
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
331
                                Calibration.MagY.Range = Ymax - Ymin;
332
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
333
                                Calibration.MagZ.Range = Zmax - Zmin;
334
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
265 holgerb 335
                                if((Calibration.MagX.Range > MIN_CALIBRATION) && (Calibration.MagY.Range > MIN_CALIBRATION) && (Calibration.MagZ.Range > MIN_CALIBRATION))
254 killagreg 336
                                {
337
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
270 killagreg 338
                                        BeepTime = 2500;
265 holgerb 339
                                        UART1_PutString("\r\n Calibration okay");
254 killagreg 340
                                }
341
                                else
342
                                {
343
                                        // restore old calibration data from eeprom
344
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
265 holgerb 345
                                        UART1_PutString("\r\n Calibration FAILED - Values too low: ");
346
                                        if(Calibration.MagX.Range < MIN_CALIBRATION) UART1_PutString("X! ");
347
                                        if(Calibration.MagY.Range < MIN_CALIBRATION) UART1_PutString("Y! ");
348
                                        if(Calibration.MagZ.Range < MIN_CALIBRATION) UART1_PutString("Z! ");
254 killagreg 349
                                }
350
                        }
351
                        break;
352
 
353
                default:
354
                        break; 
355
        }
356
        OldCalState = Compass_CalState;
357
}
358
 
242 killagreg 359
// ---------- call back handlers -----------------------------------------
360
 
361
// rx data handler for id info request
253 killagreg 362
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
254 killagreg 363
{       // if number of bytes are matching
253 killagreg 364
        if(RxBufferSize == sizeof(NCMAG_Identification) )
242 killagreg 365
        {
253 killagreg 366
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
367
        }
242 killagreg 368
}
254 killagreg 369
// rx data handler for magnetic sensor raw data
253 killagreg 370
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
254 killagreg 371
{       // if number of bytes are matching
372
        if(RxBufferSize == sizeof(MagRawVector) )
243 killagreg 373
        {       // byte order from big to little endian
256 killagreg 374
                s16 raw;
375
                raw = pRxBuffer[0]<<8;
376
                raw+= pRxBuffer[1];
377
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
378
                raw = pRxBuffer[2]<<8;
379
                raw+= pRxBuffer[3];
380
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.Y = raw;
381
                raw = pRxBuffer[4]<<8;
382
                raw+= pRxBuffer[5];
383
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.Z = raw;
242 killagreg 384
        }
254 killagreg 385
        if(Compass_CalState || !NCMAG_IsCalibrated)
386
        {       // direct output the raw data
387
                memcpy((u8*)&MagVector,(u8*)&MagRawVector, sizeof(MagVector));
388
                Compass_Heading = -1;
389
        }
390
        else
391
        {
392
                // update MagVector from MagRaw Vector by Scaling
393
                MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range);
394
                MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range);
395
                MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range);
256 killagreg 396
 
397
                if(UART_VersionInfo.HardwareError[0] & NC_ERROR0_SPI_RX)
398
                {
399
                        Compass_Heading = -1;
400
                }
401
                else // fc attitude is avialable
402
                {
403
                        // calculate attitude correction
404
                        // a float implementation takes too long for an ISR call!
405
                        s16 tmp, Hx, Hy;
406
                        s32 sinnick, cosnick, sinroll, cosroll;
407
 
408
                        tmp = FromFlightCtrl.AngleNick/10; // in deg 
409
                        sinnick = (s32)c_sin_8192(tmp);
410
                        cosnick = (s32)c_cos_8192(tmp);
411
                        tmp = FromFlightCtrl.AngleRoll/10; // in deg 
412
                        sinroll = (s32)c_sin_8192(tmp);
413
                        cosroll = (s32)c_cos_8192(tmp);
414
                        // tbd. compensation signs and oriantation has to be fixed 
257 killagreg 415
                        Hx = (s16)((MagVector.Y * cosnick + MagVector.Z * sinnick)/8192L);
416
                        Hy = (s16)((MagVector.X * cosroll - MagVector.Z * sinroll)/8192L);             
256 killagreg 417
                        // calculate heading
418
                        tmp = (s16)(c_tan2_546(Hy, Hx)/546L);
257 killagreg 419
                        if (tmp > 0) tmp = 360 - tmp;
420
                        else tmp = -tmp;
256 killagreg 421
                        Compass_Heading = tmp;
422
                }
254 killagreg 423
        }
242 killagreg 424
}
254 killagreg 425
// rx data handler  for acceleration raw data
253 killagreg 426
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
427
{       // if number of byte are matching
254 killagreg 428
        if(RxBufferSize == sizeof(AccRawVector) )
253 killagreg 429
        {
254 killagreg 430
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
253 killagreg 431
        }
432
}
254 killagreg 433
// rx data handler for reading magnetic sensor configuration
253 killagreg 434
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
435
{       // if number of byte are matching
436
        if(RxBufferSize == sizeof(MagConfig) )
437
        {
438
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
439
        }
440
}
254 killagreg 441
// rx data handler for reading acceleration sensor configuration
253 killagreg 442
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
443
{       // if number of byte are matching
444
        if(RxBufferSize == sizeof(AccConfig) )
445
        {
446
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
447
        }
448
}
254 killagreg 449
//----------------------------------------------------------------------
253 killagreg 450
 
254 killagreg 451
 
452
// ---------------------------------------------------------------------
253 killagreg 453
u8 NCMAG_SetMagConfig(void)
454
{
455
        u8 retval = 0;
456
        // try to catch the i2c buffer within 100 ms timeout
457
        if(I2C_LockBuffer(100))
458
        {
459
                u8 TxBytes = 0;
460
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;    
461
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
462
                TxBytes += sizeof(MagConfig);
463
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
464
                {
465
                        if(I2C_WaitForEndOfTransmission(100))
466
                        {
467
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
468
                        }
469
                }
470
        }
471
        return(retval);        
472
}
242 killagreg 473
 
253 killagreg 474
// ----------------------------------------------------------------------------------------
475
u8 NCMAG_GetMagConfig(void)
242 killagreg 476
{
253 killagreg 477
        u8 retval = 0;
252 killagreg 478
        // try to catch the i2c buffer within 100 ms timeout
248 killagreg 479
        if(I2C_LockBuffer(100))
242 killagreg 480
        {
253 killagreg 481
                u8 TxBytes = 0;
482
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;
483
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
248 killagreg 484
                {
252 killagreg 485
                        if(I2C_WaitForEndOfTransmission(100))
486
                        {
487
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
488
                        }
248 killagreg 489
                }
242 killagreg 490
        }
253 killagreg 491
        return(retval);        
242 killagreg 492
}
493
 
494
// ----------------------------------------------------------------------------------------
253 killagreg 495
u8 NCMAG_SetAccConfig(void)
242 killagreg 496
{
252 killagreg 497
        u8 retval = 0;
253 killagreg 498
        // try to catch the i2c buffer within 100 ms timeout
248 killagreg 499
        if(I2C_LockBuffer(100))
242 killagreg 500
        {
253 killagreg 501
                u8 TxBytes = 0;
502
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;  
503
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
504
                TxBytes += sizeof(AccConfig);
505
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
506
                {
507
                        if(I2C_WaitForEndOfTransmission(100))
508
                        {
509
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
510
                        }
511
                }
512
        }
513
        return(retval);        
514
}
515
 
516
// ----------------------------------------------------------------------------------------
517
u8 NCMAG_GetAccConfig(void)
518
{
519
        u8 retval = 0;
520
        // try to catch the i2c buffer within 100 ms timeout
521
        if(I2C_LockBuffer(100))
522
        {
523
                u8 TxBytes = 0;
524
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;
525
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
526
                {
527
                        if(I2C_WaitForEndOfTransmission(100))
528
                        {
529
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
530
                        }
531
                }
532
        }
533
        return(retval);        
534
}
535
 
536
// ----------------------------------------------------------------------------------------
537
u8 NCMAG_GetIdentification(void)
538
{
539
        u8 retval = 0;
540
        // try to catch the i2c buffer within 100 ms timeout
541
        if(I2C_LockBuffer(100))
542
        {
543
                u16 TxBytes = 0;
544
                NCMAG_Identification.A = 0xFF;
545
                NCMAG_Identification.B = 0xFF;
546
                NCMAG_Identification.C = 0xFF;
547
                I2C_Buffer[TxBytes++] = REG_MAG_IDA;
248 killagreg 548
                // initiate transmission
253 killagreg 549
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
248 killagreg 550
                {
253 killagreg 551
                        if(I2C_WaitForEndOfTransmission(100))
252 killagreg 552
                        {
553
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
554
                        }
248 killagreg 555
                }
242 killagreg 556
        }
253 killagreg 557
        return(retval);
242 killagreg 558
}
559
 
253 killagreg 560
// ----------------------------------------------------------------------------------------
561
void NCMAG_GetMagVector(void)
562
{
563
        // try to catch the I2C buffer within 0 ms
564
        if(I2C_LockBuffer(0))
565
        {
566
                u16 TxBytes = 0;
567
                // set register pointer
568
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
569
                // initiate transmission
570
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
571
        }
572
}
573
 
242 killagreg 574
//----------------------------------------------------------------
253 killagreg 575
void NCMAG_GetAccVector(void)
243 killagreg 576
{
252 killagreg 577
        // try to catch the I2C buffer within 0 ms
578
        if(I2C_LockBuffer(0))
243 killagreg 579
        {
248 killagreg 580
                u16 TxBytes = 0;
243 killagreg 581
                // set register pointer
253 killagreg 582
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB;
243 killagreg 583
                // initiate transmission
254 killagreg 584
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
243 killagreg 585
        }
586
}
587
 
253 killagreg 588
// --------------------------------------------------------
589
void NCMAG_UpdateCompass(void)
243 killagreg 590
{
591
        static u32 TimerCompassUpdate = 0;
592
 
254 killagreg 593
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
594
        {
595
                Compass_Heading = -1;
596
                return;
597
        }
253 killagreg 598
 
243 killagreg 599
        if(CheckDelay(TimerCompassUpdate))
600
        {
254 killagreg 601
                // check for new calibration state
602
                Compass_UpdateCalState();
603
                if(Compass_CalState) NCMAG_Calibrate();
604
                NCMAG_GetMagVector(); //Get new data;
243 killagreg 605
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
606
        }
607
}
608
 
254 killagreg 609
// --------------------------------------------------------
253 killagreg 610
u8 NCMAG_SelfTest(void)
243 killagreg 611
{
266 holgerb 612
        u8 msg[64];
275 killagreg 613
        static u8 done = 0;
266 holgerb 614
 
275 killagreg 615
        if(done) return(1);
616
 
271 holgerb 617
        #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;}
243 killagreg 618
        u32 time;
253 killagreg 619
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
620
        s16 xscale, yscale, zscale, scale_min, scale_max;
621
        u8 crb_gain, cra_rate;
622
        u8 i = 0, retval = 1;
243 killagreg 623
 
253 killagreg 624
        switch(NCMAG_MagType)
625
        {
626
                case MAG_TYPE_HMC5843:
627
                        crb_gain = HMC5843_CRB_GAIN_10GA;
628
                        cra_rate = HMC5843_CRA_RATE_50HZ;
629
                        xscale = HMC5843_TEST_XSCALE;
630
                        yscale = HMC5843_TEST_YSCALE;
631
                        zscale = HMC5843_TEST_ZSCALE;
632
                        break;
633
 
634
                case MAG_TYPE_LSM303DLH:
635
                        crb_gain = LSM303DLH_CRB_GAIN_13GA;
636
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
637
                        xscale = LSM303DLH_TEST_XSCALE;
638
                        yscale = LSM303DLH_TEST_YSCALE;
639
                        zscale = LSM303DLH_TEST_ZSCALE;
640
                        break;
641
 
642
                default:
643
                return(0);
644
        }
645
 
646
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
647
        MagConfig.crb = crb_gain;
648
        MagConfig.mode = MODE_CONTINUOUS;
649
        // activate positive bias field
650
        NCMAG_SetMagConfig();
251 killagreg 651
        // wait for stable readings
652
        time = SetDelay(50);
653
        while(!CheckDelay(time));
243 killagreg 654
        // averaging
253 killagreg 655
        #define AVERAGE 20
656
        for(i = 0; i<AVERAGE; i++)
243 killagreg 657
        {
253 killagreg 658
                NCMAG_GetMagVector();
243 killagreg 659
                time = SetDelay(20);
660
        while(!CheckDelay(time));
254 killagreg 661
                XMax += MagRawVector.X;
662
                YMax += MagRawVector.Y;
663
                ZMax += MagRawVector.Z;
243 killagreg 664
        }
253 killagreg 665
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
666
        // activate positive bias field
667
        NCMAG_SetMagConfig();
251 killagreg 668
    // wait for stable readings
669
        time = SetDelay(50);
670
        while(!CheckDelay(time));
243 killagreg 671
        // averaging
253 killagreg 672
        for(i = 0; i < AVERAGE; i++)
243 killagreg 673
        {
253 killagreg 674
                NCMAG_GetMagVector();
243 killagreg 675
                time = SetDelay(20);
676
        while(!CheckDelay(time));
254 killagreg 677
                XMin += MagRawVector.X;
678
                YMin += MagRawVector.Y;
679
                ZMin += MagRawVector.Z;
243 killagreg 680
        }
681
        // setup final configuration
253 killagreg 682
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
683
        // activate positive bias field
684
        NCMAG_SetMagConfig();
266 holgerb 685
        // check scale for all axes
243 killagreg 686
        // prepare scale limits
253 killagreg 687
        LIMITS(xscale, scale_min, scale_max);
267 holgerb 688
        xscale = (XMax - XMin)/(2*AVERAGE);
266 holgerb 689
        if((xscale > scale_max) || (xscale < scale_min))
690
     {
691
          retval = 0;
692
      sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
693
          UART1_PutString(msg);
694
     }
267 holgerb 695
        LIMITS(yscale, scale_min, scale_max);
266 holgerb 696
        yscale = (YMax - YMin)/(2*AVERAGE);
697
        if((yscale > scale_max) || (yscale < scale_min))
698
     {
699
          retval = 0;
700
      sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
701
          UART1_PutString(msg);
702
     }
267 holgerb 703
        LIMITS(zscale, scale_min, scale_max);
266 holgerb 704
        zscale = (ZMax - ZMin)/(2*AVERAGE);
705
        if((zscale > scale_max) || (zscale < scale_min))      
706
         {
707
          retval = 0;
708
      sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
709
          UART1_PutString(msg);
710
     }
275 killagreg 711
        done = retval;
253 killagreg 712
        return(retval);
243 killagreg 713
}
714
 
715
 
716
//----------------------------------------------------------------
253 killagreg 717
u8 NCMAG_Init(void)
242 killagreg 718
{
719
        u8 msg[64];
252 killagreg 720
        u8 retval = 0;
242 killagreg 721
        u8 repeat;
722
 
253 killagreg 723
        NCMAG_Present = 0;
724
        NCMAG_MagType = MAG_TYPE_HMC5843;       // assuming having an HMC5843
725
        // polling for LSM302DLH option
726
        repeat = 0;
727
        do
728
        {
729
                retval = NCMAG_GetAccConfig();
730
                if(retval) break; // break loop on success
731
                UART1_PutString(".");
732
                repeat++;
733
        }while(repeat < 3);
734
        if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH
242 killagreg 735
        // polling of identification
736
        repeat = 0;
737
        do
738
        {
253 killagreg 739
                retval = NCMAG_GetIdentification();
252 killagreg 740
                if(retval) break; // break loop on success
242 killagreg 741
                UART1_PutString(".");
742
                repeat++;
252 killagreg 743
        }while(repeat < 12);
253 killagreg 744
        // if we got an answer to id request
252 killagreg 745
        if(retval)
242 killagreg 746
        {
253 killagreg 747
                u8 n1[] = "HMC5843";
748
                u8 n2[] = "LSM303DLH";
749
                u8* pn;
750
                if(NCMAG_MagType == MAG_TYPE_LSM303DLH) pn = n2;
751
                else pn = n1;
752
                sprintf(msg, " %s ID%d/%d/%d", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C);
242 killagreg 753
                UART1_PutString(msg);
253 killagreg 754
                if (    (NCMAG_Identification.A == MAG_IDA)
755
                     && (NCMAG_Identification.B == MAG_IDB)
756
                         && (NCMAG_Identification.C == MAG_IDC))
242 killagreg 757
                {
268 killagreg 758
                        NCMAG_Present = 1;
253 killagreg 759
                        if(!NCMAG_SelfTest())
243 killagreg 760
                        {
253 killagreg 761
                                UART1_PutString(" Selftest failed!");
243 killagreg 762
                                LED_RED_ON;
268 killagreg 763
                                NCMAG_IsCalibrated = 0;
243 killagreg 764
                        }
254 killagreg 765
                        else
766
                        {
264 killagreg 767
                                if(EEPROM_Init())
768
                                {
769
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
770
                                        if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
771
                                }
772
                                else UART1_PutString("\r\n Calibration data not available!");
254 killagreg 773
                        }
242 killagreg 774
                }
775
                else
776
                {
254 killagreg 777
                        UART1_PutString("\n\r Not compatible!");
256 killagreg 778
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
242 killagreg 779
                        LED_RED_ON;
780
                }
781
        }
253 killagreg 782
        else // nothing found
783
        {
784
                NCMAG_MagType = MAG_TYPE_NONE;
785
                UART1_PutString("not found!");  
786
        }
787
        return(NCMAG_Present);
242 killagreg 788
}
789