Rev 229 | Rev 242 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 229 | Rev 241 | ||
---|---|---|---|
Line 58... | Line 58... | ||
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "i2c.h" |
59 | #include "i2c.h" |
60 | #include "uart1.h" |
60 | #include "uart1.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | #include "config.h" |
62 | #include "config.h" |
63 | #include "main.h" |
- | |
64 | #include "led.h" |
63 | #include "led.h" |
65 | #include "spi_slave.h" |
- | |
Line 66... | Line -... | ||
66 | - | ||
67 | #define I2C_SLAVE_ADDRESS 0x50 |
- | |
68 | - | ||
69 | // I2C states |
- | |
70 | #define I2C_UNDEF 0 |
- | |
71 | #define I2C_IDLE 1 |
- | |
72 | #define I2C_TX_PROGRESS 2 |
- | |
73 | #define I2C_RX_PENDING 3 |
- | |
74 | #define I2C_RX_PROGRESS 4 |
- | |
75 | #define I2C_OFF 5 |
- | |
76 | 64 | ||
77 | volatile u8 I2C_State = I2C_OFF; // only on byte! because of sync by nesting irqs |
- | |
Line 78... | Line 65... | ||
78 | u8 I2C_StopPolling = 1; |
65 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
79 | 66 | ||
80 | // rxbuffer |
67 | // rxbuffer |
81 | volatile u8 I2C_RxBufferSize; |
68 | volatile u8 I2C_RxBufferSize; |
82 | volatile u8 *I2C_RxBuffer; |
69 | volatile u8 I2C_RxBuffer[I2C_RXBUFFER_LEN]; |
83 | // txbuffer |
70 | // txbuffer |
Line 84... | Line 71... | ||
84 | volatile u8 I2C_TxBufferSize; |
71 | volatile u8 I2C_TxBufferSize; |
85 | volatile u8 *I2C_TxBuffer; |
72 | volatile u8 I2C_TxBuffer[I2C_TXBUFFER_LEN]; |
86 | - | ||
87 | volatile u8 I2C_Direction; |
- | |
88 | volatile u8 I2C_Command; |
- | |
89 | // I2C Transfer buffers |
73 | |
90 | volatile I2C_Heading_t I2C_Heading; |
- | |
91 | volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
- | |
92 | volatile I2C_Mag_t I2C_Mag; |
- | |
Line 93... | Line -... | ||
93 | volatile I2C_Version_t MK3MAG_Version; |
- | |
94 | volatile I2C_Cal_t I2C_WriteCal; |
74 | volatile u8 I2C_Direction; |
Line 95... | Line 75... | ||
95 | volatile I2C_Cal_t I2C_ReadCal; |
75 | volatile u8 I2C_SlaveAddress = 0x00; |
96 | 76 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
|
97 | #define I2C1_TIMEOUT 500 // 500 ms |
77 | |
98 | volatile u32 I2C1_Timeout = 0; |
78 | volatile u32 I2C1_Timeout = 0; |
99 | 79 | ||
Line 100... | Line 80... | ||
100 | //-------------------------------------------------------------- |
80 | //-------------------------------------------------------------- |
Line 101... | Line 81... | ||
101 | void I2C1_Init(void) |
81 | void I2C1_Init(void) |
102 | { |
82 | { |
103 | I2C_InitTypeDef I2C_Struct; |
83 | I2C_InitTypeDef I2C_Struct; |
104 | GPIO_InitTypeDef GPIO_InitStructure; |
84 | GPIO_InitTypeDef GPIO_InitStructure; |
Line 174... | Line 154... | ||
174 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
154 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
175 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
155 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
176 | I2C_Struct.I2C_OwnAddress = 0x00; |
156 | I2C_Struct.I2C_OwnAddress = 0x00; |
177 | I2C_Init(I2C1, &I2C_Struct); |
157 | I2C_Init(I2C1, &I2C_Struct); |
Line 178... | Line 158... | ||
178 | 158 | ||
179 | I2C_TxBuffer = NULL; |
159 | // empty rx and tx buffer |
180 | I2C_TxBufferSize = 0; |
- | |
181 | - | ||
182 | I2C_RxBuffer = NULL; |
160 | I2C_TxBufferSize = 0; |
Line 183... | Line 161... | ||
183 | I2C_RxBufferSize = 0; |
161 | I2C_RxBufferSize = 0; |
184 | 162 | ||
Line 185... | Line 163... | ||
185 | I2C_Cmd(I2C1, ENABLE); |
163 | I2C_Cmd(I2C1, ENABLE); |
186 | I2C_ITConfig(I2C1, ENABLE); |
- | |
Line 187... | Line 164... | ||
187 | 164 | I2C_ITConfig(I2C1, ENABLE); |
|
188 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
165 | |
189 | I2C_Heading.Heading = -1; |
166 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
190 | - | ||
191 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
- | |
Line 192... | Line 167... | ||
192 | I2C_GenerateSTOP(I2C1, ENABLE); |
167 | |
193 | I2C_State = I2C_IDLE; |
168 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
Line 194... | Line 169... | ||
194 | 169 | I2C_GenerateSTOP(I2C1, ENABLE); |
|
195 | I2C_StopPolling = 0; // start polling |
170 | I2C_State = I2C_STATE_IDLE; |
196 | 171 | ||
197 | UART1_PutString("ok"); |
172 | UART1_PutString("ok"); |
198 | } |
- | |
199 | 173 | } |
|
200 | 174 | ||
201 | //-------------------------------------------------------------- |
175 | |
202 | void I2C1_Deinit(void) |
176 | //-------------------------------------------------------------- |
203 | { |
177 | void I2C1_Deinit(void) |
204 | GPIO_InitTypeDef GPIO_InitStructure; |
178 | { |
205 | I2C_StopPolling = 1;// stop polling |
179 | GPIO_InitTypeDef GPIO_InitStructure; |
206 | UART1_PutString("\r\n I2C deinit..."); |
180 | UART1_PutString("\r\n I2C deinit..."); |
207 | I2C_GenerateStart(I2C1, DISABLE); |
181 | I2C_GenerateStart(I2C1, DISABLE); |
Line 221... | Line 195... | ||
221 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
195 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
222 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
196 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
223 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
197 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
224 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
198 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
Line 225... | Line 199... | ||
225 | 199 | ||
226 | I2C_TxBuffer = NULL; |
200 | // empty rx and tx buffer |
227 | I2C_TxBufferSize = 0; |
- | |
228 | - | ||
229 | I2C_RxBuffer = NULL; |
201 | I2C_TxBufferSize = 0; |
Line 230... | Line 202... | ||
230 | I2C_RxBufferSize = 0; |
202 | I2C_RxBufferSize = 0; |
231 | - | ||
Line 232... | Line 203... | ||
232 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
203 | |
233 | I2C_Heading.Heading = -1; |
204 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
Line 234... | Line 205... | ||
234 | 205 | ||
235 | UART1_PutString("ok"); |
206 | UART1_PutString("ok"); |
236 | } |
207 | } |
237 | 208 | ||
238 | - | ||
239 | //-------------------------------------------------------------- |
209 | |
Line 240... | Line 210... | ||
240 | void I2C1_IRQHandler(void) |
210 | //-------------------------------------------------------------- |
Line 241... | Line 211... | ||
241 | { |
211 | void I2C1_IRQHandler(void) |
Line 253... | Line 223... | ||
253 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
223 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
254 | { |
224 | { |
255 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
225 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
256 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
226 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
257 | } |
227 | } |
258 | I2C_State = I2C_IDLE; |
228 | I2C_State = I2C_STATE_IDLE; |
259 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
229 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
260 | LED_GRN_OFF; |
230 | LED_GRN_OFF; |
261 | return; |
231 | return; |
262 | } |
232 | } |
263 | else |
233 | else |
264 | { // depending on current i2c state |
234 | { // depending on current i2c state |
265 | switch (status) |
235 | switch(status) |
266 | { |
236 | { |
267 | // the start condition was initiated on the bus |
237 | // the start condition was initiated on the bus |
268 | case I2C_EVENT_MASTER_MODE_SELECT: |
238 | case I2C_EVENT_MASTER_MODE_SELECT: |
269 | LED_GRN_ON; |
239 | LED_GRN_ON; |
270 | // update current bus state variable |
240 | // update current bus state variable |
- | 241 | // jump to rx state if there is nothing to send |
|
271 | switch(I2C_Direction) |
242 | switch(I2C_Direction) |
272 | { |
243 | { |
273 | case I2C_MODE_TRANSMITTER: |
244 | case I2C_MODE_TRANSMITTER: |
274 | I2C_State = I2C_TX_PROGRESS; |
245 | I2C_State = I2C_STATE_TX_PROGRESS; |
275 | break; |
246 | break; |
Line 276... | Line 247... | ||
276 | 247 | ||
277 | case I2C_MODE_RECEIVER: |
248 | case I2C_MODE_RECEIVER: |
278 | if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
249 | if (I2C_RxBufferSize == 0) // nothingto send? |
279 | { |
250 | { |
280 | I2C_GenerateSTOP (I2C1, ENABLE); |
251 | I2C_GenerateSTOP (I2C1, ENABLE); |
281 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
252 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
282 | LED_GRN_OFF; |
253 | LED_GRN_OFF; |
283 | I2C_State = I2C_IDLE; |
254 | I2C_State = I2C_STATE_IDLE; |
284 | return; |
255 | return; |
285 | } |
256 | } |
286 | else |
257 | else |
287 | { |
258 | { |
288 | I2C_State = I2C_RX_PROGRESS; |
259 | I2C_State = I2C_STATE_RX_PROGRESS; |
289 | } |
260 | } |
Line 290... | Line 261... | ||
290 | break; |
261 | break; |
291 | 262 | ||
292 | default: // invalid direction |
263 | default: // invalid direction |
293 | I2C_GenerateSTOP (I2C1, ENABLE); |
264 | I2C_GenerateSTOP (I2C1, ENABLE); |
294 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
265 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
295 | LED_GRN_OFF; |
266 | LED_GRN_OFF; |
296 | I2C_State = I2C_IDLE; |
267 | I2C_State = I2C_STATE_IDLE; |
297 | return; |
268 | return; |
298 | } |
269 | } |
299 | // enable acknowledge |
270 | // enable acknowledge |
300 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
271 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
301 | // send address/direction byte on the bus |
272 | // send address/direction byte on the bus |
Line 302... | Line 273... | ||
302 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
273 | I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction); |
303 | break; |
274 | break; |
304 | 275 | ||
305 | // the address byte was send |
276 | // the address byte was send |
306 | case I2C_EVENT_MASTER_MODE_SELECTED: |
- | |
307 | // Clear EV6 by set again the PE bit |
- | |
308 | I2C_Cmd(I2C1, ENABLE); |
277 | case I2C_EVENT_MASTER_MODE_SELECTED: |
309 | // reset checksum |
278 | // Clear EV6 by set again the PE bit |
310 | crc = 0; |
279 | I2C_Cmd(I2C1, ENABLE); |
311 | switch(I2C_State) |
- | |
312 | { |
- | |
313 | case I2C_TX_PROGRESS: |
280 | switch(I2C_State) |
314 | // send command 1st data byte (allways the command id) |
281 | { |
- | 282 | case I2C_STATE_TX_PROGRESS: |
|
- | 283 | // send 1st data byte |
|
315 | I2C_SendData(I2C1, I2C_Command); |
284 | Tx_Idx = 0; |
316 | crc += I2C_Command; |
285 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
317 | Tx_Idx = 0; |
286 | Tx_Idx++; |
Line 318... | Line 287... | ||
318 | // reset timeout |
287 | // reset timeout |
319 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
288 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
- | 289 | break; |
|
- | 290 | ||
320 | break; |
291 | case I2C_STATE_RX_PROGRESS: |
Line 321... | Line 292... | ||
321 | 292 | Rx_Idx = 0; |
|
322 | case I2C_RX_PROGRESS: |
293 | // disable acknoledge if only one byte has to be read |
323 | Rx_Idx = 0; |
294 | if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE); |
324 | break; |
295 | break; |
325 | 296 | ||
326 | default: // unknown I2C state |
297 | default: // unknown I2C state |
327 | // should never happen |
298 | // should never happen |
328 | I2C_GenerateSTOP (I2C1, ENABLE); |
299 | I2C_GenerateSTOP (I2C1, ENABLE); |
329 | LED_GRN_OFF; |
300 | LED_GRN_OFF; |
330 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
301 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
Line 338... | Line 309... | ||
338 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
309 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
Line 339... | Line 310... | ||
339 | 310 | ||
340 | // some bytes have to be transmitted |
311 | // some bytes have to be transmitted |
341 | if(Tx_Idx < I2C_TxBufferSize) |
312 | if(Tx_Idx < I2C_TxBufferSize) |
342 | { |
- | |
343 | if(I2C_TxBuffer != NULL) |
- | |
344 | { |
313 | { |
345 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
- | |
346 | crc += I2C_TxBuffer[Tx_Idx]; |
- | |
347 | } |
314 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
348 | else |
- | |
349 | { |
- | |
350 | I2C_SendData(I2C1, 0x00); |
- | |
351 | } |
- | |
352 | } |
- | |
353 | else if(Tx_Idx == I2C_TxBufferSize) // the last tx buffer byte was send |
- | |
354 | { |
- | |
355 | // send crc byte at the end |
- | |
356 | crc = ~crc; // flip all bits in the checksum |
- | |
357 | I2C_SendData(I2C1, crc); |
- | |
358 | } |
- | |
359 | else if(Tx_Idx == (I2C_TxBufferSize+1) ) |
- | |
360 | { |
- | |
361 | I2C_SendData(I2C1, 0xAA); // send a dummybyte |
315 | Tx_Idx++; |
362 | } |
316 | } |
363 | else // last byte was send |
317 | else // last byte was send |
364 | { |
318 | { |
365 | // generate stop or repeated start condition |
319 | // generate stop or repeated start condition |
366 | if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
320 | if (I2C_RxBufferSize > 0) // is any answer byte expected? |
367 | { |
321 | { |
368 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
322 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
369 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
323 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
370 | } |
324 | } |
371 | else |
325 | else |
372 | { // stop communication |
326 | { // stop communication |
373 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
327 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
374 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
328 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
375 | LED_GRN_OFF; |
- | |
376 | DebugOut.Analog[15]++; |
329 | LED_GRN_OFF; |
377 | I2C_State = I2C_IDLE; // ready for new actions |
- | |
378 | 330 | I2C_State = I2C_STATE_IDLE; // ready for new actions |
|
379 | } |
331 | } |
380 | } |
- | |
381 | Tx_Idx++; |
332 | } |
Line 382... | Line 333... | ||
382 | break; |
333 | break; |
383 | 334 | ||
384 | // the master has received a byte from the slave |
335 | // the master has received a byte from the slave |
385 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
336 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
386 | // some bytes have to be received |
337 | // some bytes have to be received |
387 | if (Rx_Idx < I2C_RxBufferSize) |
338 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
388 | { // copy received byte from the data register to the rx-buffer |
- | |
389 | I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
- | |
390 | // update checksum |
339 | { // copy received byte from the data register to the rx-buffer |
391 | crc += I2C_PrimRxBuffer[Rx_Idx]; |
340 | I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
392 | } |
- | |
393 | // if the last byte (crc) was received |
341 | } |
394 | else if ( Rx_Idx == I2C_RxBufferSize) |
342 | else // if the last byte was received |
395 | { |
343 | { |
396 | // generate a STOP condition on the bus before reading data register |
- | |
397 | I2C_GenerateSTOP(I2C1, ENABLE); |
- | |
398 | // compare last byte with checksum |
344 | // generate a STOP condition on the bus before reading data register |
399 | crc = ~crc;// flip all bits in calulated checksum |
- | |
400 | if(crc == I2C_ReceiveData(I2C1)) |
345 | I2C_GenerateSTOP(I2C1, ENABLE); |
401 | { |
- | |
402 | // copy primary rx buffer content to rx buffer if exist |
- | |
403 | if(I2C_RxBuffer != NULL) |
346 | I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
404 | { |
- | |
405 | memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
347 | // call the rx handler function to process recieved data |
406 | } |
348 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_RxBuffer, I2C_RxBufferSize); |
407 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
- | |
408 | DebugOut.Analog[15]++; |
- | |
409 | } |
- | |
410 | else // checksum error detected |
- | |
411 | { |
- | |
412 | DebugOut.Analog[14]++; |
349 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
413 | } |
350 | DebugOut.Analog[15]++; |
414 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
351 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
415 | LED_GRN_OFF; |
352 | LED_GRN_OFF; |
416 | I2C_State = I2C_IDLE; |
353 | I2C_State = I2C_STATE_IDLE; |
417 | return; |
354 | return; |
418 | } |
355 | } |
419 | Rx_Idx++; |
356 | Rx_Idx++; |
420 | // if the 2nd last byte was received disable acknowledge for the last one |
357 | // if the 2nd last byte was received disable acknowledge for the last one |
421 | if ( Rx_Idx == I2C_RxBufferSize ) |
358 | if ( (Rx_Idx + 1) == I2C_RxBufferSize ) |
422 | { |
359 | { |
423 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
360 | I2C_AcknowledgeConfig(I2C1, DISABLE); |
Line 424... | Line 361... | ||
424 | } |
361 | } |
- | 362 | break; |
|
- | 363 | ||
- | 364 | default:// unknown event |
|
- | 365 | // should never happen |
|
- | 366 | I2C_GenerateSTOP (I2C1, ENABLE); |
|
425 | break; |
367 | LED_GRN_OFF; |
426 | 368 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
|
427 | default: |
369 | I2C_State = I2C_STATE_IDLE; |
428 | break; |
- | |
429 | } |
370 | break; |
430 | } |
371 | } |
431 | 372 | } |
|
432 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
373 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
- | 374 | } |
|
433 | } |
375 | // ---------------------------------------------------------------------------------------- |
434 | //---------------------------------------------------------------- |
376 | // initate an i2c transmission |
435 | void I2C1_SendCommand(u8 command) |
- | |
436 | { |
- | |
- | 377 | u8 I2C_Transmission(u8 SlaveAddr, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
|
437 | // disable I2C IRQ to check state |
378 | { |
438 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
379 | u8 retval = 0; |
439 | // If I2C transmission is in progress |
380 | |
440 | if(I2C_State == I2C_IDLE) |
381 | if(I2C_State == I2C_STATE_IDLE) |
- | 382 | { |
|
441 | { |
383 | I2C_RxBufferSize = RxBytes; |
442 | // update current command id |
384 | // set direction to master transmitter |
443 | I2C_Command = command; |
385 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_TXBUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
444 | // set pointers to data area with respect to the command id |
- | |
445 | switch (command) |
- | |
446 | { |
- | |
447 | case I2C_CMD_VERSION: |
- | |
448 | I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
386 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_RXBUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
449 | I2C_RxBufferSize = sizeof(MK3MAG_Version); |
- | |
450 | I2C_TxBuffer = NULL; |
- | |
451 | I2C_TxBufferSize = 0; |
- | |
452 | break; |
- | |
453 | case I2C_CMD_WRITE_CAL: |
- | |
454 | I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
- | |
455 | I2C_RxBufferSize = sizeof(I2C_ReadCal); |
- | |
456 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
- | |
457 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
- | |
458 | break; |
- | |
459 | case I2C_CMD_READ_MAG: |
- | |
460 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
387 | else // nothing to send or receive |
461 | I2C_RxBufferSize = sizeof(I2C_Mag); |
- | |
462 | I2C_TxBuffer = NULL; |
388 | { |
463 | I2C_TxBufferSize = 0; |
- | |
464 | break; |
- | |
465 | case I2C_CMD_READ_HEADING: |
- | |
466 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
- | |
467 | I2C_RxBufferSize = sizeof(I2C_Heading); |
- | |
468 | I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
- | |
469 | I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
- | |
470 | // update attitude from spi rx buffer |
- | |
471 | VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy |
389 | I2C_TxBufferSize = 0; |
472 | I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
- | |
473 | I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
- | |
474 | VIC_ITCmd(SSP0_ITLine, ENABLE); |
- | |
475 | break; |
- | |
476 | default: // unknown command id |
- | |
477 | I2C_RxBuffer = NULL; |
- | |
478 | I2C_RxBufferSize = 0; |
390 | I2C_RxBufferSize = 0; |
479 | I2C_TxBuffer = NULL; |
391 | // enable I2C IRQ again |
480 | I2C_TxBufferSize = 0; |
392 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
- | 393 | return(retval); |
|
481 | break; |
394 | } |
482 | } |
395 | // update slave address and rx data handler funbction pointer |
483 | // set direction to master transmitter |
396 | I2C_SlaveAddress = SlaveAddr; |
484 | I2C_Direction = I2C_MODE_TRANSMITTER; |
- | |
485 | // test on busy flag and clear it |
- | |
486 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
397 | I2C_pRxHandler = pRxHandler; |
487 | // enable I2C IRQ again |
398 | // test on busy flag and clear it |
488 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
- | |
489 | // initiate start condition on the bus |
- | |
490 | I2C_GenerateStart(I2C1, ENABLE); |
- | |
491 | // to be continued in the I2C1_IRQHandler() above |
- | |
492 | } // EOF I2C_State == I2C_IDLE |
- | |
493 | else // I2C_State != I2C_IDLE |
- | |
494 | { |
- | |
495 | // re-enable I2C IRQ again |
- | |
496 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
- | |
497 | } |
- | |
498 | } |
- | |
499 | - | ||
500 | //---------------------------------------------------------------- |
- | |
501 | void I2C1_GetMK3MagVersion(void) |
- | |
502 | { |
- | |
503 | u8 msg[64]; |
- | |
504 | u8 repeat; |
- | |
505 | u32 timeout; |
- | |
506 | - | ||
507 | UART1_PutString("\r\n Getting Version from MK3MAG"); |
- | |
508 | // stop polling of other commands |
- | |
509 | I2C_StopPolling = 1; |
- | |
510 | - | ||
511 | MK3MAG_Version.Major = 0xFF; |
- | |
512 | MK3MAG_Version.Minor = 0xFF; |
- | |
513 | MK3MAG_Version.Patch = 0xFF; |
399 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
514 | MK3MAG_Version.Compatible = 0xFF; |
- | |
515 | // polling of version info |
- | |
516 | repeat = 0; |
- | |
517 | do |
- | |
518 | { |
- | |
519 | I2C1_SendCommand(I2C_CMD_VERSION); |
- | |
520 | timeout = SetDelay(250); |
- | |
521 | do |
- | |
522 | { |
- | |
523 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
- | |
524 | }while (!CheckDelay(timeout)); |
- | |
525 | UART1_PutString("."); |
- | |
526 | repeat++; |
- | |
527 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
- | |
528 | // if we got it |
- | |
529 | if (MK3MAG_Version.Major != 0xFF) |
- | |
530 | { |
- | |
531 | sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
- | |
532 | UART1_PutString(msg); |
400 | // initiate start condition on the bus |
533 | sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible); |
- | |
534 | UART1_PutString(msg); |
- | |
535 | } |
- | |
536 | else UART1_PutString("\n\r No version information from MK3Mag."); |
- | |
537 | - | ||
538 | I2C_StopPolling = 0; // enable polling of heading command |
- | |
539 | } |
- | |
540 | - | ||
541 | - | ||
542 | //---------------------------------------------------------------- |
- | |
543 | void I2C1_UpdateCompass(void) |
- | |
544 | { |
- | |
545 | static u32 TimerCompassUpdate = 0; |
- | |
546 | - | ||
547 | if( (I2C_State == I2C_OFF) || I2C_StopPolling ) return; |
- | |
548 | - | ||
549 | if(CheckDelay(TimerCompassUpdate)) |
- | |
550 | { |
- | |
551 | // check for incomming compass calibration request |
401 | I2C_GenerateStart(I2C1, ENABLE); |
552 | // update CalByte from spi input queue |
- | |
553 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
- | |
554 | // send new calstate |
- | |
555 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
- | |
556 | { |
- | |
557 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
- | |
558 | } |
- | |
559 | else // request current heading |
- | |
560 | { |
- | |
561 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
- | |
562 | } |
402 | retval = 1; |