/**
* Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
*
* 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, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, 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 Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
*
*/
#include "nrf_error.h"
#include "nrf_esb.h"
#include "nrf_esb_error_codes.h"
#include "nrf_gpio.h"
#include <string.h>
#include <stddef.h>
#include "sdk_common.h"
#include "sdk_macros.h"
#include "app_util.h"
#include "nrf_log.h"
#include "nrf_delay.h"
#define BIT_MASK_UINT_8(x) (0xFF >> (8 - (x)))
#define NRF_ESB_PIPE_COUNT 8
// Constant parameters
#define RX_WAIT_FOR_ACK_TIMEOUT_US_2MBPS (48) /**< 2 Mb RX wait for acknowledgment time-out value. Smallest reliable value - 43. */
#define RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS (64) /**< 1 Mb RX wait for acknowledgment time-out value. Smallest reliable value - 59. */
#define RX_WAIT_FOR_ACK_TIMEOUT_US_250KBPS (250) /**< 250 Kb RX wait for acknowledgment time-out value. */
#define RX_WAIT_FOR_ACK_TIMEOUT_US_1MBPS_BLE (73) /**< 1 Mb RX wait for acknowledgment time-out (combined with BLE). Smallest reliable value - 68.*/
// Interrupt flags
#define NRF_ESB_INT_TX_SUCCESS_MSK 0x01 /**< Interrupt mask value for TX success. */
#define NRF_ESB_INT_TX_FAILED_MSK 0x02 /**< Interrupt mask value for TX failure. */
#define NRF_ESB_INT_RX_DATA_RECEIVED_MSK 0x04 /**< Interrupt mask value for RX_DR. */
#define NRF_ESB_PID_RESET_VALUE 0xFF /**< Invalid PID value which is guaranteed to not collide with any valid PID value. */
#define NRF_ESB_PID_MAX 3 /**< Maximum value for PID. */
#define NRF_ESB_CRC_RESET_VALUE 0xFFFF /**< CRC reset value. */
// Internal Enhanced ShockBurst module state.
typedef enum {
NRF_ESB_STATE_IDLE, /**< Module idle. */
NRF_ESB_STATE_PTX_TX, /**< Module transmitting without acknowledgment. */
NRF_ESB_STATE_PTX_TX_ACK, /**< Module transmitting with acknowledgment. */
NRF_ESB_STATE_PTX_RX_ACK, /**< Module transmitting with acknowledgment and reception of payload with the acknowledgment response. */
NRF_ESB_STATE_PRX, /**< Module receiving packets without acknowledgment. */
NRF_ESB_STATE_PRX_SEND_ACK, /**< Module transmitting acknowledgment in RX mode. */
} nrf_esb_mainstate_t;
#define DISABLE_RF_IRQ() NVIC_DisableIRQ(RADIO_IRQn)
#define ENABLE_RF_IRQ() NVIC_EnableIRQ(RADIO_IRQn)
#define _RADIO_SHORTS_COMMON ( RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk | \
RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk )
#define VERIFY_PAYLOAD_LENGTH(p) \
do \
{ \
if (p->length == 0 || \
p->length > NRF_ESB_MAX_PAYLOAD_LENGTH || \
(m_config_local.protocol == NRF_ESB_PROTOCOL_ESB && \
p->length > m_config_local.payload_length)) \
{ \
return NRF_ERROR_INVALID_LENGTH; \
} \
}while (0)
/* @brief Structure holding pipe info PID and CRC and acknowledgment payload. */
typedef struct
{
uint16_t crc; /**< CRC value of the last received packet (Used to detect retransmits). */
uint8_t pid; /**< Packet ID of the last received packet (Used to detect retransmits). */
bool ack_payload; /**< Flag indicating the state of the transmission of acknowledgment payloads. */
} pipe_info_t;
/* @brief First-in, first-out queue of payloads to be transmitted. */
typedef struct
{
nrf_esb_payload_t * p_payload[NRF_ESB_TX_FIFO_SIZE]; /**< Pointer to the actual queue. */
uint32_t entry_point; /**< Current start of queue. */
uint32_t exit_point; /**< Current end of queue. */
uint32_t count; /**< Current number of elements in the queue. */
} nrf_esb_payload_tx_fifo_t;
/* @brief First-in, first-out queue of received payloads. */
typedef struct
{
nrf_esb_payload_t * p_payload[NRF_ESB_RX_FIFO_SIZE]; /**< Pointer to the actual queue. */
uint32_t entry_point; /**< Current start of queue. */
uint32_t exit_point; /**< Current end of queue. */
uint32_t count; /**< Current number of elements in the queue. */
} nrf_esb_payload_rx_fifo_t;
/**@brief Enhanced ShockBurst address.
*
* Enhanced ShockBurst addresses consist of a base address and a prefix
* that is unique for each pipe. See @ref esb_addressing in the ESB user
* guide for more information.
*/
typedef struct
{
uint8_t base_addr_p0[4]; /**< Base address for pipe 0 encoded in big endian. */
uint8_t base_addr_p1[4]; /**< Base address for pipe 1-7 encoded in big endian. */
uint8_t pipe_prefixes[8]; /**< Address prefix for pipe 0 to 7. */
uint8_t num_pipes; /**< Number of pipes available. */
uint8_t addr_length; /**< Length of the address including the prefix. */
uint8_t rx_pipes_enabled; /**< Bitfield for enabled pipes. */
uint8_t rf_channel; /**< Channel to use (must be between 0 and 100). */
} nrf_esb_address_t;
// Module state
static bool m_esb_initialized = false;
static nrf_esb_mainstate_t m_nrf_esb_mainstate