-
Notifications
You must be signed in to change notification settings - Fork 5
/
measure.c
68 lines (54 loc) · 2.64 KB
/
measure.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdint.h>
#include <stdbool.h>
#include "scaler/scaler.h"
#include "config/board_config.h"
#include "scaler/scaler_access.h"
#include "scaler/measure.h"
#include "peripherals/timer.h"
_INPUT_MEAS_DATA InputMeasData;
/*uint8_t MeasureResolution(bool isDigital)
{
if (isDigital)
ScalerWriteBit(S_SYNC_SELECT, 0, 0b1);
ScalerWriteBit(S_STABLE_MEASURE, 0, 0b0);
ScalerWriteBit(S_STABLE_MEASURE, 0, 0b1); // Start measurement
Poll(10, !ScalerReadBit(S_STABLE_MEASURE, 0));
ScalerWriteBit(S_STABLE_MEASURE, 1, 0b1); // Shift in the measured data
INPUT_MEASUREMENT_DATA.HSPolarity = ScalerReadBit(S_STABLE_MEASURE, 6); // Get stable line polarity
// TODO: Detect Sync type
}*/
int8_t MeasureSignal(bool isDigital)
{
if (isDigital)
ScalerWriteBit(S_SYNC_SELECT, 0, 0b1);
else
ScalerWriteBit(S_SYNC_SELECT, 0, 0b0);
// Begin HSync, VSync measurement
ScalerWriteBit(S_MEAS_HSYNC_PERIOD_HI, 5, 0b1);
Poll(50, !ScalerReadBit(S_MEAS_HSYNC_PERIOD_HI, 5))
{
ScalerWriteBit(S_MEAS_HSYNC_PERIOD_HI, 5, 0b0); // Reset start bit
return -1;
}
// Hsync, Vsync overflow test
if (ScalerReadBit(S_MEAS_HSYNC_PERIOD_HI, 4) ||
ScalerReadBit(S_MEAS_VSYNC_PERIOD_HI, 4) ||
ScalerReadBit(S_MEAS_VSYNC_PERIOD_HI, 5))
{
return -2;
}
ScalerWriteBit(S_MEAS_HSYNC_PERIOD_HI, 6, 0b1); // Shift in the measured data
InputMeasData.HTotal = (ScalerReadBits(S_MEAS_HSYNC_PERIOD_HI, 0, 4) << 8) | ScalerReadByte(S_MEAS_HSYNC_PERIOD_LO);
InputMeasData.VTotal = (ScalerReadBits(S_MEAS_VSYNC_PERIOD_HI, 0, 4) << 8) | ScalerReadByte(S_MEAS_VSYNC_PERIOD_LO);
ScalerWriteBit(S_MEAS_HSYNC_VSYNC_HIGH_PERIOD_SELECT, 0, 0b0); // Read HSYNC High period measurement result
InputMeasData.HSync = (ScalerReadBits(S_MEAS_HSYNC_VSYNC_HIGH_PERIOD_HI, 4, 4) << 8) | ScalerReadByte(S_MEAS_HSYNC_VSYNC_HIGH_PERIOD_LO);
ScalerWriteBit(S_MEAS_HSYNC_VSYNC_HIGH_PERIOD_SELECT, 0, 0b1); // Read VSYNC High period measurement result
InputMeasData.VSync = (ScalerReadBits(S_MEAS_HSYNC_VSYNC_HIGH_PERIOD_HI, 4, 4) << 8) | ScalerReadByte(S_MEAS_HSYNC_VSYNC_HIGH_PERIOD_LO);
// HFreq and VFreq can only be calculated in comparision to RTD_FREQ
// Thus they can be measured only in analog mode
if (!isDigital)
InputMeasData.HFreq = RTD_FREQ / InputMeasData.HTotal;
InputMeasData.VFreq = RTD_FREQ / InputMeasData.HTotal / InputMeasData.VTotal;
InputMeasData.HSPolarity = ScalerReadBit(S_MEAS_VSYNC_PERIOD_HI, 6);
InputMeasData.VSPolarity = ScalerReadBit(S_MEAS_VSYNC_PERIOD_HI, 7);
}