Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 464 → Rev 465

/trunk/config.h
37,9 → 37,10
//#define PRIORITY_UART1 0 // VIC1.2: debug uart
#define PRIORITY_UART1 3 // VIC1.2: debug uart
 
//<o> VIC1.4: i2c to MK3MAG <0-15>
//<o> VIC1.4: i2c1 to compass and eeprom<0-15>
#define PRIORITY_I2C1 4 // VIC1.4: i2c to MK3MAG
#define PRIORITY_I2C0 5 // VIC1.5: i2c to MK3MAG
//<o> VIC1.3: i2c0 to compass <0-15>
#define PRIORITY_I2C0 5 // VIC1.3: i2c to external compass
 
//<o> VIC1.5: SPI0 <0-15>
//#define PRIORITY_SPI0 3 // VIC1.5: timing forced by FC, must be lower than UARTS for flashing FC thrue the NC
64,6 → 65,7
 
//<o> I2C-Bus1 <22000=> 20 kBit <50000=> 50 kBit <100000=> 100 kBit <200000=> 200 kBit <400000=> 400 kBit
#define I2C1_CLOCK 50000 // Bit Rate for I2C
#define I2C0_CLOCK 50000 // Bit Rate for I2C
 
//</h>
//<<< end of configuration section >>>
/trunk/i2c.c
88,7 → 88,7
I2C_State = I2C_STATE_OFF;
 
