furi_hal_spi.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <furi_hal_spi.h>
  2. #include <furi_hal_resources.h>
  3. #include <furi_hal_power.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <stm32wbxx_ll_spi.h>
  7. #include <stm32wbxx_ll_utils.h>
  8. #include <stm32wbxx_ll_cortex.h>
  9. void furi_hal_spi_bus_init(FuriHalSpiBus* bus) {
  10. furi_assert(bus);
  11. bus->callback(bus, FuriHalSpiBusEventInit);
  12. }
  13. void furi_hal_spi_bus_deinit(FuriHalSpiBus* bus) {
  14. furi_assert(bus);
  15. bus->callback(bus, FuriHalSpiBusEventDeinit);
  16. }
  17. void furi_hal_spi_bus_handle_init(FuriHalSpiBusHandle* handle) {
  18. furi_assert(handle);
  19. handle->callback(handle, FuriHalSpiBusHandleEventInit);
  20. }
  21. void furi_hal_spi_bus_handle_deinit(FuriHalSpiBusHandle* handle) {
  22. furi_assert(handle);
  23. handle->callback(handle, FuriHalSpiBusHandleEventDeinit);
  24. }
  25. void furi_hal_spi_acquire(FuriHalSpiBusHandle* handle) {
  26. furi_assert(handle);
  27. furi_hal_power_insomnia_enter();
  28. handle->bus->callback(handle->bus, FuriHalSpiBusEventLock);
  29. handle->bus->callback(handle->bus, FuriHalSpiBusEventActivate);
  30. furi_assert(handle->bus->current_handle == NULL);
  31. handle->bus->current_handle = handle;
  32. handle->callback(handle, FuriHalSpiBusHandleEventActivate);
  33. }
  34. void furi_hal_spi_release(FuriHalSpiBusHandle* handle) {
  35. furi_assert(handle);
  36. furi_assert(handle->bus->current_handle == handle);
  37. // Handle event and unset handle
  38. handle->callback(handle, FuriHalSpiBusHandleEventDeactivate);
  39. handle->bus->current_handle = NULL;
  40. // Bus events
  41. handle->bus->callback(handle->bus, FuriHalSpiBusEventDeactivate);
  42. handle->bus->callback(handle->bus, FuriHalSpiBusEventUnlock);
  43. furi_hal_power_insomnia_exit();
  44. }
  45. static void furi_hal_spi_bus_end_txrx(FuriHalSpiBusHandle* handle, uint32_t timeout) {
  46. UNUSED(timeout); // FIXME
  47. while(LL_SPI_GetTxFIFOLevel(handle->bus->spi) != LL_SPI_TX_FIFO_EMPTY)
  48. ;
  49. while(LL_SPI_IsActiveFlag_BSY(handle->bus->spi))
  50. ;
  51. while(LL_SPI_GetRxFIFOLevel(handle->bus->spi) != LL_SPI_RX_FIFO_EMPTY) {
  52. LL_SPI_ReceiveData8(handle->bus->spi);
  53. }
  54. }
  55. bool furi_hal_spi_bus_rx(
  56. FuriHalSpiBusHandle* handle,
  57. uint8_t* buffer,
  58. size_t size,
  59. uint32_t timeout) {
  60. furi_assert(handle);
  61. furi_assert(handle->bus->current_handle == handle);
  62. furi_assert(buffer);
  63. furi_assert(size > 0);
  64. return furi_hal_spi_bus_trx(handle, buffer, buffer, size, timeout);
  65. }
  66. bool furi_hal_spi_bus_tx(
  67. FuriHalSpiBusHandle* handle,
  68. uint8_t* buffer,
  69. size_t size,
  70. uint32_t timeout) {
  71. furi_assert(handle);
  72. furi_assert(handle->bus->current_handle == handle);
  73. furi_assert(buffer);
  74. furi_assert(size > 0);
  75. bool ret = true;
  76. while(size > 0) {
  77. if(LL_SPI_IsActiveFlag_TXE(handle->bus->spi)) {
  78. LL_SPI_TransmitData8(handle->bus->spi, *buffer);
  79. buffer++;
  80. size--;
  81. }
  82. }
  83. furi_hal_spi_bus_end_txrx(handle, timeout);
  84. LL_SPI_ClearFlag_OVR(handle->bus->spi);
  85. return ret;
  86. }
  87. bool furi_hal_spi_bus_trx(
  88. FuriHalSpiBusHandle* handle,
  89. uint8_t* tx_buffer,
  90. uint8_t* rx_buffer,
  91. size_t size,
  92. uint32_t timeout) {
  93. furi_assert(handle);
  94. furi_assert(handle->bus->current_handle == handle);
  95. furi_assert(size > 0);
  96. bool ret = true;
  97. size_t tx_size = size;
  98. bool tx_allowed = true;
  99. while(size > 0) {
  100. if(tx_size > 0 && LL_SPI_IsActiveFlag_TXE(handle->bus->spi) && tx_allowed) {
  101. if(tx_buffer) {
  102. LL_SPI_TransmitData8(handle->bus->spi, *tx_buffer);
  103. tx_buffer++;
  104. } else {
  105. LL_SPI_TransmitData8(handle->bus->spi, 0xFF);
  106. }
  107. tx_size--;
  108. tx_allowed = false;
  109. }
  110. if(LL_SPI_IsActiveFlag_RXNE(handle->bus->spi)) {
  111. if(rx_buffer) {
  112. *rx_buffer = LL_SPI_ReceiveData8(handle->bus->spi);
  113. rx_buffer++;
  114. } else {
  115. LL_SPI_ReceiveData8(handle->bus->spi);
  116. }
  117. size--;
  118. tx_allowed = true;
  119. }
  120. }
  121. furi_hal_spi_bus_end_txrx(handle, timeout);
  122. return ret;
  123. }