Skip to content

Commit

Permalink
Merge branch 'backport/ieee802154_receive_done_handler_v51' into 'rel…
Browse files Browse the repository at this point in the history
…ease/v5.1'

fix(ieee802154): introduce a receive done handler feature(backportV5.1)

See merge request espressif/esp-idf!28133
  • Loading branch information
chshu committed Dec 28, 2023
2 parents 9088450 + 66efe67 commit 47c8944
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 18 deletions.
8 changes: 8 additions & 0 deletions components/ieee802154/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ menu "IEEE 802.15.4"
configure the CCA mode to Carrier sense AND energy above threshold
endchoice

config IEEE802154_RECEIVE_DONE_HANDLER
bool "Enable the receive done handler feature"
default n
help
configure the receive done handler feature, when enabled, the user must call the
function `esp_ieee802154_receive_handle_done` to inform the 802.15.4 driver that
the received frame has been processed, so the frame space could be freed.

config IEEE802154_CCA_MODE
depends on IEEE802154_ENABLED
int
Expand Down
137 changes: 119 additions & 18 deletions components/ieee802154/driver/esp_ieee802154_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,87 @@ extern void bt_bb_set_zb_tx_on_delay(uint16_t time);

IEEE802154_STATIC volatile ieee802154_state_t s_ieee802154_state;
static uint8_t *s_tx_frame = NULL;
static uint8_t s_rx_frame[CONFIG_IEEE802154_RX_BUFFER_SIZE][127 + 1 + 1]; // +1: len, +1: for dma test
#define IEEE802154_RX_FRAME_SIZE (127 + 1 + 1) // +1: len, +1: for dma test

#if CONFIG_IEEE802154_RECEIVE_DONE_HANDLER
// +1: for the stub buffer when the valid buffers are full.
//
// |--------------------VB[0]--------------------|
// |--------------------VB[1]--------------------|
// |--------------------VB[2]--------------------|
// |--------------------VB[3]--------------------|
// |--------------------.....--------------------|
// |-----VB[CONFIG_IEEE802154_RX_BUFFER_SIZE]----|
// |---------------------STUB--------------------|
//
// VB: Valid buffer, used for storing the frame received by HW.
// STUB : Stub buffer, used when all valid buffers are under processing, the received frame will be dropped.
static uint8_t s_rx_frame[CONFIG_IEEE802154_RX_BUFFER_SIZE + 1][IEEE802154_RX_FRAME_SIZE];
static esp_ieee802154_frame_info_t s_rx_frame_info[CONFIG_IEEE802154_RX_BUFFER_SIZE + 1];
#else
static uint8_t s_rx_frame[CONFIG_IEEE802154_RX_BUFFER_SIZE][IEEE802154_RX_FRAME_SIZE];
static esp_ieee802154_frame_info_t s_rx_frame_info[CONFIG_IEEE802154_RX_BUFFER_SIZE];
#endif

static uint8_t s_rx_index = 0;
static uint8_t s_enh_ack_frame[128];
static uint8_t s_recent_rx_frame_info_index;
static portMUX_TYPE s_ieee802154_spinlock = portMUX_INITIALIZER_UNLOCKED;
static intr_handle_t s_ieee802154_isr_handle = NULL;

static esp_err_t ieee802154_sleep_init(void);
static void next_operation(void);

#if CONFIG_IEEE802154_RECEIVE_DONE_HANDLER
static void ieee802154_receive_done(uint8_t *data, esp_ieee802154_frame_info_t *frame_info)
{
// If the RX done packet is written in the stub buffer, drop it silently.
if (s_rx_index == CONFIG_IEEE802154_RX_BUFFER_SIZE) {
esp_rom_printf("receive buffer full, drop the current frame.\n");
} else {
// Otherwise, post it to the upper layer.
frame_info->process = true;
esp_ieee802154_receive_done(data, frame_info);
}
}

static void ieee802154_transmit_done(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info)
{
if (ack && ack_frame_info) {
if (s_rx_index == CONFIG_IEEE802154_RX_BUFFER_SIZE) {
esp_rom_printf("receive buffer full, drop the current ack frame.\n");
esp_ieee802154_transmit_failed(frame, ESP_IEEE802154_TX_ERR_NO_ACK);
} else {
ack_frame_info->process = true;
esp_ieee802154_transmit_done(frame, ack, ack_frame_info);
}
} else {
esp_ieee802154_transmit_done(frame, ack, ack_frame_info);
}
}

