Lots of refactoring
This commit is contained in:
204
source/ports.c
204
source/ports.c
@@ -29,13 +29,13 @@
|
||||
#include "fsl_sctimer.h"
|
||||
#include "pwm.h"
|
||||
#include "pwr_level.h"
|
||||
#include "System/system.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;
|
||||
@@ -45,19 +45,123 @@ extern uint16_t Pot_Value_AB[];
|
||||
extern uint16_t Pot_Value[];
|
||||
extern uint8_t Dds_Pot_Val[];
|
||||
|
||||
extern SYSTEM_DATA_t sys;
|
||||
|
||||
uint8_t whatever;
|
||||
|
||||
static int handleBroadcast(ACCESSORY_t *accy)
|
||||
{
|
||||
switch (accy->state)
|
||||
{
|
||||
case PORT_STATE_INIT:
|
||||
break;
|
||||
|
||||
case PORT_STATE_DEINIT:
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handleClamp(ACCESSORY_t *accy)
|
||||
{
|
||||
switch (accy->state)
|
||||
{
|
||||
case PORT_STATE_INIT:
|
||||
break;
|
||||
|
||||
case PORT_STATE_DEINIT:
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handleDirect(ACCESSORY_t *accy)
|
||||
{
|
||||
switch (accy->state)
|
||||
{
|
||||
case PORT_STATE_INIT:
|
||||
break;
|
||||
|
||||
case PORT_STATE_DEINIT:
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int initAccessory(ACCESSORY_t *accy)
|
||||
{
|
||||
accy->state = PORT_STATE_INIT;
|
||||
accy->initState = true;
|
||||
accy->handler(accy);
|
||||
}
|
||||
|
||||
static int deinitAccessory(ACCESSORY_t *accy)
|
||||
{
|
||||
accy->state = PORT_STATE_DEINIT;
|
||||
accy->initState = true;
|
||||
accy->handler(accy);
|
||||
}
|
||||
|
||||
void ACCY_Connect(ACCESSORY_t *accy, ACCY_ID_t id)
|
||||
{
|
||||
accy->connected = id;
|
||||
accy->isConnected = true;
|
||||
|
||||
accy->state = PORT_STATE_INIT;
|
||||
|
||||
accy->channels[0] = 0;
|
||||
accy->channels[1] = 0;
|
||||
|
||||
switch (id)
|
||||
{
|
||||
case ID_TX_SINGLE_DIRECT:
|
||||
case ID_TX_DUAL_DIRECT:
|
||||
{
|
||||
accy->handler = handleDirect;
|
||||
break;
|
||||
}
|
||||
|
||||
case ID_CLAMP:
|
||||
case ID_CLAMP2:
|
||||
{
|
||||
accy->handler = handleClamp;
|
||||
break;
|
||||
}
|
||||
|
||||
case ID_BROADCAST:
|
||||
{
|
||||
accy->handler = handleBroadcast;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
accy->handler = NULL;
|
||||
accy->connected = ID_NONE;
|
||||
accy->isConnected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
if (port >= NUM_PORTS)
|
||||
{
|
||||
return ID_NONE;
|
||||
}
|
||||
|
||||
uint32_t idNumber = (uint32_t)((idVoltage * 3.3333) + 0.25); //multiply by 3.3333 and round down to nearest integer
|
||||
ACCESSORY_t *accy = &sys.ports[port];
|
||||
|
||||
uint32_t idNumber = (uint32_t)((*accy->idVoltage * 3.3333f) + 0.5f); //multiply by 3.3333 and round down to nearest integer
|
||||
|
||||
|
||||
return (ACCY_ID_t)idNumber;
|
||||
@@ -65,7 +169,7 @@ static ACCY_ID_t ReadAccessory(uint8_t port)
|
||||
|
||||
void Disconnect(uint8_t port) // called when you disconnect
|
||||
{
|
||||
if (port ==1)
|
||||
if (port == ACCY_PORT_1)
|
||||
{
|
||||
accy1.connected = ID_NONE;
|
||||
accy1.consecutiveScans = 0;
|
||||
@@ -135,6 +239,17 @@ void Disconnect(uint8_t port) // called when you disconnect
|
||||
}
|
||||
void ACCY_Init(void)
|
||||
{
|
||||
|
||||
for (int i=0; i<NUM_PORTS; ++i)
|
||||
{
|
||||
sys.ports[i].connected = ID_NONE;
|
||||
sys.ports[i].consecutiveScans = 0;
|
||||
sys.ports[i].isConnected = false;
|
||||
}
|
||||
|
||||
sys.ports[ACCY_PORT_1].idVoltage = &sys.adc.V_ID1;
|
||||
sys.ports[ACCY_PORT_2].idVoltage = &sys.adc.V_ID2;
|
||||
|
||||
accy1.connected = ID_NONE;
|
||||
accy1.consecutiveScans = 0;
|
||||
accy1.isConnected = false;
|
||||
@@ -149,6 +264,40 @@ void ACCY_Init(void)
|
||||
//Setup accessory for GPIO gain control
|
||||
// InitAccessoryGainGPIO();
|
||||
}
|
||||
void ACCY_Update(void)
|
||||
{
|
||||
|
||||
for (int i=0; i<NUM_PORTS; ++i)
|
||||
{
|
||||
ACCESSORY_t *accy = &sys.ports[i];
|
||||
ACCY_ID_t detected = ReadAccessory(i);
|
||||
|
||||
if(accy->connected == ID_NONE) //Nothing connected. Look for accessory to connect
|
||||
{
|
||||
|
||||
if((detected == accy->lastDetected) && (detected != ID_NONE)) //If detected same as last time and not ID_NONE
|
||||
{
|
||||
|
||||
if(++accy->consecutiveScans == AC_NUM_SCANS_TO_CONNECT) //Connect on 3rd consecutive scan of same accessory
|
||||
{
|
||||
ACCY_Connect(accy, detected);
|
||||
}
|
||||
}
|
||||
else //If different than last scan
|
||||
{
|
||||
accy->lastDetected = detected; //remember what was scanned last time
|
||||
accy->consecutiveScans = 0;
|
||||
}
|
||||
|
||||
}
|
||||
else if (detected != accy->connected) //If connected and detected reads different, disconnect
|
||||
{
|
||||
Disconnect(i);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Call @ 10Hz to detect, connect, and disconnect accessories
|
||||
void ACCY_Update1(void)
|
||||
{
|
||||
@@ -180,7 +329,7 @@ void ACCY_Update1(void)
|
||||
}
|
||||
else if (detected != accy1.connected) //If connected and detected reads different, disconnect
|
||||
{
|
||||
Disconnect(1);
|
||||
Disconnect(ACCY_PORT_1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -217,13 +366,30 @@ void ACCY_Update2(void)
|
||||
}
|
||||
else if (detected != accy2.connected) //If connected and detected reads different, disconnect
|
||||
{
|
||||
Disconnect(2);
|
||||
Disconnect(ACCY_PORT_2);
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Connect accessory
|
||||
*/
|
||||
bool ACCY_isKnown(ACCY_ID_t id)
|
||||
{
|
||||
bool known = false;
|
||||
|
||||
switch(id)
|
||||
{
|
||||
case ID_TX_SINGLE_DIRECT:
|
||||
case ID_TX_DUAL_DIRECT:
|
||||
case ID_CLAMP:
|
||||
case ID_CLAMP2:
|
||||
known = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return known;
|
||||
}
|
||||
|
||||
void ACCY_Connect1(ACCY_ID_t id)
|
||||
{
|
||||
accy1.connected = id;
|
||||
@@ -319,12 +485,14 @@ ACCY_ID_t ACCY_GetActive(void)
|
||||
|
||||
void Read_Tx_Ports(void) // check for whats plugged in at the ports every 100mS.
|
||||
{
|
||||
uint8_t chip;
|
||||
uint8_t chip;
|
||||
|
||||
Port_timer = DELAY_100MS;
|
||||
Port_timer = DELAY_100MS;
|
||||
|
||||
|
||||
ACCY_Update1();
|
||||
ACCY_Update();
|
||||
|
||||
//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
|
||||
@@ -354,7 +522,7 @@ uint8_t chip;
|
||||
}
|
||||
|
||||
|
||||
ACCY_Update2();
|
||||
//ACCY_Update2();
|
||||
if((mode_Array[PORT2_A].Selected == false) && (mode_Array[PORT2_B].Selected == false) && (accy2.isConnected)) //new bit here
|
||||
Cur_Mode = PORT2_A;
|
||||
|
||||
@@ -396,14 +564,15 @@ void Select_Output_Port(void) // which DC Out port, to switch on
|
||||
{
|
||||
Delay_Ticks(DELAY_100MS);
|
||||
|
||||
if(Compare_Voltage(adc.V_CHK, MAX_BYPASS_VOLTS))
|
||||
if(Compare_Voltage(sys.adc.V_CHK, MAX_BYPASS_VOLTS))
|
||||
{
|
||||
Over_Voltage_Flag = true;
|
||||
sys.status[OVERVOLTAGE] = true;
|
||||
Select_Bypass(OFF); // Force into blocked state.
|
||||
Delay_Ticks(20);
|
||||
|
||||
}
|
||||
|
||||
// HACK
|
||||
Select_Estop(OFF); // Ensure output is not ISOLATED from connections
|
||||
|
||||
switch (Cur_Mode)
|
||||
@@ -615,6 +784,7 @@ void Disable_BC(void)
|
||||
// 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))
|
||||
|
||||
Reference in New Issue
Block a user