platform.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 const GpioPin pin = {ST25R_INT_PORT, ST25R_INT_PIN};
  10. void nfc_isr(void* _ctx) {
  11. if(platform_irq_callback
  12. && platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  13. osThreadFlagsSet(platform_irq_thread_id, 0x1);
  14. }
  15. }
  16. void platformIrqWorker() {
  17. while(1) {
  18. uint32_t flags = osThreadFlagsWait(0x1, osFlagsWaitAny, osWaitForever);
  19. if (flags & 0x1) {
  20. platform_irq_callback();
  21. }
  22. }
  23. }
  24. void platformEnableIrqCallback() {
  25. hal_gpio_init(&pin, GpioModeInterruptRise, GpioPullDown, GpioSpeedLow);
  26. hal_gpio_enable_int_callback(&pin);
  27. }
  28. void platformDisableIrqCallback() {
  29. hal_gpio_init(&pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  30. hal_gpio_disable_int_callback(&pin);
  31. }
  32. void platformSetIrqCallback(PlatformIrqCallback callback) {
  33. platform_irq_callback = callback;
  34. platform_irq_thread_attr.name = "RfalIrqWorker";
  35. platform_irq_thread_attr.stack_size = 1024;
  36. platform_irq_thread_attr.priority = osPriorityISR;
  37. platform_irq_thread_id = osThreadNew(platformIrqWorker, 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 = 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. if(!ret) {
  53. asm("bkpt 1");
  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. }