Skip to content

teensy sdr transceiver – part 1

I’ve been following along with ZL2CTM’s latest Teensy SDR transceiver build and I’ve been trying to emulate his design even down to his construction style with the strip board. So far, I’ve got the radio receiving well enough. After troubleshooting a bad grinding and squealing noise for a week, I found a bad audio transformer. After replacing that, the rig came to life.

I was able to generate the quadrature oscillators with the SI5351 as detailed in this post and I’m amplifying the audio with the teensy itself, rather than an external audio amp. I’ll probably change that soon as it is still fairly low output and doesn’t sound that great. There is a video below demonstrating reception, but the audio is kind of low.

I’ve got some bad tuning clicks that I’ll need to resolve, but I’m happy with the progress so far. I would definitely recommend subscribing to Charlie’s Youtube channel. His videos are great and I have learned a lot from them.

 

 

 

 

 

Below is my code so far. I started with Charlie’s code and modified it to suit my needs.

#include <Wire.h>                            // I2C comms
#include "si5351.h"                          // Si5351 library
#include <arm_math.h>                        // Needed for float64_t 
#include "ILI9341_t3.h"
#include <Audio.h>
#include "font_Arial.h"
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>



#define TFT_DC      20
#define TFT_CS      21
#define TFT_RST    255  // 255 = unused, connect to 3.3V
#define TFT_MOSI     7
#define TFT_SCLK    14
#define TFT_MISO    12
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);



// Define Constants and Variables
static const uint64_t BAND_START = 700000000ULL;     // start of HF band
static const uint64_t BAND_END =   1400000000ULL;    // end of HF band
static const uint64_t BAND_INIT =  720000000ULL;     // where to initially set the frequency
static const uint64_t PLL_MIN = 60000000000ULL;
static const uint64_t PLL_MAX = 90000000000ULL;


volatile uint64_t oldfreq = 0;
volatile uint64_t freq = BAND_INIT ;
volatile long radix = 1000000;                // how much to change the frequency by, clicking the rotary encoder will change this.
volatile int updatedisplay = 0;


uint64_t pll_freq;
int multiple;


// Rotary Encoder
static const int pushPin = 39;
static const int rotBPin = 37;
static const int rotAPin = 36;

volatile int rotState = 0;
volatile int rotAval = 1;
volatile int rotBval = 1;
volatile int rotAcc = 0;

// Instantiate the Objects
Si5351 si5351;



#define NUM_HILBERT_COEFFS 70

