i2c_bitbang.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /****************************************************************************
  2. * drivers/i2c/i2c_bitbang.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <debug.h>
  27. #include <nuttx/spinlock.h>
  28. #include <nuttx/semaphore.h>
  29. #include <nuttx/kmalloc.h>
  30. #include <nuttx/i2c/i2c_master.h>
  31. #include <nuttx/i2c/i2c_bitbang.h>
  32. /****************************************************************************
  33. * Pre-processor Definitions
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Private Types
  37. ****************************************************************************/
  38. struct i2c_bitbang_dev_s
  39. {
  40. struct i2c_master_s i2c;
  41. struct i2c_bitbang_lower_dev_s *lower;
  42. #ifndef CONFIG_I2C_BITBANG_NO_DELAY
  43. int32_t delay;
  44. #endif
  45. };
  46. /****************************************************************************
  47. * Private Function Prototypes
  48. ****************************************************************************/
  49. static int i2c_bitbang_transfer(FAR struct i2c_master_s *dev,
  50. FAR struct i2c_msg_s *msgs, int count);
  51. static int i2c_bitbang_set_scl(FAR struct i2c_bitbang_dev_s *dev,
  52. bool high, bool nodelay);
  53. static void i2c_bitbang_set_sda(FAR struct i2c_bitbang_dev_s *dev,
  54. bool high);
  55. static int i2c_bitbang_wait_ack(FAR struct i2c_bitbang_dev_s *dev);
  56. static void i2c_bitbang_send(FAR struct i2c_bitbang_dev_s *dev,
  57. uint8_t data);
  58. /****************************************************************************
  59. * Private Data
  60. ****************************************************************************/
  61. static const struct i2c_ops_s g_i2c_ops =
  62. {
  63. .transfer = i2c_bitbang_transfer
  64. };
  65. /****************************************************************************
  66. * Inline Functions
  67. ****************************************************************************/
  68. inline static bool i2c_bitbang_get_sda(FAR struct i2c_bitbang_dev_s *dev)
  69. {
  70. return dev->lower->ops->get_sda(dev->lower);
  71. }
  72. #ifdef CONFIG_I2C_BITBANG_CLOCK_STRETCHING
  73. inline static bool i2c_bitbang_get_scl(FAR struct i2c_bitbang_dev_s *dev)
  74. {
  75. return dev->lower->ops->get_scl(dev->lower);
  76. }
  77. #endif
  78. /****************************************************************************
  79. * Private Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: i2c_bitbang_transfer
  83. ****************************************************************************/
  84. static int i2c_bitbang_transfer(FAR struct i2c_master_s *dev,
  85. FAR struct i2c_msg_s *msgs, int count)
  86. {
  87. FAR struct i2c_bitbang_dev_s *priv = (FAR struct i2c_bitbang_dev_s *)dev;
  88. int ret = OK;
  89. int i;
  90. irqstate_t flags;
  91. /* Lock to enforce timings */
  92. flags = spin_lock_irqsave(NULL);
  93. for (i = 0; i < count; i++)
  94. {
  95. uint8_t addr;
  96. FAR struct i2c_msg_s *msg = &msgs[i];
  97. #ifndef CONFIG_I2C_BITBANG_NO_DELAY
  98. /* Compute delay from frequency */
  99. priv->delay = (USEC_PER_SEC / (2 * msg->frequency)) -
  100. CONFIG_I2C_BITBANG_GPIO_OVERHEAD;
  101. if (priv->delay < 0)
  102. {
  103. priv->delay = 0;
  104. }
  105. #endif
  106. /* If this is the start of transfer or we're changing sending direction
  107. * from last transfer, send START
  108. */
  109. if (i == 0 ||
  110. (msgs[i - 1].flags & I2C_M_READ) != (msgs[i].flags & I2C_M_READ))
  111. {
  112. /* Send start bit */
  113. i2c_bitbang_set_scl(priv, true, false);
  114. i2c_bitbang_set_sda(priv, false);
  115. i2c_bitbang_set_scl(priv, false, false);
  116. /* Send the address */
  117. addr = (msg->flags & I2C_M_READ ? I2C_READADDR8(msg->addr) :
  118. I2C_WRITEADDR8(msg->addr));
  119. i2c_bitbang_send(priv, addr);
  120. /* Wait for ACK */
  121. ret = i2c_bitbang_wait_ack(priv);
  122. if (ret < 0)
  123. {
  124. goto out;
  125. }
  126. }
  127. i2c_bitbang_set_scl(priv, false, false);
  128. if (msg->flags & I2C_M_READ)
  129. {
  130. int j;
  131. int k;
  132. for (j = 0; j < msg->length; j++)
  133. {
  134. uint8_t data = 0;
  135. i2c_bitbang_set_sda(priv, true);
  136. msg->buffer[j] = 0;
  137. for (k = 0; k < 8; k++)
  138. {
  139. i2c_bitbang_set_scl(priv, true, false);
  140. data |= (i2c_bitbang_get_sda(priv) & 1) << (7 - k);
  141. i2c_bitbang_set_scl(priv, false, false);
  142. }
  143. msg->buffer[j] = data;
  144. if (j < msg->length - 1)
  145. {
  146. /* Send ACK */
  147. i2c_bitbang_set_sda(priv, false);
  148. i2c_bitbang_set_scl(priv, true, false);
  149. i2c_bitbang_set_scl(priv, false, false);
  150. }
  151. else
  152. {
  153. /* On the last byte send NAK */
  154. i2c_bitbang_set_sda(priv, true);
  155. i2c_bitbang_set_scl(priv, true, false);
  156. i2c_bitbang_set_scl(priv, false, false);
  157. }
  158. }
  159. }
  160. else
  161. {
  162. int j;
  163. for (j = 0; j < msg->length; j++)
  164. {
  165. /* Send the data */
  166. i2c_bitbang_send(priv, msg->buffer[j]);
  167. ret = i2c_bitbang_wait_ack(priv);
  168. if (ret < 0)
  169. {
  170. goto out;
  171. }
  172. i2c_bitbang_set_scl(priv, false, false);
  173. }
  174. }
  175. if (!(msg->flags & I2C_M_NOSTOP))
  176. {
  177. /* Send stop */
  178. i2c_bitbang_set_sda(priv, false);
  179. i2c_bitbang_set_scl(priv, true, true);
  180. i2c_bitbang_set_sda(priv, true);
  181. }
  182. }
  183. out:
  184. /* Ensure lines are released */
  185. i2c_bitbang_set_scl(priv, true, false);
  186. i2c_bitbang_set_sda(priv, true);
  187. spin_unlock_irqrestore(NULL, flags);
  188. return ret;
  189. }
  190. /****************************************************************************
  191. * Name: i2c_bitbang_wait_ack
  192. ****************************************************************************/
  193. static int i2c_bitbang_wait_ack(FAR struct i2c_bitbang_dev_s *priv)
  194. {
  195. int ret = OK;
  196. int i;
  197. /* Wait for ACK */
  198. i2c_bitbang_set_sda(priv, true);
  199. i2c_bitbang_set_scl(priv, true, true);
  200. for (i = 0; i2c_bitbang_get_sda(priv) &&
  201. i < CONFIG_I2C_BITBANG_TIMEOUT; i++)
  202. {
  203. up_udelay(1);
  204. }
  205. if (i == CONFIG_I2C_BITBANG_TIMEOUT)
  206. {
  207. ret = -EIO;
  208. }
  209. else
  210. {
  211. int remaining = priv->delay - i;
  212. if (remaining > 0)
  213. {
  214. up_udelay(remaining);
  215. }
  216. }
  217. return ret;
  218. }
  219. /****************************************************************************
  220. * Name: i2c_bitbang_send
  221. ****************************************************************************/
  222. static void i2c_bitbang_send(FAR struct i2c_bitbang_dev_s *priv,
  223. uint8_t data)
  224. {
  225. uint8_t bit = 0b10000000;
  226. while (bit)
  227. {
  228. i2c_bitbang_set_sda(priv, !!(data & bit));
  229. i2c_bitbang_set_scl(priv, true, false);
  230. i2c_bitbang_set_scl(priv, false, false);
  231. bit >>= 1;
  232. }
  233. }
  234. /****************************************************************************
  235. * Name: i2c_bitbang_set_sda
  236. ****************************************************************************/
  237. static void i2c_bitbang_set_sda(FAR struct i2c_bitbang_dev_s *dev, bool high)
  238. {
  239. dev->lower->ops->set_sda(dev->lower, high);
  240. }
  241. /****************************************************************************
  242. * Name: i2c_bitbang_set_scl
  243. ****************************************************************************/
  244. static int i2c_bitbang_set_scl(FAR struct i2c_bitbang_dev_s *dev, bool high,
  245. bool nodelay)
  246. {
  247. dev->lower->ops->set_scl(dev->lower, high);
  248. #ifndef CONFIG_I2C_BITBANG_NO_DELAY
  249. if (!nodelay && dev->delay)
  250. {
  251. up_udelay(dev->delay);
  252. }
  253. #endif
  254. #ifdef CONFIG_I2C_BITBANG_CLOCK_STRETCHING
  255. /* Allow for clock stretching */
  256. if (high)
  257. {
  258. int i;
  259. for (i = 0; !i2c_bitbang_get_scl(dev) &&
  260. i < CONFIG_I2C_BITBANG_TIMEOUT; i++)
  261. {
  262. up_udelay(1);
  263. if (i == CONFIG_I2C_BITBANG_TIMEOUT)
  264. {
  265. return -ETIMEDOUT;
  266. }
  267. }
  268. }
  269. #endif
  270. return OK;
  271. }
  272. /****************************************************************************
  273. * Public Functions
  274. ****************************************************************************/
  275. /****************************************************************************
  276. * Name: i2c_bitbang_initialize
  277. *
  278. * Description:
  279. * Initialize a bitbang I2C device instance
  280. *
  281. * Input Parameters:
  282. * lower - Lower half of driver
  283. *
  284. * Returned Value:
  285. * Pointer to a the I2C instance
  286. *
  287. ****************************************************************************/
  288. FAR struct i2c_master_s *i2c_bitbang_initialize(
  289. FAR struct i2c_bitbang_lower_dev_s *lower)
  290. {
  291. FAR struct i2c_bitbang_dev_s *dev;
  292. DEBUGASSERT(lower && lower->ops);
  293. dev = (FAR struct i2c_bitbang_dev_s *)kmm_zalloc(sizeof(*dev));
  294. if (!dev)
  295. {
  296. return NULL;
  297. }
  298. dev->i2c.ops = &g_i2c_ops;
  299. dev->lower = lower;
  300. dev->lower->ops->initialize(dev->lower);
  301. return &dev->i2c;
  302. }