aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/chibios/printf.c
diff options
context:
space:
mode:
authorfredizzimo <fsundvik@gmail.com>2016-07-01 17:04:53 +0300
committerJack Humbert <jack.humb@gmail.com>2016-07-01 10:04:53 -0400
commit4d4f7684e684bec319f166121463a88cd4a62703 (patch)
tree4c04ce03e49356283925556342845b1bd656e930 /tmk_core/common/chibios/printf.c
parenta8b55f8c858933a0f7641f7bf2dede640235aa70 (diff)
downloadqmk_firmware-4d4f7684e684bec319f166121463a88cd4a62703.tar.gz
qmk_firmware-4d4f7684e684bec319f166121463a88cd4a62703.zip
Add ChibiOS support for QMK (#465)
* Modularity and gcc warnings fixes. * Add ChibiOS support (USB stack + support files). * Make usb_main more USB_DRIVER #define independent. * Move chibios to tool. * Implement jump-to-bootloader. * Small updates. * Fix bootloader-jump compiling. * Move AVR specific sleep_led.c into avr. * Add basic sleep_led for chibios. * Update chibios README. * NKRO fixes. * Rename some Makefile defines. * Move STM32 bootloader address config to separate .h file. * Add ARM Teensies bootloader code. * Fix chibios/usb_main GET_REPORT handing. * Add missing #include to keymap.c. * Make bootmagic.c code portable (_delay_ms -> wait_ms). * Move declaration of keymap_config. Should really not declare variables in .h files - since it's included in different .c files, a proper linker then complains that the same variable is declared more than once (once for each .c file that the offending .h is included in). * Add eeprom support for chibios/kinetis. * Rename chibios example keyboard. * Move chibios/cortex selection to local Makefiles. * Chibios: use WFI in idle. WIP suspend stuff. * ChibiOS/kinetis: sending remote wakeup. * ChibiOS/STM32: send remote wakeup. * Fix report size of boot protocol. * Fix drop key stroke Keyboard report should be checked if its transfer finishs successfully. Otherwise key stroke can be missing when other key event occurs before the last report transfer is done. Boot protocol 10ms interval probably causes this problem in case it receives key events in a row within the period. NKRO protocol suffers less or nothing due to its interval 1ms. * Chibios/usb_main: rename a variable for clarity. * Add correct chibios/bootloader_jump for infinity KB. * ChibiOS: make reset request more CMSISy. * Chibios: Add breathing sleep LED on Kinetis MCUs. * ChibiOS: Update infinity bootloader code to match updated ChibiOS. * ChibiOS: prettify/document sleep_led code. * Chibios: Remove the wait in the main loop. * Add maple mini code. * Do timeout when writing to CONSOLE EP queue. Fixes TMK bug #266. * Chibios: add 'core/protocol' to the makefiles' search path. * Chibios: Update to new USB API. * Chibios: add more guards for transmitting (fix a deadlock bug). * Add update for chibios in README * Chibios: Fix a HardFault bug (wait after start). * Chibios: cleanup usb_main code. * Chibios: Revert common.mk change (fix AVR linking problem). * core: Fix chibios user compile options Compile options can be defined in project Makefile such as UDEFS, UADEFS, UINCDIR, ULIBDIR and ULIBS. * Sysv format for ChibiOS arm-none-eabi-size Some new patches to ChibiOS puts heap as it's own section. So the berkeley format is now useless, as the heap will be included in the BSS report. The sysv format displays the bss size correctly. * Fix hard-coded path of CHIBIOS * Add support for new version of ChibiOS and Contrib The Kinetis support has moved to a separate Contrib repository in the newest version of Chibios. There has also been some structure changes. So this adds support for those, while maintaining back- wards compability. * Update ChibiOS instructions * Chibios: implement sleep LED for STM32. * Chibios: Update the main chibios README. * Chibios: fix STM32_BOOTLOADER_ADDRESS name. * Chibios: make the default bootloader_jump redefinable (weak). * Chibios: disable LTO (link-time optimisation). With LTO enabled, sometimes things fail for mysterious reasons (e.g. bootloader jump on WF with LEDs enabled), just because the linker optimisation is too aggressive. * Chibios: add default location for chibios-contrib. * ChibiOS: update mk to match chibios/master. * ChibiOS: update instructions.md. * Add chibi_onekey example. * Add comments to chibi_onekey Makefile. * Rename some Makefile defines. * Move STM32 bootloader address config to separate .h file. * Rename chibios example keyboard. * Move chibios/cortex selection to local Makefiles. * Add Teensy LC onekey example. * Chibios: use WFI in idle. WIP suspend stuff. * Update chibi/teensy instructions. * Update chibios/Teensy instructions. * Add infinity_chibios * Add keymap_hasu.c * Infinity_chibios: select correct bootloader_jump. * Infinity_chibios: improve comments. * Add generic STM32F103C8T6 example. * Add maple mini code. * STM32F103x fixes. * Add maple mini pinout pic. * Chibios: updates for 3.0.4 git. * Chibios: rename example stm32_onekey -> stm32_f072_onekey. * Chibios: add makefiles for Teensy 3.x examples. * Chibios: update Teensy 3.x instructions. * Chibios: Tsy LC is cortex-m0plus. * Chibios: add more guards for transmitting (fix a deadlock bug). * Change README for chibios * Chibios: update examples to current chibios git. Match the changes in mainline chibios: - update chconf.h - update supplied ld scripts structure - update Teensy instructions (switch to official chibios and introduce contrib) * Add ChibiOS and ChibiOS-Contrib submodules Also fix the makefile path for them. * Moves chibios keyboards to keyboards folder * First version of ChibiOS compilation Only the stm32_f072_onkey keyboard is ported at the moment. It compiles, but still doesn't link. * More chibios fixes It now compiles without warnings and links * Move the teensy_lc_onekey to the keyboards folder * Clean up the make file rule structure * Remove keymap_fn_to_action * Update more ChibiOS keyboards to QMK Most of them does not compile at the moment though. * Use older version of Chibios libraries The newest ones have problems with compilation * Remove USB_UNCONFIGURED event It isn't present in the older version of ChibiOS * Fix the infinity_chibios compilation * Fix potentially uninitialized variable * Add missing include * Fix the ChibiOS makefile * Fix some Chibios keyboard compilation * Revert the rules.mk file back to master version * Combine the chibios and AVR makefiles With just the required overrides in the respective platform specific one. * Slight makefile restrucuring Platform specific compiler options * Move avr specific targets out of the main rules * Fix ChibiOS objcopy The ChibiOS objcopy needs different parameters, so the parameters are moved to the corresponding platform rule file * Fix the objcopy for real this time The comands were moved around, so chibios used avr and the ohter way around. Also change the objsize output format * Fix the thumb flags * Fix the infinity hasu keymap * Per platform cpp flags * Add gcc-arm-none-eabi package to travis * Add arm-none-eabi-newlib to travis * Fix the name of the libnewlib-arm-none-eabi lib * Fix the ChibiOS paths So that they are properly relative, and builds don't generate extra folders * Fix the board path of stm32_f103_onekey * Only consider folders with Makefiles as subproject
Diffstat (limited to 'tmk_core/common/chibios/printf.c')
-rw-r--r--tmk_core/common/chibios/printf.c240
1 files changed, 240 insertions, 0 deletions
diff --git a/tmk_core/common/chibios/printf.c b/tmk_core/common/chibios/printf.c
new file mode 100644
index 000000000..72e3d4f8c
--- /dev/null
+++ b/tmk_core/common/chibios/printf.c
@@ -0,0 +1,240 @@
1/*
2 * found at: http://www.sparetimelabs.com/tinyprintf/tinyprintf.php
3 * and: http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
4 */
5
6/*
7File: printf.c
8
9Copyright (C) 2004 Kustaa Nyholm
10
11This library is free software; you can redistribute it and/or
12modify it under the terms of the GNU Lesser General Public
13License as published by the Free Software Foundation; either
14version 2.1 of the License, or (at your option) any later version.
15
16This library is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public
22License along with this library; if not, write to the Free Software
23Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25*/
26
27#include "printf.h"
28
29typedef void (*putcf) (void*,char);
30static putcf stdout_putf;
31static void* stdout_putp;
32
33// this adds cca 400 bytes
34#define PRINTF_LONG_SUPPORT
35
36#ifdef PRINTF_LONG_SUPPORT
37
38static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf)
39 {
40 int n=0;
41 unsigned int d=1;
42 while (num/d >= base)
43 d*=base;
44 while (d!=0) {
45 int dgt = num / d;
46 num%=d;
47 d/=base;
48 if (n || dgt>0|| d==0) {
49 *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
50 ++n;
51 }
52 }
53 *bf=0;
54 }
55
56static void li2a (long num, char * bf)
57 {
58 if (num<0) {
59 num=-num;
60 *bf++ = '-';
61 }
62 uli2a(num,10,0,bf);
63 }
64
65#endif
66
67static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
68 {
69 int n=0;
70 unsigned int d=1;
71 while (num/d >= base)
72 d*=base;
73 while (d!=0) {
74 int dgt = num / d;
75 num%= d;
76 d/=base;
77 if (n || dgt>0 || d==0) {
78 *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
79 ++n;
80 }
81 }
82 *bf=0;
83 }
84
85static void i2a (int num, char * bf)
86 {
87 if (num<0) {
88 num=-num;
89 *bf++ = '-';
90 }
91 ui2a(num,10,0,bf);
92 }
93
94static int a2d(char ch)
95 {
96 if (ch>='0' && ch<='9')
97 return ch-'0';
98 else if (ch>='a' && ch<='f')
99 return ch-'a'+10;
100 else if (ch>='A' && ch<='F')
101 return ch-'A'+10;
102 else return -1;
103 }
104
105static char a2i(char ch, char** src,int base,int* nump)
106 {
107 char* p= *src;
108 int num=0;
109 int digit;
110 while ((digit=a2d(ch))>=0) {
111 if (digit>base) break;
112 num=num*base+digit;
113 ch=*p++;
114 }
115 *src=p;
116 *nump=num;
117 return ch;
118 }
119
120static void putchw(void* putp,putcf putf,int n, char z, char* bf)
121 {
122 char fc=z? '0' : ' ';
123 char ch;
124 char* p=bf;
125 while (*p++ && n > 0)
126 n--;
127 while (n-- > 0)
128 putf(putp,fc);
129 while ((ch= *bf++))
130 putf(putp,ch);
131 }
132
133void tfp_format(void* putp,putcf putf,char *fmt, va_list va)
134 {
135 char bf[12];
136
137 char ch;
138
139
140 while ((ch=*(fmt++))) {
141 if (ch!='%')
142 putf(putp,ch);
143 else {
144 char lz=0;
145#ifdef PRINTF_LONG_SUPPORT
146 char lng=0;
147#endif
148 int w=0;
149 ch=*(fmt++);
150 if (ch=='0') {
151 ch=*(fmt++);
152 lz=1;
153 }
154 if (ch>='0' && ch<='9') {
155 ch=a2i(ch,&fmt,10,&w);
156 }
157#ifdef PRINTF_LONG_SUPPORT
158 if (ch=='l') {
159 ch=*(fmt++);
160 lng=1;
161 }
162#endif
163 switch (ch) {
164 case 0:
165 goto abort;
166 case 'u' : {
167#ifdef PRINTF_LONG_SUPPORT
168 if (lng)
169 uli2a(va_arg(va, unsigned long int),10,0,bf);
170 else
171#endif
172 ui2a(va_arg(va, unsigned int),10,0,bf);
173 putchw(putp,putf,w,lz,bf);
174 break;
175 }
176 case 'd' : {
177#ifdef PRINTF_LONG_SUPPORT
178 if (lng)
179 li2a(va_arg(va, unsigned long int),bf);
180 else
181#endif
182 i2a(va_arg(va, int),bf);
183 putchw(putp,putf,w,lz,bf);
184 break;
185 }
186 case 'x': case 'X' :
187#ifdef PRINTF_LONG_SUPPORT
188 if (lng)
189 uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
190 else
191#endif
192 ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
193 putchw(putp,putf,w,lz,bf);
194 break;
195 case 'c' :
196 putf(putp,(char)(va_arg(va, int)));
197 break;
198 case 's' :
199 putchw(putp,putf,w,0,va_arg(va, char*));
200 break;
201 case '%' :
202 putf(putp,ch);
203 default:
204 break;
205 }
206 }
207 }
208 abort:;
209 }
210
211
212void init_printf(void* putp,void (*putf) (void*,char))
213 {
214 stdout_putf=putf;
215 stdout_putp=putp;
216 }
217
218void tfp_printf(char *fmt, ...)
219 {
220 va_list va;
221 va_start(va,fmt);
222 tfp_format(stdout_putp,stdout_putf,fmt,va);
223 va_end(va);
224 }
225
226static void putcp(void* p,char c)
227 {
228 *(*((char**)p))++ = c;
229 }
230
231
232
233void tfp_sprintf(char* s,char *fmt, ...)
234 {
235 va_list va;
236 va_start(va,fmt);
237 tfp_format(&s,putcp,fmt,va);
238 putcp(&s,0);
239 va_end(va);
240 }