A copy of the mbed USBDevice with USBSerial library

Dependents:   STM32L0_LoRa Smartage STM32L0_LoRa Turtle_RadioShuttle

Committer:
Helmut Tschemernjak
Date:
Sun Feb 24 14:52:33 2019 +0100
Revision:
8:961423d1da74
Parent:
2:195554780c9b
Added sleep manager support to avoids sleeps while a USB CDC
connection is active

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut Tschemernjak 2:195554780c9b 1 #include "mbed.h"
Helmut Tschemernjak 2:195554780c9b 2 #include "PinMap.h"
Helmut Tschemernjak 2:195554780c9b 3
Helmut Tschemernjak 2:195554780c9b 4 #ifdef FEATURE_USBSERIAL
Helmut Tschemernjak 2:195554780c9b 5
Helmut64 0:a3ea811f80f2 6 /* Copyright (c) 2010-2011 mbed.org, MIT License
Helmut64 0:a3ea811f80f2 7 *
Helmut64 0:a3ea811f80f2 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Helmut64 0:a3ea811f80f2 9 * and associated documentation files (the "Software"), to deal in the Software without
Helmut64 0:a3ea811f80f2 10 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Helmut64 0:a3ea811f80f2 11 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Helmut64 0:a3ea811f80f2 12 * Software is furnished to do so, subject to the following conditions:
Helmut64 0:a3ea811f80f2 13 *
Helmut64 0:a3ea811f80f2 14 * The above copyright notice and this permission notice shall be included in all copies or
Helmut64 0:a3ea811f80f2 15 * substantial portions of the Software.
Helmut64 0:a3ea811f80f2 16 *
Helmut64 0:a3ea811f80f2 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Helmut64 0:a3ea811f80f2 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Helmut64 0:a3ea811f80f2 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Helmut64 0:a3ea811f80f2 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Helmut64 0:a3ea811f80f2 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Helmut64 0:a3ea811f80f2 22 */
Helmut64 0:a3ea811f80f2 23
Helmut64 0:a3ea811f80f2 24 #include "stdint.h"
Helmut64 0:a3ea811f80f2 25 #include "USBSerial.h"
Helmut64 0:a3ea811f80f2 26
Helmut64 0:a3ea811f80f2 27 int USBSerial::_putc(int c) {
Helmut64 0:a3ea811f80f2 28 if (!terminal_connected)
Helmut64 0:a3ea811f80f2 29 return 0;
Helmut64 0:a3ea811f80f2 30 send((uint8_t *)&c, 1);
Helmut64 0:a3ea811f80f2 31 return 1;
Helmut64 0:a3ea811f80f2 32 }
Helmut64 0:a3ea811f80f2 33
Helmut64 0:a3ea811f80f2 34 int USBSerial::_getc() {
Helmut64 0:a3ea811f80f2 35 uint8_t c = 0;
Helmut64 0:a3ea811f80f2 36 while (buf.isEmpty());
Helmut64 0:a3ea811f80f2 37 buf.dequeue(&c);
Helmut64 0:a3ea811f80f2 38 return c;
Helmut64 0:a3ea811f80f2 39 }
Helmut64 0:a3ea811f80f2 40
Helmut64 0:a3ea811f80f2 41
Helmut64 0:a3ea811f80f2 42 bool USBSerial::writeBlock(uint8_t * buf, uint16_t size) {
Helmut64 0:a3ea811f80f2 43 if(size > MAX_PACKET_SIZE_EPBULK) {
Helmut64 0:a3ea811f80f2 44 return false;
Helmut64 0:a3ea811f80f2 45 }
Helmut64 0:a3ea811f80f2 46 if(!send(buf, size)) {
Helmut64 0:a3ea811f80f2 47 return false;
Helmut64 0:a3ea811f80f2 48 }
Helmut64 0:a3ea811f80f2 49 return true;
Helmut64 0:a3ea811f80f2 50 }
Helmut64 0:a3ea811f80f2 51
Helmut64 0:a3ea811f80f2 52
Helmut64 0:a3ea811f80f2 53
Helmut64 0:a3ea811f80f2 54 bool USBSerial::EPBULK_OUT_callback() {
Helmut64 0:a3ea811f80f2 55 uint8_t c[65];
Helmut64 0:a3ea811f80f2 56 uint32_t size = 0;
Helmut64 0:a3ea811f80f2 57
Helmut64 0:a3ea811f80f2 58 //we read the packet received and put it on the circular buffer
Helmut64 0:a3ea811f80f2 59 readEP(c, &size);
Helmut64 0:a3ea811f80f2 60 for (uint32_t i = 0; i < size; i++) {
Helmut64 0:a3ea811f80f2 61 buf.queue(c[i]);
Helmut64 0:a3ea811f80f2 62 }
Helmut64 0:a3ea811f80f2 63
Helmut64 0:a3ea811f80f2 64 //call a potential handlenr
Helmut64 0:a3ea811f80f2 65 if (rx)
Helmut64 0:a3ea811f80f2 66 rx.call();
Helmut64 0:a3ea811f80f2 67
Helmut64 0:a3ea811f80f2 68 return true;
Helmut64 0:a3ea811f80f2 69 }
Helmut64 0:a3ea811f80f2 70
Helmut64 0:a3ea811f80f2 71 uint8_t USBSerial::available() {
Helmut64 0:a3ea811f80f2 72 return buf.available();
Helmut64 0:a3ea811f80f2 73 }
Helmut64 0:a3ea811f80f2 74
Helmut64 0:a3ea811f80f2 75 bool USBSerial::connected() {
Helmut64 0:a3ea811f80f2 76 return terminal_connected;
Helmut64 0:a3ea811f80f2 77 }
Helmut Tschemernjak 2:195554780c9b 78
Helmut Tschemernjak 2:195554780c9b 79 #endif // FEATURE_USBSERIAL