Subversion Repositories NaviCtrl

Rev

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

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