esp_ieee802154.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "esp_err.h"
  17. #include "esp_ieee802154_types.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @brief Initialize the IEEE 802.15.4 subsystem.
  23. *
  24. */
  25. void esp_ieee802154_enable(void);
  26. /**
  27. * @brief Deinitialize the IEEE 802.15.4 subsystem.
  28. *
  29. */
  30. void esp_ieee802154_disable(void);
  31. /**
  32. * @brief Get the operational channel.
  33. *
  34. * @return The channel number (11~26).
  35. *
  36. */
  37. uint8_t esp_ieee802154_get_channel(void);
  38. /**
  39. * @brief Set the operational channel.
  40. *
  41. * @param[in] channel The channel number (11-26).
  42. *
  43. */
  44. void esp_ieee802154_set_channnel(uint8_t channel);
  45. /**
  46. * @brief Get the transmit power.
  47. *
  48. * @return The transmit power in dBm.
  49. *
  50. */
  51. int8_t esp_ieee802154_get_txpower(void);
  52. /**
  53. * @brief Set the transmit power.
  54. *
  55. * @param[in] power The transmit power in dBm.
  56. *
  57. */
  58. void esp_ieee802154_set_txpower(int8_t power);
  59. /**
  60. * @brief Get the promiscuous mode.
  61. *
  62. * @return
  63. * - True The promiscuous mode is enabled.
  64. * - False The promiscuous mode is disabled.
  65. *
  66. */
  67. bool esp_ieee802154_get_promiscuous(void);
  68. /**
  69. * @brief Set the promiscuous mode.
  70. *
  71. * @param[in] enable The promiscuous mode to be set.
  72. *
  73. */
  74. void esp_ieee802154_set_promiscuous(bool enable);
  75. /**
  76. * @brief Get the IEEE 802.15.4 Radio state.
  77. *
  78. * @return The IEEE 802.15.4 Radio state, refer to esp_ieee802154_state_t.
  79. *
  80. */
  81. esp_ieee802154_state_t esp_ieee802154_get_state(void);
  82. /**
  83. * @brief Set the IEEE 802.15.4 Radio to sleep state.
  84. *
  85. * @return
  86. * - ESP_OK on success.
  87. * - ESP_FAIL on failure due to invalid state.
  88. *
  89. */
  90. esp_err_t esp_ieee802154_sleep(void);
  91. /**
  92. * @brief Set the IEEE 802.15.4 Radio to receive state.
  93. *
  94. * @return
  95. * - ESP_OK on success
  96. * - ESP_FAIL on failure due to invalid state.
  97. *
  98. * Note: Radio will continue receiving until it receives a valid frame.
  99. * Ref to esp_ieee802154_receive_done().
  100. *
  101. */
  102. esp_err_t esp_ieee802154_receive(void);
  103. /**
  104. * @brief Transmit the given frame.
  105. *
  106. * @param[in] frame The pointer to the frame, the frame format:
  107. * |-----------------------------------------------------------------------|
  108. * | Len | MHR | MAC Payload | FCS |
  109. * |-----------------------------------------------------------------------|
  110. * @param[in] cca Perform CCA before transmission if it's true, otherwise transmit the frame directly.
  111. *
  112. * @return
  113. * - ESP_OK on success.
  114. * - ESP_FAIL on failure due to invalid state.
  115. *
  116. * Note: The transmit result will be reported via esp_ieee802154_transmit_done()
  117. * or esp_ieee802154_transmit_failed().
  118. *
  119. */
  120. esp_err_t esp_ieee802154_transmit(const uint8_t *frame, bool cca);
  121. /**
  122. * @brief Set the time to wait for the ack frame.
  123. *
  124. * @param[in] timeout The time to wait for the ack frame, in symbol unit (16 us).
  125. * Default: 0x006C, Range: 0x0000 - 0xFFFF.
  126. *
  127. */
  128. void esp_ieee802154_set_ack_timeout(uint32_t timeout);
  129. /**
  130. * @brief Get the device PAN ID.
  131. *
  132. * @return The device PAN ID.
  133. *
  134. */
  135. uint16_t esp_ieee802154_get_panid(void);
  136. /**
  137. * @brief Set the device PAN ID.
  138. *
  139. * @param[in] panid The device PAN ID.
  140. *
  141. */
  142. void esp_ieee802154_set_panid(uint16_t panid);
  143. /**
  144. * @brief Get the device short address.
  145. *
  146. * @return The device short address.
  147. *
  148. */
  149. uint16_t esp_ieee802154_get_short_address(void);
  150. /**
  151. * @brief Set the device short address.
  152. *
  153. * @param[in] short_address The device short address.
  154. *
  155. */
  156. void esp_ieee802154_set_short_address(uint16_t short_address);
  157. /**
  158. * @brief Get the device extended address.
  159. *
  160. * @param[out] ext_addr The pointer to the device extended address.
  161. *
  162. */
  163. void esp_ieee802154_get_extended_address(uint8_t *ext_addr);
  164. /**
  165. * @brief Set the device extended address.
  166. *
  167. * @param[in] ext_addr The pointer to the device extended address.
  168. *
  169. */
  170. void esp_ieee802154_set_extended_address(const uint8_t *ext_addr);
  171. /**
  172. * @brief Get the device coordinator.
  173. *
  174. * @return
  175. * - True The coordinator is enabled.
  176. * - False The coordinator is disabled.
  177. *
  178. */
  179. bool esp_ieee802154_get_coordinator(void);
  180. /**
  181. * @brief Set the device coordinator role.
  182. *
  183. * @param[in] enable The coordinator role to be set.
  184. *
  185. */
  186. void esp_ieee802154_set_coordinator(bool enable);
  187. /**
  188. * @brief Get the auto frame pending mode.
  189. *
  190. * @return The auto frame pending mode, refer to esp_ieee802154_pending_mode_t.
  191. *
  192. */
  193. esp_ieee802154_pending_mode_t esp_ieee802154_get_pending_mode(void);
  194. /**
  195. * @brief Set the auto frame pending mode.
  196. *
  197. * @param[in] pending_mode The auto frame pending mode, refer to esp_ieee802154_pending_mode_t.
  198. *
  199. */
  200. void esp_ieee802154_set_pending_mode(esp_ieee802154_pending_mode_t pending_mode);
  201. /**
  202. * @brief Add address to the source matching table.
  203. *
  204. * @param[in] addr The pointer to the address.
  205. * @param[in] is_short Short address or Extended address.
  206. *
  207. * @return
  208. * - ESP_OK on success.
  209. * - ESP_ERR_NO_MEM if the pending table is full.
  210. *
  211. */
  212. esp_err_t esp_ieee802154_add_pending_addr(const uint8_t *addr, bool is_short);
  213. /**
  214. * @brief Remove address from the source matching table.
  215. *
  216. * @param[in] addr The pointer to the address.
  217. * @param[in] is_short Short address or Extended address.
  218. *
  219. * @return
  220. * - ESP_OK on success.
  221. * - ESP_ERR_NOT_FOUND if the address was not found from the source matching table.
  222. *
  223. */
  224. esp_err_t esp_ieee802154_clear_pending_addr(const uint8_t *addr, bool is_short);
  225. /**
  226. * @brief Clear the source matching table to empty.
  227. *
  228. * @param[in] is_short Clear Short address table or Extended address table.
  229. *
  230. */
  231. void esp_ieee802154_reset_pending_table(bool is_short);
  232. /**
  233. * @brief Get the CCA threshold.
  234. *
  235. * @return The CCA threshold in dBm.
  236. *
  237. */
  238. int8_t esp_ieee802154_get_cca_threshold(void);
  239. /**
  240. * @brief Set the CCA threshold.
  241. *
  242. * @param[in] cca_threshold The CCA threshold in dBm.
  243. *
  244. */
  245. void esp_ieee802154_set_cca_threshold(int8_t cca_threshold);
  246. /**
  247. * @brief Get the CCA mode.
  248. *
  249. * @return The CCA mode, refer to esp_ieee802154_cca_mode_t.
  250. *
  251. */
  252. esp_ieee802154_cca_mode_t esp_ieee802154_get_cca_mode(void);
  253. /**
  254. * @brief Set the CCA mode.
  255. *
  256. * @param[in] cca_mode The CCA mode, refer to esp_ieee802154_cca_mode_t.
  257. *
  258. */
  259. void esp_ieee802154_set_cca_mode(esp_ieee802154_cca_mode_t cca_mode);
  260. /**
  261. * @brief Enable rx_on_when_idle mode, radio will receive during idle.
  262. *
  263. * @param[in] enable Enable/Disable rx_on_when_idle mode.
  264. *
  265. */
  266. void esp_ieee802154_set_rx_when_idle(bool enable);
  267. /**
  268. * @brief Get the rx_on_when_idle mode.
  269. *
  270. * @return rx_on_when_idle mode.
  271. *
  272. */
  273. bool esp_ieee802154_get_rx_when_idle(void);
  274. /**
  275. * @brief Perform energy detection.
  276. *
  277. * @param[in] duration The duration of energy detection, in symbol unit (16 us).
  278. * The result will be reported via esp_ieee802154_energy_detect_done().
  279. *
  280. * @return
  281. * - ESP_OK on success.
  282. * - ESP_FAIL on failure due to invalid state.
  283. *
  284. */
  285. esp_err_t esp_ieee802154_energy_detect(uint32_t duration);
  286. /** Below are the events generated by IEEE 802.15.4 subsystem, which are in ISR context **/
  287. /**
  288. * @brief A Frame was received.
  289. *
  290. * @param[in] frame The point to the received frame, frame format:
  291. * |-----------------------------------------------------------------------|
  292. * | Len | MHR | MAC Payload (no FCS) |
  293. * |-----------------------------------------------------------------------|
  294. * @param[in] frame_info More information of the received frame, refer to esp_ieee802154_frame_info_t.
  295. *
  296. */
  297. extern void esp_ieee802154_receive_done(uint8_t *frame, esp_ieee802154_frame_info_t *frame_info);
  298. /**
  299. * @brief The SFD field of the frame was received.
  300. *
  301. */
  302. extern void esp_ieee802154_receive_sfd_done(void);
  303. /**
  304. * @brief The Frame Transmission succeeded.
  305. *
  306. * @param[in] frame The pointer to the transmitted frame.
  307. * @param[in] ack The received ACK frame, it could be NULL if the transmitted frame's AR bit is not set.
  308. * @param[in] ack_frame_info More information of the ACK frame, refer to esp_ieee802154_frame_info_t.
  309. *
  310. * Note: refer to esp_ieee802154_transmit().
  311. *
  312. */
  313. extern void esp_ieee802154_transmit_done(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info);
  314. /**
  315. * @brief The Frame Transmission failed.
  316. *
  317. * @param[in] frame The pointer to the frame.
  318. * @param[in] error The transmission failure reason, refer to esp_ieee802154_tx_error_t.
  319. *
  320. * Note: refer to esp_ieee802154_transmit().
  321. *
  322. */
  323. extern void esp_ieee802154_transmit_failed(const uint8_t *frame, esp_ieee802154_tx_error_t error);
  324. /**
  325. * @brief The SFD field of the frame was transmitted.
  326. *
  327. */
  328. extern void esp_ieee802154_transmit_sfd_done(uint8_t *frame);
  329. /**
  330. * @brief The energy detection done.
  331. *
  332. * @param[in] power The detected power level, in dBm.
  333. *
  334. * Note: refer to esp_ieee802154_energy_detect().
  335. *
  336. */
  337. extern void esp_ieee802154_energy_detect_done(int8_t power);
  338. #ifdef __cplusplus
  339. }
  340. #endif