2016-09-17 20:26:03 +02:00
|
|
|
/*
|
2016-09-20 19:49:58 +02:00
|
|
|
* Author: klaute -Kai Lauterbach - @kailauterbach - me@klaute.de
|
|
|
|
* Date: 09/2016
|
|
|
|
* License: GPLv3
|
2016-09-17 20:26:03 +02:00
|
|
|
*/
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2024-02-14 16:42:47 +01:00
|
|
|
#include <Adafruit_MCP4725.h>
|
2016-09-17 20:50:47 +02:00
|
|
|
#include <si5351.h>
|
2024-02-14 16:42:47 +01:00
|
|
|
|
2016-09-17 20:26:03 +02:00
|
|
|
#include "Wire.h"
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
#include <EEPROM.h>
|
|
|
|
|
2016-09-18 08:05:46 +02:00
|
|
|
extern "C" {
|
|
|
|
#include "globals.h"
|
2024-02-11 21:56:13 +01:00
|
|
|
#include "waveforms.h"
|
2016-09-18 08:05:46 +02:00
|
|
|
}
|
2016-09-17 20:26:03 +02:00
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-17 20:26:03 +02:00
|
|
|
Si5351 si5351;
|
2024-02-14 16:42:47 +01:00
|
|
|
Adafruit_MCP4725 dac;
|
2016-09-17 20:26:03 +02:00
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
uint32_t start_freq = 10000;
|
|
|
|
uint32_t end_freq = 50000000;
|
|
|
|
uint32_t step_freq = 1000000; // 1 MHz default step size
|
|
|
|
uint16_t intervall = 1000; // intervall to change the frequency as milli seconds
|
2024-02-14 16:42:47 +01:00
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
enum si5351_drive drive_str = SI5351_DRIVE_2MA;
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-17 20:26:03 +02:00
|
|
|
void setup()
|
|
|
|
{
|
2016-09-27 16:30:52 +02:00
|
|
|
// manage the analog pins
|
|
|
|
pinMode(A0, INPUT); // forward SWR measurement
|
|
|
|
pinMode(A1, INPUT); // backward SWR measurement
|
|
|
|
pinMode(A2, OUTPUT);
|
|
|
|
pinMode(A3, OUTPUT);
|
|
|
|
pinMode(A6, OUTPUT);
|
|
|
|
pinMode(A7, OUTPUT);
|
|
|
|
|
|
|
|
digitalWrite(A2, LOW);
|
|
|
|
digitalWrite(A3, LOW);
|
|
|
|
digitalWrite(A6, LOW);
|
|
|
|
digitalWrite(A7, LOW);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
// Init the serial connection
|
2016-09-20 13:08:56 +02:00
|
|
|
Serial.begin(115200);
|
2016-09-18 11:41:59 +02:00
|
|
|
|
|
|
|
// initialize the command control module
|
|
|
|
cc_init();
|
|
|
|
|
2024-02-14 16:42:47 +01:00
|
|
|
if (dac.begin(0x62))
|
|
|
|
{
|
|
|
|
bool tmp = dac.setVoltage(500, false, 100000);
|
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
blinkCode(150);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
blinkCode(500);
|
|
|
|
}
|
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
// init the Si5351
|
2016-09-17 20:26:03 +02:00
|
|
|
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
|
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
si5351.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
|
|
|
|
|
|
|
|
si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
|
|
|
|
si5351.output_enable(SI5351_CLK1, 0); // disable clock output 1
|
|
|
|
si5351.output_enable(SI5351_CLK2, 0); // disable clock output 2
|
|
|
|
|
2016-09-27 16:30:52 +02:00
|
|
|
analogReference(DEFAULT); // 5V reference
|
|
|
|
|
2024-02-11 12:50:31 +01:00
|
|
|
initWaveformGenerator();
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
readEEPValues();
|
|
|
|
|
2016-09-17 20:26:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-17 20:26:03 +02:00
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// Read the Status Register and print it every 10 seconds
|
|
|
|
si5351.update_status();
|
2016-09-18 11:41:59 +02:00
|
|
|
|
|
|
|
/*
|
2016-09-17 20:26:03 +02:00
|
|
|
Serial.print("SYS_INIT: ");
|
|
|
|
Serial.print(si5351.dev_status.SYS_INIT);
|
|
|
|
Serial.print(" LOL_A: ");
|
|
|
|
Serial.print(si5351.dev_status.LOL_A);
|
|
|
|
Serial.print(" LOL_B: ");
|
|
|
|
Serial.print(si5351.dev_status.LOL_B);
|
|
|
|
Serial.print(" LOS: ");
|
|
|
|
Serial.print(si5351.dev_status.LOS);
|
|
|
|
Serial.print(" REVID: ");
|
|
|
|
Serial.println(si5351.dev_status.REVID);
|
2016-09-18 11:41:59 +02:00
|
|
|
*/
|
2016-09-17 20:26:03 +02:00
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
if (Serial.available() > 0)
|
|
|
|
{
|
|
|
|
uint8_t c = Serial.read() & 0xff;
|
2016-09-25 17:44:41 +02:00
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
cc_processData(c);
|
|
|
|
}
|
|
|
|
|
2024-02-11 12:50:31 +01:00
|
|
|
pollWaveformGenerator();
|
|
|
|
|
2016-10-14 21:25:31 +02:00
|
|
|
//delay(100);
|
2024-02-11 21:56:13 +01:00
|
|
|
//delay(10);
|
|
|
|
delayMicroseconds(MAIN_LOOP_DELAY_US);
|
2016-09-17 20:26:03 +02:00
|
|
|
}
|
2016-09-27 16:30:52 +02:00
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-10-14 09:46:04 +02:00
|
|
|
void write32BitEEPROM(uint8_t addr, uint32_t value)
|
2016-09-28 14:43:05 +02:00
|
|
|
{
|
2016-10-14 09:46:04 +02:00
|
|
|
EEPROM.write(addr , (uint8_t)((value & 0xff000000) >> 24));
|
|
|
|
EEPROM.write(addr + 1, (uint8_t)((value & 0x00ff0000) >> 16));
|
|
|
|
EEPROM.write(addr + 2, (uint8_t)((value & 0x0000ff00) >> 8));
|
|
|
|
EEPROM.write(addr + 3, (uint8_t) (value & 0x000000ff));
|
|
|
|
}
|
|
|
|
|
|
|
|
void write16BitEEPROM(uint8_t addr, uint16_t value)
|
|
|
|
{
|
|
|
|
EEPROM.write(addr , (uint8_t)((value & 0xff00) >> 8));
|
|
|
|
EEPROM.write(addr + 1, (uint8_t) (value & 0x00ff));
|
|
|
|
}
|
2016-09-28 14:43:05 +02:00
|
|
|
|
2016-10-14 09:46:04 +02:00
|
|
|
void writeEEPROMConfig()
|
|
|
|
{
|
|
|
|
write32BitEEPROM( 0, start_freq);
|
|
|
|
write32BitEEPROM( 4, end_freq);
|
|
|
|
write32BitEEPROM( 8, step_freq);
|
|
|
|
write16BitEEPROM(12, intervall);
|
|
|
|
EEPROM.write(14, (uint8_t)drive_str);
|
|
|
|
}
|
2016-09-28 14:43:05 +02:00
|
|
|
|
2016-10-14 09:46:04 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
uint32_t read32BitEEPROM(uint8_t addr)
|
|
|
|
{
|
|
|
|
uint32_t tmp = (uint32_t)EEPROM.read(addr ) << 24;
|
|
|
|
tmp += (uint32_t)EEPROM.read(addr + 1) << 16;
|
|
|
|
tmp += (uint32_t)EEPROM.read(addr + 2) << 8;
|
|
|
|
tmp += (uint32_t)EEPROM.read(addr + 3);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t read16BitEEPROM(uint8_t addr)
|
|
|
|
{
|
|
|
|
uint16_t tmp = (uint16_t)EEPROM.read(addr ) << 8;
|
|
|
|
tmp += (uint16_t)EEPROM.read(addr + 1);
|
2016-10-14 21:15:43 +02:00
|
|
|
|
|
|
|
return tmp;
|
2016-09-28 14:43:05 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 09:46:04 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
void readEEPValues()
|
|
|
|
{
|
2016-10-14 09:46:04 +02:00
|
|
|
uint32_t tmp_start_freq = read32BitEEPROM(0);
|
|
|
|
|
|
|
|
start_freq = keepFreqRange(tmp_start_freq);
|
|
|
|
|
|
|
|
uint32_t tmp_end_freq = read32BitEEPROM(4);
|
|
|
|
|
|
|
|
end_freq = keepFreqRange(tmp_end_freq);
|
|
|
|
|
|
|
|
uint32_t tmp_step_freq = read32BitEEPROM(8);
|
|
|
|
|
|
|
|
step_freq = keepFreqRange(tmp_step_freq);
|
|
|
|
|
|
|
|
uint16_t tmp_intervall = read16BitEEPROM(12);
|
2016-09-28 14:43:05 +02:00
|
|
|
|
|
|
|
intervall = tmp_intervall;
|
|
|
|
|
|
|
|
enum si5351_drive tmp_ds = (enum si5351_drive)EEPROM.read(14);
|
|
|
|
if (tmp_ds == SI5351_DRIVE_2MA ||
|
|
|
|
tmp_ds == SI5351_DRIVE_4MA ||
|
|
|
|
tmp_ds == SI5351_DRIVE_6MA ||
|
|
|
|
tmp_ds == SI5351_DRIVE_8MA)
|
|
|
|
{
|
|
|
|
drive_str = tmp_ds;
|
|
|
|
} else {
|
|
|
|
drive_str = SI5351_DRIVE_2MA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-10-14 09:46:04 +02:00
|
|
|
uint32_t keepFreqRange(uint32_t freq)
|
|
|
|
{
|
|
|
|
uint32_t f = freq;
|
|
|
|
|
|
|
|
if (freq < 1)
|
|
|
|
f = 1;
|
|
|
|
else if (freq > 150000000)
|
|
|
|
f = 150000000;
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2024-02-14 16:42:47 +01:00
|
|
|
|
|
|
|
void blinkCode(uint16_t d)
|
|
|
|
{
|
|
|
|
pinMode(13, OUTPUT);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
digitalWrite(13, HIGH);
|
|
|
|
delay(d);
|
|
|
|
digitalWrite(13, LOW);
|
|
|
|
delay(d);
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_NOK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|