2018-07-09 18:13:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/**
|
2018-07-23 12:35:37 +00:00
|
|
|
* \addtogroup cc13xx-cc26xx-rf-sched
|
2018-07-09 18:13:01 +00:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2018-07-23 12:35:37 +00:00
|
|
|
* Implementation of the CC13xx/CC26xx RF scheduler.
|
|
|
|
* \author
|
|
|
|
* Edvard Pettersen <e.pettersen@ti.com>
|
2018-07-09 18:13:01 +00:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "dev/watchdog.h"
|
|
|
|
#include "sys/cc.h"
|
2018-07-17 16:02:38 +00:00
|
|
|
#include "sys/etimer.h"
|
2018-07-09 18:13:01 +00:00
|
|
|
#include "sys/process.h"
|
|
|
|
#include "sys/energest.h"
|
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "net/packetbuf.h"
|
|
|
|
#include "net/mac/mac.h"
|
2018-07-20 15:20:25 +00:00
|
|
|
#include "lib/random.h"
|
2018-07-09 18:13:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include <ti/devices/DeviceFamily.h>
|
|
|
|
#include DeviceFamily_constructPath(driverlib/rf_common_cmd.h)
|
|
|
|
#include DeviceFamily_constructPath(driverlib/rf_mailbox.h)
|
|
|
|
|
|
|
|
#include <ti/drivers/rf/RF.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-19 14:44:39 +00:00
|
|
|
#include "rf/rf.h"
|
|
|
|
#include "rf/sched.h"
|
|
|
|
#include "rf/data-queue.h"
|
2018-07-24 15:24:06 +00:00
|
|
|
#include "rf/settings.h"
|
2018-07-09 18:13:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-24 15:24:06 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "sys/log.h"
|
|
|
|
#define LOG_MODULE "Radio"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_NONE
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Configuration parameters */
|
2018-07-09 18:13:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define CMD_FS_RETRIES 3
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
#define RF_EVENTS_CMD_DONE (RF_EventCmdDone | RF_EventLastCmdDone | \
|
2018-07-09 18:13:01 +00:00
|
|
|
RF_EventFGCmdDone | RF_EventLastFGCmdDone)
|
|
|
|
|
|
|
|
#define CMD_STATUS(cmd) (CC_ACCESS_NOW(RF_Op, cmd).status)
|
|
|
|
|
|
|
|
#define CMD_HANDLE_OK(handle) (((handle) != RF_ALLOC_ERROR) && \
|
|
|
|
((handle) != RF_SCHEDULE_CMD_ERROR))
|
|
|
|
|
|
|
|
#define EVENTS_CMD_DONE(events) (((events) & RF_EVENTS_CMD_DONE) != 0)
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-17 16:02:38 +00:00
|
|
|
/* Synth re-calibration every 3 minutes */
|
2018-07-25 14:59:28 +00:00
|
|
|
#define SYNTH_RECAL_INTERVAL (CLOCK_SECOND * 60 * 3)
|
2018-07-20 15:20:25 +00:00
|
|
|
/* Set re-calibration interval with a jitter of 10 seconds */
|
2018-07-25 14:59:28 +00:00
|
|
|
#define SYNTH_RECAL_JITTER (CLOCK_SECOND * 10)
|
2018-07-17 16:02:38 +00:00
|
|
|
|
|
|
|
static struct etimer synth_recal_timer;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-24 10:16:53 +00:00
|
|
|
static RF_Object rf_netstack;
|
2018-07-25 14:59:28 +00:00
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
#if RF_CONF_BLE_BEACON_ENABLE
|
2018-07-24 10:16:53 +00:00
|
|
|
static RF_Object rf_ble;
|
2018-07-25 14:59:28 +00:00
|
|
|
#endif
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-12 08:44:14 +00:00
|
|
|
static RF_CmdHandle cmd_rx_handle;
|
2018-07-13 12:34:08 +00:00
|
|
|
|
2018-07-24 10:16:53 +00:00
|
|
|
static bool rf_is_on;
|
2018-07-13 12:34:08 +00:00
|
|
|
static volatile bool rx_buf_full;
|
2018-07-12 08:44:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
cmd_rx_cb(RF_Handle client, RF_CmdHandle command, RF_EventMask events)
|
|
|
|
{
|
|
|
|
/* Unused arguments */
|
|
|
|
(void)client;
|
|
|
|
(void)command;
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(events & RF_EventRxEntryDone) {
|
2018-07-18 15:41:04 +00:00
|
|
|
process_poll(&rf_sched_process);
|
2018-07-12 08:44:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(events & RF_EventRxBufFull) {
|
2018-07-13 12:34:08 +00:00
|
|
|
rx_buf_full = true;
|
2018-07-18 15:41:04 +00:00
|
|
|
process_poll(&rf_sched_process);
|
2018-07-12 08:44:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 18:13:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-20 15:20:25 +00:00
|
|
|
static inline clock_time_t
|
|
|
|
synth_recal_interval(void)
|
|
|
|
{
|
|
|
|
/*
|
2018-07-23 07:37:23 +00:00
|
|
|
* Add jitter centered around SYNTH_RECAL_INTERVAL, giving a plus/minus
|
|
|
|
* jitter seconds halved.
|
2018-07-20 15:20:25 +00:00
|
|
|
*/
|
|
|
|
return SYNTH_RECAL_INTERVAL + (random_rand() % SYNTH_RECAL_JITTER) - (SYNTH_RECAL_JITTER / 2);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-09 18:13:01 +00:00
|
|
|
static inline bool
|
|
|
|
cmd_rx_is_active(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Active in this case means RX command is either running to be running,
|
|
|
|
* that is ACTIVE for running or PENDING for to be running.
|
|
|
|
*/
|
|
|
|
const uint16_t status = CMD_STATUS(netstack_cmd_rx);
|
|
|
|
return (status == ACTIVE) ||
|
|
|
|
(status == PENDING);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint_fast8_t
|
|
|
|
cmd_rx_disable(void)
|
|
|
|
{
|
|
|
|
const bool is_active = cmd_rx_is_active();
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(is_active) {
|
2018-07-09 18:13:01 +00:00
|
|
|
CMD_STATUS(netstack_cmd_rx) = DONE_STOPPED;
|
2018-07-12 08:44:14 +00:00
|
|
|
RF_cancelCmd(&rf_netstack, cmd_rx_handle, RF_ABORT_GRACEFULLY);
|
|
|
|
cmd_rx_handle = 0;
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (uint_fast8_t)is_active;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static rf_result_t
|
|
|
|
cmd_rx_restore(uint_fast8_t rx_key)
|
|
|
|
{
|
|
|
|
const bool was_active = (rx_key != 0) ? true : false;
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!was_active) {
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RF_ScheduleCmdParams sched_params;
|
|
|
|
RF_ScheduleCmdParams_init(&sched_params);
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
sched_params.priority = RF_PriorityNormal;
|
|
|
|
sched_params.endTime = 0;
|
2018-07-09 18:13:01 +00:00
|
|
|
sched_params.allowDelay = RF_AllowDelayAny;
|
|
|
|
|
|
|
|
CMD_STATUS(netstack_cmd_rx) = PENDING;
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
cmd_rx_handle = RF_scheduleCmd(
|
|
|
|
&rf_netstack,
|
|
|
|
(RF_Op *)&netstack_cmd_rx,
|
|
|
|
&sched_params,
|
|
|
|
cmd_rx_cb,
|
|
|
|
RF_EventRxEntryDone | RF_EventRxBufFull);
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!CMD_HANDLE_OK(cmd_rx_handle)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Unable to restore RX command, handle=%d status=0x%04x",
|
2018-07-25 16:16:27 +00:00
|
|
|
cmd_rx_handle, CMD_STATUS(netstack_cmd_rx));
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
rf_yield(void)
|
|
|
|
{
|
|
|
|
/* Force abort of any ongoing RF operation */
|
|
|
|
RF_flushCmd(&rf_netstack, RF_CMDHANDLE_FLUSH_ALL, RF_ABORT_GRACEFULLY);
|
2018-07-25 16:16:27 +00:00
|
|
|
#if RF_CONF_BLE_BEACON_ENABLE
|
|
|
|
RF_flushCmd(&rf_ble, RF_CMDHANDLE_FLUSH_ALL, RF_ABORT_GRACEFULLY);
|
2018-07-13 17:09:02 +00:00
|
|
|
#endif
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
/* Trigger a manual power-down */
|
|
|
|
RF_yield(&rf_netstack);
|
2018-07-25 16:16:27 +00:00
|
|
|
#if RF_CONF_BLE_BEACON_ENABLE
|
2018-07-09 18:13:01 +00:00
|
|
|
RF_yield(&rf_ble);
|
2018-07-13 17:09:02 +00:00
|
|
|
#endif
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
|
|
|
|
|
2018-07-17 16:02:38 +00:00
|
|
|
etimer_stop(&synth_recal_timer);
|
|
|
|
rf_is_on = false;
|
|
|
|
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
rf_set_tx_power(RF_Handle handle, RF_TxPowerTable_Entry *table, int8_t dbm)
|
|
|
|
{
|
2018-07-25 16:16:27 +00:00
|
|
|
const RF_Stat stat = RF_setTxPower(handle, RF_TxPowerTable_findValue(table, dbm));
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
return (stat == RF_StatSuccess)
|
2018-07-24 10:16:53 +00:00
|
|
|
? RF_RESULT_OK
|
|
|
|
: RF_RESULT_ERROR;
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
rf_get_tx_power(RF_Handle handle, RF_TxPowerTable_Entry *table, int8_t *dbm)
|
|
|
|
{
|
2018-07-25 16:16:27 +00:00
|
|
|
*dbm = RF_TxPowerTable_findPowerLevel(table, RF_getTxPower(handle));
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
return (*dbm != RF_TxPowerTable_INVALID_DBM)
|
2018-07-24 10:16:53 +00:00
|
|
|
? RF_RESULT_OK
|
|
|
|
: RF_RESULT_ERROR;
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
RF_Handle
|
|
|
|
netstack_open(RF_Params *params)
|
|
|
|
{
|
2018-07-25 16:16:27 +00:00
|
|
|
return RF_open(&rf_netstack, &netstack_mode, (RF_RadioSetup *)&netstack_cmd_radio_setup, params);
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
netstack_sched_fs(void)
|
|
|
|
{
|
|
|
|
const uint_fast8_t rx_key = cmd_rx_disable();
|
|
|
|
|
|
|
|
/*
|
2018-07-13 12:34:08 +00:00
|
|
|
* For IEEE-mode, restarting CMD_IEEE_RX re-calibrates the synth by using the
|
|
|
|
* channel field in the CMD_IEEE_RX command. It is assumed this field is
|
2018-07-23 12:35:37 +00:00
|
|
|
* already configured before this function is called. However, if
|
|
|
|
* CMD_IEEE_RX wasn't active, manually calibrate the synth with CMD_FS.
|
2018-07-09 18:13:01 +00:00
|
|
|
*
|
|
|
|
* For Prop-mode, the synth is always manually calibrated with CMD_FS.
|
|
|
|
*/
|
2018-07-20 15:20:25 +00:00
|
|
|
#if (RF_MODE == RF_CORE_MODE_2_4_GHZ)
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_key) {
|
2018-07-09 18:13:01 +00:00
|
|
|
cmd_rx_restore(rx_key);
|
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
2018-07-20 15:20:25 +00:00
|
|
|
#endif /* RF_MODE == RF_CORE_MODE_2_4_GHZ */
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
RF_EventMask events;
|
|
|
|
bool synth_error = false;
|
|
|
|
uint8_t num_tries = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
CMD_STATUS(netstack_cmd_fs) = PENDING;
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
events = RF_runCmd(
|
|
|
|
&rf_netstack,
|
|
|
|
(RF_Op *)&netstack_cmd_fs,
|
|
|
|
RF_PriorityNormal,
|
|
|
|
NULL,
|
|
|
|
0);
|
|
|
|
|
|
|
|
synth_error = (EVENTS_CMD_DONE(events)) && (CMD_STATUS(netstack_cmd_fs) == ERROR_SYNTH_PROG);
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
} while(synth_error && (num_tries++ < CMD_FS_RETRIES));
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
cmd_rx_restore(rx_key);
|
|
|
|
|
|
|
|
return (CMD_STATUS(netstack_cmd_fs) == DONE_OK)
|
2018-07-24 10:16:53 +00:00
|
|
|
? RF_RESULT_OK
|
|
|
|
: RF_RESULT_ERROR;
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
2018-07-18 15:41:04 +00:00
|
|
|
netstack_sched_ieee_tx(bool ack_request)
|
2018-07-09 18:13:01 +00:00
|
|
|
{
|
2018-07-12 08:44:14 +00:00
|
|
|
rf_result_t res;
|
|
|
|
|
2018-07-09 18:13:01 +00:00
|
|
|
RF_ScheduleCmdParams sched_params;
|
|
|
|
RF_ScheduleCmdParams_init(&sched_params);
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
sched_params.priority = RF_PriorityNormal;
|
|
|
|
sched_params.endTime = 0;
|
2018-07-09 18:13:01 +00:00
|
|
|
sched_params.allowDelay = RF_AllowDelayAny;
|
|
|
|
|
2018-07-18 15:41:04 +00:00
|
|
|
const bool rx_is_active = cmd_rx_is_active();
|
|
|
|
const bool rx_needed = (ack_request && !rx_is_active);
|
2018-07-12 08:44:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we expect ACK after transmission, RX must be running to be able to
|
|
|
|
* run the RX_ACK command. Therefore, turn on RX before starting the
|
|
|
|
* chained TX command.
|
|
|
|
*/
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_needed) {
|
2018-07-17 16:02:38 +00:00
|
|
|
res = netstack_sched_rx(false);
|
2018-07-23 12:35:37 +00:00
|
|
|
if(res != RF_RESULT_OK) {
|
2018-07-12 08:44:14 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:13:01 +00:00
|
|
|
CMD_STATUS(netstack_cmd_tx) = PENDING;
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
RF_CmdHandle tx_handle = RF_scheduleCmd(
|
|
|
|
&rf_netstack,
|
|
|
|
(RF_Op *)&netstack_cmd_tx,
|
|
|
|
&sched_params,
|
|
|
|
NULL,
|
|
|
|
0);
|
2018-07-12 08:44:14 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!CMD_HANDLE_OK(tx_handle)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Unable to schedule TX command, handle=%d status=0x%04x\n",
|
2018-07-25 16:16:27 +00:00
|
|
|
tx_handle, CMD_STATUS(netstack_cmd_tx));
|
2018-07-12 08:44:14 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_is_active) {
|
2018-07-12 08:44:14 +00:00
|
|
|
ENERGEST_SWITCH(ENERGEST_TYPE_LISTEN, ENERGEST_TYPE_TRANSMIT);
|
|
|
|
} else {
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until TX operation finishes */
|
|
|
|
RF_EventMask tx_events = RF_pendCmd(&rf_netstack, tx_handle, 0);
|
|
|
|
|
|
|
|
/* Stop RX if it was turned on only for ACK */
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_needed) {
|
2018-07-12 08:44:14 +00:00
|
|
|
netstack_stop_rx();
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_is_active) {
|
2018-07-12 08:44:14 +00:00
|
|
|
ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
|
|
|
|
} else {
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!EVENTS_CMD_DONE(tx_events)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Pending on TX comand generated error, events=0x%08llx status=0x%04x\n",
|
2018-07-25 16:16:27 +00:00
|
|
|
tx_events, CMD_STATUS(netstack_cmd_tx));
|
2018-07-12 08:44:14 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
netstack_sched_prop_tx(void)
|
|
|
|
{
|
|
|
|
RF_ScheduleCmdParams sched_params;
|
|
|
|
RF_ScheduleCmdParams_init(&sched_params);
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
sched_params.priority = RF_PriorityNormal;
|
|
|
|
sched_params.endTime = 0;
|
2018-07-12 08:44:14 +00:00
|
|
|
sched_params.allowDelay = RF_AllowDelayAny;
|
|
|
|
|
|
|
|
CMD_STATUS(netstack_cmd_tx) = PENDING;
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
RF_CmdHandle tx_handle = RF_scheduleCmd(
|
|
|
|
&rf_netstack,
|
|
|
|
(RF_Op *)&netstack_cmd_tx,
|
|
|
|
&sched_params,
|
|
|
|
NULL,
|
|
|
|
0);
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!CMD_HANDLE_OK(tx_handle)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Unable to schedule TX command, handle=%d status=0x%04x\n",
|
|
|
|
tx_handle, CMD_STATUS(netstack_cmd_tx));
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-07-12 08:44:14 +00:00
|
|
|
* Prop TX requires any on-going RX operation to be stopped to be
|
|
|
|
* able to transmit. Therefore, disable RX if running.
|
2018-07-09 18:13:01 +00:00
|
|
|
*/
|
2018-07-12 08:44:14 +00:00
|
|
|
const bool rx_key = cmd_rx_disable();
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_key) {
|
2018-07-09 18:13:01 +00:00
|
|
|
ENERGEST_SWITCH(ENERGEST_TYPE_LISTEN, ENERGEST_TYPE_TRANSMIT);
|
|
|
|
} else {
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until TX operation finishes */
|
|
|
|
RF_EventMask tx_events = RF_pendCmd(&rf_netstack, tx_handle, 0);
|
|
|
|
|
2018-07-12 08:44:14 +00:00
|
|
|
cmd_rx_restore(rx_key);
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_key) {
|
2018-07-09 18:13:01 +00:00
|
|
|
ENERGEST_SWITCH(ENERGEST_TYPE_TRANSMIT, ENERGEST_TYPE_LISTEN);
|
|
|
|
} else {
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
|
|
|
|
}
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!EVENTS_CMD_DONE(tx_events)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Pending on scheduled TX command generated error, events=0x%08llx status=0x%04x\n",
|
|
|
|
tx_events, CMD_STATUS(netstack_cmd_tx));
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
2018-07-17 16:02:38 +00:00
|
|
|
netstack_sched_rx(bool start)
|
2018-07-09 18:13:01 +00:00
|
|
|
{
|
2018-07-23 12:35:37 +00:00
|
|
|
if(cmd_rx_is_active()) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_WARN("Already in RX when scheduling RX\n");
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RF_ScheduleCmdParams sched_params;
|
|
|
|
RF_ScheduleCmdParams_init(&sched_params);
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
sched_params.priority = RF_PriorityNormal;
|
|
|
|
sched_params.endTime = 0;
|
2018-07-09 18:13:01 +00:00
|
|
|
sched_params.allowDelay = RF_AllowDelayAny;
|
|
|
|
|
|
|
|
CMD_STATUS(netstack_cmd_rx) = PENDING;
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
cmd_rx_handle = RF_scheduleCmd(
|
|
|
|
&rf_netstack,
|
|
|
|
(RF_Op *)&netstack_cmd_rx,
|
|
|
|
&sched_params,
|
|
|
|
cmd_rx_cb,
|
|
|
|
RF_EventRxEntryDone | RF_EventRxBufFull);
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!CMD_HANDLE_OK(cmd_rx_handle)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Unable to schedule RX command, handle=%d status=0x%04x\n",
|
|
|
|
cmd_rx_handle, CMD_STATUS(netstack_cmd_rx));
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_LISTEN);
|
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(start) {
|
2018-07-17 16:02:38 +00:00
|
|
|
rf_is_on = true;
|
2018-07-18 15:41:04 +00:00
|
|
|
process_poll(&rf_sched_process);
|
2018-07-17 16:02:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
netstack_stop_rx(void)
|
|
|
|
{
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!cmd_rx_is_active()) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_WARN("RX not active when stopping RX\n");
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMD_STATUS(netstack_cmd_rx) = DONE_STOPPED;
|
2018-07-12 08:44:14 +00:00
|
|
|
const RF_Stat stat = RF_cancelCmd(&rf_netstack, cmd_rx_handle, RF_ABORT_GRACEFULLY);
|
|
|
|
cmd_rx_handle = 0;
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
|
|
|
|
|
|
|
|
return (stat == RF_StatSuccess)
|
2018-07-24 10:16:53 +00:00
|
|
|
? RF_RESULT_OK
|
|
|
|
: RF_RESULT_ERROR;
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
RF_Handle
|
|
|
|
ble_open(RF_Params *params)
|
|
|
|
{
|
2018-07-25 16:16:27 +00:00
|
|
|
#if RF_CONF_BLE_BEACON_ENABLE
|
|
|
|
return RF_open(&rf_ble, &ble_mode, (RF_RadioSetup *)&ble_cmd_radio_setup, params);
|
2018-07-25 14:59:28 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
return (RF_Handle)NULL;
|
|
|
|
#endif
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rf_result_t
|
|
|
|
ble_sched_beacon(RF_Callback cb, RF_EventMask bm_event)
|
|
|
|
{
|
2018-07-25 16:16:27 +00:00
|
|
|
#if RF_CONF_BLE_BEACON_ENABLE
|
2018-07-09 18:13:01 +00:00
|
|
|
RF_ScheduleCmdParams sched_params;
|
|
|
|
RF_ScheduleCmdParams_init(&sched_params);
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
sched_params.priority = RF_PriorityNormal;
|
|
|
|
sched_params.endTime = 0;
|
2018-07-09 18:13:01 +00:00
|
|
|
sched_params.allowDelay = RF_AllowDelayAny;
|
|
|
|
|
|
|
|
CMD_STATUS(ble_cmd_beacon) = PENDING;
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
RF_CmdHandle beacon_handle = RF_scheduleCmd(
|
|
|
|
&rf_ble,
|
|
|
|
(RF_Op *)&ble_cmd_beacon,
|
|
|
|
&sched_params,
|
|
|
|
cb,
|
|
|
|
bm_event);
|
2018-07-09 18:13:01 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!CMD_HANDLE_OK(beacon_handle)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Unable to schedule BLE Beacon command, handle=%d status=0x%04x\n",
|
|
|
|
beacon_handle, CMD_STATUS(ble_cmd_beacon));
|
2018-07-09 18:13:01 +00:00
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint_fast8_t rx_key = cmd_rx_disable();
|
|
|
|
|
|
|
|
/* Wait until Beacon operation finishes */
|
|
|
|
RF_EventMask beacon_events = RF_pendCmd(&rf_ble, beacon_handle, 0);
|
2018-07-23 12:35:37 +00:00
|
|
|
if(!EVENTS_CMD_DONE(beacon_events)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("Pending on scheduled BLE Beacon command generated error, events=0x%08llx status=0x%04x\n",
|
|
|
|
beacon_events, CMD_STATUS(ble_cmd_beacon));
|
2018-07-09 18:13:01 +00:00
|
|
|
|
|
|
|
cmd_rx_restore(rx_key);
|
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_rx_restore(rx_key);
|
|
|
|
return RF_RESULT_OK;
|
2018-07-25 14:59:28 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
return RF_RESULT_ERROR;
|
|
|
|
#endif
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-23 07:37:23 +00:00
|
|
|
PROCESS(rf_sched_process, "RF Scheduler Process");
|
2018-07-09 18:13:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-18 15:41:04 +00:00
|
|
|
PROCESS_THREAD(rf_sched_process, ev, data)
|
2018-07-09 18:13:01 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
while(1) {
|
2018-07-17 16:02:38 +00:00
|
|
|
PROCESS_YIELD_UNTIL((ev == PROCESS_EVENT_POLL) ||
|
2018-07-20 15:20:25 +00:00
|
|
|
(ev == PROCESS_EVENT_TIMER));
|
|
|
|
|
|
|
|
/* start the synth re-calibration timer once. */
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rf_is_on) {
|
2018-07-20 15:20:25 +00:00
|
|
|
rf_is_on = false;
|
2018-07-24 15:24:06 +00:00
|
|
|
clock_time_t interval = synth_recal_interval();
|
|
|
|
LOG_INFO("Starting synth re-calibration timer, next timeout %lu\n", interval);
|
|
|
|
etimer_set(&synth_recal_timer, interval);
|
2018-07-20 15:20:25 +00:00
|
|
|
}
|
2018-07-17 16:02:38 +00:00
|
|
|
|
2018-07-23 12:35:37 +00:00
|
|
|
if(ev == PROCESS_EVENT_POLL) {
|
2018-07-17 16:02:38 +00:00
|
|
|
do {
|
|
|
|
watchdog_periodic();
|
2018-07-20 15:20:25 +00:00
|
|
|
|
2018-07-17 16:02:38 +00:00
|
|
|
packetbuf_clear();
|
|
|
|
len = NETSTACK_RADIO.read(packetbuf_dataptr(), PACKETBUF_SIZE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RX will stop if the RX buffers are full. In this case, restart
|
|
|
|
* RX after we've freed at least on packet.
|
|
|
|
*/
|
2018-07-23 12:35:37 +00:00
|
|
|
if(rx_buf_full) {
|
2018-07-24 15:24:06 +00:00
|
|
|
LOG_ERR("RX buffer full, restart RX status=0x%04x\n", CMD_STATUS(netstack_cmd_rx));
|
2018-07-17 16:02:38 +00:00
|
|
|
rx_buf_full = false;
|
|
|
|
|
|
|
|
/* Restart RX. */
|
|
|
|
netstack_stop_rx();
|
|
|
|
netstack_sched_rx(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len > 0) {
|
|
|
|
packetbuf_set_datalen(len);
|
|
|
|
|
|
|
|
NETSTACK_MAC.input();
|
|
|
|
}
|
2018-07-24 10:16:53 +00:00
|
|
|
/* Only break when we receive -1 => No available data */
|
2018-07-23 07:37:23 +00:00
|
|
|
} while(len >= 0);
|
2018-07-17 16:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scheduling CMD_FS will re-calibrate the synth. */
|
2018-07-23 12:35:37 +00:00
|
|
|
if((ev == PROCESS_EVENT_TIMER) &&
|
2018-07-24 10:16:53 +00:00
|
|
|
etimer_expired(&synth_recal_timer)) {
|
2018-07-24 15:24:06 +00:00
|
|
|
clock_time_t interval = synth_recal_interval();
|
|
|
|
LOG_DBG("Re-calibrate synth, next interval %lu\n", interval);
|
2018-07-17 16:02:38 +00:00
|
|
|
|
2018-07-24 15:24:06 +00:00
|
|
|
netstack_sched_fs();
|
|
|
|
etimer_set(&synth_recal_timer, interval);
|
2018-07-17 16:02:38 +00:00
|
|
|
}
|
2018-07-09 18:13:01 +00:00
|
|
|
}
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|