platform.c 2.2 KB

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