platform.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "platform.h"
  2. #include <assert.h>
  3. #include <furi.h>
  4. #include <furi_hal_spi.h>
  5. static const osThreadAttr_t platform_irq_thread_attr = {
  6. .name = "RfalIrqDriver",
  7. .stack_size = 1024,
  8. .priority = osPriorityRealtime,
  9. };
  10. static volatile osThreadId_t platform_irq_thread_id = NULL;
  11. static volatile PlatformIrqCallback platform_irq_callback = NULL;
  12. static const GpioPin pin = {ST25R_INT_PORT, ST25R_INT_PIN};
  13. void nfc_isr(void* _ctx) {
  14. if(platform_irq_callback && platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  15. osThreadFlagsSet(platform_irq_thread_id, 0x1);
  16. }
  17. }
  18. void platformIrqThread() {
  19. while(1) {
  20. uint32_t flags = osThreadFlagsWait(0x1, osFlagsWaitAny, osWaitForever);
  21. if(flags & 0x1) {
  22. platform_irq_callback();
  23. }
  24. }
  25. }
  26. void platformEnableIrqCallback() {
  27. furi_hal_gpio_init(&pin, GpioModeInterruptRise, GpioPullDown, GpioSpeedLow);
  28. furi_hal_gpio_enable_int_callback(&pin);
  29. }
  30. void platformDisableIrqCallback() {
  31. furi_hal_gpio_init(&pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  32. furi_hal_gpio_disable_int_callback(&pin);
  33. }
  34. void platformSetIrqCallback(PlatformIrqCallback callback) {
  35. platform_irq_callback = callback;
  36. platform_irq_thread_id = osThreadNew(platformIrqThread, NULL, &platform_irq_thread_attr);
  37. furi_hal_gpio_add_int_callback(&pin, nfc_isr, NULL);
  38. // Disable interrupt callback as the pin is shared between 2 apps
  39. // It is enabled in rfalLowPowerModeStop()
  40. furi_hal_gpio_disable_int_callback(&pin);
  41. }
  42. bool platformSpiTxRx(const uint8_t* txBuf, uint8_t* rxBuf, uint16_t len) {
  43. bool ret = false;
  44. if(txBuf && rxBuf) {
  45. ret =
  46. furi_hal_spi_bus_trx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)txBuf, rxBuf, len, 1000);
  47. } else if(txBuf) {
  48. ret = furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)txBuf, len, 1000);
  49. } else if(rxBuf) {
  50. ret = furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_nfc, (uint8_t*)rxBuf, len, 1000);
  51. }
  52. return ret;
  53. }
  54. void platformProtectST25RComm() {
  55. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_nfc);
  56. }
  57. void platformUnprotectST25RComm() {
  58. furi_hal_spi_release(&furi_hal_spi_bus_handle_nfc);
  59. }