977 lines
21 KiB
C
977 lines
21 KiB
C
/*
|
|
* frq.c
|
|
*
|
|
* Created on: Jun 8, 2022
|
|
* Author: Keith.Lloyd
|
|
*/
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include "arm_math.h"
|
|
#include "spi.h"
|
|
#include "fsl_gpio.h"
|
|
#include "frq.h"
|
|
#include "LPC54114_cm4.h"
|
|
#include "display.h"
|
|
#include "utils.h"
|
|
#include "init.h"
|
|
#include "amps.h"
|
|
#include "ports.h"
|
|
#include "timer.h"
|
|
#include "mode.h"
|
|
#include "stdbool.h"
|
|
#include "eeprom.h"
|
|
#include "pwr_level.h"
|
|
|
|
extern uint8_t Test_Mode, catch_up_flag;
|
|
extern uint8_t Taps_Flag;
|
|
extern uint32_t new_freq;
|
|
extern uint8_t frequency,Cur_Mode,LD_Flag;
|
|
extern uint8_t Dds_Pot_Val[2];
|
|
extern uint16_t Sys_Chk_tmr;
|
|
|
|
uint8_t Tx_mode, frq_chg_tmr,old_freq;
|
|
FREQDATA_t freq;
|
|
FREQUENCY_t freqArray[FREQ_MAX_NUM];
|
|
|
|
uint8_t foo[4]; //stuff the 2 16-bit values into an array of 4 uint8_t
|
|
uint8_t frq_update_flag;
|
|
|
|
|
|
extern uint8_t Power_Level;
|
|
extern ClampData_t clampData;
|
|
extern uint32_t systemTime;
|
|
|
|
uint32_t Calc_Freq(uint32_t fout) // Calculate frequency word for DDS
|
|
{
|
|
float Freg;
|
|
uint32_t Frq_result;
|
|
|
|
Freg = fout * 268435456.0; // FREG = (Frequency * 2^28)/fclk = 12MHz normally
|
|
Frq_result = Freg/DDS_CLK + 0.5;
|
|
|
|
return(Frq_result);
|
|
|
|
}
|
|
|
|
void Load_Ramp(uint32_t ramp_freq) // Sets up RAMP DDS for PWM AMP
|
|
|
|
{
|
|
|
|
uint16_t ramp_hi = FREQ0_REG; //top 2 bits 0x01 for Freq0 register address
|
|
uint16_t ramp_lo = FREQ0_REG;
|
|
|
|
|
|
if (ramp_freq > 0)
|
|
{
|
|
Send_Ctrl_Word(RAMP_CTRL_WORD1,RAMP); // send Control word to hold setup
|
|
|
|
ramp_lo |= (ramp_freq & 0x3fff);
|
|
ramp_freq >>= 14;
|
|
ramp_hi |= (ramp_freq & 0x3fff);
|
|
|
|
//ramp_hi |= (ramp_freq & 0xc000) >> 14; // Preserve upper 2 bits
|
|
|
|
|
|
foo[0] = (uint8_t)(ramp_lo >> 8); //LS 16-bit word, MSB
|
|
foo[1] = (uint8_t)(ramp_lo & 0x00ff); //LS 16-bit word, LSB
|
|
foo[2] = (uint8_t)(ramp_hi >> 8);
|
|
foo[3] = (uint8_t)(ramp_hi & 0x00ff);
|
|
|
|
SPI0_SendBytes(foo, 4, RAMP);
|
|
|
|
Send_Ctrl_Word(RAMP_CTRL_WORD2,RAMP); // send Control word to release setup
|
|
}
|
|
else
|
|
{
|
|
Send_Ctrl_Word(RAMP_CTRL_WORD3,RAMP); // Control word to hold setup and put part to sleep
|
|
GPIO_PinWrite(GPIO, RAMP_PORT, RAMP_RST_GPIO_PIN, HI); //Hold RST HI
|
|
while(1); //stop
|
|
}
|
|
}
|
|
|
|
void Load_Frq_Gen(FRQ_OUT_SELECT_t Freq_Mode, int32_t f1, int32_t f2)
|
|
|
|
{
|
|
uint8_t x=0;
|
|
uint16_t freq_hi = FREQ0_REG;
|
|
uint16_t freq_lo = FREQ0_REG;
|
|
|
|
if (Freq_Mode == NULL_FREQ)
|
|
x = 1; // Switch both DDS chips OFF
|
|
|
|
if (Freq_Mode == SINGLE)
|
|
{
|
|
x = 2; // Switch the primary DDS ON
|
|
|
|
|
|
Send_Ctrl_Word(FRQ_CTRL_WORD1,SIGNAL); // send Control word to hold setup
|
|
|
|
freq_lo |= (f1 & 0x3fff);
|
|
f1 >>= 14;
|
|
freq_hi |= (f1 & 0x3fff);
|
|
|
|
foo[0] = (uint8_t)(freq_lo >> 8); //LS 16-bit word, MSB
|
|
foo[1] = (uint8_t)(freq_lo & 0x00ff); //LS 16-bit word, LSB
|
|
foo[2] = (uint8_t)(freq_hi >> 8);
|
|
foo[3] = (uint8_t)(freq_hi & 0x00ff);
|
|
|
|
// freq_hi |= (f1 & 0xc000) >> 14; // Preserve upper 2 bits
|
|
// freq_lo |= (f1 & 0x3fff);
|
|
// foo[0] = (uint8_t)(freq_lo >> 8);
|
|
// foo[1] = (uint8_t)(freq_lo & 0x00ff);
|
|
// foo[2] = (uint8_t)(freq_hi >> 8);
|
|
// foo[3] = (uint8_t)(freq_hi & 0x00ff);
|
|
|
|
SPI0_SendBytes(foo, 4, SIGNAL);
|
|
|
|
|
|
Send_Ctrl_Word(FRQ_CTRL_WORD2,SIGNAL); // send Control word to release setup
|
|
|
|
|
|
|
|
}
|
|
|
|
if (Freq_Mode == DUAL)
|
|
{ // ############---Switch both DDS chips ON SYNCHRONOUSLY--######
|
|
|
|
// Set RESET to hold
|
|
// Load F1
|
|
Send_Ctrl_Word(FRQ_CTRL_WORD1,SIGNAL); // send Control word to hold setup
|
|
|
|
freq_lo |= (f1 & 0x3fff);
|
|
f1 >>= 14;
|
|
freq_hi |= (f1 & 0x3fff);
|
|
|
|
foo[0] = (uint8_t)(freq_lo >> 8); //LS 16-bit word, MSB
|
|
foo[1] = (uint8_t)(freq_lo & 0x00ff); //LS 16-bit word, LSB
|
|
foo[2] = (uint8_t)(freq_hi >> 8);
|
|
foo[3] = (uint8_t)(freq_hi & 0x00ff);
|
|
SPI0_SendBytes(foo, 4, SIGNAL);
|
|
|
|
Send_Ctrl_Word(FRQ_CTRL_WORD2,SIGNAL); // send Control word to release setup
|
|
|
|
|
|
// Load F2
|
|
freq_hi = FREQ0_REG;
|
|
freq_lo = FREQ0_REG;
|
|
|
|
Send_Ctrl_Word(FRQ_CTRL_WORD1,SDSIGNAL); // send Control word to hold setup
|
|
|
|
|
|
|
|
freq_lo |= (f2 & 0x3fff);
|
|
f2 >>= 14;
|
|
freq_hi |= (f2 & 0x3fff);
|
|
|
|
foo[0] = (uint8_t)(freq_lo >> 8); //LS 16-bit word, MSB
|
|
foo[1] = (uint8_t)(freq_lo & 0x00ff); //LS 16-bit word, LSB
|
|
foo[2] = (uint8_t)(freq_hi >> 8);
|
|
foo[3] = (uint8_t)(freq_hi & 0x00ff);
|
|
SPI0_SendBytes(foo, 4, SDSIGNAL);
|
|
|
|
|
|
Send_Ctrl_Word(FRQ_CTRL_WORD2,SDSIGNAL); // send Control word to release setup
|
|
|
|
// Send_Ctrl_Word(SLP_CTRL_WRD,SIGNAL); // TEST ONLY REMOVE Put unused DDS to sleep
|
|
|
|
|
|
|
|
// Send_Ctrl_Word(PHASE_RESET,SIGNAL); // // Set Phase F1 to 0
|
|
|
|
|
|
|
|
// Send_Ctrl_Word(PHASE_RESET,SDSIGNAL); // Set Phase F2 to 0
|
|
|
|
|
|
|
|
// Send_Ctrl_Word(FRQ_CTRL_WORD2,BOTH_SIGNAL); // Send release code word simultaneously
|
|
|
|
// End#.
|
|
|
|
|
|
// GPIO_PinWrite(GPIO, 1, 13, 1);
|
|
// Set high SD_RST
|
|
// Set high SIG_RST
|
|
|
|
// Set up both Sine Wave generators
|
|
|
|
// set low SD_RST
|
|
// set low SIG_RST.
|
|
}
|
|
}
|
|
|
|
void Send_Ctrl_Word(uint16_t Control_Reg, SPI_MODE_t mode)
|
|
{
|
|
//Set the control register Send MS Byte first then LSByte
|
|
|
|
foo[0] = (uint8_t)(Control_Reg >> 8); // Reset bit held
|
|
foo[1] = (uint8_t)(Control_Reg & 0x00ff);
|
|
SPI0_SendBytes(foo, 2, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddFrequency(uint32_t frequency1,uint32_t frequency2, uint8_t enabled, uint8_t bc_enabled,uint8_t bc_mask,uint8_t pot,float i_lo,float i_hi,float v_lo,float v_hi)
|
|
{
|
|
uint32_t i = freq.numFrequencies;
|
|
|
|
freqArray[i].frequency1 = frequency1;
|
|
freqArray[i].frequency2 = frequency2;
|
|
|
|
freqArray[i].enabled = enabled;
|
|
freqArray[i].bc_enabled = bc_enabled;
|
|
|
|
freqArray[i].bc_Mask = bc_mask;
|
|
freqArray[i].max_pot = pot;
|
|
|
|
// freqArray[i].v_coeff_lo = 0.876923;
|
|
freqArray[i].v_coeff_lo = v_lo;
|
|
freqArray[i].i_coeff_hi = i_hi;
|
|
freqArray[i].i_coeff_lo = i_lo;
|
|
|
|
freq.numFrequencies++;
|
|
}
|
|
|
|
void ClearFreqArray(void)
|
|
{
|
|
for(uint32_t i = 0; i < FREQ_MAX_NUM; i++)
|
|
{
|
|
freqArray[i].frequency1 = 0;
|
|
freqArray[i].frequency2 = 0;
|
|
freqArray[i].enabled = 0;
|
|
freqArray[i].bc_enabled = 0;
|
|
|
|
}
|
|
}
|
|
|
|
#define PIN_OUTPUT()
|
|
void FREQ_Init(void)
|
|
{
|
|
uint32_t tmp;
|
|
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SIG_RST_GPIO_PIN, 1); // Set all RESET signals to RESET
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SD_RST_GPIO_PIN, 1);
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, RAMP_RST_GPIO_PIN, 1);
|
|
Delay_Ticks(1);
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, RAMP_RST_GPIO_PIN, 0);
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SIG_RST_GPIO_PIN, 0); // Set all RESET signals to RESET
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SD_RST_GPIO_PIN, 0); // 8/26/24
|
|
|
|
freq.numFrequencies = 0;
|
|
|
|
|
|
if(Test_Mode) //
|
|
{
|
|
FREQ_LoadFactoryDefaults();
|
|
// frq_update_flag = false;
|
|
}
|
|
else
|
|
{
|
|
if(!EE_LoadFrequencies())
|
|
{
|
|
FREQ_LoadFactoryDefaults();
|
|
}
|
|
}
|
|
|
|
Send_Ctrl_Word(SLP_CTRL_WRD,SDSIGNAL); // Put unused DDS to sleep
|
|
|
|
// tmp = Calc_Freq(32768);
|
|
// Load_Frq_Gen(SINGLE,tmp,0); // testing only 8KHz
|
|
|
|
// Load_Ramp(7829367); // 350KHz ramp waveform
|
|
|
|
Load_Ramp(6710886); // 300KHz ramp waveform
|
|
// Load_Ramp(20132659); // 900KHz ramp waveform
|
|
// Load_Ramp(72370199); // 3.2352MHz ramp waveform
|
|
|
|
}
|
|
|
|
void FREQ_LoadFactoryDefaults(void)
|
|
{
|
|
#if 1 //Use new function so we don't have to specify all the parameters
|
|
|
|
//These are the FACTORY TEST Frequencies
|
|
FREQ_ClearFrequencies();
|
|
FREQ_AddFrequency(512, 1, 1, FT_ACTIVE);
|
|
FREQ_AddFrequency(3140, 1, 1, FT_ACTIVE);
|
|
FREQ_AddFrequency(8192, 1, 1, FT_ACTIVE);
|
|
FREQ_AddFrequency(29433, 1, 1, FT_ACTIVE);
|
|
FREQ_AddFrequency(32770, 1, 1, FT_ACTIVE);
|
|
FREQ_AddFrequency(65055, 1, 1, FT_ACTIVE);
|
|
FREQ_AddFrequency(88779, 1, 1, FT_ACTIVE);
|
|
|
|
#else //Or we can specify all the parameters
|
|
|
|
AddFrequency(98, 0, 0, 0, 0, 0,0,0,0,0);
|
|
AddFrequency(128, 0, 0, 0, 0, 0,0,0,0,0);
|
|
AddFrequency(263, 0, 1, 0, 0, 165,0.8396,0.47,0.876923,0);
|
|
AddFrequency(440, 0, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
AddFrequency(440, 220, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
|
|
AddFrequency(512, 0, 1, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
AddFrequency(512, 256, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
|
|
AddFrequency(560, 0, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
AddFrequency(577, 0, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
|
|
AddFrequency(640, 0, 0, 0, 0,180,0.8396,0.47,0.876923,0);
|
|
AddFrequency(640, 320, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
|
|
AddFrequency(815, 0, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
AddFrequency(870, 0, 1, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
AddFrequency(870, 435, 0, 0, 0, 180,0.8396,0.47,0.876923,0);
|
|
|
|
AddFrequency(940, 0, 0, 0, 0, 180,0.8105,0.47,0.876923,0);
|
|
AddFrequency(940, 470, 0, 0, 0, 180,0.8105,0.47,0.876923,0);
|
|
|
|
AddFrequency(1024, 0, 0, 0, 0, 180,0.8105,0.49,0.911538,0);
|
|
|
|
AddFrequency(1170, 0, 1, 0, 0, 180,0.8105,0.49,0.911538,0);
|
|
AddFrequency(1170, 585, 0, 0, 0, 180,0.8105,0.49,0.911538,0);
|
|
|
|
AddFrequency(3140, 0, 1, 1, MASK_144, 180,0.8634,0.482,0.97083,0);
|
|
AddFrequency(3140, 1570, 0, 0, 0 , 180,0.8634,0.482,0.97083,0);
|
|
|
|
AddFrequency(4096, 0, 1, 0, 0, 180,0.8634,0.482,0.97083,0);
|
|
AddFrequency(6000, 0, 1, 0, 0, 180,0.8634,0.482,0.97083,0);
|
|
AddFrequency(8010, 0, 0, 1,MASK_144, 180,0.8976,0.53,1.025,0);
|
|
AddFrequency(8192, 0, 1, 1,MASK_144, 180,0.8976,0.53,1.025,0);
|
|
AddFrequency(9820, 0, 1, 0,MASK_144, 180,0.8976,0.53,1.025,0);
|
|
|
|
AddFrequency(29433, 0, 1, 1,MASK_58, 180,0.9534,0.69,1.043,0);
|
|
AddFrequency(32770, 0, 1, 1,MASK_58, 180,0.9581,0.713333333,1.009,0);
|
|
AddFrequency(44499, 0, 1, 1,MASK_15, 180,1,0.91,1.035,0);
|
|
AddFrequency(66055, 0, 1, 1,MASK_58, 120,1.215,1.35,1.6375,0);
|
|
AddFrequency(88779, 0, 1, 1,MASK_15, 120,1.355,1.36,1.8857,0);
|
|
AddFrequency(99037, 0, 0, 1,MASK_58, 120,1.355,01.36,0,0);
|
|
AddFrequency(200000, 0, 1, 1,MASK_144, 120,1.355,01.36,7.6,0);
|
|
AddFrequency(480000, 0, 0, 0,MASK_144, 120,4.111,2.111,7.6,0);
|
|
#endif
|
|
}
|
|
|
|
|
|
/*
|
|
* Pack frequency data and return
|
|
* Packed data is used for saving to FLASH
|
|
*/
|
|
uint32_t FREQ_GetPackedFrequency(uint32_t index)
|
|
{
|
|
uint32_t packed = freqArray[index].frequency1;
|
|
//packed |= freqArray[index].type << FREQ_PACK_SHIFT_TYPE;
|
|
packed |= (uint32_t)freqArray[index].enabled << FREQ_PACK_SHIFT_ENABLED;
|
|
//packed |= (uint32_t)freqArray[index].inMenu << FREQ_PACK_SHIFT_INMENU;
|
|
|
|
return packed;
|
|
}
|
|
|
|
/*
|
|
* Add a frequency to the list from packed data
|
|
* Packed data is used for saving to FLASH
|
|
*/
|
|
void FREQ_AddPackedFrequency(uint32_t packedFreq)
|
|
{
|
|
uint32_t frequency = packedFreq & FREQ_PACK_MASK_FREQ;
|
|
//FREQ_TYPE_t type = (packedFreq & FREQ_PACK_MASK_TYPE) >> FREQ_PACK_SHIFT_TYPE;
|
|
bool enabled = packedFreq & FREQ_PACK_MASK_ENABLED;
|
|
//bool inMenu = packedFreq & FREQ_PACK_MASK_INMENU;
|
|
|
|
FREQ_AddFrequency(frequency, enabled, 1, FT_ACTIVE); //inMenu always 1, all FT_ACTIVE
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* Clear the frequency list
|
|
* Zero out all data and set freq.numFrequencies to zero
|
|
*/
|
|
void FREQ_ClearFrequencies(void)
|
|
{
|
|
// frq_update_flag = true;
|
|
for(uint32_t i = 0; i < freq.numFrequencies; i++)
|
|
{
|
|
freqArray[i].frequency1 = 0;
|
|
freqArray[i].enabled = 0;
|
|
|
|
}
|
|
|
|
freq.numFrequencies = 0;
|
|
|
|
frequency = 0;
|
|
}
|
|
|
|
/*
|
|
* Add a Frequency from UM Setup
|
|
*/
|
|
void FREQ_AddFrequency(uint32_t f1, uint8_t enabledTemp, uint8_t inMenu, FREQ_TYPE_t type)
|
|
{
|
|
if(!inMenu)
|
|
{
|
|
return; //ignore freqs that are not in menu
|
|
}
|
|
|
|
if((type != FT_ACTIVE) && (type != FT_LD_ACTIVE))
|
|
{
|
|
return; //ignore RX frequency types
|
|
}
|
|
|
|
//TODO: Calculate other things in Keith's freqArray[] here
|
|
//frequency2 for LD needs a function to determine 2nd frequency
|
|
//Doesn't matter yet since RX doesn't support LD
|
|
//scale factors
|
|
//other crap
|
|
|
|
//f2
|
|
uint32_t f2 = 0;
|
|
if(type == FT_LD_ACTIVE)
|
|
{
|
|
if(f1 <= FREQ_LD_SWITCH_POINT)
|
|
{
|
|
f2 = f1 * 2;
|
|
}
|
|
else if(f1 <= FREQ_LD_MAX_FREQUENCY)
|
|
{
|
|
f2 = f1 / 2; //TODO: This will break for odd frequencies since it's an integer. Need to rework freqs...
|
|
}
|
|
else
|
|
{
|
|
f2 = 0;
|
|
}
|
|
}
|
|
|
|
//TODO: Generate values for these!!!!!!!!!!
|
|
uint32_t bc_enabledTemp = BC_Enable_Chk(f1);
|
|
uint32_t bc_maskTemp = BC_Mask_Chk(f1); //Todo remove as unused
|
|
uint32_t max_pot = Max_Pot_Chk(f1);
|
|
float32_t v_coeff_lo = V_coeff_lo_Chk(f1);
|
|
float32_t i_coeff_hi = I_coeff_hi_Chk(f1);
|
|
float32_t i_coeff_lo = I_coeff_lo_Chk(f1);
|
|
float32_t v_coeff_hi = V_coeff_hi_Chk(f1); //always 0
|
|
|
|
|
|
AddFrequency(f1, f2, enabledTemp, bc_enabledTemp, bc_maskTemp, max_pot, i_coeff_lo, i_coeff_hi, v_coeff_lo, v_coeff_hi);
|
|
}
|
|
|
|
|
|
uint8_t BC_Enable_Chk(uint32_t f1)
|
|
{
|
|
if((f1 >= BCAST_MIN) && (f1 < BCAST_MAX+1))
|
|
return(1);
|
|
else
|
|
return(0);
|
|
}
|
|
|
|
uint8_t BC_Mask_Chk(uint32_t f1)
|
|
{
|
|
if((f1 >= BCAST_MIN) && (f1 < 9820)|| (f1 >= 200000) && (f1 <=480000))
|
|
return(MASK_144);
|
|
else if(f1 == 29430 || f1==32768 || f1==65536 || f1==131072)
|
|
return(MASK_58);
|
|
else if(f1 == 44624 || f1== 83078)
|
|
return(MASK_15);
|
|
else
|
|
{
|
|
return 99;
|
|
}
|
|
|
|
}
|
|
|
|
uint8_t Max_Pot_Chk(uint32_t f1)
|
|
{
|
|
if(f1 < 440)
|
|
return(180);//165
|
|
if((f1 >= 440) && (f1 < 12000)) // 29430
|
|
return(235);
|
|
|
|
if((f1 >= 12000) && (f1 < 29430))
|
|
return(245);
|
|
|
|
if((f1 >= 29430)&& (f1 <= 44500))
|
|
return(250);
|
|
if((f1 > 44624) && (f1 < 200000))
|
|
return(200);
|
|
if(f1 == 200000)
|
|
return(140);
|
|
else
|
|
return(140);
|
|
|
|
//TODO set up default value
|
|
}
|
|
|
|
|
|
float V_coeff_lo_Chk(uint32_t f1)
|
|
{
|
|
float a;
|
|
switch(f1)
|
|
{
|
|
case 1 ... 940:
|
|
a= 0.876923;
|
|
break;
|
|
case 941 ... 1170:
|
|
a = 0.911538;
|
|
break;
|
|
case 1171 ... 6000:
|
|
a = 0.97083;
|
|
break;
|
|
|
|
case 6001 ... 8009:
|
|
a = 1.025;
|
|
|
|
break;
|
|
case 8010 ... 9820:
|
|
a = 1.025;
|
|
break;
|
|
case 9821 ... 29430:
|
|
a = 0.81;
|
|
break;
|
|
case 29431 ... 32768:
|
|
a = 0.81;//1.009;
|
|
break;
|
|
case 32769 ... 44100:
|
|
a = 1.035;
|
|
break;
|
|
case 44101 ... 65536:
|
|
a = 1.0;
|
|
break;
|
|
case 65537 ... 80000:
|
|
a = 1.1;
|
|
break;
|
|
case 80001 ... 199999:
|
|
a = 1.36;
|
|
break;
|
|
case 200000 ... 480000:
|
|
a = 4.5;
|
|
break;
|
|
default:
|
|
a = 0.53;
|
|
}
|
|
return(a);
|
|
}
|
|
|
|
|
|
|
|
float V_coeff_hi_Chk(uint32_t f1)
|
|
{
|
|
return(0);
|
|
}
|
|
|
|
float I_coeff_lo_Chk(uint32_t f1)
|
|
{
|
|
float a;
|
|
// a = freqArray[Search_for_frq(f1)].i_coeff_lo;
|
|
switch(f1)
|
|
{
|
|
case 1 ... 500:
|
|
a = 1;
|
|
break;
|
|
case 501 ... 870:
|
|
a= 0.8396;
|
|
break;
|
|
case 871 ... 1170:
|
|
a = 0.8105;
|
|
break;
|
|
case 1171 ... 3139:
|
|
a = 0.8634;
|
|
break;
|
|
case 3140 ... 6000:
|
|
a = 0.8634;
|
|
break;
|
|
case 6001 ... 8009:
|
|
a = 0.8634;
|
|
|
|
break;
|
|
case 8010 ... 9820:
|
|
a = 0.8976;
|
|
break;
|
|
case 9821 ... 29430:
|
|
a = 0.8976;
|
|
break;
|
|
case 29431 ... 32768:
|
|
a = 0.8976;
|
|
break;
|
|
case 32769 ... 44100:
|
|
a = 0.91;
|
|
break;
|
|
case 44101 ... 65536:
|
|
a = 1.015;
|
|
break;
|
|
case 65537 ... 199999:
|
|
a = 1.255;
|
|
break;
|
|
case 200000 ... 480000:
|
|
a = 3.111;
|
|
break;
|
|
default:
|
|
a = 0.8976;
|
|
}
|
|
return(a);
|
|
}
|
|
|
|
float I_coeff_hi_Chk(uint32_t f1)
|
|
{
|
|
float a;
|
|
a = freqArray[Search_for_frq(f1)].i_coeff_hi;
|
|
switch(f1)
|
|
{
|
|
case 1 ... 940:
|
|
a= 0.47;
|
|
break;
|
|
case 941 ... 1170:
|
|
a = 0.49;
|
|
break;
|
|
case 1171 ... 6000:
|
|
a = 0.482;
|
|
break;
|
|
|
|
case 6001 ... 8009:
|
|
a = 0.53;
|
|
|
|
break;
|
|
case 8010 ... 9820:
|
|
a = 0.53;
|
|
break;
|
|
case 9821 ... 29430:
|
|
a = 0.69;
|
|
break;
|
|
case 29431 ... 32768:
|
|
a = 0.71333;
|
|
break;
|
|
case 32769 ... 44100:
|
|
a = 1.035;
|
|
break;
|
|
case 44101 ... 65536:
|
|
a = 1.35;
|
|
break;
|
|
case 65537 ... 200000:
|
|
a = 1.36;
|
|
break;
|
|
case 200001 ... 480000:
|
|
a = 2.111;
|
|
break;
|
|
default:
|
|
a = 0.53;
|
|
}
|
|
|
|
return(a);
|
|
}
|
|
|
|
uint8_t Search_for_frq(uint32_t f1)
|
|
{
|
|
uint8_t i =0;
|
|
|
|
for(i=0; i < FREQ_MAX_NUM - 1; i++)
|
|
{
|
|
if((f1 == freqArray[i].frequency1)) //(freqArray[frq_tmp].enabled > 0)
|
|
break;
|
|
|
|
}
|
|
return(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FREQ_ToggleEnable(uint32_t selected) //selected is same as frequency index
|
|
{
|
|
freqArray[selected].enabled ^= 1;
|
|
|
|
}
|
|
|
|
FREQUENCY_t FREQ_GetFreqByIndex(uint32_t index)
|
|
{
|
|
return freqArray[index];
|
|
}
|
|
|
|
uint32_t FREQ_GetNumFrequencies(void)
|
|
{
|
|
return freq.numFrequencies;
|
|
}
|
|
|
|
|
|
uint8_t Next_Frequency(uint8_t frq_tmp) // increments frequency
|
|
{
|
|
|
|
uint8_t found = false;
|
|
uint8_t i;
|
|
|
|
frq_tmp = frequency;
|
|
|
|
if (frq_tmp < FREQ_MAX_NUM - 1)
|
|
frq_tmp++;
|
|
else
|
|
frq_tmp = 0;
|
|
|
|
for(i=0; i < FREQ_MAX_NUM - 1; i++)
|
|
{
|
|
if(Check_freq_enabled(frq_tmp)) //(freqArray[frq_tmp].enabled > 0)
|
|
{
|
|
found = 1;
|
|
break;
|
|
}
|
|
if (frq_tmp < FREQ_MAX_NUM - 1)
|
|
frq_tmp++;
|
|
else
|
|
frq_tmp= 0;
|
|
}
|
|
|
|
|
|
return(frq_tmp);
|
|
}
|
|
|
|
bool Check_freq_enabled(uint8_t frq_tmp)
|
|
{
|
|
bool x;
|
|
|
|
x = false;
|
|
|
|
if(Cur_Mode != BROADCAST && (freqArray[frq_tmp].enabled > 0))
|
|
x= true;
|
|
else
|
|
if ((Cur_Mode == BROADCAST) && (freqArray[frq_tmp].bc_enabled > 0) && (freqArray[frq_tmp].enabled > 0))
|
|
x = true;
|
|
return(x);
|
|
}
|
|
|
|
|
|
/* Generate name for the frequency at the specified index
|
|
* @param index index in the frequency list
|
|
* @param *string pointer to the storage location for the name string
|
|
*/
|
|
void FREQ_GetFrequencyName(uint32_t index, uint8_t *string)
|
|
{
|
|
//Generate frequency name
|
|
// < 1000Hz: "xxxHz"
|
|
// < 10kHz: "x.xxkHz"
|
|
// < 100kHz: "xx.xkHz"
|
|
// >=100kHz: "xxxkHz"
|
|
|
|
uint32_t freqTemp = freqArray[index].frequency1; //the numeric freqTemp
|
|
|
|
if(freqArray[index].frequency2 != 0)
|
|
{
|
|
if(freqTemp < 1000)
|
|
{
|
|
sprintf(string, "LD %dHz", freqTemp);
|
|
}
|
|
else if(freqTemp < 10000)
|
|
{
|
|
sprintf(string, "LD %.2fkHz", (float32_t)freqTemp / 1000.0);
|
|
}
|
|
else
|
|
{
|
|
sprintf(string, "ERROR");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(freqTemp < 1000)
|
|
{
|
|
sprintf(string, "%dHz", freqTemp);
|
|
}
|
|
else if(freqTemp < 10000)
|
|
{
|
|
sprintf(string, "%.2fkHz", (float32_t)freqTemp / 1000.0);
|
|
}
|
|
else if(freqTemp < 100000)
|
|
{
|
|
sprintf(string, "%.1fkHz", (float32_t)freqTemp / 1000.0);
|
|
}
|
|
else if(freqTemp < 500000)
|
|
{
|
|
sprintf(string, "%.0fkHz", (float32_t)freqTemp / 1000.0);
|
|
}
|
|
else
|
|
{
|
|
sprintf(string, "ERROR");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*while(!found && frq_tmp < FREQ_MAX_NUM)
|
|
{
|
|
frq_tmp++;
|
|
|
|
if(freqArray[frq_tmp].enabled > 0)
|
|
found = true;
|
|
|
|
if(frq_tmp >= FREQ_MAX_NUM)
|
|
frq_tmp = FREQ_MIN;
|
|
|
|
} */
|
|
|
|
void Update_Min_Frequency(void)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update_Frequency()
|
|
{
|
|
uint32_t tmp;
|
|
|
|
if (Cur_Mode != BROADCAST) // IF - ! Broadcast
|
|
{
|
|
Enable_Amplifier(); // Select correct amplifier
|
|
Select_Transformer(); // Select correct transformer
|
|
|
|
// tmp = Calc_Freq(freqArray[frequency].frequency1);
|
|
// Load_Frq_Gen(SINGLE,tmp,0); // # update the frequency generators
|
|
|
|
Set_Selected_Freq();
|
|
|
|
Taps_Flag = false;
|
|
|
|
|
|
if((ACCY_GetConnectedAccessory(1) == ID_CLAMP) || (ACCY_GetConnectedAccessory(2) == ID_CLAMP))
|
|
{
|
|
Get_Clamp_Value(); // Ensure correct table for Clamp amplitude
|
|
SPI0_SendBytes(Dds_Pot_Val, 2, AMPLITUDE);
|
|
}
|
|
else
|
|
if ((ACCY_GetConnectedAccessory(1) == ID_CLAMP2) || (ACCY_GetConnectedAccessory(2) == ID_CLAMP2))
|
|
{
|
|
clampData.regulate = true;
|
|
|
|
if (freqArray[frequency].frequency1 < 800)
|
|
{
|
|
clampData.maxPower = MAX_POWER_TARGET_VLF;
|
|
}
|
|
else
|
|
if (freqArray[frequency].frequency1 < 45000)
|
|
{
|
|
clampData.maxPower = MAX_POWER_TARGET_LF;
|
|
}
|
|
else
|
|
{
|
|
clampData.maxPower = MAX_POWER_TARGET_HF;
|
|
}
|
|
|
|
Get_Clamp_Value();
|
|
|
|
clampData.timeout = systemTime + CLAMP_REGULATE_DELAY;
|
|
|
|
if (Power_Level > 0)
|
|
{
|
|
clampData.prevPowerLevel = 0;
|
|
setTestPot();
|
|
}
|
|
|
|
}
|
|
|
|
// Select_Output_Port(ONE); // Switch in the correct pathway
|
|
}
|
|
else // else
|
|
{
|
|
All_Amps_Off(); // shut down Amplifiers DC connections
|
|
Disable_DC();
|
|
Enable_BC(); // Enable BCAST circuitry using either Minimum or previously selected freq
|
|
LD_Flag = false;
|
|
|
|
// Init_PWM(); // update PWM generators
|
|
}
|
|
old_freq = frequency; // Indicate done
|
|
}
|
|
|
|
void Set_Selected_Freq(void)
|
|
{
|
|
uint32_t frq1;
|
|
uint32_t frq2;
|
|
|
|
if(LD_Flag && freqArray[frequency].frequency1 <= MAX_LD_FREQ)
|
|
{
|
|
frq1 = Calc_Freq(freqArray[frequency].frequency1);
|
|
frq2 = freqArray[frequency].frequency1;
|
|
frq2 = frq2 * 2.0;
|
|
// frq2 = 9100; // test only
|
|
frq2 = Calc_Freq(frq2);
|
|
Load_Frq_Gen(DUAL,frq1,frq2); // # update the frequency generators
|
|
|
|
}
|
|
else
|
|
{
|
|
frq1 = Calc_Freq(freqArray[frequency].frequency1);
|
|
frq2 = 0;
|
|
Load_Frq_Gen(SINGLE,frq1,frq2); // # update the frequency generators
|
|
|
|
Send_Ctrl_Word(SLP_CTRL_WRD,SDSIGNAL); // Put unused DDS to sleep
|
|
}
|
|
}
|
|
|
|
void Reset_DDS(void)
|
|
{
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SIG_RST_GPIO_PIN, 1); // Set all RESET signals to RESET
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SD_RST_GPIO_PIN, 1);
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, RAMP_RST_GPIO_PIN, 1);
|
|
Delay_Ticks(1);
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, RAMP_RST_GPIO_PIN, 0);
|
|
GPIO_PinWrite(GPIO, SIGNAL_PORT, SIG_RST_GPIO_PIN, 0); // Set all RESET signals to RESET
|
|
}
|
|
void Change_to_next_dc_frq(void)
|
|
{
|
|
uint32_t tmp_frqx;
|
|
uint8_t count;
|
|
|
|
if((ACCY_GetConnectedAccessory(1) == ID_CLAMP) || (ACCY_GetConnectedAccessory(2) == ID_CLAMP))
|
|
{
|
|
frequency = Next_Frequency(frequency); // increment the frequency
|
|
new_freq = freqArray[frequency].frequency1;
|
|
|
|
if(freqArray[frequency].frequency1 <= MIN_CTYPE)// is frequecny > min
|
|
{
|
|
count = 0;
|
|
|
|
while(freqArray[frequency].frequency1 < MIN_CTYPE && count < FREQ_MAX_NUM)
|
|
{
|
|
frequency = Next_Frequency(frequency); // increment the frequency
|
|
new_freq = freqArray[frequency].frequency1;
|
|
count++;
|
|
|
|
}
|
|
|
|
// tmp_frqx = Search_Frequency(256); // select minimum frequency for DC Clamp operation
|
|
// if (tmp_frqx < FREQ_MAX_NUM) // set up new min freq
|
|
// {
|
|
// frequency = tmp_frqx;
|
|
// new_freq = freqArray[frequency].frequency1;
|
|
// }
|
|
}
|
|
else
|
|
{
|
|
// frequency = Next_Frequency(frequency); // increment the frequency
|
|
new_freq = freqArray[frequency].frequency1;
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
frequency = Next_Frequency(frequency); // increment the frequency
|
|
new_freq = freqArray[frequency].frequency1;
|
|
Sys_Chk_tmr= DELAY_500MS; // Allow system settling before checking measurements
|
|
// catch_up_flag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Change_To_Next_BC_Freq(void)
|
|
{
|
|
uint32_t tmp_frqx;
|
|
|
|
if(Cur_Mode == BROADCAST) // Special case only if BC mode already selected
|
|
{
|
|
if (freqArray[frequency].frequency1 < BCAST_MIN) // if (freq < min freq) ToDo
|
|
|
|
tmp_frqx = Search_Frequency(3140); // select minimum frequency for BCAST
|
|
if (tmp_frqx < FREQ_MAX_NUM)
|
|
{
|
|
frequency = tmp_frqx;
|
|
new_freq = freqArray[frequency].frequency1;
|
|
}
|
|
}
|
|
|
|
}
|