esp_err_t ieee802154_receive_handle_done(uint8_t *data)
{
uint16_t size = data - &s_rx_frame[0][0];
if ((size % IEEE802154_RX_FRAME_SIZE) != 0
|| (size / IEEE802154_RX_FRAME_SIZE) >= CONFIG_IEEE802154_RX_BUFFER_SIZE) {
return ESP_FAIL;
}
s_rx_frame_info[size / IEEE802154_RX_FRAME_SIZE].process = false;
return ESP_OK;
}
#else
static void ieee802154_receive_done(uint8_t *data, esp_ieee802154_frame_info_t *frame_info)
{
esp_ieee802154_receive_done(data, frame_info);
}

static void ieee802154_transmit_done(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info)
{
esp_ieee802154_transmit_done(frame, ack, ack_frame_info);
}

#endif

static IRAM_ATTR void event_end_process(void)
{
Expand Down Expand Up @@ -97,15 +169,43 @@ uint8_t ieee802154_get_recent_lqi(void)

IEEE802154_STATIC void set_next_rx_buffer(void)
{
uint8_t* next_rx_buffer = NULL;
#if CONFIG_IEEE802154_RECEIVE_DONE_HANDLER
uint8_t index = 0;
if (s_rx_index != CONFIG_IEEE802154_RX_BUFFER_SIZE && s_rx_frame_info[s_rx_index].process == false) {
// If buffer is not full, and current index is empty, set it to hardware.
next_rx_buffer = s_rx_frame[s_rx_index];
} else {
// Otherwise, trave the buffer to find an empty one.
// Notice, the s_rx_index + 1 is more like an empty one, so check it first.
for (uint8_t i = 1; i <= CONFIG_IEEE802154_RX_BUFFER_SIZE; i++) {
index = (i + s_rx_index) % CONFIG_IEEE802154_RX_BUFFER_SIZE;
if (s_rx_frame_info[index].process == true) {
continue;
} else {
s_rx_index = index;
next_rx_buffer = s_rx_frame[s_rx_index];
break;
}
}
}
// If all buffer is under processing by the upper layer, we set the stub buffer, and
// will not post the received frame to the upper layer.
if (!next_rx_buffer) {
s_rx_index = CONFIG_IEEE802154_RX_BUFFER_SIZE;
next_rx_buffer = s_rx_frame[CONFIG_IEEE802154_RX_BUFFER_SIZE];
}
#else
if (s_rx_frame[s_rx_index][0] != 0) {
s_rx_index++;
if (s_rx_index == CONFIG_IEEE802154_RX_BUFFER_SIZE) {
s_rx_index = 0;
memset(s_rx_frame[s_rx_index], 0, sizeof(s_rx_frame[s_rx_index]));
}
memset(s_rx_frame[s_rx_index], 0, sizeof(s_rx_frame[s_rx_index]));
}

ieee802154_ll_set_rx_addr((uint8_t *)&s_rx_frame[s_rx_index]);
next_rx_buffer = (uint8_t *)&s_rx_frame[s_rx_index];
#endif
ieee802154_ll_set_rx_addr(next_rx_buffer);
}

static bool stop_rx(void)
Expand All @@ -116,7 +216,7 @@ static bool stop_rx(void)

events = ieee802154_ll_get_events();
if (events & IEEE802154_EVENT_RX_DONE) {
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
}

ieee802154_ll_clear_events(IEEE802154_EVENT_RX_DONE | IEEE802154_EVENT_RX_ABORT | IEEE802154_EVENT_RX_SFD_DONE);
Expand All @@ -128,7 +228,7 @@ static bool stop_tx_ack(void)
{
ieee802154_set_cmd(IEEE802154_CMD_STOP);

esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);

ieee802154_ll_clear_events(IEEE802154_EVENT_ACK_TX_DONE | IEEE802154_EVENT_RX_ABORT | IEEE802154_EVENT_TX_SFD_DONE); // ZB-81: clear TX_SFD_DONE event

Expand All @@ -145,11 +245,11 @@ static bool stop_tx(void)

if (s_ieee802154_state == IEEE802154_STATE_TX_ENH_ACK) {
// if current operation is sending 2015 Enh-ack, SW should create the receive-done event.
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_ll_clear_events(IEEE802154_EVENT_ACK_TX_DONE);
} else if ((events & IEEE802154_EVENT_TX_DONE) && (!ieee802154_frame_is_ack_required(s_tx_frame) || !ieee802154_ll_get_rx_auto_ack())) {
// if the tx is already done, and the frame is not ack request OR auto ack rx is disabled.
esp_ieee802154_transmit_done(s_tx_frame, NULL, NULL);
ieee802154_transmit_done(s_tx_frame, NULL, NULL);
} else {
esp_ieee802154_transmit_failed(s_tx_frame, ESP_IEEE802154_TX_ERR_ABORT);
}
Expand Down Expand Up @@ -185,7 +285,7 @@ static bool stop_rx_ack(void)
ieee802154_ll_disable_events(IEEE802154_EVENT_TIMER0_OVERFLOW);

if (events & IEEE802154_EVENT_ACK_RX_DONE) {
esp_ieee802154_transmit_done(s_tx_frame, (uint8_t *)&s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_transmit_done(s_tx_frame, (uint8_t *)&s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
} else {
esp_ieee802154_transmit_failed(s_tx_frame, ESP_IEEE802154_TX_ERR_NO_ACK);
}
Expand Down Expand Up @@ -298,11 +398,11 @@ static IRAM_ATTR void isr_handle_tx_done(void)
{
event_end_process();
if (s_ieee802154_state == IEEE802154_STATE_TX_ENH_ACK) {
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
} else {
if (s_ieee802154_state == IEEE802154_STATE_TEST_TX) {
esp_ieee802154_transmit_done(s_tx_frame, NULL, NULL);
ieee802154_transmit_done(s_tx_frame, NULL, NULL);
next_operation();
} else if (s_ieee802154_state == IEEE802154_STATE_TX || s_ieee802154_state == IEEE802154_STATE_TX_CCA) {
if (ieee802154_frame_is_ack_required(s_tx_frame) && ieee802154_ll_get_rx_auto_ack()) {
Expand All @@ -311,7 +411,7 @@ static IRAM_ATTR void isr_handle_tx_done(void)
receive_ack_timeout_timer_start(200000); // 200ms for receive ack timeout
#endif
} else {
esp_ieee802154_transmit_done(s_tx_frame, NULL, NULL);
ieee802154_transmit_done(s_tx_frame, NULL, NULL);
next_operation();
}
}
Expand Down Expand Up @@ -345,19 +445,19 @@ static IRAM_ATTR void isr_handle_rx_done(void)
} else {
// Stop current process if generator returns errors.
ieee802154_set_cmd(IEEE802154_CMD_STOP);
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
}
} else {
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
}
}
}

static IRAM_ATTR void isr_handle_ack_tx_done(void)
{
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
}

Expand All @@ -366,7 +466,7 @@ static IRAM_ATTR void isr_handle_ack_rx_done(void)
ieee802154_timer0_stop();
ieee802154_ll_disable_events(IEEE802154_EVENT_TIMER0_OVERFLOW);
ieee802154_rx_frame_info_update();
esp_ieee802154_transmit_done(s_tx_frame, (uint8_t *)&s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_transmit_done(s_tx_frame, (uint8_t *)&s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
}

Expand Down Expand Up @@ -408,7 +508,7 @@ static IRAM_ATTR void isr_handle_rx_abort(void)
case IEEE802154_RX_ABORT_BY_TX_ACK_COEX_BREAK:
IEEE802154_ASSERT(s_ieee802154_state == IEEE802154_STATE_TX_ACK || s_ieee802154_state == IEEE802154_STATE_TX_ENH_ACK);
#if !CONFIG_IEEE802154_TEST
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
#else
esp_ieee802154_receive_failed(rx_status);
Expand All @@ -417,7 +517,7 @@ static IRAM_ATTR void isr_handle_rx_abort(void)
case IEEE802154_RX_ABORT_BY_ENHACK_SECURITY_ERROR:
IEEE802154_ASSERT(s_ieee802154_state == IEEE802154_STATE_TX_ENH_ACK);
#if !CONFIG_IEEE802154_TEST
esp_ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
ieee802154_receive_done((uint8_t *)s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index]);
next_operation();
#else
esp_ieee802154_receive_failed(rx_status);
Expand Down Expand Up @@ -657,6 +757,7 @@ esp_err_t ieee802154_mac_init(void)
#endif

memset(s_rx_frame, 0, sizeof(s_rx_frame));

ieee802154_set_state(IEEE802154_STATE_IDLE);

// TODO: Add flags for IEEE802154 ISR allocating. TZ-102
Expand Down
9 changes: 9 additions & 0 deletions components/ieee802154/esp_ieee802154.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include <stdint.h>
#include <string.h>
#include "sdkconfig.h"
#include "esp_ieee802154.h"
#include "esp_err.h"
#include "esp_phy_init.h"
#include "esp_ieee802154_ack.h"
#include "esp_ieee802154_dev.h"
Expand Down Expand Up @@ -336,6 +338,13 @@ uint8_t esp_ieee802154_get_recent_lqi(void)
return ieee802154_get_recent_lqi();
}

