Arduino Due, CC1120 (ccRF 3 click board)

serialplotter.jpg

CC1120 ASK/OOK transparent serial mode.
CC1120 GPIO3 configured for CARRIER SENSE, triggers when RSSI is above defined threshold (AGC_CS_THR register). GPIO0 configured for SERIAL_RX.

#include <SPI.h>
#include "cc1120.h"

#define PRINT_INFO
#undef PRINT_INFO
#define DATA_SIZE   512
#define PLOT_SCALE  64

uint16_t data[DATA_SIZE]; // uint16_t is enough for max 32767 uS pulse len
// bit 15 = 1 when high pulse, 0 when low pulse
volatile bool printData = false;
volatile uint16_t idx   = 0;


void setup() {
  Serial.begin(57600);
  pinMode(RESET_PIN, OUTPUT);
  pinMode(SS_PIN, OUTPUT);
  pinMode(GDO0_PIN, INPUT); // SERIAL_RX
  pinMode(GDO3_PIN, INPUT); // CARRIER SENSE
  digitalWrite(SS_PIN, HIGH);
  digitalWrite(RESET_PIN, HIGH);
  memset(data, 0, sizeof(data));
 
  SPI.begin();

  strobeSPI(SNOP);

  // Sync off
  uint8_t v = readSPI(SYNC_CFG0);
  v &= B11100011;
  writeSPI(SYNC_CFG0, v);

  writeSPI(IOCFG3, CARRIER_SENSE);
  writeSPI(IOCFG0, SERIAL_RX);
  writeSPI(SYNC_CFG1, SMARTRF_SETTING_SYNC_CFG1);
  writeSPI(MODCFG_DEV_E, SMARTRF_SETTING_MODCFG_DEV_E);
  writeSPI(DCFILT_CFG, SMARTRF_SETTING_DCFILT_CFG);
  writeSPI(PREAMBLE_CFG1, SMARTRF_SETTING_PREAMBLE_CFG1);
  writeSPI(IQIC, SMARTRF_SETTING_IQIC);
  writeSPI(CHAN_BW, SMARTRF_SETTING_CHAN_BW);
  writeSPI(MDMCFG1, SMARTRF_SETTING_MDMCFG1);
  writeSPI(MDMCFG0, SMARTRF_SETTING_MDMCFG0);
  writeSPI(AGC_REF, SMARTRF_SETTING_AGC_REF);
  // -102 dBm + 0x19 = -77 dBm carrier sense rssi threshold
  writeSPI(AGC_CS_THR, SMARTRF_SETTING_AGC_CS_THR);
  writeSPI(AGC_CFG1, SMARTRF_SETTING_AGC_CFG1);
  writeSPI(AGC_CFG0, SMARTRF_SETTING_AGC_CFG0);
  writeSPI(FS_CFG, SMARTRF_SETTING_FS_CFG);
  writeSPI(PKT_CFG2, SMARTRF_SETTING_PKT_CFG2);
  writeSPI(PKT_CFG1, SMARTRF_SETTING_PKT_CFG1);
  writeSPI(PKT_CFG0, SMARTRF_SETTING_PKT_CFG0);
  writeSPI(PA_CFG2, SMARTRF_SETTING_PA_CFG2);
  writeSPI(PA_CFG0, SMARTRF_SETTING_PA_CFG0);

  writeExtAddrSPI(IF_MIX_CFG, SMARTRF_SETTING_IF_MIX_CFG);
  writeExtAddrSPI(FREQOFF_CFG, SMARTRF_SETTING_FREQOFF_CFG);
  writeExtAddrSPI(FREQ2, SMARTRF_SETTING_FREQ2);
  writeExtAddrSPI(FREQ1, SMARTRF_SETTING_FREQ1);
  writeExtAddrSPI(FREQ0, SMARTRF_SETTING_FREQ0);
  writeExtAddrSPI(FS_DIG1, SMARTRF_SETTING_FS_DIG1);
  writeExtAddrSPI(FS_DIG0, SMARTRF_SETTING_FS_DIG0);
  writeExtAddrSPI(FS_CAL1, SMARTRF_SETTING_FS_CAL1);
  writeExtAddrSPI(FS_CAL0, SMARTRF_SETTING_FS_CAL0);
  writeExtAddrSPI(FS_DIVTWO, SMARTRF_SETTING_FS_DIVTWO);
  writeExtAddrSPI(FS_DSM0, SMARTRF_SETTING_FS_DSM0);
  writeExtAddrSPI(FS_DVC0, SMARTRF_SETTING_FS_DVC0);
  writeExtAddrSPI(FS_PFD, SMARTRF_SETTING_FS_PFD);
  writeExtAddrSPI(FS_PRE, SMARTRF_SETTING_FS_PRE);
  writeExtAddrSPI(FS_REG_DIV_CML, SMARTRF_SETTING_FS_REG_DIV_CML);
  writeExtAddrSPI(FS_SPARE, SMARTRF_SETTING_FS_SPARE);
  writeExtAddrSPI(FS_VCO0, SMARTRF_SETTING_FS_VCO0);
  writeExtAddrSPI(XOSC5, SMARTRF_SETTING_XOSC5);
  writeExtAddrSPI(XOSC1, SMARTRF_SETTING_XOSC1);
  writeExtAddrSPI(SERIAL_STATUS, SMARTRF_SETTING_SERIAL_STATUS);

#ifdef PRINT_INFO
  v = readExtAddrSPI(PARTVERSION);
  Serial.print(F("PARTVERSION "));
  Serial.println(v, HEX);
  v = readExtAddrSPI(PARTNUMBER);
  Serial.print(F("PARTNUMBER "));
  Serial.println(v, HEX);
#endif

  strobeSPI(SRX);
  delay(100);

#ifdef PRINT_INFO
  v = readExtAddrSPI(MARCSTATE);
  Serial.print(F("MARCSTATE "));
  Serial.println(v, BIN);
#endif

  attachInterrupt(digitalPinToInterrupt(GDO0_PIN), isr, CHANGE);
}

