Subversion Repositories NaviCtrl

Rev

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

Rev 487 Rev 488
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, Zmin2 = 0, Zmax2 = 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 = (X + MagRawVector.X)/2;
347
        Y = (4*Y + MagRawVector.Y + 3)/5;
347
        Y = (Y + MagRawVector.Y)/2;
348
        Z = (4*Z + MagRawVector.Z + 3)/5;
348
        Z = (Z + MagRawVector.Z)/2;
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
                        CompassValueErrorCount = 0;
363
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
364
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
364
                        {
365
                        {
365
                         UART1_PutString("\r\nStarting compass calibration");
366
                         UART1_PutString("\r\nStarting compass calibration");
366
                         if(I2C_CompassPort == I2C_EXTERN_0)
367
                         if(I2C_CompassPort == I2C_EXTERN_0)
367
                         {
368
                         {
368
                          if(!ExtCompassOrientation) ExtCompassOrientation = GetExtCompassOrientation();
369
                          if(!ExtCompassOrientation) ExtCompassOrientation = GetExtCompassOrientation();
369
                          UART1_PutString(" - External sensor ");
370
                          UART1_PutString(" - External sensor ");
370
                          sprintf(msg, "with orientation: %d ",ExtCompassOrientation);
371
                          sprintf(msg, "with orientation: %d ",ExtCompassOrientation);
371
                          UART1_PutString(msg);
372
                          UART1_PutString(msg);
372
                         }
373
                         }
373
                         else UART1_PutString(" - Internal sensor ");
374
                         else UART1_PutString(" - Internal sensor ");
374
                        }
375
                        }
375
                        break;
376
                        break;
376
                case 2: // 2nd step of calibration
377
                case 2: // 2nd step of calibration
377
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
378
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
378
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
379
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
379
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
380
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
380
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
381
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
381
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
382
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
382
                        if(Z < Zmin)        { Zmin = Z; } // silent
383
                        if(Z < Zmin)        { Zmin = Z; } // silent
383
                        else if(Z > Zmax)   { Zmax = Z; }
384
                        else if(Z > Zmax)   { Zmax = Z; }
384
 
385
 
385
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
386
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
386
                        break;
387
                        break;
387
 
388
 
388
                case 3: // 3rd step of calibration
389
                case 3: // 3rd step of calibration
389
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
390
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
390
                        speak = 1;
391
                        speak = 1;
391
                        break;
392
                        break;
392
 
393
 
393
                case 4:
394
                case 4:
394
                        // find Min and Max of the Z-Sensor
395
                        // find Min and Max of the Z-Sensor
395
                        if(Z < Zmin2)     { Zmin2 = Z; BeepTime = 80;}
396
                        if(Z < Zmin2)     { Zmin2 = Z; BeepTime = 80;}
396
                        else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;}
397
                        else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;}
397
                       
398
                       
398
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
399
                        if(X < Xmin)            { Xmin = X; BeepTime = 20;}
399
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
400
                        else if(X > Xmax)       { Xmax = X; BeepTime = 20;}
400
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
401
                        if(Y < Ymin)            { Ymin = Y; BeepTime = 60;}
401
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
402
                        else if(Y > Ymax)       { Ymax = Y; BeepTime = 60;}
402
 
403
 
403
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
404
                        if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
404
                        break;
405
                        break;
405
               
406
               
406
                case 5:
407
                case 5:
407
                        // Save values
408
                        // Save values
408
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
409
                        if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
409
                        {
410
                        {
410
                                switch(NCMAG_SensorType)
411
                                switch(NCMAG_SensorType)
411
                                {
412
                                {
412
                                        case TYPE_HMC5843:
413
                                        case TYPE_HMC5843:
413
                                                UART1_PutString("\r\nFinished: HMC5843 calibration\n\r");
414
                                                UART1_PutString("\r\nFinished: HMC5843 calibration\n\r");
414
                                                MinCalibration = HMC5843_CALIBRATION_RANGE;
415
                                                MinCalibration = HMC5843_CALIBRATION_RANGE;
415
                                                break;
416
                                                break;
416
 
417
 
417
                                        case TYPE_LSM303DLH:
418
                                        case TYPE_LSM303DLH:
418
                                        case TYPE_LSM303DLM:
419
                                        case TYPE_LSM303DLM:
419
                                                UART1_PutString("\r\nFinished: LSM303 calibration\n\r");
420
                                                UART1_PutString("\r\nFinished: LSM303 calibration\n\r");
420
                                                MinCalibration = LSM303_CALIBRATION_RANGE;
421
                                                MinCalibration = LSM303_CALIBRATION_RANGE;
421
                                        break;
422
                                        break;
422
                                }
423
                                }
423
                                if(EarthMagneticStrengthTheoretic)
424
                                if(EarthMagneticStrengthTheoretic)
424
                                 {
425
                                 {
425
                                  MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50;
426
                                  MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50;
426
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
427
                                  sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic);
427
                                  UART1_PutString(msg);
428
                                  UART1_PutString(msg);
428
                                 }
429
                                 }
429
                            else UART1_PutString("without GPS\n\r");
430
                            else UART1_PutString("without GPS\n\r");
430
 
431
 
431
                                if(Zmin2 < Zmin)          { Zmin = Zmin2; }
432
                                if(Zmin2 < Zmin)          { Zmin = Zmin2; }
432
                                else if(Zmax2 > Zmax) { Zmax = Zmax2; }
433
                                else if(Zmax2 > Zmax) { Zmax = Zmax2; }
433
 
434
 
434
                                Calibration.MagX.Range = Xmax - Xmin;
435
                                Calibration.MagX.Range = Xmax - Xmin;
435
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
436
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
436
                                Calibration.MagY.Range = Ymax - Ymin;
437
                                Calibration.MagY.Range = Ymax - Ymin;
437
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
438
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
438
                                Calibration.MagZ.Range = Zmax - Zmin;
439
                                Calibration.MagZ.Range = Zmax - Zmin;
439
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
440
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
-
 
441
                                if(CompassValueErrorCount)
-
 
442
                                 {
-
 
443
                                        SpeakHoTT = SPEAK_ERR_CALIBARTION;
-
 
444
                                        UART1_PutString("\r\nCalibration FAILED - Compass sensor error !!!!\r\n ");
-
 
445
 
-
 
446
                                }
-
 
447
                                else
440
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
448
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
441
                                {
449
                                {
442
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort);
450
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort);
443
                                        BeepTime = 2500;
451
                                        BeepTime = 2500;
444
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
452
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
445
SpeakHoTT = SPEAK_MIKROKOPTER;
453
SpeakHoTT = SPEAK_MIKROKOPTER;
446
                                }
454
                                }
447
                                else
