]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/arm_atsam/printf.c
Fix undefined reference to `console_printf` for CTRL and ALT keyboards
[qmk_firmware.git] / tmk_core / common / arm_atsam / printf.c
1 /*
2 Copyright 2018 Massdrop Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef CONSOLE_ENABLE
19
20 #include "samd51j18a.h"
21 #include "arm_atsam_protocol.h"
22 #include "printf.h"
23 #include <string.h>
24 #include <stdarg.h>
25
26 void console_printf(char *fmt, ...) {
27     while (udi_hid_con_b_report_trans_ongoing) {}           //Wait for any previous transfers to complete
28
29     static char console_printbuf[CONSOLE_PRINTBUF_SIZE];    //Print and send buffer
30     va_list va;
31     int result;
32
33     va_start(va, fmt);
34     result = vsnprintf(console_printbuf, CONSOLE_PRINTBUF_SIZE, fmt, va);
35     va_end(va);
36
37     uint32_t irqflags;
38     char *pconbuf = console_printbuf;                       //Pointer to start send from
39     int send_out = CONSOLE_EPSIZE;                          //Bytes to send per transfer
40
41     while (result > 0) {                                    //While not error and bytes remain
42         while (udi_hid_con_b_report_trans_ongoing) {}       //Wait for any previous transfers to complete
43
44         irqflags = __get_PRIMASK();
45         __disable_irq();
46         __DMB();
47
48         if (result < CONSOLE_EPSIZE) {                      //If remaining bytes are less than console epsize
49             memset(udi_hid_con_report, 0, CONSOLE_EPSIZE);  //Clear the buffer
50             send_out = result;                              //Send remaining size
51         }
52
53         memcpy(udi_hid_con_report, pconbuf, send_out);      //Copy data into the send buffer
54
55         udi_hid_con_b_report_valid = 1;                     //Set report valid
56         udi_hid_con_send_report();                          //Send report
57
58         __DMB();
59         __set_PRIMASK(irqflags);
60
61         result -= send_out;                                 //Decrement result by bytes sent
62         pconbuf += send_out;                                //Increment buffer point by bytes sent
63     }
64 }
65
66 #endif //CONSOLE_ENABLE