Rev 330 | Rev 339 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 330 | Rev 338 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2010 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2010 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <math.h> |
57 | #include <math.h> |
58 | #include <stdio.h> |
58 | #include <stdio.h> |
59 | #include <string.h> |
59 | #include <string.h> |
60 | #include "91x_lib.h" |
60 | #include "91x_lib.h" |
61 | #include "ncmag.h" |
61 | #include "ncmag.h" |
62 | #include "i2c.h" |
62 | #include "i2c.h" |
63 | #include "timer1.h" |
63 | #include "timer1.h" |
64 | #include "led.h" |
64 | #include "led.h" |
65 | #include "uart1.h" |
65 | #include "uart1.h" |
66 | #include "eeprom.h" |
66 | #include "eeprom.h" |
67 | #include "mymath.h" |
67 | #include "mymath.h" |
68 | #include "main.h" |
68 | #include "main.h" |
69 | 69 | ||
70 | u8 NCMAG_Present = 0; |
70 | u8 NCMAG_Present = 0; |
71 | u8 NCMAG_IsCalibrated = 0; |
71 | u8 NCMAG_IsCalibrated = 0; |
72 | 72 | ||
73 | #define MAG_TYPE_NONE 0 |
73 | #define MAG_TYPE_NONE 0 |
74 | #define MAG_TYPE_HMC5843 1 |
74 | #define MAG_TYPE_HMC5843 1 |
75 | #define MAG_TYPE_LSM303DLH 2 |
75 | #define MAG_TYPE_LSM303DLH 2 |
76 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
76 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
77 | 77 | ||
78 | #define CALIBRATION_VERSION 1 |
78 | #define CALIBRATION_VERSION 1 |
- | 79 | #define EEPROM_ADR_MAG_CALIBRATION 50 |
|
79 | #define EEPROM_ADR_MAG_CALIBRATION 50 |
80 | #define MAG_CALIBRATION_COMPATIBEL 0xA1 |
80 | 81 | ||
81 | #define NCMAG_MIN_RAWVALUE -2047 |
82 | #define NCMAG_MIN_RAWVALUE -2047 |
82 | #define NCMAG_MAX_RAWVALUE 2047 |
83 | #define NCMAG_MAX_RAWVALUE 2047 |
83 | #define NCMAG_INVALID_DATA -4096 |
84 | #define NCMAG_INVALID_DATA -4096 |
84 | 85 | ||
85 | typedef struct |
86 | typedef struct |
86 | { |
87 | { |
87 | s16 Range; |
88 | s16 Range; |
88 | s16 Offset; |
89 | s16 Offset; |
89 | } __attribute__((packed)) Scaling_t; |
90 | } __attribute__((packed)) Scaling_t; |
90 | 91 | ||
91 | typedef struct |
92 | typedef struct |
92 | { |
93 | { |
93 | Scaling_t MagX; |
94 | Scaling_t MagX; |
94 | Scaling_t MagY; |
95 | Scaling_t MagY; |
95 | Scaling_t MagZ; |
96 | Scaling_t MagZ; |
96 | u8 Version; |
97 | u8 Version; |
97 | u8 crc; |
98 | u8 crc; |
98 | } __attribute__((packed)) Calibration_t; |
99 | } __attribute__((packed)) Calibration_t; |
99 | 100 | ||
100 | Calibration_t Calibration; // calibration data in RAM |
101 | Calibration_t Calibration; // calibration data in RAM |
101 | 102 | ||
102 | // i2c MAG interface |
103 | // i2c MAG interface |
103 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
104 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
104 | 105 | ||
105 | // register mapping |
106 | // register mapping |
106 | #define REG_MAG_CRA 0x00 |
107 | #define REG_MAG_CRA 0x00 |
107 | #define REG_MAG_CRB 0x01 |
108 | #define REG_MAG_CRB 0x01 |
108 | #define REG_MAG_MODE 0x02 |
109 | #define REG_MAG_MODE 0x02 |
109 | #define REG_MAG_DATAX_MSB 0x03 |
110 | #define REG_MAG_DATAX_MSB 0x03 |
110 | #define REG_MAG_DATAX_LSB 0x04 |
111 | #define REG_MAG_DATAX_LSB 0x04 |
111 | #define REG_MAG_DATAY_MSB 0x05 |
112 | #define REG_MAG_DATAY_MSB 0x05 |
112 | #define REG_MAG_DATAY_LSB 0x06 |
113 | #define REG_MAG_DATAY_LSB 0x06 |
113 | #define REG_MAG_DATAZ_MSB 0x07 |
114 | #define REG_MAG_DATAZ_MSB 0x07 |
114 | #define REG_MAG_DATAZ_LSB 0x08 |
115 | #define REG_MAG_DATAZ_LSB 0x08 |
115 | #define REG_MAG_STATUS 0x09 |
116 | #define REG_MAG_STATUS 0x09 |
116 | 117 | ||
117 | #define REG_MAG_IDA 0x0A |
118 | #define REG_MAG_IDA 0x0A |
118 | #define REG_MAG_IDB 0x0B |
119 | #define REG_MAG_IDB 0x0B |
119 | #define REG_MAG_IDC 0x0C |
120 | #define REG_MAG_IDC 0x0C |
120 | #define REG_MAG_IDF 0x0F |
121 | #define REG_MAG_IDF 0x0F |
121 | 122 | ||
122 | // bit mask for configuration mode |
123 | // bit mask for configuration mode |
123 | #define CRA_MODE_MASK 0x03 |
124 | #define CRA_MODE_MASK 0x03 |
124 | #define CRA_MODE_NORMAL 0x00 //default |
125 | #define CRA_MODE_NORMAL 0x00 //default |
125 | #define CRA_MODE_POSBIAS 0x01 |
126 | #define CRA_MODE_POSBIAS 0x01 |
126 | #define CRA_MODE_NEGBIAS 0x02 |
127 | #define CRA_MODE_NEGBIAS 0x02 |
127 | #define CRA_MODE_SELFTEST 0x03 |
128 | #define CRA_MODE_SELFTEST 0x03 |
128 | 129 | ||
129 | // bit mask for measurement mode |
130 | // bit mask for measurement mode |
130 | #define MODE_MASK 0xFF |
131 | #define MODE_MASK 0xFF |
131 | #define MODE_CONTINUOUS 0x00 |
132 | #define MODE_CONTINUOUS 0x00 |
132 | #define MODE_SINGLE 0x01 // default |
133 | #define MODE_SINGLE 0x01 // default |
133 | #define MODE_IDLE 0x02 |
134 | #define MODE_IDLE 0x02 |
134 | #define MODE_SLEEP 0x03 |
135 | #define MODE_SLEEP 0x03 |
135 | 136 | ||
136 | // bit mask for rate |
137 | // bit mask for rate |
137 | #define CRA_RATE_MASK 0x1C |
138 | #define CRA_RATE_MASK 0x1C |
138 | 139 | ||
139 | // bit mask for gain |
140 | // bit mask for gain |
140 | #define CRB_GAIN_MASK 0xE0 |
141 | #define CRB_GAIN_MASK 0xE0 |
141 | 142 | ||
142 | // ids |
143 | // ids |
143 | #define MAG_IDA 0x48 |
144 | #define MAG_IDA 0x48 |
144 | #define MAG_IDB 0x34 |
145 | #define MAG_IDB 0x34 |
145 | #define MAG_IDC 0x33 |
146 | #define MAG_IDC 0x33 |
146 | 147 | ||
147 | // the special HMC5843 interface |
148 | // the special HMC5843 interface |
148 | // bit mask for rate |
149 | // bit mask for rate |
149 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
150 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
150 | #define HMC5843_CRA_RATE_1HZ 0x04 |
151 | #define HMC5843_CRA_RATE_1HZ 0x04 |
151 | #define HMC5843_CRA_RATE_2HZ 0x08 |
152 | #define HMC5843_CRA_RATE_2HZ 0x08 |
152 | #define HMC5843_CRA_RATE_5HZ 0x0C |
153 | #define HMC5843_CRA_RATE_5HZ 0x0C |
153 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
154 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
154 | #define HMC5843_CRA_RATE_20HZ 0x14 |
155 | #define HMC5843_CRA_RATE_20HZ 0x14 |
155 | #define HMC5843_CRA_RATE_50HZ 0x18 |
156 | #define HMC5843_CRA_RATE_50HZ 0x18 |
156 | // bit mask for gain |
157 | // bit mask for gain |
157 | #define HMC5843_CRB_GAIN_07GA 0x00 |
158 | #define HMC5843_CRB_GAIN_07GA 0x00 |
158 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
159 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
159 | #define HMC5843_CRB_GAIN_15GA 0x40 |
160 | #define HMC5843_CRB_GAIN_15GA 0x40 |
160 | #define HMC5843_CRB_GAIN_20GA 0x60 |
161 | #define HMC5843_CRB_GAIN_20GA 0x60 |
161 | #define HMC5843_CRB_GAIN_32GA 0x80 |
162 | #define HMC5843_CRB_GAIN_32GA 0x80 |
162 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
163 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
163 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
164 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
164 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
165 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
165 | // self test value |
166 | // self test value |
166 | #define HMC5843_TEST_XSCALE 715 |
167 | #define HMC5843_TEST_XSCALE 715 |
167 | #define HMC5843_TEST_YSCALE 715 |
168 | #define HMC5843_TEST_YSCALE 715 |
168 | #define HMC5843_TEST_ZSCALE 715 |
169 | #define HMC5843_TEST_ZSCALE 715 |
169 | 170 | ||
170 | 171 | ||
171 | // the special LSM302DLH interface |
172 | // the special LSM302DLH interface |
172 | // bit mask for rate |
173 | // bit mask for rate |
173 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
174 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
174 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
175 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
175 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
176 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
176 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
177 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
177 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
178 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
178 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
179 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
179 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
180 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
- | 181 | ||
180 | // bit mask for gain |
182 | // bit mask for gain |
181 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
183 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
182 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
184 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
183 | #define LSM303DLH_CRB_GAIN_19GA 0x40 |
185 | #define LSM303DLH_CRB_GAIN_19GA 0x40 |
184 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
186 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
185 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
187 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
186 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
188 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
187 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
189 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
188 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
190 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
189 | // self test value |
191 | // self test value |
190 | #define LSM303DLH_TEST_XSCALE 655 |
192 | #define LSM303DLH_TEST_XSCALE 495 |
191 | #define LSM303DLH_TEST_YSCALE 655 |
193 | #define LSM303DLH_TEST_YSCALE 495 |
192 | #define LSM303DLH_TEST_ZSCALE 630 |
194 | #define LSM303DLH_TEST_ZSCALE 470 |
193 | 195 | ||
194 | // the i2c ACC interface |
196 | // the i2c ACC interface |
195 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
197 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
196 | // register mapping |
198 | // register mapping |
197 | #define REG_ACC_CTRL1 0x20 |
199 | #define REG_ACC_CTRL1 0x20 |
198 | #define REG_ACC_CTRL2 0x21 |
200 | #define REG_ACC_CTRL2 0x21 |
199 | #define REG_ACC_CTRL3 0x22 |
201 | #define REG_ACC_CTRL3 0x22 |
200 | #define REG_ACC_CTRL4 0x23 |
202 | #define REG_ACC_CTRL4 0x23 |
201 | #define REG_ACC_CTRL5 0x24 |
203 | #define REG_ACC_CTRL5 0x24 |
202 | #define REG_ACC_HP_FILTER_RESET 0x25 |
204 | #define REG_ACC_HP_FILTER_RESET 0x25 |
203 | #define REG_ACC_REFERENCE 0x26 |
205 | #define REG_ACC_REFERENCE 0x26 |
204 | #define REG_ACC_STATUS 0x27 |
206 | #define REG_ACC_STATUS 0x27 |
205 | #define REG_ACC_X_LSB 0x28 |
207 | #define REG_ACC_X_LSB 0x28 |
206 | #define REG_ACC_X_MSB 0x29 |
208 | #define REG_ACC_X_MSB 0x29 |
207 | #define REG_ACC_Y_LSB 0x2A |
209 | #define REG_ACC_Y_LSB 0x2A |
208 | #define REG_ACC_Y_MSB 0x2B |
210 | #define REG_ACC_Y_MSB 0x2B |
209 | #define REG_ACC_Z_LSB 0x2C |
211 | #define REG_ACC_Z_LSB 0x2C |
210 | #define REG_ACC_Z_MSB 0x2D |
212 | #define REG_ACC_Z_MSB 0x2D |
211 | 213 | ||
212 | 214 | ||
213 | 215 | ||
214 | typedef struct |
216 | typedef struct |
215 | { |
217 | { |
216 | u8 A; |
218 | u8 A; |
217 | u8 B; |
219 | u8 B; |
218 | u8 C; |
220 | u8 C; |
219 | } __attribute__((packed)) Identification_t; |
221 | } __attribute__((packed)) Identification_t; |
220 | volatile Identification_t NCMAG_Identification; |
222 | volatile Identification_t NCMAG_Identification; |
221 | 223 | ||
222 | typedef struct |
224 | typedef struct |
223 | { |
225 | { |
224 | u8 Sub; |
226 | u8 Sub; |
225 | } __attribute__((packed)) Identification2_t; |
227 | } __attribute__((packed)) Identification2_t; |
226 | volatile Identification2_t NCMAG_Identification2; |
228 | volatile Identification2_t NCMAG_Identification2; |
227 | 229 | ||
228 | typedef struct |
230 | typedef struct |
229 | { |
231 | { |
230 | u8 cra; |
232 | u8 cra; |
231 | u8 crb; |
233 | u8 crb; |
232 | u8 mode; |
234 | u8 mode; |
233 | } __attribute__((packed)) MagConfig_t; |
235 | } __attribute__((packed)) MagConfig_t; |
234 | 236 | ||
235 | volatile MagConfig_t MagConfig; |
237 | volatile MagConfig_t MagConfig; |
236 | 238 | ||
237 | typedef struct |
239 | typedef struct |
238 | { |
240 | { |
239 | u8 ctrl_1; |
241 | u8 ctrl_1; |
240 | u8 ctrl_2; |
242 | u8 ctrl_2; |
241 | u8 ctrl_3; |
243 | u8 ctrl_3; |
242 | u8 ctrl_4; |
244 | u8 ctrl_4; |
243 | u8 ctrl_5; |
245 | u8 ctrl_5; |
244 | } __attribute__((packed)) AccConfig_t; |
246 | } __attribute__((packed)) AccConfig_t; |
245 | 247 | ||
246 | volatile AccConfig_t AccConfig; |
248 | volatile AccConfig_t AccConfig; |
247 | 249 | ||
248 | volatile s16vec_t AccRawVector; |
250 | volatile s16vec_t AccRawVector; |
249 | volatile s16vec_t MagRawVector; |
251 | volatile s16vec_t MagRawVector; |
250 | 252 | ||
251 | 253 | ||
252 | u8 NCMag_CalibrationWrite(void) |
254 | u8 NCMag_CalibrationWrite(void) |
253 | { |
255 | { |
254 | u8 i, crc = 0xAA; |
256 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
255 | EEPROM_Result_t eres; |
257 | EEPROM_Result_t eres; |
256 | u8 *pBuff = (u8*)&Calibration; |
258 | u8 *pBuff = (u8*)&Calibration; |
257 | 259 | ||
258 | Calibration.Version = CALIBRATION_VERSION; |
260 | Calibration.Version = CALIBRATION_VERSION; |
259 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
261 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
260 | { |
262 | { |
261 | crc += pBuff[i]; |
263 | crc += pBuff[i]; |
262 | } |
264 | } |
263 | Calibration.crc = ~crc; |
265 | Calibration.crc = ~crc; |
264 | eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)); |
266 | eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)); |
265 | if(EEPROM_SUCCESS == eres) i = 1; |
267 | if(EEPROM_SUCCESS == eres) i = 1; |
266 | else i = 0; |
268 | else i = 0; |
267 | return(i); |
269 | return(i); |
268 | } |
270 | } |
269 | 271 | ||
270 | u8 NCMag_CalibrationRead(void) |
272 | u8 NCMag_CalibrationRead(void) |
271 | { |
273 | { |
272 | u8 i, crc = 0xAA; |
274 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
273 | u8 *pBuff = (u8*)&Calibration; |
275 | u8 *pBuff = (u8*)&Calibration; |
274 | 276 | ||
275 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration))) |
277 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration))) |
276 | { |
278 | { |
277 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
279 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
278 | { |
280 | { |
279 | crc += pBuff[i]; |
281 | crc += pBuff[i]; |
280 | } |
282 | } |
281 | crc = ~crc; |
283 | crc = ~crc; |
282 | if(Calibration.crc != crc) return(0); // crc mismatch |
284 | if(Calibration.crc != crc) return(0); // crc mismatch |
283 | if(Calibration.Version == CALIBRATION_VERSION) return(1); |
285 | if(Calibration.Version == CALIBRATION_VERSION) return(1); |
284 | } |
286 | } |
285 | return(0); |
287 | return(0); |
286 | } |
288 | } |
287 | 289 | ||
288 | 290 | ||
289 | void NCMAG_Calibrate(void) |
291 | void NCMAG_Calibrate(void) |
290 | { |
292 | { |
291 | u8 msg[64]; |
293 | u8 msg[64]; |
292 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
294 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
293 | static s16 X = 0, Y = 0, Z = 0; |
295 | static s16 X = 0, Y = 0, Z = 0; |
294 | static u8 OldCalState = 0; |
296 | static u8 OldCalState = 0; |
295 | 297 | ||
296 | X = (4*X + MagRawVector.X + 3)/5; |
298 | X = (4*X + MagRawVector.X + 3)/5; |
297 | Y = (4*Y + MagRawVector.Y + 3)/5; |
299 | Y = (4*Y + MagRawVector.Y + 3)/5; |
298 | Z = (4*Z + MagRawVector.Z + 3)/5; |
300 | Z = (4*Z + MagRawVector.Z + 3)/5; |
299 | 301 | ||
300 | switch(Compass_CalState) |
302 | switch(Compass_CalState) |
301 | { |
303 | { |
302 | case 1: |
304 | case 1: |
303 | // 1st step of calibration |
305 | // 1st step of calibration |
304 | // initialize ranges |
306 | // initialize ranges |
305 | // used to change the orientation of the NC in the horizontal plane |
307 | // used to change the orientation of the NC in the horizontal plane |
306 | Xmin = 10000; |
308 | Xmin = 10000; |
307 | Xmax = -10000; |
309 | Xmax = -10000; |
308 | Ymin = 10000; |
310 | Ymin = 10000; |
309 | Ymax = -10000; |
311 | Ymax = -10000; |
310 | Zmin = 10000; |
312 | Zmin = 10000; |
311 | Zmax = -10000; |
313 | Zmax = -10000; |
312 | break; |
314 | break; |
313 | 315 | ||
314 | case 2: // 2nd step of calibration |
316 | case 2: // 2nd step of calibration |
315 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
317 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
316 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
318 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
317 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
319 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
318 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
320 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
319 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
321 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
320 | break; |
322 | break; |
321 | 323 | ||
322 | case 3: // 3rd step of calibration |
324 | case 3: // 3rd step of calibration |
323 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
325 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
324 | break; |
326 | break; |
325 | 327 | ||
326 | case 4: |
328 | case 4: |
327 | // find Min and Max of the Z-Sensor |
329 | // find Min and Max of the Z-Sensor |
328 | if(Z < Zmin) { Zmin = Z; BeepTime = 80;} |
330 | if(Z < Zmin) { Zmin = Z; BeepTime = 80;} |
329 | else if(Z > Zmax) { Zmax = Z; BeepTime = 80;} |
331 | else if(Z > Zmax) { Zmax = Z; BeepTime = 80;} |
330 | break; |
332 | break; |
331 | 333 | ||
332 | case 5: |
334 | case 5: |
333 | // Save values |
335 | // Save values |
334 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
336 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
335 | { |
337 | { |
336 | #define MIN_CALIBRATION 256 |
338 | // #define MIN_CALIBRATION 256 |
- | 339 | #define MIN_CALIBRATION 450 |
|
337 | Calibration.MagX.Range = Xmax - Xmin; |
340 | Calibration.MagX.Range = Xmax - Xmin; |
338 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
341 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
339 | Calibration.MagY.Range = Ymax - Ymin; |
342 | Calibration.MagY.Range = Ymax - Ymin; |
340 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
343 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
341 | Calibration.MagZ.Range = Zmax - Zmin; |
344 | Calibration.MagZ.Range = Zmax - Zmin; |
342 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
345 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
343 | if((Calibration.MagX.Range > MIN_CALIBRATION) && (Calibration.MagY.Range > MIN_CALIBRATION) && (Calibration.MagZ.Range > MIN_CALIBRATION)) |
346 | if((Calibration.MagX.Range > MIN_CALIBRATION) && (Calibration.MagY.Range > MIN_CALIBRATION) && (Calibration.MagZ.Range > MIN_CALIBRATION)) |
344 | { |
347 | { |
345 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(); |
348 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(); |
346 | BeepTime = 2500; |
349 | BeepTime = 2500; |
347 | UART1_PutString("\r\n Calibration okay\n\r"); |
350 | UART1_PutString("\r\n Calibration okay\n\r"); |
348 | } |
351 | } |
349 | else |
352 | else |
350 | { |
353 | { |
351 | UART1_PutString("\r\n Calibration FAILED - Values too low"); |
354 | UART1_PutString("\r\n Calibration FAILED - Values too low"); |
352 | if(Calibration.MagX.Range < MIN_CALIBRATION) UART1_PutString("X! "); |
355 | if(Calibration.MagX.Range < MIN_CALIBRATION) UART1_PutString("X! "); |
353 | if(Calibration.MagY.Range < MIN_CALIBRATION) UART1_PutString("y! "); |
356 | if(Calibration.MagY.Range < MIN_CALIBRATION) UART1_PutString("y! "); |
354 | if(Calibration.MagZ.Range < MIN_CALIBRATION) UART1_PutString("Z! "); |
357 | if(Calibration.MagZ.Range < MIN_CALIBRATION) UART1_PutString("Z! "); |
355 | UART1_PutString("\r\n"); |
358 | UART1_PutString("\r\n"); |
356 | // restore old calibration data from eeprom |
359 | // restore old calibration data from eeprom |
357 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
360 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
358 | } |
361 | } |
359 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
362 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
360 | UART1_PutString(msg); |
363 | UART1_PutString(msg); |
361 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
364 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
362 | UART1_PutString(msg); |
365 | UART1_PutString(msg); |
363 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
366 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
364 | UART1_PutString(msg); |
367 | UART1_PutString(msg); |
365 | } |
368 | } |
366 | break; |
369 | break; |
367 | 370 | ||
368 | default: |
371 | default: |
369 | break; |
372 | break; |
370 | } |
373 | } |
371 | OldCalState = Compass_CalState; |
374 | OldCalState = Compass_CalState; |
372 | } |
375 | } |
373 | 376 | ||
374 | // ---------- call back handlers ----------------------------------------- |
377 | // ---------- call back handlers ----------------------------------------- |
375 | 378 | ||
376 | // rx data handler for id info request |
379 | // rx data handler for id info request |
377 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
380 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
378 | { // if number of bytes are matching |
381 | { // if number of bytes are matching |
379 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
382 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
380 | { |
383 | { |
381 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
384 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
382 | } |
385 | } |
383 | } |
386 | } |
384 | 387 | ||
385 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
388 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
386 | { // if number of bytes are matching |
389 | { // if number of bytes are matching |
387 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
390 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
388 | { |
391 | { |
389 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
392 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
390 | } |
393 | } |
391 | } |
394 | } |
392 | 395 | ||
393 | // rx data handler for magnetic sensor raw data |
396 | // rx data handler for magnetic sensor raw data |
394 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
397 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
395 | { // if number of bytes are matching |
398 | { // if number of bytes are matching |
396 | if(RxBufferSize == sizeof(MagRawVector) ) |
399 | if(RxBufferSize == sizeof(MagRawVector) ) |
397 | { // byte order from big to little endian |
400 | { // byte order from big to little endian |
398 | s16 raw; |
401 | s16 raw; |
399 | raw = pRxBuffer[0]<<8; |
402 | raw = pRxBuffer[0]<<8; |
400 | raw+= pRxBuffer[1]; |
403 | raw+= pRxBuffer[1]; |
401 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw; |
404 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw; |
402 | raw = pRxBuffer[2]<<8; |
405 | raw = pRxBuffer[2]<<8; |
403 | raw+= pRxBuffer[3]; |
406 | raw+= pRxBuffer[3]; |
404 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
407 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
405 | { |
408 | { |
406 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged |
409 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged |
407 | else MagRawVector.Y = raw; |
410 | else MagRawVector.Y = raw; |
408 | } |
411 | } |
409 | raw = pRxBuffer[4]<<8; |
412 | raw = pRxBuffer[4]<<8; |
410 | raw+= pRxBuffer[5]; |
413 | raw+= pRxBuffer[5]; |
411 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
414 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
412 | { |
415 | { |
413 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged |
416 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged |
414 | else MagRawVector.Z = raw; |
417 | else MagRawVector.Z = raw; |
415 | } |
418 | } |
416 | 419 | ||
417 | } |
420 | } |
418 | if(Compass_CalState || !NCMAG_IsCalibrated) |
421 | if(Compass_CalState || !NCMAG_IsCalibrated) |
419 | { // mark out data invalid |
422 | { // mark out data invalid |
420 | MagVector.X = MagRawVector.X; |
423 | MagVector.X = MagRawVector.X; |
421 | MagVector.Y = MagRawVector.Y; |
424 | MagVector.Y = MagRawVector.Y; |
422 | MagVector.Z = MagRawVector.Z; |
425 | MagVector.Z = MagRawVector.Z; |
423 | Compass_Heading = -1; |
426 | Compass_Heading = -1; |
424 | } |
427 | } |
425 | else |
428 | else |
426 | { |
429 | { |
427 | // update MagVector from MagRaw Vector by Scaling |
430 | // update MagVector from MagRaw Vector by Scaling |
428 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
431 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
429 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
432 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
430 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
433 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
431 | Compass_CalcHeading(); |
434 | Compass_CalcHeading(); |
432 | } |
435 | } |
433 | } |
436 | } |
434 | // rx data handler for acceleration raw data |
437 | // rx data handler for acceleration raw data |
435 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
438 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
436 | { // if number of byte are matching |
439 | { // if number of byte are matching |
437 | if(RxBufferSize == sizeof(AccRawVector) ) |
440 | if(RxBufferSize == sizeof(AccRawVector) ) |
438 | { |
441 | { |
439 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
442 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
440 | } |
443 | } |
441 | } |
444 | } |
442 | // rx data handler for reading magnetic sensor configuration |
445 | // rx data handler for reading magnetic sensor configuration |
443 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
446 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
444 | { // if number of byte are matching |
447 | { // if number of byte are matching |
445 | if(RxBufferSize == sizeof(MagConfig) ) |
448 | if(RxBufferSize == sizeof(MagConfig) ) |
446 | { |
449 | { |
447 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
450 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
448 | } |
451 | } |
449 | } |
452 | } |
450 | // rx data handler for reading acceleration sensor configuration |
453 | // rx data handler for reading acceleration sensor configuration |
451 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
454 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
452 | { // if number of byte are matching |
455 | { // if number of byte are matching |
453 | if(RxBufferSize == sizeof(AccConfig) ) |
456 | if(RxBufferSize == sizeof(AccConfig) ) |
454 | { |
457 | { |
455 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
458 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
456 | } |
459 | } |
457 | } |
460 | } |
458 | //---------------------------------------------------------------------- |
461 | //---------------------------------------------------------------------- |
459 | 462 | ||
460 | 463 | ||
461 | // --------------------------------------------------------------------- |
464 | // --------------------------------------------------------------------- |
462 | u8 NCMAG_SetMagConfig(void) |
465 | u8 NCMAG_SetMagConfig(void) |
463 | { |
466 | { |
464 | u8 retval = 0; |
467 | u8 retval = 0; |
465 | // try to catch the i2c buffer within 100 ms timeout |
468 | // try to catch the i2c buffer within 100 ms timeout |
466 | if(I2C_LockBuffer(100)) |
469 | if(I2C_LockBuffer(100)) |
467 | { |
470 | { |
468 | u8 TxBytes = 0; |
471 | u8 TxBytes = 0; |
469 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
472 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
470 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
473 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
471 | TxBytes += sizeof(MagConfig); |
474 | TxBytes += sizeof(MagConfig); |
472 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
475 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
473 | { |
476 | { |
474 | if(I2C_WaitForEndOfTransmission(100)) |
477 | if(I2C_WaitForEndOfTransmission(100)) |
475 | { |
478 | { |
476 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
479 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
477 | } |
480 | } |
478 | } |
481 | } |
479 | } |
482 | } |
480 | return(retval); |
483 | return(retval); |
481 | } |
484 | } |
482 | 485 | ||
483 | // ---------------------------------------------------------------------------------------- |
486 | // ---------------------------------------------------------------------------------------- |
484 | u8 NCMAG_GetMagConfig(void) |
487 | u8 NCMAG_GetMagConfig(void) |
485 | { |
488 | { |
486 | u8 retval = 0; |
489 | u8 retval = 0; |
487 | // try to catch the i2c buffer within 100 ms timeout |
490 | // try to catch the i2c buffer within 100 ms timeout |
488 | if(I2C_LockBuffer(100)) |
491 | if(I2C_LockBuffer(100)) |
489 | { |
492 | { |
490 | u8 TxBytes = 0; |
493 | u8 TxBytes = 0; |
491 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
494 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
492 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
495 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
493 | { |
496 | { |
494 | if(I2C_WaitForEndOfTransmission(100)) |
497 | if(I2C_WaitForEndOfTransmission(100)) |
495 | { |
498 | { |
496 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
499 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
497 | } |
500 | } |
498 | } |
501 | } |
499 | } |
502 | } |
500 | return(retval); |
503 | return(retval); |
501 | } |
504 | } |
502 | 505 | ||
503 | // ---------------------------------------------------------------------------------------- |
506 | // ---------------------------------------------------------------------------------------- |
504 | u8 NCMAG_SetAccConfig(void) |
507 | u8 NCMAG_SetAccConfig(void) |
505 | { |
508 | { |
506 | u8 retval = 0; |
509 | u8 retval = 0; |
507 | // try to catch the i2c buffer within 100 ms timeout |
510 | // try to catch the i2c buffer within 100 ms timeout |
508 | if(I2C_LockBuffer(100)) |
511 | if(I2C_LockBuffer(100)) |
509 | { |
512 | { |
510 | u8 TxBytes = 0; |
513 | u8 TxBytes = 0; |
511 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
514 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
512 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
515 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
513 | TxBytes += sizeof(AccConfig); |
516 | TxBytes += sizeof(AccConfig); |
514 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
517 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
515 | { |
518 | { |
516 | if(I2C_WaitForEndOfTransmission(100)) |
519 | if(I2C_WaitForEndOfTransmission(100)) |
517 | { |
520 | { |
518 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
521 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
519 | } |
522 | } |
520 | } |
523 | } |
521 | } |
524 | } |
522 | return(retval); |
525 | return(retval); |
523 | } |
526 | } |
524 | 527 | ||
525 | // ---------------------------------------------------------------------------------------- |
528 | // ---------------------------------------------------------------------------------------- |
526 | u8 NCMAG_GetAccConfig(void) |
529 | u8 NCMAG_GetAccConfig(void) |
527 | { |
530 | { |
528 | u8 retval = 0; |
531 | u8 retval = 0; |
529 | // try to catch the i2c buffer within 100 ms timeout |
532 | // try to catch the i2c buffer within 100 ms timeout |
530 | if(I2C_LockBuffer(100)) |
533 | if(I2C_LockBuffer(100)) |
531 | { |
534 | { |
532 | u8 TxBytes = 0; |
535 | u8 TxBytes = 0; |
533 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
536 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
534 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
537 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
535 | { |
538 | { |
536 | if(I2C_WaitForEndOfTransmission(100)) |
539 | if(I2C_WaitForEndOfTransmission(100)) |
537 | { |
540 | { |
538 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
541 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
539 | } |
542 | } |
540 | } |
543 | } |
541 | } |
544 | } |
542 | return(retval); |
545 | return(retval); |
543 | } |
546 | } |
544 | 547 | ||
545 | // ---------------------------------------------------------------------------------------- |
548 | // ---------------------------------------------------------------------------------------- |
546 | u8 NCMAG_GetIdentification(void) |
549 | u8 NCMAG_GetIdentification(void) |
547 | { |
550 | { |
548 | u8 retval = 0; |
551 | u8 retval = 0; |
549 | // try to catch the i2c buffer within 100 ms timeout |
552 | // try to catch the i2c buffer within 100 ms timeout |
550 | if(I2C_LockBuffer(100)) |
553 | if(I2C_LockBuffer(100)) |
551 | { |
554 | { |
552 | u16 TxBytes = 0; |
555 | u16 TxBytes = 0; |
553 | NCMAG_Identification.A = 0xFF; |
556 | NCMAG_Identification.A = 0xFF; |
554 | NCMAG_Identification.B = 0xFF; |
557 | NCMAG_Identification.B = 0xFF; |
555 | NCMAG_Identification.C = 0xFF; |
558 | NCMAG_Identification.C = 0xFF; |
556 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
559 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
557 | // initiate transmission |
560 | // initiate transmission |
558 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
561 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
559 | { |
562 | { |
560 | if(I2C_WaitForEndOfTransmission(100)) |
563 | if(I2C_WaitForEndOfTransmission(100)) |
561 | { |
564 | { |
562 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
565 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
563 | } |
566 | } |
564 | } |
567 | } |
565 | } |
568 | } |
566 | return(retval); |
569 | return(retval); |
567 | } |
570 | } |
568 | 571 | ||
569 | u8 NCMAG_GetIdentification_Sub(void) |
572 | u8 NCMAG_GetIdentification_Sub(void) |
570 | { |
573 | { |
571 | u8 retval = 0; |
574 | u8 retval = 0; |
572 | // try to catch the i2c buffer within 100 ms timeout |
575 | // try to catch the i2c buffer within 100 ms timeout |
573 | if(I2C_LockBuffer(100)) |
576 | if(I2C_LockBuffer(100)) |
574 | { |
577 | { |
575 | u16 TxBytes = 0; |
578 | u16 TxBytes = 0; |
576 | NCMAG_Identification2.Sub = 0xFF; |
579 | NCMAG_Identification2.Sub = 0xFF; |
577 | I2C_Buffer[TxBytes++] = REG_MAG_IDF; |
580 | I2C_Buffer[TxBytes++] = REG_MAG_IDF; |
578 | // initiate transmission |
581 | // initiate transmission |
579 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
582 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
580 | { |
583 | { |
581 | if(I2C_WaitForEndOfTransmission(100)) |
584 | if(I2C_WaitForEndOfTransmission(100)) |
582 | { |
585 | { |
583 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
586 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
584 | } |
587 | } |
585 | } |
588 | } |
586 | } |
589 | } |
587 | return(retval); |
590 | return(retval); |
588 | } |
591 | } |
589 | 592 | ||
590 | 593 | ||
591 | // ---------------------------------------------------------------------------------------- |
594 | // ---------------------------------------------------------------------------------------- |
592 | void NCMAG_GetMagVector(void) |
595 | void NCMAG_GetMagVector(void) |
593 | { |
596 | { |
594 | // try to catch the I2C buffer within 0 ms |
597 | // try to catch the I2C buffer within 0 ms |
595 | if(I2C_LockBuffer(0)) |
598 | if(I2C_LockBuffer(0)) |
596 | { |
599 | { |
597 | // s16 tmp; |
600 | // s16 tmp; |
598 | u16 TxBytes = 0; |
601 | u16 TxBytes = 0; |
599 | // set register pointer |
602 | // set register pointer |
600 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
603 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
601 | // initiate transmission |
604 | // initiate transmission |
602 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
605 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
603 | } |
606 | } |
604 | } |
607 | } |
605 | 608 | ||
606 | //---------------------------------------------------------------- |
609 | //---------------------------------------------------------------- |
607 | void NCMAG_GetAccVector(void) |
610 | void NCMAG_GetAccVector(void) |
608 | { |
611 | { |
609 | // try to catch the I2C buffer within 0 ms |
612 | // try to catch the I2C buffer within 0 ms |
610 | if(I2C_LockBuffer(0)) |
613 | if(I2C_LockBuffer(0)) |
611 | { |
614 | { |
612 | u16 TxBytes = 0; |
615 | u16 TxBytes = 0; |
613 | // set register pointer |
616 | // set register pointer |
614 | I2C_Buffer[TxBytes++] = REG_ACC_X_LSB; |
617 | I2C_Buffer[TxBytes++] = REG_ACC_X_LSB; |
615 | // initiate transmission |
618 | // initiate transmission |
616 | I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
619 | I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
617 | } |
620 | } |
618 | } |
621 | } |
619 | 622 | ||
620 | //---------------------------------------------------------------- |
623 | //---------------------------------------------------------------- |
621 | void InitNC_MagnetSensor(void) |
624 | void InitNC_MagnetSensor(void) |
622 | { |
625 | { |
623 | s16 xscale, yscale, zscale; |
626 | s16 xscale, yscale, zscale; |
624 | u8 crb_gain, cra_rate; |
627 | u8 crb_gain, cra_rate; |
625 | u8 i = 0, retval = 1; |
628 | // u8 retval = 1; |
626 | 629 | ||
627 | switch(NCMAG_MagType) |
630 | switch(NCMAG_MagType) |
628 | { |
631 | { |
629 | case MAG_TYPE_HMC5843: |
632 | case MAG_TYPE_HMC5843: |
630 | crb_gain = HMC5843_CRB_GAIN_10GA; |
633 | crb_gain = HMC5843_CRB_GAIN_10GA; |
631 | cra_rate = HMC5843_CRA_RATE_50HZ; |
634 | cra_rate = HMC5843_CRA_RATE_50HZ; |
632 | xscale = HMC5843_TEST_XSCALE; |
635 | xscale = HMC5843_TEST_XSCALE; |
633 | yscale = HMC5843_TEST_YSCALE; |
636 | yscale = HMC5843_TEST_YSCALE; |
634 | zscale = HMC5843_TEST_ZSCALE; |
637 | zscale = HMC5843_TEST_ZSCALE; |
635 | break; |
638 | break; |
636 | 639 | ||
637 | case MAG_TYPE_LSM303DLH: |
640 | case MAG_TYPE_LSM303DLH: |
638 | crb_gain = LSM303DLH_CRB_GAIN_13GA; |
641 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
639 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
642 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
640 | xscale = LSM303DLH_TEST_XSCALE; |
643 | xscale = LSM303DLH_TEST_XSCALE; |
641 | yscale = LSM303DLH_TEST_YSCALE; |
644 | yscale = LSM303DLH_TEST_YSCALE; |
642 | zscale = LSM303DLH_TEST_ZSCALE; |
645 | zscale = LSM303DLH_TEST_ZSCALE; |
643 | break; |
646 | break; |
644 | 647 | ||
645 | default: |
648 | default: |
646 | return(0); |
649 | return; |
647 | } |
650 | } |
648 | 651 | ||
649 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
652 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
650 | MagConfig.crb = crb_gain; |
653 | MagConfig.crb = crb_gain; |
651 | MagConfig.mode = MODE_CONTINUOUS; |
654 | MagConfig.mode = MODE_CONTINUOUS; |
652 | NCMAG_SetMagConfig(); |
655 | NCMAG_SetMagConfig(); |
653 | } |
656 | } |
654 | 657 | ||
655 | 658 | ||
656 | // -------------------------------------------------------- |
659 | // -------------------------------------------------------- |
657 | void NCMAG_Update(void) |
660 | void NCMAG_Update(void) |
658 | { |
661 | { |
659 | static u32 TimerUpdate = 0; |
662 | static u32 TimerUpdate = 0; |
660 | static u8 send_config = 0; |
663 | static u8 send_config = 0; |
661 | 664 | ||
662 | if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) |
665 | if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) |
663 | { |
666 | { |
664 | Compass_Heading = -1; |
667 | Compass_Heading = -1; |
665 | DebugOut.Analog[14]++; // count I2C error |
668 | DebugOut.Analog[14]++; // count I2C error |
666 | return; |
669 | return; |
667 | } |
670 | } |
668 | if(CheckDelay(TimerUpdate)) |
671 | if(CheckDelay(TimerUpdate)) |
669 | { |
672 | { |
670 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
673 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
671 | if(++send_config == 25) // 500ms |
674 | if(++send_config == 25) // 500ms |
672 | { |
675 | { |
673 | send_config = 0; |
676 | send_config = 0; |
674 | InitNC_MagnetSensor(); |
677 | InitNC_MagnetSensor(); |
675 | TimerUpdate = SetDelay(15); // back into the old time-slot |
678 | TimerUpdate = SetDelay(15); // back into the old time-slot |
676 | } |
679 | } |
677 | else |
680 | else |
678 | { |
681 | { |
679 | // check for new calibration state |
682 | // check for new calibration state |
680 | Compass_UpdateCalState(); |
683 | Compass_UpdateCalState(); |
681 | if(Compass_CalState) NCMAG_Calibrate(); |
684 | if(Compass_CalState) NCMAG_Calibrate(); |
682 | NCMAG_GetMagVector(); //Get new data; |
685 | NCMAG_GetMagVector(); //Get new data; |
683 | if(send_config == 24) TimerUpdate = SetDelay(5); // next event is the re-configuration |
686 | if(send_config == 24) TimerUpdate = SetDelay(5); // next event is the re-configuration |
684 | else TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
687 | else TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
685 | } |
688 | } |
686 | } |
689 | } |
687 | } |
690 | } |
688 | 691 | ||
689 | 692 | ||
690 | // -------------------------------------------------------- |
693 | // -------------------------------------------------------- |
691 | u8 NCMAG_SelfTest(void) |
694 | u8 NCMAG_SelfTest(void) |
692 | { |
695 | { |
693 | u8 msg[64]; |
696 | u8 msg[64]; |
694 | static u8 done = 0; |
697 | static u8 done = 0; |
695 | 698 | ||
696 | if(done) return(1); // just make it once |
699 | if(done) return(1); // just make it once |
697 | 700 | ||
698 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
701 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
699 | u32 time; |
702 | u32 time; |
700 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
703 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
701 | s16 xscale, yscale, zscale, scale_min, scale_max; |
704 | s16 xscale, yscale, zscale, scale_min, scale_max; |
702 | u8 crb_gain, cra_rate; |
705 | u8 crb_gain, cra_rate; |
703 | u8 i = 0, retval = 1; |
706 | u8 i = 0, retval = 1; |
704 | 707 | ||
705 | switch(NCMAG_MagType) |
708 | switch(NCMAG_MagType) |
706 | { |
709 | { |
707 | case MAG_TYPE_HMC5843: |
710 | case MAG_TYPE_HMC5843: |
708 | crb_gain = HMC5843_CRB_GAIN_10GA; |
711 | crb_gain = HMC5843_CRB_GAIN_10GA; |
709 | cra_rate = HMC5843_CRA_RATE_50HZ; |
712 | cra_rate = HMC5843_CRA_RATE_50HZ; |
710 | xscale = HMC5843_TEST_XSCALE; |
713 | xscale = HMC5843_TEST_XSCALE; |
711 | yscale = HMC5843_TEST_YSCALE; |
714 | yscale = HMC5843_TEST_YSCALE; |
712 | zscale = HMC5843_TEST_ZSCALE; |
715 | zscale = HMC5843_TEST_ZSCALE; |
713 | break; |
716 | break; |
714 | 717 | ||
715 | case MAG_TYPE_LSM303DLH: |
718 | case MAG_TYPE_LSM303DLH: |
716 | crb_gain = LSM303DLH_CRB_GAIN_13GA; |
719 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
717 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
720 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
718 | xscale = LSM303DLH_TEST_XSCALE; |
721 | xscale = LSM303DLH_TEST_XSCALE; |
719 | yscale = LSM303DLH_TEST_YSCALE; |
722 | yscale = LSM303DLH_TEST_YSCALE; |
720 | zscale = LSM303DLH_TEST_ZSCALE; |
723 | zscale = LSM303DLH_TEST_ZSCALE; |
721 | break; |
724 | break; |
722 | 725 | ||
723 | default: |
726 | default: |
724 | return(0); |
727 | return(0); |
725 | } |
728 | } |
726 | 729 | ||
727 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
730 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
728 | MagConfig.crb = crb_gain; |
731 | MagConfig.crb = crb_gain; |
729 | MagConfig.mode = MODE_CONTINUOUS; |
732 | MagConfig.mode = MODE_CONTINUOUS; |
730 | // activate positive bias field |
733 | // activate positive bias field |
731 | NCMAG_SetMagConfig(); |
734 | NCMAG_SetMagConfig(); |
732 | // wait for stable readings |
735 | // wait for stable readings |
733 | time = SetDelay(50); |
736 | time = SetDelay(50); |
734 | while(!CheckDelay(time)); |
737 | while(!CheckDelay(time)); |
735 | // averaging |
738 | // averaging |
736 | #define AVERAGE 20 |
739 | #define AVERAGE 20 |
737 | for(i = 0; i<AVERAGE; i++) |
740 | for(i = 0; i<AVERAGE; i++) |
738 | { |
741 | { |
739 | NCMAG_GetMagVector(); |
742 | NCMAG_GetMagVector(); |
740 | time = SetDelay(20); |
743 | time = SetDelay(20); |
741 | while(!CheckDelay(time)); |
744 | while(!CheckDelay(time)); |
742 | XMax += MagRawVector.X; |
745 | XMax += MagRawVector.X; |
743 | YMax += MagRawVector.Y; |
746 | YMax += MagRawVector.Y; |
744 | ZMax += MagRawVector.Z; |
747 | ZMax += MagRawVector.Z; |
745 | } |
748 | } |
746 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
749 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
747 | // activate positive bias field |
750 | // activate positive bias field |
748 | NCMAG_SetMagConfig(); |
751 | NCMAG_SetMagConfig(); |
749 | // wait for stable readings |
752 | // wait for stable readings |
750 | time = SetDelay(50); |
753 | time = SetDelay(50); |
751 | while(!CheckDelay(time)); |
754 | while(!CheckDelay(time)); |
752 | // averaging |
755 | // averaging |
753 | for(i = 0; i < AVERAGE; i++) |
756 | for(i = 0; i < AVERAGE; i++) |
754 | { |
757 | { |
755 | NCMAG_GetMagVector(); |
758 | NCMAG_GetMagVector(); |
756 | time = SetDelay(20); |
759 | time = SetDelay(20); |
757 | while(!CheckDelay(time)); |
760 | while(!CheckDelay(time)); |
758 | XMin += MagRawVector.X; |
761 | XMin += MagRawVector.X; |
759 | YMin += MagRawVector.Y; |
762 | YMin += MagRawVector.Y; |
760 | ZMin += MagRawVector.Z; |
763 | ZMin += MagRawVector.Z; |
761 | } |
764 | } |
762 | // setup final configuration |
765 | // setup final configuration |
763 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
766 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
764 | // activate positive bias field |
767 | // activate positive bias field |
765 | NCMAG_SetMagConfig(); |
768 | NCMAG_SetMagConfig(); |
766 | // check scale for all axes |
769 | // check scale for all axes |
767 | // prepare scale limits |
770 | // prepare scale limits |
768 | LIMITS(xscale, scale_min, scale_max); |
771 | LIMITS(xscale, scale_min, scale_max); |
769 | xscale = (XMax - XMin)/(2*AVERAGE); |
772 | xscale = (XMax - XMin)/(2*AVERAGE); |
770 | if((xscale > scale_max) || (xscale < scale_min)) |
773 | if((xscale > scale_max) || (xscale < scale_min)) |
771 | { |
774 | { |
772 | retval = 0; |
775 | retval = 0; |
773 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
776 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
774 | UART1_PutString(msg); |
777 | UART1_PutString(msg); |
775 | } |
778 | } |
776 | LIMITS(yscale, scale_min, scale_max); |
779 | LIMITS(yscale, scale_min, scale_max); |
777 | yscale = (YMax - YMin)/(2*AVERAGE); |
780 | yscale = (YMax - YMin)/(2*AVERAGE); |
778 | if((yscale > scale_max) || (yscale < scale_min)) |
781 | if((yscale > scale_max) || (yscale < scale_min)) |
779 | { |
782 | { |
780 | retval = 0; |
783 | retval = 0; |
781 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
784 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
782 | UART1_PutString(msg); |
785 | UART1_PutString(msg); |
783 | } |
786 | } |
784 | LIMITS(zscale, scale_min, scale_max); |
787 | LIMITS(zscale, scale_min, scale_max); |
785 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
788 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
786 | if((zscale > scale_max) || (zscale < scale_min)) |
789 | if((zscale > scale_max) || (zscale < scale_min)) |
787 | { |
790 | { |
788 | retval = 0; |
791 | retval = 0; |
789 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
792 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
790 | UART1_PutString(msg); |
793 | UART1_PutString(msg); |
791 | } |
794 | } |
792 | done = retval; |
795 | done = retval; |
793 | return(retval); |
796 | return(retval); |
794 | } |
797 | } |
795 | 798 | ||
796 | 799 | ||
797 | //---------------------------------------------------------------- |
800 | //---------------------------------------------------------------- |
798 | u8 NCMAG_Init(void) |
801 | u8 NCMAG_Init(void) |
799 | { |
802 | { |
800 | u8 msg[64]; |
803 | u8 msg[64]; |
801 | u8 retval = 0; |
804 | u8 retval = 0; |
802 | u8 repeat; |
805 | u8 repeat; |
803 | 806 | ||
804 | NCMAG_Present = 0; |
807 | NCMAG_Present = 0; |
805 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
808 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
806 | // polling for LSM302DLH option |
809 | // polling for LSM302DLH option |
807 | repeat = 0; |
810 | repeat = 0; |
808 | do |
811 | do |
809 | { |
812 | { |
810 | retval = NCMAG_GetAccConfig(); |
813 | retval = NCMAG_GetAccConfig(); |
811 | if(retval) break; // break loop on success |
814 | if(retval) break; // break loop on success |
812 | UART1_PutString("."); |
815 | UART1_PutString("."); |
813 | repeat++; |
816 | repeat++; |
814 | }while(repeat < 3); |
817 | }while(repeat < 3); |
815 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
818 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
816 | // polling of identification |
819 | // polling of identification |
817 | repeat = 0; |
820 | repeat = 0; |
818 | do |
821 | do |
819 | { |
822 | { |
820 | retval = NCMAG_GetIdentification_Sub(); |
823 | retval = NCMAG_GetIdentification_Sub(); |
821 | if(retval) break; // break loop on success |
824 | if(retval) break; // break loop on success |
822 | UART1_PutString("."); |
825 | UART1_PutString("."); |
823 | repeat++; |
826 | repeat++; |
824 | }while(repeat < 12); |
827 | }while(repeat < 12); |
825 | retval = 0; |
828 | retval = 0; |
826 | do |
829 | do |
827 | { |
830 | { |
828 | retval = NCMAG_GetIdentification(); |
831 | retval = NCMAG_GetIdentification(); |
829 | if(retval) break; // break loop on success |
832 | if(retval) break; // break loop on success |
830 | UART1_PutString("."); |
833 | UART1_PutString("."); |
831 | repeat++; |
834 | repeat++; |
832 | }while(repeat < 12); |
835 | }while(repeat < 12); |
833 | 836 | ||
834 | // if we got an answer to id request |
837 | // if we got an answer to id request |
835 | if(retval) |
838 | if(retval) |
836 | { |
839 | { |
837 | u8 n1[] = "\n\r HMC5843"; |
840 | u8 n1[] = "\n\r HMC5843"; |
838 | u8 n2[] = "\n\r LSM303DLH"; |
841 | u8 n2[] = "\n\r LSM303DLH"; |
839 | u8 n3[] = "\n\r LSM303DLM"; |
842 | u8 n3[] = "\n\r LSM303DLM"; |
840 | u8* pn; |
843 | u8* pn; |
841 | 844 | ||
842 | pn = n1; |
845 | pn = n1; |
843 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
846 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
844 | { |
847 | { |
845 | if(NCMAG_Identification2.Sub == 0x3c) pn = n3; |
848 | if(NCMAG_Identification2.Sub == 0x3c) pn = n3; |
846 | else pn = n2; |
849 | else pn = n2; |
847 | } |
850 | } |
848 | 851 | ||
849 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
852 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
850 | UART1_PutString(msg); |
853 | UART1_PutString(msg); |
851 | if ( (NCMAG_Identification.A == MAG_IDA) |
854 | if ( (NCMAG_Identification.A == MAG_IDA) |
852 | && (NCMAG_Identification.B == MAG_IDB) |
855 | && (NCMAG_Identification.B == MAG_IDB) |
853 | && (NCMAG_Identification.C == MAG_IDC)) |
856 | && (NCMAG_Identification.C == MAG_IDC)) |
854 | { |
857 | { |
855 | NCMAG_Present = 1; |
858 | NCMAG_Present = 1; |
856 | 859 | ||
857 | if(EEPROM_Init()) |
860 | if(EEPROM_Init()) |
858 | { |
861 | { |
859 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
862 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
860 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
863 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
861 | } |
864 | } |
862 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
865 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
863 | 866 | ||
864 | if(NCMAG_Identification2.Sub == 0x00) |
867 | if(NCMAG_Identification2.Sub == 0x00) |
865 | { |
868 | { |
866 | if(!NCMAG_SelfTest()) |
869 | if(!NCMAG_SelfTest()) |
867 | { |
870 | { |
868 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
871 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
869 | LED_RED_ON; |
872 | LED_RED_ON; |
870 | NCMAG_IsCalibrated = 0; |
873 | NCMAG_IsCalibrated = 0; |
871 | } else UART1_PutString("\r\n Selftest ok"); |
874 | } else UART1_PutString("\r\n Selftest ok"); |
872 | } |
875 | } |
873 | else InitNC_MagnetSensor(); |
876 | else InitNC_MagnetSensor(); |
874 | } |
877 | } |
875 | else |
878 | else |
876 | { |
879 | { |
877 | UART1_PutString("\n\r Not compatible!"); |
880 | UART1_PutString("\n\r Not compatible!"); |
878 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
881 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
879 | LED_RED_ON; |
882 | LED_RED_ON; |
880 | } |
883 | } |
881 | } |
884 | } |
882 | else // nothing found |
885 | else // nothing found |
883 | { |
886 | { |
884 | NCMAG_MagType = MAG_TYPE_NONE; |
887 | NCMAG_MagType = MAG_TYPE_NONE; |
885 | UART1_PutString("not found!"); |
888 | UART1_PutString("not found!"); |
886 | } |
889 | } |
887 | return(NCMAG_Present); |
890 | return(NCMAG_Present); |
888 | } |
891 | } |
889 | 892 | ||
890 | 893 |