const short Hilbert_Plus_45_Coeffs[NUM_HILBERT_COEFFS] = {
(short)(32768 * 0.000026665388842506),
(short)(32768 * 0.000045350266683587),
(short)(32768 * 0.000049535194963343),
(short)(32768 * 0.000013929445802525),
(short)(32768 * -0.000089756932471845),
(short)(32768 * -0.000280899822020724),
(short)(32768 * -0.000554463834139723),
(short)(32768 * -0.000866732397801864),
(short)(32768 * -0.001128869571306335),
(short)(32768 * -0.001214793584591925),
(short)(32768 * -0.000987123427391450),
(short)(32768 * -0.000339739120777462),
(short)(32768 * 0.000751166835621107),
(short)(32768 * 0.002181855899770147),
(short)(32768 * 0.003699317878546329),
(short)(32768 * 0.004919290372383914),
(short)(32768 * 0.005393023804155144),
(short)(32768 * 0.004719490953542981),
(short)(32768 * 0.002684917857603390),
(short)(32768 * -0.000601873045713977),
(short)(32768 * -0.004617356732843290),
(short)(32768 * -0.008411605699112680),
(short)(32768 * -0.010701388680801362),
(short)(32768 * -0.010070233160010302),
(short)(32768 * -0.005250377124685787),
(short)(32768 * 0.004564114354746081),
(short)(32768 * 0.019440673287414619),
(short)(32768 * 0.038537923280388298),
(short)(32768 * 0.060096847866633052),
(short)(32768 * 0.081618252179492840),
(short)(32768 * 0.100211560649161716),
(short)(32768 * 0.113059746959379301),
(short)(32768 * 0.117914393674033696),
(short)(32768 * 0.113521768501838344),
(short)(32768 * 0.099889668870034684),
(short)(32768 * 0.078334626955959316),
(short)(32768 * 0.051293625327079304),
(short)(32768 * 0.021933662563154727),
(short)(32768 * -0.006365101806065199),
(short)(32768 * -0.030551195192096176),
(short)(32768 * -0.048364086483228520),
(short)(32768 * -0.058631997671255619),
(short)(32768 * -0.061357904565856151),
(short)(32768 * -0.057592344202841303),
(short)(32768 * -0.049135236893174006),
(short)(32768 * -0.038140342465779013),
(short)(32768 * -0.026708902650401732),
(short)(32768 * -0.016552007744391070),
(short)(32768 * -0.008777661819717671),
(short)(32768 * -0.003825446520437530),
(short)(32768 * -0.001537721831848443),
(short)(32768 * -0.001329366926757822),
(short)(32768 * -0.002403463761614129),
(short)(32768 * -0.003959730656157654),
(short)(32768 * -0.005353975172548658),
(short)(32768 * -0.006185747399762316),
(short)(32768 * -0.006311999590758985),
(short)(32768 * -0.005801655201517503),
(short)(32768 * -0.004856081867239870),
(short)(32768 * -0.003722447168239438),
(short)(32768 * -0.002621972625618343),
(short)(32768 * -0.001705906352533607),
(short)(32768 * -0.001041873927200467),
(short)(32768 * -0.000624975669045641),
(short)(32768 * -0.000403299387333375),
(short)(32768 * -0.000306778456517453),
(short)(32768 * -0.000270745067644623),
(short)(32768 * -0.000249620856480216),
(short)(32768 * -0.000220358422272639000),
(short)(32768 * -0.000178283990611435),
};

const short Hilbert_Minus_45_Coeffs[NUM_HILBERT_COEFFS] = {
(short)(32768 * -0.000178283990611436), 
(short)(32768 * -0.000220358422272644),
(short)(32768 * -0.000249620856480226),
(short)(32768 * -0.000270745067644634),
(short)(32768 * -0.000306778456517456),
(short)(32768 * -0.000403299387333353),
(short)(32768 * -0.000624975669045577),
(short)(32768 * -0.001041873927200345),
(short)(32768 * -0.001705906352533420),
(short)(32768 * -0.002621972625618107),
(short)(32768 * -0.003722447168239188),
(short)(32768 * -0.004856081867239669),
(short)(32768 * -0.005801655201517424),
(short)(32768 * -0.006311999590759096),
(short)(32768 * -0.006185747399762651),
(short)(32768 * -0.005353975172549197),
(short)(32768 * -0.003959730656158292),
(short)(32768 * -0.002403463761614686),
(short)(32768 * -0.001329366926758052),
(short)(32768 * -0.001537721831848083),
(short)(32768 * -0.003825446520436358),
(short)(32768 * -0.008777661819715581),
(short)(32768 * -0.016552007744388125),
(short)(32768 * -0.026708902650398218),
(short)(32768 * -0.038140342465775433),
(short)(32768 * -0.049135236893170994),
(short)(32768 * -0.057592344202839631),
(short)(32768 * -0.061357904565856464),
(short)(32768 * -0.058631997671258360),
(short)(32768 * -0.048364086483233870),
(short)(32768 * -0.030551195192103930),
(short)(32768 * -0.006365101806074743),
(short)(32768 * 0.021933662563144312),
(short)(32768 * 0.051293625327069131),
(short)(32768 * 0.078334626955950518),
(short)(32768 * 0.099889668870028217),
(short)(32768 * 0.113521768501834860),
(short)(32768 * 0.117914393674033446),
(short)(32768 * 0.113059746959382076),
(short)(32768 * 0.100211560649166906),
(short)(32768 * 0.081618252179499598),
(short)(32768 * 0.060096847866640407),
(short)(32768 * 0.038537923280395278),
(short)(32768 * 0.019440673287420517),
(short)(32768 * 0.004564114354750423),
(short)(32768 * -0.005250377124683159),
(short)(32768 * -0.010070233160009257),
(short)(32768 * -0.010701388680801569),
(short)(32768 * -0.008411605699113688),
(short)(32768 * -0.004617356732844640),
(short)(32768 * -0.000601873045715285),
(short)(32768 * 0.002684917857602387),
(short)(32768 * 0.004719490953542407),
(short)(32768 * 0.005393023804154996),
(short)(32768 * 0.004919290372384100),
(short)(32768 * 0.003699317878546716),
(short)(32768 * 0.002181855899770600),
(short)(32768 * 0.000751166835621515),
(short)(32768 * -0.000339739120777163),
(short)(32768 * -0.000987123427391280),
(short)(32768 * -0.001214793584591868),
(short)(32768 * -0.001128869571306359),
(short)(32768 * -0.000866732397801929),
(short)(32768 * -0.000554463834139796),
(short)(32768 * -0.000280899822020784),
(short)(32768 * -0.000089756932471884),
(short)(32768 * 0.000013929445802507),
(short)(32768 * 0.000049535194963339),
(short)(32768 * 0.000045350266683589),
(short)(32768 * 0.000026665388842510),
};

