Wilson Tang
/
HSP_RPC_GUI
Fork of the offical HSP_RPC_GUI firmware
Fork of MAXREFDES100 firmware for MAX32620HSP
HSP/RpcServer/StringInOut.cpp@4:18155622d30a, 2020-01-28 (annotated)
- Committer:
- wt8008
- Date:
- Tue Jan 28 20:22:16 2020 +0000
- Revision:
- 4:18155622d30a
- Parent:
- 1:9490836294ea
Update Maxim's USBDevice project to the latest version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbradshaw | 0:e4a10ed6eb92 | 1 | /******************************************************************************* |
jbradshaw | 0:e4a10ed6eb92 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
jbradshaw | 0:e4a10ed6eb92 | 3 | * |
jbradshaw | 0:e4a10ed6eb92 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
jbradshaw | 0:e4a10ed6eb92 | 5 | * copy of this software and associated documentation files (the "Software"), |
jbradshaw | 0:e4a10ed6eb92 | 6 | * to deal in the Software without restriction, including without limitation |
jbradshaw | 0:e4a10ed6eb92 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
jbradshaw | 0:e4a10ed6eb92 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
jbradshaw | 0:e4a10ed6eb92 | 9 | * Software is furnished to do so, subject to the following conditions: |
jbradshaw | 0:e4a10ed6eb92 | 10 | * |
jbradshaw | 0:e4a10ed6eb92 | 11 | * The above copyright notice and this permission notice shall be included |
jbradshaw | 0:e4a10ed6eb92 | 12 | * in all copies or substantial portions of the Software. |
jbradshaw | 0:e4a10ed6eb92 | 13 | * |
jbradshaw | 0:e4a10ed6eb92 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
jbradshaw | 0:e4a10ed6eb92 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
jbradshaw | 0:e4a10ed6eb92 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
jbradshaw | 0:e4a10ed6eb92 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
jbradshaw | 0:e4a10ed6eb92 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
jbradshaw | 0:e4a10ed6eb92 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
jbradshaw | 0:e4a10ed6eb92 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
jbradshaw | 0:e4a10ed6eb92 | 21 | * |
jbradshaw | 0:e4a10ed6eb92 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
jbradshaw | 0:e4a10ed6eb92 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
jbradshaw | 0:e4a10ed6eb92 | 24 | * Products, Inc. Branding Policy. |
jbradshaw | 0:e4a10ed6eb92 | 25 | * |
jbradshaw | 0:e4a10ed6eb92 | 26 | * The mere transfer of this software does not imply any licenses |
jbradshaw | 0:e4a10ed6eb92 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
jbradshaw | 0:e4a10ed6eb92 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
jbradshaw | 0:e4a10ed6eb92 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
jbradshaw | 0:e4a10ed6eb92 | 30 | * ownership rights. |
jbradshaw | 0:e4a10ed6eb92 | 31 | ******************************************************************************* |
jbradshaw | 0:e4a10ed6eb92 | 32 | */ |
jbradshaw | 0:e4a10ed6eb92 | 33 | #include "mbed.h" |
jbradshaw | 0:e4a10ed6eb92 | 34 | #include "USBSerial.h" |
jbradshaw | 0:e4a10ed6eb92 | 35 | #include "RpcFifo.h" |
jbradshaw | 0:e4a10ed6eb92 | 36 | #include "RpcServer.h" |
jbradshaw | 0:e4a10ed6eb92 | 37 | #include "StringInOut.h" |
jbradshaw | 0:e4a10ed6eb92 | 38 | #include "Peripherals.h" |
jbradshaw | 0:e4a10ed6eb92 | 39 | |
jbradshaw | 0:e4a10ed6eb92 | 40 | /// a running index that keeps track of where an incoming string has been |
jbradshaw | 0:e4a10ed6eb92 | 41 | /// buffered to |
jbradshaw | 0:e4a10ed6eb92 | 42 | static int lineBuffer_index = 0; |
jbradshaw | 0:e4a10ed6eb92 | 43 | /// a flag that keeps track of the state of accumulating a string |
jbradshaw | 0:e4a10ed6eb92 | 44 | static int getLine_State = GETLINE_WAITING; |
jbradshaw | 0:e4a10ed6eb92 | 45 | |
jbradshaw | 1:9490836294ea | 46 | extern USBSerial *usbSerialPtr; |
jbradshaw | 1:9490836294ea | 47 | |
jbradshaw | 0:e4a10ed6eb92 | 48 | /** |
jbradshaw | 0:e4a10ed6eb92 | 49 | * @brief Place incoming USB characters into a fifo |
jbradshaw | 0:e4a10ed6eb92 | 50 | * @param data_IN buffer of characters |
jbradshaw | 0:e4a10ed6eb92 | 51 | * @param len length of data |
jbradshaw | 0:e4a10ed6eb92 | 52 | */ |
jbradshaw | 0:e4a10ed6eb92 | 53 | int fifoIncomingChars(uint8_t data_IN[], unsigned int len) { |
jbradshaw | 1:9490836294ea | 54 | unsigned int i; |
jbradshaw | 0:e4a10ed6eb92 | 55 | for (i = 0; i < len; i++) { |
jbradshaw | 0:e4a10ed6eb92 | 56 | fifo_put8(GetUSBIncomingFifo(), data_IN[i]); |
jbradshaw | 0:e4a10ed6eb92 | 57 | } |
jbradshaw | 0:e4a10ed6eb92 | 58 | return 0; |
jbradshaw | 0:e4a10ed6eb92 | 59 | } |
jbradshaw | 0:e4a10ed6eb92 | 60 | |
jbradshaw | 0:e4a10ed6eb92 | 61 | /** |
jbradshaw | 0:e4a10ed6eb92 | 62 | * @brief Check the USB incoming fifo to see if there is data to be read |
jbradshaw | 0:e4a10ed6eb92 | 63 | * @return 1 if there is data to be read, 0 if data is not available |
jbradshaw | 0:e4a10ed6eb92 | 64 | */ |
jbradshaw | 0:e4a10ed6eb92 | 65 | int isReadReady(void) { |
jbradshaw | 0:e4a10ed6eb92 | 66 | if (fifo_empty(GetUSBIncomingFifo()) == 0) |
jbradshaw | 0:e4a10ed6eb92 | 67 | return 1; |
jbradshaw | 0:e4a10ed6eb92 | 68 | return 0; |
jbradshaw | 0:e4a10ed6eb92 | 69 | } |
jbradshaw | 0:e4a10ed6eb92 | 70 | |
jbradshaw | 0:e4a10ed6eb92 | 71 | /** |
jbradshaw | 0:e4a10ed6eb92 | 72 | * @brief Clear the incoming USB read fifo |
jbradshaw | 0:e4a10ed6eb92 | 73 | */ |
jbradshaw | 0:e4a10ed6eb92 | 74 | void clearOutReadFifo(void) { fifo_clear(GetUSBIncomingFifo()); } |
jbradshaw | 0:e4a10ed6eb92 | 75 | |
jbradshaw | 0:e4a10ed6eb92 | 76 | /** |
jbradshaw | 0:e4a10ed6eb92 | 77 | * @brief Block until a character can be read from the USB |
jbradshaw | 0:e4a10ed6eb92 | 78 | * @return the character read |
jbradshaw | 0:e4a10ed6eb92 | 79 | */ |
jbradshaw | 0:e4a10ed6eb92 | 80 | char getch(void) { |
jbradshaw | 0:e4a10ed6eb92 | 81 | uint8_t ch; |
jbradshaw | 0:e4a10ed6eb92 | 82 | // block until char is ready |
jbradshaw | 0:e4a10ed6eb92 | 83 | while (isReadReady() == 0) { |
jbradshaw | 0:e4a10ed6eb92 | 84 | } |
jbradshaw | 0:e4a10ed6eb92 | 85 | // read a char from buffer |
jbradshaw | 0:e4a10ed6eb92 | 86 | fifo_get8(GetUSBIncomingFifo(), &ch); |
jbradshaw | 0:e4a10ed6eb92 | 87 | return ch; |
jbradshaw | 0:e4a10ed6eb92 | 88 | } |
jbradshaw | 0:e4a10ed6eb92 | 89 | |
jbradshaw | 0:e4a10ed6eb92 | 90 | /** |
jbradshaw | 0:e4a10ed6eb92 | 91 | * @brief Place incoming USB characters into a fifo |
jbradshaw | 0:e4a10ed6eb92 | 92 | * @param lineBuffer buffer to place the incoming characters |
jbradshaw | 0:e4a10ed6eb92 | 93 | * @param bufferLength length of buffer |
jbradshaw | 0:e4a10ed6eb92 | 94 | * @return GETLINE_WAITING if still waiting for a CRLF, GETLINE_DONE |
jbradshaw | 0:e4a10ed6eb92 | 95 | */ |
jbradshaw | 0:e4a10ed6eb92 | 96 | int getLine(char *lineBuffer, int bufferLength) { |
jbradshaw | 0:e4a10ed6eb92 | 97 | uint8_t ch; |
jbradshaw | 0:e4a10ed6eb92 | 98 | |
jbradshaw | 1:9490836294ea | 99 | //USBSerial *serial = Peripherals::usbSerial(); |
jbradshaw | 0:e4a10ed6eb92 | 100 | if (getLine_State == GETLINE_DONE) { |
jbradshaw | 0:e4a10ed6eb92 | 101 | getLine_State = GETLINE_WAITING; |
jbradshaw | 0:e4a10ed6eb92 | 102 | } |
jbradshaw | 1:9490836294ea | 103 | if (usbSerialPtr->available() != 0) { |
jbradshaw | 1:9490836294ea | 104 | ch = usbSerialPtr->_getc(); |
jbradshaw | 0:e4a10ed6eb92 | 105 | if (ch != 0x0A && ch != 0x0D) { |
jbradshaw | 0:e4a10ed6eb92 | 106 | lineBuffer[lineBuffer_index++] = ch; |
jbradshaw | 0:e4a10ed6eb92 | 107 | } |
jbradshaw | 0:e4a10ed6eb92 | 108 | if (ch == 0x0D) { |
jbradshaw | 0:e4a10ed6eb92 | 109 | lineBuffer[lineBuffer_index++] = 0; |
jbradshaw | 0:e4a10ed6eb92 | 110 | lineBuffer_index = 0; |
jbradshaw | 0:e4a10ed6eb92 | 111 | getLine_State = GETLINE_DONE; |
jbradshaw | 0:e4a10ed6eb92 | 112 | } |
jbradshaw | 0:e4a10ed6eb92 | 113 | if (lineBuffer_index > bufferLength) { |
jbradshaw | 0:e4a10ed6eb92 | 114 | lineBuffer[bufferLength - 1] = 0; |
jbradshaw | 0:e4a10ed6eb92 | 115 | getLine_State = GETLINE_DONE; |
jbradshaw | 0:e4a10ed6eb92 | 116 | } |
jbradshaw | 0:e4a10ed6eb92 | 117 | } |
jbradshaw | 0:e4a10ed6eb92 | 118 | return getLine_State; |
jbradshaw | 0:e4a10ed6eb92 | 119 | } |
jbradshaw | 0:e4a10ed6eb92 | 120 | |
jbradshaw | 0:e4a10ed6eb92 | 121 | /** |
jbradshaw | 0:e4a10ed6eb92 | 122 | * @brief Block until a fixed number of characters has been accumulated from the |
jbradshaw | 0:e4a10ed6eb92 | 123 | * incoming USB |
jbradshaw | 0:e4a10ed6eb92 | 124 | * @param lineBuffer buffer to place the incoming characters |
jbradshaw | 0:e4a10ed6eb92 | 125 | * @param maxLength length of buffer |
jbradshaw | 0:e4a10ed6eb92 | 126 | */ |
jbradshaw | 0:e4a10ed6eb92 | 127 | void getStringFixedLength(uint8_t *lineBuffer, int maxLength) { |
jbradshaw | 0:e4a10ed6eb92 | 128 | uint8_t ch; |
jbradshaw | 0:e4a10ed6eb92 | 129 | int index = 0; |
jbradshaw | 0:e4a10ed6eb92 | 130 | // block until maxLength is captured |
jbradshaw | 0:e4a10ed6eb92 | 131 | while (1) { |
jbradshaw | 0:e4a10ed6eb92 | 132 | ch = getch(); |
jbradshaw | 0:e4a10ed6eb92 | 133 | lineBuffer[index++] = ch; |
jbradshaw | 0:e4a10ed6eb92 | 134 | if (index == maxLength) |
jbradshaw | 0:e4a10ed6eb92 | 135 | return; |
jbradshaw | 0:e4a10ed6eb92 | 136 | } |
jbradshaw | 0:e4a10ed6eb92 | 137 | } |
jbradshaw | 0:e4a10ed6eb92 | 138 | |
jbradshaw | 0:e4a10ed6eb92 | 139 | /** |
jbradshaw | 0:e4a10ed6eb92 | 140 | * @brief Outut an array of bytes out the USB serial port |
jbradshaw | 0:e4a10ed6eb92 | 141 | * @param data buffer to output |
jbradshaw | 0:e4a10ed6eb92 | 142 | * @param length length of buffer |
jbradshaw | 0:e4a10ed6eb92 | 143 | */ |
jbradshaw | 0:e4a10ed6eb92 | 144 | int putBytes(uint8_t *data, uint32_t length) { |
jbradshaw | 1:9490836294ea | 145 | int sent; |
jbradshaw | 1:9490836294ea | 146 | int send; |
jbradshaw | 1:9490836294ea | 147 | bool status; |
jbradshaw | 1:9490836294ea | 148 | uint8_t *ptr; |
jbradshaw | 1:9490836294ea | 149 | ptr = data; |
jbradshaw | 1:9490836294ea | 150 | // |
jbradshaw | 1:9490836294ea | 151 | // break up the string into chunks |
jbradshaw | 1:9490836294ea | 152 | // |
jbradshaw | 1:9490836294ea | 153 | sent = 0; |
jbradshaw | 1:9490836294ea | 154 | do { |
jbradshaw | 1:9490836294ea | 155 | send = MAX_PACKET_SIZE_EPBULK; |
jbradshaw | 1:9490836294ea | 156 | if ((sent + MAX_PACKET_SIZE_EPBULK) > (int)length) { |
jbradshaw | 1:9490836294ea | 157 | send = length - sent; |
jbradshaw | 1:9490836294ea | 158 | } |
jbradshaw | 1:9490836294ea | 159 | status = usbSerialPtr->writeBlock(ptr,send); |
jbradshaw | 1:9490836294ea | 160 | if (status != true) { |
jbradshaw | 1:9490836294ea | 161 | while (1) ; |
jbradshaw | 1:9490836294ea | 162 | } |
jbradshaw | 1:9490836294ea | 163 | sent += send; |
jbradshaw | 1:9490836294ea | 164 | ptr += send; |
jbradshaw | 1:9490836294ea | 165 | } while (sent < (int)length); |
jbradshaw | 0:e4a10ed6eb92 | 166 | return 0; |
jbradshaw | 0:e4a10ed6eb92 | 167 | } |
jbradshaw | 0:e4a10ed6eb92 | 168 | |
jbradshaw | 0:e4a10ed6eb92 | 169 | /** |
jbradshaw | 1:9490836294ea | 170 | * @brief Output a string out the USB serial port |
jbradshaw | 1:9490836294ea | 171 | * @param str to output |
jbradshaw | 0:e4a10ed6eb92 | 172 | */ |
jbradshaw | 1:9490836294ea | 173 | int putStr(const char *str) { |
jbradshaw | 1:9490836294ea | 174 | int length; |
jbradshaw | 0:e4a10ed6eb92 | 175 | uint8_t *ptr; |
jbradshaw | 1:9490836294ea | 176 | ptr = (uint8_t *)str; |
jbradshaw | 1:9490836294ea | 177 | length = strlen(str); |
jbradshaw | 1:9490836294ea | 178 | putBytes(ptr, length); |
jbradshaw | 0:e4a10ed6eb92 | 179 | return 0; |
jbradshaw | 0:e4a10ed6eb92 | 180 | } |