@@ -212,6 +212,72 @@ static int ptp_clock_stm32_rate_adjust(const struct device *dev, double ratio)
212212 return ret ;
213213}
214214
215+ static void eth_stm32_ptp_enable_timestamping (ETH_HandleTypeDef * heth )
216+ {
217+ /* Mask the Timestamp Trigger interrupt and enable timestamping */
218+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
219+ heth -> Instance -> MACIER &= ~(ETH_MACIER_TSIE );
220+ heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSENA ;
221+ #else
222+ heth -> Instance -> MACIMR &= ~(ETH_MACIMR_TSTIM );
223+ heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSE ;
224+ #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
225+ }
226+
227+ static void eth_stm32_ptp_set_addend (ETH_HandleTypeDef * heth , uint32_t addend_val )
228+ {
229+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
230+ heth -> Instance -> MACTSAR = addend_val ;
231+ heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSADDREG ;
232+ while (heth -> Instance -> MACTSCR & ETH_MACTSCR_TSADDREG_Msk ) {
233+ k_yield ();
234+ }
235+ #else
236+ heth -> Instance -> PTPTSAR = addend_val ;
237+ heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSARU ;
238+ while (heth -> Instance -> PTPTSCR & ETH_PTPTSCR_TSARU_Msk ) {
239+ k_yield ();
240+ }
241+ #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
242+ }
243+
244+ static void eth_stm32_ptp_enable_fine_timestamp_update (ETH_HandleTypeDef * heth )
245+ {
246+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
247+ heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSCFUPDT ;
248+ #else
249+ heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSFCU ;
250+ #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
251+ }
252+
253+ static void eth_stm32_ptp_enable_nsec_rollover (ETH_HandleTypeDef * heth )
254+ {
255+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
256+ heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSCTRLSSR ;
257+ #else
258+ heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSSSR ;
259+ #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
260+ }
261+
262+ static void eth_stm32_ptp_init_timestamp (ETH_HandleTypeDef * heth )
263+ {
264+ #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
265+ heth -> Instance -> MACSTSUR = 0 ;
266+ heth -> Instance -> MACSTNUR = 0 ;
267+ heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSINIT ;
268+ while (heth -> Instance -> MACTSCR & ETH_MACTSCR_TSINIT_Msk ) {
269+ k_yield ();
270+ }
271+ #else
272+ heth -> Instance -> PTPTSHUR = 0 ;
273+ heth -> Instance -> PTPTSLUR = 0 ;
274+ heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSSTI ;
275+ while (heth -> Instance -> PTPTSCR & ETH_PTPTSCR_TSSTI_Msk ) {
276+ k_yield ();
277+ }
278+ #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
279+ }
280+
215281static DEVICE_API (ptp_clock , api ) = {
216282 .set = ptp_clock_stm32_set ,
217283 .get = ptp_clock_stm32_get ,
@@ -234,19 +300,7 @@ static int ptp_stm32_init(const struct device *port)
234300 eth_dev_data -> ptp_clock = port ;
235301 ptp_context -> eth_dev_data = eth_dev_data ;
236302
237- /* Mask the Timestamp Trigger interrupt */
238- #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
239- heth -> Instance -> MACIER &= ~(ETH_MACIER_TSIE );
240- #else
241- heth -> Instance -> MACIMR &= ~(ETH_MACIMR_TSTIM );
242- #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
243-
244- /* Enable timestamping */
245- #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
246- heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSENA ;
247- #else
248- heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSE ;
249- #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
303+ eth_stm32_ptp_enable_timestamping (heth );
250304
251305 /* Query ethernet clock rate */
252306 ret = clock_control_get_rate (DEVICE_DT_GET (STM32_CLOCK_CONTROL_NODE ),
@@ -288,50 +342,14 @@ static int ptp_stm32_init(const struct device *port)
288342 */
289343 addend_val =
290344 UINT32_MAX * eth_dev_data -> clk_ratio ;
291- #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
292- heth -> Instance -> MACTSAR = addend_val ;
293- heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSADDREG ;
294- while (heth -> Instance -> MACTSCR & ETH_MACTSCR_TSADDREG_Msk ) {
295- k_yield ();
296- }
297- #else
298- heth -> Instance -> PTPTSAR = addend_val ;
299- heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSARU ;
300- while (heth -> Instance -> PTPTSCR & ETH_PTPTSCR_TSARU_Msk ) {
301- k_yield ();
302- }
303- #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
304345
305- /* Enable fine timestamp correction method */
306- #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
307- heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSCFUPDT ;
308- #else
309- heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSFCU ;
310- #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
346+ eth_stm32_ptp_set_addend (heth , addend_val );
311347
312- /* Enable nanosecond rollover into a new second */
313- #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
314- heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSCTRLSSR ;
315- #else
316- heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSSSR ;
317- #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
348+ eth_stm32_ptp_enable_fine_timestamp_update (heth );
318349
319- /* Initialize timestamp */
320- #if DT_HAS_COMPAT_STATUS_OKAY (st_stm32h7_ethernet )
321- heth -> Instance -> MACSTSUR = 0 ;
322- heth -> Instance -> MACSTNUR = 0 ;
323- heth -> Instance -> MACTSCR |= ETH_MACTSCR_TSINIT ;
324- while (heth -> Instance -> MACTSCR & ETH_MACTSCR_TSINIT_Msk ) {
325- k_yield ();
326- }
327- #else
328- heth -> Instance -> PTPTSHUR = 0 ;
329- heth -> Instance -> PTPTSLUR = 0 ;
330- heth -> Instance -> PTPTSCR |= ETH_PTPTSCR_TSSTI ;
331- while (heth -> Instance -> PTPTSCR & ETH_PTPTSCR_TSSTI_Msk ) {
332- k_yield ();
333- }
334- #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_ethernet) */
350+ eth_stm32_ptp_enable_nsec_rollover (heth );
351+
352+ eth_stm32_ptp_init_timestamp (heth );
335353
336354 /* Set PTP Configuration done */
337355 heth -> IsPtpConfigured = ETH_STM32_PTP_CONFIGURED ;
0 commit comments