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.   /images/teensy_sdr_1_web.jpg

/images/teensy_sdr_2_web-1.jpg

/images/teensy_sdr_3_web-1.jpg

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

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#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
}
```