Skip to content

Commit 3918ea2

Browse files
committed
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
2 parents 7f87b11 + 8da1898 commit 3918ea2

File tree

7 files changed

+156
-48
lines changed

7 files changed

+156
-48
lines changed

common/action_layer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static void default_layer_state_set(uint32_t state)
2222
debug("default_layer_state: ");
2323
default_layer_debug(); debug(" to ");
2424
default_layer_state = state;
25+
hook_default_layer_change(default_layer_state);
2526
default_layer_debug(); debug("\n");
2627
clear_keyboard_but_mods(); // To avoid stuck keys
2728
}

common/hook.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
#include "hook.h"
2020

2121
/* -------------------------------------------------
22-
* Definitions of hardware-independent default hooks
22+
* Definitions of default hooks
2323
* ------------------------------------------------- */
2424

25-
/* Called on layer state change event. */
26-
/* Default behaviour: do nothing. */
2725
__attribute__((weak))
28-
void hook_layer_change(uint8_t layer_state) {
29-
(void)layer_state;
26+
void hook_keyboard_loop(void) {}
27+
28+
__attribute__((weak))
29+
void hook_matrix_change(keyevent_t event) {
30+
(void)event;
3031
}
3132

32-
/* Called periodically from the matrix scan loop (very often!) */
33-
/* Default behaviour: do nothing. */
3433
__attribute__((weak))
35-
void hook_keyboard_loop(void) {}
34+
void hook_default_layer_change(uint32_t default_layer_state) {
35+
(void)default_layer_state;
36+
}
3637

37-
/* Called on matrix state change event (every keypress => often!) */
38-
/* Default behaviour: do nothing. */
3938
__attribute__((weak))
40-
void hook_matrix_change(keyevent_t event) {
41-
(void)event;
39+
void hook_layer_change(uint32_t layer_state) {
40+
(void)layer_state;
4241
}
4342

44-
/* Called on indicator LED update event (when reported from host). */
45-
/* Default behaviour: calls led_set (for compatibility). */
4643
__attribute__((weak))
4744
void hook_keyboard_leds_change(uint8_t led_status) {
4845
keyboard_set_leds(led_status);
4946
}
5047

51-
/* Called once, on checking the bootmagic combos. */
52-
/* Default behaviour: do nothing. */
5348
__attribute__((weak))
54-
void hook_bootmagic(void) {
55-
/* An example: */
56-
// #include "bootmagic.h"
57-
// #include "keymap.h"
58-
// if(bootmagic_scan_keycode(KC_W)) {
59-
// // do something
60-
// }
61-
}
49+
void hook_bootmagic(void) {}

common/hook.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#include "led.h"
2323

2424
/* -------------------------------------
25-
* Hardware / one-off hooks
25+
* Protocol hooks
2626
* ------------------------------------- */
2727

28-
/* Called once, before initialising USB. */
28+
/* Called once, very early stage of initialization, just after processor startup. */
2929
/* Default behaviour: do nothing. */
3030
void hook_early_init(void);
3131

32-
/* Called once, after USB is connected and keyboard initialised. */
32+
/* Called once, very last stage of initialization, just before keyboard loop. */
3333
/* Default behaviour: do nothing. */
3434
void hook_late_init(void);
3535

@@ -47,12 +47,9 @@ void hook_usb_suspend_loop(void);
4747
* the "normal" indicator LED status by default. */
4848
void hook_usb_wakeup(void);
4949

50-
/* Called once, on checking the bootmagic combos. */
51-
/* Default behaviour: do nothing. */
52-
void hook_bootmagic(void);
5350

5451
/* -------------------------------------
55-
* Keyboard / periodic hooks
52+
* Keyboard hooks
5653
* ------------------------------------- */
5754

5855
/* Called periodically from the keyboard loop (very often!) */
@@ -63,12 +60,21 @@ void hook_keyboard_loop(void);
6360
/* Default behaviour: do nothing. */
6461
void hook_matrix_change(keyevent_t event);
6562

63+
/* Called on default layer state change event. */
64+
/* Default behaviour: do nothing. */
65+
void hook_default_layer_change(uint32_t default_layer_state);
66+
6667
/* Called on layer state change event. */
6768
/* Default behaviour: do nothing. */
68-
void hook_layer_change(uint8_t layer_state);
69+
void hook_layer_change(uint32_t layer_state);
6970

7071
/* Called on indicator LED update event (when reported from host). */
71-
/* Default behaviour: calls keyboard_set_leds (for compatibility). */
72+
/* Default behaviour: calls keyboard_set_leds. */
7273
void hook_keyboard_leds_change(uint8_t led_status);
7374

75+
/* Called once, on checking the bootmagic combos. */
76+
/* Default behaviour: do nothing. */
77+
void hook_bootmagic(void);
78+
79+
7480
#endif /* _HOOKS_H_ */

doc/hook.txt

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
Hooks
2+
-----
3+
Hooks allow you to execute custom code at certain predefined points in the firmware execution. To use them, just define the hook function in your keymap file.
4+
5+
The following hooks are available available:
6+
7+
Hook function | Timing
8+
--------------------------------|-----------------------------------------------
9+
`hook_early_init(void)` | Early in the boot process, before the matrix is initialized and before a connection is made with the host. Thus, this hook has access to very few parameters, but it is a good place to define any custom parameters needed by other early processes.
10+
`hook_late_init(void)` | Near the end of the boot process, after Boot Magic has run and LEDs have been initialized.
11+
`hook_bootmagic(void)` | During the Boot Magic window, after EEPROM and Bootloader checks are made, but before any other built-in Boot Magic checks are made.
12+
`hook_usb_wakeup(void)` | When the device wakes up from USB suspend state.
13+
`hook_usb_suspend_entry(void)` | When the device enters USB suspend state.
14+
`hook_usb_suspend_loop(void)` | Continuously, while the device is in USB suspend state. *Default action:* power down and periodically check the matrix, causing wakeup if needed.
15+
`hook_keyboard_loop(void)` | Continuously, during the main loop, after the matrix is checked.
16+
`hook_matrix_change(keyevent_t event)` | When a matrix state change is detected, before any other actions are processed.
17+
`hook_layer_change(uint32_t layer_state)` | When any layer is changed.
18+
`hook_default_layer_change(uint32_t default_layer_state)` | When any default layer is changed.
19+
`hook_keyboard_leds_change(uint8_t led_status)` | Whenever a change in the LED status is performed. *Default action:* call `keyboard_set_leds(led_status)`
20+
21+
22+
23+
24+
25+
### Hooks Examples
26+
27+
You can try these out by copying the code to your keymap file, or any .c file in the Makefile `SRC`.
28+
29+
#### Activate keymap layer 5 on startup
30+
31+
```C
32+
#include "action_layer.h"
33+
34+
void hook_late_init(void)
35+
{
36+
layer_on(5);
37+
print("Layer 5 enabled!");
38+
}
39+
```
40+
41+
#### Blink the Caps Lock LED every .5 seconds
42+
43+
```C
44+
#include "timer.h"
45+
#include "led.h"
46+
47+
bool my_led_status = 0;
48+
uint16_t my_led_timer;
49+
50+
void hook_keyboard_loop(void)
51+
{
52+
// check if we've reached 500 milliseconds yet...
53+
if (timer_elapsed(my_led_timer) > 500)
54+
{
55+
// we've reached 500 milliseconds!
56+
// reset the timer
57+
my_led_timer = timer_read();
58+
59+
// check the current LED state
60+
if (my_led_status)
61+
{
62+
// LED is on, so let's turn it off
63+
led_set(host_keyboard_leds() & ~(1<<USB_LED_CAPS_LOCK));
64+
my_led_status = 0;
65+
}
66+
else
67+
{
68+
// LED is off, so let's turn it on
69+
led_set(host_keyboard_leds() | (1<<USB_LED_CAPS_LOCK));
70+
my_led_status = 1;
71+
}
72+
}
73+
}
74+
```
75+
76+
#### Flash the Caps Lock LED for 20ms on every keypress
77+
```C
78+
include "timer.h"
79+
#include "led.h"
80+
81+
bool my_led_status = 0;
82+
uint16_t my_led_timer;
83+
84+
void hook_matrix_change(keyevent_t event)
85+
{
86+
// only flash LED for key press events, not key release events.
87+
if (event.pressed)
88+
{
89+
// check the current LED status and reverse it
90+
led_set(host_keyboard_leds() ^ (1<<USB_LED_CAPS_LOCK));
91+
92+
my_led_status = 1;
93+
my_led_timer = timer_read();
94+
}
95+
}
96+
97+
void hook_keyboard_loop(void)
98+
{
99+
if (my_led_status)
100+
{
101+
// check if we've reached 20 milliseconds yet...
102+
if (timer_elapsed(my_led_timer) > 50)
103+
{
104+
led_set(host_keyboard_leds());
105+
106+
my_led_status = 0;
107+
}
108+
}
109+
}
110+
111+
```

protocol/chibios/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ This code can be used to run TMK keyboard logic on top of [ChibiOS], meaning tha
44

55
### Usage
66

7-
- To use, unpack or symlink [ChibiOS] to `tmk_core/tool/chibios/chibios`. For Kinetis support (this means Teensies, Infinity keyboard, WhiteFox keyboard), you'll need a fork which implements the USB driver, e.g. [this one](https://github.com/flabbergast/ChibiOS/tree/kinetis).
7+
- To use, [get a zip of chibios](https://github.com/ChibiOS/ChibiOS/archive/a7df9a891067621e8e1a5c2a2c0ceada82403afe.zip) and unpack/rename it to `tmk_core/tool/chibios/chibios`; or you can just clone [the repo](https://github.com/ChibiOS/ChibiOS) there. For Freescale/NXP Kinetis support (meaning ARM Teensies and the Infinity keyboard), you'll also need [a zip of chibios-contrib](https://github.com/ChibiOS/ChibiOS-Contrib/archive/e1311c4db6cd366cf760673f769e925741ac0ad3.zip), unpacked/renamed to `tmk_core/tool/chibios/chibios-contrib`. Likewise, for git-savvy people, just clone [the repo](https://github.com/ChibiOS/ChibiOS-Contrib) there.
8+
- Note: the abovementioned directories are the defaults. You can have the two chibios repositories wherever you want, just define their location in `CHIBIOS` and `CHIBIOS_CONTRIB` variables in your `Makefile`.
89
- You will also need to install an ARM toolchain, for instance from [here](https://launchpad.net/gcc-arm-embedded). On linux, this is usually also present as a package for your distribution (as `gcc-arm` or something similar). On OS X, you can use [homebrew](http://brew.sh/) with an appropriate tap.
910

1011
### Notes
1112

1213
- Some comments about ChibiOS syntax and the most commonly used GPIO functions are, as well as an example for ARM Teensies, is [here](https://github.com/tmk/tmk_keyboard/blob/master/keyboard/teensy_lc_onekey/instructions.md).
1314
- For gcc options, inspect `tmk_core/tool/chibios/chibios.mk`. For instance, I enabled `-Wno-missing-field-initializers`, because TMK common bits generated a lot of warnings on that.
14-
Also pay attention to `-O0` (enabled for debugging); for deployment use `-O2`.
15+
- For debugging, it is sometimes useful disable gcc optimisations, you can do that by adding `-O0` to `OPT_DEFS` in your `Makefile`.
1516
- USB string descriptors are messy. I did not find a way to cleanly generate the right structures from actual strings, so the definitions in individual keyboards' `config.h` are ugly as heck.
1617
- It is easy to add some code for testing (e.g. blink LED, do stuff on button press, etc...) - just create another thread in `main.c`, it will run independently of the keyboard business.
17-
- Jumping to (the built-in) bootloaders on STM32 works, but it is not entirely pleasant, since it is very much MCU dependent. So, one needs to dig out the right address to jump to, and either pass it to the compiler in the `Makefile`, or better, define it in `<your_kb>/bootloader_defs.h`. An additional startup code is also needed; the best way to deal with this is to define custom board files. (Example forthcoming.)
18+
- Jumping to (the built-in) bootloaders on STM32 works, but it is not entirely pleasant, since it is very much MCU dependent. So, one needs to dig out the right address to jump to, and either pass it to the compiler in the `Makefile`, or better, define it in `<your_kb>/bootloader_defs.h`. An additional startup code is also needed; the best way to deal with this is to define custom board files. (Example forthcoming.) In any case, there are no problems for Teensies.
1819

19-
### Experimental pre-ChibiOS 4 support
20-
- As an alternative to the mentioned flabbergast branch above, you can use the [master branch of ChibiOS](https://github.com/ChibiOS/ChibiOS).
21-
- Note that the Kinetis support has moved to the [ChibiOS-Contrib repository](https://github.com/ChibiOS/ChibiOS-Contrib), so you need to put that into your repository in the same way as you did for the main ChibiOS repository.
22-
- You also need to define CHIBIOS_CONTRIB in your makefile and point it to the right directory.
23-
- You need to add some new options to chconf.h, or you will get compiler errors. Just copy the new options from samples provided by the ChibiOS-Contrib repository.
2420

2521
### Immediate todo
2622

@@ -43,15 +39,16 @@ Also pay attention to `-O0` (enabled for debugging); for deployment use `-O2`.
4339
## ChibiOS-supported MCUs
4440

4541
- Pretty much all STM32 chips.
46-
- There is some support for K20x and KL2x Freescale chips (i.e. Teensy 3.x/LC, mchck, FRDM-KL2{5,6}Z, FRDM-K20D50M), but again, no official USB stack yet. However the `kinetis` branch of [flabbergast's ChibiOS fork](https://github.com/flabbergast/ChibiOS). With this fork, TMK work normally on all the ARM Teensies.
47-
- There is also support for AVR8, but the USB stack is not implemented for them yet, and also the kernel itself takes about 1k of RAM. I think people managed to get ChibiOS running on atmega32[8p/u4] though.
48-
- I've seen community support for Nordic NRF51822 (the chip in Adafruit's Bluefruit bluetooth-low-energy boards), but not sure about the extent.
42+
- K20x and KL2x Freescale/NXP chips (i.e. Teensy 3.x/LC, mchck, FRDM-KL2{5,6}Z, FRDM-K20D50M), via the [ChibiOS-Contrib](https://github.com/ChibiOS/ChibiOS-Contrib) repository.
43+
- There is also support for AVR8, but the USB stack is not implemented for them yet (some news on that front recently though), and also the kernel itself takes about 1k of RAM. I think people managed to get ChibiOS running on atmega32[8p/u4] though.
44+
- There is also support for Nordic NRF51822 (the chip in Adafruit's Bluefruit bluetooth-low-energy boards), but be aware that that chip does *not* have USB, and the BLE softdevice (i.e. Bluetooth) is not supported directly at the moment.
4945

5046
## STM32-based keyboard design considerations
5147

5248
- STM32F0x2 chips can do crystal-less USB, but they still need a 3.3V voltage regulator.
5349
- The BOOT0 pin should be tied to GND.
5450
- For a hardware way of accessing the in-built DFU bootloader, in addition to the reset button, put another button between the BOOT0 pin and 3V3.
51+
- There is a working example of a STM32F042-based keyboard: [firmware here](https://github.com/flabbergast/flabber_kbs/tree/master/kb45p) and [hardware (kicad) here](https://github.com/flabbergast/kicad/tree/master/kb45p). You can check this example firmware for custom board files, and a more complicated matrix than just one key.
5552

5653

5754

tool/chibios/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
chibios
2+
chibios-contrib

tool/chibios/chibios.mk

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ endif
8484

8585
# Imported source files and paths
8686
CHIBIOS ?= $(TMK_DIR)/tool/chibios/chibios
87+
CHIBIOS_CONTRIB ?= $(TMK_DIR)/tool/chibios/chibios-contrib
8788
# Startup files. Try a few different locations, for compability with old versions and
8889
# for things hardware in the contrib repository
8990
STARTUP_MK = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/startup_$(MCU_STARTUP).mk
@@ -122,6 +123,7 @@ PORT_V = $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v$(ARMV)m.mk
122123
endif
123124
include $(PORT_V)
124125
# Other files (optional).
126+
include $(CHIBIOS)/os/hal/lib/streams/streams.mk
125127

126128
# Define linker script file here
127129
ifneq ("$(wildcard $(TARGET_DIR)/ld/$(MCU_LDSCRIPT).ld)","")
@@ -139,7 +141,7 @@ CSRC = $(STARTUPSRC) \
139141
$(HALSRC) \
140142
$(PLATFORMSRC) \
141143
$(BOARDSRC) \
142-
$(CHIBIOS)/os/hal/lib/streams/chprintf.c \
144+
$(STREAMSSRC) \
143145
$(TMK_DIR)/protocol/chibios/usb_main.c \
144146
$(TMK_DIR)/protocol/chibios/main.c \
145147
$(SRC)
@@ -169,11 +171,13 @@ TCSRC =
169171
TCPPSRC =
170172

171173
# List ASM source files here
172-
ASMSRC = $(STARTUPASM) $(PORTASM) $(OSALASM)
174+
ASMSRC =
175+
ASMXSRC = $(STARTUPASM) $(PORTASM) $(OSALASM)
173176

174-
INCDIR = $(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
177+
INCDIR = $(CHIBIOS)/os/license \
178+
$(STARTUPINC) $(KERNINC) $(PORTINC) $(OSALINC) \
175179
$(HALINC) $(PLATFORMINC) $(BOARDINC) $(TESTINC) \
176-
$(CHIBIOS)/os/hal/lib/streams $(CHIBIOS)/os/various \
180+
$(STREAMSINC) $(CHIBIOS)/os/various \
177181
$(TMK_DIR) $(COMMON_DIR) $(TMK_DIR)/protocol/chibios \
178182
$(TMK_DIR)/protocol $(TARGET_DIR)
179183

0 commit comments

Comments
 (0)