Skip to content

Commit

Permalink
0.3.rev.A. Improve communication device opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonsmartins committed Feb 13, 2024
1 parent a2ab715 commit c0fe4a7
Show file tree
Hide file tree
Showing 18 changed files with 238 additions and 312 deletions.
Binary file modified docs/specs.odt
Binary file not shown.
Binary file modified docs/specs.pdf
Binary file not shown.
45 changes: 24 additions & 21 deletions firmware/usbflashprog/modules/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,22 @@ void Device::setTwc(uint32_t value) {
settings_.twc = value;
}

void Device::setFlags(uint8_t value) {
void Device::configure(uint16_t value) {
uint8_t flags = value & 0xFF;
uint8_t algo = (value & 0xFF00) >> 8;
// 0 = Skip Write 0xFF
// 1 = Prog with VPP on
// 2 = VPP/~OE Pin
// 3 = ~PGM/~CE Pin
// 4 = PGM positive
// clang-format off
settings_.flags.skipFF = (value & 0x01) != 0;
settings_.flags.progWithVpp = (value & 0x02) != 0;
settings_.flags.vppOePin = (value & 0x04) != 0;
settings_.flags.pgmCePin = (value & 0x08) != 0;
settings_.flags.pgmPositive = (value & 0x10) != 0;
settings_.flags.skipFF = (flags & 0x01) != 0;
settings_.flags.progWithVpp = (flags & 0x02) != 0;
settings_.flags.vppOePin = (flags & 0x04) != 0;
settings_.flags.pgmCePin = (flags & 0x08) != 0;
settings_.flags.pgmPositive = (flags & 0x10) != 0;
// clang-format on
settings_.algo = algo;
}

bool Device::setupBus(uint8_t operation) {
Expand Down Expand Up @@ -423,11 +426,11 @@ bool Device::blankCheckW(size_t count) {
return blankCheckBuffer_(count, true);
}

bool Device::getId(uint16_t& id) {
bool Device::getId(uint32_t& id) {
// Setup bus
if (!setupBus(kCmdDeviceOperationGetId)) return false;
bool success = true;
uint8_t manufacturer, device;
uint16_t manufacturer, device;
// Get manufacturer data (byte)
manufacturer = dataGet();
// Increment Address (0x01)
Expand All @@ -449,21 +452,21 @@ bool Device::getId(uint16_t& id) {
// Check if equals data at address 0x200/0x201
if ((rd1 & 0xFF) == manufacturer && (rd2 & 0xFF) == device) success = false;

// If not success, return 0x00
// return ID
if (success) {
id = manufacturer;
id <<= 8;
id <<= 16;
id |= device;
}
// Reset Bus
setupBus(kCmdDeviceOperationReset);
return success;
}

bool Device::erase(uint8_t algo) {
bool Device::erase() {
// Erase entire chip
switch (algo) {
case kCmdDeviceAlgorithm27E:
switch (settings_.algo) {
case kCmdDeviceAlgorithmEPROM27:
return erase27E_();
default:
return false;
Expand Down Expand Up @@ -500,24 +503,24 @@ bool Device::erase27E_() {
return success;
}

bool Device::unprotect(uint8_t algo) {
bool Device::unprotect() {
// Unprotect entire chip
switch (algo) {
case kCmdDeviceAlgorithm28C64:
switch (settings_.algo) {
case kCmdDeviceAlgorithmEEPROM28C64:
return protect28C_(false, false);
case kCmdDeviceAlgorithm28C256:
case kCmdDeviceAlgorithmEEPROM28C256:
return protect28C_(false, true);
default:
return false;
}
}

bool Device::protect(uint8_t algo) {
bool Device::protect() {
// Protect entire chip
switch (algo) {
case kCmdDeviceAlgorithm28C64:
switch (settings_.algo) {
case kCmdDeviceAlgorithmEEPROM28C64:
return protect28C_(true, false);
case kCmdDeviceAlgorithm28C256:
case kCmdDeviceAlgorithmEEPROM28C256:
return protect28C_(true, true);
default:
return false;
Expand Down
17 changes: 8 additions & 9 deletions firmware/usbflashprog/modules/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Device {
uint32_t twc;
/** @brief Device flags. */
TDeviceFlags flags;
/** @brief Device algorithm. */
uint8_t algo;
} TDeviceSettings;

/** @brief Defines a command to send to a device. */
Expand Down Expand Up @@ -265,10 +267,10 @@ class Device {
*/
void setTwc(uint32_t value);
/**
* @brief Device Set Flags.
* @brief Device Configure.
* @param value Value to set.
*/
void setFlags(uint8_t value);
void configure(uint16_t value);
/**
* @brief Device Setup Bus.
* @param operation Operation to realize.
Expand Down Expand Up @@ -372,25 +374,22 @@ class Device {
* @param id[out] Manufacturer ID (MSB); Device ID (LSB).
* @return True if success, false otherwise.
*/
bool getId(uint16_t& id);
bool getId(uint32_t& id);
/**
* @brief Device Erase.
* @param algo Device algorithm.
* @return True if success, false otherwise.
*/
bool erase(uint8_t algo);
bool erase();
/**
* @brief Device Unprotect.
* @param algo Device algorithm.
* @return True if success, false otherwise.
*/
bool unprotect(uint8_t algo);
bool unprotect();
/**
* @brief Device Protect.
* @param algo Device algorithm.
* @return True if success, false otherwise.
*/
bool protect(uint8_t algo);
bool protect();

private:
/* @brief VGenerator instance. */
Expand Down
95 changes: 33 additions & 62 deletions firmware/usbflashprog/modules/opcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ enum kCmdOpCodeEnum {
*/
kCmdDeviceSetTwc = 0x82,
/**
* @brief OPCODE / DEVICE : Opcode Device Set Flags.
* @details The parameter (one byte) represents the flags, and
* @brief OPCODE / DEVICE : Opcode Device Configure.
* @details The first parameter (one byte) represents the device
* algorithm.<br/>
* The second parameter (one byte) represents the flags, and
* follows the table:
* <pre>
* +----------------------+
Expand All @@ -147,8 +149,9 @@ enum kCmdOpCodeEnum {
* | 4 | PGM positive |
* +----------------------+
* </pre>
* @see kCmdDeviceAlgorithmEnum
*/
kCmdDeviceSetFlags = 0x83,
kCmdDeviceConfigure = 0x83,
/**
* @brief OPCODE / DEVICE : Opcode Device Setup Bus.
* @details The parameter (one byte) represents the operation, and
Expand Down Expand Up @@ -244,61 +247,22 @@ enum kCmdOpCodeEnum {
kCmdDeviceBlankCheckW = 0x8E,
/**
* @brief OPCODE / DEVICE : Opcode Device Get ID.
* @details The result (two bytes) represents Manufacturer/Device ID,
* @details The result (four bytes) represents Manufacturer/Device ID,
* following the table:
* <pre>
* +-------------------------------+
* |Response | Description |
* | First (MSB) | Manufacurer ID |
* | Second (LSB) | Device ID |
* +-------------------------------+
* +--------------------------------------------+
* |Response | Description |
* | First (MSB)/Second (LSB) | Manufacurer ID |
* | Third (MSB)/Fourth (LSB) | Device ID |
* +--------------------------------------------+
* </pre>
*/
kCmdDeviceGetId = 0x8F,
/**
* @brief OPCODE / DEVICE : Opcode Device Erase.
* @details The parameter (one byte) represents the algorithm, and
* follows the table:
* <pre>
* +-------------------------------+
* |Algorithm| Description |
* | 0x01 | EPROM 27E/W27/27SF |
* | 0x02 | EEPROM 28C64 |
* | 0x03 | EEPROM 28C256/upper |
* +-------------------------------+
* </pre>
* @see kCmdDeviceAlgorithmEnum
*/
/** @brief OPCODE / DEVICE : Opcode Device Erase. */
kCmdDeviceErase = 0x90,
/**
* @brief OPCODE / DEVICE : Opcode Device Unprotect.
* @details The parameter (one byte) represents the algorithm, and
* follows the table:
* <pre>
* +-------------------------------+
* |Algorithm| Description |
* | 0x01 | EPROM 27E/W27/27SF |
* | 0x02 | EEPROM 28C64 |
* | 0x03 | EEPROM 28C256/upper |
* +-------------------------------+
* </pre>
* @see kCmdDeviceAlgorithmEnum
*/
/** @brief OPCODE / DEVICE : Opcode Device Unprotect. */
kCmdDeviceUnprotect = 0x91,
/**
* @brief OPCODE / DEVICE : Opcode Device Protect.
* @details The parameter (one byte) represents the algorithm, and
* follows the table:
* <pre>
* +-------------------------------+
* |Algorithm| Description |
* | 0x01 | EPROM 27E/W27/27SF |
* | 0x02 | EEPROM 28C64 |
* | 0x03 | EEPROM 28C256/upper |
* +-------------------------------+
* </pre>
* @see kCmdDeviceAlgorithmEnum
*/
/** @brief OPCODE / DEVICE : Opcode Device Protect. */
kCmdDeviceProtect = 0x92
};

Expand All @@ -323,18 +287,25 @@ enum kCmdDeviceOperationEnum {

/**
* @brief Enumeration of the Device Algorithms.
* @see kCmdDeviceErase
* @see kCmdDeviceUnprotect
* @see kCmdDeviceConfigure
*/
enum kCmdDeviceAlgorithmEnum {
/** @brief CMD / DEVICE : Defines an unknown algorithm. */
kCmdDeviceAlgorithmUnknown = 0x00,
/** @brief CMD / DEVICE : Defines an algorithm EPROM 27E/SF/W27. */
kCmdDeviceAlgorithm27E = 0x01,
/** @brief CMD / DEVICE : Defines an algorithm SRAM. */
kCmdDeviceAlgorithmSRAM = 0x01,
/** @brief CMD / DEVICE : Defines an algorithm EPROM 27. */
kCmdDeviceAlgorithmEPROM27 = 0x02,
/** @brief CMD / DEVICE : Defines an algorithm EEPROM 28C64. */
kCmdDeviceAlgorithm28C64 = 0x02,
kCmdDeviceAlgorithmEEPROM28C64 = 0x03,
/** @brief CMD / DEVICE : Defines an algorithm EEPROM 28C256 or upper. */
kCmdDeviceAlgorithm28C256 = 0x03
kCmdDeviceAlgorithmEEPROM28C256 = 0x04,
/** @brief CMD / DEVICE : Defines an algorithm Flash 28F. */
kCmdDeviceAlgorithmFlash28F = 0x05,
/** @brief CMD / DEVICE : Defines an algorithm Flash Am28F. */
kCmdDeviceAlgorithmFlashAm28F = 0x06,
/** @brief CMD / DEVICE : Defines an algorithm Flash i28F. */
kCmdDeviceAlgorithmFlashI28F = 0x07
};

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -421,7 +392,7 @@ static const TCmdOpCodeMap kCmdOpCodes = {

{kCmdDeviceSetTwp , {kCmdDeviceSetTwp , "Device Set Twp" , 4, 0}},
{kCmdDeviceSetTwc , {kCmdDeviceSetTwc , "Device Set Twc" , 4, 0}},
{kCmdDeviceSetFlags , {kCmdDeviceSetFlags , "Device Set Flags" , 1, 0}},
{kCmdDeviceConfigure , {kCmdDeviceConfigure , "Device Configure" , 2, 0}},
{kCmdDeviceSetupBus , {kCmdDeviceSetupBus , "Device Setup Bus" , 1, 0}},
{kCmdDeviceRead , {kCmdDeviceRead , "Device Read" , 1, 0}},
{kCmdDeviceReadW , {kCmdDeviceReadW , "Device ReadWord" , 1, 0}},
Expand All @@ -433,10 +404,10 @@ static const TCmdOpCodeMap kCmdOpCodes = {
{kCmdDeviceVerifyW , {kCmdDeviceVerifyW , "Device VerifyWord" , 1, 0}},
{kCmdDeviceBlankCheck , {kCmdDeviceBlankCheck , "Device BlankCheck" , 1, 0}},
{kCmdDeviceBlankCheckW , {kCmdDeviceBlankCheckW , "Device BlankCheckWord" , 1, 0}},
{kCmdDeviceGetId , {kCmdDeviceGetId , "Device GetID" , 0, 2}},
{kCmdDeviceErase , {kCmdDeviceErase , "Device Erase" , 1, 0}},
{kCmdDeviceUnprotect , {kCmdDeviceUnprotect , "Device Unprotect" , 1, 0}},
{kCmdDeviceProtect , {kCmdDeviceProtect , "Device Protect" , 1, 0}}
{kCmdDeviceGetId , {kCmdDeviceGetId , "Device GetID" , 0, 4}},
{kCmdDeviceErase , {kCmdDeviceErase , "Device Erase" , 0, 0}},
{kCmdDeviceUnprotect , {kCmdDeviceUnprotect , "Device Unprotect" , 0, 0}},
{kCmdDeviceProtect , {kCmdDeviceProtect , "Device Protect" , 0, 0}}

};
// clang-format on
Expand Down
23 changes: 9 additions & 14 deletions firmware/usbflashprog/modules/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ void Runner::runDeviceSettingsCommand_(uint8_t opcode) {
serial_.putChar(kCmdResponseOk, true);
device_.setTwc(getParamAsDWord_());
break;
case kCmdDeviceSetFlags:
case kCmdDeviceConfigure:
serial_.putChar(kCmdResponseOk, true);
device_.setFlags(getParamAsByte_());
device_.configure(getParamAsWord_());
break;
case kCmdDeviceSetupBus:
if (device_.setupBus(getParamAsByte_())) {
Expand Down Expand Up @@ -457,15 +457,15 @@ void Runner::runDeviceVerifyCommand_(uint8_t opcode) {
}

void Runner::runDeviceGetIdCommand_(uint8_t opcode) {
uint16_t w;
uint32_t dw;
TByteArray response;
switch (opcode) {
case kCmdDeviceGetId:
if (device_.getId(w)) {
if (device_.getId(dw)) {
// response
response.resize(3);
response.resize(5);
response[0] = kCmdResponseOk;
createParamsFromWord_(&response, w);
createParamsFromDWord_(&response, dw);
serial_.putBuf(response.data(), response.size());
} else {
serial_.putChar(kCmdResponseNok);
Expand All @@ -477,11 +477,9 @@ void Runner::runDeviceGetIdCommand_(uint8_t opcode) {
}

void Runner::runDeviceEraseCommand_(uint8_t opcode) {
uint8_t algo;
switch (opcode) {
case kCmdDeviceErase:
algo = getParamAsByte_();
if (device_.erase(algo)) {
if (device_.erase()) {
// response
serial_.putChar(kCmdResponseOk);
} else {
Expand All @@ -494,20 +492,17 @@ void Runner::runDeviceEraseCommand_(uint8_t opcode) {
}

void Runner::runDeviceProtectCommand_(uint8_t opcode) {
uint8_t algo;
switch (opcode) {
case kCmdDeviceProtect:
algo = getParamAsByte_();
if (device_.protect(algo)) {
if (device_.protect()) {
// response
serial_.putChar(kCmdResponseOk);
} else {
serial_.putChar(kCmdResponseNok);
}
break;
case kCmdDeviceUnprotect:
algo = getParamAsByte_();
if (device_.unprotect(algo)) {
if (device_.unprotect()) {
// response
serial_.putChar(kCmdResponseOk);
} else {
Expand Down
Loading

0 comments on commit c0fe4a7

Please sign in to comment.