Skip to content

Commit

Permalink
debug:ioexp
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jul 19, 2019
1 parent ee615c4 commit a5cf720
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/eez/apps/psu/ioexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class IOExpander {
uint8_t readGpio();
uint8_t readGpioB();

int getBitDirection(int io_bit); // 0: output, 1: input
bool testBit(int io_bit);
void changeBit(int io_bit, bool set);

Expand Down
73 changes: 73 additions & 0 deletions src/eez/apps/psu/scpi/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,79 @@ scpi_result_t scpi_cmd_debugCsvQ(scpi_t *context) {
return SCPI_RES_OK;
}

scpi_result_t scpi_cmd_debugIoexp(scpi_t *context) {
#ifdef DEBUG
scpi_psu_t *psu_context = (scpi_psu_t *)context->user_context;
uint8_t ch = psu_context->selected_channel_index;
Channel *channel = &Channel::get(ch - 1);

int32_t bit;
if (!SCPI_ParamInt(context, &bit, TRUE)) {
return SCPI_RES_ERR;
}
if (bit < 0 || bit > 15) {
SCPI_ErrorPush(context, SCPI_ERROR_DATA_OUT_OF_RANGE);
return SCPI_RES_ERR;
}

int32_t direction;
if (!SCPI_ParamInt(context, &direction, TRUE)) {
return SCPI_RES_ERR;
}
if (direction != channel->ioexp.getBitDirection(bit)) {
SCPI_ErrorPush(context, SCPI_ERROR_ILLEGAL_PARAMETER_VALUE);
return SCPI_RES_ERR;
}

bool state;
if (!SCPI_ParamBool(context, &state, TRUE)) {
return SCPI_RES_ERR;
}

channel->ioexp.changeBit(bit, state);

return SCPI_RES_OK;
#else
SCPI_ErrorPush(context, SCPI_ERROR_HARDWARE_MISSING);
return SCPI_RES_ERR;
#endif // DEBUG
}

scpi_result_t scpi_cmd_debugIoexpQ(scpi_t *context) {
#ifdef DEBUG
scpi_psu_t *psu_context = (scpi_psu_t *)context->user_context;
uint8_t ch = psu_context->selected_channel_index;
Channel *channel = &Channel::get(ch - 1);

int32_t bit;
if (!SCPI_ParamInt(context, &bit, TRUE)) {
return SCPI_RES_ERR;
}
if (bit < 0 || bit > 15) {
SCPI_ErrorPush(context, SCPI_ERROR_DATA_OUT_OF_RANGE);
return SCPI_RES_ERR;
}

int32_t direction;
if (!SCPI_ParamInt(context, &direction, TRUE)) {
return SCPI_RES_ERR;
}
if (direction != channel->ioexp.getBitDirection(bit)) {
SCPI_ErrorPush(context, SCPI_ERROR_ILLEGAL_PARAMETER_VALUE);
return SCPI_RES_ERR;
}

bool state = channel->ioexp.testBit(bit);

SCPI_ResultBool(context, state);

return SCPI_RES_OK;
#else
SCPI_ErrorPush(context, SCPI_ERROR_HARDWARE_MISSING);
return SCPI_RES_ERR;
#endif // DEBUG
}

} // namespace scpi
} // namespace psu
} // namespace eez
19 changes: 18 additions & 1 deletion src/eez/apps/psu/stm32/ioexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,25 @@ uint8_t IOExpander::readGpioB() {
return read(REG_GPIOB);
}

int IOExpander::getBitDirection(int bit) {
uint8_t dir;
if (bit < 8) {
dir = g_slots[channel.slotIndex].moduleType == MODULE_TYPE_DCP405 ? DCP405_REG_VALUE_IODIRA : DCP505_REG_VALUE_IODIRA;
} else {
dir = g_slots[channel.slotIndex].moduleType == MODULE_TYPE_DCP405 ? DCP405_REG_VALUE_IODIRB : DCP505_REG_VALUE_IODIRB;
bit -= 8;
}
return dir & (1 << bit) ? 1 : 0;
}

bool IOExpander::testBit(int io_bit) {
uint8_t value = readGpio();
uint8_t value;
if (io_bit < 8) {
value = readGpio();
} else {
value = readGpioB();
io_bit -= 8;
}
return value & (1 << io_bit) ? true : false;
}

Expand Down

0 comments on commit a5cf720

Please sign in to comment.