transceiver.c 2.8 KB

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