<commit_before>// Implementation of an RF233 radio stack for the Tock operating
// system. Adapted from the Contiki RF233 stack provided as part
// of Atmel Studio. The licenses for the original code are at the
// bottom of the file to reduce clutter.
//
// Author: <NAME> <<EMAIL>>
// Date: April 18 2016


#include <gpio.h>
#include <spi.h>
#include <stdint.h>
#include <timer.h>

#include "rf233-const.h"
#include "rf233-config.h"
#include "rf233-arch.h"
#include "trx_access.h"
#include "rf233.h"

#define RADIO_SLP 8
#define RADIO_RST 9
#define RADIO_IRQ 10

/*---------------------------------------------------------------------------*/
static int on(void);
static int off(void);
static void rf_generate_random_seed(void);
static void flush_buffer(void);
static uint8_t flag_transmit = 0;
static uint8_t ack_status = 0;
static volatile int radio_is_on = 0;
static volatile int pending_frame = 0;
static volatile int sleep_on = 0;
static int rf233_prepare_without_header(const uint8_t *data, unsigned short data_len);
static int rf233_setup(void);
static int rf233_prepare(const void *payload, unsigned short payload_len);
static int rf233_transmit(void);
static int (*rx_callback)(void*, int, uint16_t, uint16_t, uint16_t);
static int set_callback = 0;

#define IEEE802154_CONF_PANID 0x1111

enum {
  RADIO_TX_OK        = 0,
  RADIO_TX_ERR       = 1,
  RADIO_TX_NOACK     = 2,
  RADIO_TX_COLLISION = 3
};

typedef struct {
  uint16_t fcf;
  uint8_t  seq;
  uint16_t pan;
  uint16_t dest;
  uint16_t src;
  char payload[];
} __attribute__((packed)) header_t;

static header_t radio_header;

/*---------------------------------------------------------------------------*/
int rf233_read(void *buf, unsigned short bufsize);
int rf233_receiving_packet(void);
int rf233_pending_packet(void);

/*---------------------------------------------------------------------------*/
/* convenience macros */
//#define RF233_STATUS()                    rf233_arch_status()
#define RF233_COMMAND(c)                  trx_reg_write(RF233_REG_TRX_STATE, c)

/* each frame has a footer consisting of LQI, ED, RX_STATUS added by the radio */
#define FOOTER_LEN                        3   /* bytes */
#define MAX_PACKET_LEN                    127 /* bytes, excluding the length (first) byte */
#define PACKETBUF_SIZE                    128 /* bytes, for even int writes */
#define HEADER_SIZE                       9 /* bytes */

/*---------------------------------------------------------------------------*/
#define _DEBUG_               0
#define DEBUG_PRINTDATA       0    /* print frames to/from the radio; requires DEBUG == 1 */
#if _DEBUG_
#define PRINTF(...)       printf(__VA_ARGS__)

// Used for debugging output
static const char* state_str(uint8_t state) {
  switch (state) {
  case STATE_P_ON:
    return "STATE_POWER_ON";
  case STATE_BUSY_RX:
    return "STATE_BUSY_RX";
  case STATE_BUSY_TX:
    return "STATE_BUSY_TX";
  case STATE_RX_ON:
    return "STATE_RX_ON";
  case STATE_TRX_OFF:
    return "STATE_TRX_OFF";
  case STATE_PLL_ON:
    return "STATE_PLL_ON";
  case STATE_SLEEP:
    return "STATE_SLEEP";
  case STATE_PREP_DEEP_SLEEP:
    return "STATE_PREP_DEEP_SLEEP";
  case STATE_BUSY_RX_AACK:<commit_msg>rf233: comment unused macro<commit_after>// Implementation of an RF233 radio stack for the Tock operating
// system. Adapted from the Contiki RF233 stack provided as part
// of Atmel Studio. The licenses for the original code are at the
// bottom of the file to reduce clutter.
//
// Author: <NAME> <<EMAIL>>
// Date: April 18 2016


#include <gpio.h>
#include <spi.h>
#include <stdint.h>
#include <timer.h>

#include "rf233-const.h"
#include "rf233-config.h"
#include "rf233-arch.h"
#include "trx_access.h"
#include "rf233.h"

#define RADIO_SLP 8
#define RADIO_RST 9
#define RADIO_IRQ 10

/*---------------------------------------------------------------------------*/
static int on(void);
static int off(void);
static void rf_generate_random_seed(void);
static void flush_buffer(void);
static uint8_t flag_transmit = 0;
static uint8_t ack_status = 0;
static volatile int radio_is_on = 0;
static volatile int pending_frame = 0;
static volatile int sleep_on = 0;
static int rf233_prepare_without_header(const uint8_t *data, unsigned short data_len);
static int rf233_setup(void);
static int rf233_prepare(const void *payload, unsigned short payload_len);
static int rf233_transmit(void);
static int (*rx_callback)(void*, int, uint16_t, uint16_t, uint16_t);
static int set_callback = 0;

#define IEEE802154_CONF_PANID 0x1111

enum {
  RADIO_TX_OK        = 0,
  RADIO_TX_ERR       = 1,
  RADIO_TX_NOACK     = 2,
  RADIO_TX_COLLISION = 3
};

typedef struct {
  uint16_t fcf;
  uint8_t  seq;
  uint16_t pan;
  uint16_t dest;
  uint16_t src;
  char payload[];
} __attribute__((packed)) header_t;

static header_t radio_header;

/*---------------------------------------------------------------------------*/
int rf233_read(void *buf, unsigned short bufsize);
int rf233_receiving_packet(void);
int rf233_pending_packet(void);

/*---------------------------------------------------------------------------*/
/* convenience macros */
//#define RF233_STATUS()                    rf233_arch_status()
#define RF233_COMMAND(c)                  trx_reg_write(RF233_REG_TRX_STATE, c)

/* each frame has a footer consisting of LQI, ED, RX_STATUS added by the radio */
//#define FOOTER_LEN                        3   /* bytes */
#define MAX_PACKET_LEN                    127 /* bytes, excluding the length (first) byte */
#define PACKETBUF_SIZE                    128 /* bytes, for even int writes */
#define HEADER_SIZE                       9 /* bytes */

/*---------------------------------------------------------------------------*/
#define _DEBUG_               0
#define DEBUG_PRINTDATA       0    /* print frames to/from the radio; requires DEBUG == 1 */
#if _DEBUG_
#define PRINTF(...)       printf(__VA_ARGS__)

// Used for debugging output
static const char* state_str(uint8_t state) {
  switch (state) {
  case STATE_P_ON:
    return "STATE_POWER_ON";
  case STATE_BUSY_RX:
    return "STATE_BUSY_RX";
  case STATE_BUSY_TX:
    return "STATE_BUSY_TX";
  case STATE_RX_ON:
    return "STATE_RX_ON";
  case STATE_TRX_OFF:
    return "STATE_TRX_OFF";
  case STATE_PLL_ON:
    return "STATE_PLL_ON";
  case STATE_SLEEP:
    return "STATE_SLEEP";
  case STATE_PREP_DEEP_SLEEP:
    return "STATE_PREP_DEEP_SLEEP";
  case STATE_BUSY_RX_AACK:
{'fraction_non_alphanumeric': 0.18140906269397888, 'fraction_numerical': 0.035226567349472375, 'mean_word_length': 2.384978991596639, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181531', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2042, 'n_words': 592}