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

Generic refactoring of the code #370

Merged
merged 3 commits into from
Aug 31, 2023
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
14 changes: 7 additions & 7 deletions src/altera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void Altera::reset()
void Altera::programMem(RawParser &_bit)
{
int byte_length = _bit.getLength()/8;
uint8_t *data = _bit.getData();
const uint8_t *data = _bit.getData();

uint32_t clk_period = 1e9/static_cast<float>(_jtag->getClkFreq());

Expand All @@ -101,7 +101,7 @@ void Altera::programMem(RawParser &_bit)

int xfer_len = 512;
int tx_len;
int tx_end;
Jtag::tapState_t tx_end;

for (int i=0; i < byte_length; i+=xfer_len) {
if (i + xfer_len > byte_length) { // last packet with some size
Expand Down Expand Up @@ -232,7 +232,7 @@ void Altera::program(unsigned int offset, bool unprotect_flash)
reverseOrder = true;

/* prepare data to write */
uint8_t *data = NULL;
const uint8_t *data = NULL;
int length = 0;

RawParser bit(_filename, reverseOrder);
Expand All @@ -250,7 +250,7 @@ void Altera::program(unsigned int offset, bool unprotect_flash)
}
}

int Altera::idCode()
uint32_t Altera::idCode()
{
unsigned char tx_data[4] = {IDCODE};
unsigned char rx_data[4];
Expand All @@ -266,7 +266,7 @@ int Altera::idCode()

/* SPI interface */

int Altera::spi_put(uint8_t cmd, uint8_t *tx, uint8_t *rx, uint32_t len)
int Altera::spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx, uint32_t len)
{
/* +1 because send first cmd + len byte + 1 for rx due to a delay of
* one bit
Expand All @@ -291,7 +291,7 @@ int Altera::spi_put(uint8_t cmd, uint8_t *tx, uint8_t *rx, uint32_t len)

return 0;
}
int Altera::spi_put(uint8_t *tx, uint8_t *rx, uint32_t len)
int Altera::spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len)
{
return spi_put(tx[0], &tx[1], rx, len-1);
}
Expand Down Expand Up @@ -352,7 +352,7 @@ void Altera::shiftVIR(uint32_t reg)
}

void Altera::shiftVDR(uint8_t * tx, uint8_t * rx, uint32_t len,
int end_state, bool debug)
Jtag::tapState_t end_state, bool debug)
{
(void) debug;
uint8_t tx_ir[2] = {USER0, 0};
Expand Down
8 changes: 4 additions & 4 deletions src/altera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Altera: public Device, SPIInterface {
return SPIInterface::dump(base_addr, len);
}

int idCode() override;
uint32_t idCode() override;
void reset() override;

/*************************/
Expand All @@ -65,9 +65,9 @@ class Altera: public Device, SPIInterface {
return SPIInterface::bulk_erase_flash();
}

int spi_put(uint8_t cmd, uint8_t *tx, uint8_t *rx,
int spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx,
uint32_t len) override;
int spi_put(uint8_t *tx, uint8_t *rx, uint32_t len) override;
int spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len) override;
int spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond,
uint32_t timeout, bool verbose = false) override;

Expand Down Expand Up @@ -98,7 +98,7 @@ class Altera: public Device, SPIInterface {
* \param[in] end_state: next state at the end of xfer
*/
void shiftVDR(uint8_t * tx, uint8_t * rx, uint32_t len,
int end_state = Jtag::UPDATE_DR, bool debug = false);
Jtag::tapState_t end_state = Jtag::UPDATE_DR, bool debug = false);

std::string _device_package;
std::string _spiOverJtagPath; /**< spiOverJtag explicit path */
Expand Down
16 changes: 6 additions & 10 deletions src/anlogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Anlogic::program(unsigned int offset, bool unprotect_flash)
if (_verbose)
bit.displayHeader();

uint8_t *data = bit.getData();
const uint8_t *data = bit.getData();
int len = bit.getLength() / 8;

if (_mode == Device::SPI_MODE) {
Expand Down Expand Up @@ -110,14 +110,10 @@ void Anlogic::program(unsigned int offset, bool unprotect_flash)

ProgressBar progress("Loading", len, 50, _quiet);
int pos = 0;
uint8_t *ptr = data;
const uint8_t *ptr = data;
while (len > 0) {
int xfer_len = (len > 512)?512:len;
int tx_end;
if (len - xfer_len == 0)
tx_end = Jtag::RUN_TEST_IDLE;
else
tx_end = Jtag::SHIFT_DR;
Jtag::tapState_t tx_end = (len - xfer_len) ? Jtag::SHIFT_DR : Jtag::RUN_TEST_IDLE;
_jtag->shiftDR(ptr, NULL, xfer_len * 8, tx_end);
len -= xfer_len;
progress.display(pos);
Expand All @@ -143,7 +139,7 @@ void Anlogic::program(unsigned int offset, bool unprotect_flash)
}
}

