platform.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "platform.h"
  2. #include <assert.h>
  3. #include <furi.h>
  4. #include <furi_hal_spi.h>
  5. typedef struct {
  6. FuriThread* thread;
  7. volatile PlatformIrqCallback callback;
  8. bool need_spi_lock;
  9. } RfalPlatform;
  10. static volatile RfalPlatform rfal_platform = {
  11. .thread = NULL,
  12. .callback = NULL,
  13. .need_spi_lock = true,
  14. };
  15. void nfc_isr(void* _ctx) {
  16. UNUSED(_ctx);
  17. if(rfal_platform.callback && platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  18. furi_thread_flags_set(furi_thread_get_id(rfal_platform.thread), 0x1);
  19. }
  20. }
  21. int32_t rfal_platform_irq_thread(void* context) {
  22. UNUSED(context);
  23. while(1) {
  24. uint32_t flags = furi_thread_flags_wait(0x1, osFlagsWaitAny, osWaitForever);
  25. if(flags & 0x1) {
  26. rfal_platform.callback();
  27. }
  28. }
  29. }
  30. void platformEnableIrqCallback() {
  31. furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeInterruptRise, GpioPullDown, GpioSpeedLow);
  32. furi_hal_gpio_enable_int_callback(&gpio_nfc_irq_rfid_pull);
  33. }
  34. void platformDisableIrqCallback() {
  35. furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  36. furi_hal_gpio_disable_int_callback(&gpio_nfc_irq_rfid_pull);
  37. }
  38. void platformSetIrqCallback(PlatformIrqCallback callback) {
  39. rfal_platform.callback = callback;
  40. rfal_platform.thread = furi_thread_alloc();
  41. furi_thread_set_name(rfal_platform.thread, "RfalIrqDriver");
  42. furi_thread_set_callback(rfal_platform.thread, rfal_platform_irq_thread);
  43. furi_thread_set_stack_size(rfal_platform.thread, 1024);
  44. furi_thread_set_priority(rfal_platform.thread, FuriThreadPriorityIsr);
  45. furi_thread_start(rfal_platform.thread);
  46. furi_hal_gpio_add_int_callback(&gpio_nfc_irq_rfid_pull, nfc_isr, NULL);
  47. // Disable interrupt callback as the pin is shared between 2 apps
  48. // It is enabled in rfalLowPowerModeStop()
  49. furi_hal_gpio_disable_int_callback(&gpio_nfc_irq_rfid_pull);
  50. }
  51. bool platformSpiTxRx(const uint8_t* txBuf, uint8_t* rxBuf, uint16_t len) {
  52. bool ret = false;
  53. if(txBuf && rxBuf) {
  54. ret =
  55. furi_hal_spi_bus_trx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)txBuf, rxBuf, len, 1000);
  56. } else if(txBuf) {
  57. ret = furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)txBuf, len, 1000);
  58. } else if(rxBuf) {
  59. ret = furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)rxBuf, len, 1000);
  60. }
  61. return ret;
  62. }
  63. // Until we completely remove RFAL, NFC works with SPI from rfal_platform_irq_thread and nfc_worker
  64. // threads. Some nfc features already stop using RFAL and work with SPI from nfc_worker only.
  65. // rfal_platform_spi_acquire() and rfal_platform_spi_release() functions are used to lock SPI for a
  66. // long term without locking it for each SPI transaction. This is needed for time critical communications.
  67. void rfal_platform_spi_acquire() {
  68. platformDisableIrqCallback();
  69. rfal_platform.need_spi_lock = false;
  70. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_nfc);
  71. }
  72. void rfal_platform_spi_release() {
  73. furi_hal_spi_release(&furi_hal_spi_bus_handle_nfc);
  74. rfal_platform.need_spi_lock = true;
  75. platformEnableIrqCallback();
  76. }
  77. void platformProtectST25RComm() {
  78. if(rfal_platform.need_spi_lock) {
  79. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_nfc);
  80. }
  81. }
  82. void platformUnprotectST25RComm() {
  83. if(rfal_platform.need_spi_lock) {
  84. furi_hal_spi_release(&furi_hal_spi_bus_handle_nfc);
  85. }
  86. }