Subversion Repositories NaviCtrl

Rev

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

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