platform.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. } RfalPlatform;
  9. static volatile RfalPlatform rfal_platform = {
  10. .thread = NULL,
  11. .callback = NULL,
  12. };
  13. void nfc_isr(void* _ctx) {
  14. UNUSED(_ctx);
  15. if(rfal_platform.callback && platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  16. furi_thread_flags_set(furi_thread_get_id(rfal_platform.thread), 0x1);
  17. }
  18. }
  19. int32_t rfal_platform_irq_thread(void* context) {
  20. UNUSED(context);
  21. while(1) {
  22. uint32_t flags = furi_thread_flags_wait(0x1, osFlagsWaitAny, osWaitForever);
  23. if(flags & 0x1) {
  24. rfal_platform.callback();
  25. }
  26. }
  27. }
  28. void platformEnableIrqCallback() {
  29. furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeInterruptRise, GpioPullDown, GpioSpeedLow);
  30. furi_hal_gpio_enable_int_callback(&gpio_nfc_irq_rfid_pull);
  31. }
  32. void platformDisableIrqCallback() {
  33. furi_hal_gpio_init(&gpio_nfc_irq_rfid_pull, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  34. furi_hal_gpio_disable_int_callback(&gpio_nfc_irq_rfid_pull);
  35. }
  36. void platformSetIrqCallback(PlatformIrqCallback callback) {
  37. rfal_platform.callback = callback;
  38. rfal_platform.thread = furi_thread_alloc();
  39. furi_thread_set_name(rfal_platform.thread, "RfalIrqDriver");
  40. furi_thread_set_callback(rfal_platform.thread, rfal_platform_irq_thread);
  41. furi_thread_set_stack_size(rfal_platform.thread, 1024);
  42. furi_thread_set_priority(rfal_platform.thread, FuriThreadPriorityIsr);
  43. furi_thread_start(rfal_platform.thread);
  44. furi_hal_gpio_add_int_callback(&gpio_nfc_irq_rfid_pull, nfc_isr, NULL);
  45. // Disable interrupt callback as the pin is shared between 2 apps
  46. // It is enabled in rfalLowPowerModeStop()
  47. furi_hal_gpio_disable_int_callback(&gpio_nfc_irq_rfid_pull);
  48. }
  49. bool platformSpiTxRx(const uint8_t* txBuf, uint8_t* rxBuf, uint16_t len) {
  50. bool ret = false;
  51. if(txBuf && rxBuf) {
  52. ret =
  53. furi_hal_spi_bus_trx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)txBuf, rxBuf, len, 1000);
  54. } else if(txBuf) {
  55. ret = furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)txBuf, len, 1000);
  56. } else if(rxBuf) {
  57. ret = furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)rxBuf, len, 1000);
  58. }
  59. return ret;
  60. }
  61. void platformProtectST25RComm() {
  62. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_nfc);
  63. }
  64. void platformUnprotectST25RComm() {
  65. furi_hal_spi_release(&furi_hal_spi_bus_handle_nfc);
  66. }