//#DOC Main, program entry and main loop
#include "main.h"
#include <otPort.h>
#include <otSerial.h>
#include <otTimer.h>
#include <otPrintf.h>
// Period and frequency meter realized using the interrupt generated on a port
// of the DSP from both sides (rising/falling) of the signal.
// The maximum measurable frequency is 100Hz and the minimum is 0.1Hz with a
// duty cycle of 50%. Minimum tick is 10ns
bool done = false, high = false;
U32 timValue[2] = { 0, 0 };
U32 outValue[2] = { 0, 0 };
U32 delta;
FAST_CODE_1 void callback()
{
PortFClearInterrupt(); // Clear interrupt flag
timValue[0] = TimerGetCounts(TIMER0); // Save counts
delta = ElapsedTimeNs(timValue[1], timValue[0]); // Store delta time
timValue[1] = timValue[0]; // Slide for next measure
done = true; // Signal measure is done
//PortFInterruptEnable(BIT(12), false);
}
void setup()
{
// Setup the UART0 used by printf
UartEnableSignals(UART0);
UartSetBaud(UART0, 115200);
// Init printf
setStdout(4096, UART0); // printf output is now directed on UART0
StartTimer(TIMER0); // Enable TIMER0 as counter, minimum tick is 10ns
// Set up the port for the measure
PortConfig(PP_F, 12, P_IN);
PortFSetInterruptService(BIT(12), callback, true);
PortFSetInterruptBothEdges(BIT(12), true);
// Sync, wait low level
while(PortGet(PP_F, 12));
PortFInterruptEnable(BIT(12), true);
}
void loop()
{
if(done)
{
done = false;
if(high)
{
printf("[H]-> ");
outValue[0] = delta;
}
else
{
printf("[L]-> ");
outValue[1] = delta;
}
printf("Pw = %10lu ns, F = %12.9lf Hz\n", delta, 1E9 / (outValue[0] + outValue[1]));
high ^= true;
//PortFInterruptEnable(BIT(12), true);
}
}
/* Program output, 1 Hz (30% duty cycle) signal generated with a RIGOL DG1062
* Legend:
* H is high level
* L is low level
* Pw is the pulse width
* F is the frequency
*
[H]-> Pw = 299995660 ns, F = 1.000013960 Hz
[L]-> Pw = 699990240 ns, F = 1.000014100 Hz
[H]-> Pw = 299995800 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995810 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995810 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990240 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995850 ns, F = 1.000013920 Hz
[L]-> Pw = 699990190 ns, F = 1.000013960 Hz
[H]-> Pw = 299995910 ns, F = 1.000013900 Hz
[L]-> Pw = 699990130 ns, F = 1.000013960 Hz
[H]-> Pw = 299995780 ns, F = 1.000014090 Hz
[L]-> Pw = 699990260 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013940 Hz
[L]-> Pw = 699990240 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995810 ns, F = 1.000013960 Hz
[L]-> Pw = 699990250 ns, F = 1.000013940 Hz
[H]-> Pw = 299995780 ns, F = 1.000013970 Hz
[L]-> Pw = 699990320 ns, F = 1.000013900 Hz
[H]-> Pw = 299995720 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000014050 Hz
[H]-> Pw = 299995810 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995810 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013960 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995870 ns, F = 1.000013900 Hz
[L]-> Pw = 699990160 ns, F = 1.000013970 Hz
[H]-> Pw = 299995910 ns, F = 1.000013930 Hz
[L]-> Pw = 699990120 ns, F = 1.000013970 Hz
[H]-> Pw = 299995780 ns, F = 1.000014100 Hz
[L]-> Pw = 699990240 ns, F = 1.000013980 Hz
[H]-> Pw = 299995800 ns, F = 1.000013960 Hz
[L]-> Pw = 699990230 ns, F = 1.000013970 Hz
[H]-> Pw = 299995800 ns, F = 1.000013970 Hz
[L]-> Pw = 699990220 ns, F = 1.000013980 Hz
[H]-> Pw = 299995800 ns, F = 1.000013980 Hz
[L]-> Pw = 699990370 ns, F = 1.000013830 Hz
*/