smbus.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "mlx90614.h"
  2. #include "smbus.h"
  3. #include "hi_time.h"
  4. /***************************************************************************
  5. * @brief SMBus start bit
  6. * @param None
  7. * @retval None
  8. ****************************************************************************/
  9. void SMBus_Start(void)
  10. {
  11. SMBUS_SDA_H();
  12. hi_udelay(5);
  13. SMBUS_SCL_H();
  14. hi_udelay(5);
  15. SMBUS_SDA_L();
  16. hi_udelay(5);
  17. SMBUS_SCL_L();
  18. hi_udelay(5);
  19. }
  20. /***************************************************************************
  21. * @brief SMBus stop bit
  22. * @param None
  23. * @retval None
  24. ****************************************************************************/
  25. void SMBus_Stop(void)
  26. {
  27. SMBUS_SCL_L();
  28. hi_udelay(5);
  29. SMBUS_SDA_L();
  30. hi_udelay(5);
  31. SMBUS_SCL_H();
  32. hi_udelay(5);
  33. SMBUS_SDA_H();
  34. }
  35. /*******************************************************************************
  36. * 函数名: SMBus_SendBit
  37. * 功能: Send a bit on SMBus 82.5kHz
  38. * Input : bit_out
  39. * Output : None
  40. * Return : None
  41. *******************************************************************************/
  42. void SMBus_SendBit(uint8_t bit_out)
  43. {
  44. if(bit_out==0)
  45. {
  46. SMBUS_SDA_L();
  47. }
  48. else
  49. {
  50. SMBUS_SDA_H();
  51. }
  52. hi_udelay(2); // Tsu:dat = 250ns minimum
  53. SMBUS_SCL_H(); // Set SCL line
  54. hi_udelay(6); // High Level of Clock Pulse
  55. SMBUS_SCL_L(); // Clear SCL line
  56. hi_udelay(3); // Low Level of Clock Pulse
  57. return;
  58. }
  59. static uint8_t SMBUS_SDA_READ(void)
  60. {
  61. IotGpioValue temp = {0};
  62. IoTGpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_12,&temp);
  63. return temp;
  64. }
  65. /*******************************************************************************
  66. * Function Name : SMBus_ReceiveBit
  67. * Description : Receive a bit on SMBus
  68. * Input : None
  69. * Output : None
  70. * Return : Ack_bit
  71. *******************************************************************************/
  72. uint8_t SMBus_ReceiveBit(void)
  73. {
  74. uint8_t Ack_bit;
  75. SMBUS_SDA_H(); //引脚靠外部电阻上拉,当作输入
  76. hi_udelay(2); // High Level of Clock Pulse
  77. SMBUS_SCL_H(); // Set SCL line
  78. hi_udelay(5); // High Level of Clock Pulse
  79. if (SMBUS_SDA_READ())
  80. {
  81. Ack_bit=1;
  82. }
  83. else
  84. {
  85. Ack_bit=0;
  86. }
  87. SMBUS_SCL_L(); // Clear SCL line
  88. hi_udelay(3); // Low Level of Clock Pulse
  89. return Ack_bit;
  90. }
  91. /*******************************************************************************
  92. * 函数名: SMBus_SendByte
  93. * 功能: Send a byte on SMBus
  94. * Input : Tx_buffer
  95. * Output : None
  96. * Return : None
  97. *******************************************************************************/
  98. uint8_t SMBus_SendByte(uint8_t Tx_buffer)
  99. {
  100. uint8_t Bit_counter;
  101. uint8_t Ack_bit;
  102. uint8_t bit_out;
  103. for(Bit_counter=8; Bit_counter; Bit_counter--)
  104. {
  105. if (Tx_buffer&0x80)
  106. {
  107. bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
  108. }
  109. else
  110. {
  111. bit_out=0; // else clear bit_out
  112. }
  113. SMBus_SendBit(bit_out); // Send the current bit on SDA
  114. Tx_buffer<<=1; // Get next bit for checking
  115. }
  116. Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
  117. return Ack_bit;
  118. }
  119. /*******************************************************************************
  120. * 函数名: SMBus_ReceiveByte
  121. * 功能: Receive a byte on SMBus
  122. * Input : ack_nack
  123. * Output : None
  124. * Return : RX_buffer
  125. *******************************************************************************/
  126. uint8_t SMBus_ReceiveByte(uint8_t ack_nack)
  127. {
  128. uint8_t RX_buffer;
  129. uint8_t Bit_Counter;
  130. for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
  131. {
  132. if(SMBus_ReceiveBit()) // Get a bit from the SDA line
  133. {
  134. RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
  135. RX_buffer |=0x01;
  136. }
  137. else
  138. {
  139. RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
  140. RX_buffer &=0xfe;
  141. }
  142. }
  143. SMBus_SendBit(ack_nack); // Sends acknowledgment bit
  144. return RX_buffer;
  145. }