|
| 1 | +/* |
| 2 | + Copyright (c) 2015 Arduino LLC. All right reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + See the GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +#ifdef USBCON |
| 20 | + |
| 21 | +#include "USBSerial.h" |
| 22 | +#include "usbd_cdc.h" |
| 23 | +#include "usbd_cdc_if.h" |
| 24 | +#include "usbd_desc.h" |
| 25 | +#include "wiring.h" |
| 26 | + |
| 27 | +#define USB_TIMEOUT 50 |
| 28 | +/* USB Device Core handle declaration */ |
| 29 | +extern USBD_HandleTypeDef hUSBD_Device_CDC; |
| 30 | +extern __IO uint32_t device_connection_status; |
| 31 | +extern __IO uint32_t lineState; |
| 32 | +extern __IO uint8_t UserTxBuffer[APP_TX_DATA_SIZE]; |
| 33 | +extern __IO uint8_t UserRxBuffer[APP_RX_DATA_SIZE]; |
| 34 | +extern __IO uint32_t UserTxBufPtrIn; |
| 35 | +extern __IO uint32_t UserTxBufPtrOut; |
| 36 | +extern __IO uint32_t UserRxBufPtrIn; |
| 37 | +extern __IO uint32_t UserRxBufPtrOut; |
| 38 | + |
| 39 | +USBSerial SerialUSB; |
| 40 | + |
| 41 | +void USBSerial::begin(uint32_t /* baud_count */) { |
| 42 | + // uart config is ignored in USB-CDC |
| 43 | +} |
| 44 | + |
| 45 | +void USBSerial::begin(uint32_t /* baud_count */, uint8_t /* config */) { |
| 46 | + // uart config is ignored in USB-CDC |
| 47 | +} |
| 48 | + |
| 49 | +void USBSerial::end(void) { |
| 50 | + |
| 51 | + USBD_LL_DeInit(&hUSBD_Device_CDC); |
| 52 | +} |
| 53 | + |
| 54 | +int USBSerial::availableForWrite(void) |
| 55 | +{ |
| 56 | + int ret_val; |
| 57 | + |
| 58 | + /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
| 59 | + /* value that we read is correct, we need to disable TIM Interrupt. */ |
| 60 | + CDC_disable_TIM_Interrupt(); |
| 61 | + |
| 62 | + if (UserTxBufPtrIn >= UserTxBufPtrOut) { |
| 63 | + ret_val = (APP_TX_DATA_SIZE - 1 - UserTxBufPtrIn + UserTxBufPtrOut); |
| 64 | + } else { |
| 65 | + ret_val = (UserTxBufPtrOut - UserTxBufPtrIn - 1); |
| 66 | + } |
| 67 | + |
| 68 | + CDC_enable_TIM_Interrupt(); |
| 69 | + |
| 70 | + return ret_val; |
| 71 | +} |
| 72 | + |
| 73 | +size_t USBSerial::write(uint8_t ch) { |
| 74 | + |
| 75 | + /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
| 76 | + /* value that we read is correct, we need to disable TIM Interrupt. */ |
| 77 | + CDC_disable_TIM_Interrupt(); |
| 78 | + |
| 79 | + if (((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE) == UserTxBufPtrOut) { |
| 80 | + // Buffer full!!! Force a flush to not loose data and go on |
| 81 | + CDC_flush(); |
| 82 | + } |
| 83 | + UserTxBuffer[UserTxBufPtrIn] = ch; |
| 84 | + UserTxBufPtrIn = ((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE); |
| 85 | + |
| 86 | + CDC_enable_TIM_Interrupt(); |
| 87 | + |
| 88 | + return 1; |
| 89 | +} |
| 90 | + |
| 91 | +size_t USBSerial::write(const uint8_t *buffer, size_t size){ |
| 92 | + size_t i = 0; |
| 93 | + for (i=0; i < size; i++) { |
| 94 | + if (write(buffer[i]) != 1) { |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + return i; |
| 99 | +} |
| 100 | + |
| 101 | +int USBSerial::available(void) { |
| 102 | + int ret; |
| 103 | + |
| 104 | + CDC_disable_TIM_Interrupt(); |
| 105 | + ret = ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE); |
| 106 | + CDC_enable_TIM_Interrupt(); |
| 107 | + |
| 108 | + return ret; |
| 109 | +} |
| 110 | + |
| 111 | +int USBSerial::read(void) { |
| 112 | + /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
| 113 | + /* value that we read is correct, we need to disable TIM Interrupt. */ |
| 114 | + CDC_disable_TIM_Interrupt(); |
| 115 | + if (UserRxBufPtrOut == UserRxBufPtrIn) { |
| 116 | + CDC_enable_TIM_Interrupt(); |
| 117 | + return -1; |
| 118 | + } else { |
| 119 | + unsigned char c = UserRxBuffer[UserRxBufPtrOut]; |
| 120 | + UserRxBufPtrOut = ((UserRxBufPtrOut + 1) % APP_RX_DATA_SIZE); |
| 121 | + CDC_enable_TIM_Interrupt(); |
| 122 | + return c; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +int USBSerial::peek(void) |
| 127 | +{ |
| 128 | + /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
| 129 | + /* value that we read is correct, we need to disable TIM Interrupt. */ |
| 130 | + CDC_disable_TIM_Interrupt(); |
| 131 | + if (UserRxBufPtrOut == UserRxBufPtrIn) { |
| 132 | + CDC_enable_TIM_Interrupt(); |
| 133 | + return -1; |
| 134 | + } else { |
| 135 | + unsigned char c = UserRxBuffer[UserRxBufPtrOut]; |
| 136 | + CDC_enable_TIM_Interrupt(); |
| 137 | + return c; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +void USBSerial::flush(void) |
| 142 | +{ |
| 143 | + /* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */ |
| 144 | + /* value that we read is correct, we need to disable TIM Interrupt. */ |
| 145 | + CDC_disable_TIM_Interrupt(); |
| 146 | + CDC_flush(); |
| 147 | + CDC_enable_TIM_Interrupt(); |
| 148 | +} |
| 149 | + |
| 150 | +uint8_t USBSerial::pending(void) { |
| 151 | + return 0; |
| 152 | +} |
| 153 | + |
| 154 | +uint8_t USBSerial::isConnected(void) { |
| 155 | + |
| 156 | + if (device_connection_status == 1) { |
| 157 | + return 1; |
| 158 | + } else { |
| 159 | + return 0; |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +uint32_t USBSerial::baud() { |
| 164 | + return 115200; |
| 165 | +} |
| 166 | + |
| 167 | +uint8_t USBSerial::stopbits() { |
| 168 | + return ONE_STOP_BIT; |
| 169 | +} |
| 170 | + |
| 171 | +uint8_t USBSerial::paritytype() { |
| 172 | + return NO_PARITY; |
| 173 | +} |
| 174 | + |
| 175 | +uint8_t USBSerial::numbits() { |
| 176 | + return 8; |
| 177 | +} |
| 178 | + |
| 179 | +bool USBSerial::dtr(void) { |
| 180 | + return false; |
| 181 | +} |
| 182 | + |
| 183 | +bool USBSerial::rts(void) { |
| 184 | + return false; |
| 185 | +} |
| 186 | + |
| 187 | +USBSerial::operator bool() { |
| 188 | + bool result = false; |
| 189 | + if (lineState == 1) |
| 190 | + result = true; |
| 191 | + delay(10); |
| 192 | + return result; |
| 193 | +} |
| 194 | + |
| 195 | +#endif // USBCON |
0 commit comments