serial_cdc_glue.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. return 64;
  40. }
  41. extern "C" size_t opk_serial_write(uint8_t b)
  42. {
  43. if (device_is_ready(cdc))
  44. uart_poll_out(cdc, b);
  45. return 1;
  46. }