HardwareDriver/OLED/M451/OLED_TEST/hal_i2c.c

228 lines
5.7 KiB
C

#include "NUC100Series.h"
#include <stdio.h>
int OLED_WriteReg( 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 OLED_WriteAddr()
{
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 OLED_WriteACK(char cDat)
{
if((I2C_GET_STATUS(I2C0) != 0x18)&&(I2C_GET_STATUS(I2C0) != 0x28))
{
printf("I2C OLED_WriteAddrAck 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("OLED_WriteAddrAck fail ACK no recv\r\n");
return FALSE;
}
return 1;
}
char OLED_ReadReg( 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 OLED_ReadBuf( 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;
}