UART1_PutString("\r\n I2C init...");
UART1_PutString("\r\n I2C1 init...");
// enable Port 2 peripherie
SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
// disable a reset state
189,7 → 189,7
void I2C1_Deinit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
UART1_PutString("\r\n I2C deinit...");
UART1_PutString("\r\n I2C1 deinit...");
I2C_GenerateStart(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
VIC_ITCmd(I2C1_ITLine, DISABLE);
223,6 → 223,7
{
static u8 Rx_Idx = 0, Tx_Idx = 0;
u16 status;
u8 text[20];
 
//IENABLE; // do not enable IRQ nesting for I2C!!!!
// detemine I2C State
242,6 → 243,9
}
else
{ // depending on current i2c state
sprintf(text, "%02X ", status);
UART1_PutString(text);
switch(status)
{
// the start condition was initiated on the bus
/trunk/i2c0.c
107,7 → 107,7
// configure P2.2->I2C0_CLKOUT and P2.3->I2C0_DOUT to normal port operation
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
156,7 → 156,7
I2C_StructInit(&I2C_Struct);
I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
I2C_Struct.I2C_Ack = I2C_Ack_Enable;
I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
I2C_Struct.I2C_CLKSpeed = I2C0_CLOCK;
I2C_Struct.I2C_OwnAddress = 0x00;
I2C_Init(I2C0, &I2C_Struct);
 
190,7 → 190,7
void I2C0_Deinit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
UART1_PutString("\r\n I2C deinit...");
UART1_PutString("\r\n I2C0 deinit...");
I2C_GenerateStart(I2C0, DISABLE);
I2C_GenerateSTOP(I2C0, ENABLE);
VIC_ITCmd(I2C0_ITLine, DISABLE);
224,7 → 224,10
{
static u8 Rx_Idx = 0, Tx_Idx = 0;
u16 status;
u8 text[20];
 
 
//IENABLE; // do not enable IRQ nesting for I2C!!!!
// detemine I2C State
status = I2C_GetLastEvent(I2C0);
243,6 → 246,9
}
else
{ // depending on current i2c state
 
sprintf(text, "%02X ", status);
UART1_PutString(text);
switch(status)
{
// the start condition was initiated on the bus
295,7 → 301,7
I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
Tx_Idx++;
// reset timeout
I2C0_Timeout = SetDelay(I2C0_Timeout); // after inactivity the I2C0 bus will be reset
I2C0_Timeout = SetDelay(I2C0_TIMEOUT); // after inactivity the I2C0 bus will be reset
break;
 
case I2C_STATE_RX_PROGRESS:
356,7 → 362,7
I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
// call the rx handler function to process recieved data
if(I2C0_pRxHandler != NULL) (*I2C0_pRxHandler)((u8*)I2C0_Buffer, I2C0_RxBufferSize);
I2C0_Timeout = SetDelay(I2C0_Timeout);
I2C0_Timeout = SetDelay(I2C0_TIMEOUT);
DebugOut.Analog[15]++;
VIC_ITCmd(I2C0_ITLine, DISABLE);
I2C0_State = I2C_STATE_IDLE;
/trunk/libstr91x/include/91x_conf.h
106,7 → 106,7
//#define _WDG
/************************* I2C *************************/
#define _I2C
//#define _I2C0
#define _I2C0
#define _I2C1
/************************ ENET *************************/
//#define _ENET
/trunk/main.c
506,7 → 506,9
// initialize SPI0 to FC
SPI0_Init();
// initialize i2c bus (needs Timer 1)
I2C0_Init();
I2C1_Init();
 
// initialize fat16 partition on sd card (needs Timer 1)
Fat16_Init();
// initialize NC params
/trunk/mk3mag.c
171,7 → 171,7
#define MK3MAG_CMD_WRITE_CAL 0x04
 
// use I2C1 for communication
void MK3MAG_SendCommandI2C1(u8 command)
void MK3MAG_SendCommand(u8 command)
{
// try to catch the I2C buffer
if(I2C_LockBuffer(0))
213,55 → 213,7
} // EOF I2C_State == I2C_IDLE
}
 
// use I2C0 for communication
void MK3MAG_SendCommandI2C0(u8 command)
{
// try to catch the I2C buffer
if(I2C0_LockBuffer(0))
{
u16 TxBytes = 0;
u16 RxBytes = 0;
I2C_pRxHandler_t pRxHandlerFunc = NULL;
// update current command id
I2C0_Buffer[TxBytes++] = command;
 
// set pointers to data area with respect to the command id
switch (command)
{
case MK3MAG_CMD_VERSION:
RxBytes = sizeof(MK3MAG_Version)+1;
pRxHandlerFunc = &MK3MAG_UpdateVersion;
break;
case MK3MAG_CMD_WRITE_CAL:
RxBytes = sizeof(MK3MAG_ReadCal)+1;
pRxHandlerFunc = &MK3MAG_UpdateCalibration;
memcpy((u8*)I2C0_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
TxBytes += sizeof(MK3MAG_WriteCal);
break;
case MK3MAG_CMD_READ_MAGVECT:
RxBytes = sizeof(MagVector)+1;
pRxHandlerFunc = &MK3MAG_UpdateMagVector;
break;
default: // unknown command id
RxBytes = 0;
pRxHandlerFunc = NULL;
break;
}
// update packet checksum
I2C0_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C0_Buffer, TxBytes);
TxBytes++;
// initiate I2C transmission
I2C0_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes);
} // EOF I2C_State == I2C_IDLE
}
 
void MK3MAG_SendCommand(u8 command)
{
// MK3MAG_SendCommandI2C1(command);
MK3MAG_SendCommandI2C0(command);
}
 
//----------------------------------------------------------------
u8 MK3MAG_Init(void)
{
/trunk/ncmag.c
59,6 → 59,8
#include "91x_lib.h"
#include "ncmag.h"
#include "i2c.h"
#include "i2c0.h"
 
#include "timer1.h"
#include "led.h"
#include "uart1.h"
70,6 → 72,13
u8 NCMAG_Present = 0;
u8 NCMAG_IsCalibrated = 0;
 
u8 *I2C_BufferPnt;
u8 *I2C_ErrorPnt;
I2C_TransmissionFunc_t I2C_TransmissionFunc;
I2C_LockBufferFunc_t I2C_LockBufferFunc;
I2C_WaitForEndOfTransmissionFunc_t I2C_WaitForEndOfTransmissionFunc;
 
 
// supported magnetic sensor types
#define TYPE_NONE 0
#define TYPE_HMC5843 1
220,6 → 229,9
volatile MagConfig_t MagConfig;
 
 
 
 
 
// self test value
#define LSM303DLH_TEST_XSCALE 495
#define LSM303DLH_TEST_YSCALE 495
545,17 → 557,17
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBuffer(100))
if(I2C_LockBufferFunc(100))
{
u8 TxBytes = 0;
I2C_Buffer[TxBytes++] = REG_MAG_CRA;
memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
TxBytes += sizeof(MagConfig);
if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
{
if(I2C_WaitForEndOfTransmission(100))
if(I2C_WaitForEndOfTransmissionFunc(100))
{
if(I2C_Error == I2C_ERROR_NONE) retval = 1;
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
}
}
567,15 → 579,15
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBuffer(100))
if(I2C_LockBufferFunc(100))
{
u8 TxBytes = 0;
I2C_Buffer[TxBytes++] = REG_MAG_CRA;
if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
{
if(I2C_WaitForEndOfTransmission(100))
if(I2C_WaitForEndOfTransmissionFunc(100))
{
if(I2C_Error == I2C_ERROR_NONE) retval = 1;
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
}
}
587,17 → 599,17
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBuffer(100))
if(I2C_LockBufferFunc(100))
{
u8 TxBytes = 0;
I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
TxBytes += sizeof(AccConfig);
if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
{
if(I2C_WaitForEndOfTransmission(100))
if(I2C_WaitForEndOfTransmissionFunc(100))
{
if(I2C_Error == I2C_ERROR_NONE) retval = 1;
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
}
}
609,15 → 621,15
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBuffer(100))
if(I2C_LockBufferFunc(100))
{
u8 TxBytes = 0;
I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
{
if(I2C_WaitForEndOfTransmission(100))
if(I2C_WaitForEndOfTransmissionFunc(100))
{
if(I2C_Error == I2C_ERROR_NONE) retval = 1;
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
}
}
629,19 → 641,19
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBuffer(100))
if(I2C_LockBufferFunc(100))
{
u16 TxBytes = 0;
NCMAG_Identification.A = 0xFF;
NCMAG_Identification.B = 0xFF;
NCMAG_Identification.C = 0xFF;
I2C_Buffer[TxBytes++] = REG_MAG_IDA;
I2C_BufferPnt[TxBytes++] = REG_MAG_IDA;
// initiate transmission
if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
{
if(I2C_WaitForEndOfTransmission(100))
if(I2C_WaitForEndOfTransmissionFunc(100))
{
if(I2C_Error == I2C_ERROR_NONE) retval = 1;
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
}
}
652,17 → 664,17
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBuffer(100))
if(I2C_LockBufferFunc(100))
{
u16 TxBytes = 0;
NCMAG_Identification2.Sub = 0xFF;
I2C_Buffer[TxBytes++] = REG_MAG_IDF;
I2C_BufferPnt[TxBytes++] = REG_MAG_IDF;
// initiate transmission
if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
{
if(I2C_WaitForEndOfTransmission(100))
if(I2C_WaitForEndOfTransmissionFunc(100))
{
if(I2C_Error == I2C_ERROR_NONE) retval = 1;
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
}
}
}
674,13 → 686,13
void NCMAG_GetMagVector(void)
{
// try to catch the I2C buffer within 0 ms
if(I2C_LockBuffer(0))
if(I2C_LockBufferFunc(0))
{
u16 TxBytes = 0;
// set register pointer
I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB;
// initiate transmission
I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
}
}
 
688,13 → 700,13
void NCMAG_GetAccVector(void)
{
// try to catch the I2C buffer within 0 ms
if(I2C_LockBuffer(0))
if(I2C_LockBufferFunc(0))
{
u16 TxBytes = 0;
// set register pointer
I2C_Buffer[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
// initiate transmission
I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
}
}
 
904,6 → 916,27
 
 
//----------------------------------------------------------------
void NCMAG_SelectI2CBus(u8 busno)
{
if (busno == 0)
{
I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission;
I2C_LockBufferFunc = &I2C0_LockBuffer;
I2C_TransmissionFunc = &I2C0_Transmission;
I2C_BufferPnt = I2C0_Buffer;
I2C_ErrorPnt = &I2C0_Error;
}
else
{
I2C_WaitForEndOfTransmissionFunc = &I2C_WaitForEndOfTransmission;
I2C_LockBufferFunc = &I2C_LockBuffer;
I2C_TransmissionFunc = &I2C_Transmission;
I2C_BufferPnt = I2C_Buffer;
I2C_ErrorPnt = &I2C_Error;
}
}
 
//----------------------------------------------------------------
u8 NCMAG_Init(void)
{
u8 msg[64];
910,6 → 943,8
u8 retval = 0;
u8 repeat;
 
NCMAG_SelectI2CBus(0);
 
NCMAG_Present = 0;
NCMAG_SensorType = TYPE_HMC5843; // assuming having an HMC5843
// polling for LSM302DLH/DLM option by ACC address ack
918,7 → 953,7
{
retval = NCMAG_GetAccConfig();
if(retval) break; // break loop on success
UART1_PutString(".");
UART1_PutString("*");
repeat++;
}while(repeat < 3);
if(retval)
947,7 → 982,7
{
retval = NCMAG_GetIdentification();
if(retval) break; // break loop on success
UART1_PutString(".");
UART1_PutString("#");
repeat++;
}while(repeat < 12);
 
/trunk/ncmag.h
2,6 → 2,7
#define __NCMAG_H
 
#include "compass.h"
#include "i2c.h"
 
extern s16vec_t ACC_Vector;
extern volatile s16vec_t MagRawVector;
11,5 → 12,13
extern u8 NCMAG_Present;
extern u8 NCMAG_IsCalibrated;
 
 
extern u8 *I2C_BufferPnt;
extern u8 *I2C_ErrorPnt;
 
typedef u8 (*I2C_TransmissionFunc_t) (u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
typedef u8 (*I2C_LockBufferFunc_t) (u32 timeout);
typedef u8 (*I2C_WaitForEndOfTransmissionFunc_t) (u32 timeout);
 
#endif // __NCMAG_H