Subversion Repositories NaviCtrl

Rev

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

Rev 360 Rev 394
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <math.h>
56
#include <math.h>
57
#include <stdio.h>
57
#include <stdio.h>
58
#include <string.h>
58
#include <string.h>
59
#include "91x_lib.h"
59
#include "91x_lib.h"
60
#include "ncmag.h"
60
#include "ncmag.h"
61
#include "i2c.h"
61
#include "i2c.h"
62
#include "timer1.h"
62
#include "timer1.h"
63
#include "led.h"
63
#include "led.h"
64
#include "uart1.h"
64
#include "uart1.h"
65
#include "eeprom.h"
65
#include "eeprom.h"
66
#include "mymath.h"
66
#include "mymath.h"
67
#include "main.h"
67
#include "main.h"
68
 
68
 
69
u8 NCMAG_Present = 0;
69
u8 NCMAG_Present = 0;
70
u8 NCMAG_IsCalibrated = 0;
70
u8 NCMAG_IsCalibrated = 0;
-
 
71
 
71
 
72
// supported magnetic sensor types
72
#define MAG_TYPE_NONE           0
73
#define TYPE_NONE                       0
73
#define MAG_TYPE_HMC5843        1
74
#define TYPE_HMC5843            1
-
 
75
#define TYPE_LSM303DLH          2
-
 
76
#define TYPE_LSM303DLM          3
74
#define MAG_TYPE_LSM303DLH      2
77
 
75
u8 NCMAG_MagType = MAG_TYPE_NONE;
-
 
76
 
78
u8 NCMAG_SensorType = TYPE_NONE;
-
 
79
 
77
#define CALIBRATION_VERSION                     1
80
#define EEPROM_ADR_MAG_CALIBRATION              50
78
#define EEPROM_ADR_MAG_CALIBRATION              50
81
#define CALIBRATION_VERSION                     1
79
#define MAG_CALIBRATION_COMPATIBEL              0xA2
82
#define MAG_CALIBRATION_COMPATIBLE              0xA2
80
 
83
 
81
#define NCMAG_MIN_RAWVALUE -2047
84
#define NCMAG_MIN_RAWVALUE -2047
82
#define NCMAG_MAX_RAWVALUE  2047
85
#define NCMAG_MAX_RAWVALUE  2047
83
#define NCMAG_INVALID_DATA -4096
86
#define NCMAG_INVALID_DATA -4096
84
 
87
 
85
typedef struct
88
typedef struct
86
{
89
{
87
        s16 Range;
90
        s16 Range;
88
        s16 Offset;
91
        s16 Offset;
89
} __attribute__((packed)) Scaling_t;
92
} __attribute__((packed)) Scaling_t;
90
 
93
 
91
typedef struct
94
typedef struct
92
{
95
{
93
        Scaling_t MagX;
96
        Scaling_t MagX;
94
        Scaling_t MagY;
97
        Scaling_t MagY;
95
        Scaling_t MagZ;
98
        Scaling_t MagZ;
96
        u8 Version;
99
        u8 Version;
97
        u8 crc;
100
        u8 crc;
98
} __attribute__((packed)) Calibration_t;
101
} __attribute__((packed)) Calibration_t;
99
 
102
 
100
Calibration_t Calibration;              // calibration data in RAM 
103
Calibration_t Calibration;              // calibration data in RAM 
101
volatile s16vec_t AccRawVector;
104
volatile s16vec_t AccRawVector;
102
volatile s16vec_t MagRawVector;
105
volatile s16vec_t MagRawVector;
103
 
106
 
104
// i2c MAG interface
107
// i2c MAG interface
105
#define MAG_SLAVE_ADDRESS       0x3C    // i2C slave address mag. sensor registers
108
#define MAG_SLAVE_ADDRESS       0x3C    // i2C slave address mag. sensor registers
106
 
109
 
107
// register mapping
110
// register mapping
108
#define REG_MAG_CRA                     0x00
111
#define REG_MAG_CRA                     0x00
109
#define REG_MAG_CRB                     0x01
112
#define REG_MAG_CRB                     0x01
110
#define REG_MAG_MODE            0x02
113
#define REG_MAG_MODE            0x02
111
#define REG_MAG_DATAX_MSB       0x03
114
#define REG_MAG_DATAX_MSB       0x03
112
#define REG_MAG_DATAX_LSB       0x04
115
#define REG_MAG_DATAX_LSB       0x04
113
#define REG_MAG_DATAY_MSB       0x05
116
#define REG_MAG_DATAY_MSB       0x05
114
#define REG_MAG_DATAY_LSB       0x06
117
#define REG_MAG_DATAY_LSB       0x06
115
#define REG_MAG_DATAZ_MSB       0x07
118
#define REG_MAG_DATAZ_MSB       0x07
116
#define REG_MAG_DATAZ_LSB       0x08
119
#define REG_MAG_DATAZ_LSB       0x08
117
#define REG_MAG_STATUS          0x09
120
#define REG_MAG_STATUS          0x09
118
 
121
 
119
#define REG_MAG_IDA                     0x0A
122
#define REG_MAG_IDA                     0x0A
120
#define REG_MAG_IDB                     0x0B
123
#define REG_MAG_IDB                     0x0B
121
#define REG_MAG_IDC                     0x0C
124
#define REG_MAG_IDC                     0x0C
122
#define REG_MAG_IDF                     0x0F
125
#define REG_MAG_IDF                     0x0F  // WHO_AM_I _M = 0x03c when LSM303DLM is connected
123
 
126
 
124
// bit mask for configuration mode
127
// bit mask for configuration mode
125
#define CRA_MODE_MASK           0x03
128
#define CRA_MODE_MASK           0x03
126
#define CRA_MODE_NORMAL         0x00    //default
129
#define CRA_MODE_NORMAL         0x00    //default
127
#define CRA_MODE_POSBIAS        0x01
130
#define CRA_MODE_POSBIAS        0x01
128
#define CRA_MODE_NEGBIAS        0x02
131
#define CRA_MODE_NEGBIAS        0x02
129
#define CRA_MODE_SELFTEST       0x03
132
#define CRA_MODE_SELFTEST       0x03
130
 
133
 
131
// bit mask for measurement mode
134
// bit mask for measurement mode
132
#define MODE_MASK                       0xFF
135
#define MODE_MASK                       0xFF
133
#define MODE_CONTINUOUS         0x00
136
#define MODE_CONTINUOUS         0x00
134
#define MODE_SINGLE                     0x01    // default
137
#define MODE_SINGLE                     0x01    // default
135
#define MODE_IDLE                       0x02
138
#define MODE_IDLE                       0x02
136
#define MODE_SLEEP                      0x03
139
#define MODE_SLEEP                      0x03
137
 
140
 
138
// bit mask for rate
141
// bit mask for rate
139
#define CRA_RATE_MASK           0x1C
142
#define CRA_RATE_MASK           0x1C
140
 
143
 
141
// bit mask for gain
144
// bit mask for gain
142
#define CRB_GAIN_MASK           0xE0
145
#define CRB_GAIN_MASK           0xE0
143
 
146
 
144
// ids
147
// ids
145
#define MAG_IDA         0x48
148
#define MAG_IDA         0x48
146
#define MAG_IDB         0x34
149
#define MAG_IDB         0x34
147
#define MAG_IDC         0x33
150
#define MAG_IDC         0x33
-
 
151
#define MAG_IDF_LSM303DLM       0x3C
148
 
152
 
