Skip to content

Commit 205b7f2

Browse files
Alex Sergeevnashif
authored andcommitted
drivers: ethernet: stm32: Bugfix PTP clock read on second boundary
Current version of STM32 PTP clock reads current PTP time by querying second and nanosecond registers sequentially. It is possible for second to roll over between reading second and nanosecond registers, causing returned time to be off by a second. This bugfix resolves that issue. Signed-off-by: Alex Sergeev <[email protected]>
1 parent 4f39b62 commit 205b7f2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

drivers/ethernet/eth_stm32_hal.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,19 +1196,30 @@ static int ptp_clock_stm32_get(const struct device *dev,
11961196
struct eth_stm32_hal_dev_data *eth_dev_data = ptp_context->eth_dev_data;
11971197
ETH_HandleTypeDef *heth = &eth_dev_data->heth;
11981198
int key;
1199+
uint32_t second_2;
11991200

12001201
key = irq_lock();
12011202

12021203
#if defined(CONFIG_SOC_SERIES_STM32H7X)
12031204
tm->second = heth->Instance->MACSTSR;
12041205
tm->nanosecond = heth->Instance->MACSTNR;
1206+
second_2 = heth->Instance->MACSTSR;
12051207
#else
12061208
tm->second = heth->Instance->PTPTSHR;
12071209
tm->nanosecond = heth->Instance->PTPTSLR;
1210+
second_2 = heth->Instance->PTPTSHR;
12081211
#endif /* CONFIG_SOC_SERIES_STM32H7X */
12091212

12101213
irq_unlock(key);
12111214

1215+
if (tm->second != second_2 && tm->nanosecond < NSEC_PER_SEC / 2) {
1216+
/* Second rollover has happened during first measurement: second register
1217+
* was read before second boundary and nanosecond register was read after.
1218+
* We will use second_2 as a new second value.
1219+
*/
1220+
tm->second = second_2;
1221+
}
1222+
12121223
return 0;
12131224
}
12141225

0 commit comments

Comments
 (0)