int Anlogic::idCode()
uint32_t Anlogic::idCode()
{
unsigned char tx_data[4];
unsigned char rx_data[4];
Expand Down Expand Up @@ -184,7 +180,7 @@ bool Anlogic::prepare_flash_access()
* In write only operations to care about this delay
*/

int Anlogic::spi_put(uint8_t cmd, uint8_t *tx, uint8_t *rx, uint32_t len)
int Anlogic::spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx, uint32_t len)
{
int xfer_len = len + 1;
if (rx)
Expand All @@ -210,7 +206,7 @@ int Anlogic::spi_put(uint8_t cmd, uint8_t *tx, uint8_t *rx, uint32_t len)
}
return 0;
}
int Anlogic::spi_put(uint8_t *tx, uint8_t *rx, uint32_t len)
int Anlogic::spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len)
{
int xfer_len = len;
if (rx)
Expand Down
6 changes: 3 additions & 3 deletions src/anlogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Anlogic: public Device, SPIInterface {
~Anlogic();

void program(unsigned int offset, bool unprotect_flash) override;
int idCode() override;
uint32_t idCode() override;
void reset() override;

/* spi interface */
Expand Down Expand Up @@ -57,9 +57,9 @@ class Anlogic: public Device, SPIInterface {
return SPIInterface::dump(base_addr, len);
}

int spi_put(uint8_t cmd, uint8_t *tx, uint8_t *rx,
int spi_put(uint8_t cmd, const uint8_t *tx, uint8_t *rx,
uint32_t len) override;
int spi_put(uint8_t *tx, uint8_t *rx, uint32_t len) override;
int spi_put(const uint8_t *tx, uint8_t *rx, uint32_t len) override;
int spi_wait(uint8_t cmd, uint8_t mask, uint8_t cond,
uint32_t timeout, bool verbose=false) override;

Expand Down
4 changes: 2 additions & 2 deletions src/anlogicBitParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ int AnlogicBitParser::parse()
for (auto it = blocks.begin(); it != blocks.end(); it++) {
for (size_t xpos = 0; xpos < it->size(); xpos++) {
if (_reverseOrder == true)
_bit_data += reverseByte(((*it)[xpos]));
_bit_data.push_back(reverseByte(((*it)[xpos])));
else
_bit_data += ((*it)[xpos]);
_bit_data.push_back((*it)[xpos]);
}
}
_bit_length = _bit_data.size() * 8;
Expand Down
10 changes: 5 additions & 5 deletions src/anlogicCable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int AnlogicCable::setClkFreq(uint32_t clkHZ)
}

ret = libusb_bulk_transfer(dev_handle, ANLOGICCABLE_CONF_EP,
buf, 2, &actual_length, 1000);
buf, 2, &actual_length, 1000);
if (ret < 0) {
cerr << "setClkFreq: usb bulk write failed " << ret << endl;
return -EXIT_FAILURE;
Expand All @@ -137,7 +137,7 @@ int AnlogicCable::setClkFreq(uint32_t clkHZ)
return clkHZ;
}

int AnlogicCable::writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer)
int AnlogicCable::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer)
{
(void) flush_buffer;

Expand All @@ -148,7 +148,7 @@ int AnlogicCable::writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer)
uint8_t mask = (ANLOGICCABLE_TCK_PIN << 4);

int full_len = len;
uint8_t *tx_ptr = tms;
const uint8_t *tx_ptr = tms;

while (full_len > 0) {
/* when len > buffer capacity -> limit to capacity
Expand Down Expand Up @@ -219,13 +219,13 @@ int AnlogicCable::flush()
return 0;
}

int AnlogicCable::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
int AnlogicCable::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
{
uint8_t buf[512];
uint8_t mask = (ANLOGICCABLE_TCK_PIN << 4);

int full_len = len;
uint8_t *tx_ptr = tx;
const uint8_t *tx_ptr = tx;
uint8_t *rx_ptr = rx;

while (full_len > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/anlogicCable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class AnlogicCable : public JtagInterface {
int setClkFreq(uint32_t clkHZ) override;

/* TMS */
int writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer) override;
int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override;
/* TDI */
int writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;
int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;
/* clk */
int toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) override;