void loop() {
  if (printData) {
    for (uint16_t i = 0; i < idx; i++) {
      uint8_t v = 0;
      if (bitRead(data[i], 15)) {
        bitClear(data[i], 15);
        v = 1;
      }
      uint8_t count = (uint8_t) (data[i] / PLOT_SCALE);
      for (uint8_t j = 0; j < count; j++) {
        Serial.println(v);
      }
    }
    memset(data, 0, sizeof(data));
    idx = 0;
    printData = false;
  }
}

void isr() {
  static unsigned long timestart = micros();
  static unsigned long timenow = 0;
 
  if (!printData) {
    uint32_t r = PIOC->PIO_PDSR;
    if (bitRead(r, 24)) { //CARRIER_SENSE
      timenow = micros();
      data[idx] = (uint16_t) timenow - timestart;
      timestart = timenow;
      if (bitRead(r, 25)) {
        bitSet(data[idx], 15);
      }
      else {
        bitClear(data[idx], 15);
      }
      if (idx == DATA_SIZE) {
          printData = true;
      }
      idx++;
    }
    else {
      if (idx > 0) {
        printData = true;
      }
    }
  }
}

uint8_t readSPI(uint8_t addr) {
  digitalWrite(SS_PIN, LOW);
  SPI.transfer(R_BIT | addr);
  uint8_t v = SPI.transfer(0x00);
  digitalWrite(SS_PIN, HIGH);
  return v;
}

void writeSPI(uint8_t addr, uint8_t value) {
  digitalWrite(SS_PIN, LOW);
  SPI.transfer(addr);
  SPI.transfer(value);
  digitalWrite(SS_PIN, HIGH);
}

void strobeSPI(uint8_t cmd)
{
  digitalWrite(SS_PIN, LOW);
  SPI.transfer(R_BIT | cmd);
  digitalWrite(SS_PIN, HIGH);
}

uint8_t readExtAddrSPI(uint8_t addr) {
  uint8_t v;
  digitalWrite(SS_PIN, LOW);
  SPI.transfer(R_BIT | EXT_ADDR);
  SPI.transfer(addr);
  v = SPI.transfer(0xff);
  digitalWrite(SS_PIN, HIGH);
  return v;
}

void writeExtAddrSPI(uint8_t addr, uint8_t value) {
  digitalWrite(SS_PIN, LOW);
  SPI.transfer(EXT_ADDR);
  SPI.transfer(addr);
  SPI.transfer(value);
  digitalWrite(SS_PIN, HIGH);
}

