smbus.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * File : smbus.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-12-04 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "am_mcu_apollo.h"
  27. #include "board.h"
  28. #ifdef RT_USING_SMBUS
  29. #define SMBUS_GPIO_SDA 5
  30. #define SMBUS_GPIO_SCL 6
  31. #define mSDA_LOW() am_hal_gpio_out_bit_clear(SMBUS_GPIO_SCL) /* Clear SDA line */
  32. #define mSDA_HIGH() am_hal_gpio_out_bit_set(SMBUS_GPIO_SCL) /* Set SDA line */
  33. #define mSCL_LOW() am_hal_gpio_out_bit_clear(SMBUS_GPIO_SDA) /* Clear SCL line */
  34. #define mSCL_HIGH() am_hal_gpio_out_bit_set(SMBUS_GPIO_SDA) /* Set SCL line */
  35. #define mSDA_READ() am_hal_gpio_input_bit_read(SMBUS_GPIO_SCL) /* Read SCL line */
  36. #define mSDA_IN() am_hal_gpio_pin_config(SMBUS_GPIO_SCL, AM_HAL_GPIO_INPUT | AM_HAL_GPIO_PULL6K) /* Set SDA as Input */
  37. #define mSDA_OUT() am_hal_gpio_pin_config(SMBUS_GPIO_SCL, AM_HAL_GPIO_OUTPUT) /* Set SDA as Output */
  38. #define mSCL_OUT() am_hal_gpio_pin_config(SMBUS_GPIO_SDA, AM_HAL_GPIO_OUTPUT) /* Set SCL as Output */
  39. #define ACK 0
  40. #define NACK 1
  41. /* SCL keep time */
  42. static void keep_delay(void)
  43. {
  44. int i;
  45. for(i = 0; i < 30; i++)
  46. __nop();
  47. }
  48. static void few_delay(void)
  49. {
  50. __nop();
  51. __nop();
  52. }
  53. static rt_uint8_t am_smbus_send_bit(rt_uint8_t send_bit)
  54. {
  55. mSDA_OUT();
  56. few_delay();
  57. if(send_bit) /* Send a bit */
  58. mSDA_HIGH();
  59. else
  60. mSDA_LOW();
  61. mSCL_HIGH(); /* High Level of Clock Pulse */
  62. keep_delay();
  63. mSCL_LOW();
  64. keep_delay();
  65. return 0;
  66. }
  67. static rt_uint8_t am_smbus_read_bit(void)
  68. {
  69. rt_uint8_t read_bit;
  70. mSDA_IN();
  71. few_delay();
  72. mSCL_HIGH(); /* High Level of Clock Pulse */
  73. keep_delay();
  74. read_bit = mSDA_READ(); /* Read a bit, save it in Read_bit */
  75. mSCL_LOW();
  76. keep_delay();
  77. return read_bit;
  78. }
  79. static void am_smbus_start_bit(void)
  80. {
  81. mSDA_OUT();
  82. mSDA_HIGH(); /* Generate bus free time between Stop */
  83. keep_delay();
  84. mSCL_HIGH();
  85. keep_delay();
  86. mSDA_LOW(); /* Hold time after (Repeated) Start */
  87. keep_delay();
  88. mSCL_LOW();
  89. keep_delay();
  90. }
  91. static void am_smbus_stop_bit(void)
  92. {
  93. mSDA_OUT();
  94. mSDA_HIGH(); /* Generate bus free time between Stop */
  95. keep_delay();
  96. mSCL_LOW();
  97. keep_delay();
  98. mSDA_LOW(); /* Hold time after Stop */
  99. keep_delay();
  100. mSCL_HIGH(); /* For sleep mode(SCL needs to be high during Sleep.) */
  101. keep_delay();
  102. }
  103. static rt_uint8_t am_smbus_tx_byte(rt_uint8_t tx_byte)
  104. {
  105. int i;
  106. rt_uint8_t ack_bit;
  107. rt_uint8_t bit_out;
  108. for(i = 0; i < 8; i++)
  109. {
  110. if(tx_byte&0x80)
  111. bit_out = 1; /* If the current bit of Tx_buffer is 1 set bit_out */
  112. else
  113. bit_out = 0; /* else clear bit_out */
  114. am_smbus_send_bit(bit_out); /* Send the current bit on SDA */
  115. tx_byte <<= 1; /* Get next bit for checking */
  116. }
  117. ack_bit = am_smbus_read_bit(); /* Get acknowledgment bit */
  118. return ack_bit;
  119. }
  120. static rt_uint8_t am_smbus_rx_byte(rt_uint8_t ack_nack)
  121. {
  122. int i;
  123. rt_uint8_t rx_byte;
  124. for(i = 0; i < 8; i++)
  125. {
  126. if(am_smbus_read_bit()) /* Get a bit from the SDA line */
  127. {
  128. rx_byte <<= 1; /* If the bit is HIGH save 1 in RX_buffer */
  129. rx_byte |=0x01;
  130. }
  131. else
  132. {
  133. rx_byte <<= 1; /* If the bit is LOW save 0 in RX_buffer */
  134. rx_byte &=0xfe;
  135. }
  136. }
  137. am_smbus_send_bit(ack_nack); /* Sends acknowledgment bit */
  138. return rx_byte;
  139. }
  140. rt_uint8_t am_smbus_tx_then_tx(rt_uint8_t SlaveAddress, rt_uint8_t command, rt_uint8_t* pBuffer, rt_uint16_t bytesNumber)
  141. {
  142. int i;
  143. am_smbus_start_bit(); /* Start condition */
  144. if(am_smbus_tx_byte(SlaveAddress)) /* Send SlaveAddress and write */
  145. return 1;
  146. if(am_smbus_tx_byte(command)) /* Send command */
  147. return 1;
  148. for(i = 0; i < bytesNumber; i++)
  149. {
  150. am_smbus_tx_byte(pBuffer[i]); /* Write data, slave must send ACK */
  151. }
  152. am_smbus_stop_bit(); /* Stop condition */
  153. return 0;
  154. }
  155. rt_uint8_t am_smbus_tx_then_rx(rt_uint8_t SlaveAddress, rt_uint8_t command, rt_uint8_t* pBuffer, rt_uint16_t bytesNumber)
  156. {
  157. int i;
  158. am_smbus_start_bit(); /* Start condition */
  159. if(am_smbus_tx_byte(SlaveAddress)) /* Send SlaveAddress and write */
  160. return 1;
  161. if(am_smbus_tx_byte(command)) /* Send command */
  162. return 1;
  163. am_smbus_start_bit(); /* Repeated Start condition */
  164. if(am_smbus_tx_byte(SlaveAddress | 0x01)) /* Send SlaveAddress and read */
  165. return 1;
  166. for(i = 0; i < bytesNumber; i++)
  167. {
  168. pBuffer[i] = am_smbus_rx_byte(ACK); /* Read data, master must send ACK */
  169. }
  170. am_smbus_stop_bit(); /* Stop condition */
  171. return 0;
  172. }
  173. void am_smbus_scl_high(void)
  174. {
  175. mSCL_HIGH(); /* For sleep mode(SCL needs to be high during Sleep.) */
  176. keep_delay();
  177. }
  178. void am_smbus_scl_low(void)
  179. {
  180. mSCL_LOW(); /* For sleep mode(SCL needs to be high during Sleep.) */
  181. keep_delay();
  182. }
  183. int rt_hw_smbus_init(void)
  184. {
  185. mSDA_OUT();
  186. mSCL_OUT();
  187. mSDA_HIGH(); /* bus free */
  188. mSCL_HIGH();
  189. return 0;
  190. }
  191. #ifdef RT_USING_COMPONENTS_INIT
  192. INIT_BOARD_EXPORT(rt_hw_smbus_init);
  193. #endif
  194. #endif
  195. /*@}*/