Microchip® Advanced Software Framework

rf212b.c File Reference
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "contiki.h"
#include "leds.h"
#include "rtimer.h"
#include "netstack.h"
#include "net/packetbuf.h"
#include "rf212b-const.h"
#include "rf212b-config.h"
#include "rf212b-arch.h"
#include "rf212b.h"
#include "trx_access.h"
#include "delay.h"
#include "system_interrupt.h"

Macros

#define _DEBUG_   0
 
#define BUSYWAIT_UNTIL(cond, max_time)
 
#define DEBUG_PRINTDATA   0 /* print frames to/from the radio; requires DEBUG == 1 */
 
#define FOOTER_LEN   3 /* bytes */
 
#define MAX_PACKET_LEN   127 /* bytes, excluding the length (first) byte */
 
#define PREV_TX_TIMEOUT   (10 * RTIMER_SECOND/1000)
 
#define PRINTF(...)
 
#define RF212_COMMAND(c)   trx_reg_write(RF212_REG_TRX_STATE, c)
 
#define RF212_STATUS()   rf212_status()
 
#define RSSI_OFFSET   (91)
 
#define STR(x)   STR_HELPER(x)
 
#define STR_HELPER(x)   #x
 Copyright (c) 2015 Microchip Technology Inc and 2012 – 2013, Thingsquare, http://www.thingsquare.com/. More...
 

Functions

static void flush_buffer (void)
 
void goto_sleep (void)
 
static int off (void)
 
static int on (void)
 
 PROCESS (rf212_radio_process,"RF212 radio driver")
 
 PROCESS_THREAD (rf212_radio_process, ev, data)
 Radio RF212 process, infinitely awaits a poll, then checks radio state and handles received data. More...
 
static void radiocore_hard_recovery (void)
 
static int rf212_channel_clear (void)
 perform a clear channel assessment More...
 
int rf212_get_txp (void)
 Get transmission power. More...
 
static int rf212_init (void)
 Init the radio. More...
 
int rf212_interrupt_poll (void)
 RF212 radio process poll function, to be called from the interrupt handler to poll the radio process. More...
 
static int rf212_off (void)
 switch the radio off More...
 
static int rf212_on (void)
 switch the radio on to listen (rx) mode More...
 
static int rf212_pending_packet (void)
 check whether we have a frame awaiting processing More...
 
static int rf212_prepare (const void *payload, unsigned short payload_len)
 prepare a frame and the radio for immediate transmission More...
 
static int rf212_read (void *buf, unsigned short bufsize)
 read a received frame out of the radio buffer More...
 
static int rf212_receiving_packet (void)
 check whether we are currently receiving a frame More...
 
static int rf212_send (const void *payload, unsigned short payload_len)
 Send data: first prepares, then transmits. More...
 
int rf212_set_txp (uint8_t txp)
 Set transmission power. More...
 
int rf212_sleep (void)
 
uint8_t rf212_status ()
 
static int rf212_transmit (unsigned short payload_len)
 Transmit a frame already put in the radio with 'prepare'. More...
 
static void rf_generate_random_seed (void)
 
int rf_get_channel (void)
 Get radio channel. More...
 
int rf_set_channel (uint8_t ch)
 Set radio channel. More...
 
void SetIEEEAddr (uint8_t *ieee_addr)
 
void SetPanId (uint16_t panId)
 
void SetShortAddr (uint16_t addr)
 
void wake_from_sleep (void)
 

Variables

static uint8_t flag_transmit = 0
 
static volatile int interrupt_callback_in_progress = 0
 
static volatile int interrupt_callback_wants_poll = 0
 
static volatile int pending_frame = 0
 
static volatile int radio_is_on = 0
 
struct radio_driver rf212_radio_driver
 
static volatile int sleep_on = 0
 

#define _DEBUG_   0
#define BUSYWAIT_UNTIL (   cond,
  max_time 
)
Value:
do { \
t0 = RTIMER_NOW(); \
while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))); \
} while(0)
unsigned short rtimer_clock_t
Definition: rtimer.h:58
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:132
while(s->file.len > 0)
Definition: wireless/SmartConnect_6LoWPAN/apps/ip64-webserver/httpd.c:170
#define RTIMER_CLOCK_LT(a, b)
Definition: rtimer.h:59