455
                                else
448
                                {
456
                                {
449
SpeakHoTT = SPEAK_ERR_CALIBARTION;
457
SpeakHoTT = SPEAK_ERR_CALIBARTION;
450
                                        UART1_PutString("\r\nCalibration FAILED - Values too low: ");
458
                                        UART1_PutString("\r\nCalibration FAILED - Values too low: ");
451
                                    if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! ");
459
                                    if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! ");
452
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
460
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
453
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
461
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
454
                                        UART1_PutString("\r\n");
462
                                        UART1_PutString("\r\n");
455
 
463
 
456
                                        // restore old calibration data from eeprom
464
                                        // restore old calibration data from eeprom
457
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
465
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
458
                                }
466
                                }
459
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
467
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
460
                                        UART1_PutString(msg);
468
                                        UART1_PutString(msg);
461
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
469
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
462
                                        UART1_PutString(msg);
470
                                        UART1_PutString(msg);
463
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
471
                                        sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin);
464
                                        UART1_PutString(msg);
472
                                        UART1_PutString(msg);
465
                                        sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration);
473
                                        sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration);
466
                                        UART1_PutString(msg);
474
                                        UART1_PutString(msg);
467
                        }
475
                        }
468
                        break;
476
                        break;
469
                       
477
                       
470
                default:
478
                default:
471
                        break; 
479
                        break; 
472
        }
480
        }
473
        OldCalState = Compass_CalState;
481
        OldCalState = Compass_CalState;
474
}
482
}
475
 
483
 
476
// ---------- call back handlers -----------------------------------------
484
// ---------- call back handlers -----------------------------------------
477
 
485
 
478
// rx data handler for id info request
486
// rx data handler for id info request
479
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
487
void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
480
{       // if number of bytes are matching
488
{       // if number of bytes are matching
481
        if(RxBufferSize == sizeof(NCMAG_Identification) )
489
        if(RxBufferSize == sizeof(NCMAG_Identification) )
482
        {
490
        {
483
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
491
                memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification));
484
        }
492
        }
485
}
493
}
486
 
494
 
487
void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize)
495
void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize)
488
{       // if number of bytes are matching
496
{       // if number of bytes are matching
489
        if(RxBufferSize == sizeof(NCMAG_Identification2))
497
        if(RxBufferSize == sizeof(NCMAG_Identification2))
490
        {
498
        {
491
                memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2));
499
                memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2));
492
        }
500
        }
493
}
501
}
494
 
502
 
495
// rx data handler for magnetic sensor raw data
503
// rx data handler for magnetic sensor raw data
496
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
504
void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
497
{       // if number of bytes are matching
505
{       // if number of bytes are matching
498
        if(RxBufferSize == sizeof(MagRawVector) )
506
        if(RxBufferSize == sizeof(MagRawVector) )
499
        {       // byte order from big to little endian
507
        {       // byte order from big to little endian
500
                s16 raw, X = 0, Y = 0, Z = 0;
508
                s16 raw, X = 0, Y = 0, Z = 0;
501
                raw = pRxBuffer[0]<<8;
509
                raw = pRxBuffer[0]<<8;
502
                raw+= pRxBuffer[1];
510
                raw+= pRxBuffer[1];
503
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw;
511
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw;
504
                else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
512
                else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
505
 
513
 
506
                raw = pRxBuffer[2]<<8;
514
                raw = pRxBuffer[2]<<8;
507
                raw+= pRxBuffer[3];
515
                raw+= pRxBuffer[3];
508
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
516
            if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
509
                {
517
                {
510
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  Z = raw; // here Z and Y are exchanged
518
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  Z = raw; // here Z and Y are exchanged
511
                        else                                                                    Y = raw;
519
                        else                                                                    Y = raw;
512
                }
520
                }
513
                else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
521
                else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
514
 
522
 
515
                raw = pRxBuffer[4]<<8;
523
                raw = pRxBuffer[4]<<8;
516
                raw+= pRxBuffer[5];
524
                raw+= pRxBuffer[5];
517
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
525
                if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE)
518
                {
526
                {
519
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  Y = raw; // here Z and Y are exchanged
527
                        if(NCMAG_SensorType == TYPE_LSM303DLM)  Y = raw; // here Z and Y are exchanged
520
                        else                                                                    Z = raw;
528
                        else                                                                    Z = raw;
521
                }
529
                }
522
                else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
530
                else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
523
 
531
 
524
          switch(ExtCompassOrientation)
532
          switch(ExtCompassOrientation)
525
                {
533
                {
526
                 case 0:
534
                 case 0:
527
                 case 1:
535
                 case 1:
528
                 default:
536
                 default:
529
                                MagRawVector.X = X;
537
                                MagRawVector.X = X;
530
                                MagRawVector.Y = Y;
538
                                MagRawVector.Y = Y;
531
                                MagRawVector.Z = Z;
539
                                MagRawVector.Z = Z;
532
                                break;
540
                                break;
533
                 case 2:
541
                 case 2:
534
                                MagRawVector.X = -X;
542
                                MagRawVector.X = -X;
535
                                MagRawVector.Y = Y;
543
                                MagRawVector.Y = Y;
536
                                MagRawVector.Z = -Z;
544
                                MagRawVector.Z = -Z;
537
                                break;
545
                                break;
538
                 case 3:
546
                 case 3:
539
                                MagRawVector.X = -Z;
547
                                MagRawVector.X = -Z;
540
                                MagRawVector.Y = Y;
548
                                MagRawVector.Y = Y;
541
                                MagRawVector.Z = X;
549
                                MagRawVector.Z = X;
542
                                break;
550
                                break;
543
                 case 4:
551
                 case 4:
544
                                MagRawVector.X = Z;
552
                                MagRawVector.X = Z;
545
                                MagRawVector.Y = Y;
553
                                MagRawVector.Y = Y;
546
                                MagRawVector.Z = -X;
554
                                MagRawVector.Z = -X;
547
                                break;
555
                                break;
548
                 case 5:
556
                 case 5:
549
                                MagRawVector.X = X;
557
                                MagRawVector.X = X;
550
                                MagRawVector.Y = -Z;
558
                                MagRawVector.Y = -Z;
551
                                MagRawVector.Z = Y;
559
                                MagRawVector.Z = Y;
552
                                break;
560
                                break;
553
                 case 6:
561
                 case 6:
554
                                MagRawVector.X = -X;
562
                                MagRawVector.X = -X;
555
                                MagRawVector.Y = -Z;
563
                                MagRawVector.Y = -Z;
556
                                MagRawVector.Z = -Y;
564
                                MagRawVector.Z = -Y;
557
                                break;
565
                                break;
558
                }
566
                }
559
        }
567
        }
560
        if(Compass_CalState || !NCMAG_IsCalibrated)
568
        if(Compass_CalState || !NCMAG_IsCalibrated)
561
        {       // mark out data invalid
569
        {       // mark out data invalid
562
                MagVector.X = MagRawVector.X;
570
                MagVector.X = MagRawVector.X;
563
                MagVector.Y = MagRawVector.Y;
571
                MagVector.Y = MagRawVector.Y;
564
                MagVector.Z = MagRawVector.Z;
572
                MagVector.Z = MagRawVector.Z;
565
                Compass_Heading = -1;
573
                Compass_Heading = -1;
566
        }
574
        }
567
        else
575
        else
568
        {
576
        {
569
                // update MagVector from MagRaw Vector by Scaling
577
                // update MagVector from MagRaw Vector by Scaling
570
                MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range);
578
                MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range);
571
                MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range);
