Skip to content

Commit

Permalink
Xflash load from hex (#294)
Browse files Browse the repository at this point in the history
* Support loading languages from hex file to xflash
  • Loading branch information
vintagepc authored Mar 7, 2021
1 parent dc597a0 commit 4a1c5aa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
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: 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

0 comments on commit 4a1c5aa

Please sign in to comment.