Skip to content

Commit

Permalink
Merge branch 'backport/ieee802154_feature' into 'release/v5.1'
Browse files Browse the repository at this point in the history
Backport some IEEE802154 related MR to 5.1(Backport 5.1)

See merge request espressif/esp-idf!24678
  • Loading branch information
chshu committed Jul 11, 2023
2 parents 788f1de + bdc49fb commit 966312e
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 43 deletions.
5 changes: 3 additions & 2 deletions components/hal/esp32c6/include/hal/ieee802154_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
#include <stdbool.h>
#include "hal/ieee802154_common_ll.h"

#define IEEE802154_TXPOWER_VALUE_MAX 21
#define IEEE802154_TXPOWER_VALUE_MIN -24
#define IEEE802154_TXPOWER_VALUE_MAX 20
#define IEEE802154_TXPOWER_VALUE_MIN -15
#define IEEE802154_TXPOWER_INDEX_MIN 3
3 changes: 2 additions & 1 deletion components/hal/esp32h2/include/hal/ieee802154_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
#include <stdbool.h>
#include "hal/ieee802154_common_ll.h"

#define IEEE802154_TXPOWER_VALUE_MAX 21
#define IEEE802154_TXPOWER_VALUE_MAX 20
#define IEEE802154_TXPOWER_VALUE_MIN -24
#define IEEE802154_TXPOWER_INDEX_MIN 0
6 changes: 6 additions & 0 deletions components/ieee802154/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if(CONFIG_IEEE802154_ENABLED)
"driver/esp_ieee802154_sec.c"
"driver/esp_ieee802154_timer.c")
list(APPEND private_include "private_include")

if(CONFIG_IEEE802154_TEST)
list(REMOVE_ITEM private_include "private_include")
list(APPEND include "private_include")
endif()

endif()

idf_component_register(
Expand Down
45 changes: 36 additions & 9 deletions components/ieee802154/driver/esp_ieee802154_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

extern void bt_bb_set_zb_tx_on_delay(uint16_t time);

static volatile ieee802154_state_t s_ieee802154_state;
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
static esp_ieee802154_frame_info_t s_rx_frame_info[CONFIG_IEEE802154_RX_BUFFER_SIZE];
Expand Down Expand Up @@ -76,7 +76,7 @@ uint8_t ieee802154_get_recent_lqi(void)
return s_rx_frame_info[s_recent_rx_frame_info_index].lqi;
}

