49#include "sys/int-master.h"
50#include "sys/critical.h"
75#define LOG_MODULE "nRF52840 IEEE"
76#define LOG_LEVEL LOG_LEVEL_ERR
78#define NRF52840_CCA_BUSY 0
79#define NRF52840_CCA_CLEAR 1
81#define NRF52840_RECEIVING_NO 0
82#define NRF52840_RECEIVING_YES 1
84#define NRF52840_PENDING_NO 0
85#define NRF52840_PENDING_YES 1
87#define NRF52840_COMMAND_ERR 0
88#define NRF52840_COMMAND_OK 1
90#define NRF52840_CHANNEL_MIN 11
91#define NRF52840_CHANNEL_MAX 26
102#define MAX_PAYLOAD_LEN (MPDU_LEN - FCS_LEN)
104#define ACK_MPDU_MIN_LEN 5
105#define ACK_PAYLOAD_MIN_LEN (ACK_MPDU_MIN_LEN - FCS_LEN)
116static int8_t last_rssi;
117static uint8_t last_lqi;
119PROCESS(nrf52840_ieee_rf_process,
"nRF52840 IEEE RF driver");
121#ifndef NRF52840_CCA_MODE
122#define NRF52840_CCA_MODE RADIO_CCACTRL_CCAMODE_CarrierAndEdMode
125#ifndef NRF52840_CCA_ED_THRESHOLD
126#define NRF52840_CCA_ED_THRESHOLD 0x14
129#ifndef NRF52840_CCA_CORR_THRESHOLD
130#define NRF52840_CCA_CORR_THRESHOLD 0x14
133#ifndef NRF52840_CCA_CORR_COUNT
134#define NRF52840_CCA_CORR_COUNT 0x02
145#define CRC_IEEE802154_LEN 2
146#define CRC_IEEE802154_POLY 0x11021
147#define CRC_IEEE802154_INIT 0
149#define SYMBOL_DURATION_USEC 16
150#define SYMBOL_DURATION_RTIMER 1
151#define BYTE_DURATION_RTIMER (SYMBOL_DURATION_RTIMER * 2)
152#define TXRU_DURATION_TIMER 3
154typedef struct timestamps_s {
156 rtimer_clock_t framestart;
158 rtimer_clock_t mpdu_duration;
162static volatile timestamps_t timestamps;
164typedef struct tx_buf_s {
166 uint8_t mpdu[MAX_PAYLOAD_LEN];
169static tx_buf_t tx_buf;
171typedef struct rx_buf_s {
173 uint8_t mpdu[MPDU_LEN];
177static rx_buf_t rx_buf;
179typedef struct rf_cfg_s {
181 nrf_radio_txpower_t txpower;
185 uint8_t cca_corr_threshold;
186 uint8_t cca_corr_count;
187 uint8_t ed_threshold;
190static volatile rf_cfg_t rf_config = {
192 .txpower = NRF_RADIO_TXPOWER_0DBM,
195 .cca_mode = NRF52840_CCA_MODE,
196 .cca_corr_threshold = NRF52840_CCA_CORR_THRESHOLD,
197 .cca_corr_count = NRF52840_CCA_CORR_COUNT,
198 .ed_threshold = NRF52840_CCA_ED_THRESHOLD,
202phr_is_valid(uint8_t phr)
204 if(phr < ACK_MPDU_MIN_LEN || phr > MPDU_LEN) {
211radio_is_powered(
void)
213 return NRF_RADIO->POWER == 0 ? false :
true;
219 return NRF_RADIO->FREQUENCY / 5 + 10;
225 NRF_RADIO->FREQUENCY = 5 * (channel - 10);
233 ccactrl = rf_config.cca_mode;
234 ccactrl |= rf_config.ed_threshold << RADIO_CCACTRL_CCAEDTHRES_Pos;
235 ccactrl |= rf_config.cca_corr_count << RADIO_CCACTRL_CCACORRCNT_Pos;
236 ccactrl |= rf_config.cca_corr_threshold << RADIO_CCACTRL_CCACORRTHRES_Pos;
238 NRF_RADIO->CCACTRL = ccactrl;
250 nrf_radio_crc_configure(CRC_IEEE802154_LEN, NRF_RADIO_CRC_ADDR_IEEE802154,
251 CRC_IEEE802154_POLY);
253 nrf_radio_crcinit_set(CRC_IEEE802154_INIT);
260 nrf_radio_packet_conf_t conf;
262 memset(&conf, 0,
sizeof(conf));
266 conf.plen = NRF_RADIO_PREAMBLE_LENGTH_32BIT_ZERO;
268 conf.big_endian =
false;
269 conf.whiteen =
false;
270 conf.maxlen = MPDU_LEN;
272 nrf_radio_packet_configure(&conf);
276setup_interrupts(
void)
279 nrf_radio_int_mask_t interrupts = 0;
283 if(!rf_config.poll_mode) {
284 nrf_radio_event_clear(NRF_RADIO_EVENT_CRCOK);
285 nrf_radio_event_clear(NRF_RADIO_EVENT_CRCERROR);
286 interrupts |= NRF_RADIO_INT_CRCOK_MASK | NRF_RADIO_INT_CRCERROR_MASK;
290 nrf_radio_int_disable(0xFFFFFFFF);
291 NVIC_ClearPendingIRQ(RADIO_IRQn);
294 nrf_radio_int_enable(interrupts);
295 NVIC_EnableIRQ(RADIO_IRQn);
298 NVIC_DisableIRQ(RADIO_IRQn);
310setup_ppi_timestamping(
void)
312 nrf_ppi_channel_endpoint_setup(
314 (uint32_t)nrf_radio_event_address_get(NRF_RADIO_EVENT_FRAMESTART),
315 (uint32_t)nrf_timer_task_address_get(NRF_TIMER0, NRF_TIMER_TASK_CAPTURE3));
316 nrf_ppi_channel_enable(NRF_PPI_CHANNEL0);
317 nrf_ppi_channel_enable(NRF_PPI_CHANNEL27);
321set_poll_mode(
bool enable)
323 rf_config.poll_mode = enable;
330 memset(&rx_buf, 0,
sizeof(rx_buf));
336 nrf_radio_event_clear(NRF_RADIO_EVENT_FRAMESTART);
337 nrf_radio_event_clear(NRF_RADIO_EVENT_END);
338 nrf_radio_event_clear(NRF_RADIO_EVENT_CRCERROR);
339 nrf_radio_event_clear(NRF_RADIO_EVENT_CRCOK);
350 nrf_radio_mode_set(NRF_RADIO_MODE_IEEE802154_250KBIT);
366 nrf_radio_modecnf0_set(
true, RADIO_MODECNF0_DTX_Center);
370power_on_and_configure(
void)
372 nrf_radio_power_set(
true);
390 nrf_radio_state_t curr_state = nrf_radio_state_get();
392 LOG_DBG(
"Enter RX, state=%u", curr_state);
395 if(curr_state == NRF_RADIO_STATE_RX) {
396 LOG_DBG_(
". Was in RX");
402 nrf_radio_packetptr_set(&rx_buf);
405 setup_ppi_timestamping();
410 nrf_radio_shorts_enable(NRF_RADIO_SHORT_ADDRESS_RSSISTART_MASK);
411 nrf_radio_shorts_enable(NRF_RADIO_SHORT_RXREADY_START_MASK);
413 if(curr_state != NRF_RADIO_STATE_RXIDLE) {
415 nrf_radio_event_clear(NRF_RADIO_EVENT_RXREADY);
416 nrf_radio_task_trigger(NRF_RADIO_TASK_RXEN);
419 nrf_radio_task_trigger(NRF_RADIO_TASK_START);
422 LOG_DBG_(
"--->%u\n", nrf_radio_state_get());
424 LOG_DBG(
"PACKETPTR=0x%08lx (rx_buf @ 0x%08lx)\n",
425 (uint32_t)nrf_radio_packetptr_get(), (uint32_t)&rx_buf);
434 nrf_radio_task_trigger(NRF_RADIO_TASK_RSSISTART);
436 while(nrf_radio_event_check(NRF_RADIO_EVENT_RSSIEND) ==
false);
437 nrf_radio_event_clear(NRF_RADIO_EVENT_RSSIEND);
439 rssi_sample = nrf_radio_rssi_sample_get();
441 return -((int8_t)rssi_sample);
449lqi_convert_to_802154_scale(uint8_t lqi_hw)
451 return (uint8_t)lqi_hw > 63 ? 255 : lqi_hw * ED_RSSISCALE;
461 if(radio_is_powered() ==
false) {
462 LOG_DBG(
"Not powered\n");
463 power_on_and_configure();
468 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
469 return NRF52840_COMMAND_OK;
477 LOG_DBG(
"channel_clear\n");
482 nrf_radio_event_clear(NRF_RADIO_EVENT_CCABUSY);
483 nrf_radio_event_clear(NRF_RADIO_EVENT_CCAIDLE);
484 nrf_radio_event_clear(NRF_RADIO_EVENT_CCASTOPPED);
486 LOG_DBG(
"channel_clear: CCACTRL=0x%08lx\n", NRF_RADIO->CCACTRL);
489 nrf_radio_task_trigger(NRF_RADIO_TASK_CCASTART);
491 while((nrf_radio_event_check(NRF_RADIO_EVENT_CCABUSY) ==
false) &&
492 (nrf_radio_event_check(NRF_RADIO_EVENT_CCAIDLE) ==
false));
494 busy = nrf_radio_event_check(NRF_RADIO_EVENT_CCABUSY);
495 idle = nrf_radio_event_check(NRF_RADIO_EVENT_CCAIDLE);
497 LOG_DBG(
"channel_clear: I=%u, B=%u\n", idle, busy);
500 return NRF52840_CCA_BUSY;
503 return NRF52840_CCA_CLEAR;
515 timestamps.framestart = 0;
517 timestamps.mpdu_duration = 0;
521 nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
522 nrf_clock_task_trigger(NRF_CLOCK_TASK_HFCLKSTART);
523 while(!nrf_clock_event_check(NRF_CLOCK_EVENT_HFCLKSTARTED));
524 nrf_clock_event_clear(NRF_CLOCK_EVENT_HFCLKSTARTED);
533 power_on_and_configure();
536 set_poll_mode(rf_config.poll_mode);
542prepare(
const void *payload,
unsigned short payload_len)
544 LOG_DBG(
"Prepare %u bytes\n", payload_len);
546 if(payload_len > MAX_PAYLOAD_LEN) {
547 LOG_ERR(
"Too long: %u bytes, max %u\n", payload_len, MAX_PAYLOAD_LEN);
552 tx_buf.phr = (uint8_t)payload_len + FCS_LEN;
555 memcpy(tx_buf.mpdu, payload, payload_len);
561transmit(
unsigned short transmit_len)
565 LOG_DBG(
"TX %u bytes + FCS, channel=%u\n", transmit_len,
get_channel());
567 if(transmit_len > MAX_PAYLOAD_LEN) {
568 LOG_ERR(
"TX: too long (%u bytes)\n", transmit_len);
574 if(rf_config.send_on_cca) {
575 if(channel_clear() == NRF52840_CCA_BUSY) {
576 LOG_DBG(
"TX: Busy\n");
581 nrf_radio_txpower_set(rf_config.txpower);
584 nrf_radio_task_trigger(NRF_RADIO_TASK_STOP);
585 while(nrf_radio_state_get() != NRF_RADIO_STATE_RXIDLE);
587 LOG_DBG(
"Transmit: %u bytes=000000", tx_buf.phr);
588 for(i = 0; i < tx_buf.phr - 2; i++) {
589 LOG_DBG_(
" %02x", tx_buf.mpdu[i]);
593 LOG_DBG(
"TX Start. State %u", nrf_radio_state_get());
596 nrf_radio_packetptr_set(&tx_buf);
599 nrf_radio_event_clear(NRF_RADIO_EVENT_END);
600 nrf_radio_event_clear(NRF_RADIO_EVENT_PHYEND);
601 nrf_radio_event_clear(NRF_RADIO_EVENT_TXREADY);
604 nrf_radio_shorts_disable(NRF_RADIO_SHORT_ADDRESS_RSSISTART_MASK);
607 ENERGEST_SWITCH(ENERGEST_TYPE_LISTEN, ENERGEST_TYPE_TRANSMIT);
610 nrf_radio_shorts_enable(NRF_RADIO_SHORT_TXREADY_START_MASK);
611 nrf_radio_task_trigger(NRF_RADIO_TASK_TXEN);
620 LOG_DBG_(
"--->%u\n", nrf_radio_state_get());
623 while(nrf_radio_state_get() == NRF_RADIO_STATE_TX);
625 LOG_DBG(
"TX: Done\n");
636 ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
642send(
const void *payload,
unsigned short payload_len)
644 prepare(payload, payload_len);
645 return transmit(payload_len);
649read_frame(
void *buf,
unsigned short bufsize)
656 payload_len = rx_buf.phr - FCS_LEN;
658 if(phr_is_valid(rx_buf.phr) ==
false) {
659 LOG_DBG(
"Incorrect length: %d\n", payload_len);
665 memcpy(buf, rx_buf.mpdu, payload_len);
666 last_lqi = lqi_convert_to_802154_scale(rx_buf.mpdu[payload_len]);
667 last_rssi = -(nrf_radio_rssi_sample_get());
669 packetbuf_set_attr(PACKETBUF_ATTR_RSSI, last_rssi);
670 packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, last_lqi);
673 timestamps.phr = rx_buf.phr;
674 timestamps.framestart = nrf_timer_cc_read(NRF_TIMER0, NRF_TIMER_CC_CHANNEL3);
675 timestamps.end = nrf_timer_cc_read(NRF_TIMER0, NRF_TIMER_CC_CHANNEL2);
676 timestamps.mpdu_duration = rx_buf.phr * BYTE_DURATION_RTIMER;
683 timestamps.sfd = timestamps.framestart - BYTE_DURATION_RTIMER;
685 LOG_DBG(
"Read frame: len=%d, RSSI=%d, LQI=0x%02x\n", payload_len, last_rssi,
695receiving_packet(
void)
698 if(radio_is_powered() ==
false) {
699 return NRF52840_RECEIVING_NO;
703 if(nrf_radio_state_get() != NRF_RADIO_STATE_RX) {
704 return NRF52840_RECEIVING_NO;
707 if(rf_config.poll_mode) {
709 if(phr_is_valid(rx_buf.phr) ==
false) {
710 return NRF52840_RECEIVING_NO;
717 if((nrf_radio_event_check(NRF_RADIO_EVENT_CRCOK) ==
false) &&
718 (nrf_radio_event_check(NRF_RADIO_EVENT_CRCERROR) ==
false)) {
719 return NRF52840_RECEIVING_YES;
722 return NRF52840_RECEIVING_NO;
729 if(phr_is_valid(rx_buf.phr) ==
true && rx_buf.full ==
false) {
730 return NRF52840_RECEIVING_YES;
732 return NRF52840_RECEIVING_NO;
742 if(phr_is_valid(rx_buf.phr) ==
false) {
743 return NRF52840_PENDING_NO;
753 if((nrf_radio_event_check(NRF_RADIO_EVENT_CRCOK) ==
true) ||
754 (rx_buf.full ==
true)) {
755 return NRF52840_PENDING_YES;
758 return NRF52840_PENDING_NO;
765 nrf_radio_task_trigger(NRF_RADIO_TASK_DISABLE);
768 while(nrf_radio_state_get() != NRF_RADIO_STATE_DISABLED);
771 nrf_radio_power_set(
false);
773 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
775 return NRF52840_COMMAND_OK;
793 if(rf_config.poll_mode) {
799 if(rf_config.send_on_cca) {
845 case RADIO_CONST_MAX_PAYLOAD_LEN:
870 if(value < NRF52840_CHANNEL_MIN ||
871 value > NRF52840_CHANNEL_MAX) {
874 rf_config.channel = value;
877 if(radio_is_powered()) {
901 rf_config.txpower = value;
903 if(radio_is_powered()) {
904 nrf_radio_txpower_set(value);
908 rf_config.cca_corr_threshold = value;
910 if(radio_is_powered()) {
924get_object(radio_param_t param,
void *dest,
size_t size)
927 if(size !=
sizeof(rtimer_clock_t) || !dest) {
930 *(rtimer_clock_t *)dest = timestamps.sfd;
934#if MAC_CONF_WITH_TSCH
935 if(param == RADIO_CONST_TSCH_TIMING) {
936 if(size !=
sizeof(uint16_t *) || !dest) {
950set_object(radio_param_t param,
const void *src,
size_t size)
989 NETSTACK_MAC.
input();
990 LOG_DBG(
"last frame (%u bytes) timestamps:\n", timestamps.phr);
991 LOG_DBG(
" SFD=%lu (Derived)\n", timestamps.sfd);
992 LOG_DBG(
" PHY=%lu (PPI)\n", timestamps.framestart);
993 LOG_DBG(
" MPDU=%lu (Duration)\n", timestamps.mpdu_duration);
994 LOG_DBG(
" END=%lu (PPI)\n", timestamps.end);
995 LOG_DBG(
" Expected=%lu + %u + %lu = %lu\n", timestamps.sfd,
996 BYTE_DURATION_RTIMER, timestamps.mpdu_duration,
997 timestamps.sfd + BYTE_DURATION_RTIMER + timestamps.mpdu_duration);
1006RADIO_IRQHandler(
void)
1008 if(!rf_config.poll_mode) {
1009 if(nrf_radio_event_check(NRF_RADIO_EVENT_CRCOK)) {
1010 nrf_radio_event_clear(NRF_RADIO_EVENT_CRCOK);
1013 }
else if(nrf_radio_event_check(NRF_RADIO_EVENT_CRCERROR)) {
1014 nrf_radio_event_clear(NRF_RADIO_EVENT_CRCERROR);
Header file for the energy estimation mechanism.
static uint8_t get_channel()
Get the current operating channel.
static void set_channel(uint8_t channel)
Set the current operating channel.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
void watchdog_periodic(void)
Writes the WDT clear sequence.
static void critical_exit(int_master_status_t status)
Exit a critical section and restore the master interrupt.
static int_master_status_t critical_enter()
Enter a critical section.
INT_MASTER_STATUS_DATATYPE int_master_status_t
Master interrupt state representation data type.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
void packetbuf_clear(void)
Clear and reset the packetbuf.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define PROCESS_END()
Define the end of a process.
void process_start(struct process *p, process_data_t data)
Start a process.
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
void process_poll(struct process *p)
Request a process to be polled.
#define RADIO_RX_MODE_ADDRESS_FILTER
Enable address-based frame filtering.
#define RADIO_RX_MODE_POLL_MODE
Enable/disable/get the state of radio driver poll mode operation.
#define RADIO_TX_MODE_SEND_ON_CCA
Radio TX mode control / retrieval.
enum radio_result_e radio_result_t
Radio return values when setting or getting radio parameters.
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio.
#define RADIO_RX_MODE_AUTOACK
Enable automatic transmission of ACK frames.
@ RADIO_RESULT_NOT_SUPPORTED
The parameter is not supported.
@ RADIO_RESULT_INVALID_VALUE
The value argument was incorrect.
@ RADIO_RESULT_OK
The parameter was set/read successfully.
@ RADIO_PARAM_POWER_MODE
When getting the value of this parameter, the radio driver should indicate whether the radio is on or...
@ RADIO_CONST_PHY_OVERHEAD
The physical layer header (PHR) + MAC layer footer (MFR) overhead in bytes.
@ RADIO_PARAM_RSSI
Received signal strength indicator in dBm.
@ RADIO_PARAM_LAST_PACKET_TIMESTAMP
Last packet timestamp, of type rtimer_clock_t.
@ RADIO_PARAM_LAST_RSSI
The RSSI value of the last received packet.
@ RADIO_CONST_BYTE_AIR_TIME
The air time of one byte in usec, e.g.
@ RADIO_PARAM_RX_MODE
Radio receiver mode determines if the radio has address filter (RADIO_RX_MODE_ADDRESS_FILTER) and aut...
@ RADIO_PARAM_CHANNEL
Channel used for radio communication.
@ RADIO_PARAM_SHR_SEARCH
For enabling and disabling the SHR search.
@ RADIO_PARAM_LAST_LINK_QUALITY
Link quality indicator of the last received packet.
@ RADIO_CONST_DELAY_BEFORE_RX
The delay in usec between turning on the radio and it being actually listening (able to hear a preamb...
@ RADIO_PARAM_TXPOWER
Transmission power in dBm.
@ RADIO_CONST_DELAY_BEFORE_TX
The delay in usec between a call to the radio API's transmit function and the end of SFD transmission...
@ RADIO_CONST_CHANNEL_MAX
The highest radio channel number.
@ RADIO_PARAM_PAN_ID
The personal area network identifier (PAN ID), which is used by the h/w frame filtering functionality...
@ RADIO_PARAM_CCA_THRESHOLD
Clear channel assessment threshold in dBm.
@ RADIO_CONST_TXPOWER_MIN
The minimum transmission power in dBm.
@ RADIO_CONST_CHANNEL_MIN
The lowest radio channel number.
@ RADIO_CONST_TXPOWER_MAX
The maximum transmission power in dBm.
@ RADIO_CONST_DELAY_BEFORE_DETECT
The delay in usec between the end of SFD reception for an incoming frame and the radio API starting t...
@ RADIO_PARAM_16BIT_ADDR
The short address (16 bits) for the radio, which is used by the h/w filter.
@ RADIO_PARAM_TX_MODE
Radio transmission mode determines if the radio has send on CCA (RADIO_TX_MODE_SEND_ON_CCA) enabled o...
@ RADIO_POWER_MODE_OFF
Radio powered off and in the lowest possible power consumption state.
@ RADIO_POWER_MODE_ON
Radio powered on and able to receive frames.
@ RADIO_TX_COLLISION
TX failed due to a collision.
@ RADIO_TX_ERR
An error occurred during transmission.
@ RADIO_TX_OK
TX was successful and where an ACK was requested one was received.
#define RTIMER_BUSYWAIT(duration)
Busy-wait for a fixed duration.
const tsch_timeslot_timing_usec tsch_timeslot_timing_us_10000
TSCH timing attributes and description.
Header file for the logging system.
#define IEEE802154_DEFAULT_CHANNEL
The default channel for IEEE 802.15.4 networks.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
Header file for the radio API.
void(* input)(void)
Callback for getting notified of incoming packet.
The structure of a Contiki-NG radio device driver.
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
int(* off)(void)
Turn the radio off.
int(* init)(void)
Initialise the radio hardware.
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
int(* on)(void)
Turn the radio on.
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
int(* pending_packet)(void)
Check if a packet has been received and is available in the radio driver's buffers.
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
int(* channel_clear)(void)
Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not.
Main API declarations for TSCH.