#include "i2c.h"
#include "hmc6343.h"
#include "usart.h"
#include "ms4525.h"
#include <math.h>
uint16_t heading_dat[3] = {0};
uint8_t send_buffer[3] = {0};
uint8_t recv_buffer[6] = {0};
double temperatureJ = 0.0;
double pressureJ = 0.0;
double temperatureI = 0.0;
double pressureI = 0.0;
double elevation = 0.0;
#if 0
uint8_t HMC6343_I2C_Write(uint8_t *TxData, uint16_t Size)
{
uint8_t ret = HAL_OK;
ret &= HAL_I2C_Master_Transmit_DMA(&hi2c1, HMC6343_WRITE_ADDR, TxData, Size);
return ret;
}
uint8_t HMC6343_I2C_Read(uint8_t *RxData, uint16_t Size)
{
uint8_t ret = HAL_OK;
hi2c1.State = HAL_I2C_STATE_READY;
ret &= HAL_I2C_Master_Receive_DMA(&hi2c1, HMC6343_READ_ADDR, RxData, Size);
return ret;
}
void HMC6343_Read_Reg(uint8_t *TxData, uint16_t send_Size, uint8_t *RxBuffer, uint16_t read_Size)
{
if(HAL_OK == HMC6343_I2C_Write(TxData, send_Size))
{
HMC6343_I2C_Read(RxBuffer, read_Size);
}
}
uint8_t HMC6343_Read_ID(void)
{
uint8_t Txbuffer[2] = {0};
uint8_t DEVICE_ID = 0;
Txbuffer[0] = READ_EEPROM_CMD;
Txbuffer[1] = SOFTWARE_VER_EEPROM;
HMC6343_Read_Reg(Txbuffer, 2, &DEVICE_ID, 1);
return DEVICE_ID;
}
void HMC6343_Read_Accel_Data(uint16_t *read_val, uint16_t read_size)
{
uint8_t read_pos = 0;
uint8_t HMC6343_CMD = 0;
uint8_t read_data[6] = {0};
HMC6343_CMD = ACCEL_DATA_REG;
HMC6343_Read_Reg(&HMC6343_CMD, 1, read_data, sizeof(read_data));
read_val[read_pos++] = (read_data[0] << 8) | read_data[1];
read_val[read_pos++] = (read_data[2] << 8) | read_data[3];
read_val[read_pos++] = (read_data[4] << 8) | read_data[5];
}
void HMC6343_Read_mag_Data(uint16_t *read_val, uint16_t read_size)
{
uint8_t read_pos = 0;
uint8_t HMC6343_CMD = 0;
uint8_t read_data[6] = {0};
HMC6343_CMD = MAG_DATA_REG;
HMC6343_Read_Reg(&HMC6343_CMD, 1, read_data, sizeof(read_data));
read_val[read_pos++] = (read_data[0] << 8) | read_data[1];
read_val[read_pos++] = (read_data[2] << 8) | read_data[3];
read_val[read_pos++] = (read_data[4] << 8) | read_data[5];
}
void HMC6343_Read_heading_Data(uint16_t *read_val, uint16_t read_size)
{
uint8_t read_pos = 0;
uint8_t HMC6343_CMD = 0;
uint8_t read_data[6] = {0};
HMC6343_CMD = HEADING_DATA_REG;
HMC6343_Read_Reg(&HMC6343_CMD, 1, read_data, sizeof(read_data));
read_val[read_pos++] = (read_data[0] << 8) | read_data[1];
read_val[read_pos++] = (read_data[2] << 8) | read_data[3];
read_val[read_pos++] = (read_data[4] << 8) | read_data[5];
}
void HMC6343_Read_tilt_Data(uint16_t *read_val, uint16_t read_size)
{
uint8_t read_pos = 0;
uint8_t HMC6343_CMD = 0;
uint8_t read_data[6] = {0};
HMC6343_CMD = TILT_DATA_REG;
HMC6343_Read_Reg(&HMC6343_CMD, 1, read_data, sizeof(read_data));
read_val[read_pos++] = (read_data[0] << 8) | read_data[1];
read_val[read_pos++] = (read_data[2] << 8) | read_data[3];
read_val[read_pos++] = (read_data[4] << 8) | read_data[5];
}
void HMC6343_write_cmd(uint8_t cmd)
{
HMC6343_I2C_Write(&cmd, 1);
}
void HMC6343_Calibration_Mode(void)
{
HMC6343_write_cmd(Enter_User_Calibration_Mode);
HAL_Delay(90000);
HMC6343_write_cmd(Exit_User_Calibration_Mode);
}
void HMC6343_Init(void)
{
uint8_t HMC6343_ID = 0;
uint8_t send_buffer[3] = {0};
/* check chip */
HMC6343_ID = HMC6343_Read_ID();
if(HMC6343_ID)
{
/* Reset the Processor */
HMC6343_write_cmd(RESET_PROCESSOR_REG);
send_buffer[0] = WRITE_EEPROM_CMD;
send_buffer[1] = OP_MODE2_EEPROM;
send_buffer[2] = 0x02; //10hz
HMC6343_I2C_Write(send_buffer, 1);
}
}
#else
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
{
if(hi2c == &hi2c1)
{
if(comm_get_cmd() & I2C6343_TX_CMD)
{
comm_clear_cmd(I2C6343_TX_CMD);
comm_set_cmd(I2C6343_RX_CMD);
HAL_I2C_Master_Receive_DMA(&hi2c1, HMC6343_READ_ADDR, recv_buffer, 6);
}
}
}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
{
uint8_t read_pos = 0;
uint16_t pressure_output;
uint16_t temp_output;
double pressure_psi = 0.0;
double x = 0.0;
if(hi2c == &hi2c1)
{
if(comm_get_cmd() & I2C6343_RX_CMD)
{
heading_dat[read_pos++] = (recv_buffer[0] << 8) | recv_buffer[1];
heading_dat[read_pos++] = (recv_buffer[2] << 8) | recv_buffer[3];
heading_dat[read_pos++] = (recv_buffer[4] << 8) | recv_buffer[5];
comm_clear_cmd(I2C6343_RX_CMD);
MS4525DoI_Dat_Recv();
}
else if(comm_get_cmd() & I2C4525I_RX_CMD)
{
pressure_output = (uint16_t)((ms4525i_data[0] & 0x3F) << 8) | ms4525i_data[1];
temp_output = (uint16_t)(ms4525i_data[2] << 3) | ((ms4525i_data[3] & 0xE0) >> 5);
pressure_psi = (pressure_output - 0.05 * 16383.0) * 15.0 / (0.9 * 16383.0);
pressureI= 6.895 * pressure_psi;
temperatureI = temp_output * 200.0 / 2047.0 - 50.0;
comm_clear_cmd(I2C4525I_RX_CMD);
MS4525DoJ_Dat_Recv();
}
else if(comm_get_cmd() & I2C4525J_RX_CMD)
{
pressure_output = (uint16_t)((ms4525j_data[0] & 0x3F) << 8) | ms4525j_data[1];
temp_output = (uint16_t)(ms4525j_data[2] << 3) | ((ms4525j_data[3] & 0xE0) >> 5);
pressure_psi = (pressure_output - 0.05 * 16383.0) * 15.0 / (0.9 * 16383.0);
pressureJ = 6.895 * pressure_psi;
temperatureJ = temp_output * 200.0 / 2047.0 - 50.0;
x = pow(pressureJ / 101.3, 1.0 / 5.256);
elevation = (((1.0 - x) * 6357.0) / (162.1035 - (1.0 - x))) * 1000;
comm_clear_cmd(I2C4525J_RX_CMD);
}
}
}
void HMC6343_Proc(void)
{
send_buffer[0] = HEADING_DATA_REG;
comm_set_cmd(I2C6343_TX_CMD);
HAL_I2C_Master_Transmit_DMA(&hi2c1, HMC6343_WRITE_ADDR, send_buffer, 1);
}
#endif
评论0