Subversion Repositories NaviCtrl

Rev

Rev 339 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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