You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
15 KiB
C++

/*****************************************************************************
* File Name:app_main.c
* Author:ZD
* Brief:
* Note:
* Last Updated for Version:1.0
* Date of the Last Update:18-06-2020
*****************************************************************************/
#include <math.h>
#include <stdint.h>
#include "framework.h"
#include "F28335/Flash2833x_API_Config.h"
#include "F28335/Flash2833x_API_Library.h"
#include "SYSCTRL/TypeControl.h"
#include "SYSCTRL/SystemControl.h"
#include "FLASH/FLASHHeaders.h"
/*****************************************************************************
*****************************************************************************/
#include "FRAM/FRAMDATABASE.h"
#include "FRAM/FRAMDataObjects.h"
#include "FRAM/FRAMVariant.h"
#include "FRAM/ROM.h"
/*****************************************************************************
*****************************************************************************/
float TISR = periodtriangle;
float cellVoltage[3][13];
CellState cellState[3][13];
Uint16 cellVersion[3][13];
Uint16 pwmVersion[3];
Uint32 cpu_cpld_version;
/*****************************************************************************
*****************************************************************************/
#define TIMEINTERVALBREAKRESETMODBUS (Uint16)(100/0.4)
Uint16 endlesscounter = 0;
Uint16 endlesscounter_epwm = 0;
Uint16 counter_break_state = 0;
Uint16 counter_reset = 0;
stateHandle *state_modbus;
stateHandle *state_modbus_init;
enum MODBUS_RTU_EVENT_RS
{
ENTYR = 1, // state entry event
EXIT = 2, // state exit event
TIMEOUT3_5CHAR = 3, // All datagram have been received
TIMEOUT1_5CHAR = 4, // Interval time of each byte in datagram. Not used
RX_READY = 5, // SCIRXST.bit.RXRDY. Serial port received a serial data (one byte)
TX_READY = 6, // All datagram have been sent
TX_WAIT = 7 // Wait for the datagram to be sent
};
//
/*****************************************************************************
*****************************************************************************/
//Uint16 m_readCoils[300] = {0};
//Uint16 m_writeCoils[300] = {0};
#define LENGHTMODBUSBUFFER 30
Uint16 m_readRegisters[LENGHTMODBUSBUFFER] = {0};
Uint16 m_writeRegisters[LENGHTMODBUSBUFFER] = {0};
Uint16 counter_current_faults = 0;
//Uint16 m_canData_cpu2com_coils[16] = { 0 };
//Uint16 m_canData_cpu2com_registers[50] = { 0 };
//Uint16 m_canData_com2cpu_coils[16] = { 0 };
//Uint16 m_canData_com2cpu_registers[50] = { 0 };
/*****************************************************************************
*****************************************************************************/
//FLASH::TestFlashDataBase default_configuration;
//FLASH::FLASHACCESS flash_local;
//FLASH::FLASHDATABASEConfiguration flash_config(flash_local);
//SYSCTRL::SystemControlSetup dvr_setup;
//SYSCTRL::SystemControlConfiguration dvr_config;
SYSCTRL::SystemControl dvr_control = SYSCTRL::SystemControl();
#define MAGIC_NUMBER_TIMER1 ((Uint32)6)
#define MAGIC_NUMBER_TIMER2 ((Uint32)6)
Uint32 timer1_result = (Uint32)0;
Uint32 timer2_result = (Uint32)0;
Uint32 timer1_magic = MAGIC_NUMBER_TIMER1;
Uint32 timer2_magic = MAGIC_NUMBER_TIMER2;
void measure_timer1_reset();
void measure_timer2_reset();
inline void measure_timer1_start();
inline void measure_timer2_start();
inline void measure_timer1_stop(Uint32 magic, Uint32& result);
inline void measure_timer2_stop(Uint32 magic, Uint32& result);
void WDReset(void);
/*****************************************************************************
******************************************************************************/
/*****************************************************************************
** Global Function Declare
*****************************************************************************/
interrupt void epwm1_isr(void);
interrupt void reload_isr(void);
void service_routine();
void modbus_registers_config();
/*****************************************************************************
** Local Function Declare
*****************************************************************************/
void main(void)
{
InitSysCtrl();
InitEPwm();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW;
PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.USER1 = &reload_isr; //User defined Interrupt
EDIS;
InitCpuTimers();
ConfigCpuTimer(&CpuTimer0, 150, TISR);
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
EDIS;
InitEPwm1();
InitEPwm5();
InitEPwm6();
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;
// symbols are created by the linker. Refer to the F28335.cmd file.
MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
MemCopy(&MainLoopLoadStart, &MainLoopLoadEnd, &MainLoopRunStart);
// Copy the Flash API functions to SARAM
//MemCopy(&Flash28_API_LoadStart, &Flash28_API_LoadEnd, &Flash28_API_RunStart);
DINT;
Flash_CPUScaleFactor = SCALE_FACTOR;
InitFlash();
// flash.setup(SECTORH, (Uint16*) 0x300000, (Uint16*) 0x307FFF);
IER |= M_INT3;
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
Gpio_setup1();
InitXintf();
InitECanGpio();
InitECan();
InitSci();
InitEQep1Gpio();
EQep1Regs.QDECCTL.bit.QSRC = 00;
EQep1Regs.QDECCTL.bit.XCR = 0;
EQep1Regs.QEPCTL.bit.FREE_SOFT = 2;
EQep1Regs.QPOSMAX = 0xffffffff;
EQep1Regs.QEPCTL.bit.QPEN = 1;
EQep1Regs.QPOSCNT = 0x00000000;
frameworkDataInit(); // Initialize global Variable
etmp.signal=0;
modbusCtor(&modbusHMI,eBufferHMI, &modbusHMIPort);
modbusInit(&modbusHMI,&etmp);
modbusSlaveBufferSet(
&modbusHMI,
OUTPUTCOILS,
readCoils,
sizeof(readCoils)/sizeof(Uint16),
20); // Set to the starting address you need
modbusSlaveBufferSet(
&modbusHMI,
INPUTCOILS,
writeCoils,
sizeof(writeCoils)/sizeof(Uint16),
20); // Set to the starting address you need
modbusSlaveBufferSet(
&modbusHMI,
OUTPUTREGISTERS,
readRegisters,
sizeof(readRegisters)/sizeof(Uint16),
310); // Set to the starting address you need
modbusSlaveBufferSet(
&modbusHMI,
INPUTREGISTERS,
writeRegisters,
sizeof(writeRegisters)/sizeof(Uint16),
310); // Set to the starting address you need
measure_timer1_reset();
measure_timer2_reset();
dvr_control.get_hard_code_setup();
//dvr_control.get_hard_code_configuration();
dvr_control.upload_configuration();
dvr_control.setup();
dvr_control.configure();
dvr_control.setup_hvcell();
state_modbus_init = &modbusHMI.super.initState;
//flash_local.setup(SECTORG, (Uint16*)0x308000, (Uint16*)0x30FFFF);
//flash_config.register_configuration_parameters(&test_configuration);
//flash_config.load_flash(&test_configuration);
//framdb.read_from_fram_to_buffer();
//framdb.register_configuration_parameters();
//framdb.get_dafault_configuration();
//framdb.upload_configuration();
EINT;
ERTM;
//
// Watchdog Setup
//
ServiceDog();
EALLOW;
SysCtrlRegs.WDCR= 0x002C;
EDIS;
ServiceDog();
//
service_routine();
//
}//end main()
//
#pragma CODE_SECTION("ramfuncs");
void service_routine()
{
while(1)
{
/***********************************************************************/
// Detect Hardware Current Cell Fault
if((GpioDataRegs.GPADAT.bit.GPIO12 == 1) || (GpioDataRegs.GPADAT.bit.GPIO13 == 1) || (GpioDataRegs.GPADAT.bit.GPIO20 == 1))
{
counter_current_faults++;
dvr_control.hardware_analog_current_fault.signal.phase_a = GpioDataRegs.GPADAT.bit.GPIO12;
dvr_control.hardware_analog_current_fault.signal.phase_b = GpioDataRegs.GPADAT.bit.GPIO13;
dvr_control.hardware_analog_current_fault.signal.phase_c = GpioDataRegs.GPADAT.bit.GPIO20;
dvr_control.hardware_analog_current_fault.signal.fault = 1;
}//if
if(!dvr_control.hardware_analog_current_fault.signal.vs && dvr_control.hardware_analog_current_fault.signal.fault)
{
dvr_control.protection_thyristor_control();
digitalOutput.all = (Uint32)dvr_control.get_digital_output();
writeDigitalOutput(digitalOutput.all); // Write digital output
sendControlOrder(ORDER_START);
sendModulateData(0,0,0); // Send modulate data
//
}//if
//
/***********************************************************************/
ECanbRx(); // Receiving communication board data
modbusRun(&modbusHMI); // Running MODBUS state machine
/****************************user code*************************************/
// Add your code here
//measure_timer2_start();
//measure_timer2_stop(timer2_magic, timer2_result);
//
// MODBUS Service
dvr_control.modbusrtu_rubus_interface(readRegisters, writeRegisters);
//
if(dvr_control.additional_loop_is_active())
{
dvr_control.execute_additional();
dvr_control.set_hvcell_level();
dvr_control.get_hvcell_version();
dvr_control.get_pwm_version();
dvr_control.get_cpu_cpld_version();
dvr_control.get_hvcell_state();
dvr_control.hardware_diagnostic();
//
}//if
//
if(dvr_control.slow_loop_is_active())
{
digitalInput.all = readDigitalInput(); // Read digital input
//
digitalOutput.all = (Uint32)dvr_control.get_digital_output();
writeDigitalOutput(digitalOutput.all); // Write digital output
//
dvr_control.process_digital_input(digitalInput);
dvr_control.slow_loop_execute();
//
// Analog output
setAnalogOutput2001_2005(a2001_2005); // analog output
setAnalogOutput2002_2006(a2002_2006); // analog output
setAnalogOutput2003_2007(a2003_2007); // analog output
setAnalogOutput2004_2008(a2004_2008); // analog output
//
//
}//if
//
if(dvr_control.symmetrical_calculator_loop_is_active())
{
dvr_control.execute_symmetrical_calculator();
//
}//if
//
/*
for(int16_t i=0; i<LENGHTMODBUSBUFFER; i++)
{
m_readRegisters[i] = readRegisters[i];
m_writeRegisters[i] = writeRegisters[i];
//
}//
*/
//
endlesscounter++;
ServiceDog();
/****************************user code end*********************************/
}
}//end service_routine()
//
#pragma CODE_SECTION("ramfuncs");
interrupt void epwm1_isr(void)
{
/**********************************************/
endlesscounter_epwm++;
measure_timer2_stop(timer2_magic, timer2_result);
measure_timer2_start();
measure_timer1_start();
sendSynSignal(); // Send synchronization signal
Date_ComBoard(); // Send data to communication board
modbusTimerClick(&modbusHMI); // Run modbus timer
//
analogRead(&analogInput); // analog input read
//
//setCellLevel(1); // Set cell level (Example 5)
//getCellDCVoltage(1, cellVoltage); // Get cell DC voltage
//getCellState(1, cellState); // Get cell state
//getCellVersion(1, cellVersion); // Get cell version
//getPwmVersion(pwmVersion); // Get pwm version
//sendModulateData(0,0,0); // Send modulate data
//cpuCpldVersion = (Uint16)getCpuCpldVersion();
//cpu_cpld_version = (Uint32)getCpuCpldVersion();
//
// Cell operation
dvr_control.get_hvcell_dc_voltage();
/****************************user code*************************************/
if(IS_PWM_DETECTED_FAULT)
{
// user must handle this error.
// If pwm detected any error from cells or pwm board, this flag is set.
// And user must lead the VFD to fault state, and call
// sendControlOrder(ORDER_STOP) to stop cells.
sendControlOrder(ORDER_STOP);
// Add your code here
}
// Add your code here
// DVR operation
dvr_control.process_analog_input(analogInput);
dvr_control.execute();
dvr_control.send_hvcell_modulate_data();
dvr_control.send_hvcell_control_order();
//
measure_timer1_stop(timer1_magic, timer1_result);
/********* check MODBUS BREAK MODE *************************************/
state_modbus = &modbusHMI.super.currentState;
if(*state_modbus == *state_modbus_init)
{
counter_break_state++;
}
else
{
counter_break_state = 0;
}//
//
if(counter_break_state >= TIMEINTERVALBREAKRESETMODBUS)
{
counter_break_state = 0;
counter_reset++;
dvr_control.inc_break_counter();
modbusInit(&modbusHMI,&etmp);
}
/****************************user code end*********************************/
EPwm1Regs.ETCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
/*****************************************************************************
** No more.
****************************************************************************/
//
#pragma CODE_SECTION("ramfuncs");
interrupt void reload_isr(void)
{
//
WDReset();
//
}//
//
#pragma CODE_SECTION("ramfuncs");
void measure_timer1_reset()
{
//
CpuTimer1Regs.TCR.bit.TSS = 1; // Stop CPU Timer
CpuTimer1Regs.PRD.all = 0xFFFFFFFF;
CpuTimer1Regs.TCR.bit.TRB = 1; // Reload CPU Timer
//
}//
//
#pragma CODE_SECTION("ramfuncs");
void measure_timer2_reset()
{
//
CpuTimer2Regs.TCR.bit.TSS = 1; // Stop CPU Timer
CpuTimer2Regs.PRD.all = 0xFFFFFFFF;
CpuTimer2Regs.TCR.bit.TRB = 1; // Reload CPU Timer
//
}//
//
#pragma CODE_SECTION("ramfuncs");
inline void measure_timer1_start()
{
//
CpuTimer1Regs.TCR.bit.TSS = 1; // Stop CPU Timer;
CpuTimer1Regs.TCR.bit.TRB = 1; // Reload CPU Timer;
CpuTimer1Regs.TCR.bit.TSS = 0; // Start CPU Timer;
CpuTimer1.InterruptCount++;
//
}//
//
#pragma CODE_SECTION("ramfuncs");
inline void measure_timer2_start()
{
//
CpuTimer2Regs.TCR.bit.TSS = 1; // Stop CPU Timer;
CpuTimer2Regs.TCR.bit.TRB = 1; // Reload CPU Timer;
CpuTimer2Regs.TCR.bit.TSS = 0; // Start CPU Timer;
CpuTimer2.InterruptCount++;
//
}//
//
#pragma CODE_SECTION("ramfuncs");
void measure_timer1_stop(Uint32 magic, Uint32& result)
{
//
CpuTimer1Regs.TCR.bit.TSS = 1; // Stop CPU Timer
//
result = CpuTimer1Regs.PRD.all - CpuTimer1Regs.TIM.all - magic;
//
}//
//
#pragma CODE_SECTION("ramfuncs");
void measure_timer2_stop(Uint32 magic, Uint32& result)
{
//
CpuTimer2Regs.TCR.bit.TSS = 1; // Stop CPU Timer
//
result = CpuTimer2Regs.PRD.all - CpuTimer2Regs.TIM.all - magic;
//
}//
//
void WDReset(void)
{
EALLOW;
SysCtrlRegs.WDCR= 0x0078;
EDIS;
}
//