Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef USBserial_h_
  31. #define USBserial_h_
  32. #if defined(USB_SERIAL) || defined(USB_SERIAL_HID)
  33. #include <inttypes.h>
  34. // C language implementation
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. int usb_serial_getchar(void);
  39. int usb_serial_peekchar(void);
  40. int usb_serial_available(void);
  41. int usb_serial_read(void *buffer, uint32_t size);
  42. void usb_serial_flush_input(void);
  43. int usb_serial_putchar(uint8_t c);
  44. int usb_serial_write(const void *buffer, uint32_t size);
  45. void usb_serial_flush_output(void);
  46. extern uint32_t usb_cdc_line_coding[2];
  47. extern volatile uint8_t usb_cdc_line_rtsdtr;
  48. extern volatile uint8_t usb_cdc_transmit_flush_timer;
  49. extern volatile uint8_t usb_configuration;
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #define USB_SERIAL_DTR 0x01
  54. #define USB_SERIAL_RTS 0x02
  55. // C++ interface
  56. #ifdef __cplusplus
  57. #include "Stream.h"
  58. class usb_serial_class : public Stream
  59. {
  60. public:
  61. void begin(long) { /* TODO: call a function that tries to wait for enumeration */ };
  62. void end() { /* TODO: flush output and shut down USB port */ };
  63. virtual int available() { return usb_serial_available(); }
  64. virtual int read() { return usb_serial_getchar(); }
  65. virtual int peek() { return usb_serial_peekchar(); }
  66. virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
  67. virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
  68. virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
  69. size_t write(unsigned long n) { return write((uint8_t)n); }
  70. size_t write(long n) { return write((uint8_t)n); }
  71. size_t write(unsigned int n) { return write((uint8_t)n); }
  72. size_t write(int n) { return write((uint8_t)n); }
  73. using Print::write;
  74. void send_now(void) { usb_serial_flush_output(); }
  75. uint32_t baud(void) { return usb_cdc_line_coding[0]; }
  76. uint8_t stopbits(void) { uint8_t b = usb_cdc_line_coding[1]; if (!b) b = 1; return b; }
  77. uint8_t paritytype(void) { return usb_cdc_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
  78. uint8_t numbits(void) { return usb_cdc_line_coding[1] >> 16; }
  79. uint8_t dtr(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
  80. uint8_t rts(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
  81. operator bool() { return usb_configuration && (usb_cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS)); }
  82. size_t readBytes(char *buffer, size_t length) {
  83. size_t count=0;
  84. unsigned long startMillis = millis();
  85. do {
  86. count += usb_serial_read(buffer + count, length - count);
  87. if (count >= length) return count;
  88. } while(millis() - startMillis < _timeout);
  89. setReadError();
  90. return count;
  91. }
  92. };
  93. extern usb_serial_class Serial;
  94. #endif // __cplusplus
  95. #endif // USB_SERIAL || USB_SERIAL_HID
  96. #endif // USBserial_h_