Skip to content

Instantly share code, notes, and snippets.

@NT7S
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NT7S/9e0f582850be7c2e3384 to your computer and use it in GitHub Desktop.
Save NT7S/9e0f582850be7c2e3384 to your computer and use it in GitHub Desktop.
Si5351A Breakout Board Test Code
/*
* si5351example.ino - Simple example of using Si5351Arduino library
*
* Copyright (C) 2015 Jason Milldrum <milldrum@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "si5351.h"
#include "Wire.h"
#define MUX_IN1 6
#define MUX_IN2 7
#define RAIL_3V3 A0
Si5351 si5351;
enum si5351_clock output_clk = SI5351_CLK0;
long a;
int in_data;
uint16_t adc_val;
void mux_clk0(void)
{
digitalWrite(MUX_IN1, LOW);
digitalWrite(MUX_IN2, LOW);
}
void mux_clk1(void)
{
digitalWrite(MUX_IN1, HIGH);
digitalWrite(MUX_IN2, LOW);
}
void mux_clk2(void)
{
digitalWrite(MUX_IN1, LOW);
digitalWrite(MUX_IN2, HIGH);
}
void setup()
{
// Start mux on CLK0 output
pinMode(MUX_IN1, OUTPUT);
pinMode(MUX_IN2, OUTPUT);
mux_clk0();
// Setup analog read on RAIL_3V3
pinMode(RAIL_3V3, INPUT);
// Start serial and initialize the Si5351
Serial.begin(9600);
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
// Set CLK default frequencies
// Set CLK0 to output 14 MHz
//si5351.set_freq(1400000000ULL, 0ULL, SI5351_CLK0);
// Set CLK1 to output 20 MHz
//si5351.set_freq(2000000000ULL, 0ULL, SI5351_CLK1);
//si5351.set_freq(1111100000ULL, 0ULL, SI5351_CLK2);
}
void loop()
{
// Serial command interface
//
// I
// Initialize the Si5351A
//
// T<freq>
// Set the current CLK output to the integer frequency in <freq>
//
// C<clk>
// Set the mux output to the CLK output specified in <clk>
//
// V
// Return the ADC reading on the Si5351A BB 3.3V rail
while (Serial.available() > 0) // see if incoming serial data:
{
in_data = Serial.read(); // read oldest byte in serial buffer:
switch(in_data)
{
case 'I':
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
break;
case 'T':
a = Serial.parseInt();
// Tune current output clock
si5351.set_freq(a * 100ULL, 0ULL, output_clk);
break;
case 'C':
in_data = Serial.read();
switch(in_data)
{
case '0':
mux_clk0();
output_clk == SI5351_CLK0;
break;
case '1':
mux_clk1();
output_clk == SI5351_CLK1;
break;
case '2':
mux_clk2();
output_clk == SI5351_CLK2;
break;
default:
break;
}
break;
case 'V':
adc_val = analogRead(RAIL_3V3);
Serial.println(adc_val);
break;
default:
break;
}
}
}
import socket
import serial
import time
import sys
import math
# ANSI escape codes for pretty printing
class termcolors:
FG_BLUE = '\033[94m'
FG_YELLOW = '\033[93m'
FG_GREEN = '\033[92m'
FG_RED = '\033[91m'
FG_WHITE = '\033[97m'
FG_BLACK = '\033[30m'
BG_GREEN = '\033[42m'
BG_RED = '\033[41m'
BG_BLACK = '\033[40m'
BOLD = '\033[1m'
NORMAL = '\033[0m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
# The list of frequencies to check for each CLK output
freq = [140000000, 20000000, 11111000]
# Test boundaries
AMP_UPPER = 15.0
AMP_LOWER = -5.0
VOLT_UPPER = 3.4
VOLT_LOWER = 3.2
# Constants for the LXI interfaces to the instruments
rigol_port = 5555
dsa815_addr = '192.168.1.60'
ds1052z_addr = '192.168.1.61'
# Create socket objects for the LXI interfaces
dsa815 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#ds1052z = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
dsa815.connect((dsa815_addr, rigol_port))
except:
print 'Cannot open DSA815 LXI connection'
sys.exit(0)
#ds1052z.connect((ds1052z_addr, rigol_port))
# Say hi
print('\nSi5351A Breakout Board Test')
print('===========================')
# Arduino serial dev paramaters
DEVICE = '/dev/ttyACM0'
BAUD = 9600
# Open serial port
try:
ser = serial.Serial(port=DEVICE, baudrate=BAUD, timeout=2)
except:
print 'Cannot open serial port'
sys.exit(0)
# The main loop
keep_going = True
while keep_going:
print('')
# Init the Si5351A
ser.write('I')
# Preset
dsa815.send(":SYST:PRES\n")
time.sleep(2)
# Set span to 100 kHz
dsa815.send(":FREQuency:SPAN 100000\n")
time.sleep(1)
# Set ampitude ref to 20 dBm
dsa815.send(":DISP:WIN:TRAC:Y:SCAL:RLEV 20\n")
time.sleep(1)
# Check that all of the outputs are working
for clk in range(0, 3):
ser.write('C' + str(clk))
ser.write('T' + str(freq[clk]))
time.sleep(1)
dsa815.send(':FREQuency:CENTER ' + str(freq[clk]) + '\n')
time.sleep(1)
dsa815.send(':CALCulate:MARKer1:MAXimum:MAX\n')
time.sleep(1)
dsa815.send(':CALCulate:MARKer1:Y?\n')
amplitude = dsa815.recv(1024)
if(float(amplitude) < AMP_UPPER and float(amplitude) > AMP_LOWER):
alert_color_fg = termcolors.FG_GREEN
alert_color_bg = termcolors.BG_GREEN
condition = 'PASS' + termcolors.NORMAL
else:
alert_color_fg = termcolors.FG_RED
alert_color_bg = termcolors.BG_RED
condition = 'FAIL' + termcolors.NORMAL
print('{}{} CLK{:1}: {}{}{:+6.2f} dBm {}{}{}{}{}'.format(termcolors.BOLD, termcolors.FG_WHITE, str(clk), termcolors.NORMAL, alert_color_fg, float(amplitude), termcolors.NORMAL, termcolors.FG_BLACK, alert_color_bg, condition, termcolors.NORMAL))
# Get the 3.3V rail
ser.write('V')
adc_val = ser.readline()
rail_3v3 = (int(adc_val) * 5.0) / 1024.0
if(rail_3v3 < VOLT_UPPER and rail_3v3 > VOLT_LOWER):
alert_color_fg = termcolors.FG_GREEN
alert_color_bg = termcolors.BG_GREEN + termcolors.FG_BLACK
condition = 'PASS' + termcolors.NORMAL
else:
alert_color_fg = termcolors.FG_RED
alert_color_bg = termcolors.BG_RED + termcolors.FG_BLACK
condition = 'FAIL' + termcolors.NORMAL
print('{}{}3.3V Rail: {}{}{:.2f}V {}{}{}'.format(termcolors.BOLD, termcolors.FG_WHITE, termcolors.NORMAL, alert_color_fg, rail_3v3, termcolors.NORMAL, alert_color_bg, condition))
print('------------------------------')
# Give option to continue
while True:
i = raw_input('[Enter] to continue or [q] to quit: ')
if(i == 'q'):
keep_going = False
# Close the LXI interfaces
dsa815.close
#ds1052z.close
break
else:
keep_going = True;
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment