Skip to content

Commit

Permalink
Rev.F. Software - add data and address bus support
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonsmartins committed Sep 29, 2022
1 parent 0605b1a commit 773c9bc
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 227 deletions.
57 changes: 52 additions & 5 deletions software/usbflashprog/backend/opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,70 @@ bool OpCode::getValueAsBool(const void *buf, size_t size) {
return (getValueAsByte(buf, size) != kCmdParamOff);
}

bool OpCode::setValue(void *buf, size_t size, float value) {
uint16_t OpCode::getValueAsWord(const void *buf, size_t size) {
if (size < 3) { return getValueAsByte(buf, size); }
if (!buf) { return 0; }
const uint8_t *pbuf = static_cast<const uint8_t*>(buf);
return (static_cast<uint16_t>(pbuf[1]) << 8) | static_cast<uint16_t>(pbuf[2]);
}

uint32_t OpCode::getValueAsDWord(const void *buf, size_t size) {
if (size < 4) { return getValueAsWord(buf, size); }
if (!buf) { return 0; }
const uint8_t *pbuf = static_cast<const uint8_t*>(buf);
if (size == 4) {
return (static_cast<uint32_t>(pbuf[1]) << 16) |
(static_cast<uint32_t>(pbuf[2]) << 8) |
(static_cast<uint32_t>(pbuf[3]));
} else {
return (static_cast<uint32_t>(pbuf[1]) << 24) |
(static_cast<uint32_t>(pbuf[2]) << 16) |
(static_cast<uint32_t>(pbuf[3]) << 8) |
(static_cast<uint32_t>(pbuf[4]));
}
}

bool OpCode::setFloat(void *buf, size_t size, float value) {
if (!buf || size < 3) { return false; }
uint8_t *pbuf = static_cast<uint8_t*>(buf);
float i, f = std::modf(value, &i);
double i, f = std::modf(value, &i);
pbuf[1] = i;
pbuf[2] = f * 100;
return true;
}

bool OpCode::setValue(void *buf, size_t size, int value) {
bool OpCode::setByte(void *buf, size_t size, uint8_t value) {
if (!buf || size < 2) { return false; }
uint8_t *pbuf = static_cast<uint8_t*>(buf);
pbuf[1] = value & 0xFF;
return true;
}

bool OpCode::setValue(void *buf, size_t size, bool value) {
return setValue(buf, size,
bool OpCode::setWord(void *buf, size_t size, uint16_t value) {
if (!buf || size < 3) { return false; }
uint8_t *pbuf = static_cast<uint8_t*>(buf);
pbuf[1] = (value >> 8) & 0xFF;
pbuf[2] = value & 0xFF;
return true;
}

bool OpCode::setDWord(void *buf, size_t size, uint32_t value) {
if (!buf || size < 4) { return false; }
uint8_t *pbuf = static_cast<uint8_t*>(buf);
if (size == 4) {
pbuf[1] = (value >> 16) & 0xFF;
pbuf[2] = (value >> 8) & 0xFF;
pbuf[3] = value & 0xFF;
} else {
pbuf[1] = (value >> 24) & 0xFF;
pbuf[2] = (value >> 16) & 0xFF;
pbuf[3] = (value >> 8) & 0xFF;
pbuf[4] = value & 0xFF;
}
return true;
}

bool OpCode::setBool(void *buf, size_t size, bool value) {
return setByte(buf, size,
static_cast<uint8_t>(value ? kCmdParamOn : kCmdParamOff));
}
80 changes: 73 additions & 7 deletions software/usbflashprog/backend/opcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,29 @@ enum kCmdOpCodeEnum {
/** @brief OPCODE / BUS : Opcode OE Ctrl. */
kCmdBusOE = 0x22,
/** @brief OPCODE / BUS : Opcode WE Ctrl. */
kCmdBusWE = 0x23
kCmdBusWE = 0x23,

/** @brief OPCODE / BUS : Opcode Address Clear. */
kCmdBusAddrClr = 0x31,
/** @brief OPCODE / BUS : Opcode Address Increment. */
kCmdBusAddrInc = 0x32,
/** @brief OPCODE / BUS : Opcode Address Set. */
kCmdBusAddrSet = 0x33,
/** @brief OPCODE / BUS : Opcode Address Set Byte. */
kCmdBusAddrSetB = 0x34,
/** @brief OPCODE / BUS : Opcode Address Set Word. */
kCmdBusAddrSetW = 0x35,

/** @brief OPCODE / BUS : Opcode Data Clear. */
kCmdBusDataClr = 0x41,
/** @brief OPCODE / BUS : Opcode Data Set. */
kCmdBusDataSet = 0x42,
/** @brief OPCODE / BUS : Opcode Data Set Byte. */
kCmdBusDataSetB = 0x43,
/** @brief OPCODE / BUS : Opcode Data Get. */
kCmdBusDataGet = 0x44,
/** @brief OPCODE / BUS : Opcode Data Get Byte. */
kCmdBusDataGetB = 0x45
};

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -176,7 +198,19 @@ static const TCmdOpCodeMap kCmdOpCodes = {

{ kCmdBusCE , { kCmdBusCE , "CE" , 1, 0} },
{ kCmdBusOE , { kCmdBusOE , "OE" , 1, 0} },
{ kCmdBusWE , { kCmdBusWE , "WE" , 1, 0} }
{ kCmdBusWE , { kCmdBusWE , "WE" , 1, 0} },

{ kCmdBusAddrClr , { kCmdBusAddrClr , "AddrClr" , 0, 0} },
{ kCmdBusAddrInc , { kCmdBusAddrInc , "AddrInc" , 0, 0} },
{ kCmdBusAddrSet , { kCmdBusAddrSet , "AddrSet" , 3, 0} },
{ kCmdBusAddrSetB, { kCmdBusAddrSetB, "AddrSetByte", 1, 0} },
{ kCmdBusAddrSetW, { kCmdBusAddrSetW, "AddrSetWord", 2, 0} },

{ kCmdBusDataClr , { kCmdBusDataClr , "DataClr" , 0, 0} },
{ kCmdBusDataSet , { kCmdBusDataSet , "DataSet" , 2, 0} },
{ kCmdBusDataSetB, { kCmdBusDataSetB, "DataSetByte", 1, 0} },
{ kCmdBusDataGet , { kCmdBusDataGet , "DataGet" , 0, 2} },
{ kCmdBusDataGetB, { kCmdBusDataGetB, "DataGetByte", 0, 1} }
};

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -237,6 +271,22 @@ class OpCode {
* @return Value of parameter as bool, or false if an error occurs.
*/
static bool getValueAsBool(const void *buf, size_t size);
/**
* @brief Gets the param value as word.
* @param buf Pointer to the buffer that contains the result
* of the communication.
* @param size Size of buffer, in bytes.
* @return Value of parameter as word, or 0 if an error occurs.
*/
static uint16_t getValueAsWord(const void *buf, size_t size);
/**
* @brief Gets the param value as double word.
* @param buf Pointer to the buffer that contains the result
* of the communication.
* @param size Size of buffer, in bytes.
* @return Value of parameter as double word, or 0 if an error occurs.
*/
static uint32_t getValueAsDWord(const void *buf, size_t size);
/**
* @brief Sets the param value into the communication frame.
* @param buf Pointer to the buffer that contains the
Expand All @@ -245,27 +295,43 @@ class OpCode {
* @param value Value to set.
* @return True if success, false otherwise.
*/
static bool setValue(void *buf, size_t size, float value);
static bool setFloat(void *buf, size_t size, float value);
/**
* @overload
* @brief Sets the param value into the communication frame.
* @param buf Pointer to the buffer that contains the
* communication frame.
* @param size Size of buffer, in bytes.
* @param value Value to set.
* @return True if success, false otherwise.
*/
static bool setValue(void *buf, size_t size, int value);
static bool setByte(void *buf, size_t size, uint8_t value);
/**
* @brief Sets the param value into the communication frame.
* @param buf Pointer to the buffer that contains the
* communication frame.
* @param size Size of buffer, in bytes.
* @param value Value to set.
* @return True if success, false otherwise.
*/
static bool setWord(void *buf, size_t size, uint16_t value);
/**
* @brief Sets the param value into the communication frame.
* @param buf Pointer to the buffer that contains the
* communication frame.
* @param size Size of buffer, in bytes.
* @param value Value to set.
* @return True if success, false otherwise.
*/
static bool setDWord(void *buf, size_t size, uint32_t value);
/**
* @overload
* @brief Sets the param value into the communication frame.
* @param buf Pointer to the buffer that contains the
* communication frame.
* @param size Size of buffer, in bytes.
* @param value Value to set.
* @return True if success, false otherwise.
*/
static bool setValue(void *buf, size_t size, bool value);
static bool setBool(void *buf, size_t size, bool value);
};

#endif // BACKEND_OPCODES_HPP_
64 changes: 52 additions & 12 deletions software/usbflashprog/backend/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ int TRunnerCommand::responseAsByte() const {
return OpCode::getValueAsByte(response.data(), response.size());
}

int TRunnerCommand::responseAsWord() const {
return OpCode::getValueAsWord(response.data(), response.size());
}

int TRunnerCommand::responseAsDWord() const {
return OpCode::getValueAsDWord(response.data(), response.size());
}

bool TRunnerCommand::responseAsBool() const {
return OpCode::getValueAsBool(response.data(), response.size());
}
Expand All @@ -48,25 +56,39 @@ void TRunnerCommand::set(kCmdOpCodeEnum code) {
params[0] = opcode.code;
}

void TRunnerCommand::set(kCmdOpCodeEnum code, float param) {
void TRunnerCommand::setFloat(kCmdOpCodeEnum code, float param) {
opcode = OpCode::getOpCode(code);
params.resize(opcode.params + 1);
params[0] = opcode.code;
OpCode::setFloat(params.data(), params.size(), param);
}

void TRunnerCommand::setByte(kCmdOpCodeEnum code, uint8_t param) {
opcode = OpCode::getOpCode(code);
params.resize(opcode.params + 1);
params[0] = opcode.code;
OpCode::setByte(params.data(), params.size(), param);
}

void TRunnerCommand::setWord(kCmdOpCodeEnum code, uint16_t param) {
opcode = OpCode::getOpCode(code);
params.resize(opcode.params + 1);
params[0] = opcode.code;
OpCode::setValue(params.data(), params.size(), param);
OpCode::setWord(params.data(), params.size(), param);
}

void TRunnerCommand::set(kCmdOpCodeEnum code, int param) {
void TRunnerCommand::setDWord(kCmdOpCodeEnum code, uint32_t param) {
opcode = OpCode::getOpCode(code);
params.resize(opcode.params + 1);
params[0] = opcode.code;
OpCode::setValue(params.data(), params.size(), param);
OpCode::setDWord(params.data(), params.size(), param);
}

void TRunnerCommand::set(kCmdOpCodeEnum code, bool param) {
void TRunnerCommand::setBool(kCmdOpCodeEnum code, bool param) {
opcode = OpCode::getOpCode(code);
params.resize(opcode.params + 1);
params[0] = opcode.code;
OpCode::setValue(params.data(), params.size(), param);
OpCode::setBool(params.data(), params.size(), param);
}

TRunnerCommand& TRunnerCommand::operator=(const TRunnerCommand& src) {
Expand Down Expand Up @@ -148,29 +170,47 @@ void Runner::send(kCmdOpCodeEnum code) {
if (wrFifo_.size() == 1) { write_(cmd); }
}

void Runner::send(kCmdOpCodeEnum code, int param) {
void Runner::sendByte(kCmdOpCodeEnum code, uint8_t param) {
if (wrFifo_.size() >= 1) { checkAlive_(); }
if (!serial_.isOpen()) { return; }
TRunnerCommand cmd;
cmd.setByte(code, param);
wrFifo_.enqueue(cmd);
if (wrFifo_.size() == 1) { write_(cmd); }
}

void Runner::sendWord(kCmdOpCodeEnum code, uint16_t param) {
if (wrFifo_.size() >= 1) { checkAlive_(); }
if (!serial_.isOpen()) { return; }
TRunnerCommand cmd;
cmd.setWord(code, param);
wrFifo_.enqueue(cmd);
if (wrFifo_.size() == 1) { write_(cmd); }
}

void Runner::sendDWord(kCmdOpCodeEnum code, uint32_t param) {
if (wrFifo_.size() >= 1) { checkAlive_(); }
if (!serial_.isOpen()) { return; }
TRunnerCommand cmd;
cmd.set(code, param);
cmd.setDWord(code, param);
wrFifo_.enqueue(cmd);
if (wrFifo_.size() == 1) { write_(cmd); }
}

void Runner::send(kCmdOpCodeEnum code, bool param) {
void Runner::sendBool(kCmdOpCodeEnum code, bool param) {
if (wrFifo_.size() >= 1) { checkAlive_(); }
if (!serial_.isOpen()) { return; }
TRunnerCommand cmd;
cmd.set(code, param);
cmd.setBool(code, param);
wrFifo_.enqueue(cmd);
if (wrFifo_.size() == 1) { write_(cmd); }
}

void Runner::send(kCmdOpCodeEnum code, float param) {
void Runner::sendFloat(kCmdOpCodeEnum code, float param) {
if (wrFifo_.size() >= 1) { checkAlive_(); }
if (!serial_.isOpen()) { return; }
TRunnerCommand cmd;
cmd.set(code, param);
cmd.setFloat(code, param);
wrFifo_.enqueue(cmd);
if (wrFifo_.size() == 1) { write_(cmd); }
}
Expand Down
Loading

0 comments on commit 773c9bc

Please sign in to comment.