Skip to content

Commit

Permalink
serial refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jul 16, 2019
1 parent 09cfd05 commit 51fdc19
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 228 deletions.
3 changes: 0 additions & 3 deletions src/eez/apps/psu/conf_advanced.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,6 @@
/// To prevent too fast switching betweeen current ranges
#define CURRENT_AUTO_RANGE_SWITCHING_DELAY_MS 5

/// Change to 0 if you want to use programming USB port
#define CONF_SERIAL_USE_NATIVE_USB_PORT 1

/// Change to 1 if you want to add jitter column in DLOG file
#define CONF_DLOG_JITTER 0

Expand Down
15 changes: 7 additions & 8 deletions src/eez/apps/psu/psu.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@

#include <eez/apps/psu/ontime.h>

#if CONF_SERIAL_USE_NATIVE_USB_PORT
#define SERIAL_PORT SerialUSB
#else
#define SERIAL_PORT Serial
#endif

/// Namespace for the everything from the EEZ.
namespace eez {

enum TestResult { TEST_FAILED = 0, TEST_OK = 1, TEST_SKIPPED = 2, TEST_WARNING = 3 };
enum TestResult {
TEST_FAILED,
TEST_OK,
TEST_CONNECTING,
TEST_SKIPPED,
TEST_WARNING
};

/// PSU firmware.
namespace psu {
Expand All @@ -59,7 +59,6 @@ void onProtectionTripped();

void tick();

void setEsrBits(int bit_mask);
void setQuesBits(int bit_mask, bool on);
void setOperBits(int bit_mask, bool on);

Expand Down
16 changes: 8 additions & 8 deletions src/eez/apps/psu/scpi/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ scpi_result_t scpi_cmd_debugMeasureVoltage(scpi_t *context) {
int16_t adc_data = channel->adc.read();
channel->eventAdcData(adc_data, false);

SERIAL_PORT.print((int)debug::g_uMon[channel->index - 1].get());
SERIAL_PORT.print(" ");
SERIAL_PORT.print(channel->u.mon_last, 5);
SERIAL_PORT.println("V");
Serial.print((int)debug::g_uMon[channel->index - 1].get());
Serial.print(" ");
Serial.print(channel->u.mon_last, 5);
Serial.println("V");

int32_t diff = micros() - tickCount;
if (diff < 48000L) {
Expand Down Expand Up @@ -254,10 +254,10 @@ scpi_result_t scpi_cmd_debugMeasureCurrent(scpi_t *context) {
int16_t adc_data = channel->adc.read();
channel->eventAdcData(adc_data, false);

SERIAL_PORT.print((int)debug::g_iMon[channel->index - 1].get());
SERIAL_PORT.print(" ");
SERIAL_PORT.print(channel->i.mon_last, 5);
SERIAL_PORT.println("A");
Serial.print((int)debug::g_iMon[channel->index - 1].get());
Serial.print(" ");
Serial.print(channel->i.mon_last, 5);
Serial.println("A");

int32_t diff = micros() - tickCount;
if (diff < 48000L) {
Expand Down
18 changes: 3 additions & 15 deletions src/eez/apps/psu/scpi/psu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ void printError(int_fast16_t err) {
if (serial::g_testResult == TEST_OK) {
char errorOutputBuffer[256];

SERIAL_PORT.print("**ERROR");
Serial.print("**ERROR");

char datetime_buffer[20] = { 0 };
if (datetime::getDateTimeAsString(datetime_buffer)) {
sprintf(errorOutputBuffer, " [%s]", datetime_buffer);
SERIAL_PORT.print(errorOutputBuffer);
Serial.print(errorOutputBuffer);
}

sprintf(errorOutputBuffer, ": %d,\"%s\"", (int16_t)err, SCPI_ErrorTranslate(err));
SERIAL_PORT.println(errorOutputBuffer);
Serial.println(errorOutputBuffer);
}

#if OPTION_WATCHDOG && (EEZ_PSU_SELECTED_REVISION == EEZ_PSU_REVISION_R3B4 || \
Expand Down Expand Up @@ -141,18 +141,6 @@ void resultChoiceName(scpi_t *context, scpi_choice_def_t *choice, int tag) {
}
}

void resetContext(scpi_t *context) {
scpi_psu_t *psuContext = (scpi_psu_t *)context->user_context;

psuContext->selected_channel_index = 1;

#if OPTION_SD_CARD
psuContext->currentDirectory[0] = 0;
#endif

SCPI_ErrorClear(context);
}

} // namespace scpi
} // namespace psu
} // namespace eez
55 changes: 20 additions & 35 deletions src/eez/apps/psu/serial_psu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#define CONF_CHUNK_SIZE CONF_SERIAL_BUFFER_SIZE

namespace eez {

using namespace scpi;

namespace psu {

using namespace scpi;
Expand All @@ -37,7 +40,7 @@ long g_bauds[] = { 4800, 9600, 19200, 38400, 57600, 115200 };
size_t g_baudsSize = sizeof(g_bauds) / sizeof(long);

size_t SCPI_Write(scpi_t *context, const char *data, size_t len) {
SERIAL_PORT.write(data, len);
Serial.write(data, len);
return len;
}

Expand Down Expand Up @@ -65,7 +68,7 @@ scpi_result_t SCPI_Control(scpi_t *context, scpi_ctrl_name_t ctrl, scpi_reg_val_
} else {
sprintf(errorOutputBuffer, "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val);
}
SERIAL_PORT.println(errorOutputBuffer);
Serial.println(errorOutputBuffer);
}

return SCPI_RES_OK;
Expand All @@ -75,7 +78,7 @@ scpi_result_t SCPI_Reset(scpi_t *context) {
if (serial::g_testResult == TEST_OK) {
char errorOutputBuffer[256];
strcpy(errorOutputBuffer, "**Reset\r\n");
SERIAL_PORT.println(errorOutputBuffer);
Serial.println(errorOutputBuffer);
}

return reset() ? SCPI_RES_OK : SCPI_RES_ERR;
Expand Down Expand Up @@ -118,31 +121,22 @@ UARTClass::UARTModes getConfig() {

void init() {
if (g_testResult == TEST_OK) {
SERIAL_PORT.end();
Serial.end();
}

if (!persist_conf::isSerialEnabled()) {
g_testResult = TEST_SKIPPED;
return;
}

SERIAL_PORT.begin(persist_conf::getBaudFromIndex(persist_conf::getSerialBaudIndex()),
Serial.begin(persist_conf::getBaudFromIndex(persist_conf::getSerialBaudIndex()),
getConfig());

#if CONF_WAIT_SERIAL && !CONF_SERIAL_USE_NATIVE_USB_PORT
while (!SERIAL_PORT)
;
#endif

while (SERIAL_PORT.available()) {
SERIAL_PORT.read();
}

#ifdef EEZ_PLATFORM_SIMULATOR
SERIAL_PORT.print("EEZ PSU software simulator ver. ");
SERIAL_PORT.println(FIRMWARE);
Serial.print("EEZ PSU software simulator ver. ");
Serial.println(FIRMWARE);
#else
SERIAL_PORT.println("EEZ PSU serial com ready");
Serial.println("EEZ PSU serial com ready");
#endif

scpi::init(g_scpiContext, g_scpiPsuContext, &g_scpiInterface, g_scpiInputBuffer,
Expand All @@ -151,30 +145,21 @@ void init() {
g_testResult = TEST_OK;
}

void tick(uint32_t tick_usec) {
if (g_testResult == TEST_OK) {
bool isConnected = (bool)SERIAL_PORT;

void onQueueMessage(uint32_t type, uint32_t param) {
if (type == SERIAL_LINE_STATE_CHANGED) {
bool isConnected = param ? true : false;
if (isConnected != g_isConnected) {
g_isConnected = isConnected;
if (g_isConnected) {
scpi::emptyBuffer(g_scpiContext);
}
}

if (g_isConnected) {
size_t n = SERIAL_PORT.available();
if (n > 0) {
char buffer[CONF_CHUNK_SIZE];
if (n > CONF_CHUNK_SIZE) {
n = CONF_CHUNK_SIZE;
}
for (size_t i = 0; i < n; ++i) {
buffer[i] = (char)SERIAL_PORT.read();
}
input(g_scpiContext, buffer, n);
}
}
} else if (type == SERIAL_INPUT_AVAILABLE) {
uint8_t *buffer;
uint32_t length;
Serial.getInputBuffer(param, &buffer, &length);
input(g_scpiContext, (const char *)buffer, length);
Serial.releaseInputBuffer();
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/eez/apps/psu/serial_psu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ extern TestResult g_testResult;
extern scpi_t g_scpiContext;

void init();
void tick(uint32_t tick_usec);

#define SERIAL_INPUT_AVAILABLE 1
#define SERIAL_LINE_STATE_CHANGED 2

void onQueueMessage(uint32_t type, uint32_t param);

bool isConnected();

Expand All @@ -45,7 +49,6 @@ enum Parity {
PARITY_SPACE
};


}
}
} // namespace eez::psu::serial
44 changes: 24 additions & 20 deletions src/eez/apps/psu/simulator/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,43 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <eez/apps/psu/psu.h>

#include <stdio.h>
#include <string.h>

#include <eez/apps/psu/psu.h>
#include <eez/apps/psu/serial_psu.h>
#include <eez/apps/psu/simulator/serial.h>

#include <eez/scpi/scpi.h>
using namespace eez::scpi;

UARTClass Serial;
UARTClass SerialUSB;

uint8_t g_serialInputChars[SCPI_QUEUE_SIZE + 1];
int g_serialInputCharPosition = 0;

void UARTClass::begin(unsigned long baud, UARTModes config) {
}

void UARTClass::end() {
}

void UARTClass::put(int ch) {
g_serialInputChars[g_serialInputCharPosition] = ch;

osMessagePut(g_scpiMessageQueueId, SCPI_QUEUE_SERIAL_MESSAGE(SERIAL_INPUT_AVAILABLE, g_serialInputCharPosition), osWaitForever);

g_serialInputCharPosition = (g_serialInputCharPosition + 1) % (SCPI_QUEUE_SIZE + 1);
}

void UARTClass::getInputBuffer(int bufferPosition, uint8_t **buffer, uint32_t *length) {
*buffer = &g_serialInputChars[bufferPosition];
*length = 1;
}

void UARTClass::releaseInputBuffer() {
}

int UARTClass::write(const char *buffer, int size) {
return fwrite(buffer, 1, size, stdout);
}
Expand Down Expand Up @@ -61,20 +82,3 @@ int UARTClass::println(float value, int numDigits) {
// TODO numDigits
return printf("%f\n", value);
}

int UARTClass::available(void) {
return input.size();
}

int UARTClass::read(void) {
int ch = input.front();
input.pop();
return ch;
}

void UARTClass::put(int ch) {
input.push(ch);
}

void UARTClass::flush() {
}
16 changes: 6 additions & 10 deletions src/eez/apps/psu/simulator/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,19 @@ class UARTClass {

void begin(unsigned long baud, UARTModes config = Mode_8N1);
void end();

void put(int ch);

void getInputBuffer(int bufferPosition, uint8_t **buffer, uint32_t *length);
void releaseInputBuffer();

int write(const char *buffer, int size);
int print(const char *data);
int println(const char *data);
int print(int value);
int println(int value);
int print(float value, int numDigits);
int println(float value, int numDigits);
operator bool() { return true; }
int available(void);
int read(void);
void flush(void);

void put(int ch);

private:
std::queue<int> input;
};

extern UARTClass Serial;
extern UARTClass SerialUSB;
Loading

0 comments on commit 51fdc19

Please sign in to comment.