579
                MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range);
572
                MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range);
580
                MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range);
573
                Compass_CalcHeading();
581
                Compass_CalcHeading();
574
        }
582
        }
575
}
583
}
576
// rx data handler  for acceleration raw data
584
// rx data handler  for acceleration raw data
577
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
585
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
578
{       // if number of byte are matching
586
{       // if number of byte are matching
579
        if(RxBufferSize == sizeof(AccRawVector) )
587
        if(RxBufferSize == sizeof(AccRawVector) )
580
        {
588
        {
581
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
589
                memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
582
        }
590
        }
583
}
591
}
584
 
592
 
585
u8 GetExtCompassOrientation(void)
593
u8 GetExtCompassOrientation(void)
586
{
594
{
587
        if(I2C_CompassPort != I2C_EXTERN_0) return(0);
595
        if(I2C_CompassPort != I2C_EXTERN_0) return(0);
588
 
596
 
589
        if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300))
597
        if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300))
590
         {
598
         {
591
//        UART1_PutString("\r\nTilted"); 
599
//        UART1_PutString("\r\nTilted"); 
592
          return(0);
600
          return(0);
593
         }
601
         }
594
        if(AccRawVector.Z >  3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn
602
        if(AccRawVector.Z >  3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn
595
        else
603
        else
596
        if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn
604
        if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn
597
        else
605
        else
598
        if(AccRawVector.X >  3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn
606
        if(AccRawVector.X >  3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn
599
        else
607
        else
600
        if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn
608
        if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn
601
        else
609
        else
602
        if(AccRawVector.Y >  3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn
610
        if(AccRawVector.Y >  3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn
603
        else
611
        else
604
        if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten  - 'front' nach vorn
612
        if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten  - 'front' nach vorn
605
        return(0);
613
        return(0);
606
}
614
}
607
 
615
 
608
// rx data handler for reading magnetic sensor configuration
616
// rx data handler for reading magnetic sensor configuration
609
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
617
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
610
{       // if number of byte are matching
618
{       // if number of byte are matching
611
        if(RxBufferSize == sizeof(MagConfig) )
619
        if(RxBufferSize == sizeof(MagConfig) )
612
        {
620
        {
613
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
621
                memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
614
        }
622
        }
615
}
623
}
616
// rx data handler for reading acceleration sensor configuration
624
// rx data handler for reading acceleration sensor configuration
617
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
625
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
618
{       // if number of byte are matching
626
{       // if number of byte are matching
619
        if(RxBufferSize == sizeof(AccConfig) )
627
        if(RxBufferSize == sizeof(AccConfig) )
620
        {
628
        {
621
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
629
                memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
622
        }
630
        }
623
}
631
}
624
//----------------------------------------------------------------------
632
//----------------------------------------------------------------------
625
 
633
 
626
 
634
 
627
// ---------------------------------------------------------------------
635
// ---------------------------------------------------------------------
628
u8 NCMAG_SetMagConfig(void)
636
u8 NCMAG_SetMagConfig(void)
629
{
637
{
630
        u8 retval = 0;
638
        u8 retval = 0;
631
        // try to catch the i2c buffer within 100 ms timeout
639
        // try to catch the i2c buffer within 100 ms timeout
632
        if(I2C_LockBufferFunc(100))
640
        if(I2C_LockBufferFunc(100))
633
        {
641
        {
634
                u8 TxBytes = 0;
642
                u8 TxBytes = 0;
635
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;        
643
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;        
636
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
644
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
637
                TxBytes += sizeof(MagConfig);
645
                TxBytes += sizeof(MagConfig);
638
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
646
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
639
                {
647
                {
640
                        if(I2C_WaitForEndOfTransmissionFunc(100))
648
                        if(I2C_WaitForEndOfTransmissionFunc(100))
641
                        {
649
                        {
642
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
650
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
643
                        }
651
                        }
644
                }
652
                }
645
        }
653
        }
646
        return(retval);        
654
        return(retval);        
647
}
655
}
648
 
656
 
649
// ----------------------------------------------------------------------------------------
657
// ----------------------------------------------------------------------------------------
650
u8 NCMAG_GetMagConfig(void)
658
u8 NCMAG_GetMagConfig(void)
651
{
659
{
652
        u8 retval = 0;
660
        u8 retval = 0;
653
        // try to catch the i2c buffer within 100 ms timeout
661
        // try to catch the i2c buffer within 100 ms timeout
654
        if(I2C_LockBufferFunc(100))
662
        if(I2C_LockBufferFunc(100))
655
        {
663
        {
656
                u8 TxBytes = 0;
664
                u8 TxBytes = 0;
657
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
665
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
658
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
666
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
659
                {
667
                {
660
                        if(I2C_WaitForEndOfTransmissionFunc(100))
668
                        if(I2C_WaitForEndOfTransmissionFunc(100))
661
                        {
669
                        {
662
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
670
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
663
                        }
671
                        }
664
                }
672
                }
665
        }
673
        }
666
        return(retval);        
674
        return(retval);        
667
}
675
}
668
 
676
 
669
// ----------------------------------------------------------------------------------------
677
// ----------------------------------------------------------------------------------------
670
u8 NCMAG_SetAccConfig(void)
678
u8 NCMAG_SetAccConfig(void)
671
{
679
{
672
        u8 retval = 0;
680
        u8 retval = 0;
673
        // try to catch the i2c buffer within 100 ms timeout
681
        // try to catch the i2c buffer within 100 ms timeout
674
        if(I2C_LockBufferFunc(50))
682
        if(I2C_LockBufferFunc(50))
675
        {
683
        {
676
                u8 TxBytes = 0;
684
                u8 TxBytes = 0;
677
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;    
685
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;    
678
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
686
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
679
                TxBytes += sizeof(AccConfig);
687
                TxBytes += sizeof(AccConfig);
680
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
688
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
681
                {
689
                {
682
                        if(I2C_WaitForEndOfTransmissionFunc(50))
690
                        if(I2C_WaitForEndOfTransmissionFunc(50))
683
                        {
691
                        {
684
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
692
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
685
                        }
693
                        }
686
                }
694
                }
687
        }
695
        }
688
        return(retval);        
696
        return(retval);        
689
}
697
}
690
 
698
 
691
// ----------------------------------------------------------------------------------------
699
// ----------------------------------------------------------------------------------------
692
u8 NCMAG_GetAccConfig(void)
700
u8 NCMAG_GetAccConfig(void)
693
{
701
{
694
        u8 retval = 0;
702
        u8 retval = 0;
695
        // try to catch the i2c buffer within 100 ms timeout
703
        // try to catch the i2c buffer within 100 ms timeout
696
        if(I2C_LockBufferFunc(100))
704
        if(I2C_LockBufferFunc(100))
697
        {
705
        {
698
                u8 TxBytes = 0;
706
                u8 TxBytes = 0;
699
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
707
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
700
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
708
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
701
                {
709
                {
702
                        if(I2C_WaitForEndOfTransmissionFunc(100))
710
                        if(I2C_WaitForEndOfTransmissionFunc(100))
703
                        {
711
                        {
704
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
712
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
705
                        }
713
                        }
706
                }
714
                }
707
        }
715
        }
708
        return(retval);        
716
        return(retval);        
709
}
717
}
710
 
718
 
711
// ----------------------------------------------------------------------------------------
719
// ----------------------------------------------------------------------------------------
712
u8 NCMAG_GetIdentification(void)
720
u8 NCMAG_GetIdentification(void)
713
{
721
{
714
        u8 retval = 0;
722
        u8 retval = 0;
715
        // try to catch the i2c buffer within 100 ms timeout
723
        // try to catch the i2c buffer within 100 ms timeout
716
        if(I2C_LockBufferFunc(100))
724
        if(I2C_LockBufferFunc(100))
717
        {
725
        {
718
                u16 TxBytes = 0;
726
                u16 TxBytes = 0;
719
                NCMAG_Identification.A = 0xFF;
727
                NCMAG_Identification.A = 0xFF;
720
                NCMAG_Identification.B = 0xFF;
728
                NCMAG_Identification.B = 0xFF;
721
                NCMAG_Identification.C = 0xFF;
729
                NCMAG_Identification.C = 0xFF;
722
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDA;
730
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDA;
723
                // initiate transmission
731
                // initiate transmission
724
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
732
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
725
                {
733
                {
726
                        if(I2C_WaitForEndOfTransmissionFunc(100))
734
                        if(I2C_WaitForEndOfTransmissionFunc(100))
727
                        {
735
                        {
728
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
736
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
729
                        }
737
                        }
730
                }
738
                }
731
        }
739
        }
732
        return(retval);
740
        return(retval);
733
}
741
}
734
 
742
 
735
u8 NCMAG_GetIdentification_Sub(void)
743
u8 NCMAG_GetIdentification_Sub(void)
736
{
744
{
737
        u8 retval = 0;
745
        u8 retval = 0;
738
        // try to catch the i2c buffer within 100 ms timeout
746
        // try to catch the i2c buffer within 100 ms timeout
739
        if(I2C_LockBufferFunc(100))
747
        if(I2C_LockBufferFunc(100))
740
        {
748
        {
741
                u16 TxBytes = 0;
749
                u16 TxBytes = 0;
742
                NCMAG_Identification2.Sub = 0xFF;
750
                NCMAG_Identification2.Sub = 0xFF;
743
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDF;
751
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDF;
744
                // initiate transmission
752
                // initiate transmission
745
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
753
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
746
                {
754
                {
747
                        if(I2C_WaitForEndOfTransmissionFunc(100))
755
                        if(I2C_WaitForEndOfTransmissionFunc(100))
748
                        {
756
                        {
749
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
757
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
750
                        }
758
                        }
751
                }
759
                }
752
        }
760
        }
753
        return(retval);
761
        return(retval);
754
}
762
}
755
 
763
 
756
 
764
 
757
// ----------------------------------------------------------------------------------------
765
// ----------------------------------------------------------------------------------------
758
void NCMAG_GetMagVector(void)
766
void NCMAG_GetMagVector(void)
759
{
767
{
760
        // try to catch the I2C buffer within 0 ms
768
        // try to catch the I2C buffer within 0 ms
761
        if(I2C_LockBufferFunc(5))
769
        if(I2C_LockBufferFunc(5))
762
        {
770
        {
763
                u16 TxBytes = 0;
771
                u16 TxBytes = 0;
764
                // set register pointer
772
                // set register pointer
765
                I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB;
773
                I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB;
766
                // initiate transmission
774
                // initiate transmission
767
                I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
775
                I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
768
        }
776
        }
769
}
777
}
770
 
778
 
771
//----------------------------------------------------------------
779
//----------------------------------------------------------------
772
void NCMAG_GetAccVector(u8 timeout)
780
void NCMAG_GetAccVector(u8 timeout)
773
{
781
{
774
        // try to catch the I2C buffer within 0 ms
782
        // try to catch the I2C buffer within 0 ms
775
        if(I2C_LockBufferFunc(timeout))
783
        if(I2C_LockBufferFunc(timeout))
776
        {
784
        {
777
                u16 TxBytes = 0;
785
                u16 TxBytes = 0;
778
                // set register pointer
786
                // set register pointer
779
                I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
787
                I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
780
                // initiate transmission
788
                // initiate transmission
781
                I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
789
                I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
782
//DebugOut.Analog[16] = AccRawVector.X;
790
//DebugOut.Analog[16] = AccRawVector.X;
783
//DebugOut.Analog[17] = AccRawVector.Y;
791
//DebugOut.Analog[17] = AccRawVector.Y;
784
//DebugOut.Analog[18] = AccRawVector.Z;
792
//DebugOut.Analog[18] = AccRawVector.Z;
785
        }
793
        }
786
}
794
}
787
 
795
 
788
//----------------------------------------------------------------
796
//----------------------------------------------------------------
789
u8 InitNC_MagnetSensor(void)
797
u8 InitNC_MagnetSensor(void)
790
{
798
{
791
        u8 crb_gain, cra_rate;
799
        u8 crb_gain, cra_rate;
792
 
800
 
793
        switch(NCMAG_SensorType)
801
        switch(NCMAG_SensorType)
794
        {
802
        {
795
                case TYPE_HMC5843:
803
                case TYPE_HMC5843:
796
                        crb_gain = HMC5843_CRB_GAIN_15GA;
804
                        crb_gain = HMC5843_CRB_GAIN_15GA;
797
                        cra_rate = HMC5843_CRA_RATE_50HZ;
805
                        cra_rate = HMC5843_CRA_RATE_50HZ;
798
                        break;
806
                        break;
799
 
807
 
800
                case TYPE_LSM303DLH:
808
                case TYPE_LSM303DLH:
801
                case TYPE_LSM303DLM:
809
                case TYPE_LSM303DLM:
802
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
810
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
803
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
811
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
804
                        break;
812
                        break;
805
 
813
 
806
                default:
814
                default:
807
                return(0);
815
                return(0);
808
        }
816
        }
809
 
817
 
810
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
818
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
811
        MagConfig.crb = crb_gain;
819
        MagConfig.crb = crb_gain;
812
        MagConfig.mode = MODE_CONTINUOUS;
820
        MagConfig.mode = MODE_CONTINUOUS;
813
        return(NCMAG_SetMagConfig());
821
        return(NCMAG_SetMagConfig());
814
}
822
}
815
 
823
 
816
 
824
 
817
//----------------------------------------------------------------
825
//----------------------------------------------------------------
818
u8 NCMAG_Init_ACCSensor(void)
826
u8 NCMAG_Init_ACCSensor(void)
819
{
827
{
820
        AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_50HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN;
828
        AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_50HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN;
821
        AccConfig.ctrl_2 = 0;
829
        AccConfig.ctrl_2 = 0;
822
        AccConfig.ctrl_3 = 0x00;
830
        AccConfig.ctrl_3 = 0x00;
823
        AccConfig.ctrl_4 = ACC_CTRL4_BDU | ACC_CTRL4_FS_8G;
831
        AccConfig.ctrl_4 = ACC_CTRL4_BDU | ACC_CTRL4_FS_8G;
824
        AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF;
832
        AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF;
825
        return(NCMAG_SetAccConfig());
833
        return(NCMAG_SetAccConfig());
826
}
834
}
827
// --------------------------------------------------------
835
// --------------------------------------------------------
828
void NCMAG_Update(u8 init)
836
void NCMAG_Update(u8 init)
829
{
837
{
830
        static u32 TimerUpdate = 0;
838
        static u32 TimerUpdate = 0;
831
        static s8 send_config = 0;
839
        static s8 send_config = 0;
832
        u32 delay = 20;
840
        u32 delay = 20;
833
        if(init) TimerUpdate = SetDelay(10);
841
        if(init) TimerUpdate = SetDelay(10);
834
 
842
 
835
        // todo State Handling for both busses  !!
843
        // todo State Handling for both busses  !!
836
        if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF)/* || !NCMAG_Present*/ )
844
        if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF)/* || !NCMAG_Present*/ )
837
        {
845
        {
838
                Compass_Heading = -1;
846
                Compass_Heading = -1;
839
                DebugOut.Analog[14]++; // count I2C error
847
                DebugOut.Analog[14]++; // count I2C error
840
                TimerUpdate = SetDelay(10);
848
                TimerUpdate = SetDelay(10);
841
                return;
849
                return;
842
        }
850
        }
843
        if(CheckDelay(TimerUpdate))// && I2C0_State == I2C_STATE_IDLE && I2C1_State == I2C_STATE_IDLE)
851
        if(CheckDelay(TimerUpdate))// && I2C0_State == I2C_STATE_IDLE && I2C1_State == I2C_STATE_IDLE)
844
        {
852
        {
845
                if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
853
                if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
846
        if(++send_config == 25)   // 500ms
854
        if(++send_config == 25)   // 500ms
847
                {
855
                {
848
                        send_config = -25;    // next try after 1 second
856
                        send_config = -25;    // next try after 1 second
849
                InitNC_MagnetSensor();
857
                InitNC_MagnetSensor();
850
                        TimerUpdate = SetDelay(20);    // back into the old time-slot
858
                        TimerUpdate = SetDelay(20);    // back into the old time-slot
851
                }
859
                }
852
                else
860
                else
853
                {
861
                {
854
                        static u8 s = 0;
862
                        static u8 s = 0;
855
                        // check for new calibration state
863
                        // check for new calibration state
856
                        Compass_UpdateCalState();
864
                        Compass_UpdateCalState();
857
                        if(Compass_CalState) NCMAG_Calibrate();
865
                        if(Compass_CalState) NCMAG_Calibrate();
858
                       
866
                       
859
                        // in case of LSM303 type
867
                        // in case of LSM303 type
860
                        switch(NCMAG_SensorType)
868
                        switch(NCMAG_SensorType)
861
                        {
869
                        {
862
                                case TYPE_HMC5843:                             
870
                                case TYPE_HMC5843:                             
863
                                        NCMAG_GetMagVector();
871
                                        NCMAG_GetMagVector();
864
                                        delay = 20;
872
                                        delay = 20;
865
                                        break;
873
                                        break;
866
                                case TYPE_LSM303DLH:
874
                                case TYPE_LSM303DLH:
867
                                case TYPE_LSM303DLM:
875
                                case TYPE_LSM303DLM:
868
                                        delay = 20;
876
                                        delay = 20;
869
//delay = 2;
877
//delay = 2;
870
                                        if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector();
878
                                        if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector();
871
                                        else
879
                                        else
872
                                         {
880
                                         {
873
                                          if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor();
881
                                          if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor();
874
                                          NCMAG_GetAccVector(5);
882
                                          NCMAG_GetAccVector(5);
875
                                          delay = 10;
883
                                          delay = 10;
876
                                          s = 40;  // about 0,8 sec
884
                                          s = 40;  // about 0,8 sec
877
                                         };
885
                                         };
878
                                        if(!s) delay = 10; // ACC-Reading in the next step after 10ms
886
                                        if(!s) delay = 10; // ACC-Reading in the next step after 10ms
879
//if(!s) delay = 2; // ACC-Reading in the next step after 10ms
887
//if(!s) delay = 2; // ACC-Reading in the next step after 10ms
880
                                        break;                           
888
                                        break;                           
881
                        }
889
                        }
882
                        if(send_config == 24) TimerUpdate = SetDelay(15);    // next event is the re-configuration
890
                        if(send_config == 24) TimerUpdate = SetDelay(15);    // next event is the re-configuration
883
                        else TimerUpdate = SetDelay(delay);    // every 20 ms are 50 Hz
891
                        else TimerUpdate = SetDelay(delay);    // every 20 ms are 50 Hz
884
                }
892
                }
885
        }
893
        }
886
}
894
}
887
 