#if CONFIG_IEEE802154_RECEIVE_DONE_HANDLER
esp_err_t esp_ieee802154_receive_handle_done(uint8_t *frame)
{
return ieee802154_receive_handle_done(frame);
}
#endif

__attribute__((weak)) void esp_ieee802154_receive_done(uint8_t *data, esp_ieee802154_frame_info_t *frame_info)
{

Expand Down
24 changes: 24 additions & 0 deletions components/ieee802154/include/esp_ieee802154.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <stdint.h>
#include <stdbool.h>
#include "sdkconfig.h"
#include "esp_err.h"
#include "esp_ieee802154_types.h"

Expand Down Expand Up @@ -452,6 +453,21 @@ bool esp_ieee802154_get_rx_when_idle(void);
*/
esp_err_t esp_ieee802154_energy_detect(uint32_t duration);

#if CONFIG_IEEE802154_RECEIVE_DONE_HANDLER
/**
* @brief Notify the IEEE 802.15.4 Radio that the frame is handled done by upper layer.
*
* @param[in] frame The pointer to the frame which was passed from the function esp_ieee802154_receive_done.
* or ack frame from esp_ieee802154_transmit_done.
*
* @return
* - ESP_OK on success
* - ESP_FAIL if frame is invalid.
*
*/
esp_err_t esp_ieee802154_receive_handle_done(uint8_t *frame);
#endif

/** Below are the events generated by IEEE 802.15.4 subsystem, which are in ISR context **/
/**
* @brief A Frame was received.
Expand All @@ -462,6 +478,10 @@ esp_err_t esp_ieee802154_energy_detect(uint32_t duration);
* |-----------------------------------------------------------------------|
* @param[in] frame_info More information of the received frame, refer to esp_ieee802154_frame_info_t.
*
* Note: If configuration `IEEE802154_RECEIVE_DONE_HANDLER` is enabled, then the user must call the function
* `esp_ieee802154_receive_handle_done()` to inform 802.15.4 driver that the received frame is handled.
* See `esp_ieee802154_receive_handle_done()` for more informations.
*
*/
extern void esp_ieee802154_receive_done(uint8_t *frame, esp_ieee802154_frame_info_t *frame_info);

Expand All @@ -480,6 +500,10 @@ extern void esp_ieee802154_receive_sfd_done(void);
*
* Note: refer to esp_ieee802154_transmit().
*
* If configuration `IEEE802154_RECEIVE_DONE_HANDLER` is enabled and ack frame is not null, then after the upper layer has processed the frame,
* the user must call the function `esp_ieee802154_receive_handle_done()` to inform 802.15.4 driver that the ack frame is handled.
* See `esp_ieee802154_receive_handle_done()` for more informations.
*
*/
extern void esp_ieee802154_transmit_done(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info);

Expand Down
1 change: 1 addition & 0 deletions components/ieee802154/include/esp_ieee802154_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef enum {
*/
typedef struct {
bool pending; /*!< The frame was acked with frame pending set */
bool process; /*!< The frame needs to be processed by the upper layer */
uint8_t channel; /*!< Channel */
int8_t rssi; /*!< RSSI */
uint8_t lqi; /*!< LQI */
Expand Down
16 changes: 16 additions & 0 deletions components/ieee802154/private_include/esp_ieee802154_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <stdint.h>
#include <stdbool.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_err.h"
#include "soc/soc.h"
Expand Down Expand Up @@ -109,6 +110,21 @@ esp_err_t ieee802154_transmit(const uint8_t *frame, bool cca);
*/
esp_err_t ieee802154_receive(void);

#if CONFIG_IEEE802154_RECEIVE_DONE_HANDLER
/**
* @brief Notify the IEEE 802.15.4 Radio that the frame is handled done by upper layer.
*
* @param[in] frame The pointer to the frame which was passed from the function esp_ieee802154_receive_done.
* or ack frame from esp_ieee802154_transmit_done.
*
* @return
* - ESP_OK on success
* - ESP_FAIL if frame is invalid.
*
*/
esp_err_t ieee802154_receive_handle_done(uint8_t* frame);
#endif

/**
* @brief Transmit the given frame at a specific time.
*
Expand Down
Loading

0 comments on commit 47c8944

Please sign in to comment.