Expand All @@ -45,7 +45,7 @@ class AnlogicCable : public JtagInterface {
private:
int write(uint8_t *in_buf, uint8_t *out_buf, int len, int rd_len);

libusb_device_handle *dev_handle;
libusb_device_handle *dev_handle;
libusb_context *usb_ctx;
};
#endif // SRC_ANLOGICCABLE_HPP_
14 changes: 7 additions & 7 deletions src/ch347jtag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ CH347Jtag::CH347Jtag(uint32_t clkHZ, int8_t verbose):
}
libusb_bulk_transfer(dev_handle, CH347JTAG_READ_EP, ibuf, 512,
&actual_length, CH347JTAG_TIMEOUT);
setClkFreq(clkHZ);
_setClkFreq(clkHZ);
return;
usb_release:
libusb_release_interface(dev_handle, CH347JTAG_INTF);
Expand Down Expand Up @@ -221,7 +221,7 @@ CH347Jtag::~CH347Jtag()
}
}

int CH347Jtag::setClkFreq(uint32_t clkHZ)
int CH347Jtag::_setClkFreq(uint32_t clkHZ)
{
unsigned i = 0, sl = 2000000;
if (clkHZ <= 2000000) {
Expand All @@ -244,7 +244,7 @@ int CH347Jtag::setClkFreq(uint32_t clkHZ)
return _clkHZ;
}

int CH347Jtag::writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer)
int CH347Jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer)
{
(void) flush_buffer;

Expand Down Expand Up @@ -307,15 +307,15 @@ int CH347Jtag::toggleClk(uint8_t tms, uint8_t tdi, uint32_t len)
return EXIT_SUCCESS;
}

int CH347Jtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
int CH347Jtag::writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
{
if (!tx || !len)
return 0;
unsigned bytes = (len - ((end)?1:0)) / 8;
unsigned bits = len - bytes * 8;
uint8_t *rptr = rx;
uint8_t *tptr = tx;
uint8_t *txend = &tx[bytes];
const uint8_t *tptr = tx;
const uint8_t *txend = tx + bytes;
uint8_t cmd = (rx) ? CMD_BYTES_WR : CMD_BYTES_WO;
while (tptr < txend) {
unsigned avail = sizeof(obuf) - 3;
Expand Down Expand Up @@ -349,7 +349,7 @@ int CH347Jtag::writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end)
cmd = (rx) ? CMD_BITS_WR : CMD_BITS_WO;
uint8_t *ptr = &obuf[3];
uint8_t x = 0;
uint8_t *bptr = &tx[bytes];
const uint8_t *bptr = &tx[bytes];
for (unsigned i = 0; i < bits; ++i) {
uint8_t txb = bptr[i >> 3];
uint8_t _tdi = (txb & (1 << (i & 7))) ? SIG_TDI : 0;
Expand Down
8 changes: 4 additions & 4 deletions src/ch347jtag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class CH347Jtag : public JtagInterface {
CH347Jtag(uint32_t clkHZ, int8_t verbose);
virtual ~CH347Jtag();

int setClkFreq(uint32_t clkHZ) override;

int setClkFreq(uint32_t clkHZ) override { return _setClkFreq(clkHZ); };
int _setClkFreq(uint32_t clkHZ);
/* TMS */
int writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer) override;
int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override;
/* TDI */
int writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;
int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;
/* clk */
int toggleClk(uint8_t tms, uint8_t tdo, uint32_t clk_len) override;

Expand Down
4 changes: 2 additions & 2 deletions src/ch552_jtag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ int CH552_jtag::setClkFreq(uint32_t clkHZ) {
return ret;
}

int CH552_jtag::writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer)
int CH552_jtag::writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer)
{
(void) flush_buffer;
display("%s %d %d\n", __func__, len, (len/8)+1);
Expand Down Expand Up @@ -172,7 +172,7 @@ int CH552_jtag::flush()
return ret;
}

int CH552_jtag::writeTDI(uint8_t *tdi, uint8_t *tdo, uint32_t len, bool last)
int CH552_jtag::writeTDI(const uint8_t *tdi, uint8_t *tdo, uint32_t len, bool last)
{
bool rd_mode = (tdo) ? true : false;
/* 3 possible case :
Expand Down
4 changes: 2 additions & 2 deletions src/ch552_jtag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class CH552_jtag : public JtagInterface, private FTDIpp_MPSSE {
uint32_t getClkFreq() override {return FTDIpp_MPSSE::getClkFreq();}

/* TMS */
int writeTMS(uint8_t *tms, uint32_t len, bool flush_buffer) override;
int writeTMS(const uint8_t *tms, uint32_t len, bool flush_buffer) override;
/* clock */
int toggleClk(uint8_t tms, uint8_t tdi, uint32_t clk_len) override;
/* TDI */
int writeTDI(uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;
int writeTDI(const uint8_t *tx, uint8_t *rx, uint32_t len, bool end) override;

/*!
* \brief return internal buffer size (in byte).
Expand Down
Loading
Loading