Subversion Repositories NaviCtrl

Rev

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

Rev 252 Rev 253
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 <string.h>
57
#include <string.h>
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "hmc5843.h"
59
#include "ncmag.h"
60
#include "i2c.h"
60
#include "i2c.h"
61
#include "timer1.h"
61
#include "timer1.h"
62
#include "led.h"
62
#include "led.h"
63
#include "spi_slave.h"
63
#include "spi_slave.h"
64
#include "uart1.h"
64
#include "uart1.h"
65
#include "compass.h"
-
 
66
 
65
 
-
 
66
u8 NCMAG_Present = 0;
-
 
67
 
-
 
68
#define MAG_TYPE_NONE           0
67
#define HMC5843_SLAVE_ADDRESS           0x3C  // i2C slave address
69
#define MAG_TYPE_HMC5843        1
-
 
70
#define MAG_TYPE_LSM303DLH      2
-
 
71
u8 NCMAG_MagType = MAG_TYPE_NONE;
-
 
72
 
-
 
73
// i2c MAG interface
-
 
74
#define MAG_SLAVE_ADDRESS       0x3C    // i2C slave address mag. sensor registers
-
 
75
 
-
 
76
// register mapping
-
 
77
#define REG_MAG_CRA                     0x00
-
 
78
#define REG_MAG_CRB                     0x01
-
 
79
#define REG_MAG_MODE            0x02
-
 
80
#define REG_MAG_DATAX_MSB       0x03
-
 
81
#define REG_MAG_DATAX_LSB       0x04
-
 
82
#define REG_MAG_DATAY_MSB       0x05
-
 
83
#define REG_MAG_DATAY_LSB       0x06
-
 
84
#define REG_MAG_DATAZ_MSB       0x07
-
 
85
#define REG_MAG_DATAZ_LSB       0x08
-
 
86
#define REG_MAG_STATUS          0x09
-
 
87
#define REG_MAG_IDA                     0x0A
-
 
88
#define REG_MAG_IDB                     0x0B
-
 
89
#define REG_MAG_IDC                     0x0C
-
 
90
 
-
 
91
// bit mask for configuration mode
-
 
92
#define CRA_MODE_MASK           0x03
-
 
93
#define CRA_MODE_NORMAL         0x00    //default
-
 
94
#define CRA_MODE_POSBIAS        0x01
68
 
-
 
69
u8 HMC5843_Present = 0;
-
 
70
 
-
 
71
#define REGISTER_CRA            0
95
#define CRA_MODE_NEGBIAS        0x02
72
#define REGISTER_CRB            1
96
#define CRA_MODE_SELFTEST       0x03
73
#define REGISTER_MODE           2
97
 
74
#define REGISTER_DATAX_MSB      3
98
// bit mask for measurement mode
75
#define REGISTER_DATAX_LSB      4
99
#define MODE_MASK                       0xFF
76
#define REGISTER_DATAY_MSB      5
100
#define MODE_CONTINUOUS         0x00
-
 
101
#define MODE_SINGLE                     0x01    // default
77
#define REGISTER_DATAY_LSB      6
102
#define MODE_IDLE                       0x02
78
#define REGISTER_DATAZ_MSB      7
103
#define MODE_SLEEP                      0x03
-
 
104
 
79
#define REGISTER_DATAZ_LSB      8
105
// bit mask for rate
80
#define REGISTER_STATUS         9
106
#define CRA_RATE_MASK           0x1C
81
#define REGISTER_IDA            10
107
 
82
#define REGISTER_IDB            11
108
// bit mask for gain
83
#define REGISTER_IDC            12
109
#define CRB_GAIN_MASK           0xE0
84
 
110
 
85
 
111
// ids
-
 
112
#define MAG_IDA         0x48
86
#define HMC5843_IDA 0x48
113
#define MAG_IDB         0x34
87
#define HMC5843_IDB 0x34
-
 
88
#define HMC5843_IDC     0x33
114
#define MAG_IDC         0x33
89
 
115
 