895
 
888
 
896
 
889
// --------------------------------------------------------
897
// --------------------------------------------------------
890
u8 NCMAG_SelfTest(void)
898
u8 NCMAG_SelfTest(void)
891
{
899
{
892
        u8 msg[64];
900
        u8 msg[64];
893
        static u8 done = 0;
901
        static u8 done = 0;
894
 
902
 
895
        if(done) return(1);        // just make it once
903
        if(done) return(1);        // just make it once
896
       
904
       
897
        #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;}
905
        #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;}
898
        u32 time;
906
        u32 time;
899
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
907
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
900
        s16 xscale, yscale, zscale, scale_min, scale_max;
908
        s16 xscale, yscale, zscale, scale_min, scale_max;
901
        u8 crb_gain, cra_rate;
909
        u8 crb_gain, cra_rate;
902
        u8 i = 0, retval = 1;
910
        u8 i = 0, retval = 1;
903
 
911
 
904
        switch(NCMAG_SensorType)
912
        switch(NCMAG_SensorType)
905
        {
913
        {
906
                case TYPE_HMC5843:
914
                case TYPE_HMC5843:
907
                        crb_gain = HMC5843_CRB_GAIN_15GA;
915
                        crb_gain = HMC5843_CRB_GAIN_15GA;
908
                        cra_rate = HMC5843_CRA_RATE_50HZ;
916
                        cra_rate = HMC5843_CRA_RATE_50HZ;
909
                        xscale = HMC5843_TEST_XSCALE;
917
                        xscale = HMC5843_TEST_XSCALE;
910
                        yscale = HMC5843_TEST_YSCALE;
918
                        yscale = HMC5843_TEST_YSCALE;
911
                        zscale = HMC5843_TEST_ZSCALE;
919
                        zscale = HMC5843_TEST_ZSCALE;
912
                        break;
920
                        break;
913
 
921
 
914
                case TYPE_LSM303DLH:
922
                case TYPE_LSM303DLH:
915
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
923
                        crb_gain = LSM303DLH_CRB_GAIN_19GA;
916
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
924
                        cra_rate = LSM303DLH_CRA_RATE_75HZ;
917
                        xscale = LSM303DLH_TEST_XSCALE;
925
                        xscale = LSM303DLH_TEST_XSCALE;
918
                        yscale = LSM303DLH_TEST_YSCALE;
926
                        yscale = LSM303DLH_TEST_YSCALE;
919
                        zscale = LSM303DLH_TEST_ZSCALE;
927
                        zscale = LSM303DLH_TEST_ZSCALE;
920
                        break;
928
                        break;
921
 
929
 
922
                case TYPE_LSM303DLM:
930
                case TYPE_LSM303DLM:
923
                        // does not support self test feature 
931
                        // does not support self test feature 
924
                        done = retval;
932
                        done = retval;
925
                        return(retval);
933
                        return(retval);
926
                        break;
934
                        break;
927
 
935
 
928
                default:
936
                default:
929
                        return(0);
937
                        return(0);
930
        }
938
        }
931
 
939
 
932
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
940
        MagConfig.cra = cra_rate|CRA_MODE_POSBIAS;
933
        MagConfig.crb = crb_gain;
941
        MagConfig.crb = crb_gain;
934
        MagConfig.mode = MODE_CONTINUOUS;
942
        MagConfig.mode = MODE_CONTINUOUS;
935
        // activate positive bias field
943
        // activate positive bias field
936
        NCMAG_SetMagConfig();
944
        NCMAG_SetMagConfig();
937
        // wait for stable readings
945
        // wait for stable readings
938
        time = SetDelay(50);
946
        time = SetDelay(50);
939
        while(!CheckDelay(time));
947
        while(!CheckDelay(time));
940
        // averaging
948
        // averaging
941
        #define AVERAGE 20
949
        #define AVERAGE 20
942
        for(i = 0; i<AVERAGE; i++)
950
        for(i = 0; i<AVERAGE; i++)
943
        {
951
        {
944
                NCMAG_GetMagVector();
952
                NCMAG_GetMagVector();
945
                time = SetDelay(20);
953
                time = SetDelay(20);
946
        while(!CheckDelay(time));
954
        while(!CheckDelay(time));
947
                XMax += MagRawVector.X;
955
                XMax += MagRawVector.X;
948
                YMax += MagRawVector.Y;
956
                YMax += MagRawVector.Y;
949
                ZMax += MagRawVector.Z;
957
                ZMax += MagRawVector.Z;
950
        }
958
        }
951
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
959
        MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS;
952
        // activate positive bias field
960
        // activate positive bias field
953
        NCMAG_SetMagConfig();
961
        NCMAG_SetMagConfig();
954
    // wait for stable readings
962
    // wait for stable readings
955
        time = SetDelay(50);
963
        time = SetDelay(50);
956
        while(!CheckDelay(time));
964
        while(!CheckDelay(time));
957
        // averaging
965
        // averaging
958
        for(i = 0; i < AVERAGE; i++)
966
        for(i = 0; i < AVERAGE; i++)
959
        {
967
        {
960
                NCMAG_GetMagVector();
968
                NCMAG_GetMagVector();
961
                time = SetDelay(20);
969
                time = SetDelay(20);
962
        while(!CheckDelay(time));
970
        while(!CheckDelay(time));
963
                XMin += MagRawVector.X;
971
                XMin += MagRawVector.X;
964
                YMin += MagRawVector.Y;
972
                YMin += MagRawVector.Y;
965
                ZMin += MagRawVector.Z;
973
                ZMin += MagRawVector.Z;
966
        }
974
        }
967
        // setup final configuration
975
        // setup final configuration
968
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
976
        MagConfig.cra = cra_rate|CRA_MODE_NORMAL;
969
        // activate positive bias field
977
        // activate positive bias field
970
        NCMAG_SetMagConfig();
978
        NCMAG_SetMagConfig();
971
        // check scale for all axes
979
        // check scale for all axes
972
        // prepare scale limits
980
        // prepare scale limits
973
        LIMITS(xscale, scale_min, scale_max);
981
        LIMITS(xscale, scale_min, scale_max);
974
        xscale = (XMax - XMin)/(2*AVERAGE);
982
        xscale = (XMax - XMin)/(2*AVERAGE);
975
        if((xscale > scale_max) || (xscale < scale_min))
983
        if((xscale > scale_max) || (xscale < scale_min))
976
    {
984
    {
977
                retval = 0;
985
                retval = 0;
978
        sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
986
        sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max);
979
                UART1_PutString(msg);
987
                UART1_PutString(msg);
980
    }
988
    }
981
        LIMITS(yscale, scale_min, scale_max);
989
        LIMITS(yscale, scale_min, scale_max);
982
        yscale = (YMax - YMin)/(2*AVERAGE);
990
        yscale = (YMax - YMin)/(2*AVERAGE);
983
        if((yscale > scale_max) || (yscale < scale_min))
991
        if((yscale > scale_max) || (yscale < scale_min))
984
    {
992
    {
985
                retval = 0;
993
                retval = 0;
986
        sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
994
        sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max);
987
                UART1_PutString(msg);
995
                UART1_PutString(msg);
988
    }
996
    }
