单片机通讯通用框架--I2C

1.因为芯片不同,底层的驱动就公开了,可以参考UART那个,都是根据队列方式10ms一次进行数据解析

2.任务层

/* Private define ------------------------------------------------------------*/
#define Command_I2C_gState_Get()		Driver_I2C_Get_gState()


/* Private user code ---------------------------------------------------------*/
/*********************************************************************
 * @fn      Task_I2C
 *
 * @brief   I2C任务.
 *
 * @param   none
 *
 * @return  none
 */
void Task_I2C(void)
{
	uint8_t crc_temp;
	static Queue_I2C_Para i2c_temp;
	
	if(Command_I2C_gState_Get() == STATE_READY)
	{
		if(Function_Queue_I2C_Pop_aData(&i2c_temp) == Queue_OK)
		{			
			if(i2c_temp.Cmd == I2C_Write_Cmd)
			{
				if(Command_I2C_Set_Function((void *)&i2c_temp) == Command_ERROR)
				{
					Debug("i2c write is error!");
				}
			}
			else
			{
				if(i2c_temp.Read_Flag == I2C_Read_Ready)
				{
					if(Command_I2C_Set_Function((void *)&i2c_temp) == Command_ERROR)
					{
						Debug("i2c write is error!");
					}
				}
				else
				{
                    //常用的CRC校验,网上可以找到
					crc_temp = CRC_8((uint8_t *)&i2c_temp.Buffer, (i2c_temp.Lenght - 1));
					if(crc_temp == i2c_temp.Buffer[i2c_temp.Lenght - 1])
					{
						if(Command_I2C_Set_Function((void *)&i2c_temp) == Command_ERROR)
						{
							Debug("i2c read is error!");
						}
					}
					else
					{
						Debug("i2c read crc cheak is error!");
					}
				}
			}
		}
	}
}

3.命令层

/* Private define ------------------------------------------------------------*/
#define Command_I2C_Tx(slaveAddr, regAddr, pdata, len)			Driver_I2C_Master_Tx(slaveAddr, regAddr, pdata, len, Ctrl_Enable)
#define Command_I2C_Rx(slaveAddr, regAddr, pdata, len)			Driver_I2C_Master_Rx(slaveAddr, regAddr, pdata, len, Ctrl_Enable)
#define Command_I2C_TxRx_Queue(slaveAddr, regAddr, pdata, len, cmd) \
																Driver_I2C_Master_TxRx_Queue(slaveAddr, regAddr, pdata, len, cmd)


/* Private function prototypes -----------------------------------------------*/
static uint8_t *Command_I2C_Set_PWM_Channel(void *I2C_Para);


/* I2C读写任务列表 */
static const Command_I2C_TypeDef Cmd_I2C[] = 
{
//	子命令							发送字节		接收字节
	{Command_I2C_Set_PWM_Channel,	4,			4},
};


/*********************************************************************
 * @fn      Command_I2C_Set_Function
 *
 * @brief   I2C action command function.
 *
 * @param   I2C_Para:receive frame.
 *
 * @return  Command_StatusTypeDef:state for action
 */
Command_StatusTypeDef Command_I2C_Set_Function(void *I2C_Para)
{	
	Command_I2C_DBC_Head_Frame *temp = (Command_I2C_DBC_Head_Frame *)I2C_Para;
	
	if(temp->RegAddress < I2C_Function_Max)
	{
		if(temp->Cmd == I2C_Write_Cmd)
		{
			Command_I2C_Tx(temp->SlaveAddress, temp->RegAddress, \
							(uint8_t *)Cmd_I2C[temp->RegAddress].Command_I2C((void *)I2C_Para), Cmd_I2C[temp->RegAddress].Tx_Len);
		}
		else
		{
			if(temp->ReadFlag == I2C_Read_Ready)
			{
				Command_I2C_Rx(temp->SlaveAddress, temp->RegAddress, NULL, \
									Cmd_I2C[temp->RegAddress].Rx_Len);
			}
			else
			{
				Cmd_I2C[temp->RegAddress].Command_I2C((void *)I2C_Para);
			}
		}
		return Command_OK;
	}
	return Command_ERROR;
}


/*********************************************************************
 * @fn      Command_I2C_Set_PWM_Channel
 *
 * @brief   读写PWM通道,RegAddress = 0x00.
 *
 * @param   I2C_Para:传入数据
 *
 * @return  none
 */
static uint8_t *Command_I2C_Set_PWM_Channel(void *I2C_Para)
{
	Command_I2C_DBC_Set_PWM_Channel *temp = (Command_I2C_DBC_Set_PWM_Channel *)I2C_Para;
	
	/* USER CODE BEGIN */
	static Queue_PWM_Para pwm_temp;
	
	/* 当前是写命令 */
	if(temp->bit.Head_Frame.Cmd == I2C_Write_Cmd)
	{
        //这里是写的数据
	}
	else
	{
        //这里是读完数据后的操作
	}
	
	/* USER CODE END */	
	return temp->Buf + Command_I2C_DBC_Head_Frame_Buf;
}

4.数据解析定义 

/**  
 *  @brief I2C任务列表
 */
typedef struct
{
	uint8_t *(*Command_I2C)(void *I2C_Para);			/*!< 任务子命令 */
	uint8_t Tx_Len;										/*!< 发送数据长度 */
	uint8_t Rx_Len;										/*!< 读取数据长度 */
} Command_I2C_TypeDef;


#include "stdint.h"
#include "stddef.h"



#define Command_I2C_DBC_Head_Frame_Buf		8
#define Command_I2C_DBC_CRC16				2
#define Command_I2C_DBC_Buf					10
#define Command_I2C_DBC_Max_Buf				(Command_I2C_DBC_Head_Frame_Buf + Command_I2C_DBC_Buf)

/**
 *  @brief I2C DBC head frame 
 */
typedef struct
{
	uint32_t Len;
	uint8_t SlaveAddress;
	uint8_t RegAddress;
	uint8_t Cmd;
	uint8_t ReadFlag;
} Command_I2C_DBC_Head_Frame;


/**
 *  @brief I2C DBC Set_PWM_Channel 
 */
typedef union
{
	uint8_t Buf[Command_I2C_DBC_Max_Buf];
	struct
	{
		Command_I2C_DBC_Head_Frame Head_Frame;
		
		struct __Data_0
		{
			uint32_t ValvePumps;
		} Data;
		
		uint8_t Reserve[Command_I2C_DBC_Buf - sizeof(struct __Data_0)];
	} bit;
} Command_I2C_DBC_Set_PWM_Channel;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值