149
// the special HMC5843 interface
153
// the special HMC5843 interface
150
// bit mask for rate
154
// bit mask for rate
151
#define HMC5843_CRA_RATE_0_5HZ          0x00
155
#define HMC5843_CRA_RATE_0_5HZ          0x00
152
#define HMC5843_CRA_RATE_1HZ            0x04
156
#define HMC5843_CRA_RATE_1HZ            0x04
153
#define HMC5843_CRA_RATE_2HZ            0x08
157
#define HMC5843_CRA_RATE_2HZ            0x08
154
#define HMC5843_CRA_RATE_5HZ            0x0C
158
#define HMC5843_CRA_RATE_5HZ            0x0C
155
#define HMC5843_CRA_RATE_10HZ           0x10    //default
159
#define HMC5843_CRA_RATE_10HZ           0x10    //default
156
#define HMC5843_CRA_RATE_20HZ           0x14
160
#define HMC5843_CRA_RATE_20HZ           0x14
157
#define HMC5843_CRA_RATE_50HZ           0x18
161
#define HMC5843_CRA_RATE_50HZ           0x18
158
// bit mask for gain
162
// bit mask for gain
159
#define HMC5843_CRB_GAIN_07GA           0x00
163
#define HMC5843_CRB_GAIN_07GA           0x00
160
#define HMC5843_CRB_GAIN_10GA           0x20    //default
164
#define HMC5843_CRB_GAIN_10GA           0x20    //default
161
#define HMC5843_CRB_GAIN_15GA           0x40    // <--- we use this     
165
#define HMC5843_CRB_GAIN_15GA           0x40    // <--- we use this     
162
#define HMC5843_CRB_GAIN_20GA           0x60
166
#define HMC5843_CRB_GAIN_20GA           0x60
163
#define HMC5843_CRB_GAIN_32GA           0x80
167
#define HMC5843_CRB_GAIN_32GA           0x80
164
#define HMC5843_CRB_GAIN_38GA           0xA0
168
#define HMC5843_CRB_GAIN_38GA           0xA0
165
#define HMC5843_CRB_GAIN_45GA           0xC0
169
#define HMC5843_CRB_GAIN_45GA           0xC0
166
#define HMC5843_CRB_GAIN_65GA           0xE0
170
#define HMC5843_CRB_GAIN_65GA           0xE0
167
// self test value
171
// self test value
168
#define HMC5843_TEST_XSCALE             555
172
#define HMC5843_TEST_XSCALE             555
169
#define HMC5843_TEST_YSCALE             555
173
#define HMC5843_TEST_YSCALE             555
170
#define HMC5843_TEST_ZSCALE             555
174
#define HMC5843_TEST_ZSCALE             555
171
// clibration range
175
// calibration range
172
#define HMC5843_CALIBRATION_RANGE   600
176
#define HMC5843_CALIBRATION_RANGE   600
173
 
177
 
174
// the special LSM302DLH interface
178
// the special LSM302DLH interface
175
// bit mask for rate
179
// bit mask for rate
176
#define LSM303DLH_CRA_RATE_0_75HZ       0x00
180
#define LSM303DLH_CRA_RATE_0_75HZ       0x00
177
#define LSM303DLH_CRA_RATE_1_5HZ        0x04
181
#define LSM303DLH_CRA_RATE_1_5HZ        0x04
178
#define LSM303DLH_CRA_RATE_3_0HZ        0x08
182
#define LSM303DLH_CRA_RATE_3_0HZ        0x08
179
#define LSM303DLH_CRA_RATE_7_5HZ        0x0C
183
#define LSM303DLH_CRA_RATE_7_5HZ        0x0C
180
#define LSM303DLH_CRA_RATE_15HZ         0x10    //default
184
#define LSM303DLH_CRA_RATE_15HZ         0x10    //default
181
#define LSM303DLH_CRA_RATE_30HZ         0x14
185
#define LSM303DLH_CRA_RATE_30HZ         0x14
182
#define LSM303DLH_CRA_RATE_75HZ         0x18
186
#define LSM303DLH_CRA_RATE_75HZ         0x18
183
 
187
 
184
// bit mask for gain
188
// bit mask for gain
185
#define LSM303DLH_CRB_GAIN_XXGA         0x00
189
#define LSM303DLH_CRB_GAIN_XXGA         0x00
186
#define LSM303DLH_CRB_GAIN_13GA         0x20    //default
190
#define LSM303DLH_CRB_GAIN_13GA         0x20    //default
187
#define LSM303DLH_CRB_GAIN_19GA         0x40    // <--- we use this
191
#define LSM303DLH_CRB_GAIN_19GA         0x40    // <--- we use this
188
#define LSM303DLH_CRB_GAIN_25GA         0x60
192
#define LSM303DLH_CRB_GAIN_25GA         0x60
189
#define LSM303DLH_CRB_GAIN_40GA         0x80
193
#define LSM303DLH_CRB_GAIN_40GA         0x80
190
#define LSM303DLH_CRB_GAIN_47GA         0xA0
194
#define LSM303DLH_CRB_GAIN_47GA         0xA0
191
#define LSM303DLH_CRB_GAIN_56GA         0xC0
195
#define LSM303DLH_CRB_GAIN_56GA         0xC0
192
#define LSM303DLH_CRB_GAIN_81GA         0xE0
196
#define LSM303DLH_CRB_GAIN_81GA         0xE0
-
 
197
 
-
 
198
typedef struct
-
 
199
{
-
 
200
        u8 A;
-
 
201
        u8 B;
-
 
202
        u8 C;
-
 
203
} __attribute__((packed)) Identification_t;
-
 
204
volatile Identification_t NCMAG_Identification;
-
 
205
 
-
 
206
typedef struct
-
 
207
{
-
 
208
        u8 Sub;
-
 
209
} __attribute__((packed)) Identification2_t;
-
 
210
volatile Identification2_t NCMAG_Identification2;
-
 
211
 
-
 
212
typedef struct
-
 
213
{
-
 
214
        u8 cra;
-
 
215
        u8 crb;
-
 
216
        u8 mode;
-
 
217
} __attribute__((packed)) MagConfig_t;
-
 
218
 
-
 
219
volatile MagConfig_t MagConfig;
-
 
220
 
-
 
221
 
193
// self test value
222
// self test value
194
#define LSM303DLH_TEST_XSCALE   495
223
#define LSM303DLH_TEST_XSCALE   495
195
#define LSM303DLH_TEST_YSCALE   495
224
#define LSM303DLH_TEST_YSCALE   495
196
#define LSM303DLH_TEST_ZSCALE   470
225
#define LSM303DLH_TEST_ZSCALE   470
197
// clibration range
226
// clibration range
198
#define LSM303_CALIBRATION_RANGE   550
227
#define LSM303_CALIBRATION_RANGE   550
199
 
228
 
200
// the i2c ACC interface
229
// the i2c ACC interface
201
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
230
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
-
 
231
 
-
 
232
// multiple byte read/write mask
-
 
233
#define REG_ACC_MASK_AUTOINCREMENT 0x80
-
 
234
 
202
// register mapping
235
// register mapping
203
#define REG_ACC_CTRL1                   0x20
236
#define REG_ACC_CTRL1                   0x20
204
#define REG_ACC_CTRL2                   0x21
237
#define REG_ACC_CTRL2                   0x21
205
#define REG_ACC_CTRL3                   0x22
238
#define REG_ACC_CTRL3                   0x22
206
#define REG_ACC_CTRL4                   0x23
239
#define REG_ACC_CTRL4                   0x23
207
#define REG_ACC_CTRL5                   0x24
240
#define REG_ACC_CTRL5                   0x24
208
#define REG_ACC_HP_FILTER_RESET 0x25
241
#define REG_ACC_HP_FILTER_RESET 0x25
209
#define REG_ACC_REFERENCE               0x26
242
#define REG_ACC_REFERENCE               0x26
210
#define REG_ACC_STATUS                  0x27
243
#define REG_ACC_STATUS                  0x27
211
#define REG_ACC_X_LSB                   0x28
244
#define REG_ACC_X_LSB                   0x28
212
#define REG_ACC_X_MSB                   0x29
245
#define REG_ACC_X_MSB                   0x29
213
#define REG_ACC_Y_LSB                   0x2A
246
#define REG_ACC_Y_LSB                   0x2A
214
#define REG_ACC_Y_MSB                   0x2B
247
#define REG_ACC_Y_MSB                   0x2B
215
#define REG_ACC_Z_LSB                   0x2C
248
#define REG_ACC_Z_LSB                   0x2C
216
#define REG_ACC_Z_MSB                   0x2D
249
#define REG_ACC_Z_MSB                   0x2D
-
 
