tmk_keyboard/tmk_core/common
Purdea Andrei 338a1506dc tmk_core/common/timer.h: Fixing TIMER_DIFF macro to calculate difference correctly after the timer wraps.
Let's go through an example, using the following macro:

If the first timer read is 0xe4 and the second one is 0x32, the timer wrapped.
If the timer would have had more bits, it's new value would have been 0x132,
and the correct difference in time is 0x132 - 0xe4 = 0x4e

old code TIMER_DIFF_8(0x32, 0xe4) = 0xff - 0xe4 + 0x32 = 0x4d, which is wrong.
new code TIMER_DIFF_8(0x32, 0xe4) = 0xff + 1 - 0xe4 + 0x32 = 0x4e, which is correct.

This also gives a chance for a smart compiler to optimize the code using normal
integer overflow.

For example on AVR, the following C code:
uint8_t __attribute__ ((noinline)) test(uint8_t current_timer, uint8_t start_timer)
{
    return TIMER_DIFF_8(current_timer, start_timer);
}
With the original code, it gets translated to the following list of instructions:
00004c6e <test>:
    4c6e:       98 2f           mov     r25, r24
    4c70:       86 1b           sub     r24, r22
    4c72:       96 17           cp      r25, r22
    4c74:       08 f4           brcc    .+2             ; 0x4c78 <test+0xa>
    4c76:       81 50           subi    r24, 0x01       ; 1
    4c78:       08 95           ret
But with this commit, it gets translated to a single instruction:
00004c40 <test>:
    4c40:       86 1b           sub     r24, r22
    4c42:       08 95           ret

This unfortunately doesn't always work so nicely, for example the following C code:
int __attribute__ ((noinline)) test(uint8_t current_timer, uint8_t start_timer)
{
    return TIMER_DIFF_8(current_timer, start_timer);
}
(Note: return type changed to int)
With the original code it gets translated to:
00004c6e <test>:
    4c6e:       28 2f           mov     r18, r24
    4c70:       30 e0           ldi     r19, 0x00       ; 0
    4c72:       46 2f           mov     r20, r22
    4c74:       50 e0           ldi     r21, 0x00       ; 0
    4c76:       86 17           cp      r24, r22
    4c78:       20 f0           brcs    .+8             ; 0x4c82 <test+0x14>
    4c7a:       c9 01           movw    r24, r18
    4c7c:       84 1b           sub     r24, r20
    4c7e:       95 0b           sbc     r25, r21
    4c80:       08 95           ret
    4c82:       c9 01           movw    r24, r18
    4c84:       84 1b           sub     r24, r20
    4c86:       95 0b           sbc     r25, r21
    4c88:       81 50           subi    r24, 0x01       ; 1
    4c8a:       9f 4f           sbci    r25, 0xFF       ; 255
    4c8c:       08 95           ret
Wth this commit it gets translated to:
00004c40 <test>:
    4c40:       28 2f           mov     r18, r24
    4c42:       30 e0           ldi     r19, 0x00       ; 0
    4c44:       46 2f           mov     r20, r22
    4c46:       50 e0           ldi     r21, 0x00       ; 0
    4c48:       86 17           cp      r24, r22
    4c4a:       20 f0           brcs    .+8             ; 0x4c54 <test+0x14>
    4c4c:       c9 01           movw    r24, r18
    4c4e:       84 1b           sub     r24, r20
    4c50:       95 0b           sbc     r25, r21
    4c52:       08 95           ret
    4c54:       c9 01           movw    r24, r18
    4c56:       84 1b           sub     r24, r20
    4c58:       95 0b           sbc     r25, r21
    4c5a:       93 95           inc     r25
    4c5c:       08 95           ret
There is not much performance improvement in this case, however at least with this
commit it functions correctly.

