RobotHardware-UESTC/Hardware/Firmware/GeekRobotBig_Firmware v1.0/DRV/bsp_iic.c

249 lines
6.0 KiB
C
Raw Normal View History

2024-01-18 02:06:22 +08:00
#include "bsp_iic.h"
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_Delay
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ӳ٣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>400KHz
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void i2c_Delay(void)
{
uint8_t i;
/*<2A><>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AX-Pro<EFBFBD>߼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD>Եõ<EFBFBD><EFBFBD>ġ<EFBFBD>
CPU<EFBFBD><EFBFBD>Ƶ72MHzʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>Flash<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, MDK<EFBFBD><EFBFBD><EFBFBD>̲<EFBFBD><EFBFBD>Ż<EFBFBD>
ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ10ʱ<EFBFBD><EFBFBD>SCLƵ<EFBFBD><EFBFBD> = 205KHz
ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ7ʱ<EFBFBD><EFBFBD>SCLƵ<EFBFBD><EFBFBD> = 347KHz<EFBFBD><EFBFBD> SCL<EFBFBD>ߵ<EFBFBD>ƽʱ<EFBFBD><EFBFBD>1.5us<EFBFBD><EFBFBD>SCL<EFBFBD>͵<EFBFBD>ƽʱ<EFBFBD><EFBFBD>2.87us
ѭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ5ʱ<EFBFBD><EFBFBD>SCLƵ<EFBFBD><EFBFBD> = 421KHz<EFBFBD><EFBFBD> SCL<EFBFBD>ߵ<EFBFBD>ƽʱ<EFBFBD><EFBFBD>1.25us<EFBFBD><EFBFBD>SCL<EFBFBD>͵<EFBFBD>ƽʱ<EFBFBD><EFBFBD>2.375us
IAR<EFBFBD><EFBFBD><EFBFBD>̱<EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ʸߣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ7
*/
for (i = 0; i < 10; i++);
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_Start
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void IIC_Start(void)
{
/* <20><>SCL<43>ߵ<EFBFBD>ƽʱ<C6BD><CAB1>SDA<44><41><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ʾI2C<32><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD> */
I2C_SDA_1();
I2C_SCL_1();
i2c_Delay();
I2C_SDA_0();
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_Start
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<EFBFBD>ź<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void IIC_Stop(void)
{
/* <20><>SCL<43>ߵ<EFBFBD>ƽʱ<C6BD><CAB1>SDA<44><41><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ʾI2C<32><43><EFBFBD><EFBFBD>ֹͣ<CDA3>ź<EFBFBD> */
I2C_SDA_0();
I2C_SCL_1();
i2c_Delay();
I2C_SDA_1();
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_SendByte
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8bit<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD>_ucByte <EFBFBD><EFBFBD> <EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ֽ<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void IIC_Send_Byte(uint8_t _ucByte)
{
uint8_t i;
/* <20>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>ֽڵĸ<DAB5>λbit7 */
for (i = 0; i < 8; i++)
{
if (_ucByte & 0x80)
{
I2C_SDA_1();
}
else
{
I2C_SDA_0();
}
i2c_Delay();
I2C_SCL_1();
i2c_Delay();
I2C_SCL_0();
if (i == 7)
{
I2C_SDA_1(); // <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>
}
_ucByte <<= 1; /* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>bit */
i2c_Delay();
}
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_ReadByte
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ8bit<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*********************************************************************************************************
*/
uint8_t IIC_Read_Byte(u8 ack)
{
uint8_t i;
uint8_t value;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>bitΪ<74><CEAA><EFBFBD>ݵ<EFBFBD>bit7 */
value = 0;
for (i = 0; i < 8; i++)
{
value <<= 1;
I2C_SCL_1();
i2c_Delay();
if (I2C_SDA_READ())
{
value++;
}
I2C_SCL_0();
i2c_Delay();
}
if (!ack)
IIC_NAck();//??nACK
else
IIC_Ack(); //??ACK
return value;
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_WaitAck
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ʱ<EFBFBD>ӣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ȷӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
*********************************************************************************************************
*/
uint8_t IIC_Wait_Ack(void)
{
uint8_t re;
I2C_SDA_1(); /* CPU<50>ͷ<EFBFBD>SDA<44><41><EFBFBD><EFBFBD> */
i2c_Delay();
I2C_SCL_1(); /* CPU<50><55><EFBFBD><EFBFBD>SCL = 1, <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B7B5>ACKӦ<4B><D3A6> */
i2c_Delay();
if (I2C_SDA_READ()) /* CPU<50><55>ȡSDA<44><41><EFBFBD><EFBFBD>״̬ */
{
re = 1;
}
else
{
re = 0;
}
I2C_SCL_0();
i2c_Delay();
return re;
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_Ack
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ACK<EFBFBD>ź<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void IIC_Ack(void)
{
I2C_SDA_0(); /* CPU<50><55><EFBFBD><EFBFBD>SDA = 0 */
i2c_Delay();
I2C_SCL_1(); /* CPU<50><55><EFBFBD><EFBFBD>1<EFBFBD><31>ʱ<EFBFBD><CAB1> */
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
I2C_SDA_1(); /* CPU<50>ͷ<EFBFBD>SDA<44><41><EFBFBD><EFBFBD> */
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_NAck
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: CPU<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>NACK<EFBFBD>ź<EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void IIC_NAck(void)
{
I2C_SDA_1(); /* CPU<50><55><EFBFBD><EFBFBD>SDA = 1 */
i2c_Delay();
I2C_SCL_1(); /* CPU<50><55><EFBFBD><EFBFBD>1<EFBFBD><31>ʱ<EFBFBD><CAB1> */
i2c_Delay();
I2C_SCL_0();
i2c_Delay();
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_CfgGpio
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>GPIO<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>IO<EFBFBD>ķ<EFBFBD>ʽʵ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD>
*********************************************************************************************************
*/
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_I2C_PORT, ENABLE); /* <20><><EFBFBD><EFBFBD>GPIOʱ<4F><CAB1> */
GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; /* <20><>©<EFBFBD><C2A9><EFBFBD><EFBFBD> */
GPIO_Init(GPIO_PORT_I2C, &GPIO_InitStructure);
/* <20><>һ<EFBFBD><D2BB>ֹͣ<CDA3>ź<EFBFBD>, <20><>λI2C<32><43><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ */
IIC_Stop();
}
/*
*********************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD>: i2c_CheckDevice
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CPU<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϸ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD> <EFBFBD>Σ<EFBFBD>_Address<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ַ
* <EFBFBD><EFBFBD> <EFBFBD><EFBFBD> ֵ: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ 0 <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʾδ̽<EFBFBD>
*********************************************************************************************************
*/
uint8_t i2c_CheckDevice(uint8_t _Address)
{
uint8_t ucAck;
IIC_Init(); /* <20><><EFBFBD><EFBFBD>GPIO */
IIC_Start(); /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD> */
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8>ַ+<2B><>д<EFBFBD><D0B4><EFBFBD><EFBFBD>bit<69><74>0 = w<><77> 1 = r) bit7 <20>ȴ<EFBFBD> */
IIC_Send_Byte(_Address | I2C_WR);
ucAck = IIC_Wait_Ack(); /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8>ACKӦ<4B><D3A6> */
IIC_Stop(); /* <20><><EFBFBD><EFBFBD>ֹͣ<CDA3>ź<EFBFBD> */
return ucAck;
}