989
        LIMITS(zscale, scale_min, scale_max);
997
        LIMITS(zscale, scale_min, scale_max);
990
        zscale = (ZMax - ZMin)/(2*AVERAGE);
998
        zscale = (ZMax - ZMin)/(2*AVERAGE);
991
        if((zscale > scale_max) || (zscale < scale_min))      
999
        if((zscale > scale_max) || (zscale < scale_min))      
992
        {
1000
        {
993
                retval = 0;
1001
                retval = 0;
994
        sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
1002
        sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max);
995
                UART1_PutString(msg);
1003
                UART1_PutString(msg);
996
    }
1004
    }
997
        done = retval;
1005
        done = retval;
998
        return(retval);
1006
        return(retval);
999
}
1007
}
1000
 
1008
 
1001
 
1009
 
1002
//----------------------------------------------------------------
1010
//----------------------------------------------------------------
1003
void NCMAG_SelectI2CBus(u8 busno)
1011
void NCMAG_SelectI2CBus(u8 busno)
1004
{
1012
{
1005
  if (busno == 0)
1013
  if (busno == 0)
1006
  {
1014
  {
1007
    I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission;
1015
    I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission;
1008
        I2C_LockBufferFunc = &I2C0_LockBuffer;
1016
        I2C_LockBufferFunc = &I2C0_LockBuffer;
1009
        I2C_TransmissionFunc = &I2C0_Transmission;
1017
        I2C_TransmissionFunc = &I2C0_Transmission;
1010
        I2C_BufferPnt = I2C0_Buffer;
1018
        I2C_BufferPnt = I2C0_Buffer;
1011
        I2C_ErrorPnt = &I2C0_Error;
1019
        I2C_ErrorPnt = &I2C0_Error;
1012
  }
1020
  }
1013
   else
1021
   else
1014
  {
1022
  {
1015
    I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission;
1023
    I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission;
1016
        I2C_LockBufferFunc = &I2C1_LockBuffer;
1024
        I2C_LockBufferFunc = &I2C1_LockBuffer;
1017
        I2C_TransmissionFunc = &I2C1_Transmission;
1025
        I2C_TransmissionFunc = &I2C1_Transmission;
1018
        I2C_BufferPnt = I2C1_Buffer;
1026
        I2C_BufferPnt = I2C1_Buffer;
1019
        I2C_ErrorPnt = &I2C1_Error;
1027
        I2C_ErrorPnt = &I2C1_Error;
1020
  }
1028
  }
1021
}
1029
}
1022
 