AudioInputI2S audioInput;
AudioOutputI2S audioOutput;
AudioAnalyzeFFT1024 FFT;

//AudioFilterBiquad LPF_2800;
AudioFilterFIR RX_Hilbert_Plus_45;
AudioFilterFIR RX_Hilbert_Minus_45;
AudioMixer4 RX_Summer;

AudioConnection patchCord10(audioInput, 0, RX_Hilbert_Plus_45, 0);
AudioConnection patchCord11(audioInput, 1, RX_Hilbert_Minus_45, 0);
AudioConnection patchCord20(RX_Hilbert_Plus_45, 0, RX_Summer, 0);
AudioConnection patchCord21(RX_Hilbert_Minus_45, 0, RX_Summer, 1);
AudioConnection patchCord22(RX_Summer, 0, FFT, 0);
AudioConnection patchCord98(RX_Summer, 0, audioOutput, 0);
AudioConnection patchCord99(RX_Summer, 0, audioOutput, 1);

AudioControlSGTL5000 audioShield;


void setup()
{
  delay(1000);

  // Set up input switches
  pinMode(rotAPin, INPUT);
  pinMode(rotBPin, INPUT);
  pinMode(pushPin, INPUT);
  digitalWrite(rotAPin, HIGH);
  digitalWrite(rotBPin, HIGH);
  digitalWrite(pushPin, HIGH);

  // Set up interrupt pins
  attachInterrupt(digitalPinToInterrupt(rotAPin), ISRrotAChange, CHANGE);
  attachInterrupt(digitalPinToInterrupt(rotBPin), ISRrotBChange, CHANGE);

  // Initialise the lcd
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);



  // Initialise the DDS
  si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);

  GetPLLFreq();

  si5351.set_pll(pll_freq, SI5351_PLLA);
  
  si5351.set_ms_source(SI5351_CLK0, SI5351_PLLA);
  si5351.set_ms_source(SI5351_CLK1, SI5351_PLLA);

  si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_2MA);
  si5351.drive_strength(SI5351_CLK1, SI5351_DRIVE_2MA);

  si5351.set_freq_manual(freq, pll_freq, SI5351_CLK0);
  si5351.set_freq_manual(freq, pll_freq, SI5351_CLK1);
  
  si5351.set_phase(SI5351_CLK0, 0);
  si5351.set_phase(SI5351_CLK1, multiple);
  
  si5351.pll_reset(SI5351_PLLA);
  

  AudioMemory(16);
  AudioNoInterrupts();
  audioShield.enable();
  //audioShield.adcHighPassFilterDisable(); // cuts out that squeal/whine
  audioShield.inputSelect(AUDIO_INPUT_LINEIN);
  audioShield.lineInLevel(5);
  audioShield.volume(0.8);
  audioShield.unmuteHeadphone();
  RX_Hilbert_Plus_45.begin(Hilbert_Plus_45_Coeffs, NUM_HILBERT_COEFFS);
  RX_Hilbert_Minus_45.begin(Hilbert_Minus_45_Coeffs, NUM_HILBERT_COEFFS);
  
  RX_Summer.gain(0, 40); 
  RX_Summer.gain(1, 40);

  FFT.windowFunction(AudioWindowHanning1024);

  AudioInterrupts();
  UpdateDisplay();

}


