transceiver.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_sleep.h"
  9. #include "esp_log.h"
  10. #include "driver/uart.h"
  11. #include "driver/rtc_io.h"
  12. #include "esp_intr_alloc.h"
  13. #include "esp_attr.h"
  14. #include "driver/timer.h"
  15. #include <stddef.h>
  16. #include "esp_intr_alloc.h"
  17. #include "transceiver.h"
  18. #include "rxTimer.h"
  19. bool disableRx = true; // Controls if trcv is in RX or TX-mode
  20. #ifdef TRANCEIVER_ENABLED
  21. void trcvSendHighLowPulse(uint32_t high, uint32_t low) {
  22. if( disableRx == false ) return;
  23. gpio_set_level(GPIO_TX_DATA, 1);
  24. delayMicroseconds( high );
  25. gpio_set_level(GPIO_TX_DATA, 0);
  26. delayMicroseconds( low );
  27. }
  28. void initTransceiver() {
  29. gpio_set_level(GPIO_ENABLE, 0);
  30. gpio_set_level(GPIO_TX_ENABLE, 0);
  31. gpio_set_level(GPIO_TX_DATA, 0);
  32. gpio_pad_select_gpio(GPIO_ENABLE);
  33. gpio_pad_select_gpio(GPIO_TX_ENABLE);
  34. gpio_pad_select_gpio(GPIO_TX_DATA);
  35. gpio_pad_select_gpio(GPIO_RX_DATA);
  36. gpio_set_direction(GPIO_ENABLE, GPIO_MODE_OUTPUT);
  37. gpio_set_direction(GPIO_TX_ENABLE, GPIO_MODE_OUTPUT);
  38. gpio_set_direction(GPIO_TX_DATA, GPIO_MODE_OUTPUT);
  39. gpio_set_direction(GPIO_RX_DATA, GPIO_MODE_INPUT);
  40. // ------------------ INIT THE TRANCEIVER -------------------------
  41. // From powerdown mode (pin 4-5-6 low),
  42. // 1. Drive high pin 6 (ENABLE)
  43. // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us
  44. // 3. Drive down 20us pin 6 (ENABLE).
  45. gpio_set_level(GPIO_ENABLE, 1); delayMicroseconds( 20 );
  46. gpio_set_level(GPIO_TX_ENABLE, 1); delayMicroseconds( 200 ); gpio_set_level(GPIO_TX_ENABLE, 0);
  47. delayMicroseconds( 40 );
  48. gpio_set_level(GPIO_ENABLE, 0); delayMicroseconds( 20 ); gpio_set_level(GPIO_ENABLE, 1);
  49. delayMicroseconds( 200 );
  50. // ------------------ INIT THE TRANCEIVER -------------------------
  51. ESP_LOGI("TRCV", "Transceiver has been initialized.");
  52. }
  53. void trcvSwitch2transmit() {
  54. disableRx = true;
  55. // ------------------ GO TO TRANSMIT STATE -------------------------
  56. gpio_set_level(GPIO_TX_ENABLE, 1);
  57. delayMicroseconds( 400 );
  58. // ------------------ GO TO TRANSMIT STATE -------------------------
  59. }
  60. void trcvSwitch2receive() {
  61. // ------------------ GO TO RECEIVE STATE -------------------------
  62. // From Transmission Mode to Receiver Mode follow the procedure:
  63. // Drive down pin 5 (RX/TX), after 40us drive down pin 6 (ENABLE) 20us long.
  64. //After 200 us the device is ready for reception.
  65. gpio_set_level(GPIO_TX_ENABLE, 0);
  66. delayMicroseconds( 40 );
  67. gpio_set_level(GPIO_ENABLE, 0);
  68. delayMicroseconds( 20 );
  69. gpio_set_level(GPIO_ENABLE, 1);
  70. delayMicroseconds( 200 );
  71. // ------------------ GO TO RECEIVE STATE -------------------------
  72. disableRx = false;
  73. }
  74. #endif