250
 
-
 
251
#define ACC_CRTL1_PM_DOWN               0x00
-
 
252
#define ACC_CRTL1_PM_NORMAL             0x20
-
 
253
#define ACC_CRTL1_PM_LOW_0_5HZ  0x40
-
 
254
#define ACC_CRTL1_PM_LOW_1HZ    0x60
-
 
255
#define ACC_CRTL1_PM_LOW_2HZ    0x80
-
 
256
#define ACC_CRTL1_PM_LOW_5HZ    0xA0
-
 
257
#define ACC_CRTL1_PM_LOW_10HZ   0xC0
-
 
258
// Output data rate in normal power mode
-
 
259
#define ACC_CRTL1_DR_50HZ               0x00
-
 
260
#define ACC_CRTL1_DR_100HZ              0x08
-
 
261
#define ACC_CRTL1_DR_400HZ              0x10
-
 
262
#define ACC_CRTL1_DR_1000HZ             0x18
-
 
263
// axis anable flags                    
-
 
264
#define ACC_CRTL1_XEN                   0x01
-
 
265
#define ACC_CRTL1_YEN                   0x02
-
 
266
#define ACC_CRTL1_ZEN                   0x04
-
 
267
 
-
 
268
#define ACC_CTRL4_BDU                   0x80 // Block data update, (0: continuos update; 1: output registers not updated between MSB and LSB reading)
-
 
269
#define ACC_CTRL4_BLE                   0x40 // Big/little endian, (0: data LSB @ lower address; 1: data MSB @ lower address)
-
 
270
#define ACC_CTRL4_FS_2G                 0x00
-
 
271
#define ACC_CTRL4_FS_4G                 0x10
-
 
272
#define ACC_CTRL4_FS_8G                 0x30
-
 
273
#define ACC_CTRL4_STSIGN_PLUS   0x00
-
 
274
#define ACC_CTRL4_STSIGN_MINUS  0x08
217
 
-
 
218
 
-
 
219
 
-
 
220
typedef struct
-
 
221
{
-
 
222
        u8 A;
-
 
223
        u8 B;
-
 
224
        u8 C;
-
 
225
} __attribute__((packed)) Identification_t;
-
 
226
volatile Identification_t NCMAG_Identification;
-
 
227
 
-
 
228
typedef struct
-
 
229
{
-
 
230
        u8 Sub;
-
 
231
} __attribute__((packed)) Identification2_t;
-
 
232
volatile Identification2_t NCMAG_Identification2;
-
 
233
 
-
 
234
typedef struct
-
 
235
{
-
 
236
        u8 cra;
-
 
237
        u8 crb;
275
#define ACC_CTRL4_ST_ENABLE             0x02
238
        u8 mode;
-
 
239
} __attribute__((packed)) MagConfig_t;
276
 
240
 
277
#define ACC_CTRL5_STW_ON                0x03
241
volatile MagConfig_t MagConfig;
278
#define ACC_CTRL5_STW_OFF               0x00
242
 
279
 
243
typedef struct
280
typedef struct
244
{
281
{
245
        u8 ctrl_1;
282
        u8 ctrl_1;
246
        u8 ctrl_2;
283
        u8 ctrl_2;
247
        u8 ctrl_3;
284
        u8 ctrl_3;
248
        u8 ctrl_4;
285
        u8 ctrl_4;
249
        u8 ctrl_5;
286
        u8 ctrl_5;
250
} __attribute__((packed)) AccConfig_t;
287
} __attribute__((packed)) AccConfig_t;
251
 
288
 
252
volatile AccConfig_t AccConfig;
289
volatile AccConfig_t AccConfig;
253
 
290
 
254
u8 NCMag_CalibrationWrite(void)
291
u8 NCMag_CalibrationWrite(void)
255
{
292
{
256
        u8 i, crc = MAG_CALIBRATION_COMPATIBEL;
293
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
257
        EEPROM_Result_t eres;
294
        EEPROM_Result_t eres;
258
        u8 *pBuff = (u8*)&Calibration;
295
        u8 *pBuff = (u8*)&Calibration;
259
 
296
 
260
        Calibration.Version = CALIBRATION_VERSION;
297
        Calibration.Version = CALIBRATION_VERSION;
261
        for(i = 0; i<(sizeof(Calibration)-1); i++)
298
        for(i = 0; i<(sizeof(Calibration)-1); i++)
262
        {
299
        {
263
                crc += pBuff[i];        
300
                crc += pBuff[i];        
264
        }
301
        }
265
        Calibration.crc = ~crc;
302
        Calibration.crc = ~crc;
266
        eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration));
303
        eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration));
267
        if(EEPROM_SUCCESS == eres) i = 1;
304
        if(EEPROM_SUCCESS == eres) i = 1;
268
        else i = 0;
305
        else i = 0;
269
        return(i);     
306
        return(i);     
270
}
307
}
271
 
308
 
272
u8 NCMag_CalibrationRead(void)
309
u8 NCMag_CalibrationRead(void)
273
{
310
{
274
        u8 i, crc = MAG_CALIBRATION_COMPATIBEL;
311
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
275
        u8 *pBuff = (u8*)&Calibration;
312
        u8 *pBuff = (u8*)&Calibration;
276
 
313
 
277
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
314
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
278
        {
315
        {
279
                for(i = 0; i<(sizeof(Calibration)-1); i++)
316
                for(i = 0; i<(sizeof(Calibration)-1); i++)
280
                {
317
                {
281
                        crc += pBuff[i];        
318
                        crc += pBuff[i];        
282
                }
319
                }
283
                crc = ~crc;
320
                crc = ~crc;
284
                if(Calibration.crc != crc) return(0); // crc mismatch
321
                if(Calibration.crc != crc) return(0); // crc mismatch
285
                if(Calibration.Version == CALIBRATION_VERSION) return(1);
322
                if(Calibration.Version == CALIBRATION_VERSION) return(1);
286
        }
323
        }
287
        return(0);
324
        return(0);
288
}
325
}
289
 
326
 
290
 
327
 
