Skip to content

Commit 8a557b6

Browse files
ankunsrlubos
authored andcommitted
[nrf noup] drivers: ieee802154_nrf5: extract nrf5_rx_frame_process
Code processing received frame is extracted to the function `nrf5_rx_frame_process` from `nrf5_rx_thread`. Signed-off-by: Andrzej Kuros <[email protected]>
1 parent 36e0fa7 commit 8a557b6

File tree

1 file changed

+72
-64
lines changed

1 file changed

+72
-64
lines changed

drivers/ieee802154/ieee802154_nrf5.c

Lines changed: 72 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -122,94 +122,102 @@ static void nrf5_get_eui64(uint8_t *mac)
122122
memcpy(mac + index, &factoryAddress, sizeof(factoryAddress) - index);
123123
}
124124

125-
static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
125+
static void nrf5_rx_frame_process(struct nrf5_802154_data *nrf5_radio,
126+
struct nrf5_802154_rx_frame *rx_frame)
126127
{
127-
struct nrf5_802154_data *nrf5_radio = (struct nrf5_802154_data *)arg1;
128-
struct net_pkt *pkt;
129-
struct nrf5_802154_rx_frame *rx_frame;
128+
struct net_pkt *pkt = NULL;
130129
uint8_t pkt_len;
131130
uint8_t *psdu;
132131

133-
ARG_UNUSED(arg2);
134-
ARG_UNUSED(arg3);
135-
136-
while (1) {
137-
pkt = NULL;
138-
rx_frame = NULL;
139-
140-
LOG_DBG("Waiting for frame");
141-
142-
rx_frame = k_fifo_get(&nrf5_radio->rx_fifo, K_FOREVER);
143-
144-
__ASSERT_NO_MSG(rx_frame->psdu);
132+
__ASSERT_NO_MSG(rx_frame->psdu);
145133

146-
/* rx_mpdu contains length, psdu, fcs|lqi
147-
* The last 2 bytes contain LQI or FCS, depending if
148-
* automatic CRC handling is enabled or not, respectively.
149-
*/
150-
if (IS_ENABLED(CONFIG_IEEE802154_NRF5_FCS_IN_LENGTH)) {
151-
pkt_len = rx_frame->psdu[0];
152-
} else {
153-
pkt_len = rx_frame->psdu[0] - NRF5_FCS_LENGTH;
154-
}
134+
/* rx_mpdu contains length, psdu, fcs|lqi
135+
* The last 2 bytes contain LQI or FCS, depending if
136+
* automatic CRC handling is enabled or not, respectively.
137+
*/
138+
if (IS_ENABLED(CONFIG_IEEE802154_NRF5_FCS_IN_LENGTH)) {
139+
pkt_len = rx_frame->psdu[0];
140+
} else {
141+
pkt_len = rx_frame->psdu[0] - NRF5_FCS_LENGTH;
142+
}
155143

156144
#if defined(CONFIG_NET_BUF_DATA_SIZE)
157-
__ASSERT_NO_MSG(pkt_len <= CONFIG_NET_BUF_DATA_SIZE);
145+
__ASSERT_NO_MSG(pkt_len <= CONFIG_NET_BUF_DATA_SIZE);
158146
#endif
159147

160-
LOG_DBG("Frame received");
148+
LOG_DBG("Frame received");
161149

162-
/* Block the RX thread until net_pkt is available, so that we
163-
* don't drop already ACKed frame in case of temporary net_pkt
164-
* scarcity. The nRF 802154 radio driver will accumulate any
165-
* incoming frames until it runs out of internal buffers (and
166-
* thus stops acknowledging consecutive frames).
167-
*/
168-
pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, pkt_len,
169-
AF_UNSPEC, 0, K_FOREVER);
150+
/* Block the RX thread until net_pkt is available, so that we
151+
* don't drop already ACKed frame in case of temporary net_pkt
152+
* scarcity. The nRF 802154 radio driver will accumulate any
153+
* incoming frames until it runs out of internal buffers (and
154+
* thus stops acknowledging consecutive frames).
155+
*/
156+
pkt = net_pkt_rx_alloc_with_buffer(nrf5_radio->iface, pkt_len,
157+
AF_UNSPEC, 0, K_FOREVER);
170158

171-
if (net_pkt_write(pkt, rx_frame->psdu + 1, pkt_len)) {
172-
goto drop;
173-
}
159+
if (net_pkt_write(pkt, rx_frame->psdu + 1, pkt_len)) {
160+
goto drop;
161+
}
174162

175-
net_pkt_set_ieee802154_lqi(pkt, rx_frame->lqi);
176-
net_pkt_set_ieee802154_rssi(pkt, rx_frame->rssi);
177-
net_pkt_set_ieee802154_ack_fpb(pkt, rx_frame->ack_fpb);
163+
net_pkt_set_ieee802154_lqi(pkt, rx_frame->lqi);
164+
net_pkt_set_ieee802154_rssi(pkt, rx_frame->rssi);
165+
net_pkt_set_ieee802154_ack_fpb(pkt, rx_frame->ack_fpb);
178166

179167
#if defined(CONFIG_NET_PKT_TIMESTAMP)
180-
struct net_ptp_time timestamp = {
181-
.second = rx_frame->time / USEC_PER_SEC,
182-
.nanosecond =
183-
(rx_frame->time % USEC_PER_SEC) * NSEC_PER_USEC
184-
};
168+
struct net_ptp_time timestamp = {
169+
.second = rx_frame->time / USEC_PER_SEC,
170+
.nanosecond =
171+
(rx_frame->time % USEC_PER_SEC) * NSEC_PER_USEC
172+
};
185173

186-
net_pkt_set_timestamp(pkt, &timestamp);
174+
net_pkt_set_timestamp(pkt, &timestamp);
187175
#endif
188176

189-
LOG_DBG("Caught a packet (%u) (LQI: %u)",
190-
pkt_len, rx_frame->lqi);
177+
LOG_DBG("Caught a packet (%u) (LQI: %u)",
178+
pkt_len, rx_frame->lqi);
191179

192-
if (net_recv_data(nrf5_radio->iface, pkt) < 0) {
193-
LOG_ERR("Packet dropped by NET stack");
194-
goto drop;
195-
}
180+
if (net_recv_data(nrf5_radio->iface, pkt) < 0) {
181+
LOG_ERR("Packet dropped by NET stack");
182+
goto drop;
183+
}
196184

197-
psdu = rx_frame->psdu;
198-
rx_frame->psdu = NULL;
199-
nrf_802154_buffer_free_raw(psdu);
185+
psdu = rx_frame->psdu;
186+
rx_frame->psdu = NULL;
187+
nrf_802154_buffer_free_raw(psdu);
200188

201-
if (LOG_LEVEL >= LOG_LEVEL_DBG) {
202-
log_stack_usage(&nrf5_radio->rx_thread);
203-
}
189+
if (LOG_LEVEL >= LOG_LEVEL_DBG) {
190+
log_stack_usage(&nrf5_radio->rx_thread);
191+
}
204192

205-
continue;
193+
return;
206194

207195
drop:
208-
psdu = rx_frame->psdu;
209-
rx_frame->psdu = NULL;
210-
nrf_802154_buffer_free_raw(psdu);
196+
psdu = rx_frame->psdu;
197+
rx_frame->psdu = NULL;
198+
nrf_802154_buffer_free_raw(psdu);
199+
200+
net_pkt_unref(pkt);
201+
}
202+
203+
static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
204+
{
205+
struct nrf5_802154_data *nrf5_radio = (struct nrf5_802154_data *)arg1;
206+
struct nrf5_802154_rx_frame *rx_frame;
207+
208+
ARG_UNUSED(arg2);
209+
ARG_UNUSED(arg3);
210+
211+
while (1) {
212+
rx_frame = NULL;
213+
214+
LOG_DBG("Waiting for frame");
211215

212-
net_pkt_unref(pkt);
216+
rx_frame = k_fifo_get(&nrf5_radio->rx_fifo, K_FOREVER);
217+
218+
if (rx_frame != NULL) {
219+
nrf5_rx_frame_process(nrf5_radio, rx_frame);
220+
}
213221
}
214222
}
215223

0 commit comments

Comments
 (0)