void loop()
{
  
  if (freq != oldfreq)
  {
    
    SendFrequency();
    oldfreq = freq;
  }

  UpdateDisplay();
}


// Interrupt routines
void ISRrotAChange()
{
  if (digitalRead(rotAPin))
  {
    rotAval = 1;
    UpdateRot();
  }
  else
  {
    rotAval = 0;
    UpdateRot();
  }
}


void ISRrotBChange()
{
  if (digitalRead(rotBPin))
  {
    rotBval = 1;
    UpdateRot();
  }
  else
  {
    rotBval = 0;
    UpdateRot();
  }
}


void UpdateRot()
{
  switch (rotState)
  {

    case 0:                                         // Idle state, look for direction
      if (!rotBval)
        rotState = 1;                               // CW 1
      if (!rotAval)
        rotState = 11;                              // CCW 1
      break;

    case 1:                                         // CW, wait for A low while B is low
      if (!rotBval)
      {
        if (!rotAval)
        {
          // either increment radixindex or freq
          if (digitalRead(pushPin) == LOW)
          {
            updatedisplay = 1;
            if (radix == 100000000)
              radix = 10000000;
            else if (radix == 10000000)
              radix = 1000000;
            else if (radix == 1000000)
              radix = 100000;
            else if (radix == 100000)
              radix = 10000;
            else if (radix == 10000)
              radix = 1000;
            else if (radix == 1000)
              radix = 100;
            else
              radix = 100000000;
          }
          else
          {
            freq = (freq + radix);
            if (freq > BAND_END)
              freq = BAND_END;
          }
          rotState = 2;                             // CW 2
        }
      }
      else if (rotAval)
        rotState = 0;                               // It was just a glitch on B, go back to start
      break;

    case 2:                                         // CW, wait for B high
      if (rotBval)
        rotState = 3;                               // CW 3
      break;

    case 3:                                         // CW, wait for A high
      if (rotAval)
        rotState = 0;                               // back to idle (detent) state
      break;

    case 11:                                        // CCW, wait for B low while A is low
      if (!rotAval)
      {
        if (!rotBval)
        {
          // either decrement radixindex or freq
          if (digitalRead(pushPin) == LOW)
          {
            updatedisplay = 1;
            if (radix == 100)
              radix = 1000;
            else if (radix == 1000)
              radix = 10000;
            else if (radix == 10000)
              radix = 100000;
            else if (radix == 100000)
              radix = 1000000;
            else if (radix == 1000000)
              radix = 10000000;
            else if (radix == 10000000)
              radix = 100000000;
            else
              radix = 100;
          }
          else
          {
            freq = (freq - radix);
            if (freq < BAND_START)
              freq = BAND_START;
          }
          rotState = 12;                            // CCW 2
        }
      }
      else if (rotBval)
        rotState = 0;                               // It was just a glitch on A, go back to start
      break;

    case 12:                                        // CCW, wait for A high
      if (rotAval)
        rotState = 13;                              // CCW 3
      break;

    case 13:                                        // CCW, wait for B high
      if (rotBval)
        rotState = 0;                               // back to idle (detent) state
      break;
  }
}