291
void NCMAG_Calibrate(void)
328
void NCMAG_Calibrate(void)
292
{
329
{
293
        u8 msg[64];
330
        u8 msg[64];
294
        static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
331
        static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
295
        static s16 X = 0, Y = 0, Z = 0;
332
        static s16 X = 0, Y = 0, Z = 0;
296
        static u8 OldCalState = 0;     
333
        static u8 OldCalState = 0;     
297
        s16 MinCaclibration = 450;
334
        s16 MinCalibration = 450;
298
 
335
 
299
        X = (4*X + MagRawVector.X + 3)/5;
336
        X = (4*X + MagRawVector.X + 3)/5;
300
        Y = (4*Y + MagRawVector.Y + 3)/5;
337
        Y = (4*Y + MagRawVector.Y + 3)/5;
301
        Z = (4*Z + MagRawVector.Z + 3)/5;
338
        Z = (4*Z + MagRawVector.Z + 3)/5;
302
 
339
 
303
        switch(Compass_CalState)
340
        switch(Compass_CalState)
304
        {
341
        {
305
                case 1:
342
                case 1:
306
                        // 1st step of calibration
343
                        // 1st step of calibration
307
                        // initialize ranges
344
                        // initialize ranges
308
                        // used to change the orientation of the NC in the horizontal plane
345
                        // used to change the orientation of the NC in the horizontal plane
309
                        Xmin =  10000;
346
                        Xmin =  10000;
310
                        Xmax = -10000;
347
                        Xmax = -10000;
311
                        Ymin =  10000;
348
                        Ymin =  10000;
312
                        Ymax = -10000;
349
                        Ymax = -10000;
313
                        Zmin =  10000;
350
                        Zmin =  10000;
314
                        Zmax = -10000;
351
                        Zmax = -10000;
315
                        break;
352
                        break;
316
               
353
               
317
                case 2: // 2nd step of calibration
354
                case 2: // 2nd step of calibration
318
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
355
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
319
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
356
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
320
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
357
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
321
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
358
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
322
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
359
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
323
                        break;
360
                        break;
324
 
361
 
325
                case 3: // 3rd step of calibration
362
                case 3: // 3rd step of calibration
326
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
363
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
327
                        break;
364
                        break;
328
 
365
 
329
                case 4:
366
                case 4:
330
                        // find Min and Max of the Z-Sensor
367
                        // find Min and Max of the Z-Sensor
331
                        if(Z < Zmin)      { Zmin = Z; BeepTime = 80;}
368
                        if(Z < Zmin)      { Zmin = Z; BeepTime = 80;}
332
                        else if(Z > Zmax) { Zmax = Z; BeepTime = 80;}
369
                        else if(Z > Zmax) { Zmax = Z; BeepTime = 80;}
333
                        break;
370
                        break;
334
               
371
               
335
                case 5:
372
                case 5:
336
                        // Save values
373
                        // Save values
337
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
374
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
338
                        {
375
                        {
339
//                              #define MIN_CALIBRATION    256
-
 
340
                                if(NCMAG_MagType == MAG_TYPE_HMC5843)
376
                                switch(NCMAG_SensorType)
341
                                 {
377
                                {
-
 
378
                                        case TYPE_HMC5843:
342
                                  UART1_PutString("\r\nHMC5843 calibration\n\r");
379
                                                UART1_PutString("\r\nHMC5843 calibration\n\r");
343
                                  MinCaclibration = HMC5843_CALIBRATION_RANGE;
380
                                                MinCalibration = HMC5843_CALIBRATION_RANGE;
344
                                 }
381
                                                break;
-
 
382
 
345
                                if(NCMAG_MagType == MAG_TYPE_LSM303DLH)
383
                                        case TYPE_LSM303DLH:
346
                                 {
384
                                        case TYPE_LSM303DLM:
347
                                  UART1_PutString("\r\n\r\nLSM303 calibration\n\r");
385
                                                UART1_PutString("\r\n\r\nLSM303 calibration\n\r");
348
                                  MinCaclibration =LSM303_CALIBRATION_RANGE;
386
                                                MinCalibration = LSM303_CALIBRATION_RANGE;
-
 
387
                                        break;
349
                                 }
388
                                }
350
                                if(EarthMagneticStrengthTheoretic)
389
                                if(EarthMagneticStrengthTheoretic)
351
                                 {
390
                                 {
352
                                  MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50;
391
                                  MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50;
353
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
392
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
354
                                  UART1_PutString(msg);
393
                                  UART1_PutString(msg);
355
                                 }
394
                                 }
356
                            else UART1_PutString("without GPS\n\r");
395
                            else UART1_PutString("without GPS\n\r");
357
 
396
 
358
                                Calibration.MagX.Range = Xmax - Xmin;
397
                                Calibration.MagX.Range = Xmax - Xmin;
359
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
398
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
360
                                Calibration.MagY.Range = Ymax - Ymin;
399
                                Calibration.MagY.Range = Ymax - Ymin;
361
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
400
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
362
                                Calibration.MagZ.Range = Zmax - Zmin;
401
                                Calibration.MagZ.Range = Zmax - Zmin;
363
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
402
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
364
                                if((Calibration.MagX.Range > MinCaclibration) && (Calibration.MagY.Range > MinCaclibration) && (Calibration.MagZ.Range > MinCaclibration))
403
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
365
                                {
404
                                {
366
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
405
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
367
                                        BeepTime = 2500;
406
                                        BeepTime = 2500;
368
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
407
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
369
                                }
408
                                }
370
                                else
409
                                else
371
                                {
410
                                {
372
                                        UART1_PutString("\r\nCalibration FAILED - Values too low: ");
411
                                        UART1_PutString("\r\nCalibration FAILED - Values too low: ");
373
                                    if(Calibration.MagX.Range < MinCaclibration) UART1_PutString("X! ");
412
                                    if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! ");
374
                                    if(Calibration.MagY.Range < MinCaclibration) UART1_PutString("Y! ");
413
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
375
                                    if(Calibration.MagZ.Range < MinCaclibration) UART1_PutString("Z! ");
414
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
376
                                        UART1_PutString("\r\n");
415
                                        UART1_PutString("\r\n");
377
 
416
 
378
                                        // restore old calibration data from eeprom
417
                                        // restore old calibration data from eeprom
379
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
418
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
380
                                }
419
                                }
381
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
420
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
382
                                        UART1_PutString(msg);
421
                                        UART1_PutString(msg);
383
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
422
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
384
                                        UART1_PutString(msg);
423
                                        UART1_PutString(msg);
385
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
424
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
386
                                        UART1_PutString(msg);
425
                                        UART1_PutString(msg);
387
                                        sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration);
426
                                        sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration);
388
                                        UART1_PutString(msg);
427
                                        UART1_PutString(msg);
389
                        }
428
                        }
390
                        break;
429
                        break;
391
                       
430
                       
392
                default:
431
                default:
393
                        break; 
432
                        break; 
394
        }
433
        }
395
        OldCalState = Compass_CalState;
434
        OldCalState = Compass_CalState;
396
}
435
}
397
 
436
 
398
// ---------- call back handlers -----------------------------------------
437
// ---------- call back handlers -----------------------------------------
399
 
438
 
400
// rx data handler for id info request
439
// rx data handler for id info request
401
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
440
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
402
{       // if number of bytes are matching
441
{       // if number of bytes are matching
403
        if(RxBufferSize == sizeof(NCMAG_Identification) )
442
        if(RxBufferSize == sizeof(NCMAG_Identification) )
404
        {
443
        {
405
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
444
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
406
        }
445
        }
407
}
446
}
408
 
447
 
409
void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize)
448
void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize)
410
{       // if number of bytes are matching
449
{       // if number of bytes are matching
411
        if(RxBufferSize == sizeof(NCMAG_Identification2))
450
        if(RxBufferSize == sizeof(NCMAG_Identification2))
412
        {
451
        {
413
                memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2));
452
                memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2));
414
        }
453
        }
415
}
454
}
416
 
455
 
