From 3f6fe0e40c0636c6df3f5f549be328c84b4b82ad Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Tue, 8 Sep 2026 20:12:29 -0600 Subject: [PATCH] refactor(uavcan): base the libuavcan clock on HRT instead of a dedicated timer (#28569) * refactor(uavcan): base the libuavcan clock on HRT instead of a dedicated timer The stm32, stm32h7 and kinetis drivers each ran a 16-bit hardware timer with a software overflow extension, a second copy of what HRT provides, and slewed UTC by patching that accumulator from the timer interrupt. Every CAN board paid a general-purpose timer for it, and the bus time base led HRT by the timer phase at seeding even when the FC is the sync master. Monotonic time is now hrt_absolute_time() and UTC is HRT plus an offset that time sync moves. Each adjustment is applied directly; the rate PID and its lock accessors had no callers, and at the sub-100 us errors sync produces after convergence a 1 Hz offset correction bounds the error to crystal drift over one second. The timer number plumbing and the BOARD_UAVCAN_TIMER_OVERRIDE option go with it. * refactor(uavcan): drop the vestigial clock::init and the timer enables it left behind With the hardware timer gone, init() only set a flag, so the flag is set where the singleton is constructed. The three boards that enabled their override timer in defconfig alongside the override no longer need it. --- boards/accton-godwit/ga1/default.px4board | 1 - boards/amovlab/flycore/default.px4board | 1 - .../flycore/nuttx-config/nsh/defconfig | 1 - boards/ark/fmu-v6s/default.px4board | 1 - boards/ark/fmu-v6s/zenoh.px4board | 1 - boards/ark/fmu-v6x/default.px4board | 1 - boards/ark/fmu-v6x/zenoh.px4board | 1 - boards/ark/fpv/default.px4board | 1 - boards/ark/fpv/nuttx-config/nsh/defconfig | 1 - boards/ark/pi6x/default.px4board | 1 - boards/auterion/fmu-v6s/default.px4board | 1 - boards/auterion/fmu-v6s/zenoh.px4board | 1 - boards/auterion/fmu-v6x/default.px4board | 1 - boards/auterion/fmu-v6x/zenoh.px4board | 1 - boards/corvon/v5/default.px4board | 1 - boards/cuav/7-nano/default.px4board | 1 - boards/cuav/fmu-v6x/default.px4board | 1 - boards/cuav/nora/default.px4board | 1 - boards/cuav/x25-evo/default.px4board | 1 - boards/cuav/x25-mega/default.px4board | 1 - boards/cuav/x25-super/default.px4board | 1 - boards/cuav/x7pro/default.px4board | 1 - boards/cuav/x7pro/test.px4board | 1 - boards/holybro/kakuteh7-wing/default.px4board | 1 - .../holybro/kakuteh7dualimu/default.px4board | 1 - boards/narinfc/h7/default.px4board | 1 - boards/narinfc/h7/test.px4board | 1 - boards/px4/fmu-v5/cyphal.px4board | 1 - boards/px4/fmu-v5/debug.px4board | 1 - boards/px4/fmu-v5/default.px4board | 1 - boards/px4/fmu-v5/protected.px4board | 1 - boards/px4/fmu-v5/stackcheck.px4board | 1 - boards/px4/fmu-v5/test.px4board | 1 - boards/px4/fmu-v5/uavcanv0periph.px4board | 1 - boards/px4/fmu-v6c/default.px4board | 1 - boards/px4/fmu-v6c/neural.px4board | 1 - boards/px4/fmu-v6c/raptor.px4board | 1 - .../fmu-v6c/visionTargetEstMoving.px4board | 1 - .../fmu-v6c/visionTargetEstStatic.px4board | 1 - boards/px4/fmu-v6x/default.px4board | 1 - boards/px4/fmu-v6x/zenoh.px4board | 1 - boards/radiolink/PIX6/default.px4board | 1 - boards/svehicle/e2/default.px4board | 1 - boards/x-mav/ap-h743r1/default.px4board | 1 - .../ap-h743r1/nuttx-config/nsh/defconfig | 1 - boards/zeroone/x6/default.px4board | 1 - cmake/kconfig.cmake | 4 - src/drivers/uavcan/CMakeLists.txt | 11 - src/drivers/uavcan/Kconfig | 5 - .../uavcan/uavcan_drivers/kinetis/README.md | 1 - .../include/uavcan_kinetis/build_config.hpp | 9 - .../driver/include/uavcan_kinetis/clock.hpp | 113 +----- .../kinetis/driver/src/internal.hpp | 1 - .../kinetis/driver/src/uc_kinetis_clock.cpp | 318 ++-------------- .../include/uavcan_stm32/build_config.hpp | 9 - .../driver/include/uavcan_stm32/clock.hpp | 90 +---- .../stm32/driver/src/internal.hpp | 1 - .../stm32/driver/src/uc_stm32_clock.cpp | 354 ++---------------- .../include/uavcan_stm32h7/build_config.hpp | 9 - .../driver/include/uavcan_stm32h7/clock.hpp | 90 +---- .../stm32h7/driver/src/internal.hpp | 1 - .../stm32h7/driver/src/uc_stm32h7_clock.cpp | 354 ++---------------- src/drivers/uavcannode/CMakeLists.txt | 11 - 63 files changed, 107 insertions(+), 1320 deletions(-) diff --git a/boards/accton-godwit/ga1/default.px4board b/boards/accton-godwit/ga1/default.px4board index 675e5dd4320..c24abdf2af4 100644 --- a/boards/accton-godwit/ga1/default.px4board +++ b/boards/accton-godwit/ga1/default.px4board @@ -42,7 +42,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/amovlab/flycore/default.px4board b/boards/amovlab/flycore/default.px4board index 6b0de0f5c45..e421607690c 100644 --- a/boards/amovlab/flycore/default.px4board +++ b/boards/amovlab/flycore/default.px4board @@ -28,7 +28,6 @@ CONFIG_COMMON_RC=y CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=6 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/amovlab/flycore/nuttx-config/nsh/defconfig b/boards/amovlab/flycore/nuttx-config/nsh/defconfig index 69b562495fd..c8a6f1fd605 100644 --- a/boards/amovlab/flycore/nuttx-config/nsh/defconfig +++ b/boards/amovlab/flycore/nuttx-config/nsh/defconfig @@ -220,7 +220,6 @@ CONFIG_STM32H7_TIM1=y CONFIG_STM32H7_TIM2=y CONFIG_STM32H7_TIM3=y CONFIG_STM32H7_TIM4=y -CONFIG_STM32H7_TIM6=y CONFIG_STM32H7_TIM8=y CONFIG_STM32H7_UART4=y CONFIG_STM32H7_UART5=y diff --git a/boards/ark/fmu-v6s/default.px4board b/boards/ark/fmu-v6s/default.px4board index e3603cf1b41..a8ed5e2b1f7 100644 --- a/boards/ark/fmu-v6s/default.px4board +++ b/boards/ark/fmu-v6s/default.px4board @@ -43,7 +43,6 @@ CONFIG_COMMON_RC=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_CAMERA_FEEDBACK=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/ark/fmu-v6s/zenoh.px4board b/boards/ark/fmu-v6s/zenoh.px4board index 3ba1fc13e97..6bd35b129f0 100644 --- a/boards/ark/fmu-v6s/zenoh.px4board +++ b/boards/ark/fmu-v6s/zenoh.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_BOARD_CONSTRAINED_FLASH=y CONFIG_BOARD_CONSTRAINED_MEMORY=y CONFIG_MODULES_UXRCE_DDS_CLIENT=n diff --git a/boards/ark/fmu-v6x/default.px4board b/boards/ark/fmu-v6x/default.px4board index 77a12b597d5..147a7668cdc 100644 --- a/boards/ark/fmu-v6x/default.px4board +++ b/boards/ark/fmu-v6x/default.px4board @@ -43,7 +43,6 @@ CONFIG_COMMON_RC=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_CAMERA_FEEDBACK=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/ark/fmu-v6x/zenoh.px4board b/boards/ark/fmu-v6x/zenoh.px4board index 3ba1fc13e97..6bd35b129f0 100644 --- a/boards/ark/fmu-v6x/zenoh.px4board +++ b/boards/ark/fmu-v6x/zenoh.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_BOARD_CONSTRAINED_FLASH=y CONFIG_BOARD_CONSTRAINED_MEMORY=y CONFIG_MODULES_UXRCE_DDS_CLIENT=n diff --git a/boards/ark/fpv/default.px4board b/boards/ark/fpv/default.px4board index cca94b74fd4..54199025594 100644 --- a/boards/ark/fpv/default.px4board +++ b/boards/ark/fpv/default.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_PCA9685_PWM_OUT=y CONFIG_DRIVERS_PWM_OUT=y CONFIG_COMMON_RC=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/ark/fpv/nuttx-config/nsh/defconfig b/boards/ark/fpv/nuttx-config/nsh/defconfig index 99cece80102..8394dfaf574 100644 --- a/boards/ark/fpv/nuttx-config/nsh/defconfig +++ b/boards/ark/fpv/nuttx-config/nsh/defconfig @@ -220,7 +220,6 @@ CONFIG_STM32H7_SPI6_DMA=y CONFIG_STM32H7_SPI6_DMA_BUFFER=1024 CONFIG_STM32H7_TIM12=y CONFIG_STM32H7_TIM1=y -CONFIG_STM32H7_TIM2=y CONFIG_STM32H7_TIM3=y CONFIG_STM32H7_TIM4=y CONFIG_STM32H7_TIM5=y diff --git a/boards/ark/pi6x/default.px4board b/boards/ark/pi6x/default.px4board index 9933722c588..9d9cebd2eb5 100644 --- a/boards/ark/pi6x/default.px4board +++ b/boards/ark/pi6x/default.px4board @@ -26,7 +26,6 @@ CONFIG_DRIVERS_PWM_OUT=y CONFIG_COMMON_RC=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_COMMANDER=y CONFIG_MODULES_CONTROL_ALLOCATOR=y diff --git a/boards/auterion/fmu-v6s/default.px4board b/boards/auterion/fmu-v6s/default.px4board index 38659d3c346..dec70c51c0f 100644 --- a/boards/auterion/fmu-v6s/default.px4board +++ b/boards/auterion/fmu-v6s/default.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_POWER_MONITOR_INA228=y CONFIG_DRIVERS_POWER_MONITOR_INA238=y CONFIG_DRIVERS_UAVCAN=y CONFIG_BOARD_UAVCAN_INTERFACES=1 -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/auterion/fmu-v6s/zenoh.px4board b/boards/auterion/fmu-v6s/zenoh.px4board index 4ac2ed1bc68..2a2f2b8d619 100644 --- a/boards/auterion/fmu-v6s/zenoh.px4board +++ b/boards/auterion/fmu-v6s/zenoh.px4board @@ -1,5 +1,4 @@ # CONFIG_BOARD_UAVCAN_INTERFACES is not set -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_DRIVERS_UAVCAN=n CONFIG_DRIVERS_OSD_MSP_OSD=n CONFIG_MODULES_UXRCE_DDS_CLIENT=n diff --git a/boards/auterion/fmu-v6x/default.px4board b/boards/auterion/fmu-v6x/default.px4board index 1e9fe950156..fb0e11d9011 100644 --- a/boards/auterion/fmu-v6x/default.px4board +++ b/boards/auterion/fmu-v6x/default.px4board @@ -40,7 +40,6 @@ CONFIG_COMMON_RC=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/auterion/fmu-v6x/zenoh.px4board b/boards/auterion/fmu-v6x/zenoh.px4board index 1f625c024cf..46ef93c8e41 100644 --- a/boards/auterion/fmu-v6x/zenoh.px4board +++ b/boards/auterion/fmu-v6x/zenoh.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_DRIVERS_UAVCAN=n CONFIG_DRIVERS_OSD_MSP_OSD=n CONFIG_MODULES_UXRCE_DDS_CLIENT=n diff --git a/boards/corvon/v5/default.px4board b/boards/corvon/v5/default.px4board index 9a092136315..056579db915 100644 --- a/boards/corvon/v5/default.px4board +++ b/boards/corvon/v5/default.px4board @@ -38,7 +38,6 @@ CONFIG_DRIVERS_SMART_BATTERY_BATMON=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=6 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_ATTITUDE_ESTIMATOR_Q=y CONFIG_MODULES_BATTERY_STATUS=y diff --git a/boards/cuav/7-nano/default.px4board b/boards/cuav/7-nano/default.px4board index 45062669300..7b082a23838 100644 --- a/boards/cuav/7-nano/default.px4board +++ b/boards/cuav/7-nano/default.px4board @@ -29,7 +29,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/fmu-v6x/default.px4board b/boards/cuav/fmu-v6x/default.px4board index 686f4b1640f..73f5b9b8688 100644 --- a/boards/cuav/fmu-v6x/default.px4board +++ b/boards/cuav/fmu-v6x/default.px4board @@ -36,7 +36,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/nora/default.px4board b/boards/cuav/nora/default.px4board index 0e306d4ecf6..aa49988b8f0 100644 --- a/boards/cuav/nora/default.px4board +++ b/boards/cuav/nora/default.px4board @@ -36,7 +36,6 @@ CONFIG_DRIVERS_SMART_BATTERY_BATMON=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/x25-evo/default.px4board b/boards/cuav/x25-evo/default.px4board index 7b397cec62a..3e6b55eebc4 100644 --- a/boards/cuav/x25-evo/default.px4board +++ b/boards/cuav/x25-evo/default.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/x25-mega/default.px4board b/boards/cuav/x25-mega/default.px4board index 84083a82fb0..f96ebe6b274 100644 --- a/boards/cuav/x25-mega/default.px4board +++ b/boards/cuav/x25-mega/default.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/x25-super/default.px4board b/boards/cuav/x25-super/default.px4board index 6d1938713f1..d4ee6d0414d 100644 --- a/boards/cuav/x25-super/default.px4board +++ b/boards/cuav/x25-super/default.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/x7pro/default.px4board b/boards/cuav/x7pro/default.px4board index ad97b1585d3..120a99e9019 100644 --- a/boards/cuav/x7pro/default.px4board +++ b/boards/cuav/x7pro/default.px4board @@ -36,7 +36,6 @@ CONFIG_DRIVERS_SMART_BATTERY_BATMON=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/cuav/x7pro/test.px4board b/boards/cuav/x7pro/test.px4board index a9fb25cc6ac..5e45f0c68a9 100644 --- a/boards/cuav/x7pro/test.px4board +++ b/boards/cuav/x7pro/test.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_DRIVERS_ADC_ADS1115=n CONFIG_DRIVERS_IMU_ANALOG_DEVICES_ADIS16448=n CONFIG_DRIVERS_IRLOCK=n diff --git a/boards/holybro/kakuteh7-wing/default.px4board b/boards/holybro/kakuteh7-wing/default.px4board index 41d1c6e45e1..9efdcfc39c4 100644 --- a/boards/holybro/kakuteh7-wing/default.px4board +++ b/boards/holybro/kakuteh7-wing/default.px4board @@ -25,7 +25,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=7 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/holybro/kakuteh7dualimu/default.px4board b/boards/holybro/kakuteh7dualimu/default.px4board index 7ef7576f8b2..38b274c96f8 100644 --- a/boards/holybro/kakuteh7dualimu/default.px4board +++ b/boards/holybro/kakuteh7dualimu/default.px4board @@ -28,7 +28,6 @@ CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y CONFIG_BOARD_UAVCAN_INTERFACES=1 -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=6 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/narinfc/h7/default.px4board b/boards/narinfc/h7/default.px4board index 64e5481fab9..404b7e76dc4 100644 --- a/boards/narinfc/h7/default.px4board +++ b/boards/narinfc/h7/default.px4board @@ -36,7 +36,6 @@ CONFIG_DRIVERS_SMART_BATTERY_BATMON=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/narinfc/h7/test.px4board b/boards/narinfc/h7/test.px4board index a9fb25cc6ac..5e45f0c68a9 100644 --- a/boards/narinfc/h7/test.px4board +++ b/boards/narinfc/h7/test.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_DRIVERS_ADC_ADS1115=n CONFIG_DRIVERS_IMU_ANALOG_DEVICES_ADIS16448=n CONFIG_DRIVERS_IRLOCK=n diff --git a/boards/px4/fmu-v5/cyphal.px4board b/boards/px4/fmu-v5/cyphal.px4board index bb753f27137..d674c9f0f9b 100644 --- a/boards/px4/fmu-v5/cyphal.px4board +++ b/boards/px4/fmu-v5/cyphal.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_DRIVERS_HEATER=n CONFIG_DRIVERS_UAVCAN=n CONFIG_CYPHAL_BMS_SUBSCRIBER=y diff --git a/boards/px4/fmu-v5/debug.px4board b/boards/px4/fmu-v5/debug.px4board index 7a6fa4e7cfd..fc276f2891f 100644 --- a/boards/px4/fmu-v5/debug.px4board +++ b/boards/px4/fmu-v5/debug.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_COMMON_BAROMETERS=n CONFIG_COMMON_DIFFERENTIAL_PRESSURE=n CONFIG_COMMON_MAGNETOMETER=n diff --git a/boards/px4/fmu-v5/default.px4board b/boards/px4/fmu-v5/default.px4board index 0f1e52e49ca..c89ddb7fafa 100644 --- a/boards/px4/fmu-v5/default.px4board +++ b/boards/px4/fmu-v5/default.px4board @@ -40,7 +40,6 @@ CONFIG_DRIVERS_SMART_BATTERY_BATMON=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=6 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_ATTITUDE_ESTIMATOR_Q=y CONFIG_MODULES_BATTERY_STATUS=y diff --git a/boards/px4/fmu-v5/protected.px4board b/boards/px4/fmu-v5/protected.px4board index 7cd7f0c3af2..f2c0fa251ac 100644 --- a/boards/px4/fmu-v5/protected.px4board +++ b/boards/px4/fmu-v5/protected.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_COMMON_BAROMETERS=n CONFIG_COMMON_DIFFERENTIAL_PRESSURE=n CONFIG_COMMON_DISTANCE_SENSOR=n diff --git a/boards/px4/fmu-v5/stackcheck.px4board b/boards/px4/fmu-v5/stackcheck.px4board index a68062f4be5..4f0810f1879 100644 --- a/boards/px4/fmu-v5/stackcheck.px4board +++ b/boards/px4/fmu-v5/stackcheck.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_COMMON_BAROMETERS=n CONFIG_COMMON_DIFFERENTIAL_PRESSURE=n CONFIG_COMMON_DISTANCE_SENSOR=n diff --git a/boards/px4/fmu-v5/test.px4board b/boards/px4/fmu-v5/test.px4board index f6b826cd51e..524a1ed451a 100644 --- a/boards/px4/fmu-v5/test.px4board +++ b/boards/px4/fmu-v5/test.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_COMMON_HYGROMETERS=n CONFIG_COMMON_OSD=n CONFIG_DRIVERS_ADC_ADS1115=n diff --git a/boards/px4/fmu-v5/uavcanv0periph.px4board b/boards/px4/fmu-v5/uavcanv0periph.px4board index d0133ef13e6..4569f781a5e 100644 --- a/boards/px4/fmu-v5/uavcanv0periph.px4board +++ b/boards/px4/fmu-v5/uavcanv0periph.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_COMMON_BAROMETERS=n CONFIG_COMMON_DIFFERENTIAL_PRESSURE=n CONFIG_COMMON_HYGROMETERS=n diff --git a/boards/px4/fmu-v6c/default.px4board b/boards/px4/fmu-v6c/default.px4board index 86257d518b8..e3d99dfee21 100644 --- a/boards/px4/fmu-v6c/default.px4board +++ b/boards/px4/fmu-v6c/default.px4board @@ -34,7 +34,6 @@ CONFIG_DRIVERS_PX4IO=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/px4/fmu-v6c/neural.px4board b/boards/px4/fmu-v6c/neural.px4board index 70caaa39714..8d47993f486 100644 --- a/boards/px4/fmu-v6c/neural.px4board +++ b/boards/px4/fmu-v6c/neural.px4board @@ -30,7 +30,6 @@ CONFIG_DRIVERS_PX4IO=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_LIB_TFLM=y CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y diff --git a/boards/px4/fmu-v6c/raptor.px4board b/boards/px4/fmu-v6c/raptor.px4board index d10441a0810..bc116e888ce 100644 --- a/boards/px4/fmu-v6c/raptor.px4board +++ b/boards/px4/fmu-v6c/raptor.px4board @@ -5,7 +5,6 @@ CONFIG_BOARD_SERIAL_TEL1="/dev/ttyS5" CONFIG_BOARD_SERIAL_TEL2="/dev/ttyS3" CONFIG_BOARD_SERIAL_TEL3="/dev/ttyS1" CONFIG_BOARD_TOOLCHAIN="arm-none-eabi" -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_COMMON_DIFFERENTIAL_PRESSURE=y CONFIG_COMMON_DISTANCE_SENSOR=y CONFIG_COMMON_LIGHT=y diff --git a/boards/px4/fmu-v6c/visionTargetEstMoving.px4board b/boards/px4/fmu-v6c/visionTargetEstMoving.px4board index e25ec56f5c5..81d732c85b0 100644 --- a/boards/px4/fmu-v6c/visionTargetEstMoving.px4board +++ b/boards/px4/fmu-v6c/visionTargetEstMoving.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_PX4IO=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/px4/fmu-v6c/visionTargetEstStatic.px4board b/boards/px4/fmu-v6c/visionTargetEstStatic.px4board index 50916f3cf75..b2357a8b0c9 100644 --- a/boards/px4/fmu-v6c/visionTargetEstStatic.px4board +++ b/boards/px4/fmu-v6c/visionTargetEstStatic.px4board @@ -32,7 +32,6 @@ CONFIG_DRIVERS_PX4IO=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/px4/fmu-v6x/default.px4board b/boards/px4/fmu-v6x/default.px4board index 1bc99405132..d73d2631731 100644 --- a/boards/px4/fmu-v6x/default.px4board +++ b/boards/px4/fmu-v6x/default.px4board @@ -43,7 +43,6 @@ CONFIG_COMMON_RC=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/px4/fmu-v6x/zenoh.px4board b/boards/px4/fmu-v6x/zenoh.px4board index a37900a64b0..bc89a9c77f7 100644 --- a/boards/px4/fmu-v6x/zenoh.px4board +++ b/boards/px4/fmu-v6x/zenoh.px4board @@ -1,4 +1,3 @@ -# CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE is not set CONFIG_DRIVERS_UAVCAN=n CONFIG_MODULES_UXRCE_DDS_CLIENT=n CONFIG_BOARD_CONSTRAINED_FLASH=y diff --git a/boards/radiolink/PIX6/default.px4board b/boards/radiolink/PIX6/default.px4board index bdfec975da4..6ced46cd99a 100644 --- a/boards/radiolink/PIX6/default.px4board +++ b/boards/radiolink/PIX6/default.px4board @@ -37,7 +37,6 @@ CONFIG_DRIVERS_SMART_BATTERY_BATMON=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=6 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_ATTITUDE_ESTIMATOR_Q=y CONFIG_MODULES_BATTERY_STATUS=y diff --git a/boards/svehicle/e2/default.px4board b/boards/svehicle/e2/default.px4board index af8c2f9704f..9263701863f 100644 --- a/boards/svehicle/e2/default.px4board +++ b/boards/svehicle/e2/default.px4board @@ -35,7 +35,6 @@ CONFIG_COMMON_RC=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/boards/x-mav/ap-h743r1/default.px4board b/boards/x-mav/ap-h743r1/default.px4board index 1cd1f400c5c..bd48b12053f 100644 --- a/boards/x-mav/ap-h743r1/default.px4board +++ b/boards/x-mav/ap-h743r1/default.px4board @@ -29,7 +29,6 @@ CONFIG_COMMON_RC=y CONFIG_COMMON_TELEMETRY=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=4 CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y CONFIG_MODULES_COMMANDER=y diff --git a/boards/x-mav/ap-h743r1/nuttx-config/nsh/defconfig b/boards/x-mav/ap-h743r1/nuttx-config/nsh/defconfig index ad6ccf877c8..1bdc06d860e 100644 --- a/boards/x-mav/ap-h743r1/nuttx-config/nsh/defconfig +++ b/boards/x-mav/ap-h743r1/nuttx-config/nsh/defconfig @@ -221,7 +221,6 @@ CONFIG_STM32H7_SPI_DMATHRESHOLD=8 CONFIG_STM32H7_TIM1=y CONFIG_STM32H7_TIM2=y CONFIG_STM32H7_TIM3=y -CONFIG_STM32H7_TIM4=y CONFIG_STM32H7_TIM8=y CONFIG_STM32H7_UART4=y CONFIG_STM32H7_UART7=y diff --git a/boards/zeroone/x6/default.px4board b/boards/zeroone/x6/default.px4board index b357876dc77..5c961e60cb6 100644 --- a/boards/zeroone/x6/default.px4board +++ b/boards/zeroone/x6/default.px4board @@ -38,7 +38,6 @@ CONFIG_DRIVERS_RC_INPUT=y CONFIG_DRIVERS_SAFETY_BUTTON=y CONFIG_DRIVERS_TONE_ALARM=y CONFIG_DRIVERS_UAVCAN=y -CONFIG_BOARD_UAVCAN_TIMER_OVERRIDE=2 CONFIG_MODULES_AIRSPEED_SELECTOR=y CONFIG_MODULES_BATTERY_STATUS=y CONFIG_MODULES_CAMERA_FEEDBACK=y diff --git a/cmake/kconfig.cmake b/cmake/kconfig.cmake index 741e5e85bf3..3a00cc5818f 100644 --- a/cmake/kconfig.cmake +++ b/cmake/kconfig.cmake @@ -388,10 +388,6 @@ if(EXISTS ${BOARD_DEFCONFIG}) set(config_uavcan_num_ifaces ${UAVCAN_INTERFACES} CACHE INTERNAL "UAVCAN interfaces" FORCE) endif() - if(UAVCAN_TIMER_OVERRIDE) - set(config_uavcan_timer_override ${UAVCAN_TIMER_OVERRIDE} CACHE INTERNAL "UAVCAN TIMER OVERRIDE" FORCE) - endif() - # OPTIONS if(CONSTRAINED_FLASH) diff --git a/src/drivers/uavcan/CMakeLists.txt b/src/drivers/uavcan/CMakeLists.txt index 28ec86ef5a8..229221cb744 100644 --- a/src/drivers/uavcan/CMakeLists.txt +++ b/src/drivers/uavcan/CMakeLists.txt @@ -44,22 +44,12 @@ set(UAVCAN_PLATFORM "generic") if(CONFIG_ARCH_CHIP) if(${CONFIG_NET_CAN} MATCHES "y") set(UAVCAN_DRIVER "socketcan") - set(UAVCAN_TIMER 1) elseif(${CONFIG_ARCH_CHIP} MATCHES "kinetis") set(UAVCAN_DRIVER "kinetis") - set(UAVCAN_TIMER 1) elseif(${CONFIG_ARCH_CHIP} MATCHES "stm32h7") set(UAVCAN_DRIVER "stm32h7") - set(UAVCAN_TIMER 5) # The default timer is TIM5 - if (DEFINED config_uavcan_timer_override) - set (UAVCAN_TIMER ${config_uavcan_timer_override}) - endif() elseif(${CONFIG_ARCH_CHIP} MATCHES "stm32") set(UAVCAN_DRIVER "stm32") - set(UAVCAN_TIMER 6) # The default timer is TIM6 - if (DEFINED config_uavcan_timer_override) - set (UAVCAN_TIMER ${config_uavcan_timer_override}) - endif() endif() endif() @@ -82,7 +72,6 @@ string(TOUPPER "${UAVCAN_DRIVER}" UAVCAN_DRIVER_UPPER) add_definitions( -DUAVCAN_${UAVCAN_DRIVER_UPPER}_${OS_UPPER}=1 -DUAVCAN_${UAVCAN_DRIVER_UPPER}_NUM_IFACES=${config_uavcan_num_ifaces} - -DUAVCAN_${UAVCAN_DRIVER_UPPER}_TIMER_NUMBER=${UAVCAN_TIMER} -DUAVCAN_NUM_IFACES=${config_uavcan_num_ifaces} -DUAVCAN_CPP_VERSION=UAVCAN_CPP03 -DUAVCAN_DRIVER=uavcan_${UAVCAN_DRIVER} diff --git a/src/drivers/uavcan/Kconfig b/src/drivers/uavcan/Kconfig index abf4d8367af..66d454c96ca 100644 --- a/src/drivers/uavcan/Kconfig +++ b/src/drivers/uavcan/Kconfig @@ -105,8 +105,3 @@ depends on DRIVERS_UAVCAN string "UAVCAN peripheral firmware" help list of UAVCAN peripheral firmware to build and embed - -menuconfig BOARD_UAVCAN_TIMER_OVERRIDE -depends on DRIVERS_UAVCAN - int "UAVCAN timer override" - default 0 diff --git a/src/drivers/uavcan/uavcan_drivers/kinetis/README.md b/src/drivers/uavcan/uavcan_drivers/kinetis/README.md index 5d04cdd2b0f..aa0f45ebca8 100644 --- a/src/drivers/uavcan/uavcan_drivers/kinetis/README.md +++ b/src/drivers/uavcan/uavcan_drivers/kinetis/README.md @@ -15,7 +15,6 @@ and the following commandline defines: | Setting | Description | |-------------------|-----------------------------------------------------------------------------------| |UAVCAN_KINETIS_NUM_IFACES | - {1..2} Sets the number of CAN interfaces the SW will support | -|UAVCAN_KINETIS_TIMER_NUMBER | - {0..3} Sets the Periodic Interrupt Timer (PITn) channel | Things that could be improved: 1. Build time command line configuration of Mailbox/FIFO and filters diff --git a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/build_config.hpp b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/build_config.hpp index 02e27d3a801..6d2d127b0af 100644 --- a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/build_config.hpp +++ b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/build_config.hpp @@ -18,12 +18,3 @@ #if !defined(UAVCAN_KINETIS_NUM_IFACES) || (UAVCAN_KINETIS_NUM_IFACES != 1 && UAVCAN_KINETIS_NUM_IFACES != 2) # error "UAVCAN_KINETIS_NUM_IFACES must be set to either 1 or 2" #endif - -/** - * Any PIT timer channel (PIT0-PIT3) - * e.g. -DUAVCAN_KINETIS_TIMER_NUMBER=2 - */ -#ifndef UAVCAN_KINETIS_TIMER_NUMBER -// In this case the clock driver should be implemented by the application -# define UAVCAN_KINETIS_TIMER_NUMBER 0 -#endif diff --git a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/clock.hpp b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/clock.hpp index f593feb60cd..9900498e252 100644 --- a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/clock.hpp +++ b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/include/uavcan_kinetis/clock.hpp @@ -11,123 +11,44 @@ namespace uavcan_kinetis { +/* + * Monotonic time is HRT. UTC is HRT plus an offset that time sync moves, so + * the bus time base shares HRT's rate and needs no hardware of its own. + */ namespace clock { -/** - * Starts the clock. - * Can be called multiple times, only the first call will be effective. - */ -void init(); - -/** - * Returns current monotonic time since the moment when clock::init() was called. - * This function is thread safe. - */ uavcan::MonotonicTime getMonotonic(); /** - * Sets the driver's notion of the system UTC. It should be called - * at startup and any time the system clock is updated from an - * external source that is not the UAVCAN Timesync master. - * This function is thread safe. - */ -void setUtc(uavcan::UtcTime time); - -/** - * Returns UTC time if it has been set, otherwise returns zero time. - * This function is thread safe. + * Zero until set by setUtc() or the first adjustUtc(). */ uavcan::UtcTime getUtc(); /** - * Performs UTC phase and frequency adjustment. - * The UTC time will be zero until first adjustment has been performed. - * This function is thread safe. + * UTC reads `time` from now on. + */ +void setUtc(uavcan::UtcTime time); + +/** + * Moves UTC by `adjustment`. While UTC is unset the adjustment is absolute: + * UTC reads `adjustment` from now on. */ void adjustUtc(uavcan::UtcDuration adjustment); - -/** - * UTC clock synchronization parameters - */ -struct UtcSyncParams { - float offset_p; ///< PPM per one usec error - float rate_i; ///< PPM per one PPM error for second - float rate_error_corner_freq; - float max_rate_correction_ppm; - float lock_thres_rate_ppm; - uavcan::UtcDuration lock_thres_offset; - uavcan::UtcDuration min_jump; ///< Min error to jump rather than change rate - - UtcSyncParams() - : offset_p(0.01F), - rate_i(0.02F), - rate_error_corner_freq(0.01F), - max_rate_correction_ppm(300.0F), - lock_thres_rate_ppm(2.0F), - lock_thres_offset(uavcan::UtcDuration::fromMSec(4)), - min_jump(uavcan::UtcDuration::fromMSec(10)) - { - } -}; - -/** - * Clock rate error. - * Positive if the hardware timer is slower than reference time. - * This function is thread safe. - */ -float getUtcRateCorrectionPPM(); - -/** - * Number of non-gradual adjustments performed so far. - * Ideally should be zero. - * This function is thread safe. - */ -uavcan::uint32_t getUtcJumpCount(); - -/** - * Whether UTC is synchronized and locked. - * This function is thread safe. - */ -bool isUtcLocked(); - -/** - * UTC sync params get/set. - * Both functions are thread safe. - */ -UtcSyncParams getUtcSyncParams(); -void setUtcSyncParams(const UtcSyncParams ¶ms); - } /** * Adapter for uavcan::ISystemClock. */ -class SystemClock : public uavcan::ISystemClock - , uavcan::Noncopyable +class SystemClock : public uavcan::ISystemClock, uavcan::Noncopyable { - SystemClock() - { - } + SystemClock() { } - virtual void adjustUtc(uavcan::UtcDuration adjustment) - { - clock::adjustUtc(adjustment); - } + virtual void adjustUtc(uavcan::UtcDuration adjustment) { clock::adjustUtc(adjustment); } public: - virtual uavcan::MonotonicTime getMonotonic() const - { - return clock::getMonotonic(); - } - virtual uavcan::UtcTime getUtc() const - { - return clock::getUtc(); - } + virtual uavcan::MonotonicTime getMonotonic() const { return clock::getMonotonic(); } + virtual uavcan::UtcTime getUtc() const { return clock::getUtc(); } - /** - * Calls clock::init() as needed. - * This function is thread safe. - */ static SystemClock &instance(); }; diff --git a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/internal.hpp b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/internal.hpp index a76b5fde5b9..1c844894981 100644 --- a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/internal.hpp +++ b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/internal.hpp @@ -11,7 +11,6 @@ # include # include "arm_internal.h" # include -# include # include # include #else diff --git a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/uc_kinetis_clock.cpp b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/uc_kinetis_clock.cpp index 95602840946..f468c917377 100644 --- a/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/uc_kinetis_clock.cpp +++ b/src/drivers/uavcan/uavcan_drivers/kinetis/driver/src/uc_kinetis_clock.cpp @@ -7,31 +7,7 @@ #include #include "internal.hpp" -#if UAVCAN_KINETIS_TIMER_NUMBER -# include -# include - -/* - * Timer instance - * todo:Consider using Lifetime Timer support - */ -# define TIMX_IRQHandler UAVCAN_KINETIS_GLUE3(PIT, UAVCAN_KINETIS_TIMER_NUMBER, _IRQHandler) -# define TIMX (KINETIS_PIT_BASE + (UAVCAN_KINETIS_TIMER_NUMBER << 4)) -# define TMR_REG(o) (TIMX + (o)) -# define TIMX_INPUT_CLOCK BOARD_BUS_FREQ -# define TIMX_INTERRUPT_FREQ 16 -# define TIMX_IRQn UAVCAN_KINETIS_GLUE2(KINETIS_IRQ_PITCH, UAVCAN_KINETIS_TIMER_NUMBER) - -# if UAVCAN_KINETIS_TIMER_NUMBER >= 0 && UAVCAN_KINETIS_TIMER_NUMBER <= 3 -# define KINETIS_PIT_LDVAL_OFFSET KINETIS_PIT_LDVAL0_OFFSET -# define KINETIS_PIT_CVAL_OFFSET KINETIS_PIT_CVAL0_OFFSET -# define KINETIS_PIT_TCTRL_OFFSET KINETIS_PIT_TCTRL0_OFFSET -# define KINETIS_PIT_TFLG_OFFSET KINETIS_PIT_TFLG0_OFFSET -# else -# error "This UAVCAN_KINETIS_TIMER_NUMBER is not supported yet" -# endif - -extern "C" UAVCAN_KINETIS_IRQ_HANDLER(TIMX_IRQHandler); +#include namespace uavcan_kinetis { @@ -40,266 +16,57 @@ namespace clock namespace { -const uavcan::uint32_t CountsPerPeriod = (TIMX_INPUT_CLOCK / TIMX_INTERRUPT_FREQ); -const uavcan::uint32_t CountsPerUs = (TIMX_INPUT_CLOCK / 1000000); -const uavcan::uint32_t USecPerOverflow = (1000000 / TIMX_INTERRUPT_FREQ); - Mutex mutex; - bool initialized = false; +// Written under a critical section so the CAN interrupt, which reads them +// without locking, never sees a torn 64-bit offset. bool utc_set = false; -bool utc_locked = false; -uavcan::uint32_t utc_jump_cnt = 0; -UtcSyncParams utc_sync_params; -float utc_prev_adj = 0; -float utc_rel_rate_ppm = 0; -float utc_rel_rate_error_integral = 0; -uavcan::int32_t utc_accumulated_correction_nsec = 0; -uavcan::int32_t utc_correction_nsec_per_overflow = 0; -uavcan::MonotonicTime prev_utc_adj_at; - -uavcan::uint64_t time_mono = 0; -uavcan::uint64_t time_utc = 0; +uavcan::int64_t utc_offset_usec = 0; +uavcan::uint64_t utcNow() +{ + return utc_set ? uavcan::uint64_t(uavcan::int64_t(hrt_absolute_time()) + utc_offset_usec) : 0; } -void init() -{ - CriticalSectionLocker lock; - - if (initialized) { - return; - } - - initialized = true; - - // Attach IRQ - irq_attach(TIMX_IRQn, &TIMX_IRQHandler, NULL); - - // Power-on Clock - modifyreg32(KINETIS_SIM_SCGC6, 0, SIM_SCGC6_PIT); - - // Enable module - putreg32(0, KINETIS_PIT_MCR); - - // Start the timer - - putreg32(CountsPerPeriod - 1, TMR_REG(KINETIS_PIT_LDVAL_OFFSET)); - putreg32(PIT_TCTRL_TEN | PIT_TCTRL_TIE, TMR_REG(KINETIS_PIT_TCTRL_OFFSET)); // Start - - // Prioritize and Enable IRQ - -#if 0 - // This has to be off or uses the default priority - // Without the ability to point the vector - // Directly to this ISR this will reenter the - // exception_common and cause the interrupt - // Stack pointer to be reset - up_prioritize_irq(TIMX_IRQn, NVIC_SYSH_HIGH_PRIORITY); -#endif - up_enable_irq(TIMX_IRQn); -} - -void setUtc(uavcan::UtcTime time) -{ - MutexLocker mlocker(mutex); - UAVCAN_ASSERT(initialized); - - { - CriticalSectionLocker locker; - time_utc = time.toUSec(); - } - - utc_set = true; - utc_locked = false; - utc_jump_cnt++; - utc_prev_adj = 0; - utc_rel_rate_ppm = 0; -} - -static uavcan::uint64_t sampleUtcFromCriticalSection() -{ - UAVCAN_ASSERT(initialized); - UAVCAN_ASSERT(getreg32(TMR_REG(KINETIS_PIT_TCTRL_OFFSET)) & PIT_TCTRL_TIE); - - volatile uavcan::uint64_t time = time_utc; - volatile uavcan::uint32_t cnt = CountsPerPeriod - getreg32(TMR_REG(KINETIS_PIT_CVAL_OFFSET)); - - if (getreg32(TMR_REG(KINETIS_PIT_TFLG_OFFSET)) & PIT_TFLG_TIF) { - cnt = CountsPerPeriod - getreg32(TMR_REG(KINETIS_PIT_CVAL_OFFSET)); - const uavcan::int32_t add = uavcan::int32_t(USecPerOverflow) + - (utc_accumulated_correction_nsec + utc_correction_nsec_per_overflow) / 1000; - time = uavcan::uint64_t(uavcan::int64_t(time) + add); - } - - return time + (cnt / CountsPerUs); -} - -uavcan::uint64_t getUtcUSecFromCanInterrupt() -{ - return utc_set ? sampleUtcFromCriticalSection() : 0; } uavcan::MonotonicTime getMonotonic() { - uavcan::uint64_t usec = 0; - // Scope Critical section - { - CriticalSectionLocker locker; - - volatile uavcan::uint64_t time = time_mono; - volatile uavcan::uint32_t cnt = CountsPerPeriod - getreg32(TMR_REG(KINETIS_PIT_CVAL_OFFSET)); - - if (getreg32(TMR_REG(KINETIS_PIT_TFLG_OFFSET)) & PIT_TFLG_TIF) { - cnt = CountsPerPeriod - getreg32(TMR_REG(KINETIS_PIT_CVAL_OFFSET)); - time += USecPerOverflow; - } - - usec = time + (cnt / CountsPerUs); - - } // End Scope Critical section - - return uavcan::MonotonicTime::fromUSec(usec); + return uavcan::MonotonicTime::fromUSec(hrt_absolute_time()); } - uavcan::UtcTime getUtc() { - if (utc_set) { - uavcan::uint64_t usec = 0; - { - CriticalSectionLocker locker; - usec = sampleUtcFromCriticalSection(); - } - return uavcan::UtcTime::fromUSec(usec); - } - - return uavcan::UtcTime(); + CriticalSectionLocker locker; + return uavcan::UtcTime::fromUSec(utcNow()); } -static float lowpass(float xold, float xnew, float corner, float dt) +uavcan::uint64_t getUtcUSecFromCanInterrupt() { - const float tau = 1.F / corner; - return (dt * xnew + tau * xold) / (dt + tau); + return utcNow(); } -static void updateRatePID(uavcan::UtcDuration adjustment) +void setUtc(uavcan::UtcTime time) { - const uavcan::MonotonicTime ts = getMonotonic(); - const float dt = float((ts - prev_utc_adj_at).toUSec()) / 1e6F; - prev_utc_adj_at = ts; - const float adj_usec = float(adjustment.toUSec()); - - /* - * Target relative rate in PPM - * Positive to go faster - */ - const float target_rel_rate_ppm = adj_usec * utc_sync_params.offset_p; - - /* - * Current relative rate in PPM - * Positive if the local clock is faster - */ - const float new_rel_rate_ppm = (utc_prev_adj - adj_usec) / dt; // rate error in [usec/sec], which is PPM - utc_prev_adj = adj_usec; - utc_rel_rate_ppm = lowpass(utc_rel_rate_ppm, new_rel_rate_ppm, utc_sync_params.rate_error_corner_freq, dt); - - const float rel_rate_error = target_rel_rate_ppm - utc_rel_rate_ppm; - - if (dt > 10) { - utc_rel_rate_error_integral = 0; - - } else { - utc_rel_rate_error_integral += rel_rate_error * dt * utc_sync_params.rate_i; - utc_rel_rate_error_integral = - uavcan::max(utc_rel_rate_error_integral, -utc_sync_params.max_rate_correction_ppm); - utc_rel_rate_error_integral = - uavcan::min(utc_rel_rate_error_integral, utc_sync_params.max_rate_correction_ppm); - } - - /* - * Rate controller - */ - float total_rate_correction_ppm = rel_rate_error + utc_rel_rate_error_integral; - total_rate_correction_ppm = uavcan::max(total_rate_correction_ppm, -utc_sync_params.max_rate_correction_ppm); - total_rate_correction_ppm = uavcan::min(total_rate_correction_ppm, utc_sync_params.max_rate_correction_ppm); - - utc_correction_nsec_per_overflow = uavcan::int32_t((USecPerOverflow * 1000) * (total_rate_correction_ppm / 1e6F)); - -// syslog("$ adj=%f rel_rate=%f rel_rate_eint=%f tgt_rel_rate=%f ppm=%f\n", -// adj_usec, utc_rel_rate_ppm, utc_rel_rate_error_integral, target_rel_rate_ppm, -// total_rate_correction_ppm); + CriticalSectionLocker locker; + utc_offset_usec = uavcan::int64_t(time.toUSec()) - uavcan::int64_t(hrt_absolute_time()); + utc_set = true; } void adjustUtc(uavcan::UtcDuration adjustment) { - MutexLocker mlocker(mutex); - UAVCAN_ASSERT(initialized); + CriticalSectionLocker locker; - if (adjustment.getAbs() > utc_sync_params.min_jump || !utc_set) { - const uavcan::int64_t adj_usec = adjustment.toUSec(); - - { - CriticalSectionLocker locker; - - if ((adj_usec < 0) && uavcan::uint64_t(-adj_usec) > time_utc) { - time_utc = 1; - - } else { - time_utc = uavcan::uint64_t(uavcan::int64_t(time_utc) + adj_usec); - } - } - - utc_set = true; - utc_locked = false; - utc_jump_cnt++; - utc_prev_adj = 0; - utc_rel_rate_ppm = 0; + if (utc_set) { + utc_offset_usec = utc_offset_usec + adjustment.toUSec(); } else { - updateRatePID(adjustment); - - if (!utc_locked) { - utc_locked = - (std::abs(utc_rel_rate_ppm) < utc_sync_params.lock_thres_rate_ppm) && - (std::abs(utc_prev_adj) < float(utc_sync_params.lock_thres_offset.toUSec())); - } + utc_offset_usec = adjustment.toUSec() - uavcan::int64_t(hrt_absolute_time()); + utc_set = true; } } -float getUtcRateCorrectionPPM() -{ - MutexLocker mlocker(mutex); - const float rate_correction_mult = float(utc_correction_nsec_per_overflow) / float(USecPerOverflow * 1000); - return 1e6F * rate_correction_mult; -} - -uavcan::uint32_t getUtcJumpCount() -{ - MutexLocker mlocker(mutex); - return utc_jump_cnt; -} - -bool isUtcLocked() -{ - MutexLocker mlocker(mutex); - return utc_locked; -} - -UtcSyncParams getUtcSyncParams() -{ - MutexLocker mlocker(mutex); - return utc_sync_params; -} - -void setUtcSyncParams(const UtcSyncParams ¶ms) -{ - MutexLocker mlocker(mutex); - // Add some sanity check - utc_sync_params = params; -} - } // namespace clock SystemClock &SystemClock::instance() @@ -314,7 +81,7 @@ SystemClock &SystemClock::instance() if (!clock::initialized) { MutexLocker mlocker(clock::mutex); - clock::init(); + clock::initialized = true; new (ptr)SystemClock(); } @@ -322,44 +89,3 @@ SystemClock &SystemClock::instance() } } // namespace uavcan_kinetis - - -/** - * Timer interrupt handler - */ - -extern "C" -UAVCAN_KINETIS_IRQ_HANDLER(TIMX_IRQHandler) -{ - putreg32(PIT_TFLG_TIF, TMR_REG(KINETIS_PIT_TFLG_OFFSET)); - - using namespace uavcan_kinetis::clock; - UAVCAN_ASSERT(initialized); - - time_mono += USecPerOverflow; - - if (utc_set) { - time_utc += USecPerOverflow; - utc_accumulated_correction_nsec += utc_correction_nsec_per_overflow; - - if (std::abs(utc_accumulated_correction_nsec) >= 1000) { - time_utc = uavcan::uint64_t(uavcan::int64_t(time_utc) + utc_accumulated_correction_nsec / 1000); - utc_accumulated_correction_nsec %= 1000; - } - - // Correction decay - 1 nsec per 65536 usec - if (utc_correction_nsec_per_overflow > 0) { - utc_correction_nsec_per_overflow--; - - } else if (utc_correction_nsec_per_overflow < 0) { - utc_correction_nsec_per_overflow++; - - } else { - ; // Zero - } - } - - return 0; -} - -#endif diff --git a/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/build_config.hpp b/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/build_config.hpp index 56e06ec576e..ffc7b2c53f3 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/build_config.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/build_config.hpp @@ -17,12 +17,3 @@ #if !defined(UAVCAN_STM32_NUM_IFACES) || (UAVCAN_STM32_NUM_IFACES != 1 && UAVCAN_STM32_NUM_IFACES != 2) # error "UAVCAN_STM32_NUM_IFACES must be set to either 1 or 2" #endif - -/** - * Any General-Purpose timer (TIM2, TIM3, TIM4, TIM5) - * e.g. -DUAVCAN_STM32_TIMER_NUMBER=2 - */ -#ifndef UAVCAN_STM32_TIMER_NUMBER -// In this case the clock driver should be implemented by the application -# define UAVCAN_STM32_TIMER_NUMBER 0 -#endif diff --git a/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/clock.hpp b/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/clock.hpp index fcfff84da22..ab0e19e957d 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/clock.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32/driver/include/uavcan_stm32/clock.hpp @@ -10,91 +10,29 @@ namespace uavcan_stm32 { +/* + * Monotonic time is HRT. UTC is HRT plus an offset that time sync moves, so + * the bus time base shares HRT's rate and needs no hardware of its own. + */ namespace clock { -/** - * Starts the clock. - * Can be called multiple times, only the first call will be effective. - */ -void init(); - -/** - * Returns current monotonic time since the moment when clock::init() was called. - * This function is thread safe. - */ uavcan::MonotonicTime getMonotonic(); /** - * Sets the driver's notion of the system UTC. It should be called - * at startup and any time the system clock is updated from an - * external source that is not the UAVCAN Timesync master. - * This function is thread safe. - */ -void setUtc(uavcan::UtcTime time); - -/** - * Returns UTC time if it has been set, otherwise returns zero time. - * This function is thread safe. + * Zero until set by setUtc() or the first adjustUtc(). */ uavcan::UtcTime getUtc(); /** - * Performs UTC phase and frequency adjustment. - * The UTC time will be zero until first adjustment has been performed. - * This function is thread safe. + * UTC reads `time` from now on. + */ +void setUtc(uavcan::UtcTime time); + +/** + * Moves UTC by `adjustment`. While UTC is unset the adjustment is absolute: + * UTC reads `adjustment` from now on. */ void adjustUtc(uavcan::UtcDuration adjustment); - -/** - * UTC clock synchronization parameters - */ -struct UtcSyncParams { - float offset_p; ///< PPM per one usec error - float rate_i; ///< PPM per one PPM error for second - float rate_error_corner_freq; - float max_rate_correction_ppm; - float lock_thres_rate_ppm; - uavcan::UtcDuration lock_thres_offset; - uavcan::UtcDuration min_jump; ///< Min error to jump rather than change rate - - UtcSyncParams() - : offset_p(0.01F) - , rate_i(0.02F) - , rate_error_corner_freq(0.01F) - , max_rate_correction_ppm(300.0F) - , lock_thres_rate_ppm(2.0F) - , lock_thres_offset(uavcan::UtcDuration::fromMSec(4)) - , min_jump(uavcan::UtcDuration::fromMSec(10)) - { } -}; - -/** - * Clock rate error. - * Positive if the hardware timer is slower than reference time. - * This function is thread safe. - */ -float getUtcRateCorrectionPPM(); - -/** - * Number of non-gradual adjustments performed so far. - * Ideally should be zero. - * This function is thread safe. - */ -uavcan::uint32_t getUtcJumpCount(); - -/** - * Whether UTC is synchronized and locked. - * This function is thread safe. - */ -bool isUtcLocked(); - -/** - * UTC sync params get/set. - * Both functions are thread safe. - */ -UtcSyncParams getUtcSyncParams(); -void setUtcSyncParams(const UtcSyncParams ¶ms); - } /** @@ -110,10 +48,6 @@ public: virtual uavcan::MonotonicTime getMonotonic() const { return clock::getMonotonic(); } virtual uavcan::UtcTime getUtc() const { return clock::getUtc(); } - /** - * Calls clock::init() as needed. - * This function is thread safe. - */ static SystemClock &instance(); }; diff --git a/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/internal.hpp b/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/internal.hpp index 1b1c9b10740..04f80c281e2 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/internal.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/internal.hpp @@ -9,7 +9,6 @@ #if UAVCAN_STM32_NUTTX # include # include -# include # include #else # error "Unknown OS" diff --git a/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/uc_stm32_clock.cpp b/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/uc_stm32_clock.cpp index c2e0216ea22..5b49e24ee1a 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/uc_stm32_clock.cpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32/driver/src/uc_stm32_clock.cpp @@ -6,49 +6,7 @@ #include #include "internal.hpp" -#if UAVCAN_STM32_TIMER_NUMBER - -#include -#include - -/* - * Timer instance - */ -# if UAVCAN_STM32_NUTTX -# define TIMX UAVCAN_STM32_GLUE3(STM32_TIM, UAVCAN_STM32_TIMER_NUMBER, _BASE) -# define TMR_REG(o) (TIMX + (o)) -# define TIMX_INPUT_CLOCK UAVCAN_STM32_GLUE3(STM32_APB1_TIM, UAVCAN_STM32_TIMER_NUMBER, _CLKIN) - -# define TIMX_IRQn UAVCAN_STM32_GLUE2(STM32_IRQ_TIM, UAVCAN_STM32_TIMER_NUMBER) -# endif - -# if UAVCAN_STM32_TIMER_NUMBER >= 2 && UAVCAN_STM32_TIMER_NUMBER <= 7 -# define TIMX_RCC_ENR RCC->APB1ENR -# define TIMX_RCC_RSTR RCC->APB1RSTR -# define TIMX_RCC_ENR_MASK UAVCAN_STM32_GLUE3(RCC_APB1ENR_TIM, UAVCAN_STM32_TIMER_NUMBER, EN) -# define TIMX_RCC_RSTR_MASK UAVCAN_STM32_GLUE3(RCC_APB1RSTR_TIM, UAVCAN_STM32_TIMER_NUMBER, RST) -# else -# error "This UAVCAN_STM32_TIMER_NUMBER is not supported yet" -# endif - -/** - * UAVCAN_STM32_TIMX_INPUT_CLOCK can be used to manually override the auto-detected timer clock speed. - * This is useful at least with certain versions of ChibiOS which do not support the bit - * RCC_DKCFGR.TIMPRE that is available in newer models of STM32. In that case, if TIMPRE is active, - * the auto-detected value of TIMX_INPUT_CLOCK will be twice lower than the actual clock speed. - * Read this for additional context: http://www.chibios.com/forum/viewtopic.php?f=35&t=3870 - * A normal way to use the override feature is to provide an alternative macro, e.g.: - * - * -DUAVCAN_STM32_TIMX_INPUT_CLOCK=STM32_HCLK - * - * Alternatively, the new clock rate can be specified directly. - */ -# ifdef UAVCAN_STM32_TIMX_INPUT_CLOCK -# undef TIMX_INPUT_CLOCK -# define TIMX_INPUT_CLOCK UAVCAN_STM32_TIMX_INPUT_CLOCK -# endif - -extern "C" UAVCAN_STM32_IRQ_HANDLER(TIMX_IRQHandler); +#include namespace uavcan_stm32 { @@ -57,280 +15,57 @@ namespace clock namespace { -const uavcan::uint32_t USecPerOverflow = 65536; - Mutex mutex; - bool initialized = false; +// Written under a critical section so the CAN interrupt, which reads them +// without locking, never sees a torn 64-bit offset. bool utc_set = false; -bool utc_locked = false; -uavcan::uint32_t utc_jump_cnt = 0; -UtcSyncParams utc_sync_params; -float utc_prev_adj = 0; -float utc_rel_rate_ppm = 0; -float utc_rel_rate_error_integral = 0; -uavcan::int32_t utc_accumulated_correction_nsec = 0; -uavcan::int32_t utc_correction_nsec_per_overflow = 0; -uavcan::MonotonicTime prev_utc_adj_at; - -uavcan::uint64_t time_mono = 0; -uavcan::uint64_t time_utc = 0; +uavcan::int64_t utc_offset_usec = 0; +uavcan::uint64_t utcNow() +{ + return utc_set ? uavcan::uint64_t(uavcan::int64_t(hrt_absolute_time()) + utc_offset_usec) : 0; } - -void init() -{ - CriticalSectionLocker lock; - - if (initialized) { - return; - } - - initialized = true; - - -# if UAVCAN_STM32_NUTTX - - // Attach IRQ - irq_attach(TIMX_IRQn, &TIMX_IRQHandler, NULL); - - // Power-on and reset - modifyreg32(STM32_RCC_APB1ENR, 0, TIMX_RCC_ENR_MASK); - modifyreg32(STM32_RCC_APB1RSTR, 0, TIMX_RCC_RSTR_MASK); - modifyreg32(STM32_RCC_APB1RSTR, TIMX_RCC_RSTR_MASK, 0); - - - // Start the timer - putreg32(0xFFFF, TMR_REG(STM32_BTIM_ARR_OFFSET)); - putreg16(((TIMX_INPUT_CLOCK / 1000000) - 1), TMR_REG(STM32_BTIM_PSC_OFFSET)); - putreg16(BTIM_CR1_URS, TMR_REG(STM32_BTIM_CR1_OFFSET)); - putreg16(0, TMR_REG(STM32_BTIM_SR_OFFSET)); - putreg16(BTIM_EGR_UG, TMR_REG(STM32_BTIM_EGR_OFFSET)); // Reload immediately - putreg16(BTIM_DIER_UIE, TMR_REG(STM32_BTIM_DIER_OFFSET)); - putreg16(BTIM_CR1_CEN, TMR_REG(STM32_BTIM_CR1_OFFSET)); // Start - - // Prioritize and Enable IRQ -// todo: Currently changing the NVIC_SYSH_HIGH_PRIORITY is HARD faulting -// need to investigate -// up_prioritize_irq(TIMX_IRQn, NVIC_SYSH_HIGH_PRIORITY); - up_enable_irq(TIMX_IRQn); - -# endif -} - -void setUtc(uavcan::UtcTime time) -{ - MutexLocker mlocker(mutex); - UAVCAN_ASSERT(initialized); - - { - CriticalSectionLocker locker; - time_utc = time.toUSec(); - } - - utc_set = true; - utc_locked = false; - utc_jump_cnt++; - utc_prev_adj = 0; - utc_rel_rate_ppm = 0; -} - -static uavcan::uint64_t sampleUtcFromCriticalSection() -{ -# if UAVCAN_STM32_NUTTX - - UAVCAN_ASSERT(initialized); - UAVCAN_ASSERT(getreg16(TMR_REG(STM32_BTIM_DIER_OFFSET)) & BTIM_DIER_UIE); - - volatile uavcan::uint64_t time = time_utc; - volatile uavcan::uint32_t cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); - - if (getreg16(TMR_REG(STM32_BTIM_SR_OFFSET)) & BTIM_SR_UIF) { - cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); - const uavcan::int32_t add = uavcan::int32_t(USecPerOverflow) + - (utc_accumulated_correction_nsec + utc_correction_nsec_per_overflow) / 1000; - time = uavcan::uint64_t(uavcan::int64_t(time) + add); - } - - return time + cnt; -# endif -} - -uavcan::uint64_t getUtcUSecFromCanInterrupt() -{ - return utc_set ? sampleUtcFromCriticalSection() : 0; } uavcan::MonotonicTime getMonotonic() { - uavcan::uint64_t usec = 0; - // Scope Critical section - { - CriticalSectionLocker locker; - - volatile uavcan::uint64_t time = time_mono; - -# if UAVCAN_STM32_NUTTX - - volatile uavcan::uint32_t cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); - - if (getreg16(TMR_REG(STM32_BTIM_SR_OFFSET)) & BTIM_SR_UIF) { - cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); -# endif - time += USecPerOverflow; - } - - usec = time + cnt; - -# ifndef NDEBUG - static uavcan::uint64_t prev_usec = 0; // Self-test - UAVCAN_ASSERT(prev_usec <= usec); - (void)prev_usec; - prev_usec = usec; -# endif - } // End Scope Critical section - - return uavcan::MonotonicTime::fromUSec(usec); + return uavcan::MonotonicTime::fromUSec(hrt_absolute_time()); } uavcan::UtcTime getUtc() { - if (utc_set) { - uavcan::uint64_t usec = 0; - { - CriticalSectionLocker locker; - usec = sampleUtcFromCriticalSection(); - } - return uavcan::UtcTime::fromUSec(usec); - } - - return uavcan::UtcTime(); + CriticalSectionLocker locker; + return uavcan::UtcTime::fromUSec(utcNow()); } -static float lowpass(float xold, float xnew, float corner, float dt) +uavcan::uint64_t getUtcUSecFromCanInterrupt() { - const float tau = 1.F / corner; - return (dt * xnew + tau * xold) / (dt + tau); + return utcNow(); } -static void updateRatePID(uavcan::UtcDuration adjustment) +void setUtc(uavcan::UtcTime time) { - const uavcan::MonotonicTime ts = getMonotonic(); - const float dt = float((ts - prev_utc_adj_at).toUSec()) / 1e6F; - prev_utc_adj_at = ts; - const float adj_usec = float(adjustment.toUSec()); - - /* - * Target relative rate in PPM - * Positive to go faster - */ - const float target_rel_rate_ppm = adj_usec * utc_sync_params.offset_p; - - /* - * Current relative rate in PPM - * Positive if the local clock is faster - */ - const float new_rel_rate_ppm = (utc_prev_adj - adj_usec) / dt; // rate error in [usec/sec], which is PPM - utc_prev_adj = adj_usec; - utc_rel_rate_ppm = lowpass(utc_rel_rate_ppm, new_rel_rate_ppm, utc_sync_params.rate_error_corner_freq, dt); - - const float rel_rate_error = target_rel_rate_ppm - utc_rel_rate_ppm; - - if (dt > 10) { - utc_rel_rate_error_integral = 0; - - } else { - utc_rel_rate_error_integral += rel_rate_error * dt * utc_sync_params.rate_i; - utc_rel_rate_error_integral = - uavcan::max(utc_rel_rate_error_integral, -utc_sync_params.max_rate_correction_ppm); - utc_rel_rate_error_integral = - uavcan::min(utc_rel_rate_error_integral, utc_sync_params.max_rate_correction_ppm); - } - - /* - * Rate controller - */ - float total_rate_correction_ppm = rel_rate_error + utc_rel_rate_error_integral; - total_rate_correction_ppm = uavcan::max(total_rate_correction_ppm, -utc_sync_params.max_rate_correction_ppm); - total_rate_correction_ppm = uavcan::min(total_rate_correction_ppm, utc_sync_params.max_rate_correction_ppm); - - utc_correction_nsec_per_overflow = uavcan::int32_t((USecPerOverflow * 1000) * (total_rate_correction_ppm / 1e6F)); - -// syslog("$ adj=%f rel_rate=%f rel_rate_eint=%f tgt_rel_rate=%f ppm=%f\n", -// adj_usec, utc_rel_rate_ppm, utc_rel_rate_error_integral, target_rel_rate_ppm, -// total_rate_correction_ppm); + CriticalSectionLocker locker; + utc_offset_usec = uavcan::int64_t(time.toUSec()) - uavcan::int64_t(hrt_absolute_time()); + utc_set = true; } void adjustUtc(uavcan::UtcDuration adjustment) { - MutexLocker mlocker(mutex); - UAVCAN_ASSERT(initialized); + CriticalSectionLocker locker; - if (adjustment.getAbs() > utc_sync_params.min_jump || !utc_set) { - const uavcan::int64_t adj_usec = adjustment.toUSec(); - - { - CriticalSectionLocker locker; - - if ((adj_usec < 0) && uavcan::uint64_t(-adj_usec) > time_utc) { - time_utc = 1; - - } else { - time_utc = uavcan::uint64_t(uavcan::int64_t(time_utc) + adj_usec); - } - } - - utc_set = true; - utc_locked = false; - utc_jump_cnt++; - utc_prev_adj = 0; - utc_rel_rate_ppm = 0; + if (utc_set) { + utc_offset_usec = utc_offset_usec + adjustment.toUSec(); } else { - updateRatePID(adjustment); - - if (!utc_locked) { - utc_locked = - (std::abs(utc_rel_rate_ppm) < utc_sync_params.lock_thres_rate_ppm) && - (std::abs(utc_prev_adj) < float(utc_sync_params.lock_thres_offset.toUSec())); - } + utc_offset_usec = adjustment.toUSec() - uavcan::int64_t(hrt_absolute_time()); + utc_set = true; } } -float getUtcRateCorrectionPPM() -{ - MutexLocker mlocker(mutex); - const float rate_correction_mult = float(utc_correction_nsec_per_overflow) / float(USecPerOverflow * 1000); - return 1e6F * rate_correction_mult; -} - -uavcan::uint32_t getUtcJumpCount() -{ - MutexLocker mlocker(mutex); - return utc_jump_cnt; -} - -bool isUtcLocked() -{ - MutexLocker mlocker(mutex); - return utc_locked; -} - -UtcSyncParams getUtcSyncParams() -{ - MutexLocker mlocker(mutex); - return utc_sync_params; -} - -void setUtcSyncParams(const UtcSyncParams ¶ms) -{ - MutexLocker mlocker(mutex); - // Add some sanity check - utc_sync_params = params; -} - } // namespace clock SystemClock &SystemClock::instance() @@ -345,7 +80,7 @@ SystemClock &SystemClock::instance() if (!clock::initialized) { MutexLocker mlocker(clock::mutex); - clock::init(); + clock::initialized = true; new (ptr)SystemClock(); } @@ -353,48 +88,3 @@ SystemClock &SystemClock::instance() } } // namespace uavcan_stm32 - - -/** - * Timer interrupt handler - */ - -extern "C" -UAVCAN_STM32_IRQ_HANDLER(TIMX_IRQHandler) -{ - UAVCAN_STM32_IRQ_PROLOGUE(); - -# if UAVCAN_STM32_NUTTX - putreg16(0, TMR_REG(STM32_BTIM_SR_OFFSET)); -# endif - - using namespace uavcan_stm32::clock; - UAVCAN_ASSERT(initialized); - - time_mono += USecPerOverflow; - - if (utc_set) { - time_utc += USecPerOverflow; - utc_accumulated_correction_nsec += utc_correction_nsec_per_overflow; - - if (std::abs(utc_accumulated_correction_nsec) >= 1000) { - time_utc = uavcan::uint64_t(uavcan::int64_t(time_utc) + utc_accumulated_correction_nsec / 1000); - utc_accumulated_correction_nsec %= 1000; - } - - // Correction decay - 1 nsec per 65536 usec - if (utc_correction_nsec_per_overflow > 0) { - utc_correction_nsec_per_overflow--; - - } else if (utc_correction_nsec_per_overflow < 0) { - utc_correction_nsec_per_overflow++; - - } else { - ; // Zero - } - } - - UAVCAN_STM32_IRQ_EPILOGUE(); -} - -#endif diff --git a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/build_config.hpp b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/build_config.hpp index 15c1f927c87..275be429e8f 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/build_config.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/build_config.hpp @@ -17,12 +17,3 @@ #if !defined(UAVCAN_STM32H7_NUM_IFACES) || (UAVCAN_STM32H7_NUM_IFACES != 1 && UAVCAN_STM32H7_NUM_IFACES != 2) # error "UAVCAN_STM32H7_NUM_IFACES must be set to either 1 or 2" #endif - -/** - * Any General-Purpose timer (TIM2, TIM3, TIM4, TIM5, TIM6, TIM7) - * e.g. -DUAVCAN_STM32H7_TIMER_NUMBER=2 - */ -#ifndef UAVCAN_STM32H7_TIMER_NUMBER -// In this case the clock driver should be implemented by the application -# define UAVCAN_STM32H7_TIMER_NUMBER 0 -#endif diff --git a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/clock.hpp b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/clock.hpp index 98d1c2988e7..e7589d3c7fe 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/clock.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/include/uavcan_stm32h7/clock.hpp @@ -10,91 +10,29 @@ namespace uavcan_stm32h7 { +/* + * Monotonic time is HRT. UTC is HRT plus an offset that time sync moves, so + * the bus time base shares HRT's rate and needs no hardware of its own. + */ namespace clock { -/** - * Starts the clock. - * Can be called multiple times, only the first call will be effective. - */ -void init(); - -/** - * Returns current monotonic time since the moment when clock::init() was called. - * This function is thread safe. - */ uavcan::MonotonicTime getMonotonic(); /** - * Sets the driver's notion of the system UTC. It should be called - * at startup and any time the system clock is updated from an - * external source that is not the UAVCAN Timesync master. - * This function is thread safe. - */ -void setUtc(uavcan::UtcTime time); - -/** - * Returns UTC time if it has been set, otherwise returns zero time. - * This function is thread safe. + * Zero until set by setUtc() or the first adjustUtc(). */ uavcan::UtcTime getUtc(); /** - * Performs UTC phase and frequency adjustment. - * The UTC time will be zero until first adjustment has been performed. - * This function is thread safe. + * UTC reads `time` from now on. + */ +void setUtc(uavcan::UtcTime time); + +/** + * Moves UTC by `adjustment`. While UTC is unset the adjustment is absolute: + * UTC reads `adjustment` from now on. */ void adjustUtc(uavcan::UtcDuration adjustment); - -/** - * UTC clock synchronization parameters - */ -struct UtcSyncParams { - float offset_p; ///< PPM per one usec error - float rate_i; ///< PPM per one PPM error for second - float rate_error_corner_freq; - float max_rate_correction_ppm; - float lock_thres_rate_ppm; - uavcan::UtcDuration lock_thres_offset; - uavcan::UtcDuration min_jump; ///< Min error to jump rather than change rate - - UtcSyncParams() - : offset_p(0.01F) - , rate_i(0.02F) - , rate_error_corner_freq(0.01F) - , max_rate_correction_ppm(300.0F) - , lock_thres_rate_ppm(2.0F) - , lock_thres_offset(uavcan::UtcDuration::fromMSec(4)) - , min_jump(uavcan::UtcDuration::fromMSec(10)) - { } -}; - -/** - * Clock rate error. - * Positive if the hardware timer is slower than reference time. - * This function is thread safe. - */ -float getUtcRateCorrectionPPM(); - -/** - * Number of non-gradual adjustments performed so far. - * Ideally should be zero. - * This function is thread safe. - */ -uavcan::uint32_t getUtcJumpCount(); - -/** - * Whether UTC is synchronized and locked. - * This function is thread safe. - */ -bool isUtcLocked(); - -/** - * UTC sync params get/set. - * Both functions are thread safe. - */ -UtcSyncParams getUtcSyncParams(); -void setUtcSyncParams(const UtcSyncParams ¶ms); - } /** @@ -110,10 +48,6 @@ public: virtual uavcan::MonotonicTime getMonotonic() const { return clock::getMonotonic(); } virtual uavcan::UtcTime getUtc() const { return clock::getUtc(); } - /** - * Calls clock::init() as needed. - * This function is thread safe. - */ static SystemClock &instance(); }; diff --git a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/internal.hpp b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/internal.hpp index 1c17066e3f8..e960259de06 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/internal.hpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/internal.hpp @@ -10,7 +10,6 @@ #if UAVCAN_STM32H7_NUTTX # include # include -# include # include #else # error "Unknown OS" diff --git a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/uc_stm32h7_clock.cpp b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/uc_stm32h7_clock.cpp index cd4a62dc5d1..12a1456db25 100644 --- a/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/uc_stm32h7_clock.cpp +++ b/src/drivers/uavcan/uavcan_drivers/stm32h7/driver/src/uc_stm32h7_clock.cpp @@ -6,49 +6,7 @@ #include #include "internal.hpp" -#if UAVCAN_STM32H7_TIMER_NUMBER - -#include -#include - -/* - * Timer instance - */ -# if UAVCAN_STM32H7_NUTTX -# define TIMX UAVCAN_STM32H7_GLUE3(STM32_TIM, UAVCAN_STM32H7_TIMER_NUMBER, _BASE) -# define TMR_REG(o) (TIMX + (o)) -# define TIMX_INPUT_CLOCK UAVCAN_STM32H7_GLUE3(STM32_APB1_TIM, UAVCAN_STM32H7_TIMER_NUMBER, _CLKIN) - -# define TIMX_IRQn UAVCAN_STM32H7_GLUE2(STM32_IRQ_TIM, UAVCAN_STM32H7_TIMER_NUMBER) -# endif - -# if UAVCAN_STM32H7_TIMER_NUMBER >= 2 && UAVCAN_STM32H7_TIMER_NUMBER <= 7 -# define TIMX_RCC_ENR RCC->APB1ENR -# define TIMX_RCC_RSTR RCC->APB1RSTR -# define TIMX_RCC_ENR_MASK UAVCAN_STM32H7_GLUE3(RCC_APB1LENR_TIM, UAVCAN_STM32H7_TIMER_NUMBER, EN) -# define TIMX_RCC_RSTR_MASK UAVCAN_STM32H7_GLUE3(RCC_APB1LRSTR_TIM, UAVCAN_STM32H7_TIMER_NUMBER, RST) -# else -# error "This UAVCAN_STM32H7_TIMER_NUMBER is not supported yet" -# endif - -/** - * UAVCAN_STM32H7_TIMX_INPUT_CLOCK can be used to manually override the auto-detected timer clock speed. - * This is useful at least with certain versions of ChibiOS which do not support the bit - * RCC_DKCFGR.TIMPRE that is available in newer models of STM32. In that case, if TIMPRE is active, - * the auto-detected value of TIMX_INPUT_CLOCK will be twice lower than the actual clock speed. - * Read this for additional context: http://www.chibios.com/forum/viewtopic.php?f=35&t=3870 - * A normal way to use the override feature is to provide an alternative macro, e.g.: - * - * -DUAVCAN_STM32H7_TIMX_INPUT_CLOCK=STM32_HCLK - * - * Alternatively, the new clock rate can be specified directly. - */ -# ifdef UAVCAN_STM32H7_TIMX_INPUT_CLOCK -# undef TIMX_INPUT_CLOCK -# define TIMX_INPUT_CLOCK UAVCAN_STM32H7_TIMX_INPUT_CLOCK -# endif - -extern "C" UAVCAN_STM32H7_IRQ_HANDLER(TIMX_IRQHandler); +#include namespace uavcan_stm32h7 { @@ -57,280 +15,57 @@ namespace clock namespace { -const uavcan::uint32_t USecPerOverflow = 65536; - Mutex mutex; - bool initialized = false; +// Written under a critical section so the CAN interrupt, which reads them +// without locking, never sees a torn 64-bit offset. bool utc_set = false; -bool utc_locked = false; -uavcan::uint32_t utc_jump_cnt = 0; -UtcSyncParams utc_sync_params; -float utc_prev_adj = 0; -float utc_rel_rate_ppm = 0; -float utc_rel_rate_error_integral = 0; -uavcan::int32_t utc_accumulated_correction_nsec = 0; -uavcan::int32_t utc_correction_nsec_per_overflow = 0; -uavcan::MonotonicTime prev_utc_adj_at; - -uavcan::uint64_t time_mono = 0; -uavcan::uint64_t time_utc = 0; +uavcan::int64_t utc_offset_usec = 0; +uavcan::uint64_t utcNow() +{ + return utc_set ? uavcan::uint64_t(uavcan::int64_t(hrt_absolute_time()) + utc_offset_usec) : 0; } - -void init() -{ - CriticalSectionLocker lock; - - if (initialized) { - return; - } - - initialized = true; - - -# if UAVCAN_STM32H7_NUTTX - - // Attach IRQ - irq_attach(TIMX_IRQn, &TIMX_IRQHandler, NULL); - - // Power-on and reset - modifyreg32(STM32_RCC_APB1ENR, 0, TIMX_RCC_ENR_MASK); - modifyreg32(STM32_RCC_APB1RSTR, 0, TIMX_RCC_RSTR_MASK); - modifyreg32(STM32_RCC_APB1RSTR, TIMX_RCC_RSTR_MASK, 0); - - - // Start the timer - putreg32(0xFFFF, TMR_REG(STM32_BTIM_ARR_OFFSET)); - putreg16(((TIMX_INPUT_CLOCK / 1000000) - 1), TMR_REG(STM32_BTIM_PSC_OFFSET)); - putreg16(BTIM_CR1_URS, TMR_REG(STM32_BTIM_CR1_OFFSET)); - putreg16(0, TMR_REG(STM32_BTIM_SR_OFFSET)); - putreg16(BTIM_EGR_UG, TMR_REG(STM32_BTIM_EGR_OFFSET)); // Reload immediately - putreg16(BTIM_DIER_UIE, TMR_REG(STM32_BTIM_DIER_OFFSET)); - putreg16(BTIM_CR1_CEN, TMR_REG(STM32_BTIM_CR1_OFFSET)); // Start - - // Prioritize and Enable IRQ -// todo: Currently changing the NVIC_SYSH_HIGH_PRIORITY is HARD faulting -// need to investigate -// up_prioritize_irq(TIMX_IRQn, NVIC_SYSH_HIGH_PRIORITY); - up_enable_irq(TIMX_IRQn); - -# endif -} - -void setUtc(uavcan::UtcTime time) -{ - MutexLocker mlocker(mutex); - UAVCAN_ASSERT(initialized); - - { - CriticalSectionLocker locker; - time_utc = time.toUSec(); - } - - utc_set = true; - utc_locked = false; - utc_jump_cnt++; - utc_prev_adj = 0; - utc_rel_rate_ppm = 0; -} - -static uavcan::uint64_t sampleUtcFromCriticalSection() -{ -# if UAVCAN_STM32H7_NUTTX - - UAVCAN_ASSERT(initialized); - UAVCAN_ASSERT(getreg16(TMR_REG(STM32_BTIM_DIER_OFFSET)) & BTIM_DIER_UIE); - - volatile uavcan::uint64_t time = time_utc; - volatile uavcan::uint32_t cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); - - if (getreg16(TMR_REG(STM32_BTIM_SR_OFFSET)) & BTIM_SR_UIF) { - cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); - const uavcan::int32_t add = uavcan::int32_t(USecPerOverflow) + - (utc_accumulated_correction_nsec + utc_correction_nsec_per_overflow) / 1000; - time = uavcan::uint64_t(uavcan::int64_t(time) + add); - } - - return time + cnt; -# endif -} - -uavcan::uint64_t getUtcUSecFromCanInterrupt() -{ - return utc_set ? sampleUtcFromCriticalSection() : 0; } uavcan::MonotonicTime getMonotonic() { - uavcan::uint64_t usec = 0; - // Scope Critical section - { - CriticalSectionLocker locker; - - volatile uavcan::uint64_t time = time_mono; - -# if UAVCAN_STM32H7_NUTTX - - volatile uavcan::uint32_t cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); - - if (getreg16(TMR_REG(STM32_BTIM_SR_OFFSET)) & BTIM_SR_UIF) { - cnt = getreg16(TMR_REG(STM32_BTIM_CNT_OFFSET)); -# endif - time += USecPerOverflow; - } - - usec = time + cnt; - -# ifndef NDEBUG - static uavcan::uint64_t prev_usec = 0; // Self-test - UAVCAN_ASSERT(prev_usec <= usec); - (void)prev_usec; - prev_usec = usec; -# endif - } // End Scope Critical section - - return uavcan::MonotonicTime::fromUSec(usec); + return uavcan::MonotonicTime::fromUSec(hrt_absolute_time()); } uavcan::UtcTime getUtc() { - if (utc_set) { - uavcan::uint64_t usec = 0; - { - CriticalSectionLocker locker; - usec = sampleUtcFromCriticalSection(); - } - return uavcan::UtcTime::fromUSec(usec); - } - - return uavcan::UtcTime(); + CriticalSectionLocker locker; + return uavcan::UtcTime::fromUSec(utcNow()); } -static float lowpass(float xold, float xnew, float corner, float dt) +uavcan::uint64_t getUtcUSecFromCanInterrupt() { - const float tau = 1.F / corner; - return (dt * xnew + tau * xold) / (dt + tau); + return utcNow(); } -static void updateRatePID(uavcan::UtcDuration adjustment) +void setUtc(uavcan::UtcTime time) { - const uavcan::MonotonicTime ts = getMonotonic(); - const float dt = float((ts - prev_utc_adj_at).toUSec()) / 1e6F; - prev_utc_adj_at = ts; - const float adj_usec = float(adjustment.toUSec()); - - /* - * Target relative rate in PPM - * Positive to go faster - */ - const float target_rel_rate_ppm = adj_usec * utc_sync_params.offset_p; - - /* - * Current relative rate in PPM - * Positive if the local clock is faster - */ - const float new_rel_rate_ppm = (utc_prev_adj - adj_usec) / dt; // rate error in [usec/sec], which is PPM - utc_prev_adj = adj_usec; - utc_rel_rate_ppm = lowpass(utc_rel_rate_ppm, new_rel_rate_ppm, utc_sync_params.rate_error_corner_freq, dt); - - const float rel_rate_error = target_rel_rate_ppm - utc_rel_rate_ppm; - - if (dt > 10) { - utc_rel_rate_error_integral = 0; - - } else { - utc_rel_rate_error_integral += rel_rate_error * dt * utc_sync_params.rate_i; - utc_rel_rate_error_integral = - uavcan::max(utc_rel_rate_error_integral, -utc_sync_params.max_rate_correction_ppm); - utc_rel_rate_error_integral = - uavcan::min(utc_rel_rate_error_integral, utc_sync_params.max_rate_correction_ppm); - } - - /* - * Rate controller - */ - float total_rate_correction_ppm = rel_rate_error + utc_rel_rate_error_integral; - total_rate_correction_ppm = uavcan::max(total_rate_correction_ppm, -utc_sync_params.max_rate_correction_ppm); - total_rate_correction_ppm = uavcan::min(total_rate_correction_ppm, utc_sync_params.max_rate_correction_ppm); - - utc_correction_nsec_per_overflow = uavcan::int32_t((USecPerOverflow * 1000) * (total_rate_correction_ppm / 1e6F)); - -// syslog("$ adj=%f rel_rate=%f rel_rate_eint=%f tgt_rel_rate=%f ppm=%f\n", -// adj_usec, utc_rel_rate_ppm, utc_rel_rate_error_integral, target_rel_rate_ppm, -// total_rate_correction_ppm); + CriticalSectionLocker locker; + utc_offset_usec = uavcan::int64_t(time.toUSec()) - uavcan::int64_t(hrt_absolute_time()); + utc_set = true; } void adjustUtc(uavcan::UtcDuration adjustment) { - MutexLocker mlocker(mutex); - UAVCAN_ASSERT(initialized); + CriticalSectionLocker locker; - if (adjustment.getAbs() > utc_sync_params.min_jump || !utc_set) { - const uavcan::int64_t adj_usec = adjustment.toUSec(); - - { - CriticalSectionLocker locker; - - if ((adj_usec < 0) && uavcan::uint64_t(-adj_usec) > time_utc) { - time_utc = 1; - - } else { - time_utc = uavcan::uint64_t(uavcan::int64_t(time_utc) + adj_usec); - } - } - - utc_set = true; - utc_locked = false; - utc_jump_cnt++; - utc_prev_adj = 0; - utc_rel_rate_ppm = 0; + if (utc_set) { + utc_offset_usec = utc_offset_usec + adjustment.toUSec(); } else { - updateRatePID(adjustment); - - if (!utc_locked) { - utc_locked = - (std::abs(utc_rel_rate_ppm) < utc_sync_params.lock_thres_rate_ppm) && - (std::abs(utc_prev_adj) < float(utc_sync_params.lock_thres_offset.toUSec())); - } + utc_offset_usec = adjustment.toUSec() - uavcan::int64_t(hrt_absolute_time()); + utc_set = true; } } -float getUtcRateCorrectionPPM() -{ - MutexLocker mlocker(mutex); - const float rate_correction_mult = float(utc_correction_nsec_per_overflow) / float(USecPerOverflow * 1000); - return 1e6F * rate_correction_mult; -} - -uavcan::uint32_t getUtcJumpCount() -{ - MutexLocker mlocker(mutex); - return utc_jump_cnt; -} - -bool isUtcLocked() -{ - MutexLocker mlocker(mutex); - return utc_locked; -} - -UtcSyncParams getUtcSyncParams() -{ - MutexLocker mlocker(mutex); - return utc_sync_params; -} - -void setUtcSyncParams(const UtcSyncParams ¶ms) -{ - MutexLocker mlocker(mutex); - // Add some sanity check - utc_sync_params = params; -} - } // namespace clock SystemClock &SystemClock::instance() @@ -345,7 +80,7 @@ SystemClock &SystemClock::instance() if (!clock::initialized) { MutexLocker mlocker(clock::mutex); - clock::init(); + clock::initialized = true; new (ptr)SystemClock(); } @@ -353,48 +88,3 @@ SystemClock &SystemClock::instance() } } // namespace uavcan_stm32h7 - - -/** - * Timer interrupt handler - */ - -extern "C" -UAVCAN_STM32H7_IRQ_HANDLER(TIMX_IRQHandler) -{ - UAVCAN_STM32H7_IRQ_PROLOGUE(); - -# if UAVCAN_STM32H7_NUTTX - putreg16(0, TMR_REG(STM32_BTIM_SR_OFFSET)); -# endif - - using namespace uavcan_stm32h7::clock; - UAVCAN_ASSERT(initialized); - - time_mono += USecPerOverflow; - - if (utc_set) { - time_utc += USecPerOverflow; - utc_accumulated_correction_nsec += utc_correction_nsec_per_overflow; - - if (std::abs(utc_accumulated_correction_nsec) >= 1000) { - time_utc = uavcan::uint64_t(uavcan::int64_t(time_utc) + utc_accumulated_correction_nsec / 1000); - utc_accumulated_correction_nsec %= 1000; - } - - // Correction decay - 1 nsec per 65536 usec - if (utc_correction_nsec_per_overflow > 0) { - utc_correction_nsec_per_overflow--; - - } else if (utc_correction_nsec_per_overflow < 0) { - utc_correction_nsec_per_overflow++; - - } else { - ; // Zero - } - } - - UAVCAN_STM32H7_IRQ_EPILOGUE(); -} - -#endif diff --git a/src/drivers/uavcannode/CMakeLists.txt b/src/drivers/uavcannode/CMakeLists.txt index 2027237ab85..d2e7edf463c 100644 --- a/src/drivers/uavcannode/CMakeLists.txt +++ b/src/drivers/uavcannode/CMakeLists.txt @@ -45,22 +45,12 @@ set(UAVCAN_PLATFORM "generic") if(CONFIG_ARCH_CHIP) if(${CONFIG_NET_CAN} MATCHES "y") set(UAVCAN_DRIVER "socketcan") - set(UAVCAN_TIMER 1) elseif(${CONFIG_ARCH_CHIP} MATCHES "kinetis") set(UAVCAN_DRIVER "kinetis") - set(UAVCAN_TIMER 1) elseif(${CONFIG_ARCH_CHIP} MATCHES "stm32h7") set(UAVCAN_DRIVER "stm32h7") - set(UAVCAN_TIMER 5) # The default timer is TIM5 - if (DEFINED config_uavcan_timer_override) - set (UAVCAN_TIMER ${config_uavcan_timer_override}) - endif() elseif(${CONFIG_ARCH_CHIP} MATCHES "stm32") set(UAVCAN_DRIVER "stm32") - set(UAVCAN_TIMER 5) # The default timer is TIM5 - if (DEFINED config_uavcan_timer_override) - set (UAVCAN_TIMER ${config_uavcan_timer_override}) - endif() endif() endif() @@ -77,7 +67,6 @@ string(TOUPPER "${UAVCAN_DRIVER}" UAVCAN_DRIVER_UPPER) add_definitions( -DUAVCAN_${UAVCAN_DRIVER_UPPER}_${OS_UPPER}=1 -DUAVCAN_${UAVCAN_DRIVER_UPPER}_NUM_IFACES=${config_uavcan_num_ifaces} - -DUAVCAN_${UAVCAN_DRIVER_UPPER}_TIMER_NUMBER=${UAVCAN_TIMER} -DUAVCAN_CPP_VERSION=UAVCAN_CPP03 -DUAVCAN_DRIVER=uavcan_${UAVCAN_DRIVER} -DUAVCAN_IMPLEMENT_PLACEMENT_NEW=1