90
// bit mask for rate
116
// the special HMC5843 interface
91
#define HMC5843_CRA_RATE_MASK           0x1C
117
// bit mask for rate
92
#define HMC5843_CRA_RATE_0_5HZ          0x00
118
#define HMC5843_CRA_RATE_0_5HZ          0x00
93
#define HMC5843_CRA_RATE_1HZ            0x04
119
#define HMC5843_CRA_RATE_1HZ            0x04
94
#define HMC5843_CRA_RATE_2HZ            0x08
120
#define HMC5843_CRA_RATE_2HZ            0x08
95
#define HMC5843_CRA_RATE_5HZ            0x0C
121
#define HMC5843_CRA_RATE_5HZ            0x0C
96
#define HMC5843_CRA_RATE_10HZ           0x10    //default
122
#define HMC5843_CRA_RATE_10HZ           0x10    //default
97
#define HMC5843_CRA_RATE_20HZ           0x14
123
#define HMC5843_CRA_RATE_20HZ           0x14
98
#define HMC5843_CRA_RATE_50HZ           0x18
124
#define HMC5843_CRA_RATE_50HZ           0x18
99
 
-
 
100
// bit mask for mode
-
 
101
#define HMC5843_CRA_MODE_MASK           0x03
-
 
102
#define HMC5843_CRA_MODE_NORMAL         0x00    //default
-
 
103
#define HMC5843_CRA_MODE_POSBIAS        0x01
-
 
104
#define HMC5843_CRA_MODE_NEGBIAS        0x02
-
 
105
#define HMC5843_CRA_MODE_SELFTEST       0x03    
-
 
106
 
-
 
107
// bit mask for gain
125
// bit mask for gain
108
#define HMC5843_CRB_GAIN_MASK           0xE0
-
 
109
#define HMC5843_CRB_GAIN_07GA           0x00
126
#define HMC5843_CRB_GAIN_07GA           0x00
110
#define HMC5843_CRB_GAIN_10GA           0x20    //default
127
#define HMC5843_CRB_GAIN_10GA           0x20    //default
111
#define HMC5843_CRB_GAIN_15GA           0x40
128
#define HMC5843_CRB_GAIN_15GA           0x40
112
#define HMC5843_CRB_GAIN_20GA           0x60
129
#define HMC5843_CRB_GAIN_20GA           0x60
113
#define HMC5843_CRB_GAIN_32GA           0x80
130
#define HMC5843_CRB_GAIN_32GA           0x80
114
#define HMC5843_CRB_GAIN_38GA           0xA0
131
#define HMC5843_CRB_GAIN_38GA           0xA0
115
#define HMC5843_CRB_GAIN_45GA           0xC0
132
#define HMC5843_CRB_GAIN_45GA           0xC0
116
#define HMC5843_CRB_GAIN_65GA           0xE0
133
#define HMC5843_CRB_GAIN_65GA           0xE0
-
 
134
// self test value
-
 
135
#define HMC5843_TEST_XSCALE             715
-
 
136
#define HMC5843_TEST_YSCALE             715
-
 
137
#define HMC5843_TEST_ZSCALE             715
-
 
138
 
-
 
139
 
-
 
140
// the special LSM302DLH interface
-
 
141
// bit mask for rate
-
 
142
#define LSM303DLH_CRA_RATE_0_75HZ       0x00
-
 
143
#define LSM303DLH_CRA_RATE_1_5HZ        0x04
-
 
144
#define LSM303DLH_CRA_RATE_3_0HZ        0x08
-
 
145
#define LSM303DLH_CRA_RATE_7_5HZ        0x0C
-
 
146
#define LSM303DLH_CRA_RATE_15HZ         0x10    //default
-
 
147
#define LSM303DLH_CRA_RATE_30HZ         0x14
-
 
148
#define LSM303DLH_CRA_RATE_75HZ         0x18
-
 
149
// bit mask for gain
-
 
150
#define LSM303DLH_CRB_GAIN_XXGA         0x00
-
 
151
#define LSM303DLH_CRB_GAIN_13GA         0x20    //default
-
 