static void set_next_rx_buffer(void)
IEEE802154_STATIC void set_next_rx_buffer(void)
{
if (s_rx_frame[s_rx_index][0] != 0) {
s_rx_index++;
Expand Down Expand Up @@ -185,7 +185,7 @@ static bool stop_ed(void)
return true;
}

static bool stop_current_operation(void)
IEEE802154_STATIC bool stop_current_operation(void)
{
event_end_process();
switch (s_ieee802154_state) {
Expand Down Expand Up @@ -257,11 +257,17 @@ static void isr_handle_timer0_done(void)
esp_ieee802154_transmit_failed(s_tx_frame, ESP_IEEE802154_TX_ERR_NO_ACK);
next_operation();
}
#if CONFIG_IEEE802154_TEST
esp_ieee802154_timer0_done();
#endif
}

static void isr_handle_timer1_done(void)
{
// timer 1 is now unused.
#if CONFIG_IEEE802154_TEST
esp_ieee802154_timer1_done();
#endif
}

static IRAM_ATTR void isr_handle_tx_done(void)
Expand Down Expand Up @@ -302,12 +308,14 @@ static IRAM_ATTR void isr_handle_rx_done(void)
s_rx_frame_info[s_rx_index].pending = ieee802154_ack_config_pending_bit(s_rx_frame[s_rx_index]);
// For 2015 enh-ack, SW should generate an enh-ack then send it manually
if (esp_ieee802154_enh_ack_generator(s_rx_frame[s_rx_index], &s_rx_frame_info[s_rx_index], s_enh_ack_frame) == ESP_OK) {
#if !CONFIG_IEEE802154_TEST
// Send the Enh-Ack frame if generator succeeds.
ieee802154_ll_set_tx_addr(s_enh_ack_frame);
s_tx_frame = s_enh_ack_frame;
ieee802154_sec_update();
ieee802154_ll_enhack_generate_done_notify();
s_ieee802154_state = IEEE802154_STATE_TX_ENH_ACK;
#endif
} else {
// Stop current process if generator returns errors.
ieee802154_ll_set_cmd(IEEE802154_CMD_STOP);
Expand Down Expand Up @@ -355,6 +363,10 @@ static IRAM_ATTR void isr_handle_rx_abort(void)
case IEEE802154_RX_ABORT_BY_UNEXPECTED_ACK:
case IEEE802154_RX_ABORT_BY_RX_RESTART:
assert(s_ieee802154_state == IEEE802154_STATE_RX);
#if CONFIG_IEEE802154_TEST
esp_ieee802154_receive_failed(rx_status);
next_operation();
#endif
break;
case IEEE802154_RX_ABORT_BY_COEX_BREAK:
assert(s_ieee802154_state == IEEE802154_STATE_RX);
Expand All @@ -369,13 +381,21 @@ static IRAM_ATTR void isr_handle_rx_abort(void)
case IEEE802154_RX_ABORT_BY_TX_ACK_TIMEOUT:
case IEEE802154_RX_ABORT_BY_TX_ACK_COEX_BREAK:
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]);
next_operation();
#else
esp_ieee802154_receive_failed(rx_status);
#endif
break;
case IEEE802154_RX_ABORT_BY_ENHACK_SECURITY_ERROR:
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]);
next_operation();
#else
esp_ieee802154_receive_failed(rx_status);
#endif
break;
default:
assert(false);
Expand Down Expand Up @@ -525,7 +545,12 @@ static void ieee802154_isr(void *arg)
}

if (events & IEEE802154_EVENT_TIMER0_OVERFLOW) {
#if !CONFIG_IEEE802154_TEST
assert(s_ieee802154_state == IEEE802154_STATE_RX_ACK);
#else
extern bool ieee802154_timer0_test;
assert(ieee802154_timer0_test || s_ieee802154_state == IEEE802154_STATE_RX_ACK);
#endif
isr_handle_timer0_done();

events &= (uint16_t)(~IEEE802154_EVENT_TIMER0_OVERFLOW);
Expand All @@ -542,12 +567,12 @@ static void ieee802154_isr(void *arg)

}

static IRAM_ATTR void ieee802154_enter_critical(void)
IEEE802154_STATIC IRAM_ATTR void ieee802154_enter_critical(void)
{
portENTER_CRITICAL(&s_ieee802154_spinlock);
}

static IRAM_ATTR void ieee802154_exit_critical(void)
IEEE802154_STATIC IRAM_ATTR void ieee802154_exit_critical(void)
{
portEXIT_CRITICAL(&s_ieee802154_spinlock);
}
Expand All @@ -565,11 +590,13 @@ void ieee802154_disable(void)
esp_err_t ieee802154_mac_init(void)
{
esp_err_t ret = ESP_OK;
modem_clock_module_mac_reset(PERIPH_IEEE802154_MODULE); // reset ieee802154 MAC
ieee802154_pib_init();

ieee802154_ll_enable_events(IEEE802154_EVENT_MASK);
#if !CONFIG_IEEE802154_TEST
ieee802154_ll_disable_events((IEEE802154_EVENT_TIMER0_OVERFLOW) | (IEEE802154_EVENT_TIMER1_OVERFLOW));

#endif
ieee802154_ll_enable_tx_abort_events(BIT(IEEE802154_TX_ABORT_BY_RX_ACK_TIMEOUT - 1) | BIT(IEEE802154_TX_ABORT_BY_TX_COEX_BREAK - 1) | BIT(IEEE802154_TX_ABORT_BY_TX_SECURITY_ERROR - 1) | BIT(IEEE802154_TX_ABORT_BY_CCA_FAILED - 1) | BIT(IEEE802154_TX_ABORT_BY_CCA_BUSY - 1));
ieee802154_ll_enable_rx_abort_events(BIT(IEEE802154_RX_ABORT_BY_TX_ACK_TIMEOUT - 1) | BIT(IEEE802154_RX_ABORT_BY_TX_ACK_COEX_BREAK - 1));

Expand Down Expand Up @@ -598,14 +625,14 @@ esp_err_t ieee802154_mac_init(void)
return ret;
}

