Subversion Repositories NaviCtrl

Rev

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

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