152
#define LSM303DLH_CRB_GAIN_19GA         0x40
-
 
153
#define LSM303DLH_CRB_GAIN_25GA         0x60
-
 
154
#define LSM303DLH_CRB_GAIN_40GA         0x80
-
 
155
#define LSM303DLH_CRB_GAIN_47GA         0xA0
-
 
156
#define LSM303DLH_CRB_GAIN_56GA         0xC0
-
 
157
#define LSM303DLH_CRB_GAIN_81GA         0xE0
-
 
158
// self test value
-
 
159
#define LSM303DLH_TEST_XSCALE   655
-
 
160
#define LSM303DLH_TEST_YSCALE   655
-
 
161
#define LSM303DLH_TEST_ZSCALE   630
-
 
162
 
-
 
163
// the i2c ACC interface
-
 
164
#define ACC_SLAVE_ADDRESS               0x30    // i2c slave for acc. sensor registers
-
 
165
// register mapping
-
 
166
#define REG_ACC_CTRL1                   0x20
-
 
167
#define REG_ACC_CTRL2                   0x21
-
 
168
#define REG_ACC_CTRL3                   0x22
-
 
169
#define REG_ACC_CTRL4                   0x23
-
 
170
#define REG_ACC_CTRL5                   0x24
-
 
171
#define REG_ACC_HP_FILTER_RESET 0x25
-
 
172
#define REG_ACC_REFERENCE               0x26
-
 
173
#define REG_ACC_STATUS                  0x27
-
 
174
#define REG_ACC_X_LSB                   0x28
-
 
175
#define REG_ACC_X_MSB                   0x29
-
 
176
#define REG_ACC_Y_LSB                   0x2A
-
 
177
#define REG_ACC_Y_MSB                   0x2B
-
 
178
#define REG_ACC_Z_LSB                   0x2C
-
 
179
#define REG_ACC_Z_MSB                   0x2D
-
 
180
 
117
 
-
 
118
// bit mask for measurement mode
-
 
119
#define HMC5843_MODE_MASK                       0xFF
-
 
120
#define HMC5843_MODE_CONTINUOUS         0x00
-
 
121
#define HMC5843_MODE_SINGLE                     0x01    // default
-
 
122
#define HMC5843_MODE_IDLE                       0x02
-
 
123
#define HMC5843_MODE_SLEEP                      0x03    
181
 
124
 
182
 
125
typedef struct
183
typedef struct
126
{
184
{
127
  u8 A;
185
        u8 A;
128
  u8 B;
186
        u8 B;
129
  u8 C;
187
        u8 C;
130
} __attribute__((packed)) HMC5843_Identification_t;
188
} __attribute__((packed)) Identification_t;
131
 
189
 
-
 
190
volatile Identification_t NCMAG_Identification;
-
 
191
 
-
 
192
typedef struct
-
 
193
{
-
 
194
        u8 cra;
-
 
195
        u8 crb;
-
 
196
        u8 mode;
-
 
197
} __attribute__((packed)) MagConfig_t;
-
 
198
 
-
 
199
volatile MagConfig_t MagConfig;
-
 
200
 
-
 
201
typedef struct
-
 
202
{
-
 
203
        u8 ctrl_1;
-
 
204
        u8 ctrl_2;
-
 
205
        u8 ctrl_3;
-
 
206
        u8 ctrl_4;
-
 
207
        u8 ctrl_5;
-
 
208
} __attribute__((packed)) AccConfig_t;
-
 
209
 
-
 
210
volatile AccConfig_t AccConfig;
132
volatile HMC5843_Identification_t HMC5843_Identification;
211
 
133
 
212
volatile s16vec_t AccVector;
134
 
213
 
135
 
214
 
136
// ---------- call back handlers -----------------------------------------
215
// ---------- call back handlers -----------------------------------------
137
 
216
 
