Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Add configurable stop bits, various other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thenickdude committed Mar 6, 2015
1 parent f45d09c commit ef7d84c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
39 changes: 30 additions & 9 deletions utils/src/blackbox_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
typedef struct benchOptions_t {
int help;
int duration;
int baudRate;
int baudRate, stopBits;
int looptime;
const char *analyzeFilename;
const char *outputDevice;
Expand All @@ -34,6 +34,7 @@ typedef struct benchOptions_t {
benchOptions_t defaultOptions = {
.baudRate = 115200,
.looptime = 2500,
.stopBits = 1,
.duration = 15,
.help = 0,
.analyzeFilename = NULL, .outputDevice = NULL,
Expand Down Expand Up @@ -118,9 +119,10 @@ void writeBenchmarkFrames(int fd, int loopTime, int maxIterations, uint32_t *byt
pframe[0] = 'P';
iframe[0] = 'I';

// Start things off with a nice tidy "previous loop" timestamp
firstLoop = micros();
lastLoop = micros() - loopTime;

// Start things off with a nice tidy "previous loop" timestamp
lastLoop = firstLoop - loopTime;

*byteCount = 0;
timingErrorSum = 0;
Expand Down Expand Up @@ -168,8 +170,8 @@ bool runBenchmark(const char *deviceName)
int fd;
uint32_t byteCount, timingErrorUs, actualDurationMsec;

fprintf(stderr, "Opening %s at %d baud...\n", deviceName, options.baudRate);
fd = serial_open(deviceName, options.baudRate);
fprintf(stderr, "Opening %s at %d baud and %d stop bits...\n", deviceName, options.baudRate, options.stopBits);
fd = serial_open(deviceName, options.baudRate, options.stopBits);

if (fd == -1) {
fprintf(stderr, "Failed to open serial port, maybe try a different baud rate?\n");
Expand Down Expand Up @@ -218,7 +220,8 @@ bool runBenchmark(const char *deviceName)
writeBenchmarkFrames(fd, options.looptime, maxIterations, &byteCount, &timingErrorUs, &actualDurationMsec);

fprintf(stderr, "\nWrote %u bytes (%u bytes/s, %u baud) with average frame start time error %d us\n\n", byteCount,
(byteCount * 1000) / actualDurationMsec, (byteCount * 8 * 1000) / actualDurationMsec, timingErrorUs);
(byteCount * 1000) / actualDurationMsec, (unsigned int) (((uint64_t) byteCount * (8 + 1 + options.stopBits) * 1000) / actualDurationMsec),
timingErrorUs);

// Wait for card to flush
fprintf(stderr, "Waiting for OpenLog to finish...\n");
Expand Down Expand Up @@ -327,8 +330,13 @@ void analyzeLog(FILE *input)
iterationNumBuffer |= c << 24;

// Does this look like a reasonable iteration number?
if (iterationNumBuffer > lastIteration + 200 || iterationNumBuffer < lastIteration) {
if (iterationNumBuffer > lastIteration && iterationNumBuffer < lastIteration + 200) {
lastIteration = iterationNumBuffer;
} else {
// Resynchronize
currentFrameType = '\0';
ungetc(c, input);
continue;
}
} else if ((currentFrameType == 'I' && c != I_FRAME_FILL_BYTE) ||
(currentFrameType == 'P' && c != P_FRAME_FILL_BYTE)) {
Expand All @@ -343,6 +351,8 @@ void analyzeLog(FILE *input)
}

frameByteCount++;

// Did we get a complete frame worth of bytes?
if ((currentFrameType == 'I' && frameByteCount >= iSize) ||
(currentFrameType == 'P' && frameByteCount >= pSize)) {
goodFrames++;
Expand All @@ -353,7 +363,12 @@ void analyzeLog(FILE *input)
}

done:
fprintf(stderr, "Good frames %d, broken/missing iterations %d, total iterations %d\n", goodFrames, maxIterations - goodFrames, maxIterations);

if (sawHeaderIntro) {
fprintf(stderr, "Good frames %d, broken/missing iterations %d, total iterations %d\n", goodFrames, maxIterations - goodFrames, maxIterations);
} else {
fprintf(stderr, "Didn't find the benchmark file header, maybe the log got corrupted?\n");
}
}

void printUsage(const char *argv0)
Expand All @@ -366,11 +381,12 @@ void printUsage(const char *argv0)
"Options:\n"
" --help This page\n"
" --baud <num> Serial port baud rate (default %d)\n"
" --stopbits <1|2> Serial port stop bits (default %d)\n"
" --looptime <microsec> Simulated looptime (default %d us)\n"
" --duration <seconds> Simulation duration (default %d seconds)\n"
" --device <filename> Serial port to write to\n"
" --analyze <filename> OpenLog benchmark log to analyze\n"
"\n", argv0, defaultOptions.baudRate, defaultOptions.looptime, defaultOptions.duration
"\n", argv0, defaultOptions.baudRate, defaultOptions.stopBits, defaultOptions.looptime, defaultOptions.duration
);
}

Expand All @@ -383,6 +399,7 @@ static void parseCommandlineOptions(int argc, char **argv)
SETTING_DEVICE,
SETTING_BAUDRATE,
SETTING_LOOPTIME,
SETTING_STOPBITS,
SETTING_DURATION,
};

Expand All @@ -393,6 +410,7 @@ static void parseCommandlineOptions(int argc, char **argv)
{"analyze", required_argument, 0, SETTING_ANALYZE},
{"device", required_argument, 0, SETTING_DEVICE},
{"baud", required_argument, 0, SETTING_BAUDRATE},
{"stopbits", required_argument, 0, SETTING_STOPBITS},
{"looptime", required_argument, 0, SETTING_LOOPTIME},
{"duration", required_argument, 0, SETTING_DURATION},
{0, 0, 0, 0}
Expand All @@ -417,6 +435,9 @@ static void parseCommandlineOptions(int argc, char **argv)
case SETTING_BAUDRATE:
options.baudRate = atoi(optarg);
break;
case SETTING_STOPBITS:
options.stopBits = atoi(optarg);
break;
case SETTING_DURATION:
options.duration = atoi(optarg);
break;
Expand Down
11 changes: 9 additions & 2 deletions utils/src/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int rate_to_constant(int baudrate) {

// From https://jim.sh/ftx/files/linux-custom-baudrate.c:
/* Open serial port in raw mode, with custom baudrate if necessary */
int serial_open(const char *device, int rate)
int serial_open(const char *device, int rate, int stopbits)
{
struct termios options;
int fd;
Expand Down Expand Up @@ -96,9 +96,16 @@ int serial_open(const char *device, int rate)
cfmakeraw(&options);

options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~(PARENB | CRTSCTS | CSIZE | CSTOPB); // No hardware flow control, no parity, 1 stop bit, clear size
// No hardware flow control, no parity, clear size, no hangup on close
options.c_cflag &= ~(PARENB | CRTSCTS | CSIZE | HUPCL);
options.c_cflag |= CS8; //8 bits

if (stopbits >= 2) {
options.c_cflag |= CSTOPB;
} else {
options.c_cflag &= ~CSTOPB;
}

if (tcsetattr(fd, TCSANOW, &options) != 0)
return -1;

Expand Down
2 changes: 1 addition & 1 deletion utils/src/serial.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef SERIAL_H_
#define SERIAL_H_

int serial_open(const char *device, int rate);
int serial_open(const char *device, int rate, int stopbits);

#endif

0 comments on commit ef7d84c

Please sign in to comment.