From 81fc6e61bdae1871c86f0c867a0d5ef5d913546a Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 6 May 2020 20:46:08 +0200 Subject: [PATCH] Introduce MCU Properties This introduces MCU properties, so that MCU drivers can change their behaviour and/or setup tasks based on them, without having to write a `setup()` method or a custom constructor for the top-level device. In practice, this allows us to tell the MCU driver to - for example - disable JTAG or clock division during setup, and thus, we won't need to do that in code in the device constructor. This is a breaking change, kind of, because the `mcu::Base` and `mcu::ATmega32U4` drivers changed APIs. However, no device was using those directly, only via `ATmega32U4Keyboard`, and those parts remain compatible. While there, updated the `KBD4x` and `Splitography` devices to use the new properties instead of a custom constructor. Signed-off-by: Gergely Nagy --- src/kaleidoscope/device/ATmega32U4Keyboard.h | 3 ++- src/kaleidoscope/device/Base.h | 2 ++ src/kaleidoscope/device/kbdfans/KBD4x.h | 13 ++++++------- src/kaleidoscope/device/softhruf/Splitography.h | 13 ++++++------- src/kaleidoscope/driver/mcu/ATmega32U4.h | 17 ++++++++++++++--- src/kaleidoscope/driver/mcu/Base.h | 5 ++++- src/kaleidoscope/driver/mcu/None.h | 6 +++--- src/kaleidoscope/driver/mcu/SAMD.h | 4 ++-- 8 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/kaleidoscope/device/ATmega32U4Keyboard.h b/src/kaleidoscope/device/ATmega32U4Keyboard.h index c769cf32..a58146c0 100644 --- a/src/kaleidoscope/device/ATmega32U4Keyboard.h +++ b/src/kaleidoscope/device/ATmega32U4Keyboard.h @@ -70,7 +70,8 @@ namespace kaleidoscope { namespace device { struct ATmega32U4KeyboardProps : kaleidoscope::device::BaseProps { - typedef kaleidoscope::driver::mcu::ATmega32U4 MCU; + typedef kaleidoscope::driver::mcu::ATmega32U4Props MCUProps; + typedef kaleidoscope::driver::mcu::ATmega32U4 MCU; typedef kaleidoscope::driver::storage::ATmega32U4EEPROMProps StorageProps; typedef kaleidoscope::driver::storage::AVREEPROM Storage; }; diff --git a/src/kaleidoscope/device/Base.h b/src/kaleidoscope/device/Base.h index d9ac0652..825c122b 100644 --- a/src/kaleidoscope/device/Base.h +++ b/src/kaleidoscope/device/Base.h @@ -59,6 +59,7 @@ struct BaseProps { typedef kaleidoscope::driver::keyscanner::None KeyScanner; typedef kaleidoscope::driver::led::BaseProps LEDDriverProps; typedef kaleidoscope::driver::led::None LEDDriver; + typedef kaleidoscope::driver::mcu::BaseProps MCUProps; typedef kaleidoscope::driver::mcu::None MCU; typedef kaleidoscope::driver::bootloader::None Bootloader; typedef kaleidoscope::driver::storage::BaseProps StorageProps; @@ -88,6 +89,7 @@ class Base { typedef typename _DeviceProps::KeyScannerProps::KeyAddr KeyAddr; typedef typename _DeviceProps::LEDDriverProps LEDDriverProps; typedef typename _DeviceProps::LEDDriver LEDDriver; + typedef typename _DeviceProps::MCUProps MCUProps; typedef typename _DeviceProps::MCU MCU; typedef typename _DeviceProps::Bootloader Bootloader; typedef typename _DeviceProps::StorageProps StorageProps; diff --git a/src/kaleidoscope/device/kbdfans/KBD4x.h b/src/kaleidoscope/device/kbdfans/KBD4x.h index 02726e85..cc27ba67 100644 --- a/src/kaleidoscope/device/kbdfans/KBD4x.h +++ b/src/kaleidoscope/device/kbdfans/KBD4x.h @@ -32,6 +32,11 @@ namespace device { namespace kbdfans { struct KBD4xProps : kaleidoscope::device::ATmega32U4KeyboardProps { + struct MCUProps: public kaleidoscope::driver::mcu::ATmega32U4Props { + static constexpr bool disable_jtag = true; + static constexpr bool disable_clock_division = true; + }; + typedef kaleidoscope::driver::mcu::ATmega32U4 MCU; struct KeyScannerProps : public kaleidoscope::driver::keyscanner::ATmegaProps { ATMEGA_KEYSCANNER_PROPS( ROW_PIN_LIST({ PIN_D0, PIN_D1, PIN_D2, PIN_D3 }), @@ -44,13 +49,7 @@ struct KBD4xProps : kaleidoscope::device::ATmega32U4KeyboardProps { }; #ifndef KALEIDOSCOPE_VIRTUAL_BUILD -class KBD4x: public kaleidoscope::device::ATmega32U4Keyboard { - public: - KBD4x() { - mcu_.disableJTAG(); - mcu_.disableClockDivision(); - } -}; +ATMEGA32U4_DEVICE(KBD4x); #else // ifndef KALEIDOSCOPE_VIRTUAL_BUILD class KBD4x; #endif // ifndef KALEIDOSCOPE_VIRTUAL_BUILD diff --git a/src/kaleidoscope/device/softhruf/Splitography.h b/src/kaleidoscope/device/softhruf/Splitography.h index d1f9d66e..41ad1656 100644 --- a/src/kaleidoscope/device/softhruf/Splitography.h +++ b/src/kaleidoscope/device/softhruf/Splitography.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Hardware-SOFTHRUF-Splitography -- Splitography hardware support for Kaleidoscope - * Copyright (C) 2018, 2019 Keyboard.io, Inc + * Copyright (C) 2018, 2019, 2020 Keyboard.io, Inc * * Based on QMK (commit e9a67f8fd) and sdothum's fork (commit 8616b44) * (C) Jack Humbert, Jun Wako, Steven Hum, and others @@ -39,6 +39,10 @@ namespace device { namespace softhruf { struct SplitographyProps : kaleidoscope::device::ATmega32U4KeyboardProps { + struct MCUProps: kaleidoscope::driver::mcu::ATmega32U4Props { + static constexpr bool disable_jtag = true; + }; + typedef kaleidoscope::driver::mcu::ATmega32U4 MCU; struct KeyScannerProps : public kaleidoscope::driver::keyscanner::ATmegaProps { ATMEGA_KEYSCANNER_PROPS( ROW_PIN_LIST({ PIN_D0, PIN_D1, PIN_D2, PIN_D3 }), @@ -51,12 +55,7 @@ struct SplitographyProps : kaleidoscope::device::ATmega32U4KeyboardProps { }; #ifndef KALEIDOSCOPE_VIRTUAL_BUILD -class Splitography: public kaleidoscope::device::ATmega32U4Keyboard { - public: - Splitography() { - mcu_.disableJTAG(); - } -}; +ATMEGA32U4_DEVICE(Splitography); #else // ifndef KALEIDOSCOPE_VIRTUAL_BUILD class Splitography; #endif // ifndef KALEIDOSCOPE_VIRTUAL_BUILD diff --git a/src/kaleidoscope/driver/mcu/ATmega32U4.h b/src/kaleidoscope/driver/mcu/ATmega32U4.h index 2a15fafd..134e8f90 100644 --- a/src/kaleidoscope/driver/mcu/ATmega32U4.h +++ b/src/kaleidoscope/driver/mcu/ATmega32U4.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * driver::MCU::ATmega32U4 -- ATmega32U4 MCU driver for Kaleidoscope - * Copyright (C) 2019 Keyboard.io, Inc + * Copyright (C) 2019, 2020 Keyboard.io, Inc * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software @@ -23,8 +23,14 @@ namespace kaleidoscope { namespace driver { namespace mcu { +struct ATmega32U4Props: public kaleidoscope::driver::mcu::BaseProps { + static constexpr bool disable_jtag = false; + static constexpr bool disable_clock_division = false; +}; + #ifndef KALEIDOSCOPE_VIRTUAL_BUILD -class ATmega32U4 : public kaleidoscope::driver::mcu::Base { +template +class ATmega32U4 : public kaleidoscope::driver::mcu::Base<_Props> { public: void detachFromHost() { UDCON |= _BV(DETACH); @@ -61,7 +67,12 @@ class ATmega32U4 : public kaleidoscope::driver::mcu::Base { CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); } - void setup() {} + void setup() { + if (_Props::disable_jtag) + disableJTAG(); + if (_Props::disable_clock_division) + disableClockDivision(); + } }; #else typedef Base ATmega32U4; diff --git a/src/kaleidoscope/driver/mcu/Base.h b/src/kaleidoscope/driver/mcu/Base.h index 671738ab..9acc7aa4 100644 --- a/src/kaleidoscope/driver/mcu/Base.h +++ b/src/kaleidoscope/driver/mcu/Base.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * driver::mcu::Base -- MCU driver base class for Kaleidoscope - * Copyright (C) 2019 Keyboard.io, Inc + * Copyright (C) 2019, 2020 Keyboard.io, Inc * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software @@ -21,6 +21,9 @@ namespace kaleidoscope { namespace driver { namespace mcu { +struct BaseProps {}; + +template class Base { public: Base() {} diff --git a/src/kaleidoscope/driver/mcu/None.h b/src/kaleidoscope/driver/mcu/None.h index 2a1a397e..9baaf274 100644 --- a/src/kaleidoscope/driver/mcu/None.h +++ b/src/kaleidoscope/driver/mcu/None.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * driver::MCU::None -- Dummy MCU driver for Kaleidoscope - * Copyright (C) 2019 Keyboard.io, Inc + * Copyright (C) 2019, 2020 Keyboard.io, Inc * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software @@ -27,9 +27,9 @@ namespace mcu { * The purpose of this class is to serve as a default inside the base * `kaleidoscope::device::Base` class, with a name more descriptive than * `mcu::Base`. In practice, one shouldn't use it, and should override the - * bootloader in the device description. + * MCU in the device description. */ -class None : public kaleidoscope::driver::mcu::Base {}; +class None : public kaleidoscope::driver::mcu::Base {}; } } diff --git a/src/kaleidoscope/driver/mcu/SAMD.h b/src/kaleidoscope/driver/mcu/SAMD.h index 0b8e2fa3..9fc3fee9 100644 --- a/src/kaleidoscope/driver/mcu/SAMD.h +++ b/src/kaleidoscope/driver/mcu/SAMD.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * driver::mcu::SAMD -- SAMD MCU driver class for Kaleidoscope - * Copyright (C) 2019 Keyboard.io, Inc + * Copyright (C) 2019, 2020 Keyboard.io, Inc * Copyright (C) 2019 Dygma, Inc * * This program is free software: you can redistribute it and/or modify it under @@ -26,7 +26,7 @@ namespace kaleidoscope { namespace driver { namespace mcu { -class SAMD : public kaleidoscope::driver::mcu::Base { +class SAMD : public kaleidoscope::driver::mcu::Base { public: void detachFromHost() { USBDevice.detach();