138
// rx data handler for id info request
217
// rx data handler for id info request
139
void HMC5843_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
218
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
140
{       // if number of byte are matching
219
{       // if number of byte are matching
141
        if(RxBufferSize == sizeof(HMC5843_Identification) )
220
        if(RxBufferSize == sizeof(NCMAG_Identification) )
142
        {
221
        {
143
                memcpy((u8 *)&HMC5843_Identification, pRxBuffer, sizeof(HMC5843_Identification));
222
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
144
        }                
223
        }
145
}
224
}
146
 
225
 
147
// rx data handler for mag vector request
226
// rx data handler for mag vector request
148
void HMC5843_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
227
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
149
{       // if number of byte are matching
228
{       // if number of byte are matching
150
        if(RxBufferSize == sizeof(MagVector) )
229
        if(RxBufferSize == sizeof(MagVector) )
151
        {       // byte order from big to little endian
230
        {       // byte order from big to little endian
152
                MagVector.X = pRxBuffer[0]<<8;
231
                MagVector.X = pRxBuffer[0]<<8;
153
                MagVector.X+= pRxBuffer[1];
232
                MagVector.X+= pRxBuffer[1];
154
                MagVector.Y = pRxBuffer[2]<<8;
233
                MagVector.Y = pRxBuffer[2]<<8;
155
                MagVector.Y+= pRxBuffer[3];
234
                MagVector.Y+= pRxBuffer[3];
156
                MagVector.Z = pRxBuffer[4]<<8;
235
                MagVector.Z = pRxBuffer[4]<<8;
157
                MagVector.Z+= pRxBuffer[5];
236
                MagVector.Z+= pRxBuffer[5];
158
        }
237
        }
159
        // tbd. calculate heading from mag vector and attitude
238
        // tbd. calculate heading from mag vector and attitude
160
        CompassHeading = 50; // tbd.              
239
        CompassHeading = 50; // tbd.
-
 
240
}
-
 
241
 
-
 
242
// rx data handler for acc vector request
-
 
243
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
-
 
244
{       // if number of byte are matching
-
 
245
        if(RxBufferSize == sizeof(AccVector) )
-
 
246
        {
-
 
247
                memcpy((u8*)&AccVector, pRxBuffer,sizeof(AccVector));
-
 
248
        }
-
 
249
}
-
 
250
 
-
 
251
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
-
 
252
{       // if number of byte are matching
-
 
253
        if(RxBufferSize == sizeof(MagConfig) )
-
 
254
        {
-
 
255
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
-
 
256
        }
161
}
257
}
-
 
258
 
-
 
259
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
-
 
260
{       // if number of byte are matching
-
 
261
        if(RxBufferSize == sizeof(AccConfig) )
-
 
262
        {
-
 
263
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
-
 
264
        }
162
 
265
}
-
 
266
 
-
 
267
// ----------------------------------------------------------------------------------------
-
 
268
u8 NCMAG_SetMagConfig(void)
-
 
269
{
-
 
270
        u8 retval = 0;
-
 
271
        // try to catch the i2c buffer within 100 ms timeout
-
 
272
        if(I2C_LockBuffer(100))
-
 
273
        {
-
 
274
                u8 TxBytes = 0;
-
 
275
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;    
-
 
276
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
-
 
277
                TxBytes += sizeof(MagConfig);
-
 
278
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
-
 
279
                {
-
 
280
                        if(I2C_WaitForEndOfTransmission(100))
-
 
281
                        {
-
 
282
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
-
 
283
                        }
-
 
284
                }
-
 
285
        }
-
 
286
        return(retval);        
163
 
287
}
164
// ----------------------------------------------------------------------------------------
288
 
165
 
