152 lines
4.7 KiB
C
152 lines
4.7 KiB
C
/*
|
|
*文件名称:ist8310.c
|
|
*功能描述:C板板载磁力计IST8310驱动(硬件I2C3版)
|
|
*备注:总线I2C3(PA8/PC9,100kHz),RSTN=PG6,初始化前须先拉高RSTN释放复位态
|
|
*/
|
|
#include "ist8310.h"
|
|
#include "i2c.h"
|
|
#include "main.h"
|
|
#include "led.h"
|
|
|
|
#define IST8310_ADDR (0x0EU << 1)//HAL 8位I2C从机地址
|
|
#define IST8310_WHO_AM_I 0x00U //设备ID寄存器
|
|
#define IST8310_CNTL1 0x0AU //控制寄存器1
|
|
#define IST8310_CNTL2 0x0BU //控制寄存器2
|
|
#define IST8310_AVGCNTL 0x41U //采样平均配置寄存器
|
|
#define IST8310_PDCNTL 0x42U //脉冲时长配置寄存器
|
|
#define IST8310_DATA_START 0x03U //磁场数据起始寄存器
|
|
#define IST8310_WHO_AM_I_VALUE 0x10U //预期WHO_AM_I值
|
|
#define IST8310_POLL_PERIOD_MS 5U //200Hz轮询周期(ms)
|
|
|
|
float ist8310_magnetic_field[3] = {0.0f};//三轴磁场强度(uT)
|
|
float ist8310_temperature = 0.0f; //温度(℃)
|
|
uint8_t ist8310_online = 0U; //在线标志
|
|
uint8_t ist8310_last_who = 0U; //最近读取到的WHO_AM_I
|
|
|
|
static uint8_t raw_buf[6] = {0}; //I2C六字节原始数据缓冲
|
|
static uint8_t read_error_count = 0U; //连续读失败计数
|
|
|
|
/*
|
|
*函数简介:控制IST8310复位引脚RSTN(PG6)电平
|
|
*参数说明:level:1=释放复位(高电平),0=拉低复位
|
|
*返回类型:无
|
|
*备注:内部函数,电平变化后延时1ms等待生效
|
|
*/
|
|
static void ist8310_rstn(uint8_t level)
|
|
{
|
|
HAL_GPIO_WritePin(RSTN_IST8310_GPIO_Port, RSTN_IST8310_Pin,
|
|
level ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
|
HAL_Delay(1);
|
|
}
|
|
|
|
/*
|
|
*函数简介:向IST8310指定寄存器写入单字节
|
|
*参数说明:reg:寄存器地址
|
|
*参数说明:val:写入值
|
|
*返回类型:无
|
|
*备注:内部函数
|
|
*/
|
|
static void ist8310_write_reg(uint8_t reg, uint8_t val)
|
|
{
|
|
(void)HAL_I2C_Mem_Write(&hi2c3, IST8310_ADDR, reg,
|
|
I2C_MEMADD_SIZE_8BIT, &val, 1U, 10U);
|
|
}
|
|
|
|
/*
|
|
*函数简介:读取IST8310 WHO_AM_I寄存器
|
|
*参数说明:无
|
|
*返回类型:uint8_t:WHO_AM_I值,读取失败返回0
|
|
*备注:内部函数
|
|
*/
|
|
static uint8_t ist8310_read_who(void)
|
|
{
|
|
uint8_t who = 0U;
|
|
if (HAL_I2C_Mem_Read(&hi2c3, IST8310_ADDR, IST8310_WHO_AM_I,
|
|
I2C_MEMADD_SIZE_8BIT, &who, 1U, 10U) != HAL_OK)
|
|
return 0U;
|
|
return who;
|
|
}
|
|
|
|
/*
|
|
*函数简介:ID校验失败时的绿灯慢闪报警延时
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:内部函数,由ist8310_init重试循环调用
|
|
*/
|
|
static void ist8310_error_blink(void)
|
|
{
|
|
static uint8_t counter = 0U;
|
|
counter++;
|
|
if (counter == 1U)
|
|
led_set_green(1);
|
|
else if (counter == 5U)
|
|
led_set_green(0);
|
|
else if (counter == 40U)
|
|
counter = 0U;
|
|
HAL_Delay(25);
|
|
}
|
|
|
|
/*
|
|
*函数简介:初始化IST8310磁力计(硬件I2C3)
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:先拉高RSTN解除CubeMX默认低电平复位态,有界重试40次
|
|
*/
|
|
void ist8310_init(void)
|
|
{
|
|
HAL_GPIO_WritePin(RSTN_IST8310_GPIO_Port, RSTN_IST8310_Pin, GPIO_PIN_SET);
|
|
HAL_Delay(2);
|
|
|
|
for (uint8_t retry = 0U; retry < 40U; retry++)
|
|
{
|
|
ist8310_last_who = ist8310_read_who();
|
|
if (ist8310_last_who == IST8310_WHO_AM_I_VALUE)
|
|
{
|
|
ist8310_online = 1U;
|
|
ist8310_write_reg(IST8310_CNTL1, 0x03U); //连续模式200Hz
|
|
ist8310_write_reg(IST8310_CNTL2, 0x08U); //DRDY下降沿
|
|
ist8310_write_reg(IST8310_AVGCNTL, 0x12U);//4次采样平均
|
|
ist8310_write_reg(IST8310_PDCNTL, 0xC0U); //正常脉冲时长
|
|
ist8310_rstn(0U);
|
|
ist8310_rstn(1U);
|
|
return;
|
|
}
|
|
ist8310_error_blink();
|
|
}
|
|
}
|
|
|
|
/*
|
|
*函数简介:周期轮询读取IST8310三轴磁场数据
|
|
*参数说明:无
|
|
*返回类型:无
|
|
*备注:连续100次读取失败判定离线并置ist8310_online=0
|
|
*/
|
|
void ist8310_poll(void)
|
|
{
|
|
static uint32_t last_poll_tick = 0U;
|
|
uint32_t now = HAL_GetTick();
|
|
|
|
if (now - last_poll_tick < IST8310_POLL_PERIOD_MS)
|
|
return;
|
|
last_poll_tick = now;
|
|
|
|
if (!ist8310_online)
|
|
return;
|
|
|
|
if (HAL_I2C_Mem_Read(&hi2c3, IST8310_ADDR, IST8310_DATA_START,
|
|
I2C_MEMADD_SIZE_8BIT, raw_buf, 6U, 2U) != HAL_OK)
|
|
{
|
|
if (++read_error_count > 100U)
|
|
ist8310_online = 0U;
|
|
return;
|
|
}
|
|
read_error_count = 0U;
|
|
|
|
int16_t raw_x = (int16_t)(((uint16_t)raw_buf[1] << 8) | raw_buf[0]);
|
|
int16_t raw_y = (int16_t)(((uint16_t)raw_buf[3] << 8) | raw_buf[2]);
|
|
int16_t raw_z = (int16_t)(((uint16_t)raw_buf[5] << 8) | raw_buf[4]);
|
|
ist8310_magnetic_field[0] = (float)raw_x * 0.3f;
|
|
ist8310_magnetic_field[1] = (float)raw_y * 0.3f;
|
|
ist8310_magnetic_field[2] = (float)raw_z * 0.3f;
|
|
}
|