Rev 243 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
242 | killagreg | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) 2010 Ingo Busker, Holger Buss |
||
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
||
7 | // + FOR NON COMMERCIAL USE ONLY |
||
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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. |
||
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
15 | // + Verkauf von Luftbildaufnahmen, usw. |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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 |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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" |
||
22 | // + eindeutig als Ursprung verlinkt werden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
25 | // + Benutzung auf eigene Gefahr |
||
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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 |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
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 |
||
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 |
||
38 | // + for non-commercial use (directly or indirectly) |
||
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
40 | // + with our written permission |
||
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
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 |
||
44 | // |
||
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 |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
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 |
||
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 |
||
55 | // + POSSIBILITY OF SUCH DAMAGE. |
||
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
57 | #include <string.h> |
||
58 | #include "91x_lib.h" |
||
59 | #include "hmc5843.h" |
||
60 | #include "i2c.h" |
||
61 | #include "timer1.h" |
||
62 | #include "led.h" |
||
63 | #include "spi_slave.h" |
||
64 | #include "uart1.h" |
||
65 | #include "compass.h" |
||
66 | |||
67 | #define HMC5843_SLAVE_ADDRESS 0x3C // i2C slave address |
||
68 | |||
69 | u8 HMC5843_Present = 0; |
||
70 | |||
71 | #define REGISTER_CRA 0 |
||
72 | #define REGISTER_CRB 1 |
||
73 | #define REGISTER_MODE 2 |
||
74 | #define REGISTER_DATAX_MSB 3 |
||
75 | #define REGISTER_DATAX_LSB 4 |
||
76 | #define REGISTER_DATAY_MSB 5 |
||
77 | #define REGISTER_DATAY_LSB 6 |
||
78 | #define REGISTER_DATAZ_MSB 7 |
||
79 | #define REGISTER_DATAZ_LSB 8 |
||
80 | #define REGISTER_STATUS 9 |
||
81 | #define REGISTER_IDA 10 |
||
82 | #define REGISTER_IDB 11 |
||
83 | #define REGISTER_IDC 12 |
||
84 | |||
85 | |||
86 | #define HMC5843_IDA 0x48 |
||
87 | #define HMC5843_IDB 0x34 |
||
88 | #define HMC5843_IDC 0x33 |
||
89 | |||
90 | // bit mask for rate |
||
91 | #define HMC5843_CRA_RATE_MASK 0x1C |
||
92 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
||
93 | #define HMC5843_CRA_RATE_1HZ 0x04 |
||
94 | #define HMC5843_CRA_RATE_2HZ 0x08 |
||
95 | #define HMC5843_CRA_RATE_5HZ 0x0C |
||
96 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
||
97 | #define HMC5843_CRA_RATE_20HZ 0x14 |
||
98 | #define HMC5843_CRA_RATE_50HZ 0x18 |
||
99 | |||
100 | // bit mask for mode |
||
101 | #define HMC5843_CRA_MODE_MASK 0x03 |
||
102 | #define HMC5843_CRA_MODE_NORMAL 0x00 //default |
||
103 | #define HMC5843_CRA_MODE_POSBIAS 0x01 |
||
104 | #define HMC5843_CRA_MODE_NEGBIAS 0x02 |
||
105 | #define HMC5843_CRA_MODE_SELFTEST 0x03 |
||
106 | |||
107 | // bit mask for gain |
||
108 | #define HMC5843_CRB_GAIN_MASK 0xE0 |
||
109 | #define HMC5843_CRB_GAIN_07GA 0x00 |
||
110 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
||
111 | #define HMC5843_CRB_GAIN_15GA 0x40 |
||
112 | #define HMC5843_CRB_GAIN_20GA 0x60 |
||
113 | #define HMC5843_CRB_GAIN_32GA 0x80 |
||
114 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
||
115 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
||
116 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
||
117 | |||
118 | // bit mask for measurement mode |
||
119 | #define HMC5843_MODE_MASK 0xFF |
||
120 | #define HMC5843_MODE_CONTINUOUS 0x00 |
||
121 | #define HMC5843_MODE_SINGLE 0x01 // default |
||
122 | #define HMC5843_MODE_IDLE 0x02 |
||
123 | #define HMC5843_MODE_SLEEP 0x03 |
||
124 | |||
125 | typedef struct |
||
126 | { |
||
127 | u8 A; |
||
128 | u8 B; |
||
129 | u8 C; |
||
130 | } __attribute__((packed)) HMC5843_Identification_t; |
||
131 | |||
132 | volatile HMC5843_Identification_t HMC5843_Identification; |
||
133 | |||
134 | |||
135 | |||
136 | // ---------- call back handlers ----------------------------------------- |
||
137 | |||
138 | // rx data handler for id info request |
||
139 | void HMC5843_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
||
140 | { // if number of byte are matching |
||
141 | if(RxBufferSize == sizeof(HMC5843_Identification) ) |
||
142 | { |
||
143 | memcpy((u8 *)&HMC5843_Identification, pRxBuffer, sizeof(HMC5843_Identification)); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | // rx data handler for mag vector request |
||
148 | void HMC5843_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
||
149 | { // if number of byte are matching |
||
150 | if(RxBufferSize == sizeof(MagVector) ) |
||
151 | { |
||
152 | MagVector.X = pRxBuffer[0]<<8; |
||
153 | MagVector.X+= pRxBuffer[1]; |
||
154 | MagVector.Y = pRxBuffer[2]<<8; |
||
155 | MagVector.Y+= pRxBuffer[3]; |
||
156 | MagVector.Z = pRxBuffer[4]<<8; |
||
157 | MagVector.Z+= pRxBuffer[5]; |
||
158 | } |
||
159 | // tbd. calculate heading from mag vector and attitude |
||
160 | CompassHeading = 50; // tbd. |
||
161 | } |
||
162 | |||
163 | |||
164 | // ---------------------------------------------------------------------------------------- |
||
165 | |||
166 | void HMC5843_GetIdentification(void) |
||
167 | { |
||
168 | if(I2C_State == I2C_STATE_IDLE) |
||
169 | { |
||
170 | I2C_TxBufferSize = 0; |
||
171 | // set register pointer |
||
172 | I2C_TxBuffer[I2C_TxBufferSize++] = REGISTER_IDA; |
||
173 | // initiate transmission |
||
174 | I2C_Transmission(HMC5843_SLAVE_ADDRESS, &HMC5843_UpdateIdentification, sizeof(HMC5843_Identification)); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | // ---------------------------------------------------------------------------------------- |
||
179 | u8 HMC5843_SetConfiguration(u8 cra, u8 crb, u8 mode) |
||
180 | { |
||
181 | u32 timeout = SetDelay(100); |
||
182 | // wait for free I2C bus |
||
183 | while(I2C_State != I2C_STATE_IDLE) |
||
184 | { |
||
185 | if(CheckDelay(timeout)) return 0; |
||
186 | } |
||
187 | I2C_TxBufferSize = 0; |
||
188 | I2C_TxBuffer[I2C_TxBufferSize++] = REGISTER_CRA; |
||
189 | I2C_TxBuffer[I2C_TxBufferSize++] = cra; |
||
190 | I2C_TxBuffer[I2C_TxBufferSize++] = crb; |
||
191 | I2C_TxBuffer[I2C_TxBufferSize++] = mode; |
||
192 | // initiate transmission |
||
193 | I2C_Transmission(HMC5843_SLAVE_ADDRESS, NULL, 0); |
||
194 | return 1; |
||
195 | } |
||
196 | |||
197 | //---------------------------------------------------------------- |
||
198 | u8 HMC5843_Init(void) |
||
199 | { |
||
200 | u8 msg[64]; |
||
201 | u8 repeat; |
||
202 | u32 timeout; |
||
203 | |||
204 | HMC5843_Present = 0; |
||
205 | |||
206 | HMC5843_Identification.A = 0xFF; |
||
207 | HMC5843_Identification.B = 0xFF; |
||
208 | HMC5843_Identification.C = 0xFF; |
||
209 | |||
210 | // polling of identification |
||
211 | repeat = 0; |
||
212 | do |
||
213 | { |
||
214 | HMC5843_GetIdentification(); |
||
215 | timeout = SetDelay(250); |
||
216 | do |
||
217 | { |
||
218 | if (HMC5843_Identification.A != 0xFF) break; // break loop on success |
||
219 | }while (!CheckDelay(timeout)); |
||
220 | UART1_PutString("."); |
||
221 | repeat++; |
||
222 | }while ((HMC5843_Identification.A == 0xFF) && (repeat < 12)); // 12*250ms=3s |
||
223 | // if we got it |
||
224 | if(HMC5843_Identification.A != 0xFF) |
||
225 | { |
||
226 | sprintf(msg, " HMC5843 ID %d/%d/%d.", HMC5843_Identification.A, HMC5843_Identification.B, HMC5843_Identification.C); |
||
227 | UART1_PutString(msg); |
||
228 | |||
229 | if ( (HMC5843_Identification.A == HMC5843_IDA) |
||
230 | && (HMC5843_Identification.B == HMC5843_IDB) |
||
231 | && (HMC5843_Identification.C == HMC5843_IDC)) |
||
232 | { |
||
233 | HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS); |
||
234 | HMC5843_Present = 1; |
||
235 | } |
||
236 | else |
||
237 | { |
||
238 | UART1_PutString("\n\r HMC5843 not compatible!"); |
||
239 | LED_RED_ON; |
||
240 | } |
||
241 | } |
||
242 | return(HMC5843_Present); |
||
243 | } |
||
244 | |||
245 | |||
246 | //---------------------------------------------------------------- |
||
247 | void HMC5843_GetMagVector(void) |
||
248 | { |
||
249 | if(I2C_State == I2C_STATE_IDLE) |
||
250 | { |
||
251 | I2C_TxBufferSize = 0; |
||
252 | // set register pointer |
||
253 | I2C_TxBuffer[I2C_TxBufferSize++] = REGISTER_DATAX_MSB; |
||
254 | // initiate transmission |
||
255 | I2C_Transmission(HMC5843_SLAVE_ADDRESS, &HMC5843_UpdateMagVector, sizeof(MagVector)); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | void HMC5843_UpdateCompass(void) |
||
260 | { |
||
261 | static u32 TimerCompassUpdate = 0; |
||
262 | |||
263 | if( (I2C_State == I2C_STATE_OFF) || !HMC5843_Present ) return; |
||
264 | |||
265 | if(CheckDelay(TimerCompassUpdate)) |
||
266 | { |
||
267 | // check for incomming compass calibration request |
||
268 | // update CalByte from spi input queue |
||
269 | /*fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte)); |
||
270 | // send new calstate |
||
271 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
||
272 | { |
||
273 | do the calibration here |
||
274 | } |
||
275 | else // request current heading */ |
||
276 | { |
||
277 | HMC5843_GetMagVector(); |
||
278 | } |
||
279 | TimerCompassUpdate = SetDelay(20); // every 20 ms are 50 Hz |
||
280 | } |
||
281 | } |