softspi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016-2018 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "drivers/bus/spi.h"
  27. int mp_soft_spi_ioctl(void *self_in, uint32_t cmd) {
  28. mp_soft_spi_obj_t *self = (mp_soft_spi_obj_t*)self_in;
  29. switch (cmd) {
  30. case MP_SPI_IOCTL_INIT:
  31. mp_hal_pin_write(self->sck, self->polarity);
  32. mp_hal_pin_output(self->sck);
  33. mp_hal_pin_output(self->mosi);
  34. mp_hal_pin_input(self->miso);
  35. break;
  36. case MP_SPI_IOCTL_DEINIT:
  37. break;
  38. }
  39. return 0;
  40. }
  41. void mp_soft_spi_transfer(void *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
  42. mp_soft_spi_obj_t *self = (mp_soft_spi_obj_t*)self_in;
  43. uint32_t delay_half = self->delay_half;
  44. // only MSB transfer is implemented
  45. // If a port defines MICROPY_HW_SOFTSPI_MIN_DELAY, and the configured
  46. // delay_half is equal to this value, then the software SPI implementation
  47. // will run as fast as possible, limited only by CPU speed and GPIO time.
  48. #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
  49. if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
  50. for (size_t i = 0; i < len; ++i) {
  51. uint8_t data_out = src[i];
  52. uint8_t data_in = 0;
  53. for (int j = 0; j < 8; ++j, data_out <<= 1) {
  54. mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
  55. mp_hal_pin_write(self->sck, 1 - self->polarity);
  56. data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
  57. mp_hal_pin_write(self->sck, self->polarity);
  58. }
  59. if (dest != NULL) {
  60. dest[i] = data_in;
  61. }
  62. }
  63. return;
  64. }
  65. #endif
  66. for (size_t i = 0; i < len; ++i) {
  67. uint8_t data_out = src[i];
  68. uint8_t data_in = 0;
  69. for (int j = 0; j < 8; ++j, data_out <<= 1) {
  70. mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
  71. if (self->phase == 0) {
  72. mp_hal_delay_us_fast(delay_half);
  73. mp_hal_pin_write(self->sck, 1 - self->polarity);
  74. } else {
  75. mp_hal_pin_write(self->sck, 1 - self->polarity);
  76. mp_hal_delay_us_fast(delay_half);
  77. }
  78. data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
  79. if (self->phase == 0) {
  80. mp_hal_delay_us_fast(delay_half);
  81. mp_hal_pin_write(self->sck, self->polarity);
  82. } else {
  83. mp_hal_pin_write(self->sck, self->polarity);
  84. mp_hal_delay_us_fast(delay_half);
  85. }
  86. }
  87. if (dest != NULL) {
  88. dest[i] = data_in;
  89. }
  90. }
  91. }
  92. const mp_spi_proto_t mp_soft_spi_proto = {
  93. .ioctl = mp_soft_spi_ioctl,
  94. .transfer = mp_soft_spi_transfer,
  95. };