Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xflash load from hex #294

Merged
merged 4 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions parts/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,30 @@ namespace Boards {

avr_flashaddr_t Board::LoadFirmware(const string &strFW)
{
uint32_t uiFWSize = 0, uiFWStart = 0;
if (strFW.size()>4)
{
if (0==strFW.compare(strFW.size()-4, 4, ".hex"))
{
gsl::unique_ptr<uint8_t> puiBytes {read_ihex_file(strFW.c_str(),&uiFWSize, &uiFWStart) };
if (!puiBytes)
ihex_chunk_p pChunks = nullptr;
int iCount = read_ihex_chunks(strFW.c_str(), &pChunks);
if (iCount==0)
{
std::cerr << "No chunks found in .hex file. Firmware NOT loaded!\n";
return 0;
} else if (pChunks[0].data == nullptr)
{
std::cout << "WARN: Could not load " << strFW << ". MCU will execute existing flash." << '\n';
return 0;
}
else
{
std::cout << "Loaded " << uiFWSize << " bytes from HEX file: " << strFW << '\n';
gsl::span<uint8_t> flash {m_pAVR->flash, m_pAVR->flashend};
memcpy(flash.begin() + uiFWStart, puiBytes.get(), uiFWSize);
gsl::span<uint8_t> chunk0 {pChunks[0].data, pChunks[0].size};
uint32_t uiFWStart = pChunks[0].baseaddr;
if (iCount > 1) {
OnExtraHexChunk({pChunks[1].data,pChunks[1].size});
}
std::cout << "Loaded " << chunk0.size_bytes() << " bytes from HEX file: " << strFW << '\n';
gsl::span<uint8_t> flash {m_pAVR->flash, m_pAVR->flashend};
memcpy(flash.data() + uiFWStart, chunk0.begin(), chunk0.size_bytes());
free_ihex_chunks(pChunks);
m_pAVR->codeend = m_pAVR->flashend;
return uiFWStart;
}
Expand Down
3 changes: 3 additions & 0 deletions parts/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ namespace Boards
// within the context of the AVR run thread.
virtual void OnAVRCycle(){};

// For boards that can handle extra chunks in the hex (language)
virtual void OnExtraHexChunk(gsl::span<uint8_t> /*chunk*/){};

void OnKeyPress(const Key& key) override;

LineStatus ProcessAction(unsigned int ID, const std::vector<std::string> &vArgs) override;
Expand Down
5 changes: 5 additions & 0 deletions parts/boards/EinsyRambo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ namespace Boards
sd_card.Unmount();
}

void EinsyRambo::OnExtraHexChunk(gsl::span<uint8_t> chunk)
{
spiFlash.Load(chunk);
}

void EinsyRambo::OnAVRReset()
{
std::cout << "RESET\n";
Expand Down
2 changes: 2 additions & 0 deletions parts/boards/EinsyRambo.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ namespace Boards

void OnKeyPress(const Key& key) override;

void OnExtraHexChunk(gsl::span<uint8_t> chunk) override;

static constexpr float fScale24v = 1.0f/26.097f; // Based on rSense voltage divider outputting 5v

bool m_bFactoryReset = false;
Expand Down
7 changes: 6 additions & 1 deletion parts/components/TMC2130.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ void TMC2130::_Draw(bool bIsSimple)
}
// Copy atomic to local
float fPos = m_fCurPos;
glColor3f(0,0,0);
if (m_bDrawStall) {
glColor3f(0.7,0,0);
} else {
glColor3f(0,0,0);
}
glBegin(GL_QUADS);
glVertex3f(0,0,0);
glVertex3f(350,0,0);
Expand Down Expand Up @@ -191,6 +195,7 @@ void TMC2130::RaiseDiag(uint8_t value)
if (bDiag)
{
//printf("Raised: %d\n", value == m_regs.defs.GCONF.diag0_int_pushpull);
m_bDrawStall = value>0;
RaiseIRQ(DIAG_OUT, value == m_regs.defs.GCONF.diag0_int_pushpull);
}
}
Expand Down
2 changes: 2 additions & 0 deletions parts/components/TMC2130.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ class TMC2130: public SPIPeripheral, public Scriptable
bool m_bStall = false;
uint32_t m_uiStepIncrement = 1;

std::atomic_bool m_bDrawStall {false};

// Position helpers
float StepToPos(int32_t step);
int32_t PosToStep(float step);
Expand Down
7 changes: 7 additions & 0 deletions parts/components/w25x20cl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ void w25x20cl::Load(const std::string &path)
Load();
}

void w25x20cl::Load(const gsl::span<uint8_t> data)
{
size_t bytes = std::min(m_flash.size_bytes(), data.size_bytes());
memcpy(m_flash.begin(), data.begin(), bytes);
std::cout << "Loaded " << std::to_string(bytes) << " from hex file to xflash\n";
}

void w25x20cl::Load()
{
auto *path = m_filepath.c_str();
Expand Down
3 changes: 3 additions & 0 deletions parts/components/w25x20cl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class w25x20cl:public SPIPeripheral, public Scriptable
// Loads the flash contents from file. (creates "path" if it does not exit)
void Load(const std::string &path);

// Loads from an array
void Load(const gsl::span<uint8_t> data);

// Reloads the current file.
void Load();

Expand Down
Binary file modified scripts/tests/snaps/parts/TMC02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified scripts/tests/snaps/parts/TMC03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.