From ca5f23c353d2e05ad47a2f11063bf4fe82e90041 Mon Sep 17 00:00:00 2001 From: Christoph Schweppe Date: Thu, 27 Oct 2022 10:38:19 +0200 Subject: [PATCH 1/2] Add timing helpers --- nanoFramework.Device.Can/CanTiming.cs | 83 +++++++++++++++++++ .../nanoFramework.Device.Can.nfproj | 1 + 2 files changed, 84 insertions(+) create mode 100644 nanoFramework.Device.Can/CanTiming.cs diff --git a/nanoFramework.Device.Can/CanTiming.cs b/nanoFramework.Device.Can/CanTiming.cs new file mode 100644 index 0000000..59b3f48 --- /dev/null +++ b/nanoFramework.Device.Can/CanTiming.cs @@ -0,0 +1,83 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +namespace nanoFramework.Device.Can +{ + /// + /// Helper for common CAN timings for the STM32 + /// + public static class CanTimingSTM32 + { + /// + /// 25 KBaud. + /// + public static CanSettings Timing500KBaud () { + return new CanSettings(6, 8, 1, 0); + } + + } + + /// + /// Helper for common CAN timings for the ESP32. Values from https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/twai_types.h#L54 + /// + public static class CanTimingESP32 + { + /// + /// 25 KBaud. + /// + public static CanSettings Timing25KBaud () { + return new CanSettings(128, 16, 8, 3); + } + + /// + /// 50 KBaud. + /// + public static CanSettings Timing50KBaud () { + return new CanSettings(80, 15, 4, 3); + } + + /// + /// 100 KBaud. + /// + public static CanSettings Timing100KBaud () { + return new CanSettings(40, 15, 4, 3); + } + + /// + /// 125 KBaud. + /// + public static CanSettings Timing125KBaud () { + return new CanSettings(32, 15, 4, 3); + } + + /// + /// 250 KBaud. + /// + public static CanSettings Timing250KBaud () { + return new CanSettings(16, 15, 4, 3); + } + + /// + /// 500 KBaud. + /// + public static CanSettings Timing500KBaud () { + return new CanSettings(8, 15, 4, 3); + } + + /// + /// 800 KBaud. + /// + public static CanSettings Timing800KBaud() { + return new CanSettings(4, 16, 8, 3); + } + + /// + /// 1000 KBaud. + /// + public static CanSettings Timing1000KBaud () { + return new CanSettings(4, 15, 4, 3); + } + } +} diff --git a/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj b/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj index f96dc37..836e1a0 100644 --- a/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj +++ b/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj @@ -53,6 +53,7 @@ + From 13917f5d87eeacf1e7df22f0d416afb631fc91f6 Mon Sep 17 00:00:00 2001 From: Christoph Schweppe Date: Thu, 27 Oct 2022 15:22:36 +0200 Subject: [PATCH 2/2] Use enum evaluated on native side --- nanoFramework.Device.Can/CanBitTiming.cs | 58 +++++++++++++ nanoFramework.Device.Can/CanSettings.cs | 20 +++++ nanoFramework.Device.Can/CanTiming.cs | 83 ------------------- .../nanoFramework.Device.Can.nfproj | 2 +- 4 files changed, 79 insertions(+), 84 deletions(-) create mode 100644 nanoFramework.Device.Can/CanBitTiming.cs delete mode 100644 nanoFramework.Device.Can/CanTiming.cs diff --git a/nanoFramework.Device.Can/CanBitTiming.cs b/nanoFramework.Device.Can/CanBitTiming.cs new file mode 100644 index 0000000..0ef96ab --- /dev/null +++ b/nanoFramework.Device.Can/CanBitTiming.cs @@ -0,0 +1,58 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +namespace nanoFramework.Device.Can +{ + /// + /// CAN bit timings. + /// + public enum CanBitTiming + { + /// + /// None. Manually setted values. + /// + _None, + + /// + /// 25kBaud. + /// + _25kBaud, + + /// + /// 50kBaud. + /// + _50kBaud, + + /// + /// 100kBaud. + /// + _100kBaud, + + /// + /// 125kBaud. + /// + _125kBaud, + + /// + /// 250kBaud. + /// + _250kBaud, + + /// + /// 500kBaud. + /// + _500kBaud, + + /// + /// 800kBaud. + /// + _800kBaud, + + /// + /// 1000kBaud. + /// + _1000kBaud, + } +} diff --git a/nanoFramework.Device.Can/CanSettings.cs b/nanoFramework.Device.Can/CanSettings.cs index a7ac738..453b320 100644 --- a/nanoFramework.Device.Can/CanSettings.cs +++ b/nanoFramework.Device.Can/CanSettings.cs @@ -22,6 +22,9 @@ public sealed class CanSettings [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] private byte _syncJumpWidth; + [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] + private CanBitTiming _timing = CanBitTiming._None; + /// /// Initializes a new instance of . /// @@ -37,6 +40,14 @@ public sealed class CanSettings _syncJumpWidth = syncJumpWidth; } + /// + /// Initializes a new instance of . + /// + /// Bus baud rate timing preset. + public CanSettings (CanBitTiming timing) { + _timing = timing; + } + /// /// Initializes a copy of a object. /// @@ -84,5 +95,14 @@ public byte SyncJumpWidth get { return _syncJumpWidth; } set { _syncJumpWidth = value; } } + + /// + /// Gets or sets the . + /// + public CanBitTiming Timing + { + get { return _timing; } + set { _timing = value; } + } } } diff --git a/nanoFramework.Device.Can/CanTiming.cs b/nanoFramework.Device.Can/CanTiming.cs deleted file mode 100644 index 59b3f48..0000000 --- a/nanoFramework.Device.Can/CanTiming.cs +++ /dev/null @@ -1,83 +0,0 @@ -// -// Copyright (c) .NET Foundation and Contributors -// See LICENSE file in the project root for full license information. -// - -namespace nanoFramework.Device.Can -{ - /// - /// Helper for common CAN timings for the STM32 - /// - public static class CanTimingSTM32 - { - /// - /// 25 KBaud. - /// - public static CanSettings Timing500KBaud () { - return new CanSettings(6, 8, 1, 0); - } - - } - - /// - /// Helper for common CAN timings for the ESP32. Values from https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/twai_types.h#L54 - /// - public static class CanTimingESP32 - { - /// - /// 25 KBaud. - /// - public static CanSettings Timing25KBaud () { - return new CanSettings(128, 16, 8, 3); - } - - /// - /// 50 KBaud. - /// - public static CanSettings Timing50KBaud () { - return new CanSettings(80, 15, 4, 3); - } - - /// - /// 100 KBaud. - /// - public static CanSettings Timing100KBaud () { - return new CanSettings(40, 15, 4, 3); - } - - /// - /// 125 KBaud. - /// - public static CanSettings Timing125KBaud () { - return new CanSettings(32, 15, 4, 3); - } - - /// - /// 250 KBaud. - /// - public static CanSettings Timing250KBaud () { - return new CanSettings(16, 15, 4, 3); - } - - /// - /// 500 KBaud. - /// - public static CanSettings Timing500KBaud () { - return new CanSettings(8, 15, 4, 3); - } - - /// - /// 800 KBaud. - /// - public static CanSettings Timing800KBaud() { - return new CanSettings(4, 16, 8, 3); - } - - /// - /// 1000 KBaud. - /// - public static CanSettings Timing1000KBaud () { - return new CanSettings(4, 15, 4, 3); - } - } -} diff --git a/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj b/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj index 836e1a0..68ac034 100644 --- a/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj +++ b/nanoFramework.Device.Can/nanoFramework.Device.Can.nfproj @@ -53,7 +53,7 @@ - +