417
// rx data handler for magnetic sensor raw data
456
// rx data handler for magnetic sensor raw data
418
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
457
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
419
{       // if number of bytes are matching
458
{       // if number of bytes are matching
420
        if(RxBufferSize == sizeof(MagRawVector) )
459
        if(RxBufferSize == sizeof(MagRawVector) )
421
        {       // byte order from big to little endian
460
        {       // byte order from big to little endian
422
                s16 raw;
461
                s16 raw;
423
                raw = pRxBuffer[0]<<8;
462
                raw = pRxBuffer[0]<<8;
424
                raw+= pRxBuffer[1];
463
                raw+= pRxBuffer[1];
425
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
464
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw;
426
                raw = pRxBuffer[2]<<8;
465
                raw = pRxBuffer[2]<<8;
427
                raw+= pRxBuffer[3];
466
                raw+= pRxBuffer[3];
428
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
467
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
429
                {
468
                {
430
                  if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged
469
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  MagRawVector.Z = raw; // here Z and Y are exchanged
431
                  else MagRawVector.Y = raw;
470
                        else                                                                    MagRawVector.Y = raw;
432
                }
471
                }
433
                raw = pRxBuffer[4]<<8;
472
                raw = pRxBuffer[4]<<8;
434
                raw+= pRxBuffer[5];
473
                raw+= pRxBuffer[5];
435
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
474
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
436
                {
475
                {
437
                  if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged
476
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  MagRawVector.Y = raw; // here Z and Y are exchanged
438
                  else MagRawVector.Z = raw;
477
                        else                                                                    MagRawVector.Z = raw;
439
                }
478
                }
440
//MagRawVector.X += 2 * ((s32) FC.Poti[7] - 128);
-
 
441
        }
479
        }
442
        if(Compass_CalState || !NCMAG_IsCalibrated)
480
        if(Compass_CalState || !NCMAG_IsCalibrated)
443
        {       // mark out data invalid
481
        {       // mark out data invalid
444
                MagVector.X = MagRawVector.X;
482
                MagVector.X = MagRawVector.X;
445
                MagVector.Y = MagRawVector.Y;
483
                MagVector.Y = MagRawVector.Y;
446
                MagVector.Z = MagRawVector.Z;
484
                MagVector.Z = MagRawVector.Z;
447
                Compass_Heading = -1;
485
                Compass_Heading = -1;
448
        }
486
        }
449
        else
487
        else
450
        {
488
        {
451
                // update MagVector from MagRaw Vector by Scaling
489
                // update MagVector from MagRaw Vector by Scaling
452
                MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range);
490
                MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range);
453
                MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range);
491
                MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range);
454
                MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range);
492
                MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range);
455
                Compass_CalcHeading();
493
                Compass_CalcHeading();
456
        }
494
        }
457
}
495
}
458
// rx data handler  for acceleration raw data
496
// rx data handler  for acceleration raw data
459
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
497
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
460
{       // if number of byte are matching
498
{       // if number of byte are matching
461
        if(RxBufferSize == sizeof(AccRawVector) )
499
        if(RxBufferSize == sizeof(AccRawVector) )
462
        {
500
        {
463
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
501
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
464
        }
502
        }
-
 
503
        DebugOut.Analog[16] = AccRawVector.X;
-
 
504
        DebugOut.Analog[17] = AccRawVector.Y;
-
 
505
        DebugOut.Analog[18] = AccRawVector.Z;
465
}
506
}
466
// rx data handler for reading magnetic sensor configuration
507
// rx data handler for reading magnetic sensor configuration
467
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
508
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
468
{       // if number of byte are matching
509
{       // if number of byte are matching
469
        if(RxBufferSize == sizeof(MagConfig) )
510
        if(RxBufferSize == sizeof(MagConfig) )
470
        {
511
        {
471
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
512
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
472
        }
513
        }
473
}
514
}
474
// rx data handler for reading acceleration sensor configuration
515
// rx data handler for reading acceleration sensor configuration
475
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
516
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
476
{       // if number of byte are matching
517
{       // if number of byte are matching
477
        if(RxBufferSize == sizeof(AccConfig) )
518
        if(RxBufferSize == sizeof(AccConfig) )
478
        {
519
        {
479
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
520
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
480
        }
521
        }
481
}
522
}
482
//----------------------------------------------------------------------
523
//----------------------------------------------------------------------
483
 
524
 
484
 
525
 
485
// ---------------------------------------------------------------------
526
// ---------------------------------------------------------------------
486
u8 NCMAG_SetMagConfig(void)
527
u8 NCMAG_SetMagConfig(void)
487
{
528
{
488
        u8 retval = 0;
529
        u8 retval = 0;
489
        // try to catch the i2c buffer within 100 ms timeout
530
        // try to catch the i2c buffer within 100 ms timeout
490
        if(I2C_LockBuffer(100))
531
        if(I2C_LockBuffer(100))
491
        {
532
        {
492
                u8 TxBytes = 0;
533
                u8 TxBytes = 0;
493
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;    
534
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;    
494
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
535
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
495
                TxBytes += sizeof(MagConfig);
536
                TxBytes += sizeof(MagConfig);
496
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
537
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
497
                {
538
                {
498
                        if(I2C_WaitForEndOfTransmission(100))
539
                        if(I2C_WaitForEndOfTransmission(100))
499
                        {
540
                        {
500
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
541
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
501
                        }
542
                        }
502
                }
543
                }
503
        }
544
        }
504
        return(retval);        
545
        return(retval);        
505
}
546
}
506
 
547
 
507
// ----------------------------------------------------------------------------------------
548
// ----------------------------------------------------------------------------------------
508
u8 NCMAG_GetMagConfig(void)
549
u8 NCMAG_GetMagConfig(void)
509
{
550
{
510
        u8 retval = 0;
551
        u8 retval = 0;
511
        // try to catch the i2c buffer within 100 ms timeout
552
        // try to catch the i2c buffer within 100 ms timeout
512
        if(I2C_LockBuffer(100))
553
        if(I2C_LockBuffer(100))
513
        {
554
        {
514
                u8 TxBytes = 0;
555
                u8 TxBytes = 0;
515
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;
556
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;
516
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
557
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
517
                {
558
                {
518
                        if(I2C_WaitForEndOfTransmission(100))
559
                        if(I2C_WaitForEndOfTransmission(100))
519
                        {
560
                        {
520
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
561
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
521
                        }
562
                        }
522
                }
563
                }
523
        }
564
        }
524
        return(retval);        
565
        return(retval);        
525
}
566
}
526
 
567
 
527
// ----------------------------------------------------------------------------------------
568
// ----------------------------------------------------------------------------------------
528
u8 NCMAG_SetAccConfig(void)
569
u8 NCMAG_SetAccConfig(void)
529
{
570
{
530
        u8 retval = 0;
571
        u8 retval = 0;
531
        // try to catch the i2c buffer within 100 ms timeout
572
        // try to catch the i2c buffer within 100 ms timeout
532
        if(I2C_LockBuffer(100))
573
        if(I2C_LockBuffer(100))
533
        {
574
        {
534
                u8 TxBytes = 0;
575
                u8 TxBytes = 0;
535
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;  
576
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;      
536
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
577
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
537
                TxBytes += sizeof(AccConfig);
578
                TxBytes += sizeof(AccConfig);
538
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
579
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
539
                {
580
                {
540
                        if(I2C_WaitForEndOfTransmission(100))
581
                        if(I2C_WaitForEndOfTransmission(100))
541
                        {
582
                        {
542
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
583
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
543
                        }
584
                        }
544
                }
585
                }
545
        }
586
        }
546
        return(retval);        
587
        return(retval);        
547
}
588
}
548
 
589
 
549
// ----------------------------------------------------------------------------------------
590
// ----------------------------------------------------------------------------------------
550
u8 NCMAG_GetAccConfig(void)
591
u8 NCMAG_GetAccConfig(void)
551
{
592
{
552
        u8 retval = 0;
593
        u8 retval = 0;
553
        // try to catch the i2c buffer within 100 ms timeout
594
        // try to catch the i2c buffer within 100 ms timeout
554
        if(I2C_LockBuffer(100))
595
        if(I2C_LockBuffer(100))
555
        {
596
        {
556
                u8 TxBytes = 0;
597
                u8 TxBytes = 0;
557
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;
598
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
558
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
599
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
559
                {
600
                {
560
                        if(I2C_WaitForEndOfTransmission(100))
601
                        if(I2C_WaitForEndOfTransmission(100))
561
                        {
602
                        {
562
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
603
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
563
                        }
604
                        }
564
                }
605
                }
565
        }
606
        }
566
        return(retval);        
607
        return(retval);        
567
}
608
}
568
 
609
 
