添加了北醒激光单点测距模块,修复了遥控器离线检测命名错误,增加了i2c寄存器读写的16位地址模式

This commit is contained in:
NeoZng 2023-05-06 16:23:52 +08:00
parent 116b9b78fb
commit aea8ef4813
6 changed files with 63 additions and 11 deletions

View File

@ -103,15 +103,16 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e s
}
}
void IICAccessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode)
void IICAccessMem(IICInstance *iic, uint16_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode, uint8_t mem8bit_flag)
{
uint16_t bit_flag = mem8bit_flag ? I2C_MEMADD_SIZE_8BIT : I2C_MEMADD_SIZE_16BIT;
if (mem_mode == IIC_WRITE_MEM)
{
HAL_I2C_Mem_Write(iic->handle, iic->dev_address, mem_addr, I2C_MEMADD_SIZE_8BIT, data, size, 100);
HAL_I2C_Mem_Write(iic->handle, iic->dev_address, mem_addr, bit_flag, data, size, 100);
}
else if (mem_mode == IIC_READ_MEM)
{
HAL_I2C_Mem_Read(iic->handle, iic->dev_address, mem_addr, I2C_MEMADD_SIZE_8BIT, data, size, 100);
HAL_I2C_Mem_Read(iic->handle, iic->dev_address, mem_addr, bit_flag, data, size, 100);
}
else
{

View File

@ -101,5 +101,6 @@ void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e m
* @param data
* @param size
* @param mode : IIC_READ_MEM or IIC_WRITE_MEM
* @param mem8bit_flag 8
*/
void IICAccessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode);
void IICAccessMem(IICInstance *iic, uint16_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode, uint8_t mem8bit_flag);

View File

@ -0,0 +1,30 @@
#include "tfminiplus.h"
#include "stdlib.h"
#include "bsp_dwt.h"
static TFMiniInstance *tfmini;
TFMiniInstance *TFMiniRegister(I2C_HandleTypeDef *hi2c)
{
tfmini = (TFMiniInstance *)malloc(sizeof(TFMiniInstance));
IIC_Init_Config_s conf = {
.handle = hi2c,
.dev_address = 0x10,
.id = tfmini,
.work_mode = IIC_BLOCK_MODE,
};
tfmini->iic = IICRegister(&conf);
DWT_Delay(0.5);
return tfmini;
}
float GetDistance(TFMiniInstance *tfmini)
{
uint8_t cmd_buf[5] = {0x5A, 0x05, 0x00, 0x06, 0x65};
IICTransmit(tfmini->iic, cmd_buf, 5, IIC_SEQ_RELEASE);
IICReceive(tfmini->iic, tfmini->buf, 11, IIC_SEQ_RELEASE);
tfmini->Mode = tfmini->buf[6];
tfmini->Dist = (uint16_t)tfmini->buf[2] + (((uint16_t)tfmini->buf[3]) << 8);
tfmini->Strength = tfmini->buf[4] | (tfmini->buf[5] << 8);
return tfmini->Dist;
}

View File

@ -0,0 +1,20 @@
#ifndef __TFMINIPLUS_H__
#define __TFMINIPLUS_H__
#include "stdint.h"
#include "bsp_iic.h"
typedef struct
{
IICInstance *iic;
uint8_t Mode; //= buf[6];
uint16_t Dist; //= buf[2] | (buf[3] << 8);
uint32_t Strength; //= buf[4] | (buf[5] << 8);
uint8_t buf[9];
} TFMiniInstance;
TFMiniInstance *TFMiniRegister(I2C_HandleTypeDef *hi2c);
float GetDistance(TFMiniInstance *tfmini);
#endif

View File

@ -29,8 +29,8 @@ static uint8_t ist8310_write_reg_data_error[IST8310_WRITE_REG_NUM][3] = {
*/
static void IST8310Decode(IICInstance *iic)
{
int16_t temp[3]; // 用于存储解码后的数据
IST8310Instance *ist= (IST8310Instance *)(iic->id); // iic的id保存了IST8310实例的指针(父指针)
int16_t temp[3]; // 用于存储解码后的数据
IST8310Instance *ist = (IST8310Instance *)(iic->id); // iic的id保存了IST8310实例的指针(父指针)
memcpy(temp, ist->iic_buffer, 6 * sizeof(uint8_t)); // 不要强制转换,直接cpy
for (uint8_t i = 0; i < 3; i++)
@ -48,7 +48,7 @@ static void IST8310StartTransfer(GPIOInstance *gpio)
// 先获取IST8310实例的指针(通过gpio实例的父指针id)
IST8310Instance *ist_for_transfer = (IST8310Instance *)gpio->id;
// 中断说明ist已经准备好读取数据寄存器;6个字节,读取后会进入IST8310Decode函数
IICAccessMem(ist_for_transfer->iic, IST8310_DATA_REG, ist_for_transfer->iic_buffer, 6, IIC_READ_MEM);
IICAccessMem(ist_for_transfer->iic, IST8310_DATA_REG, ist_for_transfer->iic_buffer, 6, IIC_READ_MEM, 1);
// 传输完成后会进入IST8310Decode函数进行数据解析
IST8310Decode(ist_for_transfer->iic);
}
@ -80,15 +80,15 @@ IST8310Instance *IST8310Init(IST8310_Init_Config_s *config)
HAL_Delay(sleepTime);
// 读取IST8310的ID,如果不是0x10(whoami macro的值),则返回错误
IICAccessMem(ist->iic, IST8310_WHO_AM_I, &check_who_i_am, 1, IIC_READ_MEM);
IICAccessMem(ist->iic, IST8310_WHO_AM_I, &check_who_i_am, 1, IIC_READ_MEM, 1);
if (check_who_i_am != IST8310_WHO_AM_I_VALUE)
return NULL; // while(1)
// 进行初始化配置写入并检查是否写入成功,这里用循环把最上面初始化数组的东西都写进去
for (uint8_t i = 0; i < IST8310_WRITE_REG_NUM; i++)
{ // 写入配置,写一句就读一下看看ist8310是否仍然在线
IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &ist8310_write_reg_data_error[i][1], 1, IIC_WRITE_MEM);
IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &check_who_i_am, 1, IIC_READ_MEM); // 读回自身id
IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &ist8310_write_reg_data_error[i][1], 1, IIC_WRITE_MEM, 1);
IICAccessMem(ist->iic, ist8310_write_reg_data_error[i][0], &check_who_i_am, 1, IIC_READ_MEM, 1); // 读回自身id
if (check_who_i_am != ist8310_write_reg_data_error[i][1])
while (1)
ist8310_write_reg_data_error[i][2]; // 掉线/写入失败/未知错误,会返回对应的错误码

View File

@ -142,7 +142,7 @@ RC_ctrl_t *RemoteControlInit(UART_HandleTypeDef *rc_usart_handle)
return (RC_ctrl_t *)&rc_ctrl;
}
uint8_t RemotecontrolIsOnline()
uint8_t RemoteControlIsOnline()
{
if (rc_init_flag)
return DaemonIsOnline(rc_daemon_instance);