usb device

Committer:
ppo
Date:
Sat May 14 17:24:10 2022 +0000
Revision:
0:c1e89c49eae5
commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ppo 0:c1e89c49eae5 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
ppo 0:c1e89c49eae5 2 *
ppo 0:c1e89c49eae5 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ppo 0:c1e89c49eae5 4 * and associated documentation files (the "Software"), to deal in the Software without
ppo 0:c1e89c49eae5 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
ppo 0:c1e89c49eae5 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
ppo 0:c1e89c49eae5 7 * Software is furnished to do so, subject to the following conditions:
ppo 0:c1e89c49eae5 8 *
ppo 0:c1e89c49eae5 9 * The above copyright notice and this permission notice shall be included in all copies or
ppo 0:c1e89c49eae5 10 * substantial portions of the Software.
ppo 0:c1e89c49eae5 11 *
ppo 0:c1e89c49eae5 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ppo 0:c1e89c49eae5 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ppo 0:c1e89c49eae5 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ppo 0:c1e89c49eae5 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ppo 0:c1e89c49eae5 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ppo 0:c1e89c49eae5 17 */
ppo 0:c1e89c49eae5 18
ppo 0:c1e89c49eae5 19 #include "stdint.h"
ppo 0:c1e89c49eae5 20 #include "USBSerial.h"
ppo 0:c1e89c49eae5 21
ppo 0:c1e89c49eae5 22 int USBSerial::_putc(int c) {
ppo 0:c1e89c49eae5 23 send((uint8_t *)&c, 1);
ppo 0:c1e89c49eae5 24 return 1;
ppo 0:c1e89c49eae5 25 }
ppo 0:c1e89c49eae5 26
ppo 0:c1e89c49eae5 27 int USBSerial::_getc() {
ppo 0:c1e89c49eae5 28 uint8_t c;
ppo 0:c1e89c49eae5 29 while (buf.isEmpty());
ppo 0:c1e89c49eae5 30 buf.dequeue(&c);
ppo 0:c1e89c49eae5 31 return c;
ppo 0:c1e89c49eae5 32 }
ppo 0:c1e89c49eae5 33
ppo 0:c1e89c49eae5 34
ppo 0:c1e89c49eae5 35 bool USBSerial::writeBlock(uint8_t * buf, uint16_t size) {
ppo 0:c1e89c49eae5 36 if(size > MAX_PACKET_SIZE_EPBULK) {
ppo 0:c1e89c49eae5 37 return false;
ppo 0:c1e89c49eae5 38 }
ppo 0:c1e89c49eae5 39 if(!send(buf, size)) {
ppo 0:c1e89c49eae5 40 return false;
ppo 0:c1e89c49eae5 41 }
ppo 0:c1e89c49eae5 42 return true;
ppo 0:c1e89c49eae5 43 }
ppo 0:c1e89c49eae5 44
ppo 0:c1e89c49eae5 45
ppo 0:c1e89c49eae5 46
ppo 0:c1e89c49eae5 47 bool USBSerial::EP2_OUT_callback() {
ppo 0:c1e89c49eae5 48 uint8_t c[65];
ppo 0:c1e89c49eae5 49 uint32_t size = 0;
ppo 0:c1e89c49eae5 50
ppo 0:c1e89c49eae5 51 //we read the packet received and put it on the circular buffer
ppo 0:c1e89c49eae5 52 readEP(c, &size);
ppo 0:c1e89c49eae5 53 for (int i = 0; i < size; i++) {
ppo 0:c1e89c49eae5 54 buf.queue(c[i]);
ppo 0:c1e89c49eae5 55 }
ppo 0:c1e89c49eae5 56
ppo 0:c1e89c49eae5 57 //call a potential handler
ppo 0:c1e89c49eae5 58 rx.call();
ppo 0:c1e89c49eae5 59
ppo 0:c1e89c49eae5 60 // We reactivate the endpoint to receive next characters
ppo 0:c1e89c49eae5 61 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
ppo 0:c1e89c49eae5 62 return true;
ppo 0:c1e89c49eae5 63 }
ppo 0:c1e89c49eae5 64
ppo 0:c1e89c49eae5 65 uint8_t USBSerial::available() {
ppo 0:c1e89c49eae5 66 return buf.available();
ppo 0:c1e89c49eae5 67 }
ppo 0:c1e89c49eae5 68
ppo 0:c1e89c49eae5 69 void USBSerial::setCallbackConfig (void(*ptr)(uint32_t)){
ppo 0:c1e89c49eae5 70 fptr_cfg = ptr;
ppo 0:c1e89c49eae5 71 }
ppo 0:c1e89c49eae5 72
ppo 0:c1e89c49eae5 73 uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
ppo 0:c1e89c49eae5 74 //uint8_t cdc_line_coding[7]= {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
ppo 0:c1e89c49eae5 75 uint32_t baud;
ppo 0:c1e89c49eae5 76 #define CDC_SET_LINE_CODING 0x20
ppo 0:c1e89c49eae5 77 #define CDC_GET_LINE_CODING 0x21
ppo 0:c1e89c49eae5 78 #define CDC_SET_CONTROL_LINE_STATE 0x22
ppo 0:c1e89c49eae5 79
ppo 0:c1e89c49eae5 80 bool USBSerial::USBCallback_request (){
ppo 0:c1e89c49eae5 81 bool success = false;
ppo 0:c1e89c49eae5 82 CONTROL_TRANSFER * transfer = getTransferPtr();
ppo 0:c1e89c49eae5 83
ppo 0:c1e89c49eae5 84 /* Process class-specific requests */
ppo 0:c1e89c49eae5 85
ppo 0:c1e89c49eae5 86 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
ppo 0:c1e89c49eae5 87 switch (transfer->setup.bRequest) {
ppo 0:c1e89c49eae5 88 case CDC_GET_LINE_CODING:
ppo 0:c1e89c49eae5 89 transfer->remaining = 7;
ppo 0:c1e89c49eae5 90 transfer->ptr = cdc_line_coding;
ppo 0:c1e89c49eae5 91 transfer->direction = DEVICE_TO_HOST;
ppo 0:c1e89c49eae5 92 success = true;
ppo 0:c1e89c49eae5 93 break;
ppo 0:c1e89c49eae5 94 case CDC_SET_LINE_CODING:
ppo 0:c1e89c49eae5 95 for (int i = 0; i < 7 ; i++) {cdc_line_coding[i] = *(transfer->ptr+i);}
ppo 0:c1e89c49eae5 96 transfer->remaining = 7;
ppo 0:c1e89c49eae5 97 baud = *(uint32_t*)cdc_line_coding;
ppo 0:c1e89c49eae5 98 if (fptr_cfg != NULL)(*fptr_cfg)(baud);
ppo 0:c1e89c49eae5 99 success = true;
ppo 0:c1e89c49eae5 100 break;
ppo 0:c1e89c49eae5 101 case CDC_SET_CONTROL_LINE_STATE:
ppo 0:c1e89c49eae5 102 success = true;
ppo 0:c1e89c49eae5 103 break;
ppo 0:c1e89c49eae5 104 default:
ppo 0:c1e89c49eae5 105 break;
ppo 0:c1e89c49eae5 106 }
ppo 0:c1e89c49eae5 107 }
ppo 0:c1e89c49eae5 108
ppo 0:c1e89c49eae5 109 return success;
ppo 0:c1e89c49eae5 110 }
ppo 0:c1e89c49eae5 111 /*
ppo 0:c1e89c49eae5 112 void USBSerial::suspendStateChanged(unsigned int suspended)
ppo 0:c1e89c49eae5 113 {
ppo 0:c1e89c49eae5 114 usb_state = suspended;
ppo 0:c1e89c49eae5 115 }
ppo 0:c1e89c49eae5 116
ppo 0:c1e89c49eae5 117 unsigned int USBSerial::GetUSBState (void){
ppo 0:c1e89c49eae5 118 return usb_state;
ppo 0:c1e89c49eae5 119 }
ppo 0:c1e89c49eae5 120 */