HardwareDriver/mpu6050/m451/hal.c

229 lines
5.6 KiB
C
Raw Normal View History

2019-04-04 16:11:03 +00:00
#include "M451Series.h"
#include <stdio.h>
#include "hal.h"
int MPUWriteReg( char RegAddr, char pucDATD_AA)
{
int i=0;
while(i<32) i++;
I2C_START(I2C0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x08)
{
printf("I2CD_STArt write fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
I2C_SET_DATA(I2C0,0xd0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0)!= 0x18)
{
printf("I2C write ADW fail\r\n");
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
//дÈë¶ÁµØÖ·
I2C_SET_DATA(I2C0,RegAddr);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x28)
{
printf("I2C write reg addr fail\r\n");
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
I2C_SET_DATA(I2C0,pucDATD_AA);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x28)
{
printf("I2C write control fail\r\n");
while (1);
}
//Í£Ö¹
I2C_Trigger(I2C0,0,1,1,0);
//printf("I2C write ok\r\n");
return 0;
}
int MPUWriteAddr()
{
if (I2C_GET_STATUS(I2C0) != 0x08)
{
printf("I2CD_STArt write add fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
//½øÈë¶Áд¿ØÖƲÙ×÷
I2C_SET_DATA(I2C0,0xd0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0)!= 0x18)
{
printf("I2C write ADW fail\r\n");
return FALSE;
}
return 1;
}
int MPUWriteACK(char cDat)
{
if((I2C_GET_STATUS(I2C0) != 0x18)&&(I2C_GET_STATUS(I2C0) != 0x28))
{
printf("I2C MPUWriteAddrAck STATUS error \r\n");
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
//дÈë¶ÁµØÖ·
I2C_SET_DATA(I2C0,cDat);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0)!= 0x28)
{
printf("MPUWriteAddrAck fail ACK no recv\r\n");
return FALSE;
}
return 1;
}
char MPUReadReg( int unAddr/*, int unLength*/)
{
char ret;
int i=0;
while(i<32) i++;
I2C_Trigger(I2C0,0,0,1,0);
I2C_START(I2C0); //Æô¶¯
//Æô¶¯
I2C_WAIT_READY(I2C0);
if(I2C_GET_STATUS(I2C0) != 0x08)
{
printf("I2CD_STArt read reg fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
//½øÈë¶Áд¿ØÖƲÙ×÷
I2C_SET_DATA(I2C0,0xd0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x018)
{
printf("status fault shoube be 0x018 ,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
return FALSE;
}
//дÈë¶ÁµØÖ·
I2C_SET_DATA(I2C0,unAddr);
I2C_Trigger(I2C0,0,0,1,0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0)!= 0x28)
{
printf("I2C write reg addr fail\r\n");
return FALSE;
}
// ÖØÐÂÆô¶¯
I2C_Trigger(I2C0,0,0,1,0);
I2C_Trigger(I2C0,1,0,0,0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x10)
{
printf("I2C repeated D_STArt fail\r\n");
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
//½øÈë¶Á²Ù×÷
I2C_SET_DATA(I2C0,0xd0 | 1);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x40)
{
printf("I2C write control fail\r\n");
while (1);
}
//¶ÁÈ¡Êý¾Ý
I2C_Trigger(I2C0,0,0,1,0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x58)
{
printf("I2C read fail\r\n");
return FALSE;
}
ret = I2C_GET_DATA(I2C0);
I2C_Trigger(I2C0,0,1,1,0);
// I2C_WAIT_READY(I2C0);
return ret;
}
int MPUReadBuf( int unAddr, char *pucDATD_AA, int unLength)
{
char ret;
int i=0;
while(i<32) i++;
I2C_Trigger(I2C0,0,0,1,0);
I2C_START(I2C0);
I2C_WAIT_READY(I2C0);
if(I2C_GET_STATUS(I2C0) != 0x08)
{
printf("I2CD_STArt fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
I2C_SET_DATA(I2C0,0xd0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x18)
{
printf("I2CD_STArt fail,I2D_STATUS %02X\r\n",I2C_GET_STATUS(I2C0));
return FALSE;
}
I2C_SET_DATA(I2C0,unAddr);
I2C_Trigger(I2C0,0,0,1,0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0)!= 0x28)
{
printf("I2C write reg addr fail\r\n");
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
I2C_Trigger(I2C0,1,0,0,0);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x10)
{
printf("I2C repeated D_STArt fail\r\n");
return FALSE;
}
I2C_Trigger(I2C0,0,0,1,0);
I2C_SET_DATA(I2C0,0xd0 | 1);
I2C_WAIT_READY(I2C0);
if (I2C_GET_STATUS(I2C0) != 0x40)
{
printf("I2C write control fail\r\n");
while (1);
}
for(i=0;i<unLength;i++)
{
if(i==unLength-1)
I2C_Trigger(I2C0,0,0,1,0);
else
I2C_Trigger(I2C0,0,0,1,1);
I2C_WAIT_READY(I2C0);
if ((I2C_GET_STATUS(I2C0) != 0x58)&&(I2C_GET_STATUS(I2C0) != 0x50))
{
printf("I2C read fail\r\n");
return FALSE;
}
pucDATD_AA[i] = I2C_GET_DATA(I2C0);
}
I2C_Trigger(I2C0,0,1,1,0);
// I2C_WAIT_READY(I2C0);
return ret;
}