platform.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 && platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  12. osThreadFlagsSet(platform_irq_thread_id, 0x1);
  13. }
  14. }
  15. void platformIrqWorker() {
  16. while(1) {
  17. uint32_t flags = osThreadFlagsWait(0x1, osFlagsWaitAny, osWaitForever);
  18. if(flags & 0x1) {
  19. platform_irq_callback();
  20. }
  21. }
  22. }
  23. void platformEnableIrqCallback() {
  24. hal_gpio_init(&pin, GpioModeInterruptRise, GpioPullDown, GpioSpeedLow);
  25. hal_gpio_enable_int_callback(&pin);
  26. }
  27. void platformDisableIrqCallback() {
  28. hal_gpio_init(&pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
  29. hal_gpio_disable_int_callback(&pin);
  30. }
  31. void platformSetIrqCallback(PlatformIrqCallback callback) {
  32. platform_irq_callback = callback;
  33. platform_irq_thread_attr.name = "RfalIrqWorker";
  34. platform_irq_thread_attr.stack_size = 1024;
  35. platform_irq_thread_attr.priority = osPriorityRealtime;
  36. platform_irq_thread_id = osThreadNew(platformIrqWorker, NULL, &platform_irq_thread_attr);
  37. 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. hal_gpio_disable_int_callback(&pin);
  41. }
  42. HAL_StatusTypeDef 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. if(!ret) {
  53. return HAL_ERROR;
  54. } else {
  55. return HAL_OK;
  56. }
  57. }
  58. void platformProtectST25RComm() {
  59. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_nfc);
  60. }
  61. void platformUnprotectST25RComm() {
  62. furi_hal_spi_release(&furi_hal_spi_bus_handle_nfc);
  63. }