2016-09-17 19:02:36 +02:00
|
|
|
/*
|
|
|
|
* Author: klaute -Kai Lauterbach - @kailauterbach - me@klaute.de
|
2016-09-18 11:41:59 +02:00
|
|
|
* Date: 09/2016
|
2016-09-17 19:02:36 +02:00
|
|
|
* License: GPLv3
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-09-17 19:02:36 +02:00
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
#include <avr/wdt.h>
|
2016-09-17 19:02:36 +02:00
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_init(void);
|
|
|
|
void cc_abort(void);
|
|
|
|
void cc_processData(uint8_t);
|
|
|
|
void cc_clearReadDataBuffer(void);
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_setStartFreq(void);
|
|
|
|
void cc_setEndFreq(void);
|
|
|
|
void cc_setIntervall(void);
|
2016-09-19 21:09:21 +02:00
|
|
|
void cc_setFreqStep(void);
|
2016-09-18 11:41:59 +02:00
|
|
|
void cc_setDriveStrength(void);
|
|
|
|
void cc_startMeasurement(void);
|
2016-09-19 21:09:21 +02:00
|
|
|
void cc_getConfig(void);
|
2016-09-28 14:43:05 +02:00
|
|
|
void cc_enableClk(void);
|
|
|
|
void cc_disableClk(void);
|
|
|
|
void cc_saveDefaults(void);
|
2016-10-12 20:32:46 +02:00
|
|
|
void cc_setClkCorrection(void);
|
|
|
|
void cc_getClkCorrection(void);
|
2016-09-18 11:41:59 +02:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "globals.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
uint8_t cc_commands[] = {
|
|
|
|
CC_CMD_SET_START_FREQ,
|
|
|
|
CC_CMD_SET_END_FREQ,
|
|
|
|
CC_CMD_SET_INTERVALL,
|
|
|
|
CC_CMD_SET_DRIVE_STRENGTH,
|
2016-09-19 21:09:21 +02:00
|
|
|
CC_CMD_SET_FREQ_STEP,
|
2016-09-18 11:41:59 +02:00
|
|
|
CC_CMD_START_MEASUREMENT,
|
2016-09-19 21:09:21 +02:00
|
|
|
CC_CMD_GET_CONFIG,
|
2016-09-28 14:43:05 +02:00
|
|
|
CC_CMD_EN_CLK,
|
|
|
|
CC_CMD_DIS_CLK,
|
|
|
|
CC_CMD_SAV_DFLT,
|
2016-10-12 20:32:46 +02:00
|
|
|
CC_CMD_SET_CLK_CORR,
|
|
|
|
CC_CMD_GET_CLK_CORR,
|
2024-02-12 11:07:33 +01:00
|
|
|
CC_CMD_SET_WF_FREQ,
|
|
|
|
CC_CMD_SET_WF,
|
|
|
|
CC_CMD_SET_WF_DC,
|
|
|
|
CC_CMD_EN_WF,
|
2016-09-18 11:41:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void (*cc_cmd_functions[])() = {
|
|
|
|
CC_CMD_SET_START_FREQ_FUNC,
|
|
|
|
CC_CMD_SET_END_FREQ_FUNC,
|
|
|
|
CC_CMD_SET_INTERVALL_FUNC,
|
|
|
|
CC_CMD_SET_DRIVE_STRENGTH_FUNC,
|
2016-09-19 21:09:21 +02:00
|
|
|
CC_CMD_SET_FREQ_STEP_FUNC,
|
2016-09-18 11:41:59 +02:00
|
|
|
CC_CMD_START_MEASUREMENT_FUNC,
|
2016-09-19 21:09:21 +02:00
|
|
|
CC_CMD_GET_CONFIG_FUNC,
|
2016-09-28 14:43:05 +02:00
|
|
|
CC_CMD_EN_CLK_FUNC,
|
|
|
|
CC_CMD_DIS_CLK_FUNC,
|
|
|
|
CC_CMD_SAV_DFLT_FUNC,
|
2016-10-12 20:32:46 +02:00
|
|
|
CC_CMD_SET_CLK_CORR_FUNC,
|
|
|
|
CC_CMD_GET_CLK_CORR_FUNC,
|
2024-02-12 11:07:33 +01:00
|
|
|
CC_CMD_SET_WF_FREQ_FUNC,
|
|
|
|
CC_CMD_SET_WF_FUNC,
|
|
|
|
CC_CMD_SET_WF_DC_FUNC,
|
|
|
|
CC_CMD_SET_EN_WF_FUNC,
|
2016-09-18 11:41:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t cc_cmd_data_to_read[] = {
|
|
|
|
CC_CMD_SET_START_FREQ_DATA_TO_READ,
|
|
|
|
CC_CMD_SET_END_FREQ_DATA_TO_READ,
|
|
|
|
CC_CMD_SET_INTERVALL_DATA_TO_READ,
|
|
|
|
CC_CMD_SET_DRIVE_STRENGTH_DATA_TO_READ,
|
2016-09-19 21:09:21 +02:00
|
|
|
CC_CMD_SET_FREQ_STEP_DATA_TO_READ,
|
2016-09-18 11:41:59 +02:00
|
|
|
CC_CMD_START_MEASUREMENT_DATA_TO_READ,
|
2016-09-19 21:09:21 +02:00
|
|
|
CC_CMD_GET_CONFIG_DATA_TO_READ,
|
2016-09-28 14:43:05 +02:00
|
|
|
CC_CMD_EN_CLK_DATA_TO_READ,
|
|
|
|
CC_CMD_DIS_CLK_DATA_TO_READ,
|
|
|
|
CC_CMD_SAV_DFLT_DATA_TO_READ,
|
2016-10-12 20:32:46 +02:00
|
|
|
CC_CMD_SET_CLK_CORR_DATA_TO_READ,
|
|
|
|
CC_CMD_GET_CLK_CORR_DATA_TO_READ,
|
2024-02-12 11:07:33 +01:00
|
|
|
CC_CMD_SET_WF_FREQ_DATA_TO_READ,
|
|
|
|
CC_CMD_SET_WF_DATA_TO_READ,
|
|
|
|
CC_CMD_SET_WF_DC_DATA_TO_READ,
|
|
|
|
CC_CMD_SET_EN_WF_DATA_TO_READ,
|
2016-09-18 11:41:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t cc_read_data[CC_READ_DATA_MAX];
|
2016-09-17 19:02:36 +02:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
uint8_t cc_state = CC_STATE_READ_SOM1;
|
|
|
|
|
|
|
|
uint8_t cc_cmd_to_call = CC_CMD_NO_CMD;
|
|
|
|
|
|
|
|
uint8_t cc_cmd_received_correct = MSG_INCOMPLETE;
|
|
|
|
|
|
|
|
uint8_t cc_cmd_data_read_cnt = 0;
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
uint32_t read32BitDataFromBuffer(uint8_t pos)
|
|
|
|
{
|
|
|
|
uint32_t tmp = (uint32_t)cc_read_data[pos ] << 24;
|
|
|
|
tmp += (uint32_t)cc_read_data[pos + 1] << 16;
|
|
|
|
tmp += (uint32_t)cc_read_data[pos + 2] << 8;
|
|
|
|
tmp += (uint32_t)cc_read_data[pos + 3];
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t read16BitDataFromBuffer(uint8_t pos)
|
|
|
|
{
|
|
|
|
uint16_t tmp = (uint16_t)cc_read_data[pos ] << 8;
|
|
|
|
tmp += (uint16_t)cc_read_data[pos + 1];
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
void cc_setStartFreq()
|
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
uint32_t tmp_start_freq = read32BitDataFromBuffer(0);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
start_freq = keepFreqRange(tmp_start_freq);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
2016-09-18 11:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cc_setEndFreq()
|
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
uint32_t tmp_end_freq = read32BitDataFromBuffer(0);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
end_freq = keepFreqRange(tmp_end_freq);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
2016-09-18 11:41:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
void cc_setFreqStep()
|
2016-09-18 11:41:59 +02:00
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
uint32_t tmp_step_freq = read32BitDataFromBuffer(0);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
step_freq = keepFreqRange(tmp_step_freq);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
2016-09-18 11:41:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
void cc_setIntervall()
|
2016-09-18 11:41:59 +02:00
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
intervall = read16BitDataFromBuffer(0);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
2016-09-18 11:41:59 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
void cc_setDriveStrength()
|
|
|
|
{
|
|
|
|
enum si5351_drive tmp_ds = (enum si5351_drive)cc_read_data[0];
|
|
|
|
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;
|
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
2016-09-19 21:09:21 +02:00
|
|
|
} else {
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_NOK);
|
|
|
|
sendEOM();
|
2016-09-19 21:09:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 11:41:59 +02:00
|
|
|
void cc_startMeasurement()
|
|
|
|
{
|
2016-09-19 21:09:21 +02:00
|
|
|
|
|
|
|
// 1. send measurement info to host
|
|
|
|
// MSG_TYPE_CONFIG
|
|
|
|
cc_getConfig();
|
|
|
|
|
2016-10-14 10:08:56 +02:00
|
|
|
if (start_freq == 0 || start_freq > 150000000 ||
|
|
|
|
end_freq == 0 || end_freq > 150000000 ||
|
|
|
|
step_freq == 0 || step_freq > 150000000 ||
|
|
|
|
start_freq >= end_freq ||
|
|
|
|
intervall == 0)
|
|
|
|
{
|
|
|
|
// on error
|
|
|
|
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_NOK);
|
|
|
|
sendEOM();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
// 2. start a for loop from the frequence to start to the end frequence
|
2016-10-14 10:08:56 +02:00
|
|
|
si5351.drive_strength(SI5351_CLK0, drive_str); // 2 4 6 8ma
|
|
|
|
|
|
|
|
uint8_t t = 0;
|
|
|
|
for (t = 0; t < 100; t++)
|
2016-09-19 21:09:21 +02:00
|
|
|
{
|
2016-10-14 10:08:56 +02:00
|
|
|
uint16_t tmp = analogRead(A0);
|
|
|
|
tmp = analogRead(A1);
|
|
|
|
}
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:08:56 +02:00
|
|
|
uint32_t freq = 0;
|
|
|
|
for (freq = start_freq; freq <= end_freq; freq += step_freq)
|
|
|
|
{
|
|
|
|
if (freq > end_freq)
|
2016-09-21 18:27:41 +02:00
|
|
|
{
|
2016-10-14 10:08:56 +02:00
|
|
|
// prevent to step over the end frequency
|
|
|
|
// maybe the user is not allowed to send data in the frequency band
|
|
|
|
freq = end_freq;
|
2016-09-21 18:27:41 +02:00
|
|
|
}
|
2016-10-14 10:08:56 +02:00
|
|
|
uint32_t a0_sum = 0;
|
|
|
|
uint32_t a1_sum = 0;
|
|
|
|
uint16_t i = 0;
|
2016-09-21 18:27:41 +02:00
|
|
|
|
2024-02-12 11:07:33 +01:00
|
|
|
// TODO an diesem punkt muss unterschieden werden ob eine waveform ausgegeben werden soll oder eine frequenz, und welche Art von Kurve
|
|
|
|
if (freq < WF_FREQ_MAX_HZ)
|
2024-02-11 12:50:31 +01:00
|
|
|
{
|
2024-02-12 11:07:33 +01:00
|
|
|
if (freq > PWM_MAX_VALUE)
|
|
|
|
{
|
|
|
|
setWaveform(WAVEFORM_SINUS);
|
|
|
|
setWaveformFrequency(freq);
|
|
|
|
} else {
|
|
|
|
setWaveform(WAVEFORM_DUTYCYCLE);
|
|
|
|
setWaveformDC((uint8_t)(freq & 0xff));
|
|
|
|
}
|
2024-02-11 12:50:31 +01:00
|
|
|
enableWaveformOutput();
|
2024-02-12 11:07:33 +01:00
|
|
|
pollWaveformGenerator(); // manually poll the waveformgenerator
|
2024-02-11 12:50:31 +01:00
|
|
|
si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
|
2024-02-12 11:07:33 +01:00
|
|
|
delay(1);
|
2024-02-11 12:50:31 +01:00
|
|
|
} else {
|
|
|
|
disableWaveformOutput();
|
|
|
|
si5351.set_freq((uint64_t)freq * 100, SI5351_PLL_FIXED, SI5351_CLK0);
|
|
|
|
si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0
|
|
|
|
delay(1);
|
|
|
|
}
|
2016-10-14 10:08:56 +02:00
|
|
|
|
|
|
|
for (i = 0; i < intervall; i++)
|
2016-09-19 21:09:21 +02:00
|
|
|
{
|
2016-10-14 10:08:56 +02:00
|
|
|
// 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds)
|
|
|
|
// and generate the average value, read the ADC value every milli second.
|
|
|
|
uint8_t t = 0;
|
|
|
|
uint16_t ta0 = 0;
|
|
|
|
uint16_t ta1 = 0;
|
|
|
|
for (t = 0; t < MEAS_LOOP_CNT; t++)
|
2016-10-10 12:09:02 +02:00
|
|
|
{
|
2016-10-14 10:08:56 +02:00
|
|
|
ta0 += analogRead(A0);
|
|
|
|
ta1 += analogRead(A1);
|
2016-10-10 12:09:02 +02:00
|
|
|
}
|
2016-10-14 10:08:56 +02:00
|
|
|
a0_sum += (ta0 / MEAS_LOOP_CNT);
|
|
|
|
a1_sum += (ta1 / MEAS_LOOP_CNT);
|
2016-09-19 21:09:21 +02:00
|
|
|
|
|
|
|
delay(1);
|
2016-10-14 10:08:56 +02:00
|
|
|
}
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:08:56 +02:00
|
|
|
a0_sum = a0_sum / intervall;
|
|
|
|
a1_sum = a1_sum / intervall;
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:08:56 +02:00
|
|
|
// 4. send the current output frequency, the drive strength and the measured ADC values from A0 and A1 to the host
|
|
|
|
// MSG_TYPE_MEAS_FREQ_INFO
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_MEAS_FREQ_INFO);
|
2016-10-14 10:21:14 +02:00
|
|
|
send32BitValue(freq);
|
|
|
|
send16BitValue(a0_sum);
|
|
|
|
send16BitValue(a1_sum);
|
2016-10-14 10:08:56 +02:00
|
|
|
sendEOM();
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2024-02-12 11:07:33 +01:00
|
|
|
disableWaveformOutput();
|
2016-10-14 10:08:56 +02:00
|
|
|
si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-10-14 10:08:56 +02:00
|
|
|
if (freq >= end_freq)
|
|
|
|
break; // abort the loop because all is done
|
2016-09-19 21:09:21 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 10:08:56 +02:00
|
|
|
// 5. send a measurement end message to the host
|
|
|
|
// MSG_TYPE_MEAS_END_INFO
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_MEAS_END_INFO);
|
|
|
|
sendEOM();
|
|
|
|
|
2016-09-25 11:59:06 +02:00
|
|
|
si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
|
|
|
|
|
2016-09-19 21:09:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cc_getConfig()
|
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
2016-09-19 21:09:21 +02:00
|
|
|
Serial.write(MSG_TYPE_CONFIG);
|
2016-10-14 10:21:14 +02:00
|
|
|
send32BitValue(start_freq);
|
|
|
|
send32BitValue(end_freq);
|
|
|
|
send32BitValue(step_freq);
|
|
|
|
send16BitValue(intervall);
|
2016-09-19 21:09:21 +02:00
|
|
|
Serial.write((uint8_t) drive_str);
|
2016-10-14 10:02:09 +02:00
|
|
|
sendEOM();
|
2016-09-18 11:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-28 14:43:05 +02:00
|
|
|
void cc_enableClk(void)
|
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
2016-09-28 14:43:05 +02:00
|
|
|
if (cc_read_data[0] == SI5351_CLK0)
|
|
|
|
{
|
2024-02-12 11:07:33 +01:00
|
|
|
if (start_freq < WF_FREQ_MAX_HZ) // < 8kHz
|
2024-02-11 12:50:31 +01:00
|
|
|
{
|
2024-02-12 11:07:33 +01:00
|
|
|
if (start_freq > PWM_MAX_VALUE) // > 8 bit pwm value
|
|
|
|
{
|
|
|
|
setWaveform(WAVEFORM_SINUS);
|
|
|
|
setWaveformFrequency(start_freq);
|
|
|
|
} else {
|
|
|
|
setWaveform(WAVEFORM_DUTYCYCLE);
|
|
|
|
setWaveformDC((uint8_t)(start_freq & 0xff));
|
|
|
|
}
|
2024-02-11 12:50:31 +01:00
|
|
|
enableWaveformOutput();
|
|
|
|
si5351.output_enable(SI5351_CLK0, 0);
|
|
|
|
} else {
|
|
|
|
disableWaveformOutput();
|
|
|
|
si5351.set_freq((uint64_t)start_freq * 100, SI5351_PLL_FIXED, SI5351_CLK0);
|
|
|
|
si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0
|
|
|
|
}
|
2016-09-28 14:43:05 +02:00
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
}
|
|
|
|
else if (cc_read_data[0] == SI5351_CLK1)
|
|
|
|
{
|
2024-02-11 12:50:31 +01:00
|
|
|
|
2016-09-28 15:14:57 +02:00
|
|
|
si5351.set_freq((uint64_t)start_freq * 100, SI5351_PLL_FIXED, SI5351_CLK1);
|
2016-09-28 14:43:05 +02:00
|
|
|
si5351.output_enable(SI5351_CLK1, 1); // enable clock output 1
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
}
|
|
|
|
else if (cc_read_data[0] == SI5351_CLK2)
|
|
|
|
{
|
2016-09-28 15:14:57 +02:00
|
|
|
si5351.set_freq((uint64_t)start_freq * 100, SI5351_PLL_FIXED, SI5351_CLK2);
|
2016-09-28 14:43:05 +02:00
|
|
|
si5351.output_enable(SI5351_CLK2, 1); // enable clock output 2
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
} else {
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_NOK);
|
|
|
|
}
|
2016-10-14 10:02:09 +02:00
|
|
|
sendEOM();
|
2016-09-28 14:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_disableClk(void)
|
|
|
|
{
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
2016-09-28 14:43:05 +02:00
|
|
|
if (cc_read_data[0] == SI5351_CLK0)
|
|
|
|
{
|
2024-02-11 12:50:31 +01:00
|
|
|
disableWaveformOutput();
|
2016-09-28 14:43:05 +02:00
|
|
|
si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
}
|
|
|
|
else if (cc_read_data[0] == SI5351_CLK1)
|
|
|
|
{
|
|
|
|
si5351.output_enable(SI5351_CLK1, 0); // disable clock output 1
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
}
|
|
|
|
else if (cc_read_data[0] == SI5351_CLK2)
|
|
|
|
{
|
|
|
|
si5351.output_enable(SI5351_CLK2, 0); // disable clock output 2
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
} else {
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_NOK);
|
|
|
|
}
|
2016-10-14 10:02:09 +02:00
|
|
|
sendEOM();
|
2016-09-28 14:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_saveDefaults(void)
|
|
|
|
{
|
|
|
|
|
2016-10-14 09:46:04 +02:00
|
|
|
writeEEPROMConfig();
|
2016-09-28 14:43:05 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
2016-09-28 14:43:05 +02:00
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
2016-10-14 10:02:09 +02:00
|
|
|
sendEOM();
|
2016-09-28 14:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-10-12 20:32:46 +02:00
|
|
|
void cc_setClkCorrection(void)
|
|
|
|
{
|
2016-10-14 10:21:14 +02:00
|
|
|
uint32_t tmp_corr = read32BitDataFromBuffer(0);
|
2016-10-12 20:32:46 +02:00
|
|
|
|
|
|
|
si5351.set_correction(tmp_corr);
|
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
2016-10-12 20:32:46 +02:00
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
2016-10-14 10:02:09 +02:00
|
|
|
sendEOM();
|
2016-10-12 20:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cc_getClkCorrection(void)
|
|
|
|
{
|
|
|
|
uint32_t tmp_corr = si5351.get_correction();
|
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
sendSOM();
|
2016-10-14 10:21:14 +02:00
|
|
|
send32BitValue(tmp_corr);
|
2016-10-14 10:02:09 +02:00
|
|
|
sendEOM();
|
2016-10-12 20:32:46 +02:00
|
|
|
}
|
|
|
|
|
2024-02-12 11:07:33 +01:00
|
|
|
void cc_setWFFreq(void)
|
|
|
|
{
|
|
|
|
uint16_t tmp_freq = read16BitDataFromBuffer(0);
|
|
|
|
|
|
|
|
setWaveformFrequency(tmp_freq);
|
|
|
|
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cc_setWF(void)
|
|
|
|
{
|
|
|
|
uint8_t tmp_wf = cc_read_data[0];
|
|
|
|
|
|
|
|
setWaveform(tmp_wf);
|
|
|
|
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cc_setWFDC(void)
|
|
|
|
{
|
|
|
|
uint8_t tmp_dc = cc_read_data[0];
|
|
|
|
|
|
|
|
setWaveformDC(tmp_dc);
|
|
|
|
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
|
|
|
}
|
|
|
|
void cc_enableWF(void)
|
|
|
|
{
|
|
|
|
uint8_t tmp_en = cc_read_data[0];
|
|
|
|
|
|
|
|
if (tmp_en == 0)
|
|
|
|
{
|
|
|
|
disableWaveformOutput();
|
|
|
|
} else {
|
|
|
|
enableWaveformOutput();
|
|
|
|
}
|
|
|
|
|
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_OK);
|
|
|
|
sendEOM();
|
|
|
|
}
|
|
|
|
|
2016-10-12 20:32:46 +02:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-17 19:02:36 +02:00
|
|
|
void cc_init()
|
|
|
|
{
|
|
|
|
cc_state = CC_STATE_READ_SOM1;
|
|
|
|
cc_cmd_to_call = CC_CMD_NO_CMD;
|
|
|
|
cc_cmd_data_read_cnt = 0;
|
|
|
|
cc_cmd_received_correct = MSG_INCOMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_abort()
|
|
|
|
{
|
|
|
|
// send abort message, then init
|
2016-10-14 10:21:14 +02:00
|
|
|
sendSOM();
|
|
|
|
Serial.write(MSG_TYPE_ANSWER_NOK);
|
|
|
|
sendEOM();
|
2016-09-19 21:09:21 +02:00
|
|
|
|
2016-09-17 19:02:36 +02:00
|
|
|
cc_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_processData(uint8_t c)
|
|
|
|
{
|
2016-09-18 11:41:59 +02:00
|
|
|
uint8_t i = 0;
|
|
|
|
|
2016-09-17 19:02:36 +02:00
|
|
|
switch (cc_state)
|
|
|
|
{
|
|
|
|
//*********************************//
|
|
|
|
case CC_STATE_READ_SOM1:;
|
|
|
|
if (c == MSG_SOM1)
|
|
|
|
cc_state = CC_STATE_READ_SOM2;
|
|
|
|
else
|
|
|
|
cc_abort();
|
|
|
|
break;
|
|
|
|
|
|
|
|
//*********************************//
|
|
|
|
case CC_STATE_READ_SOM2:;
|
|
|
|
if (c == MSG_SOM2)
|
|
|
|
cc_state = CC_STATE_READ_CMD;
|
|
|
|
else
|
|
|
|
cc_abort();
|
|
|
|
break;
|
|
|
|
|
|
|
|
//*********************************//
|
|
|
|
case CC_STATE_READ_CMD:;
|
2016-09-18 11:41:59 +02:00
|
|
|
for (i = 0; i < sizeof(cc_commands)/sizeof(uint8_t); i++)
|
2016-09-17 19:02:36 +02:00
|
|
|
{
|
|
|
|
if (cc_commands[i] == c)
|
|
|
|
{
|
|
|
|
if (cc_cmd_data_to_read[i] > 0)
|
|
|
|
cc_state = CC_STATE_READ_DATA;
|
|
|
|
else
|
|
|
|
cc_state = CC_STATE_READ_EOM1;
|
|
|
|
|
|
|
|
cc_cmd_to_call = i; // remember the index of command to call
|
|
|
|
|
|
|
|
break; // break the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
//*********************************//
|
|
|
|
case CC_STATE_READ_DATA:;
|
|
|
|
// write the variable c to the input buffer
|
|
|
|
cc_read_data[cc_cmd_data_read_cnt] = c;
|
|
|
|
|
|
|
|
if (cc_cmd_data_read_cnt >= cc_cmd_data_to_read[cc_cmd_to_call]-1)
|
|
|
|
{
|
|
|
|
cc_state = CC_STATE_READ_EOM1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_cmd_data_read_cnt++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
//*********************************//
|
|
|
|
case CC_STATE_READ_EOM1:;
|
|
|
|
if (c == MSG_EOM1)
|
|
|
|
cc_state = CC_STATE_READ_EOM2;
|
|
|
|
else
|
|
|
|
cc_abort();
|
|
|
|
break;
|
|
|
|
|
|
|
|
//*********************************//
|
|
|
|
case CC_STATE_READ_EOM2:;
|
|
|
|
if (c == MSG_EOM2)
|
|
|
|
{
|
|
|
|
cc_cmd_received_correct = MSG_COMPLETE;
|
|
|
|
} else
|
|
|
|
cc_abort();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cc_abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
//*********************************//
|
|
|
|
if (cc_cmd_received_correct == MSG_COMPLETE)
|
|
|
|
{
|
|
|
|
// call the function using the received data
|
|
|
|
(*cc_cmd_functions[cc_cmd_to_call])();
|
|
|
|
// clear the read buffer
|
|
|
|
cc_clearReadDataBuffer();
|
|
|
|
cc_init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
void cc_clearReadDataBuffer()
|
|
|
|
{
|
2016-09-18 11:41:59 +02:00
|
|
|
uint8_t i = 0;
|
|
|
|
for (i = 0; i < CC_READ_DATA_MAX; i++)
|
2016-09-17 19:02:36 +02:00
|
|
|
{
|
|
|
|
cc_read_data[i] = 0x00;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2016-10-10 11:28:09 +02:00
|
|
|
|
2016-10-14 10:02:09 +02:00
|
|
|
void sendSOM()
|
|
|
|
{
|
|
|
|
Serial.write(MSG_SOM1);
|
|
|
|
Serial.write(MSG_SOM2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendEOM()
|
|
|
|
{
|
|
|
|
Serial.write(MSG_EOM1);
|
|
|
|
Serial.write(MSG_EOM2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-10-14 10:21:14 +02:00
|
|
|
void send32BitValue(uint32_t value)
|
|
|
|
{
|
|
|
|
Serial.write((uint8_t)((value & 0xff000000) >> 24));
|
|
|
|
Serial.write((uint8_t)((value & 0x00ff0000) >> 16));
|
|
|
|
Serial.write((uint8_t)((value & 0x0000ff00) >> 8));
|
|
|
|
Serial.write((uint8_t) (value & 0x000000ff));
|
|
|
|
}
|
|
|
|
|
|
|
|
void send16BitValue(uint16_t value)
|
|
|
|
{
|
|
|
|
Serial.write((uint8_t)((value & 0xff00) >> 8));
|
|
|
|
Serial.write((uint8_t) (value & 0x00ff));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|