569
// ----------------------------------------------------------------------------------------
610
// ----------------------------------------------------------------------------------------
570
u8 NCMAG_GetIdentification(void)
611
u8 NCMAG_GetIdentification(void)
571
{
612
{
572
        u8 retval = 0;
613
        u8 retval = 0;
573
        // try to catch the i2c buffer within 100 ms timeout
614
        // try to catch the i2c buffer within 100 ms timeout
574
        if(I2C_LockBuffer(100))
615
        if(I2C_LockBuffer(100))
575
        {
616
        {
576
                u16 TxBytes = 0;
617
                u16 TxBytes = 0;
577
                NCMAG_Identification.A = 0xFF;
618
                NCMAG_Identification.A = 0xFF;
578
                NCMAG_Identification.B = 0xFF;
619
                NCMAG_Identification.B = 0xFF;
579
                NCMAG_Identification.C = 0xFF;
620
                NCMAG_Identification.C = 0xFF;
580
                I2C_Buffer[TxBytes++] = REG_MAG_IDA;
621
                I2C_Buffer[TxBytes++] = REG_MAG_IDA;
581
                // initiate transmission
622
                // initiate transmission
582
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
623
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
583
                {
624
                {
584
                        if(I2C_WaitForEndOfTransmission(100))
625
                        if(I2C_WaitForEndOfTransmission(100))
585
                        {
626
                        {
586
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
627
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
587
                        }
628
                        }
588
                }
629
                }
589
        }
630
        }
590
        return(retval);
631
        return(retval);
591
}
632
}
592
 
633
 
593
u8 NCMAG_GetIdentification_Sub(void)
634
u8 NCMAG_GetIdentification_Sub(void)
594
{
635
{
595
        u8 retval = 0;
636
        u8 retval = 0;
596
        // try to catch the i2c buffer within 100 ms timeout
637
        // try to catch the i2c buffer within 100 ms timeout
597
        if(I2C_LockBuffer(100))
638
        if(I2C_LockBuffer(100))
598
        {
639
        {
599
                u16 TxBytes = 0;
640
                u16 TxBytes = 0;
600
                NCMAG_Identification2.Sub = 0xFF;
641
                NCMAG_Identification2.Sub = 0xFF;
601
                I2C_Buffer[TxBytes++] = REG_MAG_IDF;
642
                I2C_Buffer[TxBytes++] = REG_MAG_IDF;
602
                // initiate transmission
643
                // initiate transmission
603
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
644
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
604
                {
645
                {
605
                        if(I2C_WaitForEndOfTransmission(100))
646
                        if(I2C_WaitForEndOfTransmission(100))
606
                        {
647
                        {
607
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
648
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
608
                        }
649
                        }
609
                }
650
                }
610
        }
651
        }
611
        return(retval);
652
        return(retval);
612
}
653
}
613
 
654
 
614
 
655
 
615
// ----------------------------------------------------------------------------------------
656
// ----------------------------------------------------------------------------------------
616
void NCMAG_GetMagVector(void)
657
void NCMAG_GetMagVector(void)
617
{
658
{
618
        // try to catch the I2C buffer within 0 ms
659
        // try to catch the I2C buffer within 0 ms
619
        if(I2C_LockBuffer(0))
660
        if(I2C_LockBuffer(0))
620
        {
661
        {
621
//       s16 tmp;
-
 
622
                u16 TxBytes = 0;
662
                u16 TxBytes = 0;
623
                // set register pointer
663
                // set register pointer
624
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
664
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
625
                // initiate transmission
665
                // initiate transmission
626
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
666
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
627
        }
667
        }
628
}
668
}
629
 
669
 
630
//----------------------------------------------------------------
670
//----------------------------------------------------------------
631
void NCMAG_GetAccVector(void)
671
void NCMAG_GetAccVector(void)
632
{
672
{
633
        // try to catch the I2C buffer within 0 ms
673
        // try to catch the I2C buffer within 0 ms
634
        if(I2C_LockBuffer(0))
674
        if(I2C_LockBuffer(0))
635
        {
675
        {
636
                u16 TxBytes = 0;
676
                u16 TxBytes = 0;
637
                // set register pointer
677
                // set register pointer
638
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB;
678
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
639
                // initiate transmission
679
                // initiate transmission
640
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
680
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
641
        }
681
        }
642
}
682
}
643
 
683
 
644
//----------------------------------------------------------------
684
//----------------------------------------------------------------
645
void InitNC_MagnetSensor(void)
685
u8 InitNC_MagnetSensor(void)
646
{
686
{
647
        s16 xscale, yscale, zscale;
-
 
648
        u8 crb_gain, cra_rate;
687
        u8 crb_gain, cra_rate;
649
//      u8  retval = 1;
-
 
650
 
688
 
651
        switch(NCMAG_MagType)
689
        switch(NCMAG_SensorType)
652
        {
690
        {
653
                case MAG_TYPE_HMC5843:
691
                case TYPE_HMC5843:
654
                        crb_gain = HMC5843_CRB_GAIN_15GA;
692
                        crb_gain = HMC5843_CRB_GAIN_15GA;
655
                        cra_rate = HMC5843_CRA_RATE_50HZ;
-
 
656
                        xscale = HMC5843_TEST_XSCALE;
-
 
657
                        yscale = HMC5843_TEST_YSCALE;
-
 
658
                        zscale = HMC5843_TEST_ZSCALE;
693
                        cra_rate = HMC5843_CRA_RATE_50HZ;
659
                        break;
694
                        break;
-
 
695
 
660
 
696
                case TYPE_LSM303DLH:
661
                case MAG_TYPE_LSM303DLH:
697
                case TYPE_LSM303DLM:
662
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
-
 
663
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
-
 
664
                        xscale = LSM303DLH_TEST_XSCALE;
-
 
665
                        yscale = LSM303DLH_TEST_YSCALE;
698
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
666
                        zscale = LSM303DLH_TEST_ZSCALE;
699
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
667
                        break;
700
                        break;
668
 
701
 
669
                default:
702
                default:
670
                return;
703
                return(0);
671
        }
704
        }
672
 
705
 
673
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
706
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
674
        MagConfig.crb = crb_gain;
707
        MagConfig.crb = crb_gain;
675
        MagConfig.mode = MODE_CONTINUOUS;
708
        MagConfig.mode = MODE_CONTINUOUS;
676
        NCMAG_SetMagConfig();
709
        return(NCMAG_SetMagConfig());
677
}
710
}
-
 
711
 
-
 
712
//----------------------------------------------------------------
678
 
713
u8 NCMAG_Init_ACCSensor(void)
-
 
714
{
-
 
715
        AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_400HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN;
-
 
716
        AccConfig.ctrl_2 = 0x00;
-
 
717
        AccConfig.ctrl_3 = 0x00;
-
 
718
        AccConfig.ctrl_4 = ACC_CTRL4_BDU|ACC_CTRL4_FS_2G;
-
 
719
        AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF;
-
 
720
        return(NCMAG_SetAccConfig());
679
 
721
}
680
// --------------------------------------------------------
722
// --------------------------------------------------------
681
void NCMAG_Update(void)
723
void NCMAG_Update(void)
682
{
724
{
683
        static u32 TimerUpdate = 0;
725
        static u32 TimerUpdate = 0;
684
        static u8 send_config = 0;
726
        static u8 send_config = 0;
-
 
727
        u32 delay = 20;
685
 
728
 
686
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
729
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present )
687
        {
730
        {
688
                Compass_Heading = -1;
731
                Compass_Heading = -1;
689
                DebugOut.Analog[14]++; // count I2C error
732
                DebugOut.Analog[14]++; // count I2C error
690
                return;
733
                return;
691
        }
734
        }
692
        if(CheckDelay(TimerUpdate))
735
        if(CheckDelay(TimerUpdate))
693
        {
736
        {
694
           if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
737
                if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
695
       if(++send_config == 25)   // 500ms
738
        if(++send_config == 25)   // 500ms
696
            {
739
                {
697
                 send_config = 0;
740
                        send_config = 0;
698
             InitNC_MagnetSensor();
741
                InitNC_MagnetSensor();
699
                 TimerUpdate = SetDelay(15);    // back into the old time-slot
742
                        TimerUpdate = SetDelay(15);    // back into the old time-slot
700
            }
743
                }
701
                else
744
                else
702
                {
745
                {
-
 
746
                        static u8 s = 0;
703
                // check for new calibration state
747
                        // check for new calibration state
704
                Compass_UpdateCalState();
748
                        Compass_UpdateCalState();
705
                if(Compass_CalState) NCMAG_Calibrate();
749
                        if(Compass_CalState) NCMAG_Calibrate();
-
 
750
                       
-
 
751
                        // in case of LSM303 type
-
 
752
                        switch(NCMAG_SensorType)
-
 
753
                        {
-
 
754
                                case TYPE_HMC5843:                             
-
 
755
                                        NCMAG_GetMagVector();
-
 
756
                                        delay = 20;
-
 
757
                                        break;
-
 
758
                                case TYPE_LSM303DLH:
-
 
759
                                case TYPE_LSM303DLM:
706
                NCMAG_GetMagVector(); //Get new data;
760
                                        if(s){ NCMAG_GetMagVector(); s = 0;}
-
 
761
                                        else { NCMAG_GetAccVector(); s = 1;}
-
 
762
                                        delay = 10;
-
 
763
                                        break;                           
-
 
764
                        }
707
                if(send_config == 24) TimerUpdate = SetDelay(5);    // next event is the re-configuration
765
                        if(send_config == 24) TimerUpdate = SetDelay(5);    // next event is the re-configuration
708
                else TimerUpdate = SetDelay(20);    // every 20 ms are 50 Hz
766
                        else TimerUpdate = SetDelay(delay);    // every 20 ms are 50 Hz
709
                }
767
                }
710
        }
768
        }
711
}
769
}
712
 