1030
 
1023
//----------------------------------------------------------------
1031
//----------------------------------------------------------------
1024
u8 NCMAG_Init(void)
1032
u8 NCMAG_Init(void)
1025
{
1033
{
1026
        u8 msg[64];
1034
        u8 msg[64];
1027
        u8 retval = 0;
1035
        u8 retval = 0;
1028
        u8 repeat = 0;
1036
        u8 repeat = 0;
1029
 
1037
 
1030
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1038
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1031
// Search external sensor
1039
// Search external sensor
1032
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1040
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1033
        I2C_CompassPort = I2C_EXTERN_0;
1041
        I2C_CompassPort = I2C_EXTERN_0;
1034
        NCMAG_SelectI2CBus(I2C_CompassPort);
1042
        NCMAG_SelectI2CBus(I2C_CompassPort);
1035
 
1043
 
1036
        // get id bytes
1044
        // get id bytes
1037
        retval = 0;
1045
        retval = 0;
1038
    for(repeat = 0; repeat < 5; repeat++)
1046
    for(repeat = 0; repeat < 5; repeat++)
1039
        {
1047
        {
1040
//              retval = NCMAG_GetIdentification();
1048
//              retval = NCMAG_GetIdentification();
1041
                retval = NCMAG_GetAccConfig();            // only the sensor with ACC is supported
1049
                retval = NCMAG_GetAccConfig();            // only the sensor with ACC is supported
1042
                if(retval) break; // break loop on success
1050
                if(retval) break; // break loop on success
1043
                UART1_PutString("_");
1051
                UART1_PutString("_");
1044
        }
1052
        }
1045
//retval = 1;
1053
//retval = 1;
1046
        if(!retval)
1054
        if(!retval)
1047
        {
1055
        {
1048
         UART1_PutString(" internal sensor ");
1056
         UART1_PutString(" internal sensor ");
1049
         I2C_CompassPort = I2C_INTERN_1;
1057
         I2C_CompassPort = I2C_INTERN_1;
1050
         NCMAG_SelectI2CBus(I2C_CompassPort);
1058
         NCMAG_SelectI2CBus(I2C_CompassPort);
1051
        }
1059
        }
1052
        else
1060
        else
1053
        {
1061
        {
1054
         UART1_PutString(" external sensor ");
1062
         UART1_PutString(" external sensor ");
1055
         NCMAG_Init_ACCSensor();
1063
         NCMAG_Init_ACCSensor();
1056
 
1064
 
1057
         for(repeat = 0; repeat < 100; repeat++)
1065
         for(repeat = 0; repeat < 100; repeat++)
1058
          {
1066
          {
1059
           NCMAG_GetAccVector(10); // only the sensor with ACC is supported
1067
           NCMAG_GetAccVector(10); // only the sensor with ACC is supported
1060
       ExtCompassOrientation = GetExtCompassOrientation();       
1068
       ExtCompassOrientation = GetExtCompassOrientation();       
1061
           if(ExtCompassOrientation && (ExtCompassOrientation == Calibration.Version / 16)) break;
1069
           if(ExtCompassOrientation && (ExtCompassOrientation == Calibration.Version / 16)) break;
1062
           //UART1_Putchar('-');
1070
           //UART1_Putchar('-');
1063
          }
1071
          }
1064
//DebugOut.Analog[19] = repeat;
1072
//DebugOut.Analog[19] = repeat;
1065
 
1073
 
1066
     if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)");
1074
     if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)");
1067
         else
1075
         else
1068
         {
1076
         {
1069
          NCMag_CalibrationRead(I2C_CompassPort);
1077
          NCMag_CalibrationRead(I2C_CompassPort);
1070
          sprintf(msg, "with orientation: %d ",ExtCompassOrientation );
1078
          sprintf(msg, "with orientation: %d ",ExtCompassOrientation );
1071
          UART1_PutString(msg);
1079
          UART1_PutString(msg);
1072
          if(ExtCompassOrientation != Calibration.Version / 16)
1080
          if(ExtCompassOrientation != Calibration.Version / 16)
1073
           {
1081
           {
1074
            sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version / 16);
1082
            sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version / 16);
1075
            UART1_PutString(msg);
1083
            UART1_PutString(msg);
1076
           }
1084
           }
1077
          else UART1_PutString("ok ");
1085
          else UART1_PutString("ok ");
1078
         }
1086
         }
