You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.3 KiB

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