Subversion Repositories NaviCtrl

Rev

Rev 482 | Rev 486 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 482 Rev 483
Line 55... Line 55...
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <stdio.h>
56
#include <stdio.h>
57
#include <string.h>
57
#include <string.h>
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "mk3mag.h"
59
#include "mk3mag.h"
-
 
60
#include "i2c1.h"
60
#include "i2c.h"
61
#include "i2c0.h"
61
#include "timer1.h"
62
#include "timer1.h"
62
#include "led.h"
63
#include "led.h"
63
#include "main.h"
64
#include "main.h"
64
#include "uart1.h"
65
#include "uart1.h"
65
#include "compass.h"
66
#include "compass.h"
Line 171... Line 172...
171
 
172
 
172
// use I2C1 for communication
173
// use I2C1 for communication
173
void MK3MAG_SendCommand(u8 command)
174
void MK3MAG_SendCommand(u8 command)
174
{
175
{
175
        // try to catch the I2C buffer
176
        // try to catch the I2C buffer
176
        if(I2CBus_LockBuffer(I2C1, 0))
177
        if(I2C1_LockBuffer(0))
177
        {
-
 
178
                u8 TxData[100];
178
        {
179
                u16 TxBytes = 0;
179
                u16 TxBytes = 0;
180
                u16 RxBytes = 0;
180
                u16 RxBytes = 0;
Line 181... Line 181...
181
                I2C_pRxHandler_t pRxHandlerFunc = NULL;
181
                I2C_pRxHandler_t pRxHandlerFunc = NULL;
182
               
182
               
Line 183... Line 183...
183
                // update current command id
183
                // update current command id
184
                TxData[TxBytes++] = command;
184
                I2C1_Buffer[TxBytes++] = command;
185
 
185
 
186
                // set pointers to data area with respect to the command id
186
                // set pointers to data area with respect to the command id
Line 191... Line 191...
191
                                pRxHandlerFunc = &MK3MAG_UpdateVersion;
191
                                pRxHandlerFunc = &MK3MAG_UpdateVersion;
192
                                break;
192
                                break;
193
                        case MK3MAG_CMD_WRITE_CAL:
193
                        case MK3MAG_CMD_WRITE_CAL:
194
                                RxBytes = sizeof(MK3MAG_ReadCal)+1;
194
                                RxBytes = sizeof(MK3MAG_ReadCal)+1;
195
                                pRxHandlerFunc = &MK3MAG_UpdateCalibration;
195
                                pRxHandlerFunc = &MK3MAG_UpdateCalibration;
196
                                memcpy(TxData+TxBytes, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
196
                                memcpy((u8*)I2C1_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
197
                                TxBytes += sizeof(MK3MAG_WriteCal);
197
                                TxBytes += sizeof(MK3MAG_WriteCal);
198
                                break;
198
                                break;
199
                        case MK3MAG_CMD_READ_MAGVECT:
199
                        case MK3MAG_CMD_READ_MAGVECT:
200
                                RxBytes = sizeof(MagVector)+1;
200
                                RxBytes = sizeof(MagVector)+1;
201
                                pRxHandlerFunc = &MK3MAG_UpdateMagVector;
201
                                pRxHandlerFunc = &MK3MAG_UpdateMagVector;
Line 204... Line 204...
204
                                RxBytes = 0;
204
                                RxBytes = 0;
205
                                pRxHandlerFunc = NULL;
205
                                pRxHandlerFunc = NULL;
206
                                break;
206
                                break;
207
                }
207
                }
208
                // update packet checksum
208
                // update packet checksum
209
                TxData[TxBytes] = MK3MAG_CalcCRC(TxData, TxBytes);
209
                I2C1_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C1_Buffer, TxBytes);
210
                TxBytes++;
210
                TxBytes++;
211
                // initiate I2C transmission
211
                // initiate I2C transmission
212
                I2CBus_Transmission(I2C1, MK3MAG_SLAVE_ADDRESS, TxData, TxBytes, pRxHandlerFunc, RxBytes);
212
                I2C1_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes);
213
        } // EOF I2C_State == I2C_IDLE  
213
        } // EOF I2C_State == I2C_IDLE  
214
}
214
}
Line 215... Line 215...
215
 
215
 
216
 
216
 
217
//----------------------------------------------------------------
217
//----------------------------------------------------------------
218
u8 MK3MAG_Init(void)
-
 