1079
 
1087
 
1080
        }
1088
        }
1081
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1089
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1082
 
1090
 
1083
        NCMAG_Present = 0;
1091
        NCMAG_Present = 0;
1084
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
1092
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
1085
        // polling for LSM302DLH/DLM option by ACC address ack
1093
        // polling for LSM302DLH/DLM option by ACC address ack
1086
    for(repeat = 0; repeat < 3; repeat++)
1094
    for(repeat = 0; repeat < 3; repeat++)
1087
        {
1095
        {
1088
                retval = NCMAG_GetAccConfig();
1096
                retval = NCMAG_GetAccConfig();
1089
                if(retval) break; // break loop on success
1097
                if(retval) break; // break loop on success
1090
        }
1098
        }
1091
        if(retval)
1099
        if(retval)
1092
        {
1100
        {
1093
                // initialize ACC sensor
1101
                // initialize ACC sensor
1094
                NCMAG_Init_ACCSensor();
1102
                NCMAG_Init_ACCSensor();
1095
 
1103
 
1096
                NCMAG_SensorType = TYPE_LSM303DLH;     
1104
                NCMAG_SensorType = TYPE_LSM303DLH;     
1097
                // polling of sub identification
1105
                // polling of sub identification
1098
            for(repeat = 0; repeat < 12; repeat++)
1106
            for(repeat = 0; repeat < 12; repeat++)
1099
                {
1107
                {
1100
                        retval = NCMAG_GetIdentification_Sub();
1108
                        retval = NCMAG_GetIdentification_Sub();
1101
                        if(retval) break; // break loop on success
1109
                        if(retval) break; // break loop on success
1102
                }
1110
                }
1103
                if(retval)
1111
                if(retval)
1104
                {
1112
                {
1105
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
1113
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
1106
                }      
1114
                }      
1107
        }
1115
        }
