initial check in based on SVN revision 575

This commit is contained in:
2025-05-14 12:57:39 -05:00
commit a3ef12e24a
217 changed files with 95547 additions and 0 deletions

625
source/ports.c Normal file
View File

@@ -0,0 +1,625 @@
/*
* ports.c
*
* Created on: Mar 21, 2023
* Author: Keith.Lloyd
*/
/*
* Return accessory associated with voltage read
*/
#include "fsl_spi.h"
#include "pin_mux.h"
#include "board.h"
#include "fsl_debug_console.h"
#include <stdbool.h>
#include <stdint.h>
#include "arm_math.h"
#include "ports.h"
#include "frq.h"
#include "spi.h"
#include "display.h"
#include "utils.h"
#include "adc.h"
#include "mode.h"
#include "psu_ctrl.h"
#include "timer.h"
#include "init.h"
#include "amps.h"
#include "fsl_sctimer.h"
#include "pwm.h"
#include "pwr_level.h"
ACCESSORY_t accy1;
ACCESSORY_t accy2;
uint16_t Port_timer;
uint16_t Taps_adjust_timer;
extern uint8_t Port_changed_flag,Init_Done;
extern ADC_t adc;
extern uint8_t Port_State[],Cur_Mode,old_freq,Bcast_Pwr_Level,Bcast_LF_Value[],Bcast_HF_Value[],Power_Level;
extern MODE_REC_t mode_Array[MODE_MAX_NUM];
extern uint8_t frequency,Over_Voltage_Flag;
extern FREQUENCY_t freqArray[FREQ_MAX_NUM];
extern volatile uint8_t BC_Duty_Cycle;
extern uint16_t Pot_Value_AB[];
extern uint16_t Pot_Value[];
extern uint8_t Dds_Pot_Val[];
uint8_t whatever;
static ACCY_ID_t ReadAccessory(uint8_t port)
{
float32_t idVoltage = 0.0;
if(port == 1)
idVoltage = adc.V_ID1; //idVoltage = ANA_GetVAccyDetect(); //TODO: Write this function
else
idVoltage = adc.V_ID2;
//ID 0 through 11. Voltage target is ID * 0.3v
uint32_t idNumber = (uint32_t)((idVoltage * 3.3333) + 0.25); //multiply by 3.3333 and round down to nearest integer
return (ACCY_ID_t)idNumber;
}
void Disconnect(uint8_t port) // called when you disconnect
{
if (port ==1)
{
accy1.connected = ID_NONE;
accy1.consecutiveScans = 0;
accy1.isConnected = false;
//Change Mode if this accessory was selected
if(mode_Array[PORT1_A].Selected || mode_Array[PORT1_B].Selected)
{
//disconnect port 1 from keith's struct
mode_Array[PORT1_A].Selected = 0;
mode_Array[PORT1_B].Selected = 0;
if ((Cur_Mode > BROADCAST) && (Cur_Mode ==PORT1_A) || (Cur_Mode == PORT1_B) ) //and change mode
Cur_Mode = Next_Available_Mode(Cur_Mode); // find next Mode available
}
else
{
//disconnect port 1 from keith's struct
mode_Array[PORT1_A].Selected = 0;
mode_Array[PORT1_B].Selected = 0;
}
}
else //port 2
{
accy2.connected = ID_NONE;
accy2.consecutiveScans = 0;
accy2.isConnected = false;
//Change Mode if this accessory was selected
if(mode_Array[PORT2_A].Selected || mode_Array[PORT2_B].Selected)
{
//disconnect port 2 from keith's struct
mode_Array[PORT2_A].Selected = 0;
mode_Array[PORT2_B].Selected = 0;
if ((Cur_Mode > BROADCAST) && (Cur_Mode == PORT2_A) || (Cur_Mode == PORT2_B) ) //and change mode
// Cur_Mode = Next_Available_Mode(Cur_Mode); // find next Mode available
{
if (mode_Array[PORT1_A].Selected || mode_Array[PORT1_B].Selected) //and change mode
Cur_Mode = PORT1_A;
else
Cur_Mode = Next_Available_Mode(Cur_Mode); // find next Mode available
}
//
// else
// Cur_Mode = 1;
// if (Cur_Mode > 0)
// {
// if (mode_Array[1].Selected || mode_Array[2].Selected) //and change mode
// Cur_Mode = 1;
// else
// Cur_Mode = Next_Available_Mode(Cur_Mode); // find next Mode available
// }
}
else
{
//disconnect port 1 from keith's struct
mode_Array[PORT2_A].Selected = 0;
mode_Array[PORT2_B].Selected = 0;
}
}
Port_changed_flag = true; // added 10/2/23
Init_Done = false; // 3/20/24 Force a Power Level One change
//Configure frequency to make sure things are setup correctly
// Tx_ConfigureFrequency();
}
void ACCY_Init(void)
{
accy1.connected = ID_NONE;
accy1.consecutiveScans = 0;
accy1.isConnected = false;
accy2.connected = ID_NONE;
accy2.consecutiveScans = 0;
accy2.isConnected = false;
Port_timer = DELAY_100MS;
Port_changed_flag = false;
//Setup accessory for GPIO gain control
// InitAccessoryGainGPIO();
}
//Call @ 10Hz to detect, connect, and disconnect accessories
void ACCY_Update1(void)
{
//read ID voltage and determine connected accessory *for each port*
//Require same accessory detected 3 consecutive updates (to de-glitch)
//Read accessory voltage and identify accessory
ACCY_ID_t detected = ReadAccessory(1);
if(accy1.connected == ID_NONE) //Nothing connected. Look for accessory to connect
{
if((detected == accy1.lastDetected) && (detected != ID_NONE)) //If detected same as last time and not ID_NONE
{
if(++accy1.consecutiveScans == AC_NUM_SCANS_TO_CONNECT) //Connect on 3rd consecutive scan of same accessory
{
ACCY_Connect1(accy1.lastDetected); //CONNECT
}
}
else //If different than last scan
{
accy1.lastDetected = detected; //remember what was scanned last time
accy1.consecutiveScans = 0;
}
}
else if (detected != accy1.connected) //If connected and detected reads different, disconnect
{
Disconnect(1);
}
}
//Call @ 10Hz to detect, connect, and disconnect accessories
void ACCY_Update2(void)
{
//read ID voltage and determine connected accessory *for each port*
//Require same accessory detected 3 consecutive updates (to de-glitch)
//Read accessory voltage and identify accessory
ACCY_ID_t detected = ReadAccessory(2);
whatever = ReadAccessory(2);
if(accy2.connected == ID_NONE) //Nothing connected. Look for accessory to connect
{
if((detected == accy2.lastDetected) && (detected != ID_NONE)) //If detected same as last time and not ID_NONE
{
if(++accy2.consecutiveScans == AC_NUM_SCANS_TO_CONNECT) //Connect on 3rd consecutive scan of same accessory
{
ACCY_Connect2(accy2.lastDetected); //CONNECT
}
}
else //If different than last scan
{
accy2.lastDetected = detected; //remember what was scanned last time
accy2.consecutiveScans = 0;
}
}
else if (detected != accy2.connected) //If connected and detected reads different, disconnect
{
Disconnect(2);
}
}
/*
* Connect accessory
*/
void ACCY_Connect1(ACCY_ID_t id)
{
accy1.connected = id;
accy1.isConnected = true;
switch(id)
{
case ID_TX_SINGLE_DIRECT:
Port_changed_flag = true; //connect it
break;
case ID_TX_DUAL_DIRECT:
Port_changed_flag = true; //connect it
break;
case ID_CLAMP:
Port_changed_flag = true; //connect it
break;
case ID_CLAMP2:
Port_changed_flag = true; //connect it
break;
default:
accy1.connected = ID_NONE;
accy1.isConnected = false;
}
//Configure frequency to make sure things are setup correctly
// Tx_ConfigureFrequency();
}
void ACCY_Connect2(ACCY_ID_t id)
{
accy2.connected = id;
accy2.isConnected = true;
switch(id)
{
case ID_TX_SINGLE_DIRECT:
Port_changed_flag = true;//connect it
break;
case ID_TX_DUAL_DIRECT:
Port_changed_flag = true;//connect it
break;
case ID_CLAMP:
Port_changed_flag = true;//connect it
break;
case ID_CLAMP2:
Port_changed_flag = true;//connect it
break;
default:
accy2.connected = ID_NONE;
accy2.isConnected = false;
}
//Configure frequency to make sure things are setup correctly
// Tx_ConfigureFrequency();
}
bool ACCY_IsConnected1(uint8_t port)
{
if(port == 1)
return accy1.isConnected;
else
return accy2.isConnected;
}
ACCY_ID_t ACCY_GetConnectedAccessory(uint8_t port)
{
if(port == 1)
return accy1.connected;
else
return accy2.connected;
}
ACCY_ID_t ACCY_GetActive(void)
{
if(Cur_Mode == PORT1_A)
{
return ACCY_GetConnectedAccessory(1);
}
else
{
return ACCY_GetConnectedAccessory(2);
}
}
void Read_Tx_Ports(void) // check for whats plugged in at the ports every 100mS.
{
uint8_t chip;
Port_timer = DELAY_100MS;
ACCY_Update1();
if((mode_Array[PORT1_A].Selected == false) && (mode_Array[PORT1_B].Selected == false) && (accy1.isConnected)) //new bit here
Cur_Mode = PORT1_A; // Something is connected to PORT1
switch(accy1.connected) // Find out what it is
{
case ID_TX_DUAL_DIRECT: //Transmitter accessory
mode_Array[PORT1_A].Selected = accy1.isConnected;
mode_Array[PORT1_B].Selected = accy1.isConnected;
break;
case ID_TX_SINGLE_DIRECT:
mode_Array[PORT1_A].Selected = accy1.isConnected;
break;
case ID_CLAMP:
mode_Array[PORT1_A].Selected = accy1.isConnected;
break;
case ID_CLAMP2:
mode_Array[PORT1_A].Selected = accy1.isConnected;
break;
default: //empty
mode_Array[PORT1_A].Selected = accy1.isConnected;
mode_Array[PORT1_B].Selected = accy1.isConnected;
}
ACCY_Update2();
if((mode_Array[PORT2_A].Selected == false) && (mode_Array[PORT2_B].Selected == false) && (accy2.isConnected)) //new bit here
Cur_Mode = PORT2_A;
switch(accy2.connected)
{
case ID_TX_DUAL_DIRECT: //Transmitter accessory
mode_Array[PORT2_A].Selected = accy2.isConnected;
mode_Array[PORT2_B].Selected = accy2.isConnected;
break;
case ID_TX_SINGLE_DIRECT:
mode_Array[PORT2_A].Selected = accy2.isConnected;
break;
case ID_CLAMP:
mode_Array[PORT2_A].Selected = accy2.isConnected;
break;
case ID_CLAMP2:
mode_Array[PORT2_A].Selected = accy2.isConnected;
break;
default: //empty
mode_Array[PORT2_A].Selected = accy2.isConnected;
mode_Array[PORT2_B].Selected = accy2.isConnected;
}
}
void Select_Output_Port(void) // which DC Out port, to switch on
{
Port_State[MID_SR] &= OUT_RELAY_OFF_MASK;
if (Cur_Mode != BROADCAST)
{
Delay_Ticks(DELAY_100MS);
if(Compare_Voltage(adc.V_CHK, MAX_BYPASS_VOLTS))
{
Over_Voltage_Flag = true;
Select_Bypass(OFF); // Force into blocked state.
Delay_Ticks(20);
}
Select_Estop(OFF); // Ensure output is not ISOLATED from connections
switch (Cur_Mode)
{
case PORT1_A:
Port_State[MID_SR] |= PORT1A_ON;
break;
case PORT1_B:
Port_State[MID_SR] |= PORT1B_ON;
break;
case PORT2_A:
Port_State[MID_SR] |= PORT2A_ON;
break;
case PORT2_B:
Port_State[MID_SR] |= PORT2B_ON;
break;
default:
Port_State[MID_SR] |= PORT2B_ON; // TODO Later disconnect??
break;
}
Disable_BC();
Reset_Power_Gain();
Port_changed_flag = false;
SPI0_SendBytes(Port_State, 3, EXPANDER);
}
else
{ // Assumes broadcast mode has just been selected
Disable_DC(); // Shut down Direct connect circuitry
Select_Estop(ON); // Ensure output is ISOLATED from connections
Tx_ConfigureFrequency(); // Select correct frequency
Enable_BC(); // Enable BCAST circuitry using either Minimum or previously selected freq
Port_changed_flag = false;
}
}
void Reset_Power_Gain(void)
{
Power_Level = 0;
Bcast_Pwr_Level = 0;
// if( (ACCY_GetConnectedAccessory(1) != ID_CLAMP) && (ACCY_GetConnectedAccessory(2) != ID_CLAMP))
if(!Check_For_Clamp())
{
if (freqArray[frequency].frequency1 <= MAX_DTYPE)
Dds_Pot_Val[1] = Pot_Value[Power_Level]; // data
else
Dds_Pot_Val[1] = Pot_Value_AB[Power_Level]; // data
}
else
Get_Clamp_Value();
Dds_Pot_Val[0] = 0; // address
SPI0_SendBytes(Dds_Pot_Val, 2, AMPLITUDE);
Delay_Ticks(30); // Set power out level 1 3/20/24
// Power_Level = 1;
// if(Init_Done)
// {
// Delay_Ticks(10); // Set power out level 1 3/20/24
// Power_Level++;
// inc_pwr();
Init_Done = false;
// }
}
void Tx_ConfigureFrequency(void)
{
uint32_t tmp_frq;
if (freqArray[frequency].bc_enabled == false && Cur_Mode == BROADCAST) // if (freq < min freq) ToDo
{
tmp_frq = Search_BC_Frequency(); // select minimum frequency for BCAST
if (tmp_frq < FREQ_MAX_NUM)
frequency = tmp_frq;
//endif
}
}
uint32_t Search_BC_Frequency(void)
{
uint32_t idx;
idx = 0;
while (freqArray[idx].bc_enabled == false && idx < FREQ_MAX_NUM)
idx++;
if (idx >= FREQ_MAX_NUM)
idx = FREQ_MAX_NUM;
return(idx);
}
uint32_t Search_Frequency(uint32_t pattern)
{
uint32_t idx;
idx = 0;
while (freqArray[idx].frequency1 != pattern && idx < FREQ_MAX_NUM)
idx++;
if (idx >= FREQ_MAX_NUM)
idx = FREQ_MAX_NUM;
return(idx);
}
void Disable_DC(void)
{
All_Amps_Off(); // shut down Amplifiers DC connections
// Shut off all amplifier enables
// Reset_DDS(); // Stop all DDS chips
// Set_PSU_Voltage(V_18V); //MID_POINT_PSU 18,24,27,30,36, 55
// Set PSU to MIN.
}
void Enable_BC(void)
{
// if((freqArray[old_freq].frequency1 >8010 ) && (freqArray[frequency].frequency1 < 8010))
// Cycled_Freq_Change_BC(LF);
// else
// {
// if((freqArray[old_freq].frequency1 < 8010 ) && (freqArray[frequency].frequency1 > 8010))
// Cycled_Freq_Change_BC(HF);
// }
if(freqArray[frequency].frequency1 <= 8010)
BC_Duty_Cycle = Bcast_LF_Value[Bcast_Pwr_Level];
else
BC_Duty_Cycle = Bcast_HF_Value[Bcast_Pwr_Level];
PWM_Setup(freqArray[frequency].frequency1, BC_Duty_Cycle);//freqArray[frequency].frequency1
Port_State[MID_SR] |=ANT_AMP_EN; // Enable Antenna amp
// Port_State[BOTTOM_SR] |= ANT_AMP_PWR; // Enable PWR Switch
Port_State[TOP_SR] |= ANT_AMP_SW;
SPI0_SendBytes(Port_State, 3, EXPANDER); // Update shift register
Delay_Ticks(5);
Set_PSU_Voltage(V_27V); // Set PSU to MAX
Port_State[BOTTOM_SR] &= AMP_PSU_ON; // switch AMP PSU on
SPI0_SendBytes(Port_State, 3, EXPANDER); // Update shift register
}
void Cycled_Freq_Change_BC(uint8_t value)
{
if(value == LF)
{
if(Bcast_Pwr_Level == MAX_BCAST_PWR)
Bcast_Pwr_Level--;
BC_Duty_Cycle = 10;
BC_Duty_Cycle = BC_Duty_Cycle + Bcast_Pwr_Level*5;
}
else
{
BC_Duty_Cycle = 10;
BC_Duty_Cycle = BC_Duty_Cycle + Bcast_Pwr_Level*10;
}
}
void Enable_DC(void)
{
// Switch on correct amplifier enables
// Switch on correct power switches
// Enable correct DDS chips
// Set PSU to Medium.
}
void Disable_BC(void)
{
Port_State[MID_SR] &=~ANT_AMP_EN; // disable Antenna amp
// Port_State[BOTTOM_SR] |=ANT_AMP_PWR; // Enable PWR Switch
Port_State[TOP_SR] &= ~ANT_AMP_SW;
SPI0_SendBytes(Port_State, 3, EXPANDER); // Update shift register
// Change the SCT clock back here
PWM_Setup(15890, 0); // switches off PWM // Set duty cycle to zero
Delay_Ticks(5);
Set_PSU_Voltage(V_24V); // Set PSU to MIN
// Init_PWM_CLKS(DC_CLK); // Return timer clock to internal
}
bool Is_Clamp_Detected(void)
{
if((ACCY_GetConnectedAccessory(1) == ID_CLAMP) || (ACCY_GetConnectedAccessory(2) == ID_CLAMP) || (ACCY_GetConnectedAccessory(1) == ID_CLAMP2) || (ACCY_GetConnectedAccessory(2) == ID_CLAMP2))
return(true);
else
return(false);
}