platform.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "platform.h"
  2. #include <assert.h>
  3. #include <main.h>
  4. #include <api-hal-spi.h>
  5. static osThreadAttr_t platform_irq_thread_attr;
  6. static volatile osThreadId_t platform_irq_thread_id = NULL;
  7. static volatile PlatformIrqCallback platform_irq_callback = NULL;
  8. void nfc_isr() {
  9. if(platform_irq_callback && platformGpioIsHigh( ST25R_INT_PORT, ST25R_INT_PIN )) {
  10. osThreadFlagsSet(platform_irq_thread_id, 0x1);
  11. }
  12. }
  13. void platformIrqWorker() {
  14. while(1) {
  15. uint32_t flags = osThreadFlagsWait(0x1, osFlagsWaitAny, osWaitForever);
  16. if (flags & 0x1) {
  17. platform_irq_callback();
  18. }
  19. }
  20. }
  21. void platformSetIrqCallback(PlatformIrqCallback callback) {
  22. platform_irq_callback = callback;
  23. platform_irq_thread_attr.name = "rfal_irq_worker";
  24. platform_irq_thread_attr.stack_size = 512;
  25. platform_irq_thread_attr.priority = osPriorityISR;
  26. platform_irq_thread_id = osThreadNew(platformIrqWorker, NULL, &platform_irq_thread_attr);
  27. }
  28. HAL_StatusTypeDef platformSpiTxRx(const uint8_t *txBuf, uint8_t *rxBuf, uint16_t len) {
  29. HAL_StatusTypeDef ret;
  30. if (txBuf && rxBuf) {
  31. ret = HAL_SPI_TransmitReceive(&SPI_R, (uint8_t*)txBuf, rxBuf, len, HAL_MAX_DELAY);
  32. } else if (txBuf) {
  33. ret = HAL_SPI_Transmit(&SPI_R, (uint8_t*)txBuf, len, HAL_MAX_DELAY);
  34. } else if (rxBuf) {
  35. ret = HAL_SPI_Receive(&SPI_R, (uint8_t*)rxBuf, len, HAL_MAX_DELAY);
  36. }
  37. if(ret != HAL_OK) {
  38. asm("bkpt 1");
  39. exit(255);
  40. }
  41. return ret;
  42. }
  43. void platformProtectST25RComm() {
  44. api_hal_spi_lock(&SPI_R);
  45. NFC_SPI_Reconfigure();
  46. }
  47. void platformUnprotectST25RComm() {
  48. api_hal_spi_unlock(&SPI_R);
  49. }