2022-03-16 07:21 AM
2022-03-16 08:25 AM
Poll the pin periodically, perhaps every ms, and increment a counter when it's on.
Depends how precise you need to be, but for "hours", polling should be fine.
You can edit your post to add additional details.
2022-03-16 09:07 AM
Or use EXTI an watch/time transitions.
Could use a milli-second time base, the RTC, or TIM / DWT->CYCNT, depending on the resolution you seek.