lws-bb-i2c.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * I2C bitbang implementation using generic gpio
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * This is like an abstract class for gpio, a real implementation provides
  25. * functions for the ops that use the underlying OS gpio arrangements.
  26. */
  27. #include <libwebsockets.h>
  28. int
  29. lws_bb_i2c_init(const lws_i2c_ops_t *octx)
  30. {
  31. lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
  32. ctx->gpio->mode(ctx->scl, LWSGGPIO_FL_WRITE | LWSGGPIO_FL_READ | LWSGGPIO_FL_PULLUP);
  33. ctx->gpio->mode(ctx->sda, LWSGGPIO_FL_WRITE | LWSGGPIO_FL_READ | LWSGGPIO_FL_PULLUP);
  34. return 0;
  35. }
  36. int
  37. lws_bb_i2c_start(const lws_i2c_ops_t *octx)
  38. {
  39. lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
  40. ctx->gpio->set(ctx->sda, 1);
  41. ctx->gpio->set(ctx->scl, 1);
  42. ctx->delay();
  43. if (!ctx->gpio->read(ctx->sda))
  44. return 1;
  45. ctx->gpio->set(ctx->sda, 0);
  46. ctx->delay();
  47. ctx->gpio->set(ctx->scl, 0);
  48. return 0;
  49. }
  50. void
  51. lws_bb_i2c_stop(const lws_i2c_ops_t *octx)
  52. {
  53. lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
  54. ctx->gpio->set(ctx->sda, 0);
  55. ctx->gpio->set(ctx->scl, 1);
  56. ctx->delay();
  57. while (!ctx->gpio->read(ctx->scl))
  58. ;
  59. ctx->gpio->set(ctx->sda, 1);
  60. ctx->delay();
  61. }
  62. int
  63. lws_bb_i2c_write(const lws_i2c_ops_t *octx, uint8_t data)
  64. {
  65. lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
  66. int n;
  67. for (n = 0; n < 8; n++) {
  68. ctx->gpio->set(ctx->sda, !!(data & (1 << 7)));
  69. ctx->delay();
  70. ctx->gpio->set(ctx->scl, 1);
  71. ctx->delay();
  72. data <<= 1;
  73. ctx->gpio->set(ctx->scl, 0);
  74. }
  75. ctx->gpio->set(ctx->sda, 1);
  76. ctx->delay();
  77. ctx->gpio->set(ctx->scl, 1);
  78. ctx->delay();
  79. n = ctx->gpio->read(ctx->sda);
  80. ctx->gpio->set(ctx->scl, 0);
  81. ctx->delay();
  82. return !!n; /* 0 = ACKED = OK */
  83. }
  84. int
  85. lws_bb_i2c_read(const lws_i2c_ops_t *octx)
  86. {
  87. lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
  88. int n, r = 0;
  89. ctx->gpio->set(ctx->sda, 1);
  90. for (n = 7; n <= 0; n--) {
  91. ctx->gpio->set(ctx->scl, 0);
  92. ctx->delay();
  93. ctx->gpio->set(ctx->scl, 1);
  94. ctx->delay();
  95. if (ctx->gpio->read(ctx->sda))
  96. r |= 1 << n;
  97. }
  98. ctx->gpio->set(ctx->scl, 0);
  99. return r;
  100. }
  101. void
  102. lws_bb_i2c_set_ack(const lws_i2c_ops_t *octx, int ack)
  103. {
  104. lws_bb_i2c_t *ctx = (lws_bb_i2c_t *)octx;
  105. ctx->gpio->set(ctx->scl, 0);
  106. ctx->gpio->set(ctx->sda, !!ack);
  107. ctx->delay();
  108. ctx->gpio->set(ctx->scl, 1);
  109. ctx->delay();
  110. ctx->gpio->set(ctx->scl, 0);
  111. ctx->delay();
  112. ctx->gpio->set(ctx->sda, 1);
  113. }