You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
337 lines
7.8 KiB
337 lines
7.8 KiB
1 year ago
|
//////////////////////////////////////////////////////////////////////////
|
||
|
/// COPYRIGHT NOTICE
|
||
|
/// Copyright (c) 2018, ���ؿƼ�
|
||
|
/// All rights reserved.
|
||
|
///
|
||
|
/// @file iic_sim
|
||
|
/// @brief iic ģ��io �汾�� ��cpu�أ�Ψһ��Ҫ�ĵľ���ͷ�����еĹܽŶ���
|
||
|
/// @info ���ޱ�Ҫ������
|
||
|
///�����ļ�ʵ�ֵĹ��ܵ�������
|
||
|
///
|
||
|
/// @version 1.1 CCsens technology
|
||
|
/// @author CC
|
||
|
/// @date 20180102
|
||
|
|
||
|
//
|
||
|
//////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
#include "msp_siicx.h"
|
||
|
|
||
|
/*------------------------------------------------
|
||
|
�˿ڶ���
|
||
|
��I2C���ߴ������ݹ����У�ÿ����һ���ֽڣ�
|
||
|
��Ҫ��һ��Ӧ��״̬λ���������������ݵ�����
|
||
|
����ͨ��Ӧ��λ����֪��������Ӧ��λ��ʱ������
|
||
|
����������������Ӧ��λ������״̬����ѭ��˭
|
||
|
����˭��������ԭ�������ɽ���������Ӧ��λ��
|
||
|
����Ӧ��ʱ�������ڼ����������뽫SDA ����
|
||
|
�ͣ�ʹ��������ʱ�������ĸߵ�ƽ�ڼ䱣���ȶ��ĵ͵�ƽ����ͼ9�� ��
|
||
|
��Ȼ���뿼�ǽ����ͱ���ʱ��
|
||
|
����ϸ�����ı�6�� ���������ӻ���������ʱ��
|
||
|
Ӧ��λ�ɴӻ�����;�����Ӵӻ���������ʱ��Ӧ��λ
|
||
|
������������
|
||
|
I2C���߱��涨�� Ӧ��λΪ0��ʾ������Ӧ�� ��ACK�� ��
|
||
|
��������ΪA; Ϊ1����ʾ��Ӧ�� ��NACK�� ��
|
||
|
������ΪNA������������LSB֮����Ӧ���ͷ�SDA�ߣ�����SDA�� ��
|
||
|
�Եȴ�����������Ӧ��λ��
|
||
|
�����������ڽ���������һ���ֽڵ����ݣ�
|
||
|
���߲����ٽ��ո���������ʱ��Ӧ��������Ӧ����
|
||
|
����֪ͨ���������������������ֽ����������˷�Ӧ��״̬��
|
||
|
��Ӧ����ֹ���͡�
|
||
|
|
||
|
------------------------------------------------*/
|
||
|
///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||
|
|
||
|
void L0_iicx_INIT(TS_siic *p,unsigned char v)
|
||
|
{
|
||
|
if (v)///��������
|
||
|
{
|
||
|
p->pf_SDA_set(1);
|
||
|
p->pf_SCL_set(1);
|
||
|
|
||
|
p->pf_SDA_out(1);
|
||
|
p->pf_SCL_out(1);
|
||
|
|
||
|
|
||
|
|
||
|
}else
|
||
|
{
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
unsigned char L0_iicx_SDA_ask(TS_siic *p)
|
||
|
{
|
||
|
if(p->pf_SDA_get() != 0)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
|
||
|
|
||
|
|
||
|
|
||
|
/****************
|
||
|
|
||
|
void L0_iicx_Reset(void)
|
||
|
{
|
||
|
p->pf_SCL_set(1); p->pf_SDA_set(1);//pf_SDA_set(1);; //ȷ���ͷ�����
|
||
|
}
|
||
|
|
||
|
|
||
|
********************/
|
||
|
|
||
|
//**********************************************
|
||
|
//����ʼλ IO_SDA=1->0
|
||
|
void L0_iicx_Start(TS_siic *p)
|
||
|
{
|
||
|
p->pf_SDA_set(1);//pf_SDA_set(1);;
|
||
|
p->pf_SCL_set(1);
|
||
|
p->pf_SDA_set(0);
|
||
|
p->pf_SCL_set(0);
|
||
|
///fixme: cc
|
||
|
L0_slc_1_delay();
|
||
|
L0_slc_1_delay();
|
||
|
L0_slc_1_delay();
|
||
|
}
|
||
|
//************************************************
|
||
|
//��ֹͣλ IO_SDA = 0->1
|
||
|
void L0_iicx_Stop(TS_siic *p)
|
||
|
{
|
||
|
p->pf_SDA_set(0);
|
||
|
p->pf_SCL_set(1);
|
||
|
p->pf_SDA_set(1);
|
||
|
p->pf_close();
|
||
|
}
|
||
|
//************************************************
|
||
|
//��Ӧ��(����ack:IO_SDA = 0��no_ack:IO_SDA = 0)
|
||
|
///�������ӻ���ACK
|
||
|
void L0_iicx_Ack(TS_siic *p)
|
||
|
{
|
||
|
p->pf_SDA_out(1);//����SDA ������
|
||
|
p->pf_SCL_set(0);
|
||
|
p->pf_SDA_set(0);
|
||
|
p->pf_SCL_set(1);
|
||
|
p->pf_SCL_set(0);
|
||
|
p->pf_SDA_out(0);
|
||
|
// Lc_delay_ms(2); ///for debug wave easy to see
|
||
|
|
||
|
}
|
||
|
|
||
|
///�������ӻ���NOACK
|
||
|
void L0_iicx_NoAck(TS_siic *p)
|
||
|
{
|
||
|
p->pf_SDA_out(1);//����SDA ������
|
||
|
p->pf_SCL_set(0);
|
||
|
p->pf_SDA_set(1);
|
||
|
p->pf_SCL_set(1);
|
||
|
p->pf_SCL_set(0);
|
||
|
p->pf_SDA_out(0);
|
||
|
// Lc_delay_ms(2);
|
||
|
|
||
|
}
|
||
|
|
||
|
/// �ӻ����ص�ack
|
||
|
void L0_iicx_WaitAck(TS_siic *p)
|
||
|
{
|
||
|
p->pf_SDA_out(0);
|
||
|
p->pf_SCL_set(1);
|
||
|
////
|
||
|
p->pf_SCL_set(0);
|
||
|
p->pf_SDA_out(1);
|
||
|
/// Lc_delay_ms(2);
|
||
|
}
|
||
|
|
||
|
|
||
|
/******
|
||
|
I2C���߱��涨�� Ӧ��λΪ0��ʾ������Ӧ�� ��ACK�� �� ��������ΪA;
|
||
|
Ϊ1����ʾ��Ӧ�� ��NACK�� ��������ΪNA��
|
||
|
����������LSB֮����Ӧ���ͷ�SDA�ߣ�����SDA�� ��
|
||
|
�Եȴ�����������Ӧ��λ��
|
||
|
�����������ڽ���������һ���ֽڵ����ݣ�
|
||
|
���߲����ٽ��ո���������ʱ��Ӧ��������Ӧ����
|
||
|
����֪ͨ���������������������ֽ����������˷�Ӧ��״̬��
|
||
|
��Ӧ����ֹ���͡�
|
||
|
// ���� SDA�Ƿ���ACK
|
||
|
bit L0_iicx_Test_Ack(void)
|
||
|
{ //����SDA ������
|
||
|
bit ACK_Flag = 0;
|
||
|
p->pf_SCL_set(0);
|
||
|
p->pf_SDA_set(1);//pf_SDA_set(1);;
|
||
|
p->pf_SCL_set(1);
|
||
|
|
||
|
|
||
|
if(IO_SDA = 0)
|
||
|
ACK_Flag = 1;
|
||
|
else
|
||
|
ACK_Flag = 0;
|
||
|
p->pf_SCL_set(0);
|
||
|
return ACK_Flag;
|
||
|
// return 1;// debug
|
||
|
}
|
||
|
*******/
|
||
|
//*************************************************
|
||
|
//�ֽڷ��ͳ���
|
||
|
//����c(����������Ҳ���ǵ�ַ)�����������մ�Ӧ��
|
||
|
//�����Ǵ�Ӧ��λ
|
||
|
|
||
|
void L0_iicx_SendData(TS_siic *p,unsigned char c)
|
||
|
{
|
||
|
// unsigned char ack=8;
|
||
|
unsigned char BitCnt=8;//һ�ֽ�8λ
|
||
|
//����SDA ������
|
||
|
for(BitCnt = 0;BitCnt < 8;BitCnt ++) //Ҫ���͵����ݳ���Ϊ8λ
|
||
|
{
|
||
|
if((c << BitCnt ) & 0x80)
|
||
|
p->pf_SDA_set(1);//pf_SDA_set(1);; //�жϷ���λ
|
||
|
else
|
||
|
p->pf_SDA_set(0);
|
||
|
p->pf_SCL_set(1); //��ʱ����Ϊ�ߣ�֪ͨ��������ʼ��������λ
|
||
|
p->pf_SCL_set(0);
|
||
|
}//8clk
|
||
|
L0_iicx_WaitAck(p);
|
||
|
}
|
||
|
//**************************************************
|
||
|
//�ֽڽ��ճ���
|
||
|
//�����������������ݣ��˳���Ӧ����|��Ӧ������|i2c_ack_main(void)ʹ��
|
||
|
//return: uchar��1�ֽ�
|
||
|
|
||
|
unsigned char L0_iicx_ReceiveData(TS_siic *p)
|
||
|
{
|
||
|
unsigned char BitCnt=8,IIC_RX_Data = 0;
|
||
|
p->pf_SDA_out(0);// p->pf_SDA_set(1);//pf_SDA_set(1);; //�������� ����SDA ������
|
||
|
for(BitCnt = 0;BitCnt<8;BitCnt++)
|
||
|
{
|
||
|
L0_I2C_delay(1);
|
||
|
p->pf_SCL_set(0); //��ʱ����Ϊ�ͣ�����������λ
|
||
|
p->pf_SCL_set(1); //��ʱ����Ϊ��ʹ��������������Ч
|
||
|
L0_I2C_delay(1);
|
||
|
IIC_RX_Data = IIC_RX_Data << 1;
|
||
|
if(0 != p->pf_SDA_get())//�м� ��������������
|
||
|
IIC_RX_Data = IIC_RX_Data + 1; //������λ,���յ�����λ����retc��
|
||
|
L0_I2C_delay(1);
|
||
|
}// 8clk up
|
||
|
p->pf_SCL_set(0);
|
||
|
return IIC_RX_Data;
|
||
|
}
|
||
|
/// ��ȡ n������ ����p��
|
||
|
|
||
|
void L1_iicx_ReadNByte(TS_siic *p, unsigned char *b,unsigned char n)
|
||
|
{
|
||
|
unsigned char i;
|
||
|
L0_iicx_Start(p); // ����I2C
|
||
|
L0_iicx_SendData(p,(p->sal)| 0x01); //����������ַ
|
||
|
//p->pf_SDA_out(0);
|
||
|
for(i = 0;i<n-1;i++) //��ȡ�ֽ�����
|
||
|
{
|
||
|
*(b + i) = L0_iicx_ReceiveData(p); //��ȡ����
|
||
|
L0_iicx_Ack(p); //__/```\__
|
||
|
}
|
||
|
*(b + n - 1) = L0_iicx_ReceiveData(p);
|
||
|
L0_iicx_NoAck(p);
|
||
|
p->pf_SDA_out(1);
|
||
|
L0_iicx_Stop(p);
|
||
|
}
|
||
|
/// I2C�豸��ַΪ8bit,��������λΪ��дλ,0Ϊд,1Ϊ��
|
||
|
////��ȡ�����ֽڵģ��ȷ��������ֽ� Ȼ���ٶ�ȡ
|
||
|
void L1_iicx_Read2cmd(TS_siic *p, unsigned char *c,unsigned char *b,unsigned char n)
|
||
|
{
|
||
|
unsigned char i;
|
||
|
L0_iicx_Start(p); // ����I2C
|
||
|
L0_iicx_SendData(p,(p->sal)| 0x01); //����������ַ
|
||
|
L0_iicx_SendData(p,(*c)); //����������ַ
|
||
|
L0_iicx_SendData(p,*(c+1));
|
||
|
|
||
|
|
||
|
//p->pf_SDA_out(0);
|
||
|
for(i = 0;i<n-1;i++) //��ȡ�ֽ�����
|
||
|
{
|
||
|
*(b + i) = L0_iicx_ReceiveData(p); //��ȡ����
|
||
|
L0_iicx_Ack(p); //__/```\__
|
||
|
}
|
||
|
*(b + n - 1) = L0_iicx_ReceiveData(p);
|
||
|
L0_iicx_NoAck(p);
|
||
|
p->pf_SDA_out(1);
|
||
|
L0_iicx_Stop(p);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
///////////////////////////////
|
||
|
///д��һ��reg Ϊ������д�������߶�ȡ����
|
||
|
void L1_iicx_Write1Reg(TS_siic *p,unsigned char reg)
|
||
|
{
|
||
|
L0_iicx_Start(p); //����I2C
|
||
|
L0_iicx_SendData(p,p->sal);//����������ַ
|
||
|
L0_iicx_SendData(p,reg);
|
||
|
}
|
||
|
|
||
|
void L1_iicx_Write2Reg(TS_siic *p,unsigned char reg,unsigned char reg2)
|
||
|
{
|
||
|
L0_iicx_Start(p); //����I2C
|
||
|
L0_iicx_SendData(p,p->sal);//����������ַ
|
||
|
L0_iicx_SendData(p,reg);
|
||
|
L0_iicx_SendData(p,reg2);
|
||
|
}
|
||
|
|
||
|
|
||
|
// sla.(reg)=cmd
|
||
|
void L2_iicx_Write1Reg1Cmd(TS_siic *p, unsigned char reg,unsigned char cmd)
|
||
|
{
|
||
|
L1_iicx_Write1Reg(p,reg);
|
||
|
L0_iicx_SendData(p,cmd);
|
||
|
L0_iicx_Stop(p);
|
||
|
/*****************
|
||
|
if(gRccUs05 >0)///����iic��Ч�� ����û�н���
|
||
|
{
|
||
|
L0_uart0_uc(gRccUs05);gRccUs05 = 0;
|
||
|
}
|
||
|
***************/
|
||
|
}
|
||
|
|
||
|
//��ȡreg��ַ(1Byte)��n������ sla.(reg) sla.(reg+1)................ sla.(reg+n)
|
||
|
void L2_iicx_Read1Reg(TS_siic *p,unsigned char reg,
|
||
|
unsigned char *r,unsigned char n)
|
||
|
{
|
||
|
// U8 d[1];
|
||
|
//S ADD W A REG A
|
||
|
L1_iicx_Write1Reg(p,reg);
|
||
|
//S ADD R A D1 A D2 A.....DX N P
|
||
|
L1_iicx_ReadNByte(p,r,n);
|
||
|
|
||
|
}
|
||
|
|
||
|
void L2_iicx_Read2Reg(TS_siic *p,unsigned char H,unsigned char L,
|
||
|
unsigned char *r,unsigned char n)
|
||
|
{
|
||
|
// U8 d[1];
|
||
|
//S ADD W A REG A
|
||
|
L1_iicx_Write1Reg(p,H);
|
||
|
L0_iicx_SendData(p,L);
|
||
|
//S ADD R A D1 A D2 A.....DX N P
|
||
|
L1_iicx_ReadNByte(p,r,n);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
////// S slaW cmd cmd2 S slaR A A NACK P
|
||
|
////// A A A A MSB LSB CRC
|
||
|
void L2_iicx_Read2Cmd(TS_siic *p, unsigned char cmd,unsigned char cmd2,
|
||
|
unsigned char *buf,unsigned char n)
|
||
|
{
|
||
|
L1_iicx_Write1Reg(p,cmd);
|
||
|
L0_iicx_SendData(p,cmd2);
|
||
|
|
||
|
L1_iicx_ReadNByte(p,buf,n);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|