1108
        // get id bytes
1116
        // get id bytes
1109
    for(repeat = 0; repeat < 3; repeat++)
1117
    for(repeat = 0; repeat < 3; repeat++)
1110
        {
1118
        {
1111
                retval = NCMAG_GetIdentification();
1119
                retval = NCMAG_GetIdentification();
1112
                if(retval) break; // break loop on success
1120
                if(retval) break; // break loop on success
1113
        }
1121
        }
1114
 
1122
 
1115
        // if we got an answer to id request
1123
        // if we got an answer to id request
1116
        if(retval)
1124
        if(retval)
1117
        {
1125
        {
1118
                u8 n1[] = "\n\r HMC5843";
1126
                u8 n1[] = "\n\r HMC5843";
1119
                u8 n2[] = "\n\r LSM303DLH";
1127
                u8 n2[] = "\n\r LSM303DLH";
1120
                u8 n3[] = "\n\r LSM303DLM";
1128
                u8 n3[] = "\n\r LSM303DLM";
1121
                u8* pn = n1;
1129
                u8* pn = n1;
1122
               
1130
               
1123
                switch(NCMAG_SensorType)
1131
                switch(NCMAG_SensorType)
1124
                {
1132
                {
1125
                        case TYPE_HMC5843:
1133
                        case TYPE_HMC5843:
1126
                                pn = n1;
1134
                                pn = n1;
1127
                                break;
1135
                                break;
1128
                        case TYPE_LSM303DLH:
1136
                        case TYPE_LSM303DLH:
1129
                                pn = n2;
1137
                                pn = n2;
1130
                                break;
1138
                                break;
1131
                        case TYPE_LSM303DLM:
1139
                        case TYPE_LSM303DLM:
1132
                                pn = n3;
1140
                                pn = n3;
1133
                                break;
1141
                                break;
1134
                }
1142
                }
1135
 
1143
 
1136
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
1144
                sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub);
1137
                UART1_PutString(msg);
1145
                UART1_PutString(msg);
1138
                if (    (NCMAG_Identification.A == MAG_IDA)
1146
                if (    (NCMAG_Identification.A == MAG_IDA)
1139
                     && (NCMAG_Identification.B == MAG_IDB)
1147
                     && (NCMAG_Identification.B == MAG_IDB)
1140
                         && (NCMAG_Identification.C == MAG_IDC))
1148
                         && (NCMAG_Identification.C == MAG_IDC))
1141
                {
1149
                {
1142
                        NCMAG_Present = 1;
1150
                        NCMAG_Present = 1;
1143
 
1151
 
1144
                        if(EEPROM_Init())
1152
                        if(EEPROM_Init())
1145
                        {
1153
                        {
1146
                                NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
1154
                                NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
1147
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
1155
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
1148
                        }
1156
                        }
1149
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
1157
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
1150
                        // perform self test
1158
                        // perform self test
1151
                        if(!NCMAG_SelfTest())
1159
                        if(!NCMAG_SelfTest())
1152
                        {
1160
                        {
1153
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
1161
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
1154
                                LED_RED_ON;
1162
                                LED_RED_ON;
1155
//                              NCMAG_IsCalibrated = 0;
1163
//                              NCMAG_IsCalibrated = 0;
1156
                        }
1164
                        }
1157
                        else UART1_PutString("\r\n Selftest ok");
1165
                        else UART1_PutString("\r\n Selftest ok");
1158
 
1166
 
1159
                        // initialize magnetic sensor configuration
1167
                        // initialize magnetic sensor configuration
1160
                        InitNC_MagnetSensor();
1168
                        InitNC_MagnetSensor();
1161
                }
1169
                }
1162
                else
1170
                else
1163
                {
1171
                {
1164
                        UART1_PutString("\n\r Not compatible!");
1172
                        UART1_PutString("\n\r Not compatible!");
1165
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
1173
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
1166
                        LED_RED_ON;
1174
                        LED_RED_ON;
1167
                }
1175
                }
1168
        }
1176
        }
1169
        else // nothing found
1177
        else // nothing found
1170
        {
1178
        {
1171
                NCMAG_SensorType = TYPE_NONE;
1179
                NCMAG_SensorType = TYPE_NONE;
1172
                UART1_PutString("not found!");  
1180
                UART1_PutString("not found!");  
1173
        }
1181
        }
1174
        return(NCMAG_Present);
1182
        return(NCMAG_Present);
1175
}
1183
}
1176
 
1184
 
1177
 
1185