parent
131e37c4c2
commit
6e31ba43b8
Binary file not shown.
@ -0,0 +1,105 @@
|
||||
#include "app_i2c_master.h"
|
||||
|
||||
static mxc_i2c_regs_t* pI2C_Master = NULL;
|
||||
static sys_cfg_i2c_t sys_i2c_master_cfg = NULL; /* No system specific configuration needed. */
|
||||
|
||||
|
||||
static void I2C_Master_Handler(void);
|
||||
|
||||
|
||||
|
||||
bool I2C_Master_Initialization(mxc_i2c_regs_t* pI2C_Handler, i2c_speed_t Speed)
|
||||
{
|
||||
int error;
|
||||
|
||||
if(!(pI2C_Handler == MXC_I2C0 || pI2C_Handler == MXC_I2C1))
|
||||
return false;
|
||||
|
||||
pI2C_Master = pI2C_Handler;
|
||||
I2C_Shutdown(pI2C_Master);
|
||||
error = I2C_Init(pI2C_Master, Speed, &sys_i2c_master_cfg);
|
||||
I2C_SetTimeout(pI2C_Master, 100);
|
||||
|
||||
if(pI2C_Handler == MXC_I2C0)
|
||||
{
|
||||
NVIC_ClearPendingIRQ(I2C0_IRQn);
|
||||
NVIC_DisableIRQ(I2C0_IRQn);
|
||||
NVIC_SetVector(I2C0_IRQn, I2C_Master_Handler);
|
||||
NVIC_EnableIRQ(I2C0_IRQn);
|
||||
}
|
||||
else if(pI2C_Handler == MXC_I2C1)
|
||||
{
|
||||
NVIC_ClearPendingIRQ(I2C1_IRQn);
|
||||
NVIC_DisableIRQ(I2C1_IRQn);
|
||||
NVIC_SetVector(I2C1_IRQn, I2C_Master_Handler);
|
||||
NVIC_EnableIRQ(I2C1_IRQn);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void I2C_Master_Handler(void)
|
||||
{
|
||||
UART_Handler(pI2C_Master);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int32_t I2C_Master_Write(uint8_t SlaveAddress, uint8_t* pTxBuffer, uint32_t TxLen)
|
||||
{
|
||||
int32_t ret;
|
||||
|
||||
if(pI2C_Master == NULL)
|
||||
return E_NULL_PTR;
|
||||
|
||||
ret = I2C_MasterWrite(pI2C_Master, (SlaveAddress << 1), pTxBuffer, TxLen, 0);
|
||||
if(ret != TxLen)
|
||||
{
|
||||
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
|
||||
ret = E_COMM_ERR;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = E_NO_ERROR;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int32_t I2C_Master_Read(uint8_t SlaveAddress, uint8_t* pRxBuffer, uint32_t RxLen)
|
||||
{
|
||||
int32_t ret;
|
||||
if(pI2C_Master == NULL)
|
||||
return E_NULL_PTR;
|
||||
|
||||
if(ret = I2C_MasterRead(pI2C_Master, (SlaveAddress << 1), &pRxBuffer[0], RxLen, false) != RxLen)
|
||||
{
|
||||
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
|
||||
return E_COMM_ERR;
|
||||
}
|
||||
|
||||
return E_NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
int32_t I2C_Master_WriteRead(uint8_t SlaveAddress, uint8_t* pWriteBuff, uint32_t TxLen, uint8_t* pRxBuffer, uint32_t RxLen)
|
||||
{
|
||||
int32_t ret;
|
||||
if(pI2C_Master == NULL)
|
||||
return E_NULL_PTR;
|
||||
|
||||
if(ret = I2C_MasterWrite(pI2C_Master, (SlaveAddress << 1), &pWriteBuff[0], TxLen, true) != TxLen)
|
||||
{
|
||||
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
|
||||
return E_COMM_ERR;
|
||||
}
|
||||
|
||||
if(ret = I2C_MasterRead(pI2C_Master, (SlaveAddress << 1), &pRxBuffer[0], RxLen, false) != RxLen)
|
||||
{
|
||||
I2C_Master_Initialization(TEMP_I2C_INSTANCE, TEMP_I2C_SPEED);
|
||||
return E_COMM_ERR;
|
||||
}
|
||||
|
||||
return E_NO_ERROR;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
/** \file app_i2c_master.h */
|
||||
#if !defined(APP_I2C_MASTER_H__369DA004_50BB_4346_B148_5EE5FE8295F5__INCLUDED_)
|
||||
#define APP_I2C_MASTER_H__369DA004_50BB_4346_B148_5EE5FE8295F5__INCLUDED_
|
||||
|
||||
#include "board_config.h"
|
||||
|
||||
|
||||
bool I2C_Master_Initialization(mxc_i2c_regs_t* pI2C_Handler, i2c_speed_t Speed);
|
||||
int32_t I2C_Master_Write(uint8_t SlaveAddress, uint8_t* pTxBuffer, uint32_t TxLen);
|
||||
int32_t I2C_Master_Read(uint8_t SlaveAddress, uint8_t* pRxBuffer, uint32_t RxLen);
|
||||
int32_t I2C_Master_WriteRead(uint8_t SlaveAddress, uint8_t* pWriteBuff, uint32_t TxLen, uint8_t* pRxBuffer, uint32_t RxLen);
|
||||
|
||||
#endif
|
@ -1 +1,271 @@
|
||||
#include "ak9757w.h"
|
||||
#include "ak9757w.h"
|
||||
#include "ak9757w_def.h"
|
||||
#include "app_i2c_master.h"
|
||||
#include "app_log.h"
|
||||
#include "sw_timer.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static AK9757W_HANDLER AK9757W_Handler;
|
||||
static AK9757W_HANDLER* pHandler = &AK9757W_Handler;
|
||||
|
||||
const uint8_t default_cntl_reg_data[] = {0x20, 0xFF, 0xFE, 0x4A, 0xE1, 0x00, 0x00, 0x00, 0xDF, 0xE0};
|
||||
|
||||
|
||||
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_Process(void);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool AK9757W_Initialization(void)
|
||||
{
|
||||
|
||||
|
||||
SW_Timer_Callback_Register(SW_TIMER_RUN_CONTINUE, 10, 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
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static bool AK9757W_Process(void)
|
||||
{
|
||||
switch(pHandler->Step)
|
||||
{
|
||||
case STEP_INIT:
|
||||
AK9757W_Handler.isInitComplete = false;
|
||||
AK9757W_Handler.SensorID = 0xFFFFFFFF;
|
||||
pHandler->Step = STEP_CHECK_COMPANY;
|
||||
break;
|
||||
|
||||
case STEP_CHECK_COMPANY:
|
||||
if(AK9757W_Check_CompanyCode() == false){
|
||||
pHandler->Step = STEP_ERROR;
|
||||
}else{
|
||||
pHandler->Step = STEP_CHECK_ID_POWER_ON;
|
||||
}
|
||||
break;
|
||||
case STEP_CHECK_ID_POWER_ON:
|
||||
if(AK9757_Set_Mode(AK9757W_MODE_CONTINUOUS_MODE) == false){
|
||||
pHandler->Step = STEP_ERROR;
|
||||
}else{
|
||||
pHandler->TickCount = millis();
|
||||
AK9757W_Handler.Step = STEP_CHECK_ID_POWER_ON_WAIT;
|
||||
}
|
||||
break;
|
||||
case STEP_CHECK_ID_POWER_ON_WAIT:
|
||||
if((millis() - pHandler->TickCount) >= AK9757W_CHIPID_READ_WAIT_TIME)
|
||||
pHandler->Step = STEP_CHECK_ID_READ;
|
||||
break;
|
||||
case STEP_CHECK_ID_READ:
|
||||
if(AK9757_Get_SensorID() == false){
|
||||
pHandler->Step = STEP_ERROR;
|
||||
}
|
||||
else{
|
||||
pHandler->Step = STEP_CHECK_ID_POWER_OFF;
|
||||
}
|
||||
break;
|
||||
case STEP_CHECK_ID_POWER_OFF:
|
||||
if(AK9757_Set_Mode(AK9757W_MODE_STAND_BY_MODE) == false){
|
||||
pHandler->Step = STEP_ERROR;
|
||||
}
|
||||
else{
|
||||
AK9757W_Handler.Step = STEP_WRITE_CNTL1_9;
|
||||
}
|
||||
break;
|
||||
case STEP_WRITE_CNTL1_9:
|
||||
if(AK9757_Set_CTL1_9_Parameter() == false){
|
||||
}
|
||||
else{
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case STEP_ERROR:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static bool AK9757W_Check_CompanyCode(void)
|
||||
{
|
||||
int error;
|
||||
uint8_t TxBuff;
|
||||
uint8_t RxBuff[10];
|
||||
|
||||
TxBuff = AK9757W_REG_RO_COMPANY_CODE;
|
||||
error = I2C_Master_Write(TEMP_I2C_ADDRESS, &TxBuff, 1);
|
||||
if(error != E_NO_ERROR)
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "I2C Write Error %d\r\n", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
error = I2C_Master_Read(TEMP_I2C_ADDRESS, &RxBuff[0], 2);
|
||||
if(error != E_NO_ERROR)
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "I2C Read Error %d\r\n", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(RxBuff[0] == AK9757W_COMPANY_CODE_VALUE && RxBuff[1] == AK9757W_DEVICE_ID_VALUE))
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "Read Data %X, %X\r\n", RxBuff[0], RxBuff[1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "Company %XH, %XH\r\n", RxBuff[0], RxBuff[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool AK9757_Set_Mode(AK9757W_MODE mode)
|
||||
{
|
||||
int error;
|
||||
uint8_t TxBuff[10];
|
||||
uint8_t RxBuff[10];
|
||||
|
||||
TxBuff[0] = AK9757W_REG_RW_CNTL9;
|
||||
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxBuff[0], 1, &RxBuff[0], 1);
|
||||
if(error != E_NO_ERROR)
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "I2C Read Error %d\r\n", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
//#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "read data %x\r\n", RxBuff[0]);
|
||||
TxBuff[1] = RxBuff[0] & ~(AK9757W_CNTL9_MODE_MSK << AK9757W_CNTL9_MODE_POS);
|
||||
TxBuff[1] |= (mode << AK9757W_CNTL9_MODE_POS);
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "write data %x\r\n", TxBuff[1]);
|
||||
|
||||
error = I2C_Master_Write(TEMP_I2C_ADDRESS, &TxBuff[0], 2);
|
||||
if(error != E_NO_ERROR)
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "I2C write Error %d\r\n", error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AK9757_Set_AD_OutputDataTyte(AK9757W_IR_ADC_INVERT type)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool AK9757_Set_CTL1_9_Parameter(void)
|
||||
{
|
||||
int error;
|
||||
uint8_t i;
|
||||
memcpy(&pHandler->Cntl_Info.CNTL_Buff[0], &default_cntl_reg_data[0], sizeof(default_cntl_reg_data));
|
||||
error = I2C_Master_Write(TEMP_I2C_ADDRESS, &pHandler->Cntl_Info.CNTL_Buff[0], sizeof(default_cntl_reg_data));
|
||||
if(error != E_NO_ERROR)
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "I2C CNTL1~9 write Error %d\r\n", error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool AK9757_Get_SensorID(void)
|
||||
{
|
||||
int error;
|
||||
uint8_t TxBuff;
|
||||
uint8_t RxBuff[10];
|
||||
|
||||
TxBuff = AK9757W_REG_RO_CHIPID;
|
||||
error = I2C_Master_WriteRead(TEMP_I2C_ADDRESS, &TxBuff, 1, &RxBuff[0], 4);
|
||||
if(error != E_NO_ERROR)
|
||||
{
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "I2C Read Error %d\r\n", error);
|
||||
return false;
|
||||
}
|
||||
AK9757W_Handler.SensorID = (RxBuff[0] << 24) | (RxBuff[1] << 16) | (RxBuff[2] << 8) | (RxBuff[3] << 0);
|
||||
dbg_printf(LOG_LEVEL_DEBUG, "Read ChipID %XH\r\n", AK9757W_Handler.SensorID);
|
||||
|
||||
return true;
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue