rtctime.cpp: timepulse fixes

This commit is contained in:
Klaus K Wilting 2019-02-09 14:46:41 +01:00
parent 46f41d0ee8
commit e7a5ab3ca6

View File

@ -108,11 +108,16 @@ int timepulse_init(uint32_t pulse_period_ms) {
// use time pulse from GPS as time base with fixed 1Hz frequency // use time pulse from GPS as time base with fixed 1Hz frequency
#if defined GPS_INT && defined GPS_CLK #if defined GPS_INT && defined GPS_CLK
// setup external interupt for active low RTC INT pin // setup external interupt for active low RTC INT pin
pinMode(GPS_INT, INPUT_PULLDOWN); pinMode(GPS_INT, INPUT_PULLDOWN);
// setup external rtc 1Hz clock as pulse per second clock // setup external rtc 1Hz clock as pulse per second clock
ESP_LOGI(TAG, "Time base GPS timepulse"); ESP_LOGI(TAG, "Time base: GPS timepulse");
switch (GPS_CLK) {
case 1000:
break; // default GPS timepulse 1000ms
default:
goto pulse_period_error;
}
return 1; // success return 1; // success
// use pulse from on board RTC chip as time base with fixed frequency // use pulse from on board RTC chip as time base with fixed frequency
@ -120,11 +125,19 @@ int timepulse_init(uint32_t pulse_period_ms) {
// setup external interupt for active low RTC INT pin // setup external interupt for active low RTC INT pin
pinMode(RTC_INT, INPUT_PULLUP); pinMode(RTC_INT, INPUT_PULLUP);
// setup external rtc 1Hz clock as pulse per second clock // setup external rtc 1Hz clock as pulse per second clock
ESP_LOGI(TAG, "Time base external clock"); ESP_LOGI(TAG, "Time base: external RTC timepulse");
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz); switch (RTC_CLK) {
case 1000: // 1000ms
Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz);
break;
case 1: // 1ms
Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1kHz);
break;
default:
goto pulse_period_error;
}
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock); Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock);
I2C_MUTEX_UNLOCK(); I2C_MUTEX_UNLOCK();
} else { } else {
@ -133,21 +146,23 @@ int timepulse_init(uint32_t pulse_period_ms) {
} }
return 1; // success return 1; // success
#else #else
// use ESP32 hardware timer as time base with adjustable frequency // use ESP32 hardware timer as time base with adjustable frequency
if (pulse_period_ms) { if (pulse_period_ms) {
ESP_LOGI(TAG, "Time base ESP32 clock"); ESP_LOGI(TAG, "Time base: ESP32 hardware timer");
clockCycle = clockCycle =
timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec
timerAttachInterrupt(clockCycle, &CLOCKIRQ, true); timerAttachInterrupt(clockCycle, &CLOCKIRQ, true);
timerAlarmWrite(clockCycle, 10 * pulse_period_ms, true); // ms timerAlarmWrite(clockCycle, 10 * pulse_period_ms, true); // ms
} else { } else
ESP_LOGE(TAG, "Invalid pulse clock frequency"); goto pulse_period_error;
return 0; // failure
}
return 1; // success return 1; // success
#endif #endif
pulse_period_error:
ESP_LOGE(TAG, "Unknown timepulse period value");
return 0; // failure
} }
void timepulse_start() { void timepulse_start() {
@ -155,7 +170,7 @@ void timepulse_start() {
attachInterrupt(digitalPinToInterrupt(GPS_INT), CLOCKIRQ, RISING); attachInterrupt(digitalPinToInterrupt(GPS_INT), CLOCKIRQ, RISING);
#elif defined RTC_INT // start external clock #elif defined RTC_INT // start external clock
attachInterrupt(digitalPinToInterrupt(RTC_INT), CLOCKIRQ, FALLING); attachInterrupt(digitalPinToInterrupt(RTC_INT), CLOCKIRQ, FALLING);
#else // start internal clock #else // start internal clock
timerAlarmEnable(clockCycle); timerAlarmEnable(clockCycle);
#endif #endif
} }