# HG changeset patch # User František Kučera # Date 1496595308 -7200 # Node ID 616e71c2d754145d0a6ba926ba97bc9f1015fd1a # Parent ea8642c174958df5e3f65221e2f06aafe8209f2d lpt-signal-generator: měření času stráveného v outb() diff -r ea8642c17495 -r 616e71c2d754 c++/lpt-signal-generator/lpt.cpp --- a/c++/lpt-signal-generator/lpt.cpp Sun Jun 04 14:59:13 2017 +0200 +++ b/c++/lpt-signal-generator/lpt.cpp Sun Jun 04 18:55:08 2017 +0200 @@ -22,6 +22,7 @@ #include #include #include +#include // requires -std=c++11 /** * can not mix printf and wprintf @@ -37,8 +38,8 @@ using namespace std; -// run this program: g++ lpt.cpp && time chrt 1 ./a.out -// depending to frequency and machine performance the total time will be more than given duration +// run this program: g++ -std=c++11 lpt.cpp && time chrt 1 ./a.out +// depending on frequency and machine performance the total time will be more than given duration // despite the real-time priority, because some additional time is spent in outb() functions // so "duration" means total sleep time @@ -56,17 +57,17 @@ int addr = 0xe400; // parallel port address; first number of given port in: cat /proc/ioports | grep parport int baseFreq = 10000; // base frequency in Hz, should be between 5 000 between 10 000 Hz; lower frequency leads to dashed/dotted lines instead of greyscale int outputPower = 10; // duty cycle; 100 = 100 % - int duration = 5; // in seconds; total sleep time, see note above + int duration = 1; // in seconds; total sleep time, see note above int valueWidth = 10; // just for padding of printed values int labelWidth = -15; // just for padding of printed labels // ' = thousand separator // * = padding - wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix + wprintf(L"%*ls %*x\n", labelWidth, L"Parallel port:", valueWidth, addr); // or %#*x – adds 0x prefix wprintf(L"%*ls %'*d Hz\n", labelWidth, L"Base frequency:", valueWidth, baseFreq); - wprintf(L"%*ls %*d %%\n", labelWidth, L"Output power:", valueWidth, outputPower); - wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration); + wprintf(L"%*ls %*d %% duty cycle\n", labelWidth, L"Output power:", valueWidth, outputPower); + wprintf(L"%*ls %'*d s\n", labelWidth, L"Duration:", valueWidth, duration); // in microseconds: int oneSecond = 1000 * 1000; @@ -75,23 +76,29 @@ int cycleCount = duration * baseFreq; wprintf(L"%*ls %'*d ×\n", labelWidth, L"Cycle count:", valueWidth, cycleCount); - wprintf(L"%*ls %'*d μs in each cycle\n", labelWidth, L"Time on:", valueWidth, timeOn); - wprintf(L"%*ls %'*d μs in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff); + wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time on:", valueWidth, timeOn); + wprintf(L"%*ls %'*d μs 1× in each cycle\n", labelWidth, L"Time off:", valueWidth, timeOff); - wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …"); + //wprintf(L"%*ls %*ls\n", labelWidth, L"unicode test:", valueWidth, L"čeština → …"); if (ioperm(addr,1,1)) { fwprintf(stderr, L"Access denied to port %#x\n", addr), exit(1); } outb(0b00000000, addr); + auto startTimestamp = chrono::high_resolution_clock::now(); + for (int i = 0; i < cycleCount; i++) { - outb(0b00000001, addr); + outb(0b00000001, addr); // first data out pin = data out 0 = pin 2 on DB-25 connector usleep(timeOn); outb(0b00000000, addr); usleep(timeOff); } - wprintf(L"finished\n"); + auto finishTimestamp = chrono::high_resolution_clock::now(); + auto measuredDuration = chrono::duration_cast(finishTimestamp - startTimestamp).count(); + + wprintf(L"%*ls %'*d μs 2× in each cycle\n", labelWidth, L"single outb():", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount/2/1000); + wprintf(L"%*ls %'*d ns 2× in each cycle\n", labelWidth, L"single outb():", valueWidth, (measuredDuration-duration*oneSecond*1000)/cycleCount/2); }