289
// ----------------------------------------------------------------------------------------
166
u8 HMC5843_GetIdentification(void)
290
u8 NCMAG_GetMagConfig(void)
167
{
291
{
168
        u8 retval = 0; 
292
        u8 retval = 0;
169
        // try to catch the i2c buffer within 100 ms timeout
293
        // try to catch the i2c buffer within 100 ms timeout
170
        if(I2C_LockBuffer(100))
294
        if(I2C_LockBuffer(100))
171
        {
295
        {
172
                u16 TxBytes = 0;
296
                u8 TxBytes = 0;
173
                HMC5843_Identification.A = 0xFF;
-
 
174
                HMC5843_Identification.B = 0xFF;
-
 
175
                HMC5843_Identification.C = 0xFF;
-
 
176
                I2C_Buffer[TxBytes++] = REGISTER_IDA;
297
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;
177
                // initiate transmission
-
 
178
                if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateIdentification, sizeof(HMC5843_Identification)))
298
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
179
                {
299
                {
180
                        if(I2C_WaitForEndOfTransmission(100))
300
                        if(I2C_WaitForEndOfTransmission(100))
181
                        {
301
                        {
182
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
302
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
183
                        }
303
                        }
184
                }
304
                }
185
        }
305
        }
186
        return(retval);
306
        return(retval);        
-
 
307
}
-
 
308
 
-
 
309
// ----------------------------------------------------------------------------------------
-
 
310
u8 NCMAG_SetAccConfig(void)
-
 
311
{
-
 
312
        u8 retval = 0;
-
 
313
        // try to catch the i2c buffer within 100 ms timeout
-
 
314
        if(I2C_LockBuffer(100))
-
 
315
        {
-
 
316
                u8 TxBytes = 0;
-
 
317
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;  
-
 
318
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
-
 
319
                TxBytes += sizeof(AccConfig);
-
 
320
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
-
 
321
                {
-
 
322
                        if(I2C_WaitForEndOfTransmission(100))
-
 
323
                        {
-
 
324
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
-
 
325
                        }
-
 
326
                }
-
 
327
        }
-
 
328
        return(retval);        
187
}
329
}
188
 
330
 
189
// ----------------------------------------------------------------------------------------
331
// ----------------------------------------------------------------------------------------
190
u8 HMC5843_SetConfiguration(u8 cra, u8 crb, u8 mode)
332
u8 NCMAG_GetAccConfig(void)
191
{
333
{
192
        u8 retval = 0;
334
        u8 retval = 0;
193
        // tra to catch the I2c buffer within 100 ms timeout
335
        // try to catch the i2c buffer within 100 ms timeout
194
        if(I2C_LockBuffer(100))
336
        if(I2C_LockBuffer(100))
195
        {
337
        {
196
                u16 TxBytes = 0;       
338
                u8 TxBytes = 0;
197
                I2C_Buffer[TxBytes++] = REGISTER_CRA;
339
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1;
-
 
340
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
-
 
341
                {
-
 
342
                        if(I2C_WaitForEndOfTransmission(100))
-
 
343
                        {
-
 
344
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
-
 
345
                        }
-
 
346
                }
-
 
347
        }
-
 
348
        return(retval);        
-
 
349
}
-
 
350
 
-
 
351
// ----------------------------------------------------------------------------------------
-
 
352
u8 NCMAG_GetIdentification(void)
-
 
353
{
-
 
354
        u8 retval = 0;
-
 
355
        // try to catch the i2c buffer within 100 ms timeout
-
 
356
        if(I2C_LockBuffer(100))
-
 
357
        {
198
                I2C_Buffer[TxBytes++] = cra;
358
                u16 TxBytes = 0;
-
 
359
                NCMAG_Identification.A = 0xFF;
-
 
360
                NCMAG_Identification.B = 0xFF;
199
                I2C_Buffer[TxBytes++] = crb;
361
                NCMAG_Identification.C = 0xFF;
200
                I2C_Buffer[TxBytes++] = mode;
362
                I2C_Buffer[TxBytes++] = REG_MAG_IDA;
201
                // initiate transmission
363
                // initiate transmission
202
                if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, NULL, 0))
364
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
203
                {
365
                {
204
                        if(I2C_WaitForEndOfTransmission(50))
366
                        if(I2C_WaitForEndOfTransmission(100))
205
                        {
367
                        {
206
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
368
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
207
                        }
369
                        }
208
                }
370
                }
209
        }
371
        }
210
        return(retval);
372
        return(retval);
-
 
373
}
-
 
374
 
-
 
375
// ----------------------------------------------------------------------------------------
-
 