void UpdateDisplay()
{
  
  char freq_disp[7];
  sprintf(freq_disp, "%llu", (freq / 100));
  int font_width = 30;
  
  //tft.setCursor(0, 1);
  // if using free font, then setTextColor wont redraw background
  //tft.setFont(Arial_40);
  tft.setTextSize(5);
 
  for ( int i = 0; i < 7; i++ ) {
    tft.setCursor(font_width * i, 1);
    tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
    if (i == 0 && radix == 100000000)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
    if (i == 1 && radix == 10000000)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);    
    if (i == 2 && radix == 1000000)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);    
    if (i == 3 && radix == 100000)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);    
    if (i == 4 && radix == 10000)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);    
    if (i == 5 && radix == 1000)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);    
    if (i == 6 && radix == 100)  
      tft.setTextColor(ILI9341_RED, ILI9341_BLACK);    
    
          
    tft.print(freq_disp[i]);
  }
    
  
  
  if (FFT.available()) {


    int fft_x_min = 240; // bottom of fft line
    int fft_x_max = 90; //140; // top of fft line
  
    int scale = 511 / (fft_x_min - fft_x_max); // 3;
    int x_pos = 0;
    int bin_no = 0;
    uint16_t fft_color = ILI9341_CYAN;
    uint16_t fft_bg_color = ILI9341_BLACK;
  

    
    for (bin_no = 0; bin_no < 511; bin_no = bin_no + 2) {
      int bar = abs(FFT.output[bin_no]) / scale;
      if (bar > (fft_x_min - fft_x_max)) {
        bar = (fft_x_min - fft_x_max);
      }


      // 240 = fft x min
      // 140 = fft x max
      // 240 - height of bar = top of line
      
      if (x_pos != 159 && x_pos != 160) {
        
        // x, y, h, color
        tft.drawFastVLine(x_pos,        fft_x_min - bar,          bar, fft_color);
        tft.drawFastVLine(x_pos + 1,    fft_x_min - bar,          bar, fft_color);
   
      
      
        tft.drawFastVLine(x_pos,        fft_x_max,  abs(fft_x_max - (fft_x_min - bar)), fft_bg_color);
        tft.drawFastVLine(x_pos + 1,    fft_x_max , abs(fft_x_max - (fft_x_min - bar)), fft_bg_color);
      }
      
      tft.drawFastHLine(0, fft_x_max, 320, ILI9341_YELLOW);
      tft.drawFastVLine(160, fft_x_max, 150, ILI9341_YELLOW);
      
      x_pos = x_pos + 4;
      
    }

    
  }

  
 
}


void GetPLLFreq() {

    float64_t f_pll_freq;                               // floating point pll_freq

    for (int i = 10; i <= 200; i = i + 2) {             // loop through even numbers from 10 to 200
        f_pll_freq = freq * i;                          // set f_pll_freq
        if (f_pll_freq >= PLL_MIN) {                    // 
            if (f_pll_freq <= PLL_MAX) {                // if f_pll_freq between PLL_MIN & PLL_MAX
               if (f_pll_freq == floor(f_pll_freq)) {   // if f_pll_freq is a whole integer
                  pll_freq = f_pll_freq;                // set pll_freq = f_pll_freq
                  multiple = pll_freq/freq;             // set multiple 
                  break;                                // exit loop & function
              }
            }
        }
    } 
}


void SendFrequency()
{

  GetPLLFreq();

  si5351.set_pll(pll_freq, SI5351_PLLA);                // set pll frequency 

  si5351.set_freq_manual(freq, pll_freq, SI5351_CLK0);  // manually set clock frequency and pll frequency for CLK0
  si5351.set_freq_manual(freq, pll_freq, SI5351_CLK1);  // manually set clock frequency and pll frequency for CLK1

  si5351.set_phase(SI5351_CLK0, 0);                     // set CLK0 phase to 0
  si5351.set_phase(SI5351_CLK1, multiple);              // set CLK1 phase to multiple for 90 degree phase shift
  
  si5351.pll_reset(SI5351_PLLA);                        // reset pll to lock in phase alignment
}

 

3 Comments

    • awsh awsh

      Thanks for the links Charlie. I’ve been away for a bit, so I hadn’t seen those yet.

      I’ve done some testing with an adafruit breakout board and I don’t think it will make it down below about 4.7MHz with the quadrature outputs since it uses a 25MHz reference oscillator. Hans is using a 27MHz oscillator with his QCX and in the PDF you linked, he states:

      with a 27MHz reference oscillator crystal, the lowest frequency at which the Si5351A configuration supports quadrature LO on two outputs, is 3.2MHz.

      I’ve been searching through the junk box for a 27MHz crystal so I can test that, but no luck so far.

      I’ll definitely let you know if I get it working.

      • Charles T Morris Charles T Morris

        That very interesting thanks. Hopefully, between the two of us we can nut it out!

        73s
        Charlie, ZL2CTM

Comments are closed, but trackbacks and pingbacks are open.