Files
TX/source/init.c

391 lines
7.5 KiB
C

/*
* init.c
*
* Created on: May 20, 2022
* Author: Keith.Lloyd
*/
#include "arm_math.h"
#include <stdbool.h>
#include <stdint.h>
#include "init.h"
#include "taps.h"
#include "measure.h"
#include "ports.h"
#include "utils.h"
#include "frq.h"
#include "adc.h"
#include "mode.h"
#include "psu_ctrl.h"
#include "timer.h"
#include "main.h"
#include "lcd.h"
#include "menu.h"
#include "safety_key.h"
#include "usbComms.h"
#include "flashUpdate.h"
#include "bootloader.h"
#include "eeprom.h"
#include "sys_chk.h"
#include "pwr_level.h"
#include "Graphics/icons.h"
#include "display.h"
#include "pwm.h"
#include "battery.h"
#include "keys.h"
#include "System/system.h"
extern uint8_t OverVolts_Flag,catch_up_flag; // global OverVoltsFlag
extern ADC_t adc;
extern MODE_REC_t mode_Array[MODE_MAX_NUM];
extern uint8_t Cur_Mode, Old_Mode;
extern uint8_t Port_State[];
extern uint32_t what_val1, what_val2;
uint8_t Over_Voltage_Flag;
uint8_t Over_Current_Flag;
uint8_t Hardware_Error_Flag;
uint8_t Taps_Flag, psu_failed;
uint8_t Diag_Flag,LD_Flag;
extern uint8_t init_flag,step_count;
extern uint8_t Task,Tx_Time_Out_Flag, Test_Mode,Ports_Cleared_Flag;
extern uint8_t Power_Level;
extern uint8_t old_freq, frequency, frq_chg_tmr;
extern uint8_t Bat_Type,Bcast_Pwr_Level;
extern uint16_t Port_timer, Taps_adjust_timer;
extern uint8_t Port_changed_flag,i;
extern uint8_t Safety_Select,Suspend_Step_Chk;
uint8_t Safe_Mode, Init_Done;
extern uint16_t Estop_timer,Sys_Chk_tmr,Pot_Value[],Vchktmr,Key_Lock_Out_tmr;
extern uint8_t Dds_Pot_Val[]; // 2 byte Data for SPI
float32_t Max_Power_Limit;
extern uint32_t TX_TIME[];
extern uint32_t Sys_Timer,Tx_timer;
extern float32_t last_Ohms;
extern uint16_t PSU_Check_tmr,Power_tmr;
void Init_vars()
{
Taps_adjust_timer = DELAY_ONE_SECOND;
Task = PRIMARY_TASK;
// Task = MENU_TASK;
Power_Level = 0;
Bcast_Pwr_Level = 0;
frequency = 5;
old_freq = frequency;
Tx_Time_Out_Flag = DISABLED; // Disable time outs for now (temporary)
// Tx_timer = TX_TIME[0]; // reload the timer
Safety_Select = false;
Over_Voltage_Flag = false;
Safe_Mode = false;
Test_Mode = false; //
char *ver = SW_VERSION;
for (int i=0; i < strlen(ver); ++i)
{
if (isalpha(ver[i]))
{
Test_Mode = true;
break;
}
}
Estop_timer = 0;
Sys_Chk_tmr = 0;
Vchktmr = 0;
Key_Lock_Out_tmr = 0;
step_count = 0;
PSU_Check_tmr = PSU_DELAY;
Max_Power_Limit = 5.0; // default until battery type determined.
last_Ohms = 1;
Taps_Flag = false;
catch_up_flag= false;
Ports_Cleared_Flag = false;
psu_failed = false;
Init_Done = false;
Suspend_Step_Chk = false;
Diag_Flag = false; // default for now should be read from EEPROM
LD_Flag = false;
Power_tmr = 0;
}
void Init_peripherals(void)
{
timer_init();
SPI_Init();
LCD_Init();
}
void Init_sys(void)
{
FREQ_Init();
Init_PSU_Pot(); // initialize pot.
Init_Ports(); // Ensure Ports are set to safe mode
MENU_Init();
}
void Init_Ports() // Ensure all ports are set to safe when powering up
{
// [Not BYPASSED,PSU OFF, ALL AMPS SWITCHED OFF, BACKLIGHT ON.]
Select_Estop(ON);
Port_State[TOP_SR] = 0x00; //
Port_State[MID_SR] = 0x00; // U12
Port_State[BOTTOM_SR] = 0x24; // U13 AMP_AB = ON, BKLITE = on PWR SW's off, AMP PSU OFF
SPI0_SendBytes(Port_State, 3, EXPANDER);
Delay_Ticks(2); // execute short delay
}
void Clear_Flags()
{
OverVolts_Flag = false;
}
void Init_Output(void)
{
if(adc.V_CHK < EXCEDED) // Read external voltage disable if present
{
// Read_Tx_Ports(); // check for what's connected is controlled from timer interrupt
Init_Mode(); // default induction mode
Update_Min_Frequency(); // determine frequency to be applied from last or if clamp or broadcast mode
Init_Amplitude(); // set amplitude to minimum
// Measure_Ohms(); // Done calculate Ohms
// Check_Taps(); // Done determine optimum taps etc
} //
else
Estop_Mode(); //
}
void Init_Mode()
{
uint8_t i;
// initialize until E2PROM fitted
for(i = 0; i <= MODE_MAX_NUM; i++ )
{
mode_Array[i].Selected = true;
mode_Array[i].Plugged = true;
}
mode_Array[BROADCAST].Selected = true; //default BCAST always available
mode_Array[BROADCAST].Plugged = BROADCAST;
mode_Array[PORT1_A].Plugged = ID_TX_SINGLE_DIRECT;
mode_Array[PORT1_B].Plugged = EMPTY;
mode_Array[PORT2_A].Plugged = ID_TX_DUAL_DIRECT;
mode_Array[PORT2_B].Plugged = ID_TX_DUAL_DIRECT;
mode_Array[PORT2_A].Selected = false;
mode_Array[PORT2_B].Selected = false;
Cur_Mode = BROADCAST;
Old_Mode = Cur_Mode;
Read_Tx_Ports(); // now scan external ports
}
void Init_Amplitude(void) // set amplitude to minimum
{
// if(Cur_Mode != BROADCAST)
// {
Dds_Pot_Val[0] = 0; // address
Dds_Pot_Val[1] = Pot_Value[INIT_PWR_LEVEL]; // data
SPI0_SendBytes(Dds_Pot_Val, 2, AMPLITUDE);
// }
}
void Init_PWM_CLKS(void)
{
if(freqArray[frequency].bc_Mask == MASK_144)
Init_Int_Clk();
else
Init_Ext_Clk();
}
void Init_Ext_Clk(void)
{
Port_State[TOP_SR] &= CLK_MASK_BITS;
Port_State[TOP_SR] |= freqArray[frequency].bc_Mask;
SPI0_SendBytes(Port_State, 3, EXPANDER);
}
void Init_Int_Clk(void)
{
}
void Init_PSU_Pot(void) // Set PSU_POT half way approx 23V
{
Port_State[TOP_SR] = 0x0; //TODO Ensure PSU DISABLED
Port_State[MID_SR] = 0x00; //Port_State[MID_SR] | 0x04;
Port_State[BOTTOM_SR] = 0x24;
SPI0_SendBytes(Port_State, 3, EXPANDER);
Set_PSU_Voltage(MAX_AB_PSU); //MID_POINT_PSU 18,24,27,30,36, 55
Delay_Ticks(2);
Port_State[TOP_SR] = 0x0; //TODO ENABLE PSU
Port_State[MID_SR] = 0x00;//Port_State[MID_SR] & 0xfb;
Port_State[BOTTOM_SR] = 0x24;
SPI0_SendBytes(Port_State, 3, EXPANDER);
}
void Normal_Init(void)
{
Power_ON_OFF(ON); // Enable_Psu(); Ensure Power supply stays switched on.
Select_Estop(ON); // Ensure output is ISOLATED from connections
Init_vars();
// Init_peripherals(); 2/9/24
// Init_sys(); 2/9/24
BL_ReadInfo();
EE_LoadData(); //Read saved data
Display_Splash(); //Display splash screen
Delay_Ticks(200); // execute short delay
KEY_Init(); //Init keys after splash delay to prevent POWER short press on startup
// USB_Init();
// Read_Tx_Ports(); // Read output ports and determine what is connected
// Select_Amplifier(); // Select correct amplifier according to frequency
Init_Mode(); //
//init_PWM();
// PWM_Setup(32768, BC_Duty_Cycle);//freqArray[frequency].frequency1
// PWM_Setup(29430, BC_Duty_Cycle);
PWM_Setup(15890, 0); // switches off PWM
Safety_Check(); // Check all voltages are safe to continue in DC.
Check_Bat_Id(); // Check for Alkaline or Lithium and battery insertion error.
what_val1=0;
what_val2=0;
Cur_Mode = PORT2_A;
init_flag = 0;
for( i=0; i < 15; i++);
{
Delay_Ticks(10);
// ADC_Update();
Read_Tx_Ports(); // check for whats plugged in at the ports every 100mS.
}
if ((adc.V_ID2 > 3.0) && (adc.V_ID1 > 3.0))
{
Cur_Mode = BROADCAST;
Port_changed_flag= 1;
init_flag = 1;
}
Disconnect(2);
Delay_Ticks(30);
Check_For_Clamp_On_Pwr_Up();
Select_Output_Port();
Safety_Check(); // Second time J.I.C Check all voltages are safe to continue in DC.
Normal_Bypass_Chk();
old_freq = DUMMY_FRQ; //force a frequency on initialization
frq_chg_tmr = 0;
Update_Frequency();
// Select_Bypass(OFF);
Select_Estop(OFF); // Ensure output is ISOLATED from connections
Init_Amplitude();
}
void Init_Pwr_Level_One(void)
{
if(!Init_Done)
{
Delay_Ticks(10); // Wait for Current to catch up
inc_pwr(); // Set power level to 1
Init_Done = true;
}
}
void Init_LD_Sync(void)
{
Update_Frequency();
}