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); |
|