770
 
713
 
771
 
714
// --------------------------------------------------------
772
// --------------------------------------------------------
715
u8 NCMAG_SelfTest(void)
773
u8 NCMAG_SelfTest(void)
716
{
774
{
717
        u8 msg[64];
775
        u8 msg[64];
718
        static u8 done = 0;
776
        static u8 done = 0;
719
 
777
 
720
        if(done) return(1);        // just make it once
778
        if(done) return(1);        // just make it once
721
       
779
       
722
        #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;}
780
        #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;}
723
        u32 time;
781
        u32 time;
724
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
782
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
725
        s16 xscale, yscale, zscale, scale_min, scale_max;
783
        s16 xscale, yscale, zscale, scale_min, scale_max;
726
        u8 crb_gain, cra_rate;
784
        u8 crb_gain, cra_rate;
727
        u8 i = 0, retval = 1;
785
        u8 i = 0, retval = 1;
728
 
786
 
729
        switch(NCMAG_MagType)
787
        switch(NCMAG_SensorType)
730
        {
788
        {
731
                case MAG_TYPE_HMC5843:
789
                case TYPE_HMC5843:
732
                        crb_gain = HMC5843_CRB_GAIN_15GA;
790
                        crb_gain = HMC5843_CRB_GAIN_15GA;
733
                        cra_rate = HMC5843_CRA_RATE_50HZ;
791
                        cra_rate = HMC5843_CRA_RATE_50HZ;
734
                        xscale = HMC5843_TEST_XSCALE;
792
                        xscale = HMC5843_TEST_XSCALE;
735
                        yscale = HMC5843_TEST_YSCALE;
793
                        yscale = HMC5843_TEST_YSCALE;
736
                        zscale = HMC5843_TEST_ZSCALE;
794
                        zscale = HMC5843_TEST_ZSCALE;
737
                        break;
795
                        break;
738
 
796
 
739
                case MAG_TYPE_LSM303DLH:
797
                case TYPE_LSM303DLH:
740
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
798
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
741
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
799
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
742
                        xscale = LSM303DLH_TEST_XSCALE;
800
                        xscale = LSM303DLH_TEST_XSCALE;
743
                        yscale = LSM303DLH_TEST_YSCALE;
801
                        yscale = LSM303DLH_TEST_YSCALE;
744
                        zscale = LSM303DLH_TEST_ZSCALE;
802
                        zscale = LSM303DLH_TEST_ZSCALE;
745
                        break;
803
                        break;
-
 
804
 
-
 
805
                case TYPE_LSM303DLM:
-
 
806
                        // does not support self test feature 
-
 
807
                        done = retval;
-
 
808
                        return(retval);
-
 
809
                        break;
746
 
810
 
747
                default:
811
                default:
748
                return(0);
812
                        return(0);
749
        }
813
        }
750
 
814
 
751
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
815
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
752
        MagConfig.crb = crb_gain;
816
        MagConfig.crb = crb_gain;
753
        MagConfig.mode = MODE_CONTINUOUS;
817
        MagConfig.mode = MODE_CONTINUOUS;
754
        // activate positive bias field
818
        // activate positive bias field
755
        NCMAG_SetMagConfig();
819
        NCMAG_SetMagConfig();
756
        // wait for stable readings
820
        // wait for stable readings
757
        time = SetDelay(50);
821
        time = SetDelay(50);
758
        while(!CheckDelay(time));
822
        while(!CheckDelay(time));
759
        // averaging
823
        // averaging
760
        #define AVERAGE 20
824
        #define AVERAGE 20
761
        for(i = 0; i<AVERAGE; i++)
825
        for(i = 0; i<AVERAGE; i++)
762
        {
826
        {
763
                NCMAG_GetMagVector();
827
                NCMAG_GetMagVector();
764
                time = SetDelay(20);
828
                time = SetDelay(20);
765
        while(!CheckDelay(time));
829
        while(!CheckDelay(time));
766
                XMax += MagRawVector.X;
830
                XMax += MagRawVector.X;
767
                YMax += MagRawVector.Y;
831
                YMax += MagRawVector.Y;
768
                ZMax += MagRawVector.Z;
832
                ZMax += MagRawVector.Z;
769
        }
833
        }
770
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
834
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
771
        // activate positive bias field
835
        // activate positive bias field
772
        NCMAG_SetMagConfig();
836
        NCMAG_SetMagConfig();
773
    // wait for stable readings
837
    // wait for stable readings
774
        time = SetDelay(50);
838
        time = SetDelay(50);
775
        while(!CheckDelay(time));
839
        while(!CheckDelay(time));
776
        // averaging
840
        // averaging
777
        for(i = 0; i < AVERAGE; i++)
841
        for(i = 0; i < AVERAGE; i++)
778
        {
842
        {
779
                NCMAG_GetMagVector();
843
                NCMAG_GetMagVector();
780
                time = SetDelay(20);
844
                time = SetDelay(20);
781
        while(!CheckDelay(time));
845
        while(!CheckDelay(time));
782
                XMin += MagRawVector.X;
846
                XMin += MagRawVector.X;
783
                YMin += MagRawVector.Y;
847
                YMin += MagRawVector.Y;
784
                ZMin += MagRawVector.Z;
848
                ZMin += MagRawVector.Z;
785
        }
849
        }
786
        // setup final configuration
850
        // setup final configuration
787
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
851
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
788
        // activate positive bias field
852
        // activate positive bias field
789
        NCMAG_SetMagConfig();
853
        NCMAG_SetMagConfig();
790
        // check scale for all axes
854
        // check scale for all axes
791
        // prepare scale limits
855
        // prepare scale limits
792
        LIMITS(xscale, scale_min, scale_max);
856
        LIMITS(xscale, scale_min, scale_max);
793
        xscale = (XMax - XMin)/(2*AVERAGE);
857
        xscale = (XMax - XMin)/(2*AVERAGE);
794
        if((xscale > scale_max) || (xscale < scale_min))
858
        if((xscale > scale_max) || (xscale < scale_min))
795
     {
859
    {
796
          retval = 0;
860
                retval = 0;
797
      sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
861
        sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
798
          UART1_PutString(msg);
862
                UART1_PutString(msg);
799
     }
863
    }
800
        LIMITS(yscale, scale_min, scale_max);
864
        LIMITS(yscale, scale_min, scale_max);
801
        yscale = (YMax - YMin)/(2*AVERAGE);
865
        yscale = (YMax - YMin)/(2*AVERAGE);
802
        if((yscale > scale_max) || (yscale < scale_min))
866
        if((yscale > scale_max) || (yscale < scale_min))
803
     {
867
    {
804
          retval = 0;
868
                retval = 0;
805
      sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
869
        sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
806
          UART1_PutString(msg);
870
                UART1_PutString(msg);
807
     }
871
    }
808
        LIMITS(zscale, scale_min, scale_max);
872
        LIMITS(zscale, scale_min, scale_max);
809
        zscale = (ZMax - ZMin)/(2*AVERAGE);
873
        zscale = (ZMax - ZMin)/(2*AVERAGE);
810
        if((zscale > scale_max) || (zscale < scale_min))      
874
        if((zscale > scale_max) || (zscale < scale_min))      
811
         {
875
        {
812
          retval = 0;
876
                retval = 0;
813
      sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
877
        sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
814
          UART1_PutString(msg);
878
                UART1_PutString(msg);
815
     }
879
    }
816
        done = retval;
880
        done = retval;
817
        return(retval);
881
        return(retval);
818
}
882
}
819
 