/* Address Config = No address check */
/* Bit Rate = 0.963 */
/* Carrier Frequency = 433.946960 */
/* Deviation = 3.997803 */
/* Device Address = 0 */
/* Manchester Enable = false */
/* Modulation Format = ASK/OOK */
/* PA Ramping = true */
/* Packet Bit Length = 0 */
/* Packet Length = 3 */
/* Packet Length Mode = Variable */
/* Performance Mode = High Performance */
/* RX Filter BW = 25.000000 */
/* Symbol rate = 0.963 */
/* TX Power = 12.5 */
/* Whitening = false */
/* Transparent serial mode */
/* GPIO0 = SERIAL_RX, GPIO3 = CARRIER_SENSE */

#ifndef SMARTRF_CC1120_H
#define SMARTRF_CC1120_H

#define SMARTRF_RADIO_CC1120
#define SMARTRF_SETTING_IOCFG3           0xB0
#define SMARTRF_SETTING_IOCFG2           0x06
#define SMARTRF_SETTING_IOCFG1           0xB0
#define SMARTRF_SETTING_IOCFG0           0x40
#define SMARTRF_SETTING_SYNC_CFG1        0x0B
#define SMARTRF_SETTING_MODCFG_DEV_E     0x1B
#define SMARTRF_SETTING_DCFILT_CFG       0x1C
#define SMARTRF_SETTING_PREAMBLE_CFG1    0x00
#define SMARTRF_SETTING_IQIC             0xC6
#define SMARTRF_SETTING_CHAN_BW          0x08
#define SMARTRF_SETTING_MDMCFG1          0x86
#define SMARTRF_SETTING_MDMCFG0          0x4A
#define SMARTRF_SETTING_SYMBOL_RATE2     0x3F
#define SMARTRF_SETTING_SYMBOL_RATE1     0x8E
#define SMARTRF_SETTING_SYMBOL_RATE0     0x3B
#define SMARTRF_SETTING_AGC_REF          0x20
#define SMARTRF_SETTING_AGC_CS_THR       0x19
#define SMARTRF_SETTING_AGC_CFG1         0xA9
#define SMARTRF_SETTING_AGC_CFG0         0xCF
#define SMARTRF_SETTING_FIFO_CFG         0x00
#define SMARTRF_SETTING_FS_CFG           0x14
#define SMARTRF_SETTING_PKT_CFG2         0x07
#define SMARTRF_SETTING_PKT_CFG1         0x00
#define SMARTRF_SETTING_PKT_CFG0         0x20
#define SMARTRF_SETTING_PA_CFG2          0x29
#define SMARTRF_SETTING_PA_CFG0          0x20
#define SMARTRF_SETTING_PKT_LEN          0x21
#define SMARTRF_SETTING_IF_MIX_CFG       0x00
#define SMARTRF_SETTING_FREQOFF_CFG      0x22
#define SMARTRF_SETTING_FREQ2            0x6C
#define SMARTRF_SETTING_FREQ1            0x7C
#define SMARTRF_SETTING_FREQ0            0x9B
#define SMARTRF_SETTING_FS_DIG1          0x00
#define SMARTRF_SETTING_FS_DIG0          0x5F
#define SMARTRF_SETTING_FS_CAL1          0x40
#define SMARTRF_SETTING_FS_CAL0          0x0E
#define SMARTRF_SETTING_FS_DIVTWO        0x03
#define SMARTRF_SETTING_FS_DSM0          0x33
#define SMARTRF_SETTING_FS_DVC0          0x17
#define SMARTRF_SETTING_FS_PFD           0x50
#define SMARTRF_SETTING_FS_PRE           0x6E
#define SMARTRF_SETTING_FS_REG_DIV_CML   0x14
#define SMARTRF_SETTING_FS_SPARE         0xAC
#define SMARTRF_SETTING_FS_VCO0          0xB4
#define SMARTRF_SETTING_XOSC5            0x0E
#define SMARTRF_SETTING_XOSC1            0x03
#define SMARTRF_SETTING_SERIAL_STATUS    0x08

