|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * Taken from ST Cube library and heavily modified. See below for original |
| 5 | + * copyright header. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + ****************************************************************************** |
| 10 | + * @file USB_Device/CDC_Standalone/Src/usbd_cdc_interface.c |
| 11 | + * @author MCD Application Team |
| 12 | + * @version V1.0.1 |
| 13 | + * @date 26-February-2014 |
| 14 | + * @brief Source file for USBD CDC interface |
| 15 | + ****************************************************************************** |
| 16 | + * @attention |
| 17 | + * |
| 18 | + * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
| 19 | + * |
| 20 | + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); |
| 21 | + * You may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at: |
| 23 | + * |
| 24 | + * http://www.st.com/software_license_agreement_liberty_v2 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + * |
| 32 | + ****************************************************************************** |
| 33 | + */ |
| 34 | + |
| 35 | +/* Includes ------------------------------------------------------------------*/ |
| 36 | + |
| 37 | +#include <stdint.h> |
| 38 | + |
| 39 | +#include "usbd_cdc_msc_hid.h" |
| 40 | +#include "usbd_hid_interface.h" |
| 41 | + |
| 42 | +#include "py/obj.h" |
| 43 | +#include "irq.h" |
| 44 | +#include "usb.h" |
| 45 | + |
| 46 | +/* Private variables ---------------------------------------------------------*/ |
| 47 | + |
| 48 | +static __IO uint8_t dev_is_connected = 0; // indicates if we are connected |
| 49 | + |
| 50 | +static uint8_t buffer[2][64]; // pair of buffers to read individual packets into |
| 51 | +static int8_t current_read_buffer = 0; // which buffer to read from |
| 52 | +static uint32_t last_read_len; // length of last read |
| 53 | +static int8_t current_write_buffer = 0; // which buffer to write to |
| 54 | + |
| 55 | +static size_t rx_packet_size = 64; // expected size of packets to receive |
| 56 | + |
| 57 | +/* Private function prototypes -----------------------------------------------*/ |
| 58 | +static int8_t HID_Itf_Receive (uint8_t* pbuf, uint32_t Len); |
| 59 | + |
| 60 | +const USBD_HID_ItfTypeDef USBD_HID_fops = { |
| 61 | + HID_Itf_Receive |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief HID_Itf_Receive |
| 66 | + * Data received over USB OUT endpoint is processed here. |
| 67 | + * @param Buf: Buffer of data received |
| 68 | + * @param Len: Number of data received (in bytes) |
| 69 | + * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL |
| 70 | + * @note The buffer we are passed here is just UserRxBuffer, so we are |
| 71 | + * free to modify it. |
| 72 | + */ |
| 73 | +static int8_t HID_Itf_Receive(uint8_t* Buf, uint32_t Len) { |
| 74 | + current_write_buffer = !current_write_buffer; |
| 75 | + last_read_len = Len; |
| 76 | + // initiate next USB packet transfer, to append to existing data in buffer |
| 77 | + USBD_HID_SetRxBuffer(&hUSBDDevice, buffer[current_write_buffer]); |
| 78 | + USBD_HID_ReceivePacket(&hUSBDDevice); |
| 79 | + |
| 80 | + return USBD_OK; |
| 81 | +} |
| 82 | + |
| 83 | +// timout in milliseconds. |
| 84 | +// Returns number of bytes read from the device. |
| 85 | +int USBD_HID_Rx(uint8_t *buf, uint32_t len, uint32_t timeout) { |
| 86 | + // Wait until we have buffer to read |
| 87 | + uint32_t start = HAL_GetTick(); |
| 88 | + while (current_read_buffer == current_write_buffer) { |
| 89 | + // Wraparound of tick is taken care of by 2's complement arithmetic. |
| 90 | + if (HAL_GetTick() - start >= timeout) { |
| 91 | + // timeout |
| 92 | + return 0; |
| 93 | + } |
| 94 | + if (query_irq() == IRQ_STATE_DISABLED) { |
| 95 | + // IRQs disabled so buffer will never be filled; return immediately |
| 96 | + return 0; |
| 97 | + } |
| 98 | + __WFI(); // enter sleep mode, waiting for interrupt |
| 99 | + } |
| 100 | + |
| 101 | + // There is not enough space in buffer |
| 102 | + if (len < last_read_len) { |
| 103 | + return 0; |
| 104 | + } |
| 105 | + |
| 106 | + // Copy bytes from device to user buffer |
| 107 | + memcpy(buf, buffer[current_read_buffer], last_read_len); |
| 108 | + current_read_buffer = !current_read_buffer; |
| 109 | + |
| 110 | + // Success, return number of bytes read |
| 111 | + return last_read_len; |
| 112 | +} |
0 commit comments