Subversion Repositories NaviCtrl

Rev

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

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