serial_cdc_glue.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // serial_cdc_glue.cpp -- routes the Arduino Serial shim to the USB CDC ACM.
  2. //
  3. // The board's chosen console is the CDC ACM UART (board_cdc_acm_uart), so
  4. // Serial output already reaches USB via printk. This unit additionally wires the
  5. // input side (serial_console's single-letter command reader) and a direct write
  6. // path by strongly overriding the weak opk_serial_* hooks in arduino_compat.cpp.
  7. #include <zephyr/device.h>
  8. #include <zephyr/drivers/uart.h>
  9. #include <stdint.h>
  10. #include <stddef.h>
  11. static const struct device *const cdc = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
  12. // One-byte pushback so available()+read() work without a UART peek primitive.
  13. static int s_have = -1;
  14. extern "C" int opk_serial_available(void)
  15. {
  16. if (s_have >= 0)
  17. return 1;
  18. unsigned char c;
  19. if (device_is_ready(cdc) && uart_poll_in(cdc, &c) == 0) {
  20. s_have = c;
  21. return 1;
  22. }
  23. return 0;
  24. }
  25. extern "C" int opk_serial_read(void)
  26. {
  27. if (s_have >= 0) {
  28. int c = s_have;
  29. s_have = -1;
  30. return c;
  31. }
  32. unsigned char c;
  33. if (device_is_ready(cdc) && uart_poll_in(cdc, &c) == 0)
  34. return c;
  35. return -1;
  36. }
  37. extern "C" int opk_serial_avail_write(void)
  38. {
  39. // The firmware gates its diagnostic prints on availableForWrite() > 80/90.
  40. // Report a generous CDC TX budget so those logs are not suppressed (the
  41. // underlying uart_poll_out drops bytes when the host isn't draining, so
  42. // over-reporting can't wedge the loop).
  43. return 256;
  44. }
  45. extern "C" size_t opk_serial_write(uint8_t b)
  46. {
  47. if (device_is_ready(cdc))
  48. uart_poll_out(cdc, b);
  49. return 1;
  50. }