// Pins
#define RESET_PIN         7
#define SS_PIN            10
#define GDO0_PIN          5
#define GDO3_PIN          6

#define R_BIT             0x80
#define BURST_BIT         0x40
#define SERIAL_CLK        0x08
#define SERIAL_RX         0x09
#define CARRIER_SENSE     0x11
#define EXT_CTRL_SETTING  0x00

// CC1120 registers
#define IOCFG3          0x00
#define IOCFG2          0x01
#define IOCFG1          0x02
#define IOCFG0          0x03
#define SYNC3           0x04
#define SYNC2           0x05
#define SYNC1           0x06
#define SYNC0           0x07
#define SYNC_CFG1       0x08
#define SYNC_CFG0       0x09
#define DEVIATION_M     0x0A
#define MODCFG_DEV_E    0x0B
#define DCFILT_CFG      0x0C
#define PREAMBLE_CFG1   0x0D
#define PREAMBLE_CFG0   0x0E
#define FREQ_IF_CFG     0x0F
#define IQIC            0x10
#define CHAN_BW         0x11
#define MDMCFG1         0x12
#define MDMCFG0         0x13
#define SYMBOL_RATE2    0x14
#define SYMBOL_RATE1    0x15
#define SYMBOL_RATE0    0x16
#define AGC_REF         0x17
#define AGC_CS_THR      0x18
#define AGC_GAIN_ADJUST 0x19
#define AGC_CFG3        0x1A
#define AGC_CFG2        0x1B
#define AGC_CFG1        0x1C
#define AGC_CFG0        0x1D
#define FIFO_CFG        0x1E
#define DEV_ADDR        0x1F
#define SETTLING_CFG    0x20
#define FS_CFG          0x21
#define WOR_CFG1        0x22
#define WOR_CFG0        0x23
#define WOR_EVENT0_MSB  0x24
#define WOR_EVENT0_LSB  0x25
#define PKT_CFG2        0x26
#define PKT_CFG1        0x27
#define PKT_CFG0        0x28
#define RFEND_CFG1      0x29
#define RFEND_CFG2      0x2A
#define PA_CFG2         0x2B
#define PA_CFG1         0x2C
#define PA_CFG0         0x2D
#define PKT_LEN         0x2E
#define EXT_ADDR        0x2F
#define FIFO            0x3F

// Extended register space
#define IF_MIX_CFG        0x00
#define FREQOFF_CFG       0x01
#define EXT_CTRL          0x06
#define FREQ2             0x0C
#define FREQ1             0x0D
#define FREQ0             0x0E
#define FS_DIG1           0x12
#define FS_DIG0           0x13
#define FS_CAL1           0x16
#define FS_CAL0           0x17
#define FS_DIVTWO         0x19
#define FS_DSM0           0x1B
#define FS_DVC0           0x1D
#define FS_PFD            0x1F
#define FS_PRE            0x20
#define FS_REG_DIV_CML    0x21
#define FS_SPARE          0x22
#define FS_VCO0           0x27
#define XOSC5             0x32
#define XOSC1             0x36
#define RSSI1             0x71
#define RSSI0             0x72
#define MARCSTATE         0x73
#define ASK_SOFT_RX_DATA  0x7F
#define PARTNUMBER        0x8F
#define PARTVERSION       0x90
#define SERIAL_STATUS     0x91
#define MODEM_STATUS1     0x92
#define MODEM_STATUS0     0x93
#define MARC_STATUS1      0x94
#define MARC_STATUS0      0x95
#define NUM_RXBYTES       0xD7
#define FIFO_NUM_RXBYTES  0xD9

// CC1120 command strobes
#define SRES            0x30
#define SFSTXON         0x31
#define SXOFF           0x32
#define SCAL            0x33
#define SRX             0x34
#define STX             0x35
#define SIDLE           0x36
#define SAFC            0x37
#define SWOR            0x38
#define SPWD            0x39
#define SFRX            0x3A
#define SFTX            0x3B
#define SWORRST         0x3C
#define SNOP            0x3D

#endif // CC1120_H

Saleae Logic 2 software works with cheap (from ebay) 8 channel 24 MHz logic analyzer. Together with e.g. Arduino/CC1120 it is great tool for inspecting e.g. OOK

logic2.jpg