static void start_ed(uint32_t duration)
IEEE802154_STATIC void start_ed(uint32_t duration)
{
ieee802154_ll_enable_events(IEEE802154_EVENT_ED_DONE);
ieee802154_ll_set_ed_duration(duration);
ieee802154_ll_set_cmd(IEEE802154_CMD_ED_START);
}

static void tx_init(const uint8_t *frame)
IEEE802154_STATIC void tx_init(const uint8_t *frame)
{
s_tx_frame = (uint8_t *)frame;
stop_current_operation();
Expand Down Expand Up @@ -678,7 +705,7 @@ esp_err_t ieee802154_transmit_at(const uint8_t *frame, bool cca, uint32_t time)
return ESP_OK;
}

static void rx_init(void)
IEEE802154_STATIC void rx_init(void)
{
stop_current_operation();
ieee802154_pib_update();
Expand Down
40 changes: 20 additions & 20 deletions components/ieee802154/driver/esp_ieee802154_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@

static const char *TAG = "ieee802154 frame";

static inline bool is_security_enabled(const uint8_t *frame)
IEEE802154_STATIC IEEE802154_INLINE bool is_security_enabled(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_SECURITY_OFFSET] & IEEE802154_FRAME_SECURITY_BIT;
}

static inline bool is_ie_present(const uint8_t *frame)
IEEE802154_STATIC IEEE802154_INLINE bool is_ie_present(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_IE_OFFSET] & IEEE802154_FRAME_IE_BIT;
}

static inline bool is_dsn_present(const uint8_t *frame)
IEEE802154_STATIC IEEE802154_INLINE bool is_dsn_present(const uint8_t *frame)
{
return ((ieee802154_frame_get_version(frame) != IEEE802154_FRAME_VERSION_2) ||
!(frame[IEEE802154_FRAME_DSN_OFFSET] & IEEE802154_FRAME_DSN_BIT));
}

static inline uint8_t dst_addr_mode(const uint8_t *frame)
IEEE802154_STATIC IEEE802154_INLINE uint8_t dst_addr_mode(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_DST_MODE_OFFSET] & IEEE802154_FRAME_DST_MODE_MASK;
}

static inline uint8_t src_addr_mode(const uint8_t *frame)
IEEE802154_STATIC IEEE802154_INLINE uint8_t src_addr_mode(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_SRC_MODE_OFFSET] & IEEE802154_FRAME_SRC_MODE_MASK;
}

static inline bool is_panid_compression(const uint8_t *frame)
IEEE802154_STATIC IEEE802154_INLINE bool is_panid_compression(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_PANID_COMP_OFFSET] & IEEE802154_FRAME_PANID_COMP_BIT;
}

static inline bool is_suported_frame_type(uint8_t frame_type)
IEEE802154_STATIC IEEE802154_INLINE bool is_suported_frame_type(uint8_t frame_type)
{
// HW supports 4 kinds of frame type: Beacon, Ack, Data, Command.
return (frame_type == IEEE802154_FRAME_TYPE_BEACON || frame_type == IEEE802154_FRAME_TYPE_DATA ||
frame_type == IEEE802154_FRAME_TYPE_ACK || frame_type == IEEE802154_FRAME_TYPE_COMMAND);
}

