printf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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) {} //Wait for any previous transfers to complete
  22. static char console_printbuf[CONSOLE_PRINTBUF_SIZE]; //Print and send buffer
  23. va_list va;
  24. int result;
  25. va_start(va, fmt);
  26. result = vsnprintf(console_printbuf, CONSOLE_PRINTBUF_SIZE, fmt, va);
  27. va_end(va);
  28. uint32_t irqflags;
  29. char *pconbuf = console_printbuf; //Pointer to start send from
  30. int send_out = CONSOLE_EPSIZE; //Bytes to send per transfer
  31. while (result > 0) { //While not error and bytes remain
  32. while (udi_hid_con_b_report_trans_ongoing) {} //Wait for any previous transfers to complete
  33. irqflags = __get_PRIMASK();
  34. __disable_irq();
  35. __DMB();
  36. if (result < CONSOLE_EPSIZE) { //If remaining bytes are less than console epsize
  37. memset(udi_hid_con_report, 0, CONSOLE_EPSIZE); //Clear the buffer
  38. send_out = result; //Send remaining size
  39. }
  40. memcpy(udi_hid_con_report, pconbuf, send_out); //Copy data into the send buffer
  41. udi_hid_con_b_report_valid = 1; //Set report valid
  42. udi_hid_con_send_report(); //Send report
  43. __DMB();
  44. __set_PRIMASK(irqflags);
  45. result -= send_out; //Decrement result by bytes sent
  46. pconbuf += send_out; //Increment buffer point by bytes sent
  47. }
  48. }
  49. #endif //CONSOLE_ENABLE