883
 
820
 
884
 
821
//----------------------------------------------------------------
885
//----------------------------------------------------------------
822
u8 NCMAG_Init(void)
886
u8 NCMAG_Init(void)
823
{
887
{
824
        u8 msg[64];
888
        u8 msg[64];
825
        u8 retval = 0;
889
        u8 retval = 0;
826
        u8 repeat;
890
        u8 repeat;
827
 
891
 
828
        NCMAG_Present = 0;
892
        NCMAG_Present = 0;
829
        NCMAG_MagType = MAG_TYPE_HMC5843;       // assuming having an HMC5843
893
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
830
        // polling for LSM302DLH option
894
        // polling for LSM302DLH/DLM option by ACC address ack
831
        repeat = 0;
895
        repeat = 0;
832
        do
896
        do
833
        {
897
        {
834
                retval = NCMAG_GetAccConfig();
898
                retval = NCMAG_GetAccConfig();
835
                if(retval) break; // break loop on success
899
                if(retval) break; // break loop on success
836
                UART1_PutString(".");
900
                UART1_PutString(".");
837
                repeat++;
901
                repeat++;
838
        }while(repeat < 3);
902
        }while(repeat < 3);
839
        if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH
-
 
840
        // polling of identification
-
 
841
        repeat = 0;
903
        if(retval)
842
        do
-
 
843
        {
904
        {
-
 
905
                // initialize ACC sensor
-
 
906
                NCMAG_Init_ACCSensor();
-
 
907
 
-
 
908
                NCMAG_SensorType = TYPE_LSM303DLH;     
-
 
909
                // polling of sub identification
-
 
910
                repeat = 0;
-
 
911
                do
-
 
912
                {
844
                retval = NCMAG_GetIdentification_Sub();
913
                        retval = NCMAG_GetIdentification_Sub();
845
                if(retval) break; // break loop on success
914
                        if(retval) break; // break loop on success
846
                UART1_PutString(".");
915
                        UART1_PutString(".");
847
                repeat++;
916
                        repeat++;
848
        }while(repeat < 12);
917
                }while(repeat < 12);
-
 
918
                if(retval)
-
 
919
                {
-
 
920
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
-
 
921
                }      
-
 
922
        }
-
 
923
        // get id bytes
849
        retval = 0;
924
        retval = 0;
850
        do
925
        do
851
        {
926
        {
852
                retval = NCMAG_GetIdentification();
927
                retval = NCMAG_GetIdentification();
853
                if(retval) break; // break loop on success
928
                if(retval) break; // break loop on success
854
                UART1_PutString(".");
929
                UART1_PutString(".");
855
                repeat++;
930
                repeat++;
856
        }while(repeat < 12);
931
        }while(repeat < 12);
857
 
932
 
858
        // if we got an answer to id request
933
        // if we got an answer to id request
859
        if(retval)
934
        if(retval)
860
        {
935
        {
861
                u8 n1[] = "\n\r HMC5843";
936
                u8 n1[] = "\n\r HMC5843";
862
                u8 n2[] = "\n\r LSM303DLH";
937
                u8 n2[] = "\n\r LSM303DLH";
863
                u8 n3[] = "\n\r LSM303DLM";
938
                u8 n3[] = "\n\r LSM303DLM";
864
                u8* pn;
939
                u8* pn = n1;
865
               
-
 
866
                pn = n1;
940
               
867
                if(NCMAG_MagType == MAG_TYPE_LSM303DLH)
941
                switch(NCMAG_SensorType)
-
 
942
                {
-
 
943
                        case TYPE_HMC5843:
-
 
944
                                pn = n1;
868
                {
945
                                break;
869
                 if(NCMAG_Identification2.Sub == 0x3c) pn = n3;
946
                        case TYPE_LSM303DLH:
-
 
947
                                pn = n2;
-
 
948
                                break;
-
 
949
                        case TYPE_LSM303DLM:
-
 
950
                                pn = n3;
870
                 else pn = n2;
951
                                break;
871
                }
952
                }
872
 
953
 
873
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
954
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
874
                UART1_PutString(msg);
955
                UART1_PutString(msg);
875
                if (    (NCMAG_Identification.A == MAG_IDA)
956
                if (    (NCMAG_Identification.A == MAG_IDA)
876
                     && (NCMAG_Identification.B == MAG_IDB)
957
                     && (NCMAG_Identification.B == MAG_IDB)
877
                         && (NCMAG_Identification.C == MAG_IDC))
958
                         && (NCMAG_Identification.C == MAG_IDC))
878
                {
959
                {
879
                        NCMAG_Present = 1;
960
                        NCMAG_Present = 1;
880
 
961
 
881
                        if(EEPROM_Init())
962
                        if(EEPROM_Init())
882
                                {
-
 
883
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
-
 
884
                                        if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
-
 
885
                                }
-
 
886
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
-
 
887
                       
-
 
888
                        if(NCMAG_Identification2.Sub == 0x00)
-
 
889
                        {
963
                        {
-
 
964
                                NCMAG_IsCalibrated = NCMag_CalibrationRead();
-
 
965
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
-
 
966
                        }
-
 
967
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
-
 
968
                        // perform self test
890
                         if(!NCMAG_SelfTest())
969
                        if(!NCMAG_SelfTest())
891
                         {
970
                        {
892
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
971
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
893
                                LED_RED_ON;
972
                                LED_RED_ON;
894
                                NCMAG_IsCalibrated = 0;
973
                                NCMAG_IsCalibrated = 0;
-
 
974
                        }
895
                         }      else UART1_PutString("\r\n Selftest ok");
975
                        else UART1_PutString("\r\n Selftest ok");
896
                        }
976
 
-
 
977
                        // initialize magnetic sensor configuration
897
                        else InitNC_MagnetSensor();
978
                        InitNC_MagnetSensor();
898
                }
979
                }
899
                else
980
                else
900
                {
981
                {
901
                        UART1_PutString("\n\r Not compatible!");
982
                        UART1_PutString("\n\r Not compatible!");
902
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
983
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
903
                        LED_RED_ON;
984
                        LED_RED_ON;
904
                }
985
                }
905
        }
986
        }
906
        else // nothing found
987
        else // nothing found
907
        {
988
        {
908
                NCMAG_MagType = MAG_TYPE_NONE;
989
                NCMAG_SensorType = TYPE_NONE;
909
                UART1_PutString("not found!");  
990
                UART1_PutString("not found!");  
910
        }
991
        }
911
        return(NCMAG_Present);
992
        return(NCMAG_Present);
912
}
993
}
913
 
994
 
914
 
995