BOARD = ST_STM32F072B_DISCOVERY | BOARD = ST_STM32F072B_DISCOVERY | ||||
# ARM version, CORTEX-M0/M1 are 6, CORTEX-M3/M4/M7 are 7 | # ARM version, CORTEX-M0/M1 are 6, CORTEX-M3/M4/M7 are 7 | ||||
ARMV = 6 | 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/<FOO>/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): | # 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 | # http://www.st.com/web/en/resource/technical/document/application_note/CD00167594.pdf | ||||
# This also requires a patch to chibios: <tmk_dir>/tmk_core/ | |||||
BOOTLOADER_ADDRESS = 0x1FFFC800 | |||||
# This also requires a patch to chibios: | |||||
# <tmk_dir>/tmk_core/tool/chibios/ch-bootloader-jump.patch | |||||
#STM32_BOOTLOADER_ADDRESS = 0x1FFFC800 | |||||
# Build Options | # Build Options | ||||
# comment out to disable the options. | # comment out to disable the options. |
/* 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_dir>/tmk_core/tool/chibios/ch-bootloader-jump.patch | |||||
*/ | |||||
#define STM32_BOOTLOADER_ADDRESS 0x1FFFC800 |
#include "ch.h" | #include "ch.h" | ||||
#ifdef BOOTLOADER_ADDRESS | |||||
#ifdef STM32_BOOTLOADER_ADDRESS | |||||
#define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) | #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0)) | ||||
extern uint32_t __ram0_end__; | extern uint32_t __ram0_end__; | ||||
*((unsigned long *)(SYMVAL(__ram0_end__) - 4)) = 0xDEADBEEF; // set magic flag => reset handler will jump into boot loader | *((unsigned long *)(SYMVAL(__ram0_end__) - 4)) = 0xDEADBEEF; // set magic flag => reset handler will jump into boot loader | ||||
NVIC_SystemReset(); | NVIC_SystemReset(); | ||||
} | } | ||||
#else /* BOOTLOADER_ADDRESS */ | |||||
#else /* STM32_BOOTLOADER_ADDRESS */ | |||||
void bootloader_jump(void) {} | void bootloader_jump(void) {} | ||||
#endif /* BOOTLOADER_ADDRESS */ | |||||
#endif /* STM32_BOOTLOADER_ADDRESS */ | |||||
### Notes | ### 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. | - 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`. | 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. | - 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). | - 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. | - 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 `<your_kb>/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. | - 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: | - The USB stack works pretty completely; however there are bits of other TMK stuff that are not done yet: | ||||
### Tried with | ### 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. | - Need to test on other STM32 chips (F3, F4) to make it as much chip-independent as possible. | ||||
## STM32-based keyboard design considerations | ## STM32-based keyboard design considerations | ||||
- STM32F0x2 chips can do crystal-less USB, but they still need a 3.3V voltage regulator. | - STM32F0x2 chips can do crystal-less USB, but they still need a 3.3V voltage regulator. | ||||
- The BOOT0 pin should be tied to GND. | - 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 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. | - 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 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. | - 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/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s b/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s | 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 | --- a/os/common/ports/ARMCMx/compilers/GCC/crt0_v6m.s | ||||
+++ b/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 | #define CRT0_CALL_DESTRUCTORS TRUE | ||||
#endif | #endif | ||||
/*===========================================================================*/ | /*===========================================================================*/ | ||||
/* Code section. */ | /* Code section. */ | ||||
/*===========================================================================*/ | /*===========================================================================*/ | ||||
@@ -117,6 +124,17 @@ | |||||
@@ -124,6 +131,17 @@ | |||||
.thumb_func | .thumb_func | ||||
.global Reset_Handler | .global Reset_Handler | ||||
Reset_Handler: | Reset_Handler: | ||||
+ | + | ||||
+#ifdef BOOTLOADER_ADDRESS | |||||
+#ifdef STM32_BOOTLOADER_ADDRESS | |||||
+ /* jump to bootloader code */ | + /* jump to bootloader code */ | ||||
+ ldr r0, =__ram0_end__-4 | + ldr r0, =__ram0_end__-4 | ||||
+ ldr r1, =MAGIC_BOOTLOADER_NUMBER | + ldr r1, =MAGIC_BOOTLOADER_NUMBER | ||||
+ str r0, [r0, #0] /* erase stored magic */ | + str r0, [r0, #0] /* erase stored magic */ | ||||
+ cmp r2, r1 | + cmp r2, r1 | ||||
+ beq Bootloader_Jump | + beq Bootloader_Jump | ||||
+#endif /* BOOTLOADER_ADDRESS */ | |||||
+#endif /* STM32_BOOTLOADER_ADDRESS */ | |||||
+ | + | ||||
/* Interrupts are globally masked initially.*/ | /* Interrupts are globally masked initially.*/ | ||||
cpsid i | cpsid i | ||||
@@ -230,6 +248,21 @@ endfiniloop: | |||||
@@ -242,6 +260,21 @@ endfiniloop: | |||||
ldr r1, =__default_exit | ldr r1, =__default_exit | ||||
bx r1 | bx r1 | ||||
+#ifdef BOOTLOADER_ADDRESS | |||||
+#ifdef STM32_BOOTLOADER_ADDRESS | |||||
+/* | +/* | ||||
+ * Jump-to-bootloader function. | + * Jump-to-bootloader function. | ||||
+ */ | + */ | ||||
+ .align 2 | + .align 2 | ||||
+ .thumb_func | + .thumb_func | ||||
+Bootloader_Jump: | +Bootloader_Jump: | ||||
+ ldr r0, =BOOTLOADER_ADDRESS | |||||
+ ldr r0, =STM32_BOOTLOADER_ADDRESS | |||||
+ ldr r1, [r0, #0] | + ldr r1, [r0, #0] | ||||
+ mov sp, r1 | + mov sp, r1 | ||||
+ ldr r0, [r0, #4] | + ldr r0, [r0, #4] | ||||
+ bx r0 | + bx r0 | ||||
+#endif /* BOOTLOADER_ADDRESS */ | |||||
+#endif /* STM32_BOOTLOADER_ADDRESS */ | |||||
+ | + | ||||
#endif | #endif | ||||
/** @} */ | /** @} */ | ||||
diff --git a/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s b/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s | 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 | --- a/os/common/ports/ARMCMx/compilers/GCC/crt0_v7m.s | ||||
+++ b/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 | #define CRT0_CPACR_INIT 0x00F00000 | ||||
#endif | #endif | ||||
/*===========================================================================*/ | /*===========================================================================*/ | ||||
/* Code section. */ | /* Code section. */ | ||||
/*===========================================================================*/ | /*===========================================================================*/ | ||||
@@ -157,6 +164,16 @@ | |||||
@@ -164,6 +171,17 @@ | |||||
.thumb_func | .thumb_func | ||||
.global Reset_Handler | .global Reset_Handler | ||||
Reset_Handler: | Reset_Handler: | ||||
+#ifdef BOOTLOADER_ADDRESS | |||||
+ | |||||
+#ifdef STM32_BOOTLOADER_ADDRESS | |||||
+ /* jump to bootloader code */ | + /* jump to bootloader code */ | ||||
+ ldr r0, =__ram0_end__-4 | + ldr r0, =__ram0_end__-4 | ||||
+ ldr r1, =MAGIC_BOOTLOADER_NUMBER | + ldr r1, =MAGIC_BOOTLOADER_NUMBER | ||||
+ str r0, [r0, #0] /* erase stored magic */ | + str r0, [r0, #0] /* erase stored magic */ | ||||
+ cmp r2, r1 | + cmp r2, r1 | ||||
+ beq Bootloader_Jump | + beq Bootloader_Jump | ||||
+#endif /* BOOTLOADER_ADDRESS */ | |||||
+#endif /* STM32_BOOTLOADER_ADDRESS */ | |||||
+ | + | ||||
/* Interrupts are globally masked initially.*/ | /* Interrupts are globally masked initially.*/ | ||||
cpsid i | cpsid i | ||||
@@ -289,6 +306,21 @@ endfiniloop: | |||||
@@ -305,6 +323,21 @@ endfiniloop: | |||||
/* Branching to the defined exit handler.*/ | /* Branching to the defined exit handler.*/ | ||||
b __default_exit | b __default_exit | ||||
+#ifdef BOOTLOADER_ADDRESS | |||||
+#ifdef STM32_BOOTLOADER_ADDRESS | |||||
+/* | +/* | ||||
+ * Jump-to-bootloader function. | + * Jump-to-bootloader function. | ||||
+ */ | + */ | ||||
+ .align 2 | + .align 2 | ||||
+ .thumb_func | + .thumb_func | ||||
+Bootloader_Jump: | +Bootloader_Jump: | ||||
+ ldr r0, =BOOTLOADER_ADDRESS | |||||
+ ldr r0, =STM32_BOOTLOADER_ADDRESS | |||||
+ ldr r1, [r0, #0] | + ldr r1, [r0, #0] | ||||
+ mov sp, r1 | + mov sp, r1 | ||||
+ ldr r0, [r0, #4] | + ldr r0, [r0, #4] | ||||
+ bx r0 | + bx r0 | ||||
+#endif /* BOOTLOADER_ADDRESS */ | |||||
+#endif /* STM32_BOOTLOADER_ADDRESS */ | |||||
+ | + | ||||
#endif /* !defined(__DOXYGEN__) */ | #endif /* !defined(__DOXYGEN__) */ | ||||
# Define ASM defines here | # Define ASM defines here | ||||
UADEFS = $(OPT_DEFS) | 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 | # List all user directories here | ||||
UINCDIR = | UINCDIR = |