215 lines
4.4 KiB
C
215 lines
4.4 KiB
C
/*
|
|
* init.c
|
|
*
|
|
* Created on: May 20, 2022
|
|
* Author: Keith.Lloyd
|
|
*/
|
|
|
|
#include "arm_math.h"
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <ctype.h>
|
|
|
|
#include "init.h"
|
|
#include "taps.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"
|
|
#include "soft_timer.h"
|
|
|
|
extern uint8_t OverVolts_Flag,catch_up_flag; // global OverVoltsFlag
|
|
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
|
|
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;
|
|
|
|
extern SYSTEM_DATA_t sys;
|
|
|
|
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;
|
|
sys.status[OVERVOLTAGE] = 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;
|
|
}
|
|
}
|
|
|
|
stimer_init();
|
|
|
|
sys.h10HzTimer = stimer_start(100, TIMER_MODE_PERIODIC, NULL, NULL);
|
|
sys.h1HzTimer = stimer_start(1000, TIMER_MODE_PERIODIC, NULL, NULL);
|
|
|
|
Estop_timer = 0;
|
|
Vchktmr = 0;
|
|
Key_Lock_Out_tmr = 0;
|
|
step_count = 0;
|
|
PSU_Check_tmr = PSU_DELAY;
|
|
sys.maxPowerLimit = POWER_LIMIT_ALKALINE; // 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(ISOLATED);
|
|
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_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_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 Init_LD_Sync(void)
|
|
{
|
|
Update_Frequency();
|
|
}
|
|
|
|
|