Rev 146 | Rev 150 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
||
6 | // + Nur für den privaten Gebrauch |
||
7 | // + FOR NON COMMERCIAL USE ONLY |
||
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 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. |
||
1 | ingob | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
1 | ingob | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
1 | ingob | 29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
1 | ingob | 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 | killagreg | 37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
1 | ingob | 38 | // + for non-commercial use (directly or indirectly) |
37 | killagreg | 39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
1 | ingob | 40 | // + with our written permission |
37 | killagreg | 41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
||
1 | ingob | 43 | // + * PORTING this software (or part of it) to systems (other than 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 |
||
37 | killagreg | 55 | // + POSSIBILITY OF SUCH DAMAGE. |
1 | ingob | 56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
41 | ingob | 57 | #include <stdlib.h> |
58 | #include <string.h> |
||
59 | #include "91x_lib.h" |
||
60 | #include "i2c.h" |
||
61 | #include "uart1.h" |
||
119 | killagreg | 62 | #include "timer1.h" |
136 | killagreg | 63 | #include "config.h" |
1 | ingob | 64 | #include "main.h" |
41 | ingob | 65 | #include "led.h" |
66 | #include "spi_slave.h" |
||
1 | ingob | 67 | |
68 | |||
41 | ingob | 69 | volatile I2C_State_t I2C_State = I2C_OFF; |
70 | |||
71 | // rxbuffer |
||
72 | volatile u8 I2C_RxBufferSize; |
||
73 | volatile u8 *I2C_RxBuffer; |
||
74 | volatile u8 Rx_Idx = 0; |
||
75 | // txbuffer |
||
76 | volatile u8 I2C_TxBufferSize; |
||
77 | volatile u8 *I2C_TxBuffer; |
||
78 | volatile u8 Tx_Idx = 0; |
||
79 | |||
80 | volatile u8 I2C_Direction; |
||
81 | volatile u8 I2C_Command; |
||
82 | |||
83 | volatile I2C_Heading_t I2C_Heading; |
||
84 | volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
||
85 | volatile I2C_Mag_t I2C_Mag; |
||
86 | volatile I2C_Version_t MK3MAG_Version; |
||
87 | volatile I2C_Cal_t I2C_WriteCal; |
||
88 | volatile I2C_Cal_t I2C_ReadCal; |
||
89 | |||
146 | killagreg | 90 | #define I2C1_TIMEOUT 500 // 500 ms |
41 | ingob | 91 | volatile u32 I2C1_Timeout = 0; |
92 | volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers |
||
1 | ingob | 93 | |
41 | ingob | 94 | |
1 | ingob | 95 | //-------------------------------------------------------------- |
96 | void I2C1_Init(void) |
||
97 | { |
||
41 | ingob | 98 | I2C_InitTypeDef I2C_Struct; |
99 | GPIO_InitTypeDef GPIO_InitStructure; |
||
149 | killagreg | 100 | |
101 | I2C_State = I2C_OFF; |
||
37 | killagreg | 102 | |
110 | killagreg | 103 | UART1_PutString("\r\n I2C init..."); |
41 | ingob | 104 | // enable Port 2 peripherie |
105 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
106 | // disable a reset state |
||
107 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
||
1 | ingob | 108 | |
41 | ingob | 109 | // free a busy bus |
1 | ingob | 110 | |
41 | ingob | 111 | // At switch on I2C devices can get in a state where they |
112 | // are still waiting for a command due to all the bus lines bouncing |
||
113 | // around at startup have started clocking data into the device(s). |
||
114 | // Enable the ports as open collector port outputs |
||
115 | // and clock out at least 9 SCL pulses, then generate a stop |
||
116 | // condition and then leave the clock line high. |
||
37 | killagreg | 117 | |
41 | ingob | 118 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
119 | GPIO_StructInit(&GPIO_InitStructure); |
||
120 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
121 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
122 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
123 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
||
124 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
||
125 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
37 | killagreg | 126 | |
41 | ingob | 127 | u8 i; |
128 | u32 delay; |
||
129 | // set SCL high and then SDA to low (start condition) |
||
130 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
||
131 | delay = SetDelay(1); |
||
132 | while (!CheckDelay(delay)); |
||
133 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
||
134 | // toggle SCL at least 10 times from high to low to high |
||
135 | for(i = 0; i < 10; i++) |
||
136 | { |
||
137 | delay = SetDelay(1); |
||
138 | while (!CheckDelay(delay)); |
||
1 | ingob | 139 | |
41 | ingob | 140 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
141 | delay = SetDelay(1); |
||
142 | while (!CheckDelay(delay)); |
||
143 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
||
144 | } |
||
145 | delay = SetDelay(1); |
||
146 | while (!CheckDelay(delay)); |
||
147 | // create stop condition setting SDA HIGH when SCL is HIGH |
||
148 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
||
37 | killagreg | 149 | |
150 | |||
41 | ingob | 151 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
152 | GPIO_StructInit(&GPIO_InitStructure); |
||
153 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
154 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
155 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
156 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
||
157 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
||
158 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
159 | |||
160 | // enable I2C peripherie |
||
161 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
||
162 | // reset I2C peripherie |
||
163 | SCU_APBPeriphReset(__I2C1,ENABLE); |
||
164 | SCU_APBPeriphReset(__I2C1,DISABLE); |
||
165 | |||
166 | I2C_DeInit(I2C1); |
||
167 | I2C_StructInit(&I2C_Struct); |
||
168 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
||
169 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
||
136 | killagreg | 170 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
41 | ingob | 171 | I2C_Struct.I2C_OwnAddress = 0x00; |
172 | I2C_Init(I2C1, &I2C_Struct); |
||
173 | |||
174 | I2C_TxBuffer = NULL; |
||
175 | Tx_Idx = 0; |
||
176 | I2C_TxBufferSize = 0; |
||
177 | |||
178 | I2C_RxBuffer = NULL; |
||
179 | Rx_Idx = 0; |
||
180 | I2C_RxBufferSize = 0; |
||
181 | |||
182 | I2C_Cmd(I2C1, ENABLE); |
||
183 | I2C_ITConfig(I2C1, ENABLE); |
||
184 | |||
136 | killagreg | 185 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
41 | ingob | 186 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
187 | |||
146 | killagreg | 188 | I2C1_Timeout = SetDelay(2*I2C1_TIMEOUT); |
41 | ingob | 189 | I2C_Heading.Heading = -1; |
190 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
191 | I2C_State = I2C_IDLE; |
||
192 | |||
110 | killagreg | 193 | UART1_PutString("ok"); |
1 | ingob | 194 | } |
195 | |||
41 | ingob | 196 | |
1 | ingob | 197 | //-------------------------------------------------------------- |
41 | ingob | 198 | void I2C1_Deinit(void) |
1 | ingob | 199 | { |
79 | killagreg | 200 | GPIO_InitTypeDef GPIO_InitStructure; |
149 | killagreg | 201 | I2C_State = I2C_OFF; |
110 | killagreg | 202 | UART1_PutString("\r\n I2C deinit..."); |
41 | ingob | 203 | I2C_GenerateStart(I2C1, DISABLE); |
204 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
205 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
206 | I2C_ITConfig(I2C1, DISABLE); |
||
207 | I2C_Cmd(I2C1, DISABLE); |
||
208 | I2C_DeInit(I2C1); |
||
209 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
||
79 | killagreg | 210 | |
41 | ingob | 211 | // set ports to input |
212 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
213 | GPIO_StructInit(&GPIO_InitStructure); |
||
214 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
||
79 | killagreg | 215 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
216 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
||
41 | ingob | 217 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
79 | killagreg | 218 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
219 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
1 | ingob | 220 | |
41 | ingob | 221 | I2C_TxBuffer = NULL; |
79 | killagreg | 222 | Tx_Idx = 0; |
41 | ingob | 223 | I2C_TxBufferSize = 0; |
1 | ingob | 224 | |
41 | ingob | 225 | I2C_RxBuffer = NULL; |
226 | Rx_Idx = 0; |
||
227 | I2C_RxBufferSize = 0; |
||
37 | killagreg | 228 | |
41 | ingob | 229 | I2C1_Timeout = 0; |
230 | I2C_Heading.Heading = -1; |
||
37 | killagreg | 231 | |
110 | killagreg | 232 | UART1_PutString("ok"); |
41 | ingob | 233 | } |
1 | ingob | 234 | |
41 | ingob | 235 | |
236 | //-------------------------------------------------------------- |
||
237 | void I2C1_IRQHandler(void) |
||
238 | { |
||
239 | u16 status; |
||
240 | static u8 crc; |
||
241 | // detemine I2C State |
||
242 | status = I2C_GetLastEvent(I2C1); |
||
243 | |||
244 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
||
79 | killagreg | 245 | { // Set and subsequently clear the STOP bit while BTF is set. |
41 | ingob | 246 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
247 | { |
||
248 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
||
249 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
||
250 | } |
||
251 | I2C_State = I2C_IDLE; |
||
252 | LED_GRN_OFF; |
||
253 | } |
||
254 | else |
||
255 | { // depending on current i2c state |
||
256 | switch (status) |
||
257 | { |
||
258 | // the start condition was initiated on the bus |
||
259 | case I2C_EVENT_MASTER_MODE_SELECT: |
||
260 | LED_GRN_ON; |
||
261 | // update current bus state variable |
||
262 | switch(I2C_Direction) |
||
263 | { |
||
264 | case I2C_MODE_TRANSMITTER: |
||
265 | I2C_State = I2C_TX_PROGRESS; |
||
266 | break; |
||
79 | killagreg | 267 | |
41 | ingob | 268 | case I2C_MODE_RECEIVER: |
269 | if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
||
270 | { |
||
271 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
272 | I2C_State = I2C_IDLE; |
||
273 | return; |
||
274 | } |
||
275 | else |
||
276 | { |
||
277 | I2C_State = I2C_RX_PROGRESS; |
||
278 | } |
||
279 | break; |
||
79 | killagreg | 280 | |
41 | ingob | 281 | default: // invalid direction |
282 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
283 | I2C_State = I2C_IDLE; |
||
284 | LED_GRN_OFF; |
||
285 | return; |
||
286 | } |
||
287 | // enable acknowledge |
||
288 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
||
289 | // send address/direction byte on the bus |
||
290 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
||
291 | break; |
||
79 | killagreg | 292 | |
41 | ingob | 293 | // the address byte was send |
294 | case I2C_EVENT_MASTER_MODE_SELECTED: |
||
295 | // Clear EV6 by set again the PE bit |
||
79 | killagreg | 296 | I2C_Cmd(I2C1, ENABLE); |
41 | ingob | 297 | // reset checksum |
298 | crc = 0; |
||
299 | switch(I2C_State) |
||
300 | { |
||
301 | case I2C_TX_PROGRESS: |
||
79 | killagreg | 302 | // send command 1st data byte (allways the command id) |
41 | ingob | 303 | I2C_SendData(I2C1, I2C_Command); |
304 | crc += I2C_Command; |
||
305 | Tx_Idx = 0; |
||
306 | // reset timeout |
||
307 | I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
||
308 | break; |
||
79 | killagreg | 309 | |
41 | ingob | 310 | case I2C_RX_PROGRESS: |
311 | Rx_Idx = 0; |
||
312 | break; |
||
79 | killagreg | 313 | |
41 | ingob | 314 | default: // unknown I2C state |
315 | // should never happen |
||
316 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
317 | I2C_State = I2C_IDLE; |
||
318 | break; |
||
319 | } |
||
320 | break; |
||
79 | killagreg | 321 | |
41 | ingob | 322 | // the master has transmitted a byte and slave has been acknowledged |
323 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
||
79 | killagreg | 324 | |
41 | ingob | 325 | // some bytes have to be transmitted |
326 | if(Tx_Idx < I2C_TxBufferSize) |
||
327 | { |
||
328 | if(I2C_TxBuffer != NULL) |
||
329 | { |
||
330 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
||
331 | crc += I2C_TxBuffer[Tx_Idx]; |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | I2C_SendData(I2C1, 0x00); |
||
336 | } |
||
337 | } |
||
338 | else // the last tx buffer byte was send |
||
339 | { |
||
340 | // send crc byte at the end |
||
137 | killagreg | 341 | crc = ~crc; // flip all bits in the checksum |
41 | ingob | 342 | I2C_SendData(I2C1, crc); |
343 | // generate stop or repeated start condition |
||
344 | if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
||
345 | { |
||
79 | killagreg | 346 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
347 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
||
41 | ingob | 348 | } |
349 | else |
||
350 | { // stop communication |
||
351 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
||
352 | I2C_State = I2C_IDLE; // ready for new actions |
||
353 | LED_GRN_OFF; |
||
354 | DebugOut.Analog[15]++; |
||
355 | } |
||
356 | } |
||
79 | killagreg | 357 | Tx_Idx++; |
41 | ingob | 358 | break; |
79 | killagreg | 359 | |
41 | ingob | 360 | // the master has received a byte from the slave |
79 | killagreg | 361 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
41 | ingob | 362 | // some bytes have to be received |
363 | if (Rx_Idx < I2C_RxBufferSize) |
||
364 | { // copy received byte from the data register to the rx-buffer |
||
365 | I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
||
366 | // update checksum |
||
367 | crc += I2C_PrimRxBuffer[Rx_Idx]; |
||
368 | } |
||
369 | // if the last byte (crc) was received |
||
79 | killagreg | 370 | else if ( Rx_Idx == I2C_RxBufferSize) |
41 | ingob | 371 | { |
372 | // generate a STOP condition on the bus before reading data register |
||
373 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
374 | // compare last byte with checksum |
||
137 | killagreg | 375 | crc = ~crc;// flip all bits in calulated checksum |
41 | ingob | 376 | if(crc == I2C_ReceiveData(I2C1)) |
377 | { // copy primary rx buffer content to rx buffer if exist |
||
378 | if(I2C_RxBuffer != NULL) |
||
379 | { |
||
380 | memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
||
381 | } |
||
141 | killagreg | 382 | I2C1_Timeout = SetDelay(500); |
41 | ingob | 383 | DebugOut.Analog[15]++; |
384 | } |
||
385 | else // checksum error detected |
||
386 | { |
||
387 | DebugOut.Analog[14]++; |
||
388 | } |
||
389 | I2C_State = I2C_IDLE; |
||
79 | killagreg | 390 | LED_GRN_OFF; |
41 | ingob | 391 | } |
392 | Rx_Idx++; |
||
393 | // if the 2nd last byte was received disable acknowledge for the last one |
||
394 | if ( Rx_Idx == I2C_RxBufferSize ) |
||
395 | { |
||
396 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
||
397 | } |
||
398 | break; |
||
79 | killagreg | 399 | |
41 | ingob | 400 | default: |
401 | break; |
||
402 | } |
||
403 | } |
||
1 | ingob | 404 | } |
405 | //---------------------------------------------------------------- |
||
41 | ingob | 406 | void I2C1_SendCommand(u8 command) |
1 | ingob | 407 | { |
41 | ingob | 408 | // If I2C transmission is in progress |
149 | killagreg | 409 | if(I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
41 | ingob | 410 | // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during |
411 | // update of that buffer pointers and length |
||
412 | I2C_ITConfig(I2C1, DISABLE); |
||
413 | // update current command id |
||
414 | I2C_Command = command; |
||
415 | // set pointers to data area with respect to the command id |
||
416 | switch (command) |
||
417 | { |
||
79 | killagreg | 418 | case I2C_CMD_VERSION: |
41 | ingob | 419 | I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
420 | I2C_RxBufferSize = sizeof(MK3MAG_Version); |
||
421 | I2C_TxBuffer = NULL; |
||
422 | I2C_TxBufferSize = 0; |
||
423 | break; |
||
424 | case I2C_CMD_WRITE_CAL: |
||
425 | I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
||
426 | I2C_RxBufferSize = sizeof(I2C_ReadCal); |
||
427 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
||
428 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
||
429 | break; |
||
79 | killagreg | 430 | case I2C_CMD_READ_MAG: |
41 | ingob | 431 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
432 | I2C_RxBufferSize = sizeof(I2C_Mag); |
||
433 | I2C_TxBuffer = NULL; |
||
434 | I2C_TxBufferSize = 0; |
||
435 | break; |
||
79 | killagreg | 436 | case I2C_CMD_READ_HEADING: |
61 | holgerb | 437 | DebugOut.Analog[10] = I2C_Heading.Heading; |
41 | ingob | 438 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
439 | I2C_RxBufferSize = sizeof(I2C_Heading); |
||
440 | // updat atitude from spi rx buffer |
||
62 | killagreg | 441 | I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
442 | I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
||
41 | ingob | 443 | I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
444 | I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
||
445 | break; |
||
446 | default: // unknown command id |
||
447 | I2C_RxBuffer = NULL; |
||
448 | I2C_RxBufferSize = 0; |
||
449 | I2C_TxBuffer = NULL; |
||
450 | I2C_TxBufferSize = 0; |
||
451 | break; |
||
452 | } |
||
453 | // set direction to master transmitter |
||
454 | I2C_Direction = I2C_MODE_TRANSMITTER; |
||
79 | killagreg | 455 | // test on busy flag and clear it |
41 | ingob | 456 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
149 | killagreg | 457 | // enable I2C IRQ again |
458 | I2C_ITConfig(I2C1, ENABLE); |
||
79 | killagreg | 459 | // initiale start condition on the bus |
41 | ingob | 460 | I2C_GenerateStart(I2C1, ENABLE); |
461 | // to be continued in the I2C1_IRQHandler() above |
||
462 | } |
||
1 | ingob | 463 | |
41 | ingob | 464 | //---------------------------------------------------------------- |
465 | void I2C1_GetMK3MagVersion(void) |
||
466 | { |
||
78 | holgerb | 467 | u8 msg[64]; |
149 | killagreg | 468 | u8 repeat = 20; |
469 | u32 timeout; |
||
1 | ingob | 470 | |
41 | ingob | 471 | MK3MAG_Version.Major = 0xFF; |
472 | MK3MAG_Version.Minor = 0xFF; |
||
473 | MK3MAG_Version.Patch = 0xFF; |
||
474 | MK3MAG_Version.Compatible = 0xFF; |
||
37 | killagreg | 475 | |
41 | ingob | 476 | while ((MK3MAG_Version.Major == 0xFF) && (repeat != 0)) |
477 | { |
||
79 | killagreg | 478 | I2C1_SendCommand(I2C_CMD_VERSION); |
149 | killagreg | 479 | timeout = SetDelay(200); |
480 | while (!CheckDelay(timeout)) if (MK3MAG_Version.Major != 0xFF) break; |
||
41 | ingob | 481 | repeat--; |
482 | } |
||
79 | killagreg | 483 | |
41 | ingob | 484 | if (MK3MAG_Version.Major != 0xFF) |
79 | killagreg | 485 | { |
41 | ingob | 486 | sprintf(msg, "\n\r MK3Mag V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
110 | killagreg | 487 | UART1_PutString(msg); |
41 | ingob | 488 | sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible); |
110 | killagreg | 489 | UART1_PutString(msg); |
41 | ingob | 490 | } |
110 | killagreg | 491 | else UART1_PutString("\n\rNo version information from MK3Mag."); |
146 | killagreg | 492 | } |
1 | ingob | 493 | |
146 | killagreg | 494 | |
495 | //---------------------------------------------------------------- |
||
496 | void I2C1_UpdateCompass(void) |
||
497 | { |
||
498 | static u32 TimerCompassUpdate = 0; |
||
499 | |||
149 | killagreg | 500 | if(I2C_State == I2C_OFF) return; |
501 | |||
502 | if(CheckDelay(TimerCompassUpdate)) |
||
146 | killagreg | 503 | { |
149 | killagreg | 504 | // check for incomming compass calibration request |
505 | // update CalByte from spi input queue |
||
506 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
||
507 | // send new calstate |
||
508 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
||
146 | killagreg | 509 | { |
149 | killagreg | 510 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
146 | killagreg | 511 | } |
149 | killagreg | 512 | else // request current heading |
513 | { |
||
514 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
||
515 | } |
||
516 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
||
146 | killagreg | 517 | } |
1 | ingob | 518 | } |