esp_ieee802154_dev.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_log.h"
  10. #include "esp_err.h"
  11. #include "soc/soc.h"
  12. #include "esp_ieee802154_frame.h"
  13. #include "esp_ieee802154.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define IEEE802154_TAG "ieee802154"
  18. // These three macros are in microseconds, used for transmit_at
  19. #define IEEE802154_ED_TRIG_TX_RAMPUP_TIME_US 256
  20. #define IEEE802154_TX_RAMPUP_TIME_US 98
  21. #define IEEE802154_RX_RAMPUP_TIME_US 146
  22. /**
  23. * @brief The state of IEEE802154 radio process.
  24. */
  25. typedef enum {
  26. IEEE802154_STATE_DISABLE, /*!< IEEE802154 radio state disable */
  27. IEEE802154_STATE_IDLE, /*!< IEEE802154 radio state idle */
  28. IEEE802154_STATE_SLEEP, /*!< IEEE802154 radio state sleep */
  29. IEEE802154_STATE_RX, /*!< IEEE802154 radio state rx */
  30. IEEE802154_STATE_TX_ACK, /*!< IEEE802154 radio state tx ack */
  31. IEEE802154_STATE_TX_ENH_ACK, /*!< IEEE802154 radio state tx enh-ack */
  32. IEEE802154_STATE_TX_CCA, /*!< IEEE802154 radio state cca trigger tx */
  33. IEEE802154_STATE_TX, /*!< IEEE802154 radio state tx */
  34. IEEE802154_STATE_TEST_TX, /*!< IEEE802154 radio state test mode tx */
  35. IEEE802154_STATE_RX_ACK, /*!< IEEE802154 radio state rx ack */
  36. IEEE802154_STATE_ED, /*!< IEEE802154 radio state ED */
  37. IEEE802154_STATE_CCA, /*!< IEEE802154 radio state CCA */
  38. } ieee802154_state_t;
  39. /**
  40. * @brief Initialize the clock of IEEE 802.15.4 subsystem.
  41. *
  42. */
  43. void ieee802154_enable(void);
  44. /**
  45. * @brief Deinitialize the clock of IEEE 802.15.4 subsystem.
  46. *
  47. */
  48. void ieee802154_disable(void);
  49. /**
  50. * @brief Initialize the IEEE 802.15.4 MAC.
  51. *
  52. * @return
  53. * - ESP_OK on success.
  54. * - ESP_FAIL on failure.
  55. *
  56. */
  57. esp_err_t ieee802154_mac_init(void);
  58. /**
  59. * @brief Transmit the given frame.
  60. *
  61. * @param[in] frame The pointer to the frame
  62. * @param[in] cca Perform CCA before transmission if it's true, otherwise transmit the frame directly.
  63. *
  64. * @return
  65. * - ESP_OK on success.
  66. * - ESP_FAIL on failure due to invalid state.
  67. *
  68. */
  69. esp_err_t ieee802154_transmit(const uint8_t *frame, bool cca);
  70. /**
  71. * @brief Set the IEEE 802.15.4 Radio to receive state.
  72. *
  73. * @return
  74. * - ESP_OK on success
  75. * - ESP_FAIL on failure due to invalid state.
  76. *
  77. */
  78. esp_err_t ieee802154_receive(void);
  79. /**
  80. * @brief Transmit the given frame at a specific time.
  81. *
  82. * @param[in] frame The pointer to the frame. Refer to `esp_ieee802154_transmit()`.
  83. * @param[in] cca Perform CCA before transmission if it's true, otherwise transmit the frame directly.
  84. * @param[in] time A specific timestamp for starting transmission.
  85. *
  86. * @return
  87. * - ESP_OK on success.
  88. * - ESP_FAIL on failure due to invalid state.
  89. *
  90. * Note: The transmit result will be reported via esp_ieee802154_transmit_done()
  91. * or esp_ieee802154_transmit_failed().
  92. *
  93. */
  94. esp_err_t ieee802154_transmit_at(const uint8_t *frame, bool cca, uint32_t time);
  95. /**
  96. * @brief Set the IEEE 802.15.4 Radio to receive state at a specific time.
  97. *
  98. *
  99. * @param[in] time A specific timestamp for starting receiving.
  100. * @return
  101. * - ESP_OK on success
  102. * - ESP_FAIL on failure due to invalid state.
  103. *
  104. * Note: Radio will start receiving after the timestamp, and continue receiving until it receives a valid frame.
  105. * Ref to esp_ieee802154_receive_done().
  106. *
  107. */
  108. esp_err_t ieee802154_receive_at(uint32_t time);
  109. /**
  110. * @brief Set the IEEE 802.15.4 Radio to sleep state.
  111. *
  112. * @return
  113. * - ESP_OK on success.
  114. * - ESP_FAIL on failure due to invalid state.
  115. *
  116. */
  117. esp_err_t ieee802154_sleep(void);
  118. /**
  119. * @brief Perform energy detection(ED).
  120. *
  121. * @param[in] duration The duration of energy detection, in symbol unit (16 us).
  122. * The result will be reported via esp_ieee802154_energy_detect_done().
  123. *
  124. * @return
  125. * - ESP_OK on success.
  126. * - ESP_FAIL on failure due to invalid state.
  127. *
  128. */
  129. esp_err_t ieee802154_energy_detect(uint32_t duration);
  130. /**
  131. * @brief Perform clear channel assessment(CCA).
  132. *
  133. * @return
  134. * - ESP_OK on success.
  135. * - ESP_FAIL on failure due to invalid state.
  136. *
  137. */
  138. esp_err_t ieee802154_cca(void);
  139. /**
  140. * @brief Get the RSSI of the most recent received frame.
  141. *
  142. * @return The value of RSSI.
  143. *
  144. */
  145. int8_t ieee802154_get_recent_rssi(void);
  146. /**
  147. * @brief Get the LQI of the most recent received frame.
  148. *
  149. * @return The value of LQI.
  150. *
  151. */
  152. uint8_t ieee802154_get_recent_lqi(void);
  153. /**
  154. * @brief Get the IEEE 802.15.4 Radio state.
  155. *
  156. * @return
  157. * - Current state of IEEE 802.15.4 Radio.
  158. *
  159. */
  160. ieee802154_state_t ieee802154_get_state(void);
  161. /** The following three functions are only used for internal test. **/
  162. /**
  163. * @brief The clear channel assessment done.
  164. *
  165. * @param[in] channel_free True if the channel is clear, otherwise false.
  166. *
  167. */
  168. extern void esp_ieee802154_cca_done(bool channel_free);
  169. /**
  170. * @brief Current receiving process is failed due to some reasons.
  171. *
  172. * @param[in] error The specific received failed reason.
  173. *
  174. */
  175. extern void esp_ieee802154_receive_failed(uint16_t error);
  176. /**
  177. * @brief Current energy detection(ED) is failed due to some reasons.
  178. *
  179. * @param[in] error The specific ED failed reason.
  180. *
  181. */
  182. extern void esp_ieee802154_ed_failed(uint16_t error);
  183. #if CONFIG_IEEE802154_TEST
  184. #define IEEE802154_STATIC
  185. #define IEEE802154_INLINE
  186. extern void esp_ieee802154_timer0_done(void);
  187. extern void esp_ieee802154_timer1_done(void);
  188. #else
  189. #define IEEE802154_STATIC static
  190. #define IEEE802154_INLINE inline
  191. #endif // CONFIG_IEEE802154_TEST
  192. #ifdef __cplusplus
  193. }
  194. #endif