376
void NCMAG_GetMagVector(void)
-
 
377
{
-
 
378
        // try to catch the I2C buffer within 0 ms
-
 
379
        if(I2C_LockBuffer(0))
-
 
380
        {
-
 
381
                u16 TxBytes = 0;
-
 
382
                // set register pointer
-
 
383
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
-
 
384
                // initiate transmission
-
 
385
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
-
 
386
        }
211
}
387
}
212
 
388
 
213
//----------------------------------------------------------------
389
//----------------------------------------------------------------
214
void HMC5843_GetMagVector(void)
390
void NCMAG_GetAccVector(void)
215
{
391
{
216
        // try to catch the I2C buffer within 0 ms
392
        // try to catch the I2C buffer within 0 ms
217
        if(I2C_LockBuffer(0))
393
        if(I2C_LockBuffer(0))
218
        {
394
        {
219
                u16 TxBytes = 0;
395
                u16 TxBytes = 0;
220
                // set register pointer
396
                // set register pointer
221
                I2C_Buffer[TxBytes++] = REGISTER_DATAX_MSB;
397
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB;
222
                // initiate transmission
398
                // initiate transmission
223
                I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateMagVector, sizeof(MagVector));
399
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccVector));
224
        }
400
        }
225
}
401
}
-
 
402
 
226
 
403
// --------------------------------------------------------
227
void HMC5843_UpdateCompass(void)
404
void NCMAG_UpdateCompass(void)
228
{
405
{
229
        static u32 TimerCompassUpdate = 0;
406
        static u32 TimerCompassUpdate = 0;
230
 
407
 
231
        if( (I2C_State == I2C_STATE_OFF) || !HMC5843_Present ) return;
408
        if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) return;
232
       
409
 
233
        if(CheckDelay(TimerCompassUpdate))
410
        if(CheckDelay(TimerCompassUpdate))
234
        {
411
        {
235
                // check for incomming compass calibration request
412
                // check for incomming compass calibration request
236
                // update CalByte from spi input queue
413
                // update CalByte from spi input queue
237
                /*fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte));
414
                /*fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte));
238
                // send new calstate
415
                // send new calstate
239
                if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte)
416
                if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte)
240
                {
417
                {
241
                        do the calibration here
418
                        do the calibration here
242
                }
419
                }
243
                else // request current heading */
420
                else // request current heading */
244
                {
421
                {
245
                        HMC5843_GetMagVector();
422
                        NCMAG_GetMagVector();
246
                }              
423
                }
247
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
424
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
248
        }
425
        }
249
}
426
}
250
 
427
 
251
u8 HMC5843_SelfTest(void)
428
u8 NCMAG_SelfTest(void)
-
 
