|
|
|
@ -3,11 +3,13 @@
|
|
|
|
|
#include "app_i2c_master.h"
|
|
|
|
|
#include "app_log.h"
|
|
|
|
|
#include "sw_timer.h"
|
|
|
|
|
#include "app_oled.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define AK9757W_CHIPID_READ_WAIT_TIME 100
|
|
|
|
|
#define AK9757W_ERROR_PRINT_INTERVAL 1000
|
|
|
|
|
#define AK9757W_MEASUREMENT_INTERVAL 1000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -28,14 +30,18 @@ const uint8_t default_gir_oir_gts_ots_git_reg_data[] = {0x53, 0xE0, 0x94, 0x12,
|
|
|
|
|
static bool AK9757W_Check_CompanyCode(void);
|
|
|
|
|
static bool AK9757W_Set_Operation_Mode(AK9757W_MODE mode);
|
|
|
|
|
static bool AK9757W_Set_ADC_Inverter(AK9757W_MODE mode);
|
|
|
|
|
|
|
|
|
|
static bool AK9757W_Set_CNTL1_9_Parameter(void);
|
|
|
|
|
static bool AK9757W_Set_FCOEF4_0_Parameter(void);
|
|
|
|
|
static bool AK9757W_Set_GCOEF4_0_Parameter(void);
|
|
|
|
|
static bool AK9757W_Set_XCOEF4_0_Parameter(void);
|
|
|
|
|
static bool AK9757W_Set_IR_TS_IT_Parameter(void);
|
|
|
|
|
static bool AK9757W_Calc_Parameter(void);
|
|
|
|
|
static bool AK9757W_Read_RawData(void);
|
|
|
|
|
static bool AK9757W_Process(void);
|
|
|
|
|
static void AK9757W_Next_Step(AK9757W_STEP nextStep);
|
|
|
|
|
static int16_t AK9757W_14BitTo16Bit(uint16_t Data);
|
|
|
|
|
static int16_t AK9757W_12BitTo16Bit(uint16_t Data);
|
|
|
|
|
static int8_t AK9757W_6BitTo8it(uint8_t Data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -48,33 +54,10 @@ bool AK9757W_Initialization(void)
|
|
|
|
|
|
|
|
|
|
AK9757W_Handler.PreStep = STEP_INIT;
|
|
|
|
|
AK9757W_Handler.Step = STEP_INIT;
|
|
|
|
|
AK9757W_Handler.isMeasurementStart = true;
|
|
|
|
|
|
|
|
|
|
SW_Timer_Callback_Register(SW_TIMER_RUN_CONTINUE, 1, AK9757W_Process);
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
if(AK9757W_Check_CompanyCode() == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(AK9757_Set_Mode(AK9757W_MODE_CONTINUOUS_MODE) == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Delay_ms(200);
|
|
|
|
|
|
|
|
|
|
if(AK9757_Get_SensorID() == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AK9757W_Handler.isInitComplete = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -163,8 +146,43 @@ static bool AK9757W_Process(void)
|
|
|
|
|
AK9757W_Next_Step(STEP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
AK9757W_Next_Step(STEP_CALC_PARAMETER);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case STEP_CALC_PARAMETER:
|
|
|
|
|
AK9757W_Calc_Parameter();
|
|
|
|
|
AK9757W_Next_Step(STEP_WAIT_START);
|
|
|
|
|
break;
|
|
|
|
|
case STEP_WAIT_START:
|
|
|
|
|
if(pHandler->isMeasurementStart == true)
|
|
|
|
|
AK9757W_Next_Step(STEP_MEASUREMENT_START);
|
|
|
|
|
break;
|
|
|
|
|
case STEP_MEASUREMENT_START:
|
|
|
|
|
if(AK9757W_Set_Mode(AK9757W_MODE_CONTINUOUS_MODE) == false){
|
|
|
|
|
AK9757W_Next_Step(STEP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
pHandler->TickCount = millis();
|
|
|
|
|
AK9757W_Next_Step(STEP_MEASUREMENT);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case STEP_MEASUREMENT:
|
|
|
|
|
if((millis() - pHandler->TickCount) >= AK9757W_MEASUREMENT_INTERVAL)
|
|
|
|
|
{
|
|
|
|
|
pHandler->TickCount = millis();
|
|
|
|
|
AK9757W_Read_RawData();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case STEP_WAIT_STOP:
|
|
|
|
|
if(AK9757W_Set_Mode(AK9757W_MODE_STAND_BY_MODE) == false){
|
|
|
|
|
AK9757W_Next_Step(STEP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
pHandler->isMeasurementStart = false;
|
|
|
|
|
AK9757W_Next_Step(STEP_CALC_PARAMETER);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -347,11 +365,204 @@ static bool AK9757W_Set_IR_TS_IT_Parameter(void)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool AK9757W_Calc_Parameter(void)
|
|
|
|
|
{
|
|
|
|
|
int error;
|
|
|
|
|
uint8_t uint8_TempValue;
|
|
|
|
|
int8_t int8_TempValue;
|
|
|
|
|
uint16_t uint16_TempValue;
|
|
|
|
|
int16_t int16_TempValue;
|
|
|
|
|
uint8_t TxData;
|
|
|
|
|
uint8_t* pRxBuff;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TxData = AK9757W_REG_PARAMETER;
|
|
|
|
|
pRxBuff = &pHandler->IR_TS_IT_Info.IR_TS_IT_Buff[0];
|
|
|
|
|
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxData, 1, pRxBuff, 9);
|
|
|
|
|
if(error != E_NO_ERROR)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
TxData = AK9757W_REG_RW_FCOEF4L;
|
|
|
|
|
pRxBuff = &pHandler->Fcoef_Info.FCOEF_Buff[0];
|
|
|
|
|
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxData, 1, pRxBuff, 14);
|
|
|
|
|
if(error != E_NO_ERROR)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TxData = AK9757W_REG_RW_GCOEF4L;
|
|
|
|
|
pRxBuff = &pHandler->Gcoef_Info.GCOEF_Buff[0];
|
|
|
|
|
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxData, 1, pRxBuff, 14);
|
|
|
|
|
if(error != E_NO_ERROR)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TxData = AK9757W_REG_RW_XCOEF4L;
|
|
|
|
|
pRxBuff = &pHandler->Xcoef_Info.XCOEF_Buff[0];
|
|
|
|
|
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxData, 1, pRxBuff, 14);
|
|
|
|
|
if(error != E_NO_ERROR)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
pHandler->calc_gts = ((double)((int8_t)pHandler->IR_TS_IT_Info.IR_TS_IT.gts) * (double)TWO_POW_M10);
|
|
|
|
|
|
|
|
|
|
uint16_TempValue = (uint16_t)(pHandler->IR_TS_IT_Info.IR_TS_IT.ots_h << 8) | (uint16_t)pHandler->IR_TS_IT_Info.IR_TS_IT.ots_l;
|
|
|
|
|
pHandler->calc_ots = AK9757W_14BitTo16Bit(uint16_TempValue);
|
|
|
|
|
|
|
|
|
|
pHandler->calc_git = (double)((double)((int8_t)pHandler->IR_TS_IT_Info.IR_TS_IT.git) * (double)TWO_POW_M23);
|
|
|
|
|
|
|
|
|
|
uint16_TempValue = (uint16_t)(pHandler->IR_TS_IT_Info.IR_TS_IT.gir_h << 8) | pHandler->IR_TS_IT_Info.IR_TS_IT.gir_l;
|
|
|
|
|
pHandler->calc_gir = (double)((double)uint16_TempValue* (double)TWO_POW_M14);
|
|
|
|
|
|
|
|
|
|
uint16_TempValue = (uint16_t)(pHandler->IR_TS_IT_Info.IR_TS_IT.oir_h << 8) | pHandler->IR_TS_IT_Info.IR_TS_IT.oir_l;
|
|
|
|
|
pHandler->calc_oir = AK9757W_12BitTo16Bit(uint16_TempValue);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
int16_TempValue = (pHandler->Fcoef_Info.FCOEF.fcoef4_h << 8) | pHandler->Fcoef_Info.FCOEF.fcoef4_l;
|
|
|
|
|
uint8_TempValue = pHandler->Fcoef_Info.FCOEF.fcoef4_ex;
|
|
|
|
|
pHandler->calc_FC4 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Fcoef_Info.FCOEF.fcoef3_h << 8) | pHandler->Fcoef_Info.FCOEF.fcoef3_l;
|
|
|
|
|
uint8_TempValue = pHandler->Fcoef_Info.FCOEF.fcoef3_ex;
|
|
|
|
|
pHandler->calc_FC3 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Fcoef_Info.FCOEF.fcoef2_h << 8) | pHandler->Fcoef_Info.FCOEF.fcoef2_l;
|
|
|
|
|
uint8_TempValue = pHandler->Fcoef_Info.FCOEF.fcoef2_ex & 0x3F;
|
|
|
|
|
pHandler->calc_FC2 = (double)int16_TempValue * pow(2, ((14+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Fcoef_Info.FCOEF.fcoef1_h << 8) | pHandler->Fcoef_Info.FCOEF.fcoef1_l;
|
|
|
|
|
uint8_TempValue = pHandler->Fcoef_Info.FCOEF.fcoef1_ex & 0x1F;
|
|
|
|
|
pHandler->calc_FC1 = (double)int16_TempValue * pow(2, ((14+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Fcoef_Info.FCOEF.fcoef0_h << 8) | pHandler->Fcoef_Info.FCOEF.fcoef0_l;
|
|
|
|
|
pHandler->calc_FC0 = (double)int16_TempValue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
int16_TempValue = (pHandler->Gcoef_Info.GCOEF.gcoef4_h << 8) | pHandler->Gcoef_Info.GCOEF.gcoef4_l;
|
|
|
|
|
uint8_TempValue = pHandler->Gcoef_Info.GCOEF.gcoef4_ex;
|
|
|
|
|
pHandler->calc_GC4 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Gcoef_Info.GCOEF.gcoef3_h << 8) | pHandler->Gcoef_Info.GCOEF.gcoef3_l;
|
|
|
|
|
uint8_TempValue = pHandler->Gcoef_Info.GCOEF.gcoef3_ex;
|
|
|
|
|
pHandler->calc_GC3 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Gcoef_Info.GCOEF.gcoef2_h << 8) | pHandler->Gcoef_Info.GCOEF.gcoef2_l;
|
|
|
|
|
uint8_TempValue = pHandler->Gcoef_Info.GCOEF.gcoef2_ex & 0x3F;
|
|
|
|
|
pHandler->calc_GC2 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Gcoef_Info.GCOEF.gcoef1_h << 8) | pHandler->Gcoef_Info.GCOEF.gcoef1_l;
|
|
|
|
|
uint8_TempValue = pHandler->Gcoef_Info.GCOEF.gcoef1_ex & 0x1F;
|
|
|
|
|
pHandler->calc_GC1 = (double)int16_TempValue * pow(2, ((14+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Gcoef_Info.GCOEF.gcoef0_h << 8) | pHandler->Gcoef_Info.GCOEF.gcoef0_l;
|
|
|
|
|
pHandler->calc_GC0 = (double)int16_TempValue * TWO_POW_M14;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
int16_TempValue = (pHandler->Xcoef_Info.XCOEF.xcoef4_h << 8) | pHandler->Xcoef_Info.XCOEF.xcoef4_l;
|
|
|
|
|
uint8_TempValue = pHandler->Xcoef_Info.XCOEF.xcoef4_ex;
|
|
|
|
|
pHandler->calc_XC4 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Xcoef_Info.XCOEF.xcoef3_h << 8) | pHandler->Xcoef_Info.XCOEF.xcoef3_l;
|
|
|
|
|
uint8_TempValue = pHandler->Xcoef_Info.XCOEF.xcoef3_ex;
|
|
|
|
|
pHandler->calc_XC3 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Xcoef_Info.XCOEF.xcoef2_h << 8) | pHandler->Xcoef_Info.XCOEF.xcoef2_l;
|
|
|
|
|
uint8_TempValue = pHandler->Xcoef_Info.XCOEF.xcoef2_ex & 0x3F;
|
|
|
|
|
pHandler->calc_XC2 = (double)int16_TempValue * pow(2, ((15+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
int16_TempValue = (pHandler->Xcoef_Info.XCOEF.xcoef1_h << 8) | pHandler->Xcoef_Info.XCOEF.xcoef1_l;
|
|
|
|
|
uint8_TempValue = pHandler->Xcoef_Info.XCOEF.xcoef1_ex & 0x1F;
|
|
|
|
|
pHandler->calc_XC1 = (double)int16_TempValue * pow(2, ((14+uint8_TempValue) * -1));
|
|
|
|
|
|
|
|
|
|
uint16_TempValue = (pHandler->Xcoef_Info.XCOEF.xcoef0_h << 8) | pHandler->Xcoef_Info.XCOEF.xcoef0_l;
|
|
|
|
|
pHandler->calc_XC0 = (double)uint16_TempValue * TWO_POW_M10;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool AK9757W_Read_RawData(void)
|
|
|
|
|
{
|
|
|
|
|
int error;
|
|
|
|
|
uint8_t TxData = AK9757W_REG_RO_STATUS;
|
|
|
|
|
uint8_t RxBuff[7];
|
|
|
|
|
uint16_t uint16_TempValue;
|
|
|
|
|
int16_t int16_TempValue;
|
|
|
|
|
uint16_t ToutTemp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxData, 1, &RxBuff[0], 7);
|
|
|
|
|
if(error != E_NO_ERROR)
|
|
|
|
|
{
|
|
|
|
|
dbg_printf(LOG_LEVEL_DEBUG, "I2C W/R Error %d\r\n", error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pHandler->RAWDATA.Status.Status = RxBuff[0];
|
|
|
|
|
|
|
|
|
|
pHandler->RAWDATA.RAW_TO = (RxBuff[2] << 8) | RxBuff[1];
|
|
|
|
|
pHandler->RAWDATA.RAW_TS = RxBuff[4] << 8 | RxBuff[3];
|
|
|
|
|
pHandler->RAWDATA.RAW_IR = RxBuff[6] << 8 | RxBuff[5];
|
|
|
|
|
|
|
|
|
|
dbg_printf(LOG_LEVEL_DEBUG, "s = %x, to=%d, ts=%d,ir=%d\r\n", RxBuff[0], pHandler->RAWDATA.RAW_TO, pHandler->RAWDATA.RAW_TS, pHandler->RAWDATA.RAW_IR);
|
|
|
|
|
|
|
|
|
|
Oled_SetTemperature(pHandler->RAWDATA.RAW_TO/10);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int16_t AK9757W_14BitTo16Bit(uint16_t Data)
|
|
|
|
|
{
|
|
|
|
|
int16_t RetData = 0;
|
|
|
|
|
Data &= 0x3FFF;
|
|
|
|
|
|
|
|
|
|
if(Data & 0x2000)
|
|
|
|
|
Data |= 0xC000;
|
|
|
|
|
RetData |= Data;
|
|
|
|
|
return RetData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int16_t AK9757W_12BitTo16Bit(uint16_t Data)
|
|
|
|
|
{
|
|
|
|
|
int16_t RetData = 0;
|
|
|
|
|
Data &= 0x0FFF;
|
|
|
|
|
|
|
|
|
|
if(Data & 0x0800)
|
|
|
|
|
Data |= 0xF000;
|
|
|
|
|
RetData |= Data;
|
|
|
|
|
return RetData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int8_t AK9757W_6BitTo8it(uint8_t Data)
|
|
|
|
|
{
|
|
|
|
|
int8_t RetData = 0;
|
|
|
|
|
Data &= 0x3F;
|
|
|
|
|
|
|
|
|
|
if(Data & 0x20)
|
|
|
|
|
Data |= 0xC0;
|
|
|
|
|
RetData |= Data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -402,4 +613,14 @@ static void AK9757W_Next_Step(AK9757W_STEP nextStep)
|
|
|
|
|
{
|
|
|
|
|
AK9757W_Handler.PreStep = AK9757W_Handler.Step;
|
|
|
|
|
AK9757W_Handler.Step = nextStep;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool AK9757W_Start_Stop(bool isStartStop)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|