static bool is_dst_panid_present(const uint8_t *frame)
IEEE802154_STATIC bool is_dst_panid_present(const uint8_t *frame)
{
uint8_t dst_mode = dst_addr_mode(frame);
bool dst_panid_present = false;
Expand Down Expand Up @@ -80,7 +80,7 @@ static bool is_dst_panid_present(const uint8_t *frame)
return dst_panid_present;
}

static bool is_src_panid_present(const uint8_t *frame)
IEEE802154_STATIC bool is_src_panid_present(const uint8_t *frame)
{
uint8_t src_mode = src_addr_mode(frame);
bool panid_compression = is_panid_compression(frame);
Expand Down Expand Up @@ -109,12 +109,12 @@ static bool is_src_panid_present(const uint8_t *frame)
return src_panid_present;
}

static uint8_t inline ieee802154_frame_address_offset(const uint8_t *frame)
IEEE802154_STATIC uint8_t IEEE802154_INLINE ieee802154_frame_address_offset(const uint8_t *frame)
{
return IEEE802154_FRAME_PHR_SIZE + IEEE802154_FRAME_FCF_SIZE + (is_dsn_present(frame) ? IEEE802154_FRAME_DSN_SIZE : 0);
}

static IRAM_ATTR uint8_t ieee802154_frame_address_size(const uint8_t *frame)
IEEE802154_STATIC IRAM_ATTR uint8_t ieee802154_frame_address_size(const uint8_t *frame)
{
uint8_t address_size = 0;

Expand Down Expand Up @@ -161,7 +161,7 @@ static IRAM_ATTR uint8_t ieee802154_frame_address_size(const uint8_t *frame)
return address_size;
}

static uint8_t ieee802154_frame_security_header_offset(const uint8_t *frame)
IEEE802154_STATIC uint8_t ieee802154_frame_security_header_offset(const uint8_t *frame)
{
ESP_RETURN_ON_FALSE_ISR(is_suported_frame_type(ieee802154_frame_get_type(frame)), IEEE802154_FRAME_INVALID_ADDR_MODE, TAG, "invalid frame type");
uint8_t offset = ieee802154_frame_address_offset(frame);
Expand All @@ -175,7 +175,7 @@ static uint8_t ieee802154_frame_security_header_offset(const uint8_t *frame)
return offset;
}

static uint8_t ieee802154_frame_get_security_field_len(const uint8_t *frame)
IEEE802154_STATIC uint8_t ieee802154_frame_get_security_field_len(const uint8_t *frame)
{
ESP_RETURN_ON_FALSE_ISR(is_suported_frame_type(ieee802154_frame_get_type(frame)), IEEE802154_FRAME_INVALID_OFFSET, TAG, "invalid frame type");

Expand Down Expand Up @@ -211,7 +211,7 @@ static uint8_t ieee802154_frame_get_security_field_len(const uint8_t *frame)
return security_field_len;
}

static uint8_t ieee802154_frame_ie_header_offset(const uint8_t *frame)
IEEE802154_STATIC uint8_t ieee802154_frame_ie_header_offset(const uint8_t *frame)
{
uint8_t offset = ieee802154_frame_security_header_offset(frame);
uint8_t security_field_len = ieee802154_frame_get_security_field_len(frame);
Expand All @@ -221,7 +221,7 @@ static uint8_t ieee802154_frame_ie_header_offset(const uint8_t *frame)
return offset;
}

static uint8_t ieee802154_frame_get_mic_len(const uint8_t *frame)
IEEE802154_STATIC uint8_t ieee802154_frame_get_mic_len(const uint8_t *frame)
{
uint8_t offset = ieee802154_frame_security_header_offset(frame);
uint8_t mic_len = 0;
Expand All @@ -247,7 +247,7 @@ static uint8_t ieee802154_frame_get_mic_len(const uint8_t *frame)
return mic_len;
}

