transceiver.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. void trcvSendHighLowPulse(uint32_t high, uint32_t low) {
  21. if( disableRx == false ) return;
  22. gpio_set_level(GPIO_TX_DATA, 1);
  23. delayMicroseconds( high );
  24. gpio_set_level(GPIO_TX_DATA, 0);
  25. delayMicroseconds( low );
  26. }
  27. void initTransceiver() {
  28. gpio_set_level(GPIO_ENABLE, 0);
  29. gpio_set_level(GPIO_TX_ENABLE, 0);
  30. gpio_set_level(GPIO_TX_DATA, 0);
  31. gpio_pad_select_gpio(GPIO_ENABLE);
  32. gpio_pad_select_gpio(GPIO_TX_ENABLE);
  33. gpio_pad_select_gpio(GPIO_TX_DATA);
  34. gpio_pad_select_gpio(GPIO_RX_DATA);
  35. gpio_set_direction(GPIO_ENABLE, GPIO_MODE_OUTPUT);
  36. gpio_set_direction(GPIO_TX_ENABLE, GPIO_MODE_OUTPUT);
  37. gpio_set_direction(GPIO_TX_DATA, GPIO_MODE_OUTPUT);
  38. gpio_set_direction(GPIO_RX_DATA, GPIO_MODE_INPUT);
  39. // ------------------ INIT THE TRANCEIVER -------------------------
  40. // From powerdown mode (pin 4-5-6 low),
  41. // 1. Drive high pin 6 (ENABLE)
  42. // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us
  43. // 3. Drive down 20us pin 6 (ENABLE).
  44. gpio_set_level(GPIO_ENABLE, 1); delayMicroseconds( 20 );
  45. gpio_set_level(GPIO_TX_ENABLE, 1); delayMicroseconds( 200 ); gpio_set_level(GPIO_TX_ENABLE, 0);
  46. delayMicroseconds( 40 );
  47. gpio_set_level(GPIO_ENABLE, 0); delayMicroseconds( 20 ); gpio_set_level(GPIO_ENABLE, 1);
  48. delayMicroseconds( 200 );
  49. // ------------------ INIT THE TRANCEIVER -------------------------
  50. ESP_LOGI("TRCV", "Transceiver has been initialized.");
  51. }
  52. void trcvSwitch2transmit() {
  53. disableRx = true;
  54. // ------------------ GO TO TRANSMIT STATE -------------------------
  55. gpio_set_level(GPIO_TX_ENABLE, 1);
  56. delayMicroseconds( 400 );
  57. // ------------------ GO TO TRANSMIT STATE -------------------------
  58. }
  59. void trcvSwitch2receive() {
  60. // ------------------ GO TO RECEIVE STATE -------------------------
  61. // From Transmission Mode to Receiver Mode follow the procedure:
  62. // Drive down pin 5 (RX/TX), after 40us drive down pin 6 (ENABLE) 20us long.
  63. //After 200 us the device is ready for reception.
  64. gpio_set_level(GPIO_TX_ENABLE, 0);
  65. delayMicroseconds( 40 );
  66. gpio_set_level(GPIO_ENABLE, 0);
  67. delayMicroseconds( 20 );
  68. gpio_set_level(GPIO_ENABLE, 1);
  69. delayMicroseconds( 200 );
  70. // ------------------ GO TO RECEIVE STATE -------------------------
  71. disableRx = false;
  72. }