Referenced by rf212_channel_clear(), and rf212_transmit().

#define DEBUG_PRINTDATA   0 /* print frames to/from the radio; requires DEBUG == 1 */
#define FOOTER_LEN   3 /* bytes */
#define MAX_PACKET_LEN   127 /* bytes, excluding the length (first) byte */

Referenced by rf212_prepare().

#define PREV_TX_TIMEOUT   (10 * RTIMER_SECOND/1000)
#define RF212_COMMAND (   c)    trx_reg_write(RF212_REG_TRX_STATE, c)
#define RF212_STATUS ( )    rf212_status()
#define RSSI_OFFSET   (91)

Referenced by rf212_read().

#define STR (   x)    STR_HELPER(x)
#define STR_HELPER (   x)    #x

Copyright (c) 2015 Microchip Technology Inc and 2012 – 2013, Thingsquare, http://www.thingsquare.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 Atmel nor the name of Thingsquare 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 may only be redistributed and used in connection with an Atmel microcontroller or Atmel wireless product.

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.

static void flush_buffer ( void  )
static

References trx_frame_write().

Referenced by rf212_read().

void goto_sleep ( void  )

Referenced by rf212_sleep(), and rf233_sleep().

PROCESS ( rf212_radio_process  ,
"RF212 radio driver"   
)
PROCESS_THREAD ( rf212_radio_process  ,
ev  ,
data   
)

Radio RF212 process, infinitely awaits a poll, then checks radio state and handles received data.

References rdc_driver::input, len, NETSTACK_RDC, packetbuf_clear(), packetbuf_dataptr(), packetbuf_set_datalen(), PACKETBUF_SIZE, PRINTF, PROCESS_BEGIN, PROCESS_END, PROCESS_EVENT_POLL, PROCESS_YIELD_UNTIL, rf212_interrupt_poll(), and rf212_read().

static void radiocore_hard_recovery ( void  )
static

References printf, and rf212_init().

Referenced by rf212_transmit().

static int rf212_channel_clear ( void  )
static
int rf212_get_txp ( void  )

Get transmission power.

Returns
The transmission power

References PHY_TX_PWR_TXP, PRINTF, RF212_REG_PHY_TX_PWR_CONF, and trx_reg_read().

int rf212_interrupt_poll ( void  )

RF212 radio process poll function, to be called from the interrupt handler to poll the radio process.

Return values
0success

References flag_transmit, IRQ_BAT_LOW, IRQ_CCA_ED_DONE, IRQ_PLL_LOCK, IRQ_PLL_UNLOCK, IRQ_RX_ADDRESS_MATCH, IRQ_TRX_DONE, IRQ_TRXBUF_ACCESS_VIOLATION, pending_frame, PRINTF, process_poll(), RF212_COMMAND, RF212_REG_IRQ_STATUS, RF212_REG_TRX_STATE, trx_reg_read(), TRX_STATE_TRAC_STATUS, and TRXCMD_RX_AACK_ON.

Referenced by PROCESS_THREAD(), and rf212_init().

static int rf212_off ( void  )
static

switch the radio off

Return values
0Success

References off(), and PRINTF.

Referenced by rf212_sleep().

static int rf212_on ( void  )
static

switch the radio on to listen (rx) mode

Return values
0Success

References on(), and PRINTF.

static int rf212_pending_packet ( void  )
static

check whether we have a frame awaiting processing

Return values
>0we have a frame awaiting processing
0we have not a frame awaiting processing

References pending_frame, and PRINTF.

static int rf212_prepare ( const void *  payload,
unsigned short  payload_len 
)
static

prepare a frame and the radio for immediate transmission

Parameters
payloadPointer to data to copy/send
payload_lenlength of data to copy
Returns
Returns success/fail, refer to radio.h for explanation

References data, i, MAX_PACKET_LEN, PRINTF, RADIO_TX_ERR, RADIO_TX_OK, rf212_status(), STATE_BUSY_RX, STATE_BUSY_RX_AACK, STATE_BUSY_TX, STATE_BUSY_TX_ARET, and trx_frame_write().

