|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * USBSerial core library for Arduino STM32 + HAL + CubeMX (HALMX). |
| 4 | + * |
| 5 | + * Copyright (c) 2016 by Vassilis Serasidis <[email protected]> |
| 6 | + * Home: http://www.serasidis.gr |
| 7 | + |
| 8 | + * |
| 9 | + * Arduino_STM32 forum: http://www.stm32duino.com |
| 10 | + * |
| 11 | + * Permission to use, copy, modify, and/or distribute this software for |
| 12 | + * any purpose with or without fee is hereby granted, provided that the |
| 13 | + * above copyright notice and this permission notice appear in all copies. |
| 14 | + * |
| 15 | + * Some functions follow the sam and samd arduino core libray files. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
| 18 | + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
| 19 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR |
| 20 | + * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES |
| 21 | + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 | + * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 23 | + * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 24 | + * |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <chip.h> |
| 28 | + |
| 29 | +#if defined(SERIAL_USB) && defined(USBCON) |
| 30 | +#include <USBSerial.h> |
| 31 | +#include "variant.h" |
| 32 | + |
| 33 | +// Constructors //////////////////////////////////////////////////////////////// |
| 34 | +USBSerial::USBSerial(){ |
| 35 | + // Make sure Rx ring buffer is initialized back to empty. |
| 36 | + rx_buffer.iHead = rx_buffer.iTail = 0; |
| 37 | + //tx_buffer.iHead = tx_buffer.iTail = 0; |
| 38 | +} |
| 39 | + |
| 40 | +void USBSerial::init(void){ |
| 41 | +/* Re-enumerate the USB */ |
| 42 | + volatile unsigned int i; |
| 43 | + |
| 44 | +#ifdef USB_DISC_PIN |
| 45 | + pinMode(USB_DISC_PIN, OUTPUT); |
| 46 | + digitalWrite(USB_DISC_PIN, HIGH); |
| 47 | + for(i=0;i<512;i++); |
| 48 | + digitalWrite(USB_DISC_PIN, LOW); |
| 49 | +#else |
| 50 | + #error "USB_DISC_PIN definition missing" |
| 51 | +#endif |
| 52 | + MX_USB_DEVICE_Init(); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +void USBSerial::begin(uint32_t baud_count){ |
| 57 | + init(); |
| 58 | + // suppress "unused parameter" warning |
| 59 | + (void)baud_count; |
| 60 | +} |
| 61 | + |
| 62 | +void USBSerial::begin(uint32_t baud_count, uint8_t config){ |
| 63 | + init(); |
| 64 | + //suppress "unused parameter" warning |
| 65 | + (void)baud_count; |
| 66 | + (void)config; |
| 67 | +} |
| 68 | + |
| 69 | +void USBSerial::end(void){ |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +int USBSerial::availableForWrite(void){ |
| 75 | + //return (CDC_SERIAL_BUFFER_SIZE - available()); |
| 76 | + //return (uint32_t)(CDC_SERIAL_BUFFER_SIZE + tx_buffer.iHead - tx_buffer.iTail) % CDC_SERIAL_BUFFER_SIZE; |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +int USBSerial::available(void){ |
| 82 | + return (uint32_t)(CDC_SERIAL_BUFFER_SIZE + rx_buffer.iHead - rx_buffer.iTail) % CDC_SERIAL_BUFFER_SIZE; |
| 83 | +} |
| 84 | + |
| 85 | +int USBSerial::peek(void) |
| 86 | +{ |
| 87 | + if ( rx_buffer.iHead == rx_buffer.iTail ) |
| 88 | + return -1; |
| 89 | + |
| 90 | + return rx_buffer.buffer[rx_buffer.iTail]; |
| 91 | +} |
| 92 | + |
| 93 | +int USBSerial::read(void) |
| 94 | +{ |
| 95 | + // if the head isn't ahead of the tail, we don't have any characters |
| 96 | + if ( rx_buffer.iHead == rx_buffer.iTail ) |
| 97 | + return -1; |
| 98 | + |
| 99 | + uint8_t uc = rx_buffer.buffer[rx_buffer.iTail]; |
| 100 | + rx_buffer.iTail = (unsigned int)(rx_buffer.iTail + 1) % CDC_SERIAL_BUFFER_SIZE; |
| 101 | + |
| 102 | + return uc; |
| 103 | +} |
| 104 | + |
| 105 | +void USBSerial::flush(void){ |
| 106 | + //It's not implemented yet. |
| 107 | +} |
| 108 | + |
| 109 | +size_t USBSerial::write(const uint8_t *buffer, size_t size){ |
| 110 | + unsigned long timeout=millis()+5; |
| 111 | + if(hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) |
| 112 | + { |
| 113 | + while(millis()<timeout) |
| 114 | + { |
| 115 | + if(CDC_Transmit_FS((uint8_t*)buffer, size) == USBD_OK) |
| 116 | + { |
| 117 | + return size; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
| 123 | + |
| 124 | + |
| 125 | +/* uint8_t i; |
| 126 | +
|
| 127 | + if(hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED){ |
| 128 | +
|
| 129 | + //HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); |
| 130 | + for(i=0;i<200;i++) |
| 131 | + if(CDC_Transmit_FS((uint8_t*)buffer, size) == USBD_OK){ |
| 132 | + return size; |
| 133 | + //break; |
| 134 | + } |
| 135 | +
|
| 136 | +
|
| 137 | + //HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); |
| 138 | + } |
| 139 | + return 0; */ |
| 140 | +} |
| 141 | + |
| 142 | +size_t USBSerial::write(uint8_t c) { |
| 143 | + return write(&c, 1); |
| 144 | +} |
| 145 | + |
| 146 | +void USBSerial::CDC_RxHandler (uint8_t* Buf, uint16_t Len){ |
| 147 | + |
| 148 | + for(uint16_t i=0;i<Len;i++){ |
| 149 | + if(available() < (CDC_SERIAL_BUFFER_SIZE - 1)){ |
| 150 | + rx_buffer.buffer[rx_buffer.iHead] = *Buf++; |
| 151 | + rx_buffer.iHead = (uint16_t)(rx_buffer.iHead + 1) % CDC_SERIAL_BUFFER_SIZE; |
| 152 | + }else |
| 153 | + break; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +// This operator is a convenient way for a sketch to check whether the |
| 159 | +// port has actually been configured and opened by the host (as opposed |
| 160 | +// to just being connected to the host). It can be used, for example, in |
| 161 | +// setup() before printing to ensure that an application on the host is |
| 162 | +// actually ready to receive and display the data. |
| 163 | +// We add a short delay before returning to fix a bug observed by Federico |
| 164 | +// where the port is configured (lineState != 0) but not quite opened. |
| 165 | +USBSerial::operator bool() |
| 166 | +{ |
| 167 | + // this is here to avoid spurious opening after upload |
| 168 | + if (millis() < 500) |
| 169 | + return false; |
| 170 | + |
| 171 | + bool result = false; |
| 172 | + |
| 173 | +/* if (_usbLineInfo.lineState > 0) |
| 174 | + { |
| 175 | + result = true; |
| 176 | + } |
| 177 | +
|
| 178 | + delay(10); */ |
| 179 | + return result; |
| 180 | +} |
| 181 | + |
| 182 | +uint32_t USBSerial::baud() { |
| 183 | + //return _usbLineInfo.dwDTERate; |
| 184 | + return 0; |
| 185 | +} |
| 186 | + |
| 187 | +uint8_t USBSerial::stopbits() { |
| 188 | + //return _usbLineInfo.bCharFormat; |
| 189 | + return 0; |
| 190 | +} |
| 191 | + |
| 192 | +uint8_t USBSerial::paritytype() { |
| 193 | + //return _usbLineInfo.bParityType; |
| 194 | + return 0; |
| 195 | +} |
| 196 | + |
| 197 | +uint8_t USBSerial::numbits() { |
| 198 | + //return _usbLineInfo.bDataBits; |
| 199 | + return 0; |
| 200 | +} |
| 201 | + |
| 202 | +bool USBSerial::dtr() { |
| 203 | + //return _usbLineInfo.lineState & 0x1; |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +bool USBSerial::rts() { |
| 208 | + //return _usbLineInfo.lineState & 0x2; |
| 209 | + return 0; |
| 210 | +} |
| 211 | + |
| 212 | +#endif |
0 commit comments