platform.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "platform.h"
  2. #include <assert.h>
  3. #include <main.h>
  4. #include <furi.h>
  5. #include <api-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 ApiHalSpiDevice* platform_st25r3916 = NULL;
  10. void nfc_isr(void* _pin, void* _ctx) {
  11. uint32_t pin = (uint32_t)_pin;
  12. if(pin == NFC_IRQ_Pin
  13. && platform_irq_callback
  14. && platformGpioIsHigh(ST25R_INT_PORT, ST25R_INT_PIN)) {
  15. osThreadFlagsSet(platform_irq_thread_id, 0x1);
  16. }
  17. }
  18. void platformIrqWorker() {
  19. while(1) {
  20. uint32_t flags = osThreadFlagsWait(0x1, osFlagsWaitAny, osWaitForever);
  21. if (flags & 0x1) {
  22. platform_irq_callback();
  23. }
  24. }
  25. }
  26. void platformSetIrqCallback(PlatformIrqCallback callback) {
  27. platform_irq_callback = callback;
  28. platform_irq_thread_attr.name = "rfal_irq_worker";
  29. platform_irq_thread_attr.stack_size = 1024;
  30. platform_irq_thread_attr.priority = osPriorityISR;
  31. platform_irq_thread_id = osThreadNew(platformIrqWorker, NULL, &platform_irq_thread_attr);
  32. api_interrupt_add(nfc_isr, InterruptTypeExternalInterrupt, NULL);
  33. }
  34. HAL_StatusTypeDef platformSpiTxRx(const uint8_t *txBuf, uint8_t *rxBuf, uint16_t len) {
  35. furi_assert(platform_st25r3916);
  36. bool ret = false;
  37. if (txBuf && rxBuf) {
  38. ret = api_hal_spi_bus_trx(platform_st25r3916->bus, (uint8_t*)txBuf, rxBuf, len, 1000);
  39. } else if (txBuf) {
  40. ret = api_hal_spi_bus_tx(platform_st25r3916->bus, (uint8_t*)txBuf, len, 1000);
  41. } else if (rxBuf) {
  42. ret = api_hal_spi_bus_rx(platform_st25r3916->bus, (uint8_t*)rxBuf, len, 1000);
  43. }
  44. if(!ret) {
  45. asm("bkpt 1");
  46. return HAL_ERROR;
  47. } else {
  48. return HAL_OK;
  49. }
  50. }
  51. void platformProtectST25RComm() {
  52. platform_st25r3916 = (ApiHalSpiDevice*)api_hal_spi_device_get(ApiHalSpiDeviceIdNfc);
  53. }
  54. void platformUnprotectST25RComm() {
  55. furi_assert(platform_st25r3916);
  56. api_hal_spi_device_return(platform_st25r3916);
  57. }