Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: KL46Z-lpc81isp lpcterm2
USBMSD2/USB_CDC.cpp@0:39eb4d5b97df, 2014-05-03 (annotated)
- Committer:
- va009039
- Date:
- Sat May 03 11:21:37 2014 +0000
- Revision:
- 0:39eb4d5b97df
- Child:
- 4:8f6857784854
first commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| va009039 | 0:39eb4d5b97df | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
| va009039 | 0:39eb4d5b97df | 2 | * |
| va009039 | 0:39eb4d5b97df | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| va009039 | 0:39eb4d5b97df | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
| va009039 | 0:39eb4d5b97df | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
| va009039 | 0:39eb4d5b97df | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
| va009039 | 0:39eb4d5b97df | 7 | * Software is furnished to do so, subject to the following conditions: |
| va009039 | 0:39eb4d5b97df | 8 | * |
| va009039 | 0:39eb4d5b97df | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| va009039 | 0:39eb4d5b97df | 10 | * substantial portions of the Software. |
| va009039 | 0:39eb4d5b97df | 11 | * |
| va009039 | 0:39eb4d5b97df | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| va009039 | 0:39eb4d5b97df | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| va009039 | 0:39eb4d5b97df | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| va009039 | 0:39eb4d5b97df | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| va009039 | 0:39eb4d5b97df | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| va009039 | 0:39eb4d5b97df | 17 | */ |
| va009039 | 0:39eb4d5b97df | 18 | |
| va009039 | 0:39eb4d5b97df | 19 | #include "USB_CDC.h" |
| va009039 | 0:39eb4d5b97df | 20 | |
| va009039 | 0:39eb4d5b97df | 21 | #if (DEBUG2 > 3) |
| va009039 | 0:39eb4d5b97df | 22 | #define CDC_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
| va009039 | 0:39eb4d5b97df | 23 | #define CDC_DBG_HEX(A,B) while(0); |
| va009039 | 0:39eb4d5b97df | 24 | #else |
| va009039 | 0:39eb4d5b97df | 25 | #define CDC_DBG(...) while(0) |
| va009039 | 0:39eb4d5b97df | 26 | #define CDC_DBG_HEX(A,B) while(0) |
| va009039 | 0:39eb4d5b97df | 27 | #endif |
| va009039 | 0:39eb4d5b97df | 28 | |
| va009039 | 0:39eb4d5b97df | 29 | #define CDC_SET_LINE_CODING 0x20 |
| va009039 | 0:39eb4d5b97df | 30 | #define CDC_GET_LINE_CODING 0x21 |
| va009039 | 0:39eb4d5b97df | 31 | #define CDC_SET_CONTROL_LINE_STATE 0x22 |
| va009039 | 0:39eb4d5b97df | 32 | #define CDC_SEND_BREAK 0x23 |
| va009039 | 0:39eb4d5b97df | 33 | |
| va009039 | 0:39eb4d5b97df | 34 | #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK |
| va009039 | 0:39eb4d5b97df | 35 | |
| va009039 | 0:39eb4d5b97df | 36 | USB_CDC::USB_CDC(USBDevice* device) : _device(device), _rx_buf(128) |
| va009039 | 0:39eb4d5b97df | 37 | { |
| va009039 | 0:39eb4d5b97df | 38 | CDC_DBG("device=%p", device); |
| va009039 | 0:39eb4d5b97df | 39 | |
| va009039 | 0:39eb4d5b97df | 40 | terminal_connected = false; |
| va009039 | 0:39eb4d5b97df | 41 | //USBDevice::connect(); |
| va009039 | 0:39eb4d5b97df | 42 | } |
| va009039 | 0:39eb4d5b97df | 43 | |
| va009039 | 0:39eb4d5b97df | 44 | void USB_CDC::putc(int c) |
| va009039 | 0:39eb4d5b97df | 45 | { |
| va009039 | 0:39eb4d5b97df | 46 | if (terminal_connected) { |
| va009039 | 0:39eb4d5b97df | 47 | uint8_t buf[1]; |
| va009039 | 0:39eb4d5b97df | 48 | buf[0] = c; |
| va009039 | 0:39eb4d5b97df | 49 | _device->write(CDC_EPBULK_IN, buf, sizeof(buf), MAX_CDC_REPORT_SIZE); |
| va009039 | 0:39eb4d5b97df | 50 | } |
| va009039 | 0:39eb4d5b97df | 51 | } |
| va009039 | 0:39eb4d5b97df | 52 | |
| va009039 | 0:39eb4d5b97df | 53 | int USB_CDC::getc() |
| va009039 | 0:39eb4d5b97df | 54 | { |
| va009039 | 0:39eb4d5b97df | 55 | uint8_t c = 0; |
| va009039 | 0:39eb4d5b97df | 56 | while (_rx_buf.isEmpty()); |
| va009039 | 0:39eb4d5b97df | 57 | _rx_buf.dequeue(&c); |
| va009039 | 0:39eb4d5b97df | 58 | return c; |
| va009039 | 0:39eb4d5b97df | 59 | } |
| va009039 | 0:39eb4d5b97df | 60 | |
| va009039 | 0:39eb4d5b97df | 61 | int USB_CDC::readable() |
| va009039 | 0:39eb4d5b97df | 62 | { |
| va009039 | 0:39eb4d5b97df | 63 | return _rx_buf.available() > 0 ? 1 : 0; |
| va009039 | 0:39eb4d5b97df | 64 | } |
| va009039 | 0:39eb4d5b97df | 65 | |
| va009039 | 0:39eb4d5b97df | 66 | int USB_CDC::writeable() |
| va009039 | 0:39eb4d5b97df | 67 | { |
| va009039 | 0:39eb4d5b97df | 68 | return 1; |
| va009039 | 0:39eb4d5b97df | 69 | } |
| va009039 | 0:39eb4d5b97df | 70 | |
| va009039 | 0:39eb4d5b97df | 71 | void USB_CDC::baud_callback(int baudrate) |
| va009039 | 0:39eb4d5b97df | 72 | { |
| va009039 | 0:39eb4d5b97df | 73 | CDC_DBG("baudrate=%d", baudrate); |
| va009039 | 0:39eb4d5b97df | 74 | } |
| va009039 | 0:39eb4d5b97df | 75 | |
| va009039 | 0:39eb4d5b97df | 76 | void USB_CDC::send_break_callback(uint16_t duration) |
| va009039 | 0:39eb4d5b97df | 77 | { |
| va009039 | 0:39eb4d5b97df | 78 | CDC_DBG("duration=%04x", duration); |
| va009039 | 0:39eb4d5b97df | 79 | } |
| va009039 | 0:39eb4d5b97df | 80 | |
| va009039 | 0:39eb4d5b97df | 81 | void USB_CDC::control_line_callback(int rts, int dtr) |
| va009039 | 0:39eb4d5b97df | 82 | { |
| va009039 | 0:39eb4d5b97df | 83 | CDC_DBG("rts=%d, dtr=%d", rts, dtr); |
| va009039 | 0:39eb4d5b97df | 84 | } |
| va009039 | 0:39eb4d5b97df | 85 | |
| va009039 | 0:39eb4d5b97df | 86 | bool USB_CDC::send(uint8_t * buffer, uint32_t size) { |
| va009039 | 0:39eb4d5b97df | 87 | return _device->write(CDC_EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE); |
| va009039 | 0:39eb4d5b97df | 88 | } |
| va009039 | 0:39eb4d5b97df | 89 | |
| va009039 | 0:39eb4d5b97df | 90 | bool USB_CDC::readEP(uint8_t * buffer, uint32_t * size) { |
| va009039 | 0:39eb4d5b97df | 91 | if (!_device->readEP(CDC_EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) |
| va009039 | 0:39eb4d5b97df | 92 | return false; |
| va009039 | 0:39eb4d5b97df | 93 | if (!_device->readStart(CDC_EPBULK_OUT, MAX_CDC_REPORT_SIZE)) |
| va009039 | 0:39eb4d5b97df | 94 | return false; |
| va009039 | 0:39eb4d5b97df | 95 | return true; |
| va009039 | 0:39eb4d5b97df | 96 | } |
| va009039 | 0:39eb4d5b97df | 97 | |
| va009039 | 0:39eb4d5b97df | 98 | bool USB_CDC::readEP_NB(uint8_t * buffer, uint32_t * size) { |
| va009039 | 0:39eb4d5b97df | 99 | if (!_device->readEP_NB(CDC_EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE)) |
| va009039 | 0:39eb4d5b97df | 100 | return false; |
| va009039 | 0:39eb4d5b97df | 101 | if (!_device->readStart(CDC_EPBULK_OUT, MAX_CDC_REPORT_SIZE)) |
| va009039 | 0:39eb4d5b97df | 102 | return false; |
| va009039 | 0:39eb4d5b97df | 103 | return true; |
| va009039 | 0:39eb4d5b97df | 104 | } |
| va009039 | 0:39eb4d5b97df | 105 | |
| va009039 | 0:39eb4d5b97df | 106 | bool USB_CDC::Request_callback(CONTROL_TRANSFER* transfer) |
| va009039 | 0:39eb4d5b97df | 107 | { |
| va009039 | 0:39eb4d5b97df | 108 | static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; |
| va009039 | 0:39eb4d5b97df | 109 | |
| va009039 | 0:39eb4d5b97df | 110 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
| va009039 | 0:39eb4d5b97df | 111 | switch (transfer->setup.bRequest) { |
| va009039 | 0:39eb4d5b97df | 112 | case CDC_SET_LINE_CODING: // 0x20 |
| va009039 | 0:39eb4d5b97df | 113 | transfer->remaining = 7; |
| va009039 | 0:39eb4d5b97df | 114 | transfer->notify = true; |
| va009039 | 0:39eb4d5b97df | 115 | terminal_connected = true; |
| va009039 | 0:39eb4d5b97df | 116 | return true; |
| va009039 | 0:39eb4d5b97df | 117 | |
| va009039 | 0:39eb4d5b97df | 118 | case CDC_GET_LINE_CODING: // x021 |
| va009039 | 0:39eb4d5b97df | 119 | transfer->remaining = 7; |
| va009039 | 0:39eb4d5b97df | 120 | transfer->ptr = cdc_line_coding; |
| va009039 | 0:39eb4d5b97df | 121 | transfer->direction = DEVICE_TO_HOST; |
| va009039 | 0:39eb4d5b97df | 122 | return true; |
| va009039 | 0:39eb4d5b97df | 123 | |
| va009039 | 0:39eb4d5b97df | 124 | case CDC_SET_CONTROL_LINE_STATE: // 0x22 |
| va009039 | 0:39eb4d5b97df | 125 | control_line_callback((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1); |
| va009039 | 0:39eb4d5b97df | 126 | terminal_connected = false; |
| va009039 | 0:39eb4d5b97df | 127 | return true; |
| va009039 | 0:39eb4d5b97df | 128 | |
| va009039 | 0:39eb4d5b97df | 129 | case CDC_SEND_BREAK: // 0x23 |
| va009039 | 0:39eb4d5b97df | 130 | send_break_callback(transfer->setup.wValue); |
| va009039 | 0:39eb4d5b97df | 131 | return true; |
| va009039 | 0:39eb4d5b97df | 132 | } |
| va009039 | 0:39eb4d5b97df | 133 | } |
| va009039 | 0:39eb4d5b97df | 134 | return false; |
| va009039 | 0:39eb4d5b97df | 135 | } |
| va009039 | 0:39eb4d5b97df | 136 | |
| va009039 | 0:39eb4d5b97df | 137 | static uint32_t LD32(uint8_t* buf) |
| va009039 | 0:39eb4d5b97df | 138 | { |
| va009039 | 0:39eb4d5b97df | 139 | return buf[0]|(buf[1]<<8)|(buf[2]<<16)|(buf[3]<<24); |
| va009039 | 0:39eb4d5b97df | 140 | } |
| va009039 | 0:39eb4d5b97df | 141 | |
| va009039 | 0:39eb4d5b97df | 142 | bool USB_CDC::RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length) |
| va009039 | 0:39eb4d5b97df | 143 | { |
| va009039 | 0:39eb4d5b97df | 144 | CDC_DBG("transer=%p", transfer); |
| va009039 | 0:39eb4d5b97df | 145 | int baudrate; |
| va009039 | 0:39eb4d5b97df | 146 | if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { |
| va009039 | 0:39eb4d5b97df | 147 | switch (transfer->setup.bRequest) { |
| va009039 | 0:39eb4d5b97df | 148 | case CDC_SET_LINE_CODING: // 0x20 |
| va009039 | 0:39eb4d5b97df | 149 | baudrate = LD32(buf); |
| va009039 | 0:39eb4d5b97df | 150 | baud_callback(baudrate); |
| va009039 | 0:39eb4d5b97df | 151 | return true; |
| va009039 | 0:39eb4d5b97df | 152 | } |
| va009039 | 0:39eb4d5b97df | 153 | } |
| va009039 | 0:39eb4d5b97df | 154 | CDC_DBG_HEX((uint8_t*)transfer, sizeof(CONTROL_TRANSFER)); |
| va009039 | 0:39eb4d5b97df | 155 | return false; |
| va009039 | 0:39eb4d5b97df | 156 | } |
| va009039 | 0:39eb4d5b97df | 157 | |
| va009039 | 0:39eb4d5b97df | 158 | bool USB_CDC::EPBULK_OUT_callback() // virtual COM to target |
| va009039 | 0:39eb4d5b97df | 159 | { |
| va009039 | 0:39eb4d5b97df | 160 | uint8_t buf[MAX_CDC_REPORT_SIZE]; |
| va009039 | 0:39eb4d5b97df | 161 | uint32_t size = 0; |
| va009039 | 0:39eb4d5b97df | 162 | //we read the packet received and put it on the circular buffer |
| va009039 | 0:39eb4d5b97df | 163 | _device->readEP(CDC_EPBULK_OUT, buf, &size, MAX_CDC_REPORT_SIZE); |
| va009039 | 0:39eb4d5b97df | 164 | CDC_DBG("size=%d", size); |
| va009039 | 0:39eb4d5b97df | 165 | for(int i = 0; i < size; i++) { |
| va009039 | 0:39eb4d5b97df | 166 | _rx_buf.queue(buf[i]); |
| va009039 | 0:39eb4d5b97df | 167 | } |
| va009039 | 0:39eb4d5b97df | 168 | |
| va009039 | 0:39eb4d5b97df | 169 | // We reactivate the endpoint to receive next characters |
| va009039 | 0:39eb4d5b97df | 170 | _device->readStart(CDC_EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); |
| va009039 | 0:39eb4d5b97df | 171 | return true; |
| va009039 | 0:39eb4d5b97df | 172 | } |