Rev 613 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
242 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
360 | holgerb | 7 | // + Software Nutzungsbedingungen (english version: see below) |
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 |
||
489 | killagreg | 10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
360 | holgerb | 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. |
||
242 | killagreg | 13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 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. |
||
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. |
||
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. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
489 | killagreg | 21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
360 | holgerb | 22 | // + des Mitverschuldens offen. |
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. |
||
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 |
||
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. |
||
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. |
||
242 | killagreg | 31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 32 | // + Software LICENSING TERMS |
242 | killagreg | 33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
360 | holgerb | 34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
489 | killagreg | 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 |
360 | holgerb | 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. |
||
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 |
||
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 |
||
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 |
||
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 |
||
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. |
||
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. |
||
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. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
242 | killagreg | 55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
254 | killagreg | 56 | #include <math.h> |
292 | killagreg | 57 | #include <stdio.h> |
489 | killagreg | 58 | #include <stdlib.h> |
242 | killagreg | 59 | #include <string.h> |
60 | #include "91x_lib.h" |
||
253 | killagreg | 61 | #include "ncmag.h" |
489 | killagreg | 62 | #include "i2c.h" |
242 | killagreg | 63 | #include "timer1.h" |
64 | #include "led.h" |
||
65 | #include "uart1.h" |
||
254 | killagreg | 66 | #include "eeprom.h" |
256 | killagreg | 67 | #include "mymath.h" |
292 | killagreg | 68 | #include "main.h" |
454 | holgerb | 69 | #include "spi_slave.h" |
242 | killagreg | 70 | |
253 | killagreg | 71 | u8 NCMAG_Present = 0; |
254 | killagreg | 72 | u8 NCMAG_IsCalibrated = 0; |
394 | killagreg | 73 | u8 NCMAG_SensorType = TYPE_NONE; |
489 | killagreg | 74 | u8 NCMAG_Orientation = 0; // 0 means unknown! |
394 | killagreg | 75 | |
489 | killagreg | 76 | #define CALIBRATION_VERSION 1 |
500 | holgerb | 77 | #define MAG_CALIBRATION_COMPATIBLE 0xA3 |
489 | killagreg | 78 | |
79 | #define NCMAG_MIN_RAWVALUE -2047 |
||
80 | #define NCMAG_MAX_RAWVALUE 2047 |
||
81 | #define NCMAG_INVALID_DATA -4096 |
||
82 | |||
83 | typedef struct |
||
84 | { |
||
85 | s16 Range; |
||
86 | s16 Offset; |
||
87 | } __attribute__((packed)) Scaling_t; |
||
88 | |||
89 | typedef struct |
||
90 | { |
||
91 | Scaling_t MagX; |
||
92 | Scaling_t MagY; |
||
93 | Scaling_t MagZ; |
||
94 | u8 Version; |
||
95 | u8 crc; |
||
96 | } __attribute__((packed)) Calibration_t; |
||
97 | |||
98 | Calibration_t Calibration; // calibration data in RAM |
||
339 | holgerb | 99 | volatile s16vec_t AccRawVector; |
100 | volatile s16vec_t MagRawVector; |
||
254 | killagreg | 101 | |
253 | killagreg | 102 | // i2c MAG interface |
103 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
||
242 | killagreg | 104 | |
253 | killagreg | 105 | // register mapping |
106 | #define REG_MAG_CRA 0x00 |
||
107 | #define REG_MAG_CRB 0x01 |
||
108 | #define REG_MAG_MODE 0x02 |
||
109 | #define REG_MAG_DATAX_MSB 0x03 |
||
110 | #define REG_MAG_DATAX_LSB 0x04 |
||
111 | #define REG_MAG_DATAY_MSB 0x05 |
||
112 | #define REG_MAG_DATAY_LSB 0x06 |
||
113 | #define REG_MAG_DATAZ_MSB 0x07 |
||
114 | #define REG_MAG_DATAZ_LSB 0x08 |
||
115 | #define REG_MAG_STATUS 0x09 |
||
329 | holgerb | 116 | |
253 | killagreg | 117 | #define REG_MAG_IDA 0x0A |
118 | #define REG_MAG_IDB 0x0B |
||
119 | #define REG_MAG_IDC 0x0C |
||
394 | killagreg | 120 | #define REG_MAG_IDF 0x0F // WHO_AM_I _M = 0x03c when LSM303DLM is connected |
242 | killagreg | 121 | |
590 | holgerb | 122 | #define ADR_LSM303D_MAG 0x08 |
123 | #define REG_303D_CTRL0 0x1F // Fifo + Interrupts |
||
124 | #define REG_303D_CTRL1 0x20 // ACC Update Speed |
||
125 | #define REG_303D_CTRL2 0x21 // ACC Gain + Filter |
||
126 | #define REG_303D_CTRL3 0x22 // Fifo + Interrupts |
||
127 | #define REG_303D_CTRL4 0x23 // Fifo + Interrupts |
||
128 | #define REG_303D_CTRL5 0x24 // Temperature Enable + Magn.Resolution + Magn.Rate |
||
129 | #define REG_303D_CTRL6 0x25 // Magn. Gain |
||
130 | #define REG_303D_CTRL7 0x26 // Magn. PowerDown + Filter |
||
253 | killagreg | 131 | // bit mask for configuration mode |
132 | #define CRA_MODE_MASK 0x03 |
||
133 | #define CRA_MODE_NORMAL 0x00 //default |
||
134 | #define CRA_MODE_POSBIAS 0x01 |
||
135 | #define CRA_MODE_NEGBIAS 0x02 |
||
136 | #define CRA_MODE_SELFTEST 0x03 |
||
242 | killagreg | 137 | |
253 | killagreg | 138 | // bit mask for measurement mode |
139 | #define MODE_MASK 0xFF |
||
140 | #define MODE_CONTINUOUS 0x00 |
||
141 | #define MODE_SINGLE 0x01 // default |
||
142 | #define MODE_IDLE 0x02 |
||
143 | #define MODE_SLEEP 0x03 |
||
144 | |||
242 | killagreg | 145 | // bit mask for rate |
253 | killagreg | 146 | #define CRA_RATE_MASK 0x1C |
147 | |||
148 | // bit mask for gain |
||
149 | #define CRB_GAIN_MASK 0xE0 |
||
150 | |||
151 | // ids |
||
152 | #define MAG_IDA 0x48 |
||
153 | #define MAG_IDB 0x34 |
||
154 | #define MAG_IDC 0x33 |
||
394 | killagreg | 155 | #define MAG_IDF_LSM303DLM 0x3C |
590 | holgerb | 156 | #define MAG_IDF_LSM303D 0x49 |
157 | #define MAG_IDF_LSM303DLH 0x00 |
||
253 | killagreg | 158 | |
159 | // the special HMC5843 interface |
||
160 | // bit mask for rate |
||
242 | killagreg | 161 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
162 | #define HMC5843_CRA_RATE_1HZ 0x04 |
||
163 | #define HMC5843_CRA_RATE_2HZ 0x08 |
||
164 | #define HMC5843_CRA_RATE_5HZ 0x0C |
||
165 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
||
166 | #define HMC5843_CRA_RATE_20HZ 0x14 |
||
590 | holgerb | 167 | #define HMC5843_CRA_RATE_50HZ 0x18 // <-- we use this |
242 | killagreg | 168 | // bit mask for gain |
169 | #define HMC5843_CRB_GAIN_07GA 0x00 |
||
170 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
||
489 | killagreg | 171 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
242 | killagreg | 172 | #define HMC5843_CRB_GAIN_20GA 0x60 |
173 | #define HMC5843_CRB_GAIN_32GA 0x80 |
||
174 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
||
175 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
||
176 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
||
253 | killagreg | 177 | // self test value |
339 | holgerb | 178 | #define HMC5843_TEST_XSCALE 555 |
179 | #define HMC5843_TEST_YSCALE 555 |
||
180 | #define HMC5843_TEST_ZSCALE 555 |
||
394 | killagreg | 181 | // calibration range |
342 | holgerb | 182 | #define HMC5843_CALIBRATION_RANGE 600 |
242 | killagreg | 183 | |
253 | killagreg | 184 | // the special LSM302DLH interface |
185 | // bit mask for rate |
||
186 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
||
187 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
||
188 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
||
189 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
||
190 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
||
191 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
||
590 | holgerb | 192 | #define LSM303DLH_CRA_RATE_75HZ 0x18 // <-- we use this |
338 | holgerb | 193 | |
590 | holgerb | 194 | // LSM303D CTRL1 |
195 | #define LSM303D_ACC_RATE_0HZ 0x0F |
||
196 | #define LSM303D_ACC_RATE_3HZ 0x1F |
||
197 | #define LSM303D_ACC_RATE_6HZ 0x2F |
||
198 | #define LSM303D_ACC_RATE_12HZ 0x3F |
||
199 | #define LSM303D_ACC_RATE_25HZ 0x4F |
||
200 | #define LSM303D_ACC_RATE_50HZ 0x5F |
||
201 | #define LSM303D_ACC_RATE_100HZ 0x6F |
||
202 | #define LSM303D_ACC_RATE_200HZ 0x7F |
||
203 | #define LSM303D_ACC_RATE_400HZ 0x8F |
||
204 | #define LSM303D_ACC_RATE_800HZ 0x9F |
||
205 | #define LSM303D_ACC_RATE_1600HZ 0xAF |
||
206 | |||
207 | // LSM303D CTRL1 |
||
208 | #define LSM303D_ACC_SCALE_2g 0x00 |
||
209 | #define LSM303D_ACC_SCALE_4g 0x08 |
||
210 | #define LSM303D_ACC_SCALE_6g 0x10 |
||
211 | #define LSM303D_ACC_SCALE_8g 0x18 |
||
212 | #define LSM303D_ACC_SCALE_16g 0x20 |
||
213 | |||
214 | // LSM303D CTRL5 |
||
215 | #define LSM303D_ODR_RATE_3HZ 0x00 |
||
216 | #define LSM303D_ODR_RATE_6HZ 0x04 |
||
217 | #define LSM303D_ODR_RATE_12HZ 0x08 |
||
218 | #define LSM303D_ODR_RATE_25HZ 0x0C |
||
219 | #define LSM303D_ODR_RATE_50HZ 0x10 |
||
220 | #define LSM303D_ODR_RATE_100HZ 0x14 // attention: ACC Rate must be >50Hz to use this |
||
221 | #define LSM303D_HIGH_RESULUTION 0x60 |
||
222 | #define LSM303D_LOW_RESULUTION 0x00 |
||
223 | #define LSM303D_TEMP_ENABLE 0x80 |
||
224 | |||
225 | // LSM303D CTRL6 |
||
226 | #define LSM303D_MAG_SCALE_2GA 0x00 |
||
227 | #define LSM303D_MAG_SCALE_4GA 0x20 |
||
228 | #define LSM303D_MAG_SCALE_8GA 0x40 // <-- we use this (Achtung: der LSM303D hat eine höhere Auflösung) |
||
229 | #define LSM303D_MAG_SCALE_12GA 0x60 |
||
230 | |||
253 | killagreg | 231 | // bit mask for gain |
232 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
||
233 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
||
500 | holgerb | 234 | #define LSM303DLH_CRB_GAIN_19GA 0x40 |
253 | killagreg | 235 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
500 | holgerb | 236 | #define LSM303DLH_CRB_GAIN_40GA 0x80 // <--- we use this (Since V2.03) |
253 | killagreg | 237 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
238 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
||
239 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
||
394 | killagreg | 240 | |
241 | typedef struct |
||
242 | { |
||
243 | u8 A; |
||
244 | u8 B; |
||
245 | u8 C; |
||
246 | } __attribute__((packed)) Identification_t; |
||
247 | volatile Identification_t NCMAG_Identification; |
||
248 | |||
249 | typedef struct |
||
250 | { |
||
251 | u8 Sub; |
||
252 | } __attribute__((packed)) Identification2_t; |
||
253 | volatile Identification2_t NCMAG_Identification2; |
||
254 | |||
255 | typedef struct |
||
256 | { |
||
257 | u8 cra; |
||
258 | u8 crb; |
||
259 | u8 mode; |
||
260 | } __attribute__((packed)) MagConfig_t; |
||
261 | |||
262 | volatile MagConfig_t MagConfig; |
||
263 | |||
253 | killagreg | 264 | // self test value |
500 | holgerb | 265 | #define LSM303DLH_TEST_XSCALE 245 |
266 | #define LSM303DLH_TEST_YSCALE 245 |
||
267 | #define LSM303DLH_TEST_ZSCALE 235 |
||
339 | holgerb | 268 | // clibration range |
500 | holgerb | 269 | #define LSM303_CALIBRATION_RANGE 300 // War bis V2.02: 550 -> Auflösung von 19Ga auf 40GA reduziert |
253 | killagreg | 270 | |
271 | // the i2c ACC interface |
||
272 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
||
394 | killagreg | 273 | |
274 | // multiple byte read/write mask |
||
590 | holgerb | 275 | #define REG_MASK_AUTOINCREMENT 0x80 |
394 | killagreg | 276 | |
253 | killagreg | 277 | // register mapping |
278 | #define REG_ACC_CTRL1 0x20 |
||
279 | #define REG_ACC_CTRL2 0x21 |
||
280 | #define REG_ACC_CTRL3 0x22 |
||
281 | #define REG_ACC_CTRL4 0x23 |
||
282 | #define REG_ACC_CTRL5 0x24 |
||
283 | #define REG_ACC_HP_FILTER_RESET 0x25 |
||
284 | #define REG_ACC_REFERENCE 0x26 |
||
285 | #define REG_ACC_STATUS 0x27 |
||
286 | #define REG_ACC_X_LSB 0x28 |
||
287 | #define REG_ACC_X_MSB 0x29 |
||
288 | #define REG_ACC_Y_LSB 0x2A |
||
289 | #define REG_ACC_Y_MSB 0x2B |
||
290 | #define REG_ACC_Z_LSB 0x2C |
||
291 | #define REG_ACC_Z_MSB 0x2D |
||
292 | |||
394 | killagreg | 293 | #define ACC_CRTL1_PM_DOWN 0x00 |
294 | #define ACC_CRTL1_PM_NORMAL 0x20 |
||
295 | #define ACC_CRTL1_PM_LOW_0_5HZ 0x40 |
||
296 | #define ACC_CRTL1_PM_LOW_1HZ 0x60 |
||
297 | #define ACC_CRTL1_PM_LOW_2HZ 0x80 |
||
298 | #define ACC_CRTL1_PM_LOW_5HZ 0xA0 |
||
299 | #define ACC_CRTL1_PM_LOW_10HZ 0xC0 |
||
300 | // Output data rate in normal power mode |
||
301 | #define ACC_CRTL1_DR_50HZ 0x00 |
||
302 | #define ACC_CRTL1_DR_100HZ 0x08 |
||
303 | #define ACC_CRTL1_DR_400HZ 0x10 |
||
304 | #define ACC_CRTL1_DR_1000HZ 0x18 |
||
489 | killagreg | 305 | // axis anable flags |
394 | killagreg | 306 | #define ACC_CRTL1_XEN 0x01 |
307 | #define ACC_CRTL1_YEN 0x02 |
||
308 | #define ACC_CRTL1_ZEN 0x04 |
||
253 | killagreg | 309 | |
397 | holgerb | 310 | #define ACC_CRTL2_FILTER8 0x10 |
311 | #define ACC_CRTL2_FILTER16 0x11 |
||
312 | #define ACC_CRTL2_FILTER32 0x12 |
||
313 | #define ACC_CRTL2_FILTER64 0x13 |
||
395 | holgerb | 314 | |
394 | killagreg | 315 | #define ACC_CTRL4_BDU 0x80 // Block data update, (0: continuos update; 1: output registers not updated between MSB and LSB reading) |
316 | #define ACC_CTRL4_BLE 0x40 // Big/little endian, (0: data LSB @ lower address; 1: data MSB @ lower address) |
||
317 | #define ACC_CTRL4_FS_2G 0x00 |
||
318 | #define ACC_CTRL4_FS_4G 0x10 |
||
319 | #define ACC_CTRL4_FS_8G 0x30 |
||
320 | #define ACC_CTRL4_STSIGN_PLUS 0x00 |
||
321 | #define ACC_CTRL4_STSIGN_MINUS 0x08 |
||
322 | #define ACC_CTRL4_ST_ENABLE 0x02 |
||
253 | killagreg | 323 | |
394 | killagreg | 324 | #define ACC_CTRL5_STW_ON 0x03 |
325 | #define ACC_CTRL5_STW_OFF 0x00 |
||
242 | killagreg | 326 | |
253 | killagreg | 327 | typedef struct |
328 | { |
||
329 | u8 ctrl_1; |
||
330 | u8 ctrl_2; |
||
331 | u8 ctrl_3; |
||
332 | u8 ctrl_4; |
||
333 | u8 ctrl_5; |
||
334 | } __attribute__((packed)) AccConfig_t; |
||
335 | |||
336 | volatile AccConfig_t AccConfig; |
||
337 | |||
489 | killagreg | 338 | // write calibration data for external and internal sensor seperately |
339 | u8 NCMag_CalibrationWrite(I2C_TypeDef* I2Cx) |
||
254 | killagreg | 340 | { |
472 | holgerb | 341 | u16 address; |
489 | killagreg | 342 | u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE; |
254 | killagreg | 343 | EEPROM_Result_t eres; |
344 | u8 *pBuff = (u8*)&Calibration; |
||
345 | |||
489 | killagreg | 346 | if (I2Cx == NCMAG_PORT_EXTERN) |
473 | holgerb | 347 | { |
489 | killagreg | 348 | address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
349 | Calibration.Version = CALIBRATION_VERSION + (NCMAG_Orientation<<4);; |
||
473 | holgerb | 350 | } |
489 | killagreg | 351 | else if (I2Cx == NCMAG_PORT_INTERN) |
352 | { |
||
353 | address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
||
354 | Calibration.Version = CALIBRATION_VERSION; |
||
355 | } |
||
356 | else return(i); |
||
357 | |||
256 | killagreg | 358 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
254 | killagreg | 359 | { |
489 | killagreg | 360 | crc += pBuff[i]; |
254 | killagreg | 361 | } |
362 | Calibration.crc = ~crc; |
||
472 | holgerb | 363 | eres = EEPROM_WriteBlock(address, pBuff, sizeof(Calibration)); |
254 | killagreg | 364 | if(EEPROM_SUCCESS == eres) i = 1; |
365 | else i = 0; |
||
489 | killagreg | 366 | return(i); |
254 | killagreg | 367 | } |
368 | |||
489 | killagreg | 369 | // read calibration data for external and internal sensor seperately |
370 | u8 NCMag_CalibrationRead(I2C_TypeDef* I2Cx) |
||
254 | killagreg | 371 | { |
489 | killagreg | 372 | u8 address; |
373 | u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE; |
||
254 | killagreg | 374 | u8 *pBuff = (u8*)&Calibration; |
375 | |||
489 | killagreg | 376 | if (I2Cx == NCMAG_PORT_EXTERN) address = EEPROM_ADR_MAG_CALIBRATION_EXTERN; |
377 | else if (I2Cx == NCMAG_PORT_INTERN) address = EEPROM_ADR_MAG_CALIBRATION_INTERN; |
||
378 | else return(0); |
||
472 | holgerb | 379 | |
380 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(address, pBuff, sizeof(Calibration))) |
||
254 | killagreg | 381 | { |
256 | killagreg | 382 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
254 | killagreg | 383 | { |
489 | killagreg | 384 | crc += pBuff[i]; |
254 | killagreg | 385 | } |
386 | crc = ~crc; |
||
387 | if(Calibration.crc != crc) return(0); // crc mismatch |
||
489 | killagreg | 388 | if((Calibration.Version & 0x0F) == CALIBRATION_VERSION) return(1); |
254 | killagreg | 389 | } |
390 | return(0); |
||
391 | } |
||
392 | |||
393 | |||
394 | void NCMAG_Calibrate(void) |
||
395 | { |
||
330 | holgerb | 396 | u8 msg[64]; |
454 | holgerb | 397 | static u8 speak = 0; |
489 | killagreg | 398 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0, Zmin2 = 0, Zmax2 = 0;; |
256 | killagreg | 399 | static s16 X = 0, Y = 0, Z = 0; |
489 | killagreg | 400 | static u8 OldCalState = 0; |
394 | killagreg | 401 | s16 MinCalibration = 450; |
254 | killagreg | 402 | |
488 | holgerb | 403 | X = (X + MagRawVector.X)/2; |
404 | Y = (Y + MagRawVector.Y)/2; |
||
405 | Z = (Z + MagRawVector.Z)/2; |
||
256 | killagreg | 406 | |
254 | killagreg | 407 | switch(Compass_CalState) |
408 | { |
||
409 | case 1: |
||
410 | // 1st step of calibration |
||
411 | // initialize ranges |
||
412 | // used to change the orientation of the NC in the horizontal plane |
||
413 | Xmin = 10000; |
||
414 | Xmax = -10000; |
||
415 | Ymin = 10000; |
||
416 | Ymax = -10000; |
||
417 | Zmin = 10000; |
||
418 | Zmax = -10000; |
||
500 | holgerb | 419 | Zmin2 = 10000; |
420 | Zmax2 = -10000; |
||
421 | X = 0; Y = 0; Z = 0; |
||
489 | killagreg | 422 | speak = 1; |
488 | holgerb | 423 | CompassValueErrorCount = 0; |
489 | killagreg | 424 | if(Compass_CalState != OldCalState) // only once per state |
475 | holgerb | 425 | { |
489 | killagreg | 426 | UART1_PutString("\r\nStarting compass calibration"); |
427 | if(Compass_I2CPort == NCMAG_PORT_EXTERN) |
||
428 | { |
||
429 | if(!NCMAG_Orientation) NCMAG_Orientation = NCMAG_GetOrientationFromAcc(); |
||
430 | UART1_PutString(" - External sensor "); |
||
431 | sprintf(msg, "with orientation: %d ", NCMAG_Orientation); |
||
432 | UART1_PutString(msg); |
||
433 | } |
||
434 | else UART1_PutString(" - Internal sensor "); |
||
483 | holgerb | 435 | } |
254 | killagreg | 436 | break; |
489 | killagreg | 437 | |
254 | killagreg | 438 | case 2: // 2nd step of calibration |
439 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
||
275 | killagreg | 440 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
441 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
||
442 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
||
443 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
||
489 | killagreg | 444 | if(Z < Zmin) { Zmin = Z; } // silent |
475 | holgerb | 445 | else if(Z > Zmax) { Zmax = Z; } |
454 | holgerb | 446 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
254 | killagreg | 447 | break; |
448 | |||
449 | case 3: // 3rd step of calibration |
||
450 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
||
489 | killagreg | 451 | speak = 1; |
254 | killagreg | 452 | break; |
453 | |||
454 | case 4: |
||
455 | // find Min and Max of the Z-Sensor |
||
489 | killagreg | 456 | if(Z < Zmin2) { Zmin2 = Z; BeepTime = 80;} |
457 | else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;} |
||
458 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
||
459 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
||
475 | holgerb | 460 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
461 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
||
454 | holgerb | 462 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
254 | killagreg | 463 | break; |
489 | killagreg | 464 | |
254 | killagreg | 465 | case 5: |
466 | // Save values |
||
467 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
||
468 | { |
||
394 | killagreg | 469 | switch(NCMAG_SensorType) |
470 | { |
||
489 | killagreg | 471 | case TYPE_HMC5843: |
472 | UART1_PutString("\r\nFinished: HMC5843 calibration\n\r"); |
||
473 | MinCalibration = HMC5843_CALIBRATION_RANGE; |
||
474 | break; |
||
394 | killagreg | 475 | |
476 | case TYPE_LSM303DLH: |
||
489 | killagreg | 477 | case TYPE_LSM303DLM: |
590 | holgerb | 478 | case TYPE_LSM303D: |
479 | default: |
||
489 | killagreg | 480 | UART1_PutString("\r\nFinished: LSM303 calibration\n\r"); |
481 | MinCalibration = LSM303_CALIBRATION_RANGE; |
||
482 | break; |
||
394 | killagreg | 483 | } |
342 | holgerb | 484 | if(EarthMagneticStrengthTheoretic) |
489 | killagreg | 485 | { |
486 | MinCalibration = (MinCalibration * EarthMagneticStrengthTheoretic) / 50; |
||
487 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
||
488 | UART1_PutString(msg); |
||
489 | } |
||
342 | holgerb | 490 | else UART1_PutString("without GPS\n\r"); |
339 | holgerb | 491 | |
489 | killagreg | 492 | if(Zmin2 < Zmin) Zmin = Zmin2; |
493 | if(Zmax2 > Zmax) Zmax = Zmax2; |
||
254 | killagreg | 494 | Calibration.MagX.Range = Xmax - Xmin; |
495 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
||
496 | Calibration.MagY.Range = Ymax - Ymin; |
||
497 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
||
498 | Calibration.MagZ.Range = Zmax - Zmin; |
||
499 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
||
488 | holgerb | 500 | if(CompassValueErrorCount) |
489 | killagreg | 501 | { |
488 | holgerb | 502 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
503 | UART1_PutString("\r\nCalibration FAILED - Compass sensor error !!!!\r\n "); |
||
504 | |||
489 | killagreg | 505 | } |
488 | holgerb | 506 | else |
394 | killagreg | 507 | if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration)) |
254 | killagreg | 508 | { |
489 | killagreg | 509 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(Compass_I2CPort); |
270 | killagreg | 510 | BeepTime = 2500; |
342 | holgerb | 511 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
489 | killagreg | 512 | SpeakHoTT = SPEAK_MIKROKOPTER; |
254 | killagreg | 513 | } |
514 | else |
||
515 | { |
||
489 | killagreg | 516 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
339 | holgerb | 517 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
394 | killagreg | 518 | if(Calibration.MagX.Range < MinCalibration) UART1_PutString("X! "); |
519 | if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! "); |
||
520 | if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! "); |
||
330 | holgerb | 521 | UART1_PutString("\r\n"); |
339 | holgerb | 522 | |
254 | killagreg | 523 | // restore old calibration data from eeprom |
489 | killagreg | 524 | NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort); |
254 | killagreg | 525 | } |
330 | holgerb | 526 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
527 | UART1_PutString(msg); |
||
528 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
||
529 | UART1_PutString(msg); |
||
530 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
||
531 | UART1_PutString(msg); |
||
394 | killagreg | 532 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCalibration); |
342 | holgerb | 533 | UART1_PutString(msg); |
254 | killagreg | 534 | } |
535 | break; |
||
489 | killagreg | 536 | |
254 | killagreg | 537 | default: |
489 | killagreg | 538 | break; |
254 | killagreg | 539 | } |
540 | OldCalState = Compass_CalState; |
||
541 | } |
||
542 | |||
242 | killagreg | 543 | // ---------- call back handlers ----------------------------------------- |
544 | |||
545 | // rx data handler for id info request |
||
253 | killagreg | 546 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
254 | killagreg | 547 | { // if number of bytes are matching |
253 | killagreg | 548 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
242 | killagreg | 549 | { |
253 | killagreg | 550 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
551 | } |
||
242 | killagreg | 552 | } |
329 | holgerb | 553 | |
554 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
||
555 | { // if number of bytes are matching |
||
556 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
||
557 | { |
||
558 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
||
559 | } |
||
560 | } |
||
561 | |||
254 | killagreg | 562 | // rx data handler for magnetic sensor raw data |
253 | killagreg | 563 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
590 | holgerb | 564 | { |
565 | u8 endian = 1; |
||
566 | if(NCMAG_SensorType == TYPE_LSM303D) endian = 0; // here the low and high-bytes are mixed |
||
567 | if(RxBufferSize == sizeof(MagRawVector) ) // if number of bytes are matching |
||
243 | killagreg | 568 | { // byte order from big to little endian |
473 | holgerb | 569 | s16 raw, X = 0, Y = 0, Z = 0; |
590 | holgerb | 570 | if(endian) {raw = pRxBuffer[0]<<8; raw+= pRxBuffer[1];} else {raw = (pRxBuffer[1] << 8) + pRxBuffer[0]; raw >>= 3;} |
489 | killagreg | 571 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) X = raw; |
487 | holgerb | 572 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
590 | holgerb | 573 | if(endian) {raw = pRxBuffer[2]<<8; raw+= pRxBuffer[3];} else {raw = (pRxBuffer[3] << 8) + pRxBuffer[2]; raw >>= 3;} |
489 | killagreg | 574 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
330 | holgerb | 575 | { |
473 | holgerb | 576 | if(NCMAG_SensorType == TYPE_LSM303DLM) Z = raw; // here Z and Y are exchanged |
489 | killagreg | 577 | else Y = raw; |
330 | holgerb | 578 | } |
487 | holgerb | 579 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
590 | holgerb | 580 | if(endian) {raw = pRxBuffer[4]<<8; raw+= pRxBuffer[5];} else {raw = (pRxBuffer[5] << 8) + pRxBuffer[4]; raw >>= 3;} |
489 | killagreg | 581 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
330 | holgerb | 582 | { |
473 | holgerb | 583 | if(NCMAG_SensorType == TYPE_LSM303DLM) Y = raw; // here Z and Y are exchanged |
489 | killagreg | 584 | else Z = raw; |
330 | holgerb | 585 | } |
487 | holgerb | 586 | else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data |
489 | killagreg | 587 | // correct compass orientation |
588 | switch(NCMAG_Orientation) |
||
473 | holgerb | 589 | { |
489 | killagreg | 590 | case 0: |
591 | case 1: |
||
592 | default: |
||
593 | // 1:1 Mapping |
||
473 | holgerb | 594 | MagRawVector.X = X; |
595 | MagRawVector.Y = Y; |
||
596 | MagRawVector.Z = Z; |
||
597 | break; |
||
489 | killagreg | 598 | case 2: |
473 | holgerb | 599 | MagRawVector.X = -X; |
600 | MagRawVector.Y = Y; |
||
601 | MagRawVector.Z = -Z; |
||
602 | break; |
||
489 | killagreg | 603 | case 3: |
473 | holgerb | 604 | MagRawVector.X = -Z; |
605 | MagRawVector.Y = Y; |
||
606 | MagRawVector.Z = X; |
||
607 | break; |
||
489 | killagreg | 608 | case 4: |
473 | holgerb | 609 | MagRawVector.X = Z; |
610 | MagRawVector.Y = Y; |
||
611 | MagRawVector.Z = -X; |
||
612 | break; |
||
489 | killagreg | 613 | case 5: |
473 | holgerb | 614 | MagRawVector.X = X; |
615 | MagRawVector.Y = -Z; |
||
616 | MagRawVector.Z = Y; |
||
617 | break; |
||
489 | killagreg | 618 | case 6: |
473 | holgerb | 619 | MagRawVector.X = -X; |
620 | MagRawVector.Y = -Z; |
||
621 | MagRawVector.Z = -Y; |
||
622 | break; |
||
623 | } |
||
242 | killagreg | 624 | } |
254 | killagreg | 625 | if(Compass_CalState || !NCMAG_IsCalibrated) |
284 | killagreg | 626 | { // mark out data invalid |
289 | killagreg | 627 | MagVector.X = MagRawVector.X; |
628 | MagVector.Y = MagRawVector.Y; |
||
629 | MagVector.Z = MagRawVector.Z; |
||
254 | killagreg | 630 | Compass_Heading = -1; |
631 | } |
||
632 | else |
||
633 | { |
||
634 | // update MagVector from MagRaw Vector by Scaling |
||
635 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
||
636 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
||
637 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
||
292 | killagreg | 638 | Compass_CalcHeading(); |
254 | killagreg | 639 | } |
242 | killagreg | 640 | } |
254 | killagreg | 641 | // rx data handler for acceleration raw data |
253 | killagreg | 642 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
489 | killagreg | 643 | { // if number of bytes are matching |
254 | killagreg | 644 | if(RxBufferSize == sizeof(AccRawVector) ) |
489 | killagreg | 645 | { |
646 | // copy from I2C buffer |
||
254 | killagreg | 647 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
489 | killagreg | 648 | // scale and update Acc Vector, at the moment simply 1:1 |
649 | memcpy((u8*)&AccVector, (u8*)&AccRawVector,sizeof(AccRawVector)); |
||
253 | killagreg | 650 | } |
473 | holgerb | 651 | } |
254 | killagreg | 652 | // rx data handler for reading magnetic sensor configuration |
253 | killagreg | 653 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
654 | { // if number of byte are matching |
||
655 | if(RxBufferSize == sizeof(MagConfig) ) |
||
656 | { |
||
657 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
||
658 | } |
||
659 | } |
||
254 | killagreg | 660 | // rx data handler for reading acceleration sensor configuration |
253 | killagreg | 661 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
662 | { // if number of byte are matching |
||
663 | if(RxBufferSize == sizeof(AccConfig) ) |
||
664 | { |
||
665 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
||
666 | } |
||
667 | } |
||
254 | killagreg | 668 | //---------------------------------------------------------------------- |
253 | killagreg | 669 | |
489 | killagreg | 670 | u8 NCMAG_GetOrientationFromAcc(void) |
671 | { |
||
672 | // only if external compass connected |
||
673 | if(Compass_I2CPort != NCMAG_PORT_EXTERN) return(0); |
||
674 | // MK must not be tilted |
||
675 | if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300)) |
||
676 | { |
||
677 | // UART1_PutString("\r\nTilted"); |
||
678 | return(0); |
||
679 | } |
||
680 | // select orientation |
||
681 | if(AccRawVector.Z > 3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn |
||
682 | else |
||
683 | if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn |
||
684 | else |
||
685 | if(AccRawVector.X > 3300) return(3); // Flach - Bestückung Links - Pfeil nach vorn |
||
686 | else |
||
687 | if(AccRawVector.X < -3300) return(4); // Flach - Bestückung rechts - Pfeil nach vorn |
||
688 | else |
||
689 | if(AccRawVector.Y > 3300) return(5); // Stehend - Pfeil nach oben - 'front' nach vorn |
||
690 | else |
||
691 | if(AccRawVector.Y < -3300) return(6); // Stehend - Pfeil nach unten - 'front' nach vorn |
||
692 | return(0); |
||
693 | } |
||
254 | killagreg | 694 | |
695 | // --------------------------------------------------------------------- |
||
253 | killagreg | 696 | u8 NCMAG_SetMagConfig(void) |
697 | { |
||
698 | u8 retval = 0; |
||
489 | killagreg | 699 | |
253 | killagreg | 700 | // try to catch the i2c buffer within 100 ms timeout |
489 | killagreg | 701 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
253 | killagreg | 702 | { |
590 | holgerb | 703 | if(NCMAG_SensorType == TYPE_LSM303D) |
704 | { |
||
253 | killagreg | 705 | u8 TxBytes = 0; |
590 | holgerb | 706 | u8 CfgData[] = { REG_303D_CTRL0 | REG_MASK_AUTOINCREMENT, |
707 | 0, // Ctrl 0 -> Fifo |
||
708 | LSM303D_ACC_RATE_6HZ, // Ctrl 1 -> ACC Update Speed |
||
709 | LSM303D_ACC_SCALE_8g, // Ctrl 2 -> ACC Gain + Filter (0 = 773Hz) |
||
710 | 0, // Ctrl 3 -> Interrupts |
||
711 | 0, // Ctrl 4 -> Interrupts |
||
712 | LSM303D_ODR_RATE_50HZ | LSM303D_TEMP_ENABLE | LSM303D_LOW_RESULUTION,// Ctrl 5 -> Temperature Enable + Magn.Resolution + Magn.Rate |
||
713 | LSM303D_MAG_SCALE_8GA, // Ctrl 6 -> Magn. Scale |
||
714 | 0x80 // Ctrl 7 -> Magn. PowerDown + Filter |
||
715 | }; |
||
716 | |||
717 | TxBytes = sizeof(CfgData); |
||
598 | holgerb | 718 | |
590 | holgerb | 719 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, CfgData, TxBytes, 0, 0)) |
720 | { |
||
721 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
||
722 | { |
||
723 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
||
724 | } |
||
725 | } |
||
726 | } |
||
727 | else |
||
728 | { |
||
729 | u8 TxBytes = 0; |
||
489 | killagreg | 730 | u8 TxData[sizeof(MagConfig) + 3]; |
731 | |||
732 | TxData[TxBytes++] = REG_MAG_CRA; |
||
733 | memcpy(&TxData[TxBytes], (u8*)&MagConfig, sizeof(MagConfig)); |
||
253 | killagreg | 734 | TxBytes += sizeof(MagConfig); |
489 | killagreg | 735 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, 0, 0)) |
253 | killagreg | 736 | { |
489 | killagreg | 737 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
253 | killagreg | 738 | { |
489 | killagreg | 739 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
253 | killagreg | 740 | } |
741 | } |
||
590 | holgerb | 742 | } |
253 | killagreg | 743 | } |
489 | killagreg | 744 | return(retval); |
253 | killagreg | 745 | } |
242 | killagreg | 746 | |
253 | killagreg | 747 | // ---------------------------------------------------------------------------------------- |
590 | holgerb | 748 | /* |
253 | killagreg | 749 | u8 NCMAG_GetMagConfig(void) |
242 | killagreg | 750 | { |
253 | killagreg | 751 | u8 retval = 0; |
252 | killagreg | 752 | // try to catch the i2c buffer within 100 ms timeout |
489 | killagreg | 753 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
242 | killagreg | 754 | { |
253 | killagreg | 755 | u8 TxBytes = 0; |
489 | killagreg | 756 | u8 TxData[3]; |
757 | TxData[TxBytes++] = REG_MAG_CRA; |
||
758 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
||
248 | killagreg | 759 | { |
489 | killagreg | 760 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
252 | killagreg | 761 | { |
489 | killagreg | 762 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
252 | killagreg | 763 | } |
248 | killagreg | 764 | } |
242 | killagreg | 765 | } |
489 | killagreg | 766 | return(retval); |
242 | killagreg | 767 | } |
590 | holgerb | 768 | */ |
242 | killagreg | 769 | // ---------------------------------------------------------------------------------------- |
253 | killagreg | 770 | u8 NCMAG_SetAccConfig(void) |
242 | killagreg | 771 | { |
252 | killagreg | 772 | u8 retval = 0; |
489 | killagreg | 773 | // try to catch the i2c buffer within 50 ms timeout |
774 | if(I2CBus_LockBuffer(Compass_I2CPort, 50)) |
||
242 | killagreg | 775 | { |
253 | killagreg | 776 | u8 TxBytes = 0; |
489 | killagreg | 777 | u8 TxData[sizeof(AccConfig) + 3]; |
590 | holgerb | 778 | TxData[TxBytes++] = REG_ACC_CTRL1|REG_MASK_AUTOINCREMENT; |
489 | killagreg | 779 | memcpy(&TxData[TxBytes], (u8*)&AccConfig, sizeof(AccConfig)); |
253 | killagreg | 780 | TxBytes += sizeof(AccConfig); |
489 | killagreg | 781 | if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, 0, 0)) |
253 | killagreg | 782 | { |
489 | killagreg | 783 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 50)) |
253 | killagreg | 784 | { |
489 | killagreg | 785 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
253 | killagreg | 786 | } |
787 | } |
||
788 | } |
||
489 | killagreg | 789 | return(retval); |
253 | killagreg | 790 | } |
791 | |||
792 | // ---------------------------------------------------------------------------------------- |
||
793 | u8 NCMAG_GetAccConfig(void) |
||
794 | { |
||
795 | u8 retval = 0; |
||
796 | // try to catch the i2c buffer within 100 ms timeout |
||
489 | killagreg | 797 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
253 | killagreg | 798 | { |
799 | u8 TxBytes = 0; |
||
489 | killagreg | 800 | u8 TxData[3]; |
590 | holgerb | 801 | TxData[TxBytes++] = REG_ACC_CTRL1|REG_MASK_AUTOINCREMENT; |
489 | killagreg | 802 | if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
253 | killagreg | 803 | { |
489 | killagreg | 804 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
253 | killagreg | 805 | { |
489 | killagreg | 806 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
253 | killagreg | 807 | } |
808 | } |
||
809 | } |
||
489 | killagreg | 810 | return(retval); |
253 | killagreg | 811 | } |
812 | |||
813 | // ---------------------------------------------------------------------------------------- |
||
814 | u8 NCMAG_GetIdentification(void) |
||
815 | { |
||
816 | u8 retval = 0; |
||
590 | holgerb | 817 | if(NCMAG_SensorType == TYPE_LSM303D) return(1); |
253 | killagreg | 818 | // try to catch the i2c buffer within 100 ms timeout |
489 | killagreg | 819 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
253 | killagreg | 820 | { |
489 | killagreg | 821 | u8 TxBytes = 0; |
822 | u8 TxData[3]; |
||
253 | killagreg | 823 | NCMAG_Identification.A = 0xFF; |
824 | NCMAG_Identification.B = 0xFF; |
||
825 | NCMAG_Identification.C = 0xFF; |
||
489 | killagreg | 826 | TxData[TxBytes++] = REG_MAG_IDA; |
248 | killagreg | 827 | // initiate transmission |
489 | killagreg | 828 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
248 | killagreg | 829 | { |
489 | killagreg | 830 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
252 | killagreg | 831 | { |
489 | killagreg | 832 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
252 | killagreg | 833 | } |
248 | killagreg | 834 | } |
242 | killagreg | 835 | } |
253 | killagreg | 836 | return(retval); |
242 | killagreg | 837 | } |
838 | |||
329 | holgerb | 839 | u8 NCMAG_GetIdentification_Sub(void) |
840 | { |
||
841 | u8 retval = 0; |
||
842 | // try to catch the i2c buffer within 100 ms timeout |
||
489 | killagreg | 843 | if(I2CBus_LockBuffer(Compass_I2CPort, 100)) |
329 | holgerb | 844 | { |
489 | killagreg | 845 | u8 TxBytes = 0; |
846 | u8 TxData[3]; |
||
329 | holgerb | 847 | NCMAG_Identification2.Sub = 0xFF; |
489 | killagreg | 848 | TxData[TxBytes++] = REG_MAG_IDF; |
329 | holgerb | 849 | // initiate transmission |
489 | killagreg | 850 | if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
329 | holgerb | 851 | { |
489 | killagreg | 852 | if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100)) |
329 | holgerb | 853 | { |
489 | killagreg | 854 | if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1; |
329 | holgerb | 855 | } |
856 | } |
||
857 | } |
||
858 | return(retval); |
||
859 | } |
||
860 | |||
861 | |||
253 | killagreg | 862 | // ---------------------------------------------------------------------------------------- |
489 | killagreg | 863 | void NCMAG_GetMagVector(u8 timeout) |
253 | killagreg | 864 | { |
489 | killagreg | 865 | // try to catch the I2C buffer within timeout ms |
866 | if(I2CBus_LockBuffer(Compass_I2CPort, timeout)) |
||
253 | killagreg | 867 | { |
489 | killagreg | 868 | u8 TxBytes = 0; |
869 | u8 TxData[3]; |
||
253 | killagreg | 870 | // set register pointer |
590 | holgerb | 871 | if(NCMAG_SensorType == TYPE_LSM303D) TxData[TxBytes++] = ADR_LSM303D_MAG | REG_MASK_AUTOINCREMENT; |
872 | else TxData[TxBytes++] = REG_MAG_DATAX_MSB; |
||
253 | killagreg | 873 | // initiate transmission |
489 | killagreg | 874 | I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
253 | killagreg | 875 | } |
876 | } |
||
877 | |||
242 | killagreg | 878 | //---------------------------------------------------------------- |
473 | holgerb | 879 | void NCMAG_GetAccVector(u8 timeout) |
243 | killagreg | 880 | { |
489 | killagreg | 881 | // try to catch the I2C buffer within timeout ms |
882 | if(I2CBus_LockBuffer(Compass_I2CPort, timeout)) |
||
243 | killagreg | 883 | { |
489 | killagreg | 884 | u8 TxBytes = 0; |
885 | u8 TxData[3]; |
||
243 | killagreg | 886 | // set register pointer |
590 | holgerb | 887 | if(NCMAG_SensorType == TYPE_LSM303D) |
888 | { |
||
889 | TxData[TxBytes++] = REG_ACC_X_LSB | REG_MASK_AUTOINCREMENT; |
||
890 | I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
||
891 | } |
||
892 | else |
||
893 | { |
||
894 | TxData[TxBytes++] = REG_ACC_X_LSB | REG_MASK_AUTOINCREMENT; |
||
895 | // initiate transmission |
||
896 | I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
||
897 | } |
||
243 | killagreg | 898 | } |
899 | } |
||
900 | |||
330 | holgerb | 901 | //---------------------------------------------------------------- |
489 | killagreg | 902 | u8 NCMAG_ConfigureSensor(void) |
330 | holgerb | 903 | { |
394 | killagreg | 904 | switch(NCMAG_SensorType) |
330 | holgerb | 905 | { |
394 | killagreg | 906 | case TYPE_HMC5843: |
590 | holgerb | 907 | MagConfig.cra = HMC5843_CRA_RATE_50HZ|CRA_MODE_NORMAL; |
908 | MagConfig.crb = HMC5843_CRB_GAIN_15GA; |
||
909 | MagConfig.mode = MODE_CONTINUOUS; |
||
330 | holgerb | 910 | break; |
911 | |||
394 | killagreg | 912 | case TYPE_LSM303DLH: |
913 | case TYPE_LSM303DLM: |
||
590 | holgerb | 914 | // MagConfig.crb = LSM303DLH_CRB_GAIN_19GA; |
915 | MagConfig.crb = LSM303DLH_CRB_GAIN_40GA; // seit 2.03 -> 2.2014 |
||
916 | MagConfig.cra = LSM303DLH_CRA_RATE_75HZ|CRA_MODE_NORMAL;; |
||
917 | MagConfig.mode = MODE_CONTINUOUS; |
||
330 | holgerb | 918 | break; |
590 | holgerb | 919 | case TYPE_LSM303D: |
920 | // -> see defined data in NCMAG_SetMagConfig(); |
||
921 | break; |
||
330 | holgerb | 922 | default: |
394 | killagreg | 923 | return(0); |
330 | holgerb | 924 | } |
394 | killagreg | 925 | return(NCMAG_SetMagConfig()); |
330 | holgerb | 926 | } |
927 | |||
395 | holgerb | 928 | |
394 | killagreg | 929 | //---------------------------------------------------------------- |
930 | u8 NCMAG_Init_ACCSensor(void) |
||
931 | { |
||
590 | holgerb | 932 | if(NCMAG_SensorType == TYPE_LSM303D) |
933 | { |
||
934 | return(NCMAG_SetMagConfig()); |
||
935 | } |
||
936 | else |
||
937 | { |
||
395 | holgerb | 938 | AccConfig.ctrl_1 = ACC_CRTL1_PM_NORMAL|ACC_CRTL1_DR_50HZ|ACC_CRTL1_XEN|ACC_CRTL1_YEN|ACC_CRTL1_ZEN; |
473 | holgerb | 939 | AccConfig.ctrl_2 = 0; |
394 | killagreg | 940 | AccConfig.ctrl_3 = 0x00; |
397 | holgerb | 941 | AccConfig.ctrl_4 = ACC_CTRL4_BDU | ACC_CTRL4_FS_8G; |
394 | killagreg | 942 | AccConfig.ctrl_5 = ACC_CTRL5_STW_OFF; |
943 | return(NCMAG_SetAccConfig()); |
||
590 | holgerb | 944 | } |
394 | killagreg | 945 | } |
253 | killagreg | 946 | // -------------------------------------------------------- |
480 | holgerb | 947 | void NCMAG_Update(u8 init) |
243 | killagreg | 948 | { |
292 | killagreg | 949 | static u32 TimerUpdate = 0; |
419 | holgerb | 950 | static s8 send_config = 0; |
394 | killagreg | 951 | u32 delay = 20; |
489 | killagreg | 952 | |
480 | holgerb | 953 | if(init) TimerUpdate = SetDelay(10); |
954 | |||
489 | killagreg | 955 | if( (I2CBus(Compass_I2CPort)->State == I2C_STATE_UNDEF) /*|| !NCMAG_Present*/ ) |
254 | killagreg | 956 | { |
957 | Compass_Heading = -1; |
||
326 | holgerb | 958 | DebugOut.Analog[14]++; // count I2C error |
480 | holgerb | 959 | TimerUpdate = SetDelay(10); |
254 | killagreg | 960 | return; |
961 | } |
||
489 | killagreg | 962 | if(CheckDelay(TimerUpdate)) |
243 | killagreg | 963 | { |
394 | killagreg | 964 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
965 | if(++send_config == 25) // 500ms |
||
966 | { |
||
419 | holgerb | 967 | send_config = -25; // next try after 1 second |
489 | killagreg | 968 | NCMAG_ConfigureSensor(); |
419 | holgerb | 969 | TimerUpdate = SetDelay(20); // back into the old time-slot |
394 | killagreg | 970 | } |
321 | holgerb | 971 | else |
972 | { |
||
473 | holgerb | 973 | static u8 s = 0; |
394 | killagreg | 974 | // check for new calibration state |
975 | Compass_UpdateCalState(); |
||
976 | if(Compass_CalState) NCMAG_Calibrate(); |
||
489 | killagreg | 977 | |
394 | killagreg | 978 | // in case of LSM303 type |
979 | switch(NCMAG_SensorType) |
||
980 | { |
||
489 | killagreg | 981 | case TYPE_HMC5843: |
982 | delay = 20; // next cycle after 20 ms |
||
983 | NCMAG_GetMagVector(5); |
||
394 | killagreg | 984 | break; |
985 | case TYPE_LSM303DLH: |
||
986 | case TYPE_LSM303DLM: |
||
590 | holgerb | 987 | case TYPE_LSM303D: |
492 | killagreg | 988 | delay = 20; // next cycle after 20 ms |
489 | killagreg | 989 | if(s-- || (Compass_I2CPort == NCMAG_PORT_INTERN)) |
990 | { |
||
991 | NCMAG_GetMagVector(5); |
||
992 | } |
||
993 | else // having an external compass, read every 50th cycle the ACC vec |
||
994 | { // try to initialize if no data are there |
||
995 | if((AccRawVector.X + AccRawVector.Y + AccRawVector.Z) == 0) NCMAG_Init_ACCSensor(); |
||
996 | // get new data |
||
997 | NCMAG_GetAccVector(5); |
||
998 | delay = 10; // next cycle after 10 ms |
||
999 | s = 40; //reset downconter about 0,8 sec |
||
590 | holgerb | 1000 | /* |
613 | holgerb | 1001 | DebugOut.Analog[] = AccVector.X; |
1002 | DebugOut.Analog[] = AccVector.Y; |
||
1003 | DebugOut.Analog[] = AccVector.Z; |
||
590 | holgerb | 1004 | s = 2; |
1005 | */ |
||
489 | killagreg | 1006 | } |
1007 | break; |
||
394 | killagreg | 1008 | } |
419 | holgerb | 1009 | if(send_config == 24) TimerUpdate = SetDelay(15); // next event is the re-configuration |
394 | killagreg | 1010 | else TimerUpdate = SetDelay(delay); // every 20 ms are 50 Hz |
321 | holgerb | 1011 | } |
243 | killagreg | 1012 | } |
1013 | } |
||
1014 | |||
330 | holgerb | 1015 | |
254 | killagreg | 1016 | // -------------------------------------------------------- |
253 | killagreg | 1017 | u8 NCMAG_SelfTest(void) |
243 | killagreg | 1018 | { |
266 | holgerb | 1019 | u8 msg[64]; |
275 | killagreg | 1020 | static u8 done = 0; |
266 | holgerb | 1021 | |
287 | holgerb | 1022 | if(done) return(1); // just make it once |
489 | killagreg | 1023 | |
271 | holgerb | 1024 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
243 | killagreg | 1025 | u32 time; |
253 | killagreg | 1026 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
1027 | s16 xscale, yscale, zscale, scale_min, scale_max; |
||
1028 | u8 crb_gain, cra_rate; |
||
1029 | u8 i = 0, retval = 1; |
||
243 | killagreg | 1030 | |
394 | killagreg | 1031 | switch(NCMAG_SensorType) |
253 | killagreg | 1032 | { |
394 | killagreg | 1033 | case TYPE_HMC5843: |
339 | holgerb | 1034 | crb_gain = HMC5843_CRB_GAIN_15GA; |
253 | killagreg | 1035 | cra_rate = HMC5843_CRA_RATE_50HZ; |
1036 | xscale = HMC5843_TEST_XSCALE; |
||
1037 | yscale = HMC5843_TEST_YSCALE; |
||
1038 | zscale = HMC5843_TEST_ZSCALE; |
||
1039 | break; |
||
1040 | |||
394 | killagreg | 1041 | case TYPE_LSM303DLH: |
500 | holgerb | 1042 | // crb_gain = LSM303DLH_CRB_GAIN_19GA; |
1043 | crb_gain = LSM303DLH_CRB_GAIN_40GA; // seit 2.03 -> 2.2014 |
||
253 | killagreg | 1044 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
1045 | xscale = LSM303DLH_TEST_XSCALE; |
||
1046 | yscale = LSM303DLH_TEST_YSCALE; |
||
1047 | zscale = LSM303DLH_TEST_ZSCALE; |
||
1048 | break; |
||
1049 | |||
394 | killagreg | 1050 | case TYPE_LSM303DLM: |
489 | killagreg | 1051 | // does not support self test feature |
1052 | done = 1; |
||
1053 | return(1); // always return success |
||
394 | killagreg | 1054 | break; |
590 | holgerb | 1055 | case TYPE_LSM303D: |
1056 | done = 1; |
||
1057 | return(1); // always return success |
||
1058 | break; |
||
253 | killagreg | 1059 | default: |
394 | killagreg | 1060 | return(0); |
253 | killagreg | 1061 | } |
1062 | |||
1063 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
||
1064 | MagConfig.crb = crb_gain; |
||
1065 | MagConfig.mode = MODE_CONTINUOUS; |
||
1066 | // activate positive bias field |
||
1067 | NCMAG_SetMagConfig(); |
||
251 | killagreg | 1068 | // wait for stable readings |
1069 | time = SetDelay(50); |
||
1070 | while(!CheckDelay(time)); |
||
243 | killagreg | 1071 | // averaging |
253 | killagreg | 1072 | #define AVERAGE 20 |
1073 | for(i = 0; i<AVERAGE; i++) |
||
243 | killagreg | 1074 | { |
489 | killagreg | 1075 | NCMAG_GetMagVector(5); |
243 | killagreg | 1076 | time = SetDelay(20); |
1077 | while(!CheckDelay(time)); |
||
254 | killagreg | 1078 | XMax += MagRawVector.X; |
1079 | YMax += MagRawVector.Y; |
||
1080 | ZMax += MagRawVector.Z; |
||
243 | killagreg | 1081 | } |
253 | killagreg | 1082 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
1083 | // activate positive bias field |
||
1084 | NCMAG_SetMagConfig(); |
||
251 | killagreg | 1085 | // wait for stable readings |
1086 | time = SetDelay(50); |
||
1087 | while(!CheckDelay(time)); |
||
243 | killagreg | 1088 | // averaging |
253 | killagreg | 1089 | for(i = 0; i < AVERAGE; i++) |
243 | killagreg | 1090 | { |
489 | killagreg | 1091 | NCMAG_GetMagVector(5); |
243 | killagreg | 1092 | time = SetDelay(20); |
1093 | while(!CheckDelay(time)); |
||
254 | killagreg | 1094 | XMin += MagRawVector.X; |
1095 | YMin += MagRawVector.Y; |
||
1096 | ZMin += MagRawVector.Z; |
||
243 | killagreg | 1097 | } |
1098 | // setup final configuration |
||
253 | killagreg | 1099 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
1100 | // activate positive bias field |
||
1101 | NCMAG_SetMagConfig(); |
||
266 | holgerb | 1102 | // check scale for all axes |
243 | killagreg | 1103 | // prepare scale limits |
253 | killagreg | 1104 | LIMITS(xscale, scale_min, scale_max); |
267 | holgerb | 1105 | xscale = (XMax - XMin)/(2*AVERAGE); |
489 | killagreg | 1106 | if((xscale > scale_max) || (xscale < scale_min)) |
394 | killagreg | 1107 | { |
1108 | retval = 0; |
||
1109 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
||
1110 | UART1_PutString(msg); |
||
1111 | } |
||
267 | holgerb | 1112 | LIMITS(yscale, scale_min, scale_max); |
266 | holgerb | 1113 | yscale = (YMax - YMin)/(2*AVERAGE); |
489 | killagreg | 1114 | if((yscale > scale_max) || (yscale < scale_min)) |
394 | killagreg | 1115 | { |
1116 | retval = 0; |
||
1117 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
||
1118 | UART1_PutString(msg); |
||
1119 | } |
||
267 | holgerb | 1120 | LIMITS(zscale, scale_min, scale_max); |
266 | holgerb | 1121 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
489 | killagreg | 1122 | if((zscale > scale_max) || (zscale < scale_min)) |
394 | killagreg | 1123 | { |
1124 | retval = 0; |
||
1125 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
||
1126 | UART1_PutString(msg); |
||
1127 | } |
||
275 | killagreg | 1128 | done = retval; |
253 | killagreg | 1129 | return(retval); |
243 | killagreg | 1130 | } |
1131 | |||
1132 | |||
489 | killagreg | 1133 | void NCMAG_CheckOrientation(void) |
1134 | { // only for external sensor |
||
1135 | if(Compass_I2CPort == NCMAG_PORT_EXTERN) |
||
1136 | { |
||
1137 | NCMAG_Orientation = NCMAG_GetOrientationFromAcc(); |
||
1138 | if(NCMAG_Orientation != (Calibration.Version>>4)) NCMAG_IsCalibrated = 0; |
||
1139 | else NCMAG_IsCalibrated = 1; |
||
1140 | } |
||
465 | ingob | 1141 | } |
1142 | //---------------------------------------------------------------- |
||
253 | killagreg | 1143 | u8 NCMAG_Init(void) |
242 | killagreg | 1144 | { |
489 | killagreg | 1145 | MagRawVector.X = 0; |
1146 | MagRawVector.Y = 0; |
||
1147 | MagRawVector.Z = 0; |
||
1148 | AccRawVector.X = 0; |
||
1149 | AccRawVector.Y = 0; |
||
1150 | AccRawVector.Z = 0; |
||
483 | holgerb | 1151 | |
489 | killagreg | 1152 | if(NCMAG_Present) // do only short init ! , full init was called before |
472 | holgerb | 1153 | { |
489 | killagreg | 1154 | // reset I2C Bus |
492 | killagreg | 1155 | //I2CBus_Deinit(Compass_I2CPort); |
1156 | //I2CBus_Init(Compass_I2CPort); |
||
489 | killagreg | 1157 | // try to reconfigure senor |
1158 | NCMAG_ConfigureSensor(); |
||
492 | killagreg | 1159 | //NCMAG_Update(1); |
472 | holgerb | 1160 | } |
489 | killagreg | 1161 | else // full init |
472 | holgerb | 1162 | { |
489 | killagreg | 1163 | u8 msg[64]; |
1164 | u8 retval = 0; |
||
1165 | u8 repeat = 0; |
||
473 | holgerb | 1166 | |
489 | killagreg | 1167 | //-------------------------------------------- |
1168 | // search external sensor first |
||
1169 | //-------------------------------------------- |
||
1170 | Compass_I2CPort = NCMAG_PORT_EXTERN; |
||
590 | holgerb | 1171 | retval = 0; |
489 | killagreg | 1172 | // get id bytes |
1173 | for(repeat = 0; repeat < 5; repeat++) |
||
1174 | { |
||
590 | holgerb | 1175 | if(NCMAG_GetIdentification_Sub()) break; |
1176 | UART1_PutString("="); |
||
1177 | } |
||
1178 | //sprintf(msg, "(WhoAmI=%02x)", NCMAG_Identification2.Sub); |
||
1179 | //UART1_PutString(msg); |
||
1180 | if(NCMAG_Identification2.Sub == MAG_IDF_LSM303D) |
||
1181 | { |
||
1182 | NCMAG_SensorType = TYPE_LSM303D; |
||
1183 | NCMAG_ConfigureSensor(); |
||
1184 | retval = 1; |
||
1185 | } |
||
1186 | else |
||
1187 | { // External Sensor not LSM303D |
||
1188 | for(repeat = 0; repeat < 3; repeat++) // does it have ACC? |
||
1189 | { |
||
489 | killagreg | 1190 | retval = NCMAG_GetAccConfig(); // only the external sensor with ACC is supported |
1191 | if(retval) break; // break loop on success |
||
1192 | UART1_PutString("_"); |
||
1193 | } |
||
1194 | // Extenal sensor not found? |
||
1195 | if(!retval) |
||
1196 | { |
||
1197 | // search internal sensor afterwards |
||
1198 | UART1_PutString(" internal sensor"); |
||
1199 | Compass_I2CPort = NCMAG_PORT_INTERN; |
||
1200 | } |
||
1201 | else |
||
1202 | { |
||
1203 | UART1_PutString(" external sensor"); |
||
1204 | Compass_I2CPort = NCMAG_PORT_EXTERN; |
||
1205 | } |
||
1206 | //------------------------------------------- |
||
1207 | NCMAG_Present = 0; |
||
1208 | NCMAG_SensorType = TYPE_HMC5843; // assuming having an HMC5843 |
||
590 | holgerb | 1209 | |
489 | killagreg | 1210 | for(repeat = 0; repeat < 3; repeat++) |
394 | killagreg | 1211 | { |
590 | holgerb | 1212 | if(NCMAG_GetIdentification_Sub()) break; |
1213 | UART1_PutString("="); |
||
1214 | } |
||
1215 | if(NCMAG_Identification2.Sub == MAG_IDF_LSM303D) // internal Sensor the LSM303D? |
||
1216 | { |
||
1217 | NCMAG_SensorType = TYPE_LSM303D; |
||
1218 | NCMAG_ConfigureSensor(); |
||
1219 | retval = 1; |
||
1220 | } |
||
1221 | else |
||
1222 | for(repeat = 0; repeat < 3; repeat++) // polling for LSM303DLH/DLM option by ACC address ack |
||
1223 | { |
||
489 | killagreg | 1224 | retval = NCMAG_GetAccConfig(); |
394 | killagreg | 1225 | if(retval) break; // break loop on success |
480 | holgerb | 1226 | } |
590 | holgerb | 1227 | if(retval) // it is a sensor with ACC |
394 | killagreg | 1228 | { |
590 | holgerb | 1229 | switch(NCMAG_Identification2.Sub) |
1230 | { |
||
1231 | case MAG_IDF_LSM303DLM: NCMAG_SensorType = TYPE_LSM303DLM; |
||
1232 | NCMAG_Init_ACCSensor(); |
||
1233 | break; |
||
1234 | case MAG_IDF_LSM303DLH: NCMAG_SensorType = TYPE_LSM303DLH; |
||
1235 | NCMAG_Init_ACCSensor(); |
||
1236 | break; |
||
1237 | case MAG_IDF_LSM303D: NCMAG_SensorType = TYPE_LSM303D; |
||
1238 | break; |
||
1239 | } |
||
489 | killagreg | 1240 | } |
1241 | // get id bytes |
||
1242 | retval = 0; |
||
1243 | for(repeat = 0; repeat < 3; repeat++) |
||
329 | holgerb | 1244 | { |
489 | killagreg | 1245 | retval = NCMAG_GetIdentification(); |
1246 | if(retval) break; // break loop on success |
||
329 | holgerb | 1247 | } |
590 | holgerb | 1248 | } |
489 | killagreg | 1249 | // if we got an answer to id request |
1250 | if(retval) |
||
242 | killagreg | 1251 | { |
489 | killagreg | 1252 | u8 n1[] = "\n\r HMC5843"; |
1253 | u8 n2[] = "\n\r LSM303DLH"; |
||
1254 | u8 n3[] = "\n\r LSM303DLM"; |
||
590 | holgerb | 1255 | u8 n4[] = "\n\r LSM303D"; |
1256 | u8 n5[] = "\n\r ??"; |
||
1257 | u8* pn = n5; |
||
483 | holgerb | 1258 | |
489 | killagreg | 1259 | switch(NCMAG_SensorType) |
394 | killagreg | 1260 | { |
489 | killagreg | 1261 | case TYPE_HMC5843: |
1262 | pn = n1; |
||
1263 | break; |
||
1264 | case TYPE_LSM303DLH: |
||
1265 | pn = n2; |
||
1266 | break; |
||
1267 | case TYPE_LSM303DLM: |
||
1268 | pn = n3; |
||
1269 | break; |
||
590 | holgerb | 1270 | case TYPE_LSM303D: |
1271 | pn = n4; |
||
1272 | break; |
||
394 | killagreg | 1273 | } |
489 | killagreg | 1274 | |
1275 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
||
1276 | UART1_PutString(msg); |
||
590 | holgerb | 1277 | if (( (NCMAG_Identification.A == MAG_IDA) |
489 | killagreg | 1278 | && (NCMAG_Identification.B == MAG_IDB) |
590 | holgerb | 1279 | && (NCMAG_Identification.C == MAG_IDC)) |
1280 | || NCMAG_SensorType == TYPE_LSM303D) |
||
489 | killagreg | 1281 | { |
1282 | NCMAG_Present = 1; |
||
1283 | |||
1284 | if(EEPROM_Init()) |
||
1285 | { |
||
1286 | NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort); |
||
1287 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
||
1288 | } |
||
1289 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
||
1290 | |||
1291 | // in case of an external sensor, try to get the orientation by acc readings |
||
1292 | if(Compass_I2CPort == NCMAG_PORT_EXTERN) |
||
1293 | { |
||
1294 | // try to get orientation by acc sensor values |
||
1295 | for(repeat = 0; repeat < 100; repeat++) |
||
1296 | { |
||
1297 | NCMAG_GetAccVector(10); // only the sensor with ACC is supported |
||
1298 | NCMAG_Orientation = NCMAG_GetOrientationFromAcc(); |
||
1299 | if(NCMAG_Orientation && (NCMAG_Orientation == Calibration.Version >> 4)) break; |
||
1300 | } |
||
1301 | // check orientation result if available |
||
1302 | sprintf(msg, "\r\n Orientation: "); |
||
1303 | UART1_PutString(msg); |
||
1304 | if(NCMAG_Orientation) |
||
1305 | { |
||
1306 | sprintf(msg, "%d ", NCMAG_Orientation); |
||
1307 | UART1_PutString(msg); |
||
1308 | if(NCMAG_IsCalibrated) // check against calibration data orientation |
||
1309 | { |
||
1310 | if(NCMAG_Orientation != Calibration.Version >> 4) |
||
1311 | { |
||
1312 | sprintf(msg, "\n\r Warning: calibrated orientation was %d !",Calibration.Version >> 4); |
||
1313 | UART1_PutString(msg); |
||
1314 | } |
||
1315 | } |
||
1316 | } |
||
1317 | else |
||
1318 | { |
||
1319 | UART1_PutString("unknown!"); |
||
1320 | } |
||
1321 | } |
||
1322 | |||
1323 | |||
1324 | // perform self test |
||
1325 | if(!NCMAG_SelfTest()) |
||
1326 | { |
||
1327 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
||
1328 | LED_RED_ON; |
||
1329 | //NCMAG_IsCalibrated = 0; |
||
1330 | } |
||
1331 | else UART1_PutString("\r\n Selftest ok"); |
||
1332 | |||
1333 | // initialize magnetic sensor configuration |
||
1334 | NCMAG_ConfigureSensor(); |
||
1335 | } |
||
1336 | else |
||
1337 | { |
||
1338 | UART1_PutString("\n\r Not compatible!"); |
||
1339 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
||
329 | holgerb | 1340 | LED_RED_ON; |
489 | killagreg | 1341 | } |
242 | killagreg | 1342 | } |
489 | killagreg | 1343 | else // nothing found |
242 | killagreg | 1344 | { |
489 | killagreg | 1345 | NCMAG_SensorType = TYPE_NONE; |
508 | holgerb | 1346 | UART1_PutString(" not found!"); |
242 | killagreg | 1347 | } |
1348 | } |
||
253 | killagreg | 1349 | return(NCMAG_Present); |
242 | killagreg | 1350 | } |
1351 |