platform.c 2.5 KB

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