-
Notifications
You must be signed in to change notification settings - Fork 6
/
PPS.cpp
178 lines (158 loc) · 4.17 KB
/
PPS.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* MIT License
*
* Copyright (c) 2020 Christopher B. Liebman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "PPS.h"
//#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "esp_log.h"
#include <string.h>
#include <sys/time.h>
#include "soc/soc.h"
#include "driver/timer.h"
static const char* TAG = "PPS";
PPS::PPS(MicroSecondTimer& timer, pps_data_t* data, PPS* ref)
: _timer(timer),
_data(data),
_ref(ref)
{
memset(data, 0, sizeof(pps_data_t));
ESP_LOGI(TAG, "pps data %u bytes @ 0x%08x", sizeof(pps_data_t), (uint32_t)data);
if (ref != nullptr)
{
data->pps_ref = ref->_data;
}
}
bool PPS::begin(gpio_num_t pps_pin, bool expect_negedge)
{
_pin = pps_pin;
_data->pps_pin = pps_pin;
if (_pin != GPIO_NUM_NC)
{
ESP_LOGI(TAG, "::begin configuring PPS pin %d", _pin);
gpio_set_direction(_pin, GPIO_MODE_INPUT);
ESP_LOGI(TAG, "::begin setup ppsISR for highint5");
ESP_INTR_DISABLE(31);
intr_matrix_set(1, ETS_GPIO_INTR_SOURCE, 31);
ESP_INTR_ENABLE(31);
gpio_set_intr_type(_pin, expect_negedge ? GPIO_INTR_NEGEDGE : GPIO_INTR_POSEDGE);
gpio_intr_enable(_pin);
}
return true;
}
int PPS::getLevel()
{
return gpio_get_level(_pin);
}
/**
* get current time & microseconds.
*/
time_t PPS::getTime(struct timeval* tv)
{
if (tv == nullptr)
{
return _data->pps_time;
}
// edge case! both seconds and _last_timer only change once a second, however,
// it changes via an interrupt so we make sure we have good values by making sure
// it is the same on second look
do
{
tv->tv_sec = _data->pps_time;
tv->tv_usec = _timer.getValue() - _data->pps_last;
// timer could be slightly off, insure microseconds is not returned as (or more) than a full second!
if (tv->tv_usec > 999999)
{
tv->tv_usec = 999999;
}
} while (tv->tv_sec != _data->pps_time); // insure we stay on the same seconds (to go with the microseconds)
return tv->tv_sec;
}
/**
* set the time, seconds only
*/
void PPS::setTime(time_t time)
{
_data->pps_time = time;
}
/**
* get the minimum time in microseconds between PPS pulses
*/
uint32_t PPS::getTimerMin()
{
return _data->pps_min;
}
/**
* get the last timer value
*/
uint32_t PPS::getTimerLast()
{
return _data->pps_last;
}
/**
* get the maximum time in microseconds between PPS pulses
*/
uint32_t PPS::getTimerMax()
{
return _data->pps_max;
}
/**
* get the interval in microseconds between PPS pulses
*/
uint32_t PPS::getTimerInterval()
{
return _data->pps_interval;
}
/**
* get the number of PPS pulses considered short (and invalid)
*/
uint32_t PPS::getTimerShort()
{
return _data->pps_short;
}
/**
* get the number of PPS pulses considered long (and invalid)
*/
uint32_t PPS::getTimerLong()
{
return _data->pps_long;
}
/**
* get the offset from ref
*/
int32_t PPS::getOffset()
{
return _data->pps_offset;
}
/**
* reset the offset from ref
*/
void PPS::resetOffset()
{
_data->pps_offset = 0;
}
/**
* set disabled preventing the ISR from counting
*/
void PPS::setDisable(bool disable)
{
_data->pps_disabled = disable;
}