summaryrefslogtreecommitdiffstats
path: root/tmk_core
diff options
context:
space:
mode:
Diffstat (limited to 'tmk_core')
-rw-r--r--tmk_core/common/action.c10
-rw-r--r--tmk_core/common/action.h1
-rw-r--r--tmk_core/common/action_layer.c303
-rw-r--r--tmk_core/common/action_util.c4
-rw-r--r--tmk_core/common/arm_atsam/timer.c20
-rw-r--r--tmk_core/common/host.h11
-rw-r--r--tmk_core/common/keyboard.h2
-rw-r--r--tmk_core/common/keycode.h20
-rw-r--r--tmk_core/common/progmem.h6
-rw-r--r--tmk_core/common/wait.h4
-rw-r--r--tmk_core/protocol/arm_atsam/arm_atsam_protocol.h2
-rw-r--r--tmk_core/protocol/arm_atsam/clks.c90
-rw-r--r--tmk_core/protocol/arm_atsam/clks.h5
-rw-r--r--tmk_core/protocol/arm_atsam/i2c_master.c4
-rw-r--r--tmk_core/protocol/arm_atsam/led_matrix.c4
-rw-r--r--tmk_core/protocol/arm_atsam/main_arm_atsam.c18
-rw-r--r--tmk_core/protocol/arm_atsam/usb/compiler.h4
-rw-r--r--tmk_core/protocol/arm_atsam/usb/udi_cdc.c8
-rw-r--r--tmk_core/protocol/arm_atsam/usb/usb2422.c15
-rw-r--r--tmk_core/protocol/lufa/lufa.c17
20 files changed, 255 insertions, 293 deletions
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c
index b99c2acaa7..ec8d6ed7b9 100644
--- a/tmk_core/common/action.c
+++ b/tmk_core/common/action.c
@@ -653,7 +653,7 @@ void process_action(keyrecord_t *record, action_t action)
#ifndef NO_ACTION_TAPPING
#ifdef RETRO_TAPPING
- if (!is_tap_key(record->event.key)) {
+ if (!is_tap_action(action)) {
retro_tapping_counter = 0;
} else {
if (event.pressed) {
@@ -929,7 +929,15 @@ void clear_keyboard_but_mods_and_keys()
bool is_tap_key(keypos_t key)
{
action_t action = layer_switch_get_action(key);
+ return is_tap_action(action);
+}
+/** \brief Utilities for actions. (FIXME: Needs better description)
+ *
+ * FIXME: Needs documentation.
+ */
+bool is_tap_action(action_t action)
+{
switch (action.kind.id) {
case ACT_LMODS_TAP:
case ACT_RMODS_TAP:
diff --git a/tmk_core/common/action.h b/tmk_core/common/action.h
index 8e47e5339e..799e3bb0ef 100644
--- a/tmk_core/common/action.h
+++ b/tmk_core/common/action.h
@@ -97,6 +97,7 @@ void clear_keyboard_but_mods(void);
void clear_keyboard_but_mods_and_keys(void);
void layer_switch(uint8_t new_layer);
bool is_tap_key(keypos_t key);
+bool is_tap_action(action_t action);
#ifndef NO_ACTION_TAPPING
void process_record_tap_hint(keyrecord_t *record);
diff --git a/tmk_core/common/action_layer.c b/tmk_core/common/action_layer.c
index 120ce3f51b..6ff8c5549b 100644
--- a/tmk_core/common/action_layer.c
+++ b/tmk_core/common/action_layer.c
@@ -17,82 +17,76 @@ uint32_t default_layer_state = 0;
/** \brief Default Layer State Set At user Level
*
- * FIXME: Needs docs
+ * Run user code on default layer state change
*/
__attribute__((weak))
uint32_t default_layer_state_set_user(uint32_t state) {
- return state;
+ return state;
}
/** \brief Default Layer State Set At Keyboard Level
*
- * FIXME: Needs docs
+ * Run keyboard code on default layer state change
*/
__attribute__((weak))
uint32_t default_layer_state_set_kb(uint32_t state) {
- return default_layer_state_set_user(state);
+ return default_layer_state_set_user(state);
}
/** \brief Default Layer State Set
*
- * FIXME: Needs docs
+ * Static function to set the default layer state, prints debug info and clears keys
*/
-static void default_layer_state_set(uint32_t state)
-{
- state = default_layer_state_set_kb(state);
- debug("default_layer_state: ");
- default_layer_debug(); debug(" to ");
- default_layer_state = state;
- default_layer_debug(); debug("\n");
+static void default_layer_state_set(uint32_t state) {
+ state = default_layer_state_set_kb(state);
+ debug("default_layer_state: ");
+ default_layer_debug(); debug(" to ");
+ default_layer_state = state;
+ default_layer_debug(); debug("\n");
#ifdef STRICT_LAYER_RELEASE
- clear_keyboard_but_mods(); // To avoid stuck keys
+ clear_keyboard_but_mods(); // To avoid stuck keys
#else
- clear_keyboard_but_mods_and_keys(); // Don't reset held keys
+ clear_keyboard_but_mods_and_keys(); // Don't reset held keys
#endif
}
/** \brief Default Layer Print
*
- * FIXME: Needs docs
+ * Print out the hex value of the 32-bit default layer state, as well as the value of the highest bit.
*/
-void default_layer_debug(void)
-{
- dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
+void default_layer_debug(void) {
+ dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
}
/** \brief Default Layer Set
*
- * FIXME: Needs docs
+ * Sets the default layer state.
*/
-void default_layer_set(uint32_t state)
-{
- default_layer_state_set(state);
+void default_layer_set(uint32_t state) {
+ default_layer_state_set(state);
}
#ifndef NO_ACTION_LAYER
/** \brief Default Layer Or
*
- * FIXME: Needs docs
+ * Turns on the default layer based on matching bits between specifed layer and existing layer state
*/
-void default_layer_or(uint32_t state)
-{
- default_layer_state_set(default_layer_state | state);
+void default_layer_or(uint32_t state) {
+ default_layer_state_set(default_layer_state | state);
}
/** \brief Default Layer And
*
- * FIXME: Needs docs
+ * Turns on default layer based on matching enabled bits between specifed layer and existing layer state
*/
-void default_layer_and(uint32_t state)
-{
- default_layer_state_set(default_layer_state & state);
+void default_layer_and(uint32_t state) {
+ default_layer_state_set(default_layer_state & state);
}
/** \brief Default Layer Xor
*
- * FIXME: Needs docs
+ * Turns on default layer based on non-matching bits between specifed layer and existing layer state
*/
-void default_layer_xor(uint32_t state)
-{
- default_layer_state_set(default_layer_state ^ state);
+void default_layer_xor(uint32_t state) {
+ default_layer_state_set(default_layer_state ^ state);
}
#endif
@@ -104,170 +98,168 @@ uint32_t layer_state = 0;
/** \brief Layer state set user
*
- * FIXME: Needs docs
+ * Runs user code on layer state change
*/
__attribute__((weak))
uint32_t layer_state_set_user(uint32_t state) {
- return state;
+ return state;
}
/** \brief Layer state set keyboard
*
- * FIXME: Needs docs
+ * Runs keyboard code on layer state change
*/
__attribute__((weak))
uint32_t layer_state_set_kb(uint32_t state) {
- return layer_state_set_user(state);
+ return layer_state_set_user(state);
}
/** \brief Layer state set
*
- * FIXME: Needs docs
+ * Sets the layer to match the specifed state (a bitmask)
*/
-void layer_state_set(uint32_t state)
-{
- state = layer_state_set_kb(state);
- dprint("layer_state: ");
- layer_debug(); dprint(" to ");
- layer_state = state;
- layer_debug(); dprintln();
+void layer_state_set(uint32_t state) {
+ state = layer_state_set_kb(state);
+ dprint("layer_state: ");
+ layer_debug(); dprint(" to ");
+ layer_state = state;
+ layer_debug(); dprintln();
#ifdef STRICT_LAYER_RELEASE
- clear_keyboard_but_mods(); // To avoid stuck keys
+ clear_keyboard_but_mods(); // To avoid stuck keys
#else
- clear_keyboard_but_mods_and_keys(); // Don't reset held keys
+ clear_keyboard_but_mods_and_keys(); // Don't reset held keys
#endif
}
/** \brief Layer clear
*
- * FIXME: Needs docs
+ * Turn off all layers
*/
-void layer_clear(void)
-{
- layer_state_set(0);
+void layer_clear(void) {
+ layer_state_set(0);
}
/** \brief Layer state is
*
- * FIXME: Needs docs
+ * Return whether the given state is on (it might still be shadowed by a higher state, though)
*/
-bool layer_state_is(uint8_t layer)
-{
- return layer_state_cmp(layer_state, layer);
+bool layer_state_is(uint8_t layer) {
+ return layer_state_cmp(layer_state, layer);
}
/** \brief Layer state compare
*
- * FIXME: Needs docs
+ * Used for comparing layers {mostly used for unit testing}
*/
bool layer_state_cmp(uint32_t cmp_layer_state, uint8_t layer) {
- if (!cmp_layer_state) { return layer == 0; }
- return (cmp_layer_state & (1UL<<layer)) != 0;
+ if (!cmp_layer_state) { return layer == 0; }
+ return (cmp_layer_state & (1UL<<layer)) != 0;
}
/** \brief Layer move
*
- * FIXME: Needs docs
+ * Turns on the given layer and turn off all other layers
*/
-void layer_move(uint8_t layer)
-{
- layer_state_set(1UL<<layer);
+void layer_move(uint8_t layer) {
+ layer_state_set(1UL<<layer);
}
/** \brief Layer on
*
- * FIXME: Needs docs
+ * Turns on given layer
*/
-void layer_on(uint8_t layer)
-{
- layer_state_set(layer_state | (1UL<<layer));
+void layer_on(uint8_t layer) {
+ layer_state_set(layer_state | (1UL<<layer));
}
/** \brief Layer off
*
- * FIXME: Needs docs
+ * Turns off given layer
*/
-void layer_off(uint8_t layer)
-{
- layer_state_set(layer_state & ~(1UL<<layer));
+void layer_off(uint8_t layer) {
+ layer_state_set(layer_state & ~(1UL<<layer));
}
/** \brief Layer invert
*
- * FIXME: Needs docs
+ * Toggle the given layer (set it if it's unset, or unset it if it's set)
*/
-void layer_invert(uint8_t layer)
-{
- layer_state_set(layer_state ^ (1UL<<layer));
+void layer_invert(uint8_t layer) {
+ layer_state_set(layer_state ^ (1UL<<layer));
}
/** \brief Layer or
*
- * FIXME: Needs docs
+ * Turns on layers based on matching bits between specifed layer and existing layer state
*/
-void layer_or(uint32_t state)
-{
- layer_state_set(layer_state | state);
+void layer_or(uint32_t state) {
+ layer_state_set(layer_state | state);
}
/** \brief Layer and
*
- * FIXME: Needs docs
+ * Turns on layers based on matching enabled bits between specifed layer and existing layer state
*/
-void layer_and(uint32_t state)
-{
- layer_state_set(layer_state & state);
+void layer_and(uint32_t state) {
+ layer_state_set(layer_state & state);
}
/** \brief Layer xor
*
- * FIXME: Needs docs
+ * Turns on layers based on non-matching bits between specifed layer and existing layer state
*/
-void layer_xor(uint32_t state)
-{
- layer_state_set(layer_state ^ state);
+void layer_xor(uint32_t state) {
+ layer_state_set(layer_state ^ state);
}
/** \brief Layer debug printing
*
- * FIXME: Needs docs
+ * Print out the hex value of the 32-bit layer state, as well as the value of the highest bit.
*/
-void layer_debug(void)
-{
- dprintf("%08lX(%u)", layer_state, biton32(layer_state));
+void layer_debug(void) {
+ dprintf("%08lX(%u)", layer_state, biton32(layer_state));
}
#endif
#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
+/** \brief source layer cache
+ */
+
uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] = {{0}};
-void update_source_layers_cache(keypos_t key, uint8_t layer)
-{
- const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
- const uint8_t storage_row = key_number / 8;
- const uint8_t storage_bit = key_number % 8;
-
- for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
- source_layers_cache[storage_row][bit_number] ^=
- (-((layer & (1U << bit_number)) != 0)
- ^ source_layers_cache[storage_row][bit_number])
- & (1U << storage_bit);
- }
+/** \brief update source layers cache
+ *
+ * Updates the cached keys when changing layers
+ */
+void update_source_layers_cache(keypos_t key, uint8_t layer) {
+ const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
+ const uint8_t storage_row = key_number / 8;
+ const uint8_t storage_bit = key_number % 8;
+
+ for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
+ source_layers_cache[storage_row][bit_number] ^=
+ (-((layer & (1U << bit_number)) != 0)
+ ^ source_layers_cache[storage_row][bit_number])
+ & (1U << storage_bit);
+ }
}
-uint8_t read_source_layers_cache(keypos_t key)
-{
- const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
- const uint8_t storage_row = key_number / 8;
- const uint8_t storage_bit = key_number % 8;
- uint8_t layer = 0;
-
- for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
- layer |=
- ((source_layers_cache[storage_row][bit_number]
- & (1U << storage_bit)) != 0)
- << bit_number;
- }
-
- return layer;
+/** \brief read source layers cache
+ *
+ * reads the cached keys stored when the layer was changed
+ */
+uint8_t read_source_layers_cache(keypos_t key) {
+ const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
+ const uint8_t storage_row = key_number / 8;
+ const uint8_t storage_bit = key_number % 8;
+ uint8_t layer = 0;
+
+ for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
+ layer |=
+ ((source_layers_cache[storage_row][bit_number]
+ & (1U << storage_bit)) != 0)
+ << bit_number;
+ }
+
+ return layer;
}
#endif
@@ -278,61 +270,58 @@ uint8_t read_source_layers_cache(keypos_t key)
* when the layer is switched after the down event but before the up
* event as they may get stuck otherwise.
*/
-action_t store_or_get_action(bool pressed, keypos_t key)
-{
+action_t store_or_get_action(bool pressed, keypos_t key) {
#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
- if (disable_action_cache) {
- return layer_switch_get_action(key);
- }
-
- uint8_t layer;
-
- if (pressed) {
- layer = layer_switch_get_layer(key);
- update_source_layers_cache(key, layer);
- }
- else {
- layer = read_source_layers_cache(key);
- }
- return action_for_key(layer, key);
-#else
+ if (disable_action_cache) {
return layer_switch_get_action(key);
+ }
+
+ uint8_t layer;
+
+ if (pressed) {
+ layer = layer_switch_get_layer(key);
+ update_source_layers_cache(key, layer);
+ }
+ else {
+ layer = read_source_layers_cache(key);
+ }
+ return action_for_key(layer, key);
+#else
+ return layer_switch_get_action(key);
#endif
}
/** \brief Layer switch get layer
*
- * FIXME: Needs docs
+ * Gets the layer based on key info
*/
-int8_t layer_switch_get_layer(keypos_t key)
-{
+int8_t layer_switch_get_layer(keypos_t key) {
#ifndef NO_ACTION_LAYER
- action_t action;
- action.code = ACTION_TRANSPARENT;
-
- uint32_t layers = layer_state | default_layer_state;
- /* check top layer first */
- for (int8_t i = 31; i >= 0; i--) {
- if (layers & (1UL<<i)) {
- action = action_for_key(i, key);
- if (action.code != ACTION_TRANSPARENT) {
- return i;
- }
- }
+ action_t action;
+ action.code = ACTION_TRANSPARENT;
+
+ uint32_t layers = layer_state | default_layer_state;
+ /* check top layer first */
+ for (int8_t i = 31; i >= 0; i--) {
+ if (layers & (1UL<<i)) {
+ action = action_for_key(i, key);
+ if (action.code != ACTION_TRANSPARENT) {
+ return i;
+ }
}
- /* fall back to layer 0 */
- return 0;
+ }
+ /* fall back to layer 0 */
+ return 0;
#else
- return biton32(default_layer_state);
+ return biton32(default_layer_state);
#endif
}
/** \brief Layer switch get layer
*
- * FIXME: Needs docs
+ * Gets action code based on key position
*/
-action_t layer_switch_get_action(keypos_t key)
-{
- return action_for_key(layer_switch_get_layer(key), key);
+action_t layer_switch_get_action(keypos_t key) {
+ return action_for_key(layer_switch_get_layer(key), key);
}
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c
index afd4ae8b25..58401ace55 100644
--- a/tmk_core/common/action_util.c
+++ b/tmk_core/common/action_util.c
@@ -54,7 +54,7 @@ int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
-static int16_t oneshot_time = 0;
+static uint16_t oneshot_time = 0;
bool has_oneshot_mods_timed_out(void) {
return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
}
@@ -79,7 +79,7 @@ inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
-static int16_t oneshot_layer_time = 0;
+static uint16_t oneshot_layer_time = 0;
inline bool has_oneshot_layer_timed_out() {
return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
!(get_oneshot_layer_state() & ONESHOT_TOGGLED);
diff --git a/tmk_core/common/arm_atsam/timer.c b/tmk_core/common/arm_atsam/timer.c
index bcfe5002c3..6c3905e308 100644
--- a/tmk_core/common/arm_atsam/timer.c
+++ b/tmk_core/common/arm_atsam/timer.c
@@ -9,7 +9,7 @@ void set_time(uint64_t tset)
void timer_init(void)
{
- ms_clk = 0;
+ timer_clear();
}
uint16_t timer_read(void)
@@ -37,23 +37,7 @@ uint32_t timer_elapsed32(uint32_t tlast)
return TIMER_DIFF_32(timer_read32(), tlast);
}
-uint32_t timer_elapsed64(uint32_t tlast)
-{
- uint64_t tnow = timer_read64();
- return (tnow >= tlast ? tnow - tlast : UINT64_MAX - tlast + tnow);
-}
-
void timer_clear(void)
{
- ms_clk = 0;
-}
-
-void wait_ms(uint64_t msec)
-{
- CLK_delay_ms(msec);
-}
-
-void wait_us(uint16_t usec)
-{
- CLK_delay_us(usec);
+ set_time(0);
}
diff --git a/tmk_core/common/host.h b/tmk_core/common/host.h
index aeabba7107..3d172eed66 100644
--- a/tmk_core/common/host.h
+++ b/tmk_core/common/host.h
@@ -15,14 +15,18 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef HOST_H
-#define HOST_H
+#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "report.h"
#include "host_driver.h"
+#define IS_LED_ON(leds, led_name) ( (leds) & (1 << (led_name)))
+#define IS_LED_OFF(leds, led_name) (~(leds) & (1 << (led_name)))
+
+#define IS_HOST_LED_ON(led_name) IS_LED_ON(host_keyboard_leds(), led_name)
+#define IS_HOST_LED_OFF(led_name) IS_LED_OFF(host_keyboard_leds(), led_name)
#ifdef __cplusplus
extern "C" {
@@ -31,7 +35,6 @@ extern "C" {
extern uint8_t keyboard_idle;
extern uint8_t keyboard_protocol;
-
/* host driver */
void host_set_driver(host_driver_t *driver);
host_driver_t *host_get_driver(void);
@@ -49,5 +52,3 @@ uint16_t host_last_consumer_report(void);
#ifdef __cplusplus
}
#endif
-
-#endif
diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h
index 71e594a890..ea2f336e9d 100644
--- a/tmk_core/common/keyboard.h
+++ b/tmk_core/common/keyboard.h
@@ -67,6 +67,8 @@ void keyboard_init(void);
void keyboard_task(void);
/* it runs when host LED status is updated */
void keyboard_set_leds(uint8_t leds);
+/* it runs whenever code has to behave differently on a slave */
+bool is_keyboard_master(void);
#ifdef __cplusplus
}
diff --git a/tmk_core/common/keycode.h b/tmk_core/common/keycode.h
index ac3edbd215..d5904276ee 100644
--- a/tmk_core/common/keycode.h
+++ b/tmk_core/common/keycode.h
@@ -46,6 +46,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define MOD_BIT(code) (1 << MOD_INDEX(code))
#define MOD_INDEX(code) ((code) & 0x07)
+#define MOD_MASK_CTRL (MOD_BIT(KC_LCTRL) | MOD_BIT(KC_RCTRL))
+#define MOD_MASK_SHIFT (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))
+#define MOD_MASK_ALT (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))
+#define MOD_MASK_GUI (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))
+#define MOD_MASK_CS (MOD_MASK_CTRL | MOD_MASK_SHIFT)
+#define MOD_MASK_CA (MOD_MASK_CTRL | MOD_MASK_ALT)
+#define MOD_MASK_CG (MOD_MASK_CTRL | MOD_MASK_GUI)
+#define MOD_MASK_SA (MOD_MASK_SHIFT | MOD_MASK_ALT)
+#define MOD_MASK_SG (MOD_MASK_SHIFT | MOD_MASK_GUI)
+#define MOD_MASK_AG (MOD_MASK_ALT | MOD_MASK_GUI)
+#define MOD_MASK_CSA (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_ALT)
+#define MOD_MASK_CSG (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_GUI)
+#define MOD_MASK_CAG (MOD_MASK_CTRL | MOD_MASK_ALT | MOD_MASK_GUI)
+#define MOD_MASK_SAG (MOD_MASK_SHIFT | MOD_MASK_ALT | MOD_MASK_GUI)
+#define MOD_MASK_CSAG (MOD_MASK_CTRL | MOD_MASK_SHIFT | MOD_MASK_ALT | MOD_MASK_GUI)
+
#define FN_BIT(code) (1 << FN_INDEX(code))
#define FN_INDEX(code) ((code) - KC_FN0)
#define FN_MIN KC_FN0
@@ -174,6 +190,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define KC_BRIU KC_BRIGHTNESS_UP
#define KC_BRID KC_BRIGHTNESS_DOWN
+/* System Specific */
+#define KC_BRMU KC_PAUSE
+#define KC_BRMD KC_SCROLLLOCK
+
/* Mouse Keys */
#define KC_MS_U KC_MS_UP
#define KC_MS_D KC_MS_DOWN
diff --git a/tmk_core/common/progmem.h b/tmk_core/common/progmem.h
index dcc9efb3ce..de93138397 100644
--- a/tmk_core/common/progmem.h
+++ b/tmk_core/common/progmem.h
@@ -5,9 +5,9 @@
# include <avr/pgmspace.h>
#else
# define PROGMEM
-# define pgm_read_byte(p) *((unsigned char*)p)
-# define pgm_read_word(p) *((uint16_t*)p)
-# define pgm_read_dword(p) *((uint32_t*)p)
+# define pgm_read_byte(p) *((unsigned char*)(p))
+# define pgm_read_word(p) *((uint16_t*)(p))
+# define pgm_read_dword(p) *((uint32_t*)(p))
#endif
#endif
diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h
index a7cded9420..a77840bcef 100644
--- a/tmk_core/common/wait.h
+++ b/tmk_core/common/wait.h
@@ -15,6 +15,10 @@ extern "C" {
# include "ch.h"
# define wait_ms(ms) chThdSleepMilliseconds(ms)
# define wait_us(us) chThdSleepMicroseconds(us)
+#elif defined PROTOCOL_ARM_ATSAM
+# include "clks.h"
+# define wait_ms(ms) CLK_delay_ms(ms)
+# define wait_us(us) CLK_delay_us(us)
#elif defined(__arm__)
# include "wait_api.h"
#else // Unit tests
diff --git a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h
index 2ba0991749..928af8c7e1 100644
--- a/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h
+++ b/tmk_core/protocol/arm_atsam/arm_atsam_protocol.h
@@ -21,8 +21,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "samd51j18a.h"
#include "md_bootloader.h"
+#include "timer.h"
#include "d51_util.h"
#include "clks.h"
+#include "wait.h"
#include "adc.h"
#include "i2c_master.h"
#include "spi.h"
diff --git a/tmk_core/protocol/arm_atsam/clks.c b/tmk_core/protocol/arm_atsam/clks.c
index 8768d0a99e..1ff318e59b 100644
--- a/tmk_core/protocol/arm_atsam/clks.c
+++ b/tmk_core/protocol/arm_atsam/clks.c
@@ -21,8 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
volatile clk_t system_clks;
volatile uint64_t ms_clk;
-
-volatile uint8_t us_delay_done;
+uint32_t usec_delay_mult;
+#define USEC_DELAY_LOOP_CYCLES 3 //Sum of instruction cycles in us delay loop
const uint32_t sercom_apbbase[] = {(uint32_t)SERCOM0,(uint32_t)SERCOM1,(uint32_t)SERCOM2,(uint32_t)SERCOM3,(uint32_t)SERCOM4,(uint32_t)SERCOM5};
const uint8_t sercom_pchan[] = {7, 8, 23, 24, 34, 35};
@@ -73,6 +73,9 @@ void CLK_oscctrl_init(void)
system_clks.freq_gclk[0] = system_clks.freq_dpll[0];
+ usec_delay_mult = system_clks.freq_gclk[0] / (USEC_DELAY_LOOP_CYCLES * 1000000);
+ if (usec_delay_mult < 1) usec_delay_mult = 1; //Never allow a multiplier of zero
+
DBGC(DC_CLK_OSC_INIT_COMPLETE);
}
@@ -158,23 +161,11 @@ void TC4_Handler()
}
}
-void TC5_Handler()
-{
- if (TC5->COUNT16.INTFLAG.bit.MC0)
- {
- TC5->COUNT16.INTFLAG.reg = TC_INTENCLR_MC0;
- us_delay_done = 1;
- TC5->COUNT16.CTRLA.bit.ENABLE = 0;
- while (TC5->COUNT16.SYNCBUSY.bit.ENABLE) {}
- }
-}
-
uint32_t CLK_enable_timebase(void)
{
Gclk *pgclk = GCLK;
Mclk *pmclk = MCLK;
Tc *ptc4 = TC4;
- Tc *ptc5 = TC5;
Tc *ptc0 = TC0;
Evsys *pevsys = EVSYS;
@@ -189,11 +180,6 @@ uint32_t CLK_enable_timebase(void)
pgclk->PCHCTRL[TC4_GCLK_ID].bit.GEN = GEN_TC45;
pgclk->PCHCTRL[TC4_GCLK_ID].bit.CHEN = 1;
- //unmask TC5 sourcegclk2 to TC5
- pmclk->APBCMASK.bit.TC5_ = 1;
- pgclk->PCHCTRL[TC5_GCLK_ID].bit.GEN = GEN_TC45;
- pgclk->PCHCTRL[TC5_GCLK_ID].bit.CHEN = 1;
-
//configure TC4
DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_BEGIN);
ptc4->COUNT16.CTRLA.bit.ENABLE = 0;
@@ -220,30 +206,6 @@ uint32_t CLK_enable_timebase(void)
DBGC(DC_CLK_ENABLE_TIMEBASE_TC4_COMPLETE);
- //configure TC5
- DBGC(DC_CLK_ENABLE_TIMEBASE_TC5_BEGIN);
- ptc5->COUNT16.CTRLA.bit.ENABLE = 0;
- while (ptc5->COUNT16.SYNCBUSY.bit.ENABLE) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC5_SYNC_DISABLE); }
- ptc5->COUNT16.CTRLA.bit.SWRST = 1;
- while (ptc5->COUNT16.SYNCBUSY.bit.SWRST) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC5_SYNC_SWRST_1); }
- while (ptc5->COUNT16.CTRLA.bit.SWRST) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC5_SYNC_SWRST_2); }
-
- //CTRLA defaults
- //CTRLB as default, counting up
- ptc5->COUNT16.CTRLBCLR.reg = 5;
- while (ptc5->COUNT16.SYNCBUSY.bit.CTRLB) { DBGC(DC_CLK_ENABLE_TIMEBASE_TC5_SYNC_CLTRB); }
- //ptc5->COUNT16.DBGCTRL.bit.DBGRUN = 1;
-
- //wave mode
- ptc5->COUNT16.WAVE.bit.WAVEGEN = 1; //MFRQ match frequency mode, toggle each CC match
- //generate event for next stage
- ptc5->COUNT16.EVCTRL.bit.MCEO0 = 1;
-
- NVIC_EnableIRQ(TC5_IRQn);
- ptc5->COUNT16.INTENSET.bit.MC0 = 1;
-
- DBGC(DC_CLK_ENABLE_TIMEBASE_TC5_COMPLETE);
-
//unmask TC0,1, sourcegclk2 to TC0,1
pmclk->APBAMASK.bit.TC0_ = 1;
pgclk->PCHCTRL[TC0_GCLK_ID].bit.GEN = GEN_TC45;
@@ -289,37 +251,27 @@ uint32_t CLK_enable_timebase(void)
return 0;
}
-uint32_t CLK_get_ms(void)
+void CLK_delay_us(uint32_t usec)
{
- return ms_clk;
-}
-
-void CLK_delay_us(uint16_t usec)
-{
- us_delay_done = 0;
-
- if (TC5->COUNT16.CTRLA.bit.ENABLE)
- {
- TC5->COUNT16.CTRLA.bit.ENABLE = 0;
- while (TC5->COUNT16.SYNCBUSY.bit.ENABLE) {}
- }
-
- if (usec < 10) usec = 0;
- else usec -= 10;
-
- TC5->COUNT16.CC[0].reg = usec;
- while (TC5->COUNT16.SYNCBUSY.bit.CC0) {}
-
- TC5->COUNT16.CTRLA.bit.ENABLE = 1;
- while (TC5->COUNT16.SYNCBUSY.bit.ENABLE) {}
-
- while (!us_delay_done) {}
+ asm (
+ "CBZ R0, return\n\t" //If usec == 0, branch to return label
+ );
+ asm (
+ "MULS R0, %0\n\t" //Multiply R0(usec) by usec_delay_mult and store in R0
+ ".balign 16\n\t" //Ensure loop is aligned for fastest performance
+ "loop: SUBS R0, #1\n\t" //Subtract 1 from R0 and update flags (1 cycle)
+ "BNE loop\n\t" //Branch if non-zero to loop label (2 cycles) NOTE: USEC_DELAY_LOOP_CYCLES is the sum of loop cycles
+ "return:\n\t" //Return label
+ : //No output registers
+ : "r" (usec_delay_mult) //For %0
+ );
+ //Note: BX LR generated
}
void CLK_delay_ms(uint64_t msec)
{
- msec += CLK_get_ms();
- while (msec > CLK_get_ms()) {}
+ msec += timer_read64();
+ while (msec > timer_read64()) {}
}
void clk_enable_sercom_apbmask(int sercomn)
diff --git a/tmk_core/protocol/arm_atsam/clks.h b/tmk_core/protocol/arm_atsam/clks.h
index 96819bfdd0..1b01a1764e 100644
--- a/tmk_core/protocol/arm_atsam/clks.h
+++ b/tmk_core/protocol/arm_atsam/clks.h
@@ -77,9 +77,8 @@ void CLK_oscctrl_init(void);
void CLK_reset_time(void);
uint32_t CLK_set_gclk_freq(uint8_t gclkn, uint32_t freq);
uint32_t CLK_enable_timebase(void);
-uint32_t CLK_get_ms(void);
-uint64_t CLK_get_us(void);
-void CLK_delay_us(uint16_t usec);
+uint64_t timer_read64(void);
+void CLK_delay_us(uint32_t usec);
void CLK_delay_ms(uint64_t msec);
uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq);
diff --git a/tmk_core/protocol/arm_atsam/i2c_master.c b/tmk_core/protocol/arm_atsam/i2c_master.c
index f608a79cc9..d91a851f37 100644
--- a/tmk_core/protocol/arm_atsam/i2c_master.c
+++ b/tmk_core/protocol/arm_atsam/i2c_master.c
@@ -265,12 +265,12 @@ uint8_t I2C3733_Init_Control(void)
//USB state machine will enable driver when communication is ready
I2C3733_Control_Set(0);
- CLK_delay_ms(1);
+ wait_ms(1);
sr_exp_data.bit.IRST = 0;
SR_EXP_WriteData();
- CLK_delay_ms(1);
+ wait_ms(1);
DBGC(DC_I2C3733_INIT_CONTROL_COMPLETE);
diff --git a/tmk_core/protocol/arm_atsam/led_matrix.c b/tmk_core/protocol/arm_atsam/led_matrix.c
index 9b76d8bbc8..04d05af6db 100644
--- a/tmk_core/protocol/arm_atsam/led_matrix.c
+++ b/tmk_core/protocol/arm_atsam/led_matrix.c
@@ -504,11 +504,11 @@ void led_matrix_task(void)
if (led_enabled)
{
//If an update may run and frame processing has completed
- if (CLK_get_ms() >= led_next_run && led_cur == lede)
+ if (timer_read64() >= led_next_run && led_cur == lede)
{
uint8_t drvid;
- led_next_run = CLK_get_ms() + LED_UPDATE_RATE; //Set next frame update time
+ led_next_run = timer_read64() + LED_UPDATE_RATE; //Set next frame update time
//NOTE: GCR does not need to be timed with LED processing, but there is really no harm
if (gcr_actual != gcr_actual_last)
diff --git a/tmk_core/protocol/arm_atsam/main_arm_atsam.c b/tmk_core/protocol/arm_atsam/main_arm_atsam.c
index 2bda7d7c7b..