bossay_release_out/app/A19_MXL90614/src/smbus.c

159 lines
4.5 KiB
C
Raw Normal View History

2025-09-24 11:16:14 +08:00
#include "mlx90614.h"
#include "smbus.h"
#include "hi_time.h"
/***************************************************************************
* @brief SMBus start bit
* @param None
* @retval None
****************************************************************************/
void SMBus_Start(void)
{
SMBUS_SDA_H();
hi_udelay(5);
SMBUS_SCL_H();
hi_udelay(5);
SMBUS_SDA_L();
hi_udelay(5);
SMBUS_SCL_L();
hi_udelay(5);
}
/***************************************************************************
* @brief SMBus stop bit
* @param None
* @retval None
****************************************************************************/
void SMBus_Stop(void)
{
SMBUS_SCL_L();
hi_udelay(5);
SMBUS_SDA_L();
hi_udelay(5);
SMBUS_SCL_H();
hi_udelay(5);
SMBUS_SDA_H();
}
/*******************************************************************************
* : SMBus_SendBit
* : Send a bit on SMBus 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(uint8_t bit_out)
{
if(bit_out==0)
{
SMBUS_SDA_L();
}
else
{
SMBUS_SDA_H();
}
hi_udelay(2); // Tsu:dat = 250ns minimum
SMBUS_SCL_H(); // Set SCL line
hi_udelay(6); // High Level of Clock Pulse
SMBUS_SCL_L(); // Clear SCL line
hi_udelay(3); // Low Level of Clock Pulse
return;
}
static uint8_t SMBUS_SDA_READ(void)
{
IotGpioValue temp = {0};
IoTGpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_12,&temp);
return temp;
}
/*******************************************************************************
* Function Name : SMBus_ReceiveBit
* Description : Receive a bit on SMBus
* Input : None
* Output : None
* Return : Ack_bit
*******************************************************************************/
uint8_t SMBus_ReceiveBit(void)
{
uint8_t Ack_bit;
SMBUS_SDA_H(); //引脚靠外部电阻上拉,当作输入
hi_udelay(2); // High Level of Clock Pulse
SMBUS_SCL_H(); // Set SCL line
hi_udelay(5); // High Level of Clock Pulse
if (SMBUS_SDA_READ())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCL_L(); // Clear SCL line
hi_udelay(3); // Low Level of Clock Pulse
return Ack_bit;
}
/*******************************************************************************
* : SMBus_SendByte
* : Send a byte on SMBus
* Input : Tx_buffer
* Output : None
* Return : None
*******************************************************************************/
uint8_t SMBus_SendByte(uint8_t Tx_buffer)
{
uint8_t Bit_counter;
uint8_t Ack_bit;
uint8_t bit_out;
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if (Tx_buffer&0x80)
{
bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
}
else
{
bit_out=0; // else clear bit_out
}
SMBus_SendBit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
}
Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
return Ack_bit;
}
/*******************************************************************************
* : SMBus_ReceiveByte
* : Receive a byte on SMBus
* Input : ack_nack
* Output : None
* Return : RX_buffer
*******************************************************************************/
uint8_t SMBus_ReceiveByte(uint8_t ack_nack)
{
uint8_t RX_buffer;
uint8_t Bit_Counter;
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(SMBus_ReceiveBit()) // Get a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack); // Sends acknowledgment bit
return RX_buffer;
}