219
{
-
 
220
        if(MK3MAG_Present) // do only short init ! , full init was called before
-
 
221
        {
-
 
222
                // try reconnect by reseting the I2C bus
-
 
223
                I2CBus_Deinit(I2C1);
-
 
224
                I2CBus_Init(I2C1);
-
 
225
        }
-
 
226
        else // full init
218
u8 MK3MAG_Init(void)
227
        {
219
{
228
                u8 msg[64];
220
        u8 msg[64];
229
                u8 repeat;
-
 
230
                u32 timeout;
-
 
231
               
-
 
232
                Compass_I2CPort = I2C1;
-
 
233
                MK3MAG_Present = 0;
-
 
234
       
-
 
235
                MK3MAG_Version.Major = 0xFF;
-
 
236
                MK3MAG_Version.Minor = 0xFF;
-
 
Line -... Line 221...
-
 
221
        u8 repeat;
-
 
222
        u32 timeout;
-
 
223
       
-
 
224
        MK3MAG_Present = 0;
-
 
225
 
-
 
226
        MK3MAG_Version.Major = 0xFF;
-
 
227
        MK3MAG_Version.Minor = 0xFF;
-
 
228
        MK3MAG_Version.Patch = 0xFF;
-
 
229
        MK3MAG_Version.Compatible = 0xFF;
237
                MK3MAG_Version.Patch = 0xFF;
230
 
238
                MK3MAG_Version.Compatible = 0xFF;
231
        Compass_Heading = -1;
239
       
232
 
-
 
233
        // polling of version info
-
 
234
        repeat = 0;
-
 
235
        do
-
 
236
        {
-
 
237
                MK3MAG_SendCommand(MK3MAG_CMD_VERSION);
-
 
238
                if(Version_HW > 11) timeout = SetDelay(100);
240
                // polling of version info
239
                else timeout = SetDelay(250);
241
                repeat = 0;
-
 
242
                do
-
 
243
                {
-
 
244
                        MK3MAG_SendCommand(MK3MAG_CMD_VERSION);
-
 
245
                        if(Version_HW > 11) timeout = SetDelay(100);
-
 
246
                        else timeout = SetDelay(250);
-
 
247
       
240
 
248
                        do
241
                do
249
                        {
242
                {
250
                                if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
243
                        if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
251
                        }while (!CheckDelay(timeout));
244
                }while (!CheckDelay(timeout));
252
                        UART1_PutString(".");
245
                UART1_PutString(".");
253
                        repeat++;
246
                repeat++;
-
 
247
        }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
-
 
248
        // if we got it
-
 
249
        if (MK3MAG_Version.Major != 0xFF)
-
 
250
        {
254
                }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
251
                sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
255
                // if we got it
-
 
256
                if (MK3MAG_Version.Major != 0xFF)
-
 
257
                {
-
 
258
                        sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
-
 
259
                        UART1_PutString(msg);
252
                UART1_PutString(msg);
260
                        if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
253
                if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE)
261
                        {
254
                {
262
                                UART1_PutString("\n\r MK3MAG not compatible!");
255
                        UART1_PutString("\n\r MK3MAG not compatible!");
263
                                UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
256
                        UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE;
264
                                LED_RED_ON;
257
                        LED_RED_ON;
265
                        }
258
                }
266
                        else
-
 
267
                        {       // version ok
259
                else
268
                                MK3MAG_Present = 1;
260
                {       // version ok
269
                        }
261
                        MK3MAG_Present = 1;
270
                }
262
                }
Line 279... Line 271...
279
        static s16 x_max,y_max,z_max,x_min,y_min,z_min;
271
        static s16 x_max,y_max,z_max,x_min,y_min,z_min;
280
        static u8 last_state,speak = 0;
272
        static u8 last_state,speak = 0;
281
        u8 msg[64];
273
        u8 msg[64];
282
        u16 MinCaclibration = 500;
274
        u16 MinCaclibration = 500;
Line 283... Line 275...
283
 
275
 
Line 284... Line 276...
284
        if( (I2CBus(I2C1)->State == I2C_STATE_UNDEF) || !MK3MAG_Present ) return;
276
        if( (I2C1_State == I2C_STATE_OFF) || !MK3MAG_Present ) return;
285
       
277
       
286
        if(CheckDelay(TimerUpdate))
278
        if(CheckDelay(TimerUpdate))
287
        {
279
        {