lws-bb-spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SPI 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. #include <libwebsockets.h>
  25. int
  26. lws_bb_spi_init(const lws_spi_ops_t *octx)
  27. {
  28. lws_bb_spi_t *ctx = (lws_bb_spi_t *)octx;
  29. int n;
  30. for (n = 0; n < LWS_SPI_BB_MAX_CH; n++) {
  31. if (ctx->flags & (1 << n))
  32. ctx->gpio->mode(ctx->ncs[n], LWSGGPIO_FL_WRITE);
  33. if (ctx->flags & (1 << (n + 4)))
  34. ctx->gpio->mode(ctx->ncmd[n], LWSGGPIO_FL_WRITE);
  35. }
  36. ctx->gpio->mode(ctx->clk, LWSGGPIO_FL_WRITE |
  37. ((octx->bus_mode & LWSSPIMODE_CPOL) ?
  38. 0 : LWSGGPIO_FL_START_LOW));
  39. ctx->gpio->mode(ctx->mosi, LWSGGPIO_FL_WRITE | LWSGGPIO_FL_START_LOW);
  40. ctx->gpio->mode(ctx->miso, LWSGGPIO_FL_READ | LWSGGPIO_FL_PULLUP);
  41. return 0;
  42. }
  43. /* if active, prepare DnC before this and call separately for Cmd / Data */
  44. static void
  45. lws_bb_spi_write(lws_bb_spi_t *ctx, const uint8_t *buf, size_t len)
  46. {
  47. uint8_t u, inv = !!(ctx->bb_ops.bus_mode & LWSSPIMODE_CPOL);
  48. while (len--) {
  49. int n;
  50. u = *buf++;
  51. for (n = 0; n < 4; n++) {
  52. ctx->gpio->set(ctx->clk, inv);
  53. ctx->gpio->set(ctx->mosi, !!(u & 0x80));
  54. ctx->gpio->set(ctx->clk, !inv);
  55. ctx->gpio->set(ctx->clk, inv);
  56. ctx->gpio->set(ctx->mosi, !!(u & 0x40));
  57. ctx->gpio->set(ctx->clk, !inv);
  58. u <<= 2;
  59. }
  60. }
  61. ctx->gpio->set(ctx->clk, 0 ^ inv);
  62. }
  63. static void
  64. lws_bb_spi_read(lws_bb_spi_t *ctx, uint8_t *buf, size_t len)
  65. {
  66. uint8_t u, inv = !!(ctx->bb_ops.bus_mode & LWSSPIMODE_CPOL);
  67. while (len--) {
  68. int n;
  69. for (n = 0; n < 8; n++) {
  70. ctx->gpio->set(ctx->clk, inv);
  71. u = (u << 1) | !!ctx->gpio->read(ctx->miso);
  72. ctx->gpio->set(ctx->mosi, !!(u & 0x80));
  73. ctx->gpio->set(ctx->clk, !inv);
  74. }
  75. *buf++ = u;
  76. }
  77. ctx->gpio->set(ctx->clk, 0 ^ inv);
  78. }
  79. int
  80. lws_bb_spi_queue(const lws_spi_ops_t *octx, const lws_spi_desc_t *desc)
  81. {
  82. lws_bb_spi_t *ctx = (lws_bb_spi_t *)octx;
  83. const uint8_t *src = desc->src;
  84. /* clock to idle */
  85. ctx->gpio->set(ctx->clk, 0 ^ !!(octx->bus_mode & LWSSPIMODE_CPOL));
  86. /* enable nCS */
  87. ctx->gpio->set(ctx->ncs[desc->channel], 0);
  88. if (desc->count_cmd) {
  89. ctx->gpio->set(ctx->ncmd[desc->channel], 0);
  90. lws_bb_spi_write(ctx, src, desc->count_cmd);
  91. ctx->gpio->set(ctx->ncmd[desc->channel], 1);
  92. src += desc->count_cmd;
  93. }
  94. if (desc->count_write)
  95. lws_bb_spi_write(ctx, desc->data, desc->count_write);
  96. if (desc->count_read)
  97. lws_bb_spi_read(ctx, desc->dest, desc->count_read);
  98. /* disable nCS */
  99. ctx->gpio->set(ctx->ncs[desc->channel], 1);
  100. /* clock to idle */
  101. ctx->gpio->set(ctx->clk, 0 ^ !!(octx->bus_mode & LWSSPIMODE_CPOL));
  102. return 0;
  103. }