diff --git a/firmware/command_ctrl.ino b/firmware/command_ctrl.ino index 746b557..44fd39c 100644 --- a/firmware/command_ctrl.ino +++ b/firmware/command_ctrl.ino @@ -24,8 +24,10 @@ void cc_clearReadDataBuffer(void); void cc_setStartFreq(void); void cc_setEndFreq(void); void cc_setIntervall(void); +void cc_setFreqStep(void); void cc_setDriveStrength(void); void cc_startMeasurement(void); +void cc_getConfig(void); /*****************************************************************************/ @@ -40,7 +42,9 @@ uint8_t cc_commands[] = { CC_CMD_SET_END_FREQ, CC_CMD_SET_INTERVALL, CC_CMD_SET_DRIVE_STRENGTH, + CC_CMD_SET_FREQ_STEP, CC_CMD_START_MEASUREMENT, + CC_CMD_GET_CONFIG, }; void (*cc_cmd_functions[])() = { @@ -48,7 +52,9 @@ void (*cc_cmd_functions[])() = { CC_CMD_SET_END_FREQ_FUNC, CC_CMD_SET_INTERVALL_FUNC, CC_CMD_SET_DRIVE_STRENGTH_FUNC, + CC_CMD_SET_FREQ_STEP_FUNC, CC_CMD_START_MEASUREMENT_FUNC, + CC_CMD_GET_CONFIG_FUNC, }; uint8_t cc_cmd_data_to_read[] = { @@ -56,7 +62,9 @@ uint8_t cc_cmd_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, + CC_CMD_SET_FREQ_STEP_DATA_TO_READ, CC_CMD_START_MEASUREMENT_DATA_TO_READ, + CC_CMD_GET_CONFIG_DATA_TO_READ, }; uint8_t cc_read_data[CC_READ_DATA_MAX]; @@ -75,47 +83,207 @@ uint8_t cc_cmd_data_read_cnt = 0; void cc_setStartFreq() { + uint32_t tmp_start_freq = (uint32_t)cc_read_data[0] << 24; + tmp_start_freq += (uint32_t)cc_read_data[1] << 16; + tmp_start_freq += (uint32_t)cc_read_data[2] << 8; + tmp_start_freq += (uint32_t)cc_read_data[3]; + if (tmp_start_freq < 1) + tmp_start_freq = 1; + if (tmp_start_freq > 150000000) + tmp_start_freq = 150000000; + + start_freq = tmp_start_freq; + char* tmp = " "; sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_NOK, + MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2); Serial.write(tmp); } void cc_setEndFreq() { + uint32_t tmp_end_freq = (uint32_t)cc_read_data[0] << 24; + tmp_end_freq += (uint32_t)cc_read_data[1] << 16; + tmp_end_freq += (uint32_t)cc_read_data[2] << 8; + tmp_end_freq += (uint32_t)cc_read_data[3]; + if (tmp_end_freq < 1) + tmp_end_freq = 1; + if (tmp_end_freq > 150000000) + tmp_end_freq = 150000000; + + end_freq = tmp_end_freq; + char* tmp = " "; sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_NOK, + MSG_TYPE_ANSWER_OK, + MSG_EOM1, MSG_EOM2); + Serial.write(tmp); +} + +void cc_setFreqStep() +{ + uint32_t tmp_step_freq = (uint32_t)cc_read_data[0] << 24; + tmp_step_freq += (uint32_t)cc_read_data[1] << 16; + tmp_step_freq += (uint32_t)cc_read_data[2] << 8; + tmp_step_freq += (uint32_t)cc_read_data[3]; + if (tmp_step_freq < 1) + tmp_step_freq = 1; + if (tmp_step_freq > 150000000) + tmp_step_freq = 150000000; + + step_freq = tmp_step_freq; + + char* tmp = " "; + sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, + MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2); Serial.write(tmp); } void cc_setIntervall() { + uint16_t tmp_intervall = (uint16_t)cc_read_data[0] << 8; + tmp_intervall += (uint16_t)cc_read_data[1]; + if (tmp_intervall < 1) + tmp_intervall = 1; + if (tmp_intervall > 150000000) + tmp_intervall = 150000000; + + intervall = tmp_intervall; + char* tmp = " "; sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_NOK, + MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2); Serial.write(tmp); } void cc_setDriveStrength() { - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_NOK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + 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; + + char* tmp = " "; + sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, + MSG_TYPE_ANSWER_OK, + MSG_EOM1, MSG_EOM2); + Serial.write(tmp); + } else { + + char* tmp = " "; + sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, + MSG_TYPE_ANSWER_NOK, + MSG_EOM1, MSG_EOM2); + Serial.write(tmp); + } } void cc_startMeasurement() { - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - 123, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + + // 1. send measurement info to host + // MSG_TYPE_CONFIG + cc_getConfig(); + + // 2. start a for loop from the frequence to start to the end frequence + if (start_freq > 0 && start_freq <= 150000000 && + end_freq > 0 && end_freq <= 150000000 && + start_freq < end_freq && + intervall > 0 && + step_freq > 0) + { + si5351.drive_strength(SI5351_CLK0, drive_str); // 2 4 6 8ma + + uint32_t freq = 0; + for (freq = start_freq; freq < end_freq; freq += step_freq) + { + uint32_t a0_sum = 0; + uint32_t a1_sum = 0; + uint16_t i = 0; + + si5351.set_freq(freq * 100, SI5351_PLL_FIXED, SI5351_CLK0); + si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0 + delay(1); + + for (i = 0; i < intervall; i++) + { + // 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. + a0_sum += analogRead(A0); + a1_sum += analogRead(A1); + + delay(1); + } + + a0_sum = a0_sum / intervall; + a1_sum = a1_sum / intervall; + + // 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 + Serial.write(MSG_SOM1); + Serial.write(MSG_SOM2); + Serial.write(MSG_TYPE_MEAS_FREQ_INFO); + Serial.write((uint8_t)((freq & 0xff000000) >> 24)); + Serial.write((uint8_t)((freq & 0x00ff0000) >> 16)); + Serial.write((uint8_t)((freq & 0x0000ff00) >> 8)); + Serial.write((uint8_t) (freq & 0x000000ff)); + Serial.write((uint8_t)((a0_sum & 0xff00) >> 8)); + Serial.write((uint8_t) (a0_sum & 0x00ff)); + Serial.write((uint8_t)((a1_sum & 0xff00) >> 8)); + Serial.write((uint8_t) (a1_sum & 0x00ff)); + Serial.write(MSG_EOM1); + Serial.write(MSG_EOM2); + + si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0 + } + + // 5. send a measurement end message to the host + // MSG_TYPE_MEAS_END_INFO + char* tmp = " "; + sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, + MSG_TYPE_MEAS_END_INFO, + MSG_EOM1, MSG_EOM2); + Serial.write(tmp); + + } else { + // on error + char* tmp = " "; + sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, + MSG_TYPE_ANSWER_NOK, + MSG_EOM1, MSG_EOM2); + Serial.write(tmp); + } + +} + +void cc_getConfig() +{ + Serial.write(MSG_SOM1); + Serial.write(MSG_SOM2); + Serial.write(MSG_TYPE_CONFIG); + Serial.write((uint8_t)((start_freq & 0xff000000) >> 24)); + Serial.write((uint8_t)((start_freq & 0x00ff0000) >> 16)); + Serial.write((uint8_t)((start_freq & 0x0000ff00) >> 8)); + Serial.write((uint8_t) (start_freq & 0x000000ff)); + Serial.write((uint8_t)((end_freq & 0xff000000) >> 24)); + Serial.write((uint8_t)((end_freq & 0x00ff0000) >> 16)); + Serial.write((uint8_t)((end_freq & 0x0000ff00) >> 8)); + Serial.write((uint8_t) (end_freq & 0x000000ff)); + Serial.write((uint8_t)((step_freq & 0xff000000) >> 24)); + Serial.write((uint8_t)((step_freq & 0x00ff0000) >> 16)); + Serial.write((uint8_t)((step_freq & 0x0000ff00) >> 8)); + Serial.write((uint8_t) (step_freq & 0x000000ff)); + Serial.write((uint8_t)((intervall & 0xff00) >> 8)); + Serial.write((uint8_t) (intervall & 0x00ff)); + Serial.write((uint8_t) drive_str); + Serial.write(MSG_EOM1); + Serial.write(MSG_EOM2); } /*****************************************************************************/ @@ -138,6 +306,7 @@ void cc_abort() MSG_TYPE_ANSWER_NOK, MSG_EOM1, MSG_EOM2); Serial.write(tmp); + cc_init(); } diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 0d704a7..8bc1b56 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -10,8 +10,17 @@ extern "C" { 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() { + pinMode(A0, INPUT); + pinMode(A1, INPUT); + // Init the serial connection Serial.begin(57600); @@ -21,6 +30,13 @@ void setup() // 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 + + /* // Set CLK0 to output 14 MHz with a fixed PLL frequency si5351.set_pll(SI5351_PLL_FIXED, SI5351_PLLA); si5351.set_freq(1400000000ULL, SI5351_PLL_FIXED, SI5351_CLK0); @@ -30,6 +46,7 @@ void setup() si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0 si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_2MA); // 2 4 6 8ma + */ } void loop() diff --git a/firmware/globals.h b/firmware/globals.h index a6a1d29..fcf8a51 100644 --- a/firmware/globals.h +++ b/firmware/globals.h @@ -7,8 +7,11 @@ #define MSG_EOM1 0x0d #define MSG_EOM2 0x0a -#define MSG_TYPE_ANSWER_OK 0x01 -#define MSG_TYPE_ANSWER_NOK 0x02 +#define MSG_TYPE_ANSWER_OK 0x01 +#define MSG_TYPE_ANSWER_NOK 0x02 +#define MSG_TYPE_MEAS_FREQ_INFO 0x03 +#define MSG_TYPE_MEAS_END_INFO 0x04 +#define MSG_TYPE_CONFIG 0x05 /*****************************************************************************/ @@ -32,7 +35,9 @@ #define CC_CMD_SET_END_FREQ 0x02 #define CC_CMD_SET_INTERVALL 0x03 #define CC_CMD_SET_DRIVE_STRENGTH 0x04 -#define CC_CMD_START_MEASUREMENT 0x05 +#define CC_CMD_SET_FREQ_STEP 0x05 +#define CC_CMD_START_MEASUREMENT 0x06 +#define CC_CMD_GET_CONFIG 0x10 /*****************************************************************************/ @@ -40,15 +45,19 @@ #define CC_CMD_SET_END_FREQ_FUNC &cc_setEndFreq #define CC_CMD_SET_INTERVALL_FUNC &cc_setIntervall #define CC_CMD_SET_DRIVE_STRENGTH_FUNC &cc_setDriveStrength +#define CC_CMD_SET_FREQ_STEP_FUNC &cc_setFreqStep #define CC_CMD_START_MEASUREMENT_FUNC &cc_startMeasurement +#define CC_CMD_GET_CONFIG_FUNC &cc_getConfig /*****************************************************************************/ #define CC_CMD_SET_START_FREQ_DATA_TO_READ 4 // up to 32 bit = 4 byte => 2 ** 32 = >500000000Hz #define CC_CMD_SET_END_FREQ_DATA_TO_READ 4 // up to 32 bit = 4 byte => 2 ** 32 = >500000000Hz -#define CC_CMD_SET_INTERVALL_DATA_TO_READ 3 // up to 24 bit = 3 byte => 2 ** 24 = >1000000Hz +#define CC_CMD_SET_INTERVALL_DATA_TO_READ 2 #define CC_CMD_SET_DRIVE_STRENGTH_DATA_TO_READ 1 +#define CC_CMD_SET_FREQ_STEP_DATA_TO_READ 4 #define CC_CMD_START_MEASUREMENT_DATA_TO_READ 0 +#define CC_CMD_GET_CONFIG_DATA_TO_READ 0 /*****************************************************************************/