429
{
252
{
430
        #define LIMITS(value, min, max) {min = (90 * value)/100; max = (110 * value)/100;}
253
        u32 time;
431
        u32 time;
254
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
432
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
-
 
433
        s16 xscale, yscale, zscale, scale_min, scale_max;
255
        s16 scale, scale_min, scale_max;
434
        u8 crb_gain, cra_rate;
-
 
435
        u8 i = 0, retval = 1;
-
 
436
 
-
 
437
        switch(NCMAG_MagType)
-
 
438
        {
-
 
439
                case MAG_TYPE_HMC5843:
-
 
440
                        crb_gain = HMC5843_CRB_GAIN_10GA;
-
 
441
                        cra_rate = HMC5843_CRA_RATE_50HZ;
-
 
442
                        xscale = HMC5843_TEST_XSCALE;
-
 
443
                        yscale = HMC5843_TEST_YSCALE;
-
 
444
                        zscale = HMC5843_TEST_ZSCALE;
-
 
445
                        break;
-
 
446
 
-
 
447
                case MAG_TYPE_LSM303DLH:
-
 
448
                        crb_gain = LSM303DLH_CRB_GAIN_13GA;
-
 
449
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
-
 
450
                        xscale = LSM303DLH_TEST_XSCALE;
-
 
451
                        yscale = LSM303DLH_TEST_YSCALE;
-
 
452
                        zscale = LSM303DLH_TEST_ZSCALE;
-
 
453
                        break;
-
 
454
 
-
 
455
                default:
-
 
456
                return(0);
-
 
457
        }
-
 
458
 
-
 
459
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
256
        u8 i = 0;
460
        MagConfig.crb = crb_gain;
257
 
461
        MagConfig.mode = MODE_CONTINUOUS;
258
        // activate positive bias field  of 0.55 gauss
462
        // activate positive bias field
259
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_POSBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
463
        NCMAG_SetMagConfig();
260
        // wait for stable readings
464
        // wait for stable readings
261
        time = SetDelay(50);
465
        time = SetDelay(50);
262
        while(!CheckDelay(time));
466
        while(!CheckDelay(time));
263
        // averaging
467
        // averaging
-
 
468
        #define AVERAGE 20
264
        for(i = 0; i<20; i++)
469
        for(i = 0; i<AVERAGE; i++)
265
        {
470
        {
266
                HMC5843_GetMagVector();
471
                NCMAG_GetMagVector();
267
                time = SetDelay(20);
472
                time = SetDelay(20);
268
        while(!CheckDelay(time));
473
        while(!CheckDelay(time));
269
                XMax += MagVector.X;
474
                XMax += MagVector.X;
270
                YMax += MagVector.Y;
475
                YMax += MagVector.Y;
271
                ZMax += MagVector.Z;
476
                ZMax += MagVector.Z;
272
        }
477
        }
-
 
478
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
273
        // activate negative bias field of 0.55 gauss
479
        // activate positive bias field
274
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NEGBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
480
        NCMAG_SetMagConfig();
275
    // wait for stable readings
481
    // wait for stable readings
276
        time = SetDelay(50);
482
        time = SetDelay(50);
277
        while(!CheckDelay(time));
483
        while(!CheckDelay(time));
278
        // averaging
484
        // averaging
279
        for(i = 0; i < 20; i++)
485
        for(i = 0; i < AVERAGE; i++)
280
        {
486
        {
281
                HMC5843_GetMagVector();
487
                NCMAG_GetMagVector();
282
                time = SetDelay(20);
488
                time = SetDelay(20);
283
        while(!CheckDelay(time));
489
        while(!CheckDelay(time));
284
                XMin += MagVector.X;
490
                XMin += MagVector.X;
285
                YMin += MagVector.Y;
491
                YMin += MagVector.Y;
286
                ZMin += MagVector.Z;     
492
                ZMin += MagVector.Z;
287
        }
493
        }
288
        // setup final configuration
494
        // setup final configuration
289
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
495
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
-
 
496
        // activate positive bias field
-
 
497
        NCMAG_SetMagConfig();
290
        // prepare scale limits
498
        // prepare scale limits
291
        scale = 715;  // 1300 counts/Gauss * 0.55 Gauss = 715 counts
-
 
292
        scale_min = (scale *  90)/100;
499
        LIMITS(xscale, scale_min, scale_max);
293
        scale_max = (scale * 110)/100;
-
 
294
        // check scale for all axes
500
        // check scale for all axes
295
        scale = (XMax - XMin)/40;
501
        xscale = (XMax - XMin)/(2*AVERAGE);
296
        if((scale > scale_max) || (scale < scale_min)) return(0);
502
        if((xscale > scale_max) || (xscale < scale_min)) retval = 0;
-
 
503
        LIMITS(yscale, scale_min, scale_max);
297
        scale = (YMax - YMin)/40;
504
        yscale = (YMax - YMin)/(2*AVERAGE);
298
        if((scale > scale_max) || (scale < scale_min)) return(0);
505
        if((yscale > scale_max) || (yscale < scale_min)) retval = 0;
-
 
506
        LIMITS(zscale, scale_min, scale_max);
299
        scale = (ZMax - ZMin)/40;
507
        zscale = (ZMax - ZMin)/(2*AVERAGE);
300
        if((scale > scale_max) || (scale < scale_min)) return(0);
508
        if((zscale > scale_max) || (zscale < scale_min)) retval = 0;
301
        return(1);
509
        return(retval);
302
}
510
}
303
 
