printf.c 2.3 KB

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