Note: The following commit will improve compiler output for the latter example.
2020-04-01 07:45:05 +03:00
..
avr core: Fix bootloader for 128KB flash 2019-12-04 15:09:36 +09:00
chibios make some change to complie stm32_f103_onekey with new version of Chibios (#583) 2018-11-07 08:08:08 +09:00
mbed Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
action.c core: Add AC_BTLD to actionmap and unimap 2019-09-17 20:55:35 +09:00
action.h core: Add utility type_code() in aciton.h #528 2018-01-28 16:17:20 +09:00
action_code.h core: Add AC_BTLD to actionmap and unimap 2019-09-17 20:55:35 +09:00
action_layer.c core: Fix out-of-bounds access by TICK event #487 2017-10-22 03:43:04 +09:00
action_layer.h core: Fix for build option NO_ACTION_LAYER 2017-09-14 12:58:06 +09:00
action_macro.c core: Fix variable initialize 2016-01-15 16:26:01 +09:00
action_macro.h core: Add utility macros in action_macro.h (#532) 2018-01-29 08:10:35 +09:00
action_tapping.c core: Fix for stuck key problem #441 2017-05-30 15:25:26 +09:00
action_tapping.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
action_util.c core: Fix for disabling NKRO in Boot protocol 2015-09-20 14:34:13 +09:00
action_util.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
actionmap.c core: Add unimap support 2016-08-29 13:54:44 +09:00
actionmap.h core: Add short name for keycode KP_00 and KP_000 2019-11-12 15:38:31 +09:00
backlight.c Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
backlight.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
bootloader.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
bootmagic.c Merge commit '71381457fa1311dfa0b58ba882a96db740640871' 2016-04-21 14:37:16 +09:00
bootmagic.h core: Actionmap support 2016-03-03 16:18:19 +09:00
command.c lufa: Print TMK version to console 2019-05-29 23:42:56 +09:00
command.h Fix header guard in tmk_core/common/command.h (#581) 2018-10-31 09:34:01 +09:00
debug.c Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
debug.h Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a' 2016-02-17 08:18:03 +09:00
eeconfig.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
hook.c core: Add hook_process_action() 2019-05-29 23:57:51 +09:00
hook.h core: Add hook_process_action() 2019-05-29 23:57:51 +09:00
host.c Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c' 2016-12-10 10:29:51 +09:00
host.h Merge commit '22b6e15a179031afb7c3534cf7b109b0668b602c' 2016-12-10 10:29:51 +09:00
host_driver.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
keyboard.c core: Typo (mantrix -> matrix) 2018-01-26 16:53:27 +09:00
keyboard.h Add keyboard_setup() and matrix_setup() 2015-05-19 00:39:43 +09:00
keycode.h core: Add short name for keycode KP_00 and KP_000 2019-11-12 15:38:31 +09:00
keymap.c core: Add AC_BTLD to actionmap and unimap 2019-09-17 20:55:35 +09:00
keymap.h Move declaration of keymap_config. 2015-10-12 09:46:48 +01:00
led.h usb_usb: Support locking key indicator LED 2015-07-15 15:13:15 +09:00
matrix.c core: Change matrix_init and matrix_print 2016-10-08 13:13:48 +09:00
matrix.h core: Change matrix_init and matrix_print 2016-10-08 13:13:48 +09:00
mousekey.c Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
mousekey.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
nodebug.h Workaround for compiler warnings when console disabled. 2015-12-03 09:05:09 +00:00
print.c Add ChibiOS support (USB stack + support files). 2015-09-07 21:57:04 +01:00
print.h Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a' 2016-02-17 08:18:03 +09:00
progmem.h Modularity and gcc warnings fixes. 2015-09-07 21:33:06 +01:00
report.h core:adb_usb: Add Extended Mouse Protocol support #274 2019-06-23 15:04:35 +09:00
ringbuf.h core: Add ring buffer file 2018-09-23 12:36:18 +09:00
sendchar.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
sendchar_null.c Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
sendchar_uart.c Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
sleep_led.h core: Fix sleep_led 2016-10-12 14:35:53 +09:00
suspend.h core: Add hook_usb_startup_wait_loop 2019-05-29 14:50:56 +09:00
timer.h tmk_core/common/timer.h: Fixing TIMER_DIFF macro to calculate difference correctly after the timer wraps. 2020-04-01 07:45:05 +03:00
uart.h Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
unimap.c core: Fix unimap translation range 2020-02-03 12:47:01 +09:00
unimap.h core: Fix typo 2017-04-11 15:54:35 +09:00
util.c Merge commit 'a074364c3731d66b56d988c8a6c960a83ea0e0a1' as 'tmk_core' 2015-04-10 01:32:04 +09:00
util.h usb_usb: Add multiple keyboard support 2016-06-21 13:49:06 +09:00
wait.h Add ChibiOS support (USB stack + support files). 2015-09-07 21:57:04 +01:00