511
 
304
 
512
 
305
//----------------------------------------------------------------
513
//----------------------------------------------------------------
306
u8 HMC5843_Init(void)
514
u8 NCMAG_Init(void)
307
{
515
{
308
        u8 msg[64];
516
        u8 msg[64];
309
        u8 retval = 0;
517
        u8 retval = 0;
310
        u8 repeat;
518
        u8 repeat;
311
 
519
 
-
 
520
        NCMAG_Present = 0;
-
 
521
        NCMAG_MagType = MAG_TYPE_HMC5843;       // assuming having an HMC5843
-
 
522
        // polling for LSM302DLH option
-
 
523
        repeat = 0;
312
        HMC5843_Present = 0;
524
        do
-
 
525
        {
-
 
526
                retval = NCMAG_GetAccConfig();
-
 
527
                if(retval) break; // break loop on success
-
 
528
                UART1_PutString(".");
-
 
529
                repeat++;
-
 
530
        }while(repeat < 3);
313
 
531
        if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH
314
        // polling of identification
532
        // polling of identification
315
        repeat = 0;
533
        repeat = 0;
316
        do
534
        do
317
        {
535
        {
318
                retval = HMC5843_GetIdentification();
536
                retval = NCMAG_GetIdentification();
319
                if(retval) break; // break loop on success
537
                if(retval) break; // break loop on success
320
                UART1_PutString(".");
538
                UART1_PutString(".");
321
                repeat++;
539
                repeat++;
322
        }while(repeat < 12);
540
        }while(repeat < 12);
323
        // if we got it
541
        // if we got an answer to id request
324
        if(retval)
542
        if(retval)
325
        {
543
        {
-
 
544
                u8 n1[] = "HMC5843";
-
 
545
                u8 n2[] = "LSM303DLH";
-
 
546
                u8* pn;
-
 
547
                if(NCMAG_MagType == MAG_TYPE_LSM303DLH) pn = n2;
-
 
548
                else pn = n1;
326
                sprintf(msg, " HMC5843 ID %d/%d/%d", HMC5843_Identification.A, HMC5843_Identification.B, HMC5843_Identification.C);
549
                sprintf(msg, " %s ID%d/%d/%d", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C);
327
                UART1_PutString(msg);
550
                UART1_PutString(msg);
328
 
-
 
329
                if (    (HMC5843_Identification.A == HMC5843_IDA)
551
                if (    (NCMAG_Identification.A == MAG_IDA)
330
                     && (HMC5843_Identification.B == HMC5843_IDB)
552
                     && (NCMAG_Identification.B == MAG_IDB)
331
                         && (HMC5843_Identification.C == HMC5843_IDC))
553
                         && (NCMAG_Identification.C == MAG_IDC))
332
                {
554
                {
333
                        //HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
-
 
334
                        if(!HMC5843_SelfTest())
555
                        if(!NCMAG_SelfTest())
335
                        {
556
                        {
336
                                UART1_PutString("\n\r HMC5843 selftest failed!");
557
                                UART1_PutString(" Selftest failed!");
337
                                LED_RED_ON;
558
                                LED_RED_ON;
338
                        }
559
                        }
339
                        else HMC5843_Present = 1;
560
                        else NCMAG_Present = 1;
340
                }
561
                }
341
                else
562
                else
342
                {
563
                {
343
                        UART1_PutString("\n\r HMC5843 not compatible!");
564
                        UART1_PutString("\n\r not compatible!");
344
                        LED_RED_ON;
565
                        LED_RED_ON;
345
                }
566
                }
346
        }
567
        }
347
        // else no answer
568
        else // nothing found
-
 
569
        {
-
 
570
                NCMAG_MagType = MAG_TYPE_NONE;
-
 
571
                UART1_PutString("not found!");  
-
 
572
        }
348
        return(HMC5843_Present);
573
        return(NCMAG_Present);
349
}
574
}
350
 
575
 
351
 
576