SWRMeter/firmware/firmware.ino

254 lines
5.7 KiB
C++

/*
* Author: klaute -Kai Lauterbach - @kailauterbach - me@klaute.de
* Date: 09/2016
* License: GPLv3
*/
/*****************************************************************************/
#include <si5351.h>
#include "Wire.h"
#include <EEPROM.h>
extern "C" {
#include "globals.h"
#include "waveforms.h"
}
/*****************************************************************************/
Si5351 si5351;
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
enum si5351_drive drive_str = SI5351_DRIVE_2MA;
/*****************************************************************************/
void setup()
{
// 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);
// Init the serial connection
Serial.begin(115200);
// initialize the command control module
cc_init();
Wire.begin();
uint8_t tmp = dac_setVoltage(MCP4725_I2C_ADDRESS, 0);
if (tmp > 0)
{
errorCode(tmp);
}
// init the Si5351
si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
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
analogReference(DEFAULT); // 5V reference
initWaveformGenerator();
readEEPValues();
}
/*****************************************************************************/
void loop()
{
// Read the Status Register and print it every 10 seconds
si5351.update_status();
/*
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);
*/
if (Serial.available() > 0)
{
uint8_t c = Serial.read() & 0xff;
cc_processData(c);
}
pollWaveformGenerator();
//delay(100);
delay(10);
//delayMicroseconds(MAIN_LOOP_DELAY_US);
}
/*****************************************************************************/
void write32BitEEPROM(uint8_t addr, uint32_t value)
{
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));
}
void writeEEPROMConfig()
{
write32BitEEPROM( 0, start_freq);
write32BitEEPROM( 4, end_freq);
write32BitEEPROM( 8, step_freq);
write16BitEEPROM(12, intervall);
EEPROM.write(14, (uint8_t)drive_str);
}
/*****************************************************************************/
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);
return tmp;
}
/*****************************************************************************/
void readEEPValues()
{
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);
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;
}
}
/*****************************************************************************/
uint32_t keepFreqRange(uint32_t freq)
{
uint32_t f = freq;
if (freq < 1)
f = 1;
else if (freq > 150000000)
f = 150000000;
return f;
}
/*****************************************************************************/
void errorCode2(uint16_t d)
{
pinMode(13, OUTPUT);
while (true)
{
digitalWrite(13, HIGH);
delay(d);
digitalWrite(13, LOW);
delay(d);
Serial.write(MSG_TYPE_ANSWER_NOK);
}
}
void errorCode(uint16_t e)
{
pinMode(13, OUTPUT);
for (uint8_t j = 0; j < 2; j++)
{
for (uint8_t i = 0; i < e; i++)
{
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
Serial.write(MSG_TYPE_ANSWER_NOK);
}
delay(1000);
}
}
/*****************************************************************************/
uint8_t dac_setVoltage(uint8_t addr, uint16_t v)
{
// Example:
// 2048 = 0x1000_0000_0000 => 2048 >> 4 = 0x0000_1000_0000
// => 2048 << 4 = 0x1000_0000_0000
byte buffer[3];
buffer[0] = MCP4725_CMD_WRITEDAC;
buffer[1] = v >> 4; // MSB
buffer[2] = v << 4; // LSB
Wire.beginTransmission(addr);
Wire.write(buffer[0]);
Wire.write(buffer[1]);
Wire.write(buffer[2]);
return Wire.endTransmission();
}