Referenced by rf212_send().

static int rf212_read ( void *  buf,
unsigned short  bufsize 
)
static

read a received frame out of the radio buffer

Parameters
bufpointer to where to copy received data
bufsizeMaximum size we can copy into bufsize
Returns
Returns length of data read (> 0) if successful
Return values
-1Failed, was transmitting so FIFO is invalid
-2Failed, rx timed out (stuck in rx?)
-3Failed, too large frame for buffer
-4Failed, CRC/FCS failed (if USE_HW_FCS_CHECK is true)
  • data is invalid, bail out */
  • still receiving - data is invalid, wait for it to finish */
  • CRC/FCS fail, drop */

References flush_buffer(), len, PACKETBUF_ATTR_RSSI, packetbuf_set_attr(), pending_frame, PRINTF, RF212_REG_PHY_ED_LEVEL, RSSI_OFFSET, trx_frame_read(), trx_reg_read(), and trx_sram_read().

Referenced by PROCESS_THREAD().

static int rf212_receiving_packet ( void  )
static

check whether we are currently receiving a frame

Return values
>0we are currently receiving a frame
0we are not currently receiving a frame

References PRINTF, rf212_status(), STATE_BUSY_RX, and STATE_BUSY_RX_AACK.

static int rf212_send ( const void *  payload,
unsigned short  payload_len 
)
static

Send data: first prepares, then transmits.

Parameters
payloadPointer to data to copy/send
payload_lenlength of data to copy
Returns
Returns success/fail, refer to radio.h for explanation

References PRINTF, RADIO_TX_ERR, rf212_prepare(), and rf212_transmit().

int rf212_set_txp ( uint8_t  txp)

Set transmission power.

Parameters
txpThe transmission power
Return values
-1Fail: transmission power out of bounds
0Success

References PRINTF, RF212_REG_PHY_TX_PWR_CONF, trx_reg_write(), and TXP_M17.

int rf212_sleep ( void  )
static int rf212_transmit ( unsigned short  payload_len)
static
int rf_get_channel ( void  )

Get radio channel.

Returns
The radio channel

Referenced by main().

int rf_set_channel ( uint8_t  ch)

Set radio channel.

Parameters
chThe radio channel
Return values
-1Fail: channel number out of bounds
0Success

Referenced by main().

void SetIEEEAddr ( uint8_t *  ieee_addr)

Referenced by main().

void SetPanId ( uint16_t  panId)

Referenced by rf212_init(), and rf233_init().

void SetShortAddr ( uint16_t  addr)
void wake_from_sleep ( void  )

Referenced by on().

uint8_t flag_transmit = 0
static
volatile int interrupt_callback_in_progress = 0
static
volatile int interrupt_callback_wants_poll = 0
static
volatile int pending_frame = 0
static
volatile int radio_is_on = 0
static

Referenced by off(), and on().

struct radio_driver rf212_radio_driver
Initial value:
=
{
}
static int rf212_pending_packet(void)
check whether we have a frame awaiting processing
Definition: rf212b.c:748
static int rf212_channel_clear(void)
perform a clear channel assessment
Definition: rf212b.c:681
static int rf212_transmit(unsigned short payload_len)
Transmit a frame already put in the radio with &#39;prepare&#39;.
Definition: rf212b.c:435
static int rf212_receiving_packet(void)
check whether we are currently receiving a frame
Definition: rf212b.c:725
static int rf212_init(void)
Init the radio.
Definition: rf212b.c:230
static int rf212_off(void)
switch the radio off
Definition: rf212b.c:771
static int rf212_prepare(const void *payload, unsigned short payload_len)
prepare a frame and the radio for immediate transmission
Definition: rf212b.c:379
static int rf212_send(const void *data, unsigned short len)
Send data: first prepares, then transmits.
Definition: rf212b.c:549
static int rf212_on(void)
switch the radio on to listen (rx) mode
Definition: rf212b.c:759
static int rf212_read(void *buf, unsigned short bufsize)
read a received frame out of the radio buffer
Definition: rf212b.c:569
int rf212_sleep(void)
Definition: rf212b.c:352
volatile int sleep_on = 0
static

Referenced by rf212_sleep().