diff --git a/keyboard/chibi_onekey/Makefile b/keyboard/chibi_onekey/Makefile index 2d5ff8cd..2c5af44b 100644 --- a/keyboard/chibi_onekey/Makefile +++ b/keyboard/chibi_onekey/Makefile @@ -37,12 +37,16 @@ MCU_STARTUP = stm32f0xx BOARD = ST_STM32F072B_DISCOVERY # ARM version, CORTEX-M0/M1 are 6, CORTEX-M3/M4/M7 are 7 ARMV = 6 -# If you want to be able to jump to bootloader from firmware (on STM32 MCUs), -# set the correct BOOTLOADER_ADDRESS here. Otherwise leave commented out. +# If you want to be able to jump to bootloader from firmware on STM32 MCUs, +# set the correct BOOTLOADER_ADDRESS. Either set it here, or define it in +# ./bootloader_defs.h or in ./boards//bootloader_defs.h (if you have +# a custom board definition that you plan to reuse). +# If you're not setting it here, leave it commented out. # It is chip dependent, the correct number can be looked up here (page 175): # http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf -# This also requires a patch to chibios: /tmk_core/ -BOOTLOADER_ADDRESS = 0x1FFFC800 +# This also requires a patch to chibios: +# /tmk_core/tool/chibios/ch-bootloader-jump.patch +#STM32_BOOTLOADER_ADDRESS = 0x1FFFC800 # Build Options # comment out to disable the options. diff --git a/keyboard/chibi_onekey/bootloader_defs.h b/keyboard/chibi_onekey/bootloader_defs.h new file mode 100644 index 00000000..02c48c4e --- /dev/null +++ b/keyboard/chibi_onekey/bootloader_defs.h @@ -0,0 +1,7 @@ +/* Address for jumping to bootloader on STM32 chips. */ +/* It is chip dependent, the correct number can be looked up here (page 175): + * http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf + * This also requires a patch to chibios: + * /tmk_core/tool/chibios/ch-bootloader-jump.patch + */ +#define STM32_BOOTLOADER_ADDRESS 0x1FFFC800 diff --git a/tmk_core/common/chibios/bootloader.c b/tmk_core/common/chibios/bootloader.c index 93f2e378..f36eced4 100644 --- a/tmk_core/common/chibios/bootloader.c +++ b/tmk_core/common/chibios/bootloader.c @@ -2,7 +2,7 @@ #include "ch.h" -#ifdef BOOTLOADER_ADDRESS +#ifdef STM32_BOOTLOADER_ADDRESS #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) extern uint32_t __ram0_end__; @@ -10,7 +10,7 @@ void bootloader_jump(void) { *((unsigned long *)(SYMVAL(__ram0_end__) - 4)) = 0xDEADBEEF; // set magic flag => reset handler will jump into boot loader NVIC_SystemReset(); } -#else /* BOOTLOADER_ADDRESS */ +#else /* STM32_BOOTLOADER_ADDRESS */ void bootloader_jump(void) {} -#endif /* BOOTLOADER_ADDRESS */ +#endif /* STM32_BOOTLOADER_ADDRESS */ diff --git a/tmk_core/protocol/chibios/README.md b/tmk_core/protocol/chibios/README.md index 06569c51..676a5f4f 100644 --- a/tmk_core/protocol/chibios/README.md +++ b/tmk_core/protocol/chibios/README.md @@ -2,13 +2,13 @@ ### Notes -- To use, unpack or symlink [ChibiOS] {currently 3.0.1} to `tmk_core/tool/chibios/chibios`. +- To use, unpack or symlink [ChibiOS] {currently 3.0.2} to `tmk_core/tool/chibios/chibios`. - 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. Also pay attention to `-O0` (enabled for debugging); for deployment use `-O2`. - 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. - There are some random constants left so far, e.g. 5ms sleep between calling `keyboard_task`, or 1.5sec wait for USB init, in `main.c`. There should be no such in `usb_main.c` (the main USB stack). Everything is based on timers/interrupts/kernel scheduling (well except `keyboard_task`), so no periodically called things (again, except `keyboard_task`, which is just how TMK is designed). - 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. -- Jumping to bootloader works, but it is not entirely pleasant, since it is very much MCU dependent. The code is now geared towards STM32 chips and their built-in bootloaders. So, one needs to dig out the right address to jump to, and pass it to the compiler in the `Makefile`. Also, a patch to upstream ChibiOS is needed (supplied), because it `ResetHandler` needs adjusting. +- Jumping to bootloader works, but it is not entirely pleasant, since it is very much MCU dependent. The code is now geared towards STM32 chips and their built-in bootloaders. 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 `/bootloader_defs.h`. Also, a patch to upstream ChibiOS is needed (supplied), because it `ResetHandler` needs adjusting. - Sleep LED works, but at the moment only on/off, i.e. no breathing. - The USB stack works pretty completely; however there are bits of other TMK stuff that are not done yet: @@ -28,7 +28,7 @@ Also pay attention to `-O0` (enabled for debugging); for deployment use `-O2`. ### Tried with -- ChibiOS 3.0.1 and ST F072RB DISCOVERY board. +- ChibiOS 3.0.1, 3.0.2 and ST F072RB DISCOVERY board. - Need to test on other STM32 chips (F3, F4) to make it as much chip-independent as possible. ## STM32-based keyboard design considerations @@ -36,13 +36,13 @@ Also pay attention to `-O0` (enabled for debugging); for deployment use `-O2`. - STM32F0x2 chips can do crystal-less USB, but they still need a 3.3V voltage regulator. - The BOOT0 pin should be tied to GND. - 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. -- For breathing the caps lock LED during the suspended state ("sleep LED"), it is desirable to have that LED on a hardware PWM pin (there's usually plenty of those, look for TIMERs in the datasheet). +- For breathing the caps lock LED during the suspended state ("sleep LED"), it is desirable to have that LED on a hardware PWM pin (there's usually plenty of those, look for TIMERs in the datasheet). However this is not strictly necessary, because instead of direct output of a timer to a pin (better of course), it is easy to define timer callbacks in ChibiOS that turn on/off an arbitrary pin. -## ChibiOS-supported MCUs (as of 3.0.1) +## ChibiOS-supported MCUs (as of 3.0.2) - Pretty much all STM32 chips. - 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. -- There is some support for K20 and KL25 Freescale chips (i.e. Teensy 3.0, mchck, FRDM-KL25Z, FRDM-K20D50M), but again, no USB stack yet. +- There is some support for K20x and KL2x Freescale chips (i.e. Teensy 3.0, mchck, FRDM-KL25Z, FRDM-K20D50M), but again, no official USB stack yet. This is being worked on, see the `kinetis` branch of [my ChibiOS fork](https://github.com/flabbergast/ChibiOS). It supports also Teensy LC, 3.1 and FRDM-KL26Z. - I've seen community support for Nordic NRF51822 (the chip in Adafruit's Bluefruit bluetooth-low-energy boards), but not sure about the extent. diff --git a/tmk_core/tool/chibios/ch-bootloader-jump.patch b/tmk_core/tool/chibios/ch-bootloader-jump.patch index 7f33e8ac..d8865762 100644 --- a/tmk_core/tool/chibios/ch-bootloader-jump.patch +++ b/tmk_core/tool/chibios/ch-bootloader-jump.patch @@ -1,8 +1,8 @@ diff --git a/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s b/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s -index 38b4513..12a3f39 100644 +index 51a79bb..42d07bd 100644 --- a/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s +++ b/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s -@@ -98,6 +98,13 @@ +@@ -105,6 +105,13 @@ #define CRT0_CALL_DESTRUCTORS TRUE #endif @@ -16,12 +16,12 @@ index 38b4513..12a3f39 100644 /*===========================================================================*/ /* Code section. */ /*===========================================================================*/ -@@ -117,6 +124,17 @@ +@@ -124,6 +131,17 @@ .thumb_func .global Reset_Handler Reset_Handler: + -+#ifdef BOOTLOADER_ADDRESS ++#ifdef STM32_BOOTLOADER_ADDRESS + /* jump to bootloader code */ + ldr r0, =__ram0_end__-4 + ldr r1, =MAGIC_BOOTLOADER_NUMBER @@ -29,16 +29,16 @@ index 38b4513..12a3f39 100644 + str r0, [r0, #0] /* erase stored magic */ + cmp r2, r1 + beq Bootloader_Jump -+#endif /* BOOTLOADER_ADDRESS */ ++#endif /* STM32_BOOTLOADER_ADDRESS */ + /* Interrupts are globally masked initially.*/ cpsid i -@@ -230,6 +248,21 @@ endfiniloop: +@@ -242,6 +260,21 @@ endfiniloop: ldr r1, =__default_exit bx r1 -+#ifdef BOOTLOADER_ADDRESS ++#ifdef STM32_BOOTLOADER_ADDRESS +/* + * Jump-to-bootloader function. + */ @@ -46,21 +46,21 @@ index 38b4513..12a3f39 100644 + .align 2 + .thumb_func +Bootloader_Jump: -+ ldr r0, =BOOTLOADER_ADDRESS ++ ldr r0, =STM32_BOOTLOADER_ADDRESS + ldr r1, [r0, #0] + mov sp, r1 + ldr r0, [r0, #4] + bx r0 -+#endif /* BOOTLOADER_ADDRESS */ ++#endif /* STM32_BOOTLOADER_ADDRESS */ + #endif /** @} */ diff --git a/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s b/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s -index fcfa4de..2d560da 100644 +index 4812a29..dca9f88 100644 --- a/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s +++ b/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s -@@ -133,6 +133,13 @@ +@@ -140,6 +140,13 @@ #define CRT0_CPACR_INIT 0x00F00000 #endif @@ -74,11 +74,12 @@ index fcfa4de..2d560da 100644 /*===========================================================================*/ /* Code section. */ /*===========================================================================*/ -@@ -157,6 +164,16 @@ +@@ -164,6 +171,17 @@ .thumb_func .global Reset_Handler Reset_Handler: -+#ifdef BOOTLOADER_ADDRESS ++ ++#ifdef STM32_BOOTLOADER_ADDRESS + /* jump to bootloader code */ + ldr r0, =__ram0_end__-4 + ldr r1, =MAGIC_BOOTLOADER_NUMBER @@ -86,16 +87,16 @@ index fcfa4de..2d560da 100644 + str r0, [r0, #0] /* erase stored magic */ + cmp r2, r1 + beq Bootloader_Jump -+#endif /* BOOTLOADER_ADDRESS */ ++#endif /* STM32_BOOTLOADER_ADDRESS */ + /* Interrupts are globally masked initially.*/ cpsid i -@@ -289,6 +306,21 @@ endfiniloop: +@@ -305,6 +323,21 @@ endfiniloop: /* Branching to the defined exit handler.*/ b __default_exit -+#ifdef BOOTLOADER_ADDRESS ++#ifdef STM32_BOOTLOADER_ADDRESS +/* + * Jump-to-bootloader function. + */ @@ -103,12 +104,12 @@ index fcfa4de..2d560da 100644 + .align 2 + .thumb_func +Bootloader_Jump: -+ ldr r0, =BOOTLOADER_ADDRESS ++ ldr r0, =STM32_BOOTLOADER_ADDRESS + ldr r1, [r0, #0] + mov sp, r1 + ldr r0, [r0, #4] + bx r0 -+#endif /* BOOTLOADER_ADDRESS */ ++#endif /* STM32_BOOTLOADER_ADDRESS */ + #endif /* !defined(__DOXYGEN__) */ diff --git a/tmk_core/tool/chibios/chibios.mk b/tmk_core/tool/chibios/chibios.mk index 6ba783e3..bc8c61b3 100644 --- a/tmk_core/tool/chibios/chibios.mk +++ b/tmk_core/tool/chibios/chibios.mk @@ -207,6 +207,14 @@ UDEFS = $(OPT_DEFS) # Define ASM defines here UADEFS = $(OPT_DEFS) +# bootloader definitions may be used in the startup .s file +ifneq ("$(wildcard $(TARGET_DIR)/bootloader_defs.h)","") + UADEFS += -include $(TARGET_DIR)/bootloader_defs.h + UDEFS += -include $(TARGET_DIR)/bootloader_defs.h +else ifneq ("$(wildcard $(TARGET_DIR)/boards/$(BOARD)/bootloader_defs.h)","") + UADEFS += -include $(TARGET_DIR)/boards/$(BOARD)/bootloader_defs.h + UDEFS += -include $(TARGET_DIR)/boards/$(BOARD)/bootloader_defs.h +endif # List all user directories here UINCDIR =