improved code for time bytes evaluation

This commit is contained in:
Verkehrsrot 2019-07-24 13:52:24 +02:00
parent d35f1d97cd
commit 43c06be8e8

View File

@ -112,8 +112,9 @@ void process_timesync_req(void *taskparameter) {
// lock I2C bus and application irq to ensure accurate timing // lock I2C bus and application irq to ensure accurate timing
mask_user_IRQ(); mask_user_IRQ();
//if (!I2C_MUTEX_LOCK()) { // if (!I2C_MUTEX_LOCK()) {
// ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: i2c bus locking failed", // ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: i2c bus locking
// failed",
// millis() / 1000.0); // millis() / 1000.0);
// goto finish; // failure // goto finish; // failure
//} //}
@ -137,7 +138,7 @@ void process_timesync_req(void *taskparameter) {
finish: finish:
// end of time critical section: release I2C bus and app irq // end of time critical section: release I2C bus and app irq
//I2C_MUTEX_UNLOCK(); // I2C_MUTEX_UNLOCK();
unmask_user_IRQ(); unmask_user_IRQ();
timeSyncPending = false; timeSyncPending = false;
@ -182,13 +183,13 @@ int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) {
uint8_t k = seq_no % TIME_SYNC_SAMPLES; uint8_t k = seq_no % TIME_SYNC_SAMPLES;
uint16_t timestamp_msec; // convert 1/250th sec fractions to ms uint16_t timestamp_msec; // convert 1/250th sec fractions to ms
uint32_t timestamp_sec; uint32_t timestamp_sec, *timestamp_ptr;
// fetch timeserver time from 4 bytes containing the UTC seconds since // fetch timeserver time from 4 bytes containing the UTC seconds since
// unix epoch. Octet order is big endian. Casts are necessary, because buf // unix epoch. Octet order is big endian.
// is an array of single byte values, and they might overflow when shifted
timestamp_sec = ((uint32_t)buf[3]) | (((uint32_t)buf[2]) << 8) | timestamp_ptr = (uint32_t *)buf;
(((uint32_t)buf[1]) << 16) | (((uint32_t)buf[0]) << 24); timestamp_sec = __builtin_bswap32(*timestamp_ptr); // note: this is platform dependent (msb/lsb)
// the 5th byte contains the fractional seconds in 2^-8 second steps // the 5th byte contains the fractional seconds in 2^-8 second steps
timestamp_msec = 4 * buf[4]; timestamp_msec = 4 * buf[4];