From 08127003f052310c4924e634f2467352b562aa6d Mon Sep 17 00:00:00 2001 From: Dominykas Date: Wed, 31 Jan 2024 15:41:20 +0200 Subject: [PATCH 1/4] Deduplicate MCU_BD code between GUI code and MCU_BD code --- src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp | 703 +------------------ src/lms7002_wxgui/lms7002_pnlMCU_BD_view.h | 62 +- src/lms7002m/MCU_BD.cpp | 371 +++------- src/lms7002m/MCU_BD.h | 10 +- 4 files changed, 159 insertions(+), 987 deletions(-) diff --git a/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp b/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp index d54f19596..f97d2da1d 100644 --- a/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp +++ b/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp @@ -370,8 +370,6 @@ lms7002_pnlMCU_BD_view::lms7002_pnlMCU_BD_view(wxWindow* parent, wxWindowID id) mThreadWorking = false; progressPooler = new wxTimer(this, wxNewId()); - m_iRegAddress = 1; - m_iRegRead = 1; m_iTestNo = 0; m_bLoadedDebug = 0; m_bLoadedProd = 0; @@ -383,9 +381,6 @@ lms7002_pnlMCU_BD_view::lms7002_pnlMCU_BD_view(wxWindow* parent, wxWindowID id) m_iMode0 = 0; m_iMode1 = 0; - m_iRegAddress = 1; - m_iRegRead = 1; - RunInstr->Enable(false); ResetPC->Enable(false); InstrNo->Enable(false); @@ -401,11 +396,18 @@ lms7002_pnlMCU_BD_view::lms7002_pnlMCU_BD_view(wxWindow* parent, wxWindowID id) InstrNo->SetLabel(_("1")); m_sTestNo->SetLabel(_("1")); chkReset->SetValue(true); - mLoadedProgramFilename = ""; - m_iLoopTries = 20; + obj_ptr = this; } +void lms7002_pnlMCU_BD_view::Initialize(ControllerType* pControl) +{ + lmsControl = pControl; + + mcu = lmsControl->GetMCUControls(); + mcu->SetCallback(&lms7002_pnlMCU_BD_view::OnProgrammingCallback); +} + lms7002_pnlMCU_BD_view::~lms7002_pnlMCU_BD_view() { if (mThreadWorking) @@ -420,60 +422,6 @@ lms7002_pnlMCU_BD_view::~lms7002_pnlMCU_BD_view() } } -/** @brief Read program code from file into memory - @param inFileName source file path - @param bin binary or hex file - @return 0:success, -1:file not found -*/ -int lms7002_pnlMCU_BD_view::GetProgramCode(const char* inFileName, bool bin) -{ - unsigned char ch = 0x00; - bool find_byte = false; - size_t i = 0; - - if (!bin) - { - MCU_File inFile(inFileName, "rb"); - if (inFile.FileOpened() == false) - return -1; - - mLoadedProgramFilename = inFileName; - try - { - inFile.ReadHex(max_array_size - 1); - } catch (...) - { - return -1; - } - - for (i = 0; i < max_array_size; i++) - { - find_byte = inFile.GetByte(i, ch); - if (find_byte == true) - byte_array[i] = ch; - else - byte_array[i] = 0x00; - }; - } - else - { - char inByte = 0; - std::fstream fin; - fin.open(inFileName, std::ios::in | std::ios::binary); - if (fin.good() == false) - return -1; - mLoadedProgramFilename = inFileName; - memset(byte_array, 0, max_array_size); - for (size_t i = 0; i < max_array_size && !fin.eof(); ++i) - { - inByte = 0; - fin.read(&inByte, 1); - byte_array[i] = inByte; - } - } - return 0; -} - void lms7002_pnlMCU_BD_view::OnButton_LOADHexClick(wxCommandEvent& event) { wxFileDialog dlg(this, @@ -488,9 +436,9 @@ void lms7002_pnlMCU_BD_view::OnButton_LOADHexClick(wxCommandEvent& event) wxString m_sHexFileName = dlg.GetPath(); int status = 0; if (dlg.GetFilterIndex() == 0) - status = GetProgramCode(m_sHexFileName.mb_str(), false); + status = mcu->GetProgramCode(m_sHexFileName.mb_str(), false); else - status = GetProgramCode(m_sHexFileName.mb_str(), true); + status = mcu->GetProgramCode(m_sHexFileName.mb_str(), true); if (status != 0) { @@ -652,270 +600,6 @@ void lms7002_pnlMCU_BD_view::OnbtnLoadTestFileClick(wxCommandEvent& event) // end debugging } -void lms7002_pnlMCU_BD_view::Wait_CLK_Cycles(int delay) -{ - //// some delay - int i = 0; - uint16_t val; - for (i = 0; i < (delay / 64); i++) - LMS_ReadLMSReg(lmsControl, 0x0003, &val); -} - -void lms7002_pnlMCU_BD_view::RunTest_MCU(int m_iMode1, int m_iMode0, unsigned short test_code, int m_iDebug) -{ - - int i = 0; - int limit = 0; - unsigned short tempi = 0x0000; - unsigned short basei = 0x0000; - - if (test_code <= 15) - basei = (test_code << 4); - else - basei = 0x0000; - - basei = basei & 0xFFF0; // not necessery - // 4 LSBs are zeros - - // variable basei contains test no. value at bit positions 7-4 - // used for driving the P0 input - // P0 defines the test no. - - if ((test_code > 7) || (test_code == 0)) - limit = 1; - else - limit = 50; - - // tests 8 to 14 have short duration - - if (m_iDebug == 1) - return; // normal MCU operating mode required - - // EXT_INT2=1, external interrupt 2 is raised - tempi = 0x0000; // changed - int m_iExt2 = 1; - - if (m_iExt2 == 1) - tempi = tempi | 0x0004; - if (m_iMode1 == 1) - tempi = tempi | 0x0002; - if (m_iMode0 == 1) - tempi = tempi | 0x0001; - - // tempi variable is driving the mspi_REG2 - - LMS_WriteLMSReg(lmsControl, 0x8002, tempi); // REG2 write - - // generating waveform - for (i = 0; i <= limit; i++) - { - tempi = basei | 0x000C; - LMS_WriteLMSReg(lmsControl, 0x8000, tempi); - // REG0 write - Wait_CLK_Cycles(256); - tempi = basei | 0x000D; - LMS_WriteLMSReg(lmsControl, 0x8000, tempi); - // REG0 write - P0(0) set - Wait_CLK_Cycles(256); - tempi = basei | 0x000C; - LMS_WriteLMSReg(lmsControl, 0x8000, tempi); - // REG0 write - Wait_CLK_Cycles(256); - tempi = basei | 0x000E; - LMS_WriteLMSReg(lmsControl, 0x8000, tempi); - // REG0 write - PO(1) set - Wait_CLK_Cycles(256); - - if (i == 0) - { - // EXT_INT2=0 - // external interrupt 2 is pulled down - tempi = 0x0000; // changed - m_iExt2 = 0; - if (m_iExt2 == 1) - tempi = tempi | 0x0004; - if (m_iMode1 == 1) - tempi = tempi | 0x0002; - if (m_iMode0 == 1) - tempi = tempi | 0x0001; - LMS_WriteLMSReg(lmsControl, 0x8002, tempi); - // REG2 write - } - } -} - -void lms7002_pnlMCU_BD_view::DebugModeSet_MCU(int m_iMode1, int m_iMode0) -{ - unsigned short tempi = 0x00C0; - // bit DEBUG is set - int m_iExt2 = 0; - if (m_iExt2 == 1) - tempi = tempi | 0x0004; - if (m_iMode1 == 1) - tempi = tempi | 0x0002; - if (m_iMode0 == 1) - tempi = tempi | 0x0001; - - // Select debug mode - LMS_WriteLMSReg(lmsControl, 0x8002, tempi); - // REG2 write -} - -void lms7002_pnlMCU_BD_view::DebugModeExit_MCU(int m_iMode1, int m_iMode0) -{ - - unsigned short tempi = 0x0000; // bit DEBUG is zero - int m_iExt2 = 0; - - if (m_iExt2 == 1) - tempi = tempi | 0x0004; - if (m_iMode1 == 1) - tempi = tempi | 0x0002; - if (m_iMode0 == 1) - tempi = tempi | 0x0001; - // To run mode - LMS_WriteLMSReg(lmsControl, 0x8002, tempi); // REG2 write -} - -int lms7002_pnlMCU_BD_view::WaitUntilWritten() -{ - - // waits if WRITE_REQ (REG3[2]) flag is equal to '1' - // this means that write operation is in progress - unsigned short tempi = 0x0000; - int countDown = m_iLoopTries; // Time out value - LMS_ReadLMSReg(lmsControl, 0x0003, &tempi); // REG3 read - - while (((tempi & 0x0004) == 0x0004) && (countDown > 0)) - { - LMS_ReadLMSReg(lmsControl, 0x0003, &tempi); // REG3 read - countDown--; - } - if (countDown == 0) - return -1; // an error occured, timer elapsed - else - return 0; // Finished regularly - // pass if WRITE_REQ is '0' -} - -int lms7002_pnlMCU_BD_view::ReadOneByte(unsigned char* data) -{ - unsigned short tempi = 0x0000; - int countDown = m_iLoopTries; - - // waits when READ_REQ (REG3[3]) flag is equal to '0' - // this means that there is nothing to read - LMS_ReadLMSReg(lmsControl, 0x0003, &tempi); // REG3 read - - while (((tempi & 0x0008) == 0x0000) && (countDown > 0)) - { - // wait if READ_REQ is '0' - LMS_ReadLMSReg(lmsControl, 0x0003, &tempi); // REG3 read - countDown--; - } - - if (countDown > 0) - { // Time out has not occured - LMS_ReadLMSReg(lmsControl, 0x0005, &tempi); - ; // REG5 read - // return the read byte - (*data) = (unsigned char)(tempi); - } - else - (*data) = 0; - // return the zero, default value - - if (countDown == 0) - return -1; // an error occured - else - return 0; // finished regularly -} - -int lms7002_pnlMCU_BD_view::One_byte_command(unsigned short data1, unsigned char* rdata1) -{ - unsigned char tempc = 0x00; - int retval = 0; - *rdata1 = 0x00; //default return value - - // sends the one byte command - LMS_WriteLMSReg(lmsControl, 0x8004, data1); //REG4 write - retval = WaitUntilWritten(); - if (retval == -1) - return -1; - // error if operation executes too long - - // gets the one byte answer - retval = ReadOneByte(&tempc); - if (retval == -1) - return -1; - // error if operation takes too long - - *rdata1 = tempc; - return 0; -} - -int lms7002_pnlMCU_BD_view::ResetPC_MCU() -{ - unsigned char tempc1 = 0x00; - int retval = 0; - retval = One_byte_command(0x70, &tempc1); - return retval; -} - -int lms7002_pnlMCU_BD_view::Three_byte_command(unsigned char data1, - unsigned char data2, - unsigned char data3, - unsigned char* rdata1, - unsigned char* rdata2, - unsigned char* rdata3) -{ - - int retval = 0; - *rdata1 = 0x00; - *rdata2 = 0x00; - *rdata3 = 0x00; - - LMS_WriteLMSReg(lmsControl, 0x8004, (unsigned short)data1); - retval = WaitUntilWritten(); - if (retval == -1) - return -1; - LMS_WriteLMSReg(lmsControl, 0x8004, (unsigned short)data2); //REG4 write - retval = WaitUntilWritten(); - if (retval == -1) - return -1; - - LMS_WriteLMSReg(lmsControl, 0x8004, (unsigned short)data3); //REG4 write - retval = WaitUntilWritten(); - if (retval == -1) - return -1; - - retval = ReadOneByte(rdata1); - if (retval == -1) - return -1; - - retval = ReadOneByte(rdata2); - if (retval == -1) - return -1; - - retval = ReadOneByte(rdata3); - if (retval == -1) - return -1; - - return 0; -} - -int lms7002_pnlMCU_BD_view::RunInstr_MCU(unsigned short* pPCVAL) -{ - unsigned char tempc1, tempc2, tempc3 = 0x00; - int retval = 0; - retval = Three_byte_command(0x74, 0x00, 0x00, &tempc1, &tempc2, &tempc3); - if (retval == -1) - (*pPCVAL) = 0; - else - (*pPCVAL) = tempc2 * 256 + tempc3; - return retval; -} - void lms7002_pnlMCU_BD_view::OnbtnRunTestClick(wxCommandEvent& event) { wxString m_sTxtFileName = _("lms7suite_mcu/TestResults.txt"); @@ -971,12 +655,12 @@ void lms7002_pnlMCU_BD_view::OnbtnRunTestClick(wxCommandEvent& event) } else { - RunTest_MCU(m_iMode1, m_iMode0, m_iTestNo, m_iDebug); + mcu->RunTest_MCU(m_iMode1, m_iMode0, m_iTestNo, m_iDebug); } // check the results if (m_iDebug == 0) - DebugModeSet_MCU(m_iMode1, m_iMode0); + mcu->DebugModeSet_MCU(m_iMode1, m_iMode0); // Go to Debug mode m_iError = 0; i = 0; @@ -984,7 +668,7 @@ void lms7002_pnlMCU_BD_view::OnbtnRunTestClick(wxCommandEvent& event) { if (TestResultArray_code[i] == m_iTestNo) { - retval = Three_byte_command(0x78, (unsigned char)(TestResultArray_address[i]), 0x00, &tempc1, &tempc2, &tempc3); + retval = mcu->Three_byte_command(0x78, (unsigned char)(TestResultArray_address[i]), 0x00, &tempc1, &tempc2, &tempc3); if ((retval == -1) || (tempc3 != TestResultArray_value[i])) m_iError = 1; else @@ -994,7 +678,7 @@ void lms7002_pnlMCU_BD_view::OnbtnRunTestClick(wxCommandEvent& event) i++; }; //exit the debug mode - DebugModeExit_MCU(m_iMode1, m_iMode0); + mcu->DebugModeExit_MCU(m_iMode1, m_iMode0); wxString temps, temps1, temps2, temps3; if (m_iError == 1) @@ -1028,7 +712,7 @@ void lms7002_pnlMCU_BD_view::OnDebugModeClick(wxCommandEvent& event) SelDiv->Enable(true); m_iDebug = 1; - DebugModeSet_MCU(m_iMode1, m_iMode0); + mcu->DebugModeSet_MCU(m_iMode1, m_iMode0); } else { @@ -1041,7 +725,7 @@ void lms7002_pnlMCU_BD_view::OnDebugModeClick(wxCommandEvent& event) SelDiv->Enable(false); m_iDebug = 0; - DebugModeExit_MCU(m_iMode1, m_iMode0); + mcu->DebugModeExit_MCU(m_iMode1, m_iMode0); } } @@ -1062,7 +746,7 @@ void lms7002_pnlMCU_BD_view::OnRunInstruction(wxCommandEvent& event) { for (i = 0; i < m_iInstrNo; i++) { - retval = RunInstr_MCU(&m_iPC); + retval = mcu->RunInstr_MCU(&m_iPC); if (retval == -1) { i = m_iInstrNo; // end loop @@ -1080,271 +764,11 @@ void lms7002_pnlMCU_BD_view::OnRunInstruction(wxCommandEvent& event) void lms7002_pnlMCU_BD_view::OnResetPCClick(wxCommandEvent& event) { - int retval = ResetPC_MCU(); + int retval = mcu->ResetPC_MCU(); if (retval == -1) wxMessageBox(_("Unable to reset MCU's Program Counter")); } -int lms7002_pnlMCU_BD_view::Read_SFR() -{ - int i = 0; - unsigned char tempc1, tempc2, tempc3 = 0x00; - int retval = 0; - - //default m_SFR array initialization - for (i = 0; i <= 255; i++) - m_SFR[i] = 0x00; - OnProgrammingCallback(0, 48, ""); - // code 0x7A is for reading the SFR registers - retval = Three_byte_command(0x7A, 0x80, 0x00, &tempc1, &tempc2, &tempc3); // P0 - if (retval == -1) - return -1; - m_SFR[0x80] = tempc3; - - retval = Three_byte_command(0x7A, 0x81, 0x00, &tempc1, &tempc2, &tempc3); // SP - if (retval == -1) - return -1; - m_SFR[0x81] = tempc3; - - retval = Three_byte_command(0x7A, 0x82, 0x00, &tempc1, &tempc2, &tempc3); // DPL0 - if (retval == -1) - return -1; - m_SFR[0x82] = tempc3; - - retval = Three_byte_command(0x7A, 0x83, 0x00, &tempc1, &tempc2, &tempc3); // DPH0 - if (retval == -1) - return -1; - m_SFR[0x83] = tempc3; - - retval = Three_byte_command(0x7A, 0x84, 0x00, &tempc1, &tempc2, &tempc3); // DPL1 - if (retval == -1) - return -1; - m_SFR[0x84] = tempc3; - - retval = Three_byte_command(0x7A, 0x85, 0x00, &tempc1, &tempc2, &tempc3); // DPH1 - if (retval == -1) - return -1; - m_SFR[0x85] = tempc3; - OnProgrammingCallback(6, 48, ""); - - retval = Three_byte_command(0x7A, 0x86, 0x00, &tempc1, &tempc2, &tempc3); // DPS - if (retval == -1) - return -1; - m_SFR[0x86] = tempc3; - - retval = Three_byte_command(0x7A, 0x87, 0x00, &tempc1, &tempc2, &tempc3); // PCON - if (retval == -1) - return -1; - m_SFR[0x87] = tempc3; - - retval = Three_byte_command(0x7A, 0x88, 0x00, &tempc1, &tempc2, &tempc3); // TCON - if (retval == -1) - return -1; - m_SFR[0x88] = tempc3; - - retval = Three_byte_command(0x7A, 0x89, 0x00, &tempc1, &tempc2, &tempc3); // TMOD - if (retval == -1) - return -1; - m_SFR[0x89] = tempc3; - - retval = Three_byte_command(0x7A, 0x8A, 0x00, &tempc1, &tempc2, &tempc3); // TL0 - if (retval == -1) - return -1; - m_SFR[0x8A] = tempc3; - - retval = Three_byte_command(0x7A, 0x8B, 0x00, &tempc1, &tempc2, &tempc3); // TL1 - if (retval == -1) - return -1; - m_SFR[0x8B] = tempc3; - OnProgrammingCallback(12, 48, ""); - - retval = Three_byte_command(0x7A, 0x8C, 0x00, &tempc1, &tempc2, &tempc3); // TH0 - if (retval == -1) - return -1; - m_SFR[0x8C] = tempc3; - - retval = Three_byte_command(0x7A, 0x8D, 0x00, &tempc1, &tempc2, &tempc3); // TH1 - if (retval == -1) - return -1; - m_SFR[0x8D] = tempc3; - - retval = Three_byte_command(0x7A, 0x8E, 0x00, &tempc1, &tempc2, &tempc3); // PMSR - if (retval == -1) - return -1; - m_SFR[0x8E] = tempc3; - - retval = Three_byte_command(0x7A, 0x90, 0x00, &tempc1, &tempc2, &tempc3); // P1 - if (retval == -1) - return -1; - m_SFR[0x90] = tempc3; - - retval = Three_byte_command(0x7A, 0x91, 0x00, &tempc1, &tempc2, &tempc3); // DIR1 - if (retval == -1) - return -1; - m_SFR[0x91] = tempc3; - - retval = Three_byte_command(0x7A, 0x98, 0x00, &tempc1, &tempc2, &tempc3); // SCON - if (retval == -1) - return -1; - m_SFR[0x98] = tempc3; - OnProgrammingCallback(18, 48, ""); - - retval = Three_byte_command(0x7A, 0x99, 0x00, &tempc1, &tempc2, &tempc3); // SBUF - if (retval == -1) - return -1; - m_SFR[0x99] = tempc3; - - retval = Three_byte_command(0x7A, 0xA0, 0x00, &tempc1, &tempc2, &tempc3); // P2 - if (retval == -1) - return -1; - m_SFR[0xA0] = tempc3; - - retval = Three_byte_command(0x7A, 0xA1, 0x00, &tempc1, &tempc2, &tempc3); // DIR2 - if (retval == -1) - return -1; - m_SFR[0xA1] = tempc3; - - retval = Three_byte_command(0x7A, 0xA2, 0x00, &tempc1, &tempc2, &tempc3); // DIR0 - if (retval == -1) - return -1; - m_SFR[0xA2] = tempc3; - - retval = Three_byte_command(0x7A, 0xA8, 0x00, &tempc1, &tempc2, &tempc3); // IEN0 - if (retval == -1) - return -1; - m_SFR[0xA8] = tempc3; - - retval = Three_byte_command(0x7A, 0xA9, 0x00, &tempc1, &tempc2, &tempc3); // IEN1 - if (retval == -1) - return -1; - m_SFR[0xA9] = tempc3; - OnProgrammingCallback(24, 48, ""); - - retval = Three_byte_command(0x7A, 0xB0, 0x00, &tempc1, &tempc2, &tempc3); // EECTRL - if (retval == -1) - return -1; - m_SFR[0xB0] = tempc3; - - retval = Three_byte_command(0x7A, 0xB1, 0x00, &tempc1, &tempc2, &tempc3); // EEDATA - if (retval == -1) - return -1; - m_SFR[0xB1] = tempc3; - - retval = Three_byte_command(0x7A, 0xB8, 0x00, &tempc1, &tempc2, &tempc3); // IP0 - if (retval == -1) - return -1; - m_SFR[0xB8] = tempc3; - - retval = Three_byte_command(0x7A, 0xB9, 0x00, &tempc1, &tempc2, &tempc3); // IP1 - if (retval == -1) - return -1; - m_SFR[0xB9] = tempc3; - - retval = Three_byte_command(0x7A, 0xBF, 0x00, &tempc1, &tempc2, &tempc3); // USR2 - if (retval == -1) - return -1; - m_SFR[0xBF] = tempc3; - - retval = Three_byte_command(0x7A, 0xC0, 0x00, &tempc1, &tempc2, &tempc3); // IRCON - if (retval == -1) - return -1; - m_SFR[0xC0] = tempc3; - OnProgrammingCallback(30, 48, ""); - - retval = Three_byte_command(0x7A, 0xC8, 0x00, &tempc1, &tempc2, &tempc3); // T2CON - if (retval == -1) - return -1; - m_SFR[0xC8] = tempc3; - - retval = Three_byte_command(0x7A, 0xCA, 0x00, &tempc1, &tempc2, &tempc3); // RCAP2L - if (retval == -1) - return -1; - m_SFR[0xCA] = tempc3; - - retval = Three_byte_command(0x7A, 0xCB, 0x00, &tempc1, &tempc2, &tempc3); // RCAP2H - if (retval == -1) - return -1; - m_SFR[0xCB] = tempc3; - - retval = Three_byte_command(0x7A, 0xCC, 0x00, &tempc1, &tempc2, &tempc3); // TL2 - if (retval == -1) - return -1; - m_SFR[0xCC] = tempc3; - - retval = Three_byte_command(0x7A, 0xCD, 0x00, &tempc1, &tempc2, &tempc3); // TH2 - if (retval == -1) - return -1; - m_SFR[0xCD] = tempc3; - - retval = Three_byte_command(0x7A, 0xD0, 0x00, &tempc1, &tempc2, &tempc3); // PSW - if (retval == -1) - return -1; - m_SFR[0xD0] = tempc3; - OnProgrammingCallback(36, 48, ""); - - retval = Three_byte_command(0x7A, 0xE0, 0x00, &tempc1, &tempc2, &tempc3); // ACC - if (retval == -1) - return -1; - m_SFR[0xE0] = tempc3; - - retval = Three_byte_command(0x7A, 0xF0, 0x00, &tempc1, &tempc2, &tempc3); // B - if (retval == -1) - return -1; - m_SFR[0xF0] = tempc3; - - retval = Three_byte_command(0x7A, 0xEC, 0x00, &tempc1, &tempc2, &tempc3); // REG0 - if (retval == -1) - return -1; - m_SFR[0xEC] = tempc3; - - retval = Three_byte_command(0x7A, 0xED, 0x00, &tempc1, &tempc2, &tempc3); // REG1 - if (retval == -1) - return -1; - m_SFR[0xED] = tempc3; - - retval = Three_byte_command(0x7A, 0xEE, 0x00, &tempc1, &tempc2, &tempc3); // REG2 - if (retval == -1) - return -1; - m_SFR[0xEE] = tempc3; - - retval = Three_byte_command(0x7A, 0xEF, 0x00, &tempc1, &tempc2, &tempc3); // REG3 - if (retval == -1) - return -1; - m_SFR[0xEF] = tempc3; - OnProgrammingCallback(42, 48, ""); - retval = Three_byte_command(0x7A, 0xF4, 0x00, &tempc1, &tempc2, &tempc3); // REG4 - if (retval == -1) - return -1; - m_SFR[0xF4] = tempc3; - - retval = Three_byte_command(0x7A, 0xF5, 0x00, &tempc1, &tempc2, &tempc3); // REG5 - if (retval == -1) - return -1; - m_SFR[0xF5] = tempc3; - - retval = Three_byte_command(0x7A, 0xF6, 0x00, &tempc1, &tempc2, &tempc3); // REG6 - if (retval == -1) - return -1; - m_SFR[0xF6] = tempc3; - - retval = Three_byte_command(0x7A, 0xF7, 0x00, &tempc1, &tempc2, &tempc3); // REG7 - if (retval == -1) - return -1; - m_SFR[0xF7] = tempc3; - - retval = Three_byte_command(0x7A, 0xFC, 0x00, &tempc1, &tempc2, &tempc3); // REG8 - if (retval == -1) - return -1; - m_SFR[0xFC] = tempc3; - - retval = Three_byte_command(0x7A, 0xFD, 0x00, &tempc1, &tempc2, &tempc3); // REG9 - if (retval == -1) - return -1; - m_SFR[0xFD] = tempc3; - OnProgrammingCallback(48, 48, ""); - return 0; -} - void lms7002_pnlMCU_BD_view::OnViewSFRsClick(wxCommandEvent& event) { if (mThreadWorking) @@ -1360,7 +784,7 @@ void lms7002_pnlMCU_BD_view::OnViewSFRsClick(wxCommandEvent& event) mThreadWorking = true; mWorkerThread = std::thread( [](lms7002_pnlMCU_BD_view* pthis) { - int retval = pthis->Read_SFR(); + int retval = pthis->mcu->Read_SFR(); wxThreadEvent* evt = new wxThreadEvent(); evt->SetInt(retval); evt->SetId(ID_PROGRAMING_FINISH_EVENT); @@ -1369,41 +793,6 @@ void lms7002_pnlMCU_BD_view::OnViewSFRsClick(wxCommandEvent& event) this); } -int lms7002_pnlMCU_BD_view::Read_IRAM() -{ - unsigned char tempc1, tempc2, tempc3 = 0x00; - int i = 0; - int retval = 0; - - //default - //IRAM array initialization - for (i = 0; i <= 255; i++) - m_IRAM[i] = 0x00; - - unsigned stepsDone = 0; - OnProgrammingCallback(stepsDone, 256, ""); - for (i = 0; i <= 255; i++) - { - // code 0x78 is for reading the IRAM locations - retval = Three_byte_command(0x78, ((unsigned char)(i)), 0x00, &tempc1, &tempc2, &tempc3); - if (retval == 0) - m_IRAM[i] = tempc3; - else - { - i = 256; // error, stop - } - OnProgrammingCallback(++stepsDone, 256, ""); -#ifndef NDEBUG - //printf("MCU reading IRAM: %2i/256\r", stepsDone.load()); -#endif - Wait_CLK_Cycles(64); - } -#ifndef NDEBUG - printf("\nMCU reading IRAM finished\n"); -#endif - return retval; -} - void lms7002_pnlMCU_BD_view::OnViewIRAMClick(wxCommandEvent& event) { if (mThreadWorking) @@ -1418,7 +807,7 @@ void lms7002_pnlMCU_BD_view::OnViewIRAMClick(wxCommandEvent& event) mThreadWorking = true; mWorkerThread = std::thread( [](lms7002_pnlMCU_BD_view* pthis) { - int retval = pthis->Read_IRAM(); + int retval = pthis->mcu->Read_IRAM(); wxThreadEvent* evt = new wxThreadEvent(); evt->SetInt(retval); evt->SetId(ID_PROGRAMING_FINISH_EVENT); @@ -1427,39 +816,6 @@ void lms7002_pnlMCU_BD_view::OnViewIRAMClick(wxCommandEvent& event) this); } -int lms7002_pnlMCU_BD_view::Erase_IRAM() -{ - unsigned char tempc1, tempc2, tempc3 = 0x00; - int retval = 0; - int i = 0; - - //default ini. - for (i = 0; i <= 255; i++) - m_IRAM[i] = 0x00; - - unsigned stepsDone = 0; - OnProgrammingCallback(stepsDone, 256, ""); - for (i = 0; i <= 255; i++) - { - m_IRAM[i] = 0x00; - // code 0x7C is for writing the IRAM locations - retval = Three_byte_command(0x7C, ((unsigned char)(i)), 0x00, &tempc1, &tempc2, &tempc3); - if (retval == -1) - { - i = 256; - //aborted.store(true); - } - OnProgrammingCallback(++stepsDone, 256, ""); -#ifndef NDEBUG - //printf("MCU erasing IRAM: %2i/256\r", stepsDone.load()); -#endif - } -#ifndef NDEBUG - printf("\nMCU erasing IRAM finished\n"); -#endif - return retval; -} - void lms7002_pnlMCU_BD_view::OnEraseIRAMClick(wxCommandEvent& event) { if (mThreadWorking) @@ -1475,7 +831,7 @@ void lms7002_pnlMCU_BD_view::OnEraseIRAMClick(wxCommandEvent& event) mThreadWorking = true; mWorkerThread = std::thread( [](lms7002_pnlMCU_BD_view* pthis) { - int retval = pthis->Erase_IRAM(); + int retval = pthis->mcu->Erase_IRAM(); wxThreadEvent* evt = new wxThreadEvent(); evt->SetInt(retval); evt->SetId(ID_PROGRAMING_FINISH_EVENT); @@ -1484,24 +840,13 @@ void lms7002_pnlMCU_BD_view::OnEraseIRAMClick(wxCommandEvent& event) this); } -int lms7002_pnlMCU_BD_view::Change_MCUFrequency(unsigned char data) -{ - - unsigned char tempc1, tempc2, tempc3 = 0x00; - int retval = 0; - // code 0x7E is for writing the SFR registers - retval = Three_byte_command(0x7E, 0x8E, data, &tempc1, &tempc2, &tempc3); - // PMSR register, address 0x8E - return retval; -} - void lms7002_pnlMCU_BD_view::OnSelDivSelect(wxCommandEvent& event) { int retval = 0; int tempi = 0; tempi = SelDiv->GetSelection(); - retval = Change_MCUFrequency(tempi); + retval = mcu->Change_MCUFrequency(tempi); if (retval == -1) wxMessageBox(_("Cannot set the MCU's frequency")); else @@ -1571,7 +916,7 @@ void lms7002_pnlMCU_BD_view::OnReadIRAMfinished(wxThreadEvent& event) return; } dlgViewIRAM dlg(this); - dlg.InitGridData(m_IRAM); + dlg.InitGridData(mcu->m_IRAM); dlg.ShowModal(); } diff --git a/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.h b/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.h index af1ae0456..73b3bc296 100644 --- a/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.h +++ b/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.h @@ -6,6 +6,7 @@ #include #include #include "ILMS7002MTab.h" +#include "MCU_BD.h" class lms7002_pnlMCU_BD_view : public ILMS7002MTab { @@ -28,65 +29,40 @@ class lms7002_pnlMCU_BD_view : public ILMS7002MTab void OnRegWriteRead(wxCommandEvent& event); void OnbtnRunProductionTestClicked(wxCommandEvent& event); - public: - lms7002_pnlMCU_BD_view(wxWindow* parent, wxWindowID id); - ~lms7002_pnlMCU_BD_view(); - - protected: - static const size_t max_array_size = 16 * 1024; - int GetProgramCode(const char* inFileName, bool bin); - void RunTest_MCU(int m_iMode1, int m_iMode0, unsigned short test_code, int m_iDebug); - void Wait_CLK_Cycles(int delay); - void DebugModeSet_MCU(int m_iMode1, int m_iMode0); - void DebugModeExit_MCU(int m_iMode1, int m_iMode0); - int ResetPC_MCU(); - int Change_MCUFrequency(unsigned char data); - int One_byte_command(unsigned short data1, unsigned char* rdata1); - int Three_byte_command(unsigned char data1, - unsigned char data2, - unsigned char data3, - unsigned char* rdata1, - unsigned char* rdata2, - unsigned char* rdata3); - static bool OnProgrammingCallback(int bsent, int btotal, const char* progressMsg); void OnProgramingStatusUpdate(wxCommandEvent& event); - int WaitUntilWritten(); - int RunProductionTest_MCU(); - int Read_IRAM(); - int ReadOneByte(unsigned char* data); - int Read_SFR(); - int Erase_IRAM(); - int RunInstr_MCU(unsigned short* pPCVAL); void OnReadIRAMfinished(wxThreadEvent& event); void OnEraseIRAMfinished(wxThreadEvent& event); void OnReadSFRfinished(wxThreadEvent& event); void OnProgrammingfinished(wxThreadEvent& event); + + static bool OnProgrammingCallback(int bsent, int btotal, const char* progressMsg); + + public: + lms7002_pnlMCU_BD_view(wxWindow* parent, wxWindowID id); + ~lms7002_pnlMCU_BD_view(); + void Initialize(ControllerType* pControl) override; + + protected: + lime::MCU_BD* mcu; + bool mThreadWorking; std::thread mWorkerThread; wxTimer* progressPooler; - int m_iTestNo; - unsigned int m_iInstrNo; - int m_iPCvalue; - int m_iDebug; - int m_iMode0; - int m_iMode1; int m_iTestResultFileLine; int TestResultArray_code[256]; int TestResultArray_address[256]; int TestResultArray_value[256]; - unsigned short m_iRegAddress; - int m_iRegRead; - unsigned short m_iRegData; - + int m_iDebug; + int m_iMode0; + int m_iMode1; int m_bLoadedProd; int m_bLoadedDebug; - std::string mLoadedProgramFilename; - unsigned char byte_array[max_array_size]; - int m_iLoopTries; - unsigned char m_IRAM[256]; - unsigned char m_SFR[256]; + int m_iTestNo; + unsigned int m_iInstrNo; + int m_iPCvalue; + static lms7002_pnlMCU_BD_view* obj_ptr; static const long ID_PROGRAMING_STATUS_EVENT; static const long ID_PROGRAMING_FINISH_EVENT; diff --git a/src/lms7002m/MCU_BD.cpp b/src/lms7002m/MCU_BD.cpp index e30c83e3a..5a9a1a8d2 100644 --- a/src/lms7002m/MCU_BD.cpp +++ b/src/lms7002m/MCU_BD.cpp @@ -29,7 +29,7 @@ MCU_BD::MCU_BD() stepsTotal = 0; stepsDone = 0; aborted = false; - callback = nullptr; + m_callback = nullptr; //ctor int i = 0; m_serPort = NULL; @@ -61,6 +61,33 @@ void MCU_BD::Initialize(std::shared_ptr pSerPort, unsigned size) byte_array_size = size; } +void MCU_BD::SetCallback(ProgrammingCallback callback) +{ + m_callback = callback; +} + +void MCU_BD::IncrementStepsDone(unsigned short amount, const char* message) +{ + assert(message); + stepsDone += amount; + + if (m_callback != nullptr) + { + m_callback(stepsDone.load(), stepsTotal.load(), message); + } +} + +void MCU_BD::SetStepsDone(unsigned short amount, const char* message) +{ + assert(message); + stepsDone.store(amount); + + if (m_callback != nullptr) + { + m_callback(stepsDone.load(), stepsTotal.load(), message); + } +} + /** @brief Read program code from file into memory @param inFileName source file path @param bin binary or hex file @@ -277,7 +304,7 @@ int MCU_BD::Read_IRAM() m_IRAM[i] = 0x00; stepsTotal.store(256); - stepsDone.store(0); + SetStepsDone(0); aborted.store(false); for (i = 0; i <= 255; i++) { @@ -290,7 +317,7 @@ int MCU_BD::Read_IRAM() i = 256; // error, stop aborted.store(true); } - ++stepsDone; + IncrementStepsDone(0); #ifndef NDEBUG printf("MCU reading IRAM: %2i/256\r", stepsDone.load()); #endif @@ -313,7 +340,7 @@ int MCU_BD::Erase_IRAM() m_IRAM[i] = 0x00; stepsTotal.store(256); - stepsDone.store(0); + SetStepsDone(0); aborted.store(false); for (i = 0; i <= 255; i++) { @@ -325,7 +352,7 @@ int MCU_BD::Erase_IRAM() i = 256; aborted.store(true); } - ++stepsDone; + IncrementStepsDone(); #ifndef NDEBUG printf("MCU erasing IRAM: %2i/256\r", stepsDone.load()); #endif @@ -342,270 +369,88 @@ int MCU_BD::Read_SFR() unsigned char tempc1, tempc2, tempc3 = 0x00; int retval = 0; - stepsTotal.store(48); + const std::vector addressesToRead{ + 0x80, // P0 + 0x81, // SP + 0x82, // DPL0 + 0x83, // DPH0 + 0x84, // DPL1 + 0x85, // DPH1 + 0x86, // DPS + 0x87, // PCON + 0x88, // TCON + 0x89, // TMOD + 0x8A, // TL0 + 0x8B, // TL1 + 0x8C, // TH0 + 0x8D, // TH1 + 0x8E, // PMSR + 0x90, // P1 + 0x91, // DIR1 + 0x98, // SCON + 0x99, // SBUF + 0xA0, // P2 + 0xA1, // DIR2 + 0xA2, // DIR0 + 0xA8, // IEN0 + 0xA9, // IEN1 + 0xB0, // EECTRL + 0xB1, // EEDATA + 0xB8, // IP0 + 0xB9, // IP1 + 0xBF, // USR2 + 0xC0, // IRCON + 0xC8, // T2CON + 0xCA, // RCAP2L + 0xCB, // RCAP2H + 0xCC, // TL2 + 0xCD, // TH2 + 0xD0, // PSW + 0xE0, // ACC + 0xF0, // B + 0xEC, // REG0 + 0xED, // REG1 + 0xEE, // REG2 + 0xEF, // REG3 + 0xF4, // REG4 + 0xF5, // REG5 + 0xF6, // REG6 + 0xF7, // REG7 + 0xFC, // REG8 + 0xFD, // REG9 + }; + + stepsTotal.store(addressesToRead.size()); stepsDone.store(0); aborted.store(false); //default m_SFR array initialization for (i = 0; i <= 255; i++) + { m_SFR[i] = 0x00; + } - // code 0x7A is for reading the SFR registers - retval = Three_byte_command(0x7A, 0x80, 0x00, &tempc1, &tempc2, &tempc3); // P0 - if (retval == -1) - return -1; - m_SFR[0x80] = tempc3; - - retval = Three_byte_command(0x7A, 0x81, 0x00, &tempc1, &tempc2, &tempc3); // SP - if (retval == -1) - return -1; - m_SFR[0x81] = tempc3; - - retval = Three_byte_command(0x7A, 0x82, 0x00, &tempc1, &tempc2, &tempc3); // DPL0 - if (retval == -1) - return -1; - m_SFR[0x82] = tempc3; - - retval = Three_byte_command(0x7A, 0x83, 0x00, &tempc1, &tempc2, &tempc3); // DPH0 - if (retval == -1) - return -1; - m_SFR[0x83] = tempc3; - - retval = Three_byte_command(0x7A, 0x84, 0x00, &tempc1, &tempc2, &tempc3); // DPL1 - if (retval == -1) - return -1; - m_SFR[0x84] = tempc3; - - retval = Three_byte_command(0x7A, 0x85, 0x00, &tempc1, &tempc2, &tempc3); // DPH1 - if (retval == -1) - return -1; - m_SFR[0x85] = tempc3; - - stepsDone.store(6); - - retval = Three_byte_command(0x7A, 0x86, 0x00, &tempc1, &tempc2, &tempc3); // DPS - if (retval == -1) - return -1; - m_SFR[0x86] = tempc3; - - retval = Three_byte_command(0x7A, 0x87, 0x00, &tempc1, &tempc2, &tempc3); // PCON - if (retval == -1) - return -1; - m_SFR[0x87] = tempc3; - - retval = Three_byte_command(0x7A, 0x88, 0x00, &tempc1, &tempc2, &tempc3); // TCON - if (retval == -1) - return -1; - m_SFR[0x88] = tempc3; - - retval = Three_byte_command(0x7A, 0x89, 0x00, &tempc1, &tempc2, &tempc3); // TMOD - if (retval == -1) - return -1; - m_SFR[0x89] = tempc3; - - retval = Three_byte_command(0x7A, 0x8A, 0x00, &tempc1, &tempc2, &tempc3); // TL0 - if (retval == -1) - return -1; - m_SFR[0x8A] = tempc3; - - retval = Three_byte_command(0x7A, 0x8B, 0x00, &tempc1, &tempc2, &tempc3); // TL1 - if (retval == -1) - return -1; - m_SFR[0x8B] = tempc3; - - stepsDone.store(12); - - retval = Three_byte_command(0x7A, 0x8C, 0x00, &tempc1, &tempc2, &tempc3); // TH0 - if (retval == -1) - return -1; - m_SFR[0x8C] = tempc3; - - retval = Three_byte_command(0x7A, 0x8D, 0x00, &tempc1, &tempc2, &tempc3); // TH1 - if (retval == -1) - return -1; - m_SFR[0x8D] = tempc3; - - retval = Three_byte_command(0x7A, 0x8E, 0x00, &tempc1, &tempc2, &tempc3); // PMSR - if (retval == -1) - return -1; - m_SFR[0x8E] = tempc3; - - retval = Three_byte_command(0x7A, 0x90, 0x00, &tempc1, &tempc2, &tempc3); // P1 - if (retval == -1) - return -1; - m_SFR[0x90] = tempc3; - - retval = Three_byte_command(0x7A, 0x91, 0x00, &tempc1, &tempc2, &tempc3); // DIR1 - if (retval == -1) - return -1; - m_SFR[0x91] = tempc3; - - retval = Three_byte_command(0x7A, 0x98, 0x00, &tempc1, &tempc2, &tempc3); // SCON - if (retval == -1) - return -1; - m_SFR[0x98] = tempc3; - - stepsDone.store(18); - - retval = Three_byte_command(0x7A, 0x99, 0x00, &tempc1, &tempc2, &tempc3); // SBUF - if (retval == -1) - return -1; - m_SFR[0x99] = tempc3; - - retval = Three_byte_command(0x7A, 0xA0, 0x00, &tempc1, &tempc2, &tempc3); // P2 - if (retval == -1) - return -1; - m_SFR[0xA0] = tempc3; - - retval = Three_byte_command(0x7A, 0xA1, 0x00, &tempc1, &tempc2, &tempc3); // DIR2 - if (retval == -1) - return -1; - m_SFR[0xA1] = tempc3; - - retval = Three_byte_command(0x7A, 0xA2, 0x00, &tempc1, &tempc2, &tempc3); // DIR0 - if (retval == -1) - return -1; - m_SFR[0xA2] = tempc3; - - retval = Three_byte_command(0x7A, 0xA8, 0x00, &tempc1, &tempc2, &tempc3); // IEN0 - if (retval == -1) - return -1; - m_SFR[0xA8] = tempc3; - - stepsDone.store(24); - - retval = Three_byte_command(0x7A, 0xA9, 0x00, &tempc1, &tempc2, &tempc3); // IEN1 - if (retval == -1) - return -1; - m_SFR[0xA9] = tempc3; - - retval = Three_byte_command(0x7A, 0xB0, 0x00, &tempc1, &tempc2, &tempc3); // EECTRL - if (retval == -1) - return -1; - m_SFR[0xB0] = tempc3; - - retval = Three_byte_command(0x7A, 0xB1, 0x00, &tempc1, &tempc2, &tempc3); // EEDATA - if (retval == -1) - return -1; - m_SFR[0xB1] = tempc3; - - retval = Three_byte_command(0x7A, 0xB8, 0x00, &tempc1, &tempc2, &tempc3); // IP0 - if (retval == -1) - return -1; - m_SFR[0xB8] = tempc3; - - retval = Three_byte_command(0x7A, 0xB9, 0x00, &tempc1, &tempc2, &tempc3); // IP1 - if (retval == -1) - return -1; - m_SFR[0xB9] = tempc3; - - retval = Three_byte_command(0x7A, 0xBF, 0x00, &tempc1, &tempc2, &tempc3); // USR2 - if (retval == -1) - return -1; - m_SFR[0xBF] = tempc3; - - stepsDone.store(30); - - retval = Three_byte_command(0x7A, 0xC0, 0x00, &tempc1, &tempc2, &tempc3); // IRCON - if (retval == -1) - return -1; - m_SFR[0xC0] = tempc3; - - retval = Three_byte_command(0x7A, 0xC8, 0x00, &tempc1, &tempc2, &tempc3); // T2CON - if (retval == -1) - return -1; - m_SFR[0xC8] = tempc3; - - retval = Three_byte_command(0x7A, 0xCA, 0x00, &tempc1, &tempc2, &tempc3); // RCAP2L - if (retval == -1) - return -1; - m_SFR[0xCA] = tempc3; - - retval = Three_byte_command(0x7A, 0xCB, 0x00, &tempc1, &tempc2, &tempc3); // RCAP2H - if (retval == -1) - return -1; - m_SFR[0xCB] = tempc3; - - retval = Three_byte_command(0x7A, 0xCC, 0x00, &tempc1, &tempc2, &tempc3); // TL2 - if (retval == -1) - return -1; - m_SFR[0xCC] = tempc3; - - retval = Three_byte_command(0x7A, 0xCD, 0x00, &tempc1, &tempc2, &tempc3); // TH2 - if (retval == -1) - return -1; - m_SFR[0xCD] = tempc3; - - stepsDone.store(36); - - retval = Three_byte_command(0x7A, 0xD0, 0x00, &tempc1, &tempc2, &tempc3); // PSW - if (retval == -1) - return -1; - m_SFR[0xD0] = tempc3; - - retval = Three_byte_command(0x7A, 0xE0, 0x00, &tempc1, &tempc2, &tempc3); // ACC - if (retval == -1) - return -1; - m_SFR[0xE0] = tempc3; - - retval = Three_byte_command(0x7A, 0xF0, 0x00, &tempc1, &tempc2, &tempc3); // B - if (retval == -1) - return -1; - m_SFR[0xF0] = tempc3; - - retval = Three_byte_command(0x7A, 0xEC, 0x00, &tempc1, &tempc2, &tempc3); // REG0 - if (retval == -1) - return -1; - m_SFR[0xEC] = tempc3; - - retval = Three_byte_command(0x7A, 0xED, 0x00, &tempc1, &tempc2, &tempc3); // REG1 - if (retval == -1) - return -1; - m_SFR[0xED] = tempc3; - - retval = Three_byte_command(0x7A, 0xEE, 0x00, &tempc1, &tempc2, &tempc3); // REG2 - if (retval == -1) - return -1; - m_SFR[0xEE] = tempc3; - - stepsDone.store(42); - - retval = Three_byte_command(0x7A, 0xEF, 0x00, &tempc1, &tempc2, &tempc3); // REG3 - if (retval == -1) - return -1; - m_SFR[0xEF] = tempc3; - - retval = Three_byte_command(0x7A, 0xF4, 0x00, &tempc1, &tempc2, &tempc3); // REG4 - if (retval == -1) - return -1; - m_SFR[0xF4] = tempc3; - - retval = Three_byte_command(0x7A, 0xF5, 0x00, &tempc1, &tempc2, &tempc3); // REG5 - if (retval == -1) - return -1; - m_SFR[0xF5] = tempc3; - - retval = Three_byte_command(0x7A, 0xF6, 0x00, &tempc1, &tempc2, &tempc3); // REG6 - if (retval == -1) - return -1; - m_SFR[0xF6] = tempc3; - - retval = Three_byte_command(0x7A, 0xF7, 0x00, &tempc1, &tempc2, &tempc3); // REG7 - if (retval == -1) - return -1; - m_SFR[0xF7] = tempc3; + int counter = 0; + const int callCallbackEveryNCalls = 6; + for (const auto& address : addressesToRead) + { + // code 0x7A is for reading the SFR registers + retval = Three_byte_command(0x7A, address, 0x00, &tempc1, &tempc2, &tempc3); + if (retval == -1) + { + return -1; + } - retval = Three_byte_command(0x7A, 0xFC, 0x00, &tempc1, &tempc2, &tempc3); // REG8 - if (retval == -1) - return -1; - m_SFR[0xFC] = tempc3; + m_SFR[address] = tempc3; - retval = Three_byte_command(0x7A, 0xFD, 0x00, &tempc1, &tempc2, &tempc3); // REG9 - if (retval == -1) - return -1; - m_SFR[0xFD] = tempc3; + counter++; - stepsDone.store(48); + if (counter == callCallbackEveryNCalls) + { + counter = 0; + IncrementStepsDone(callCallbackEveryNCalls); + } + } return 0; } @@ -671,8 +516,8 @@ int MCU_BD::Program_MCU(const uint8_t* buffer, const MCU_BD::MCU_PROG_MODE mode) m_serPort->SPI(wrdata, nullptr, 2); - if (callback) - abort = callback(0, byte_array_size, ""); + if (m_callback) + abort = m_callback(0, byte_array_size, ""); for (uint16_t i = 0; i < byte_array_size && !abort; i += fifoLen) { @@ -696,8 +541,8 @@ int MCU_BD::Program_MCU(const uint8_t* buffer, const MCU_BD::MCU_PROG_MODE mode) wrdata[j] = (1 << 31) | addrDTM << 16 | buffer[i + j]; m_serPort->SPI(wrdata, nullptr, fifoLen); - if (callback) - abort = callback(i + fifoLen, byte_array_size, ""); + if (m_callback) + abort = m_callback(i + fifoLen, byte_array_size, ""); #ifndef NDEBUG printf("MCU programming : %4i/%4li\r", i + fifoLen, long(byte_array_size)); #endif diff --git a/src/lms7002m/MCU_BD.h b/src/lms7002m/MCU_BD.h index 768c1e26e..1db4e74c8 100644 --- a/src/lms7002m/MCU_BD.h +++ b/src/lms7002m/MCU_BD.h @@ -12,11 +12,13 @@ #include #include +#include "limesuite/config.h" + namespace lime { class ISPI; -class MCU_BD +class LIME_API MCU_BD { public: enum class MCU_PROG_MODE : uint8_t { RESET, EEPROM_AND_SRAM, SRAM, BOOT_SRAM_FROM_EEPROM }; @@ -79,6 +81,7 @@ class MCU_BD * @return 0-continue programming, 1-abort operation */ typedef std::function ProgrammingCallback; + void SetCallback(ProgrammingCallback callback); protected: std::string mLoadedProgramFilename; @@ -94,6 +97,9 @@ class MCU_BD int m_bLoadedProd; int byte_array_size; + void IncrementStepsDone(unsigned short amount = 1, const char* message = ""); + void SetStepsDone(unsigned short amount, const char* message = ""); + public: uint8_t ReadMCUProgramID(); OperationStatus SetDebugMode(bool enabled, MCU_PROG_MODE mode); @@ -133,7 +139,7 @@ class MCU_BD int ResetPC_MCU(); int RunInstr_MCU(unsigned short* pPCVAL); void Initialize(std::shared_ptr pSerPort, unsigned rom_size = 0); - ProgrammingCallback callback; + ProgrammingCallback m_callback; }; } // namespace lime #endif // MCU_BD_H From efe27cf55029a07d420389b3f2dd871ebf0eae36 Mon Sep 17 00:00:00 2001 From: Dominykas Date: Wed, 7 Feb 2024 13:23:03 +0200 Subject: [PATCH 2/4] Deduplicate a constant and make sure size doesn't exceed max size in initialization. --- src/lms7002m/MCU_BD.cpp | 10 ++++++++-- src/lms7002m/MCU_BD.h | 5 ++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lms7002m/MCU_BD.cpp b/src/lms7002m/MCU_BD.cpp index 5a9a1a8d2..3685c23e2 100644 --- a/src/lms7002m/MCU_BD.cpp +++ b/src/lms7002m/MCU_BD.cpp @@ -36,7 +36,7 @@ MCU_BD::MCU_BD() //default value, //must be verified during program exploatation m_iLoopTries = 20; - byte_array_size = cMaxFWSize; + byte_array_size = MCU_PROGRAM_SIZE; // array initiallization for (i = 0; i <= 255; i++) { @@ -57,7 +57,13 @@ MCU_BD::~MCU_BD() void MCU_BD::Initialize(std::shared_ptr pSerPort, unsigned size) { m_serPort = pSerPort; - if (size > 0) + + if (size > MCU_PROGRAM_SIZE) + { + byte_array_size = MCU_PROGRAM_SIZE; + lime::warning("%s %i", "MCU_BD initialize size exceeds maximum size; clamping to max of", MCU_PROGRAM_SIZE); + } + else if (size > 0) byte_array_size = size; } diff --git a/src/lms7002m/MCU_BD.h b/src/lms7002m/MCU_BD.h index 1db4e74c8..43249d264 100644 --- a/src/lms7002m/MCU_BD.h +++ b/src/lms7002m/MCU_BD.h @@ -12,6 +12,7 @@ #include #include +#include "mcu_programs.h" #include "limesuite/config.h" namespace lime { @@ -71,8 +72,6 @@ class LIME_API MCU_BD int WaitForMCU(uint32_t timeout_ms); static const char* MCUStatusMessage(const uint8_t code); - static const int cMaxFWSize = 1024 * 16; - /*! * Callback from programming processes * @param bsent number of bytes transferred @@ -112,7 +111,7 @@ class LIME_API MCU_BD // The SFR content unsigned char m_SFR[256]; // The program memory code - unsigned char byte_array[cMaxFWSize]; + unsigned char byte_array[MCU_PROGRAM_SIZE]; void mSPI_write(unsigned short addr_reg, unsigned short data_reg); unsigned short mSPI_read(unsigned short addr_reg); From 36c02c767d0ad634ee714d84178cae81f6687f66 Mon Sep 17 00:00:00 2001 From: Dominykas Date: Wed, 21 Feb 2024 12:51:26 +0200 Subject: [PATCH 3/4] Fix incrementing steps done by 0 --- src/lms7002m/MCU_BD.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lms7002m/MCU_BD.cpp b/src/lms7002m/MCU_BD.cpp index 02d21e40b..83f11a86d 100644 --- a/src/lms7002m/MCU_BD.cpp +++ b/src/lms7002m/MCU_BD.cpp @@ -324,7 +324,7 @@ int MCU_BD::Read_IRAM() i = 256; // error, stop aborted.store(true); } - IncrementStepsDone(0); + IncrementStepsDone(); #ifndef NDEBUG lime::debug("MCU reading IRAM: %2i/256\r", stepsDone.load()); #endif From 3c83651fa5a034ed1b234a334825215e64d0e57e Mon Sep 17 00:00:00 2001 From: Dominykas Date: Wed, 21 Feb 2024 14:22:16 +0200 Subject: [PATCH 4/4] Add forgotten file close --- src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp b/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp index 64922c5c3..6af05b8ba 100644 --- a/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp +++ b/src/lms7002_wxgui/lms7002_pnlMCU_BD_view.cpp @@ -584,15 +584,7 @@ void lms7002_pnlMCU_BD_view::OnbtnLoadTestFileClick(wxCommandEvent& event) } } - // debugging - //for (i=0; i