static uint8_t ieee802154_frame_get_ie_field_len(const uint8_t *frame)
IEEE802154_STATIC uint8_t ieee802154_frame_get_ie_field_len(const uint8_t *frame)
{
uint8_t offset = ieee802154_frame_ie_header_offset(frame);
uint8_t ie_field_len = 0;
Expand All @@ -274,7 +274,7 @@ static uint8_t ieee802154_frame_get_ie_field_len(const uint8_t *frame)
return ie_field_len;
}

static IRAM_ATTR uint8_t ieee802154_frame_payload_offset(const uint8_t *frame)
IEEE802154_STATIC IRAM_ATTR uint8_t ieee802154_frame_payload_offset(const uint8_t *frame)
{
uint8_t offset = ieee802154_frame_security_header_offset(frame);
if (is_security_enabled(frame)) {
Expand All @@ -298,17 +298,17 @@ static IRAM_ATTR uint8_t ieee802154_frame_payload_offset(const uint8_t *frame)
return offset - 1;
}

uint8_t inline ieee802154_frame_get_type(const uint8_t *frame)
uint8_t IEEE802154_INLINE ieee802154_frame_get_type(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_TYPE_OFFSET] & IEEE802154_FRAME_TYPE_MASK;
}

uint8_t inline ieee802154_frame_get_version(const uint8_t *frame)
uint8_t IEEE802154_INLINE ieee802154_frame_get_version(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_VERSION_OFFSET] & IEEE802154_FRAME_VERSION_MASK;
}

bool inline ieee802154_frame_is_ack_required(const uint8_t *frame)
bool IEEE802154_INLINE ieee802154_frame_is_ack_required(const uint8_t *frame)
{
return frame[IEEE802154_FRAME_AR_OFFSET] & IEEE802154_FRAME_AR_BIT;
}
Expand Down
14 changes: 7 additions & 7 deletions components/ieee802154/driver/esp_ieee802154_pib.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ void ieee802154_pib_init(void)

static uint8_t ieee802154_txpower_convert(int8_t txpower)
{
uint8_t ieee820154_txpower_value = 0;
if (txpower > IEEE802154_TXPOWER_VALUE_MAX) {
ieee820154_txpower_value = 15;
} else if (txpower < IEEE802154_TXPOWER_VALUE_MIN) {
ieee820154_txpower_value = 0;
uint8_t ieee820154_txpower_index = 0;
if (txpower >= IEEE802154_TXPOWER_VALUE_MAX) {
ieee820154_txpower_index = 15;
} else if (txpower <= IEEE802154_TXPOWER_VALUE_MIN) {
ieee820154_txpower_index = IEEE802154_TXPOWER_INDEX_MIN;
} else {
ieee820154_txpower_value = (uint8_t)((txpower - IEEE802154_TXPOWER_VALUE_MIN) / 3);
ieee820154_txpower_index = (uint8_t)((txpower - IEEE802154_TXPOWER_VALUE_MIN) / 3) + IEEE802154_TXPOWER_INDEX_MIN;
}
return ieee820154_txpower_value;
return ieee820154_txpower_index;
}

void ieee802154_pib_update(void)
Expand Down
11 changes: 10 additions & 1 deletion components/ieee802154/esp_ieee802154.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

esp_err_t esp_ieee802154_enable(void)
{

ieee802154_enable();
esp_phy_enable();
esp_btbb_enable();
Expand Down Expand Up @@ -381,3 +380,13 @@ __attribute__((weak)) esp_err_t esp_ieee802154_enh_ack_generator(uint8_t *frame,
{
return ESP_OK;
}

__attribute__((weak)) void esp_ieee802154_timer0_done(void)
{

}

__attribute__((weak)) void esp_ieee802154_timer1_done(void)
{

}
Loading

0 comments on commit 966312e

Please sign in to comment.