/** * @brief usbd_audio_DataIn * Data sent on non-control IN endpoint * @param pdev: device instance * @param epnum: endpoint number * @retval status */ static uint8_t usbd_cdc_DataIn (void *pdev, uint8_t epnum) { uint32_t USB_Tx_length; usbd_cdc_Change_Open_State(1); if (!USB_Tx_State) return USBD_OK; USB_Tx_length = ring_data_contig(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_head, USB_Tx_Buffer_tail); if (USB_Tx_length) { USB_Tx_length = MIN(USB_Tx_length, CDC_DATA_IN_PACKET_SIZE); } else if (usbd_Last_Tx_Packet_size(pdev, epnum) != CDC_DATA_IN_PACKET_SIZE) { USB_Tx_State = 0; return USBD_OK; } /* Prepare the available data buffer to be sent on IN endpoint */ DCD_EP_Tx (pdev, CDC_IN_EP, (uint8_t*)&USB_Tx_Buffer[USB_Tx_Buffer_tail], USB_Tx_length); USB_Tx_Buffer_tail = ring_wrap(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_tail + USB_Tx_length); return USBD_OK; }
/** * @brief usbd_cdc_DataOut * Data received on non-control Out endpoint * @param pdev: device instance * @param epnum: endpoint number * @retval status */ static uint8_t usbd_cdc_DataOut (void *pdev, uint8_t epnum) { uint32_t USB_Rx_Count = usbd_Last_Rx_Packet_size(pdev, epnum); USB_Rx_Buffer_head = ring_wrap(USB_Rx_Buffer_length, USB_Rx_Buffer_head + USB_Rx_Count); // Serial port is definitely open usbd_cdc_Change_Open_State(1); usbd_cdc_Start_Rx(pdev); return USBD_OK; }
static void usbd_cdc_Schedule_In(void *pdev) { uint32_t USB_Tx_length; USB_Tx_length = ring_data_contig(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_head, USB_Tx_Buffer_tail); if (USB_Tx_State) { if (USB_Serial_Open) { USB_Tx_failed_counter++; if (USB_Tx_failed_counter >= 500) { usbd_cdc_Change_Open_State(0); // Completely flush TX buffer DCD_EP_Flush(pdev, CDC_IN_EP); // Send ZLP DCD_EP_Tx(pdev, CDC_IN_EP, NULL, 0); if (USB_Tx_length) USB_Tx_Buffer_tail = ring_wrap(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_tail + USB_Tx_length); USB_Tx_State = 0; } } return; } if (!USB_Tx_length) return; USB_Tx_State = 1; USB_Tx_failed_counter = 0; USB_Tx_length = MIN(USB_Tx_length, CDC_DATA_IN_PACKET_SIZE); DCD_EP_Tx (pdev, CDC_IN_EP, (uint8_t*)&USB_Tx_Buffer[USB_Tx_Buffer_tail], USB_Tx_length); USB_Tx_Buffer_tail = ring_wrap(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_tail + USB_Tx_length); }
int32_t HAL_USB_USART_Receive_Data(HAL_USB_USART_Serial serial, uint8_t peek) { if (HAL_USB_USART_Available_Data(serial) > 0) { int state = HAL_disable_irq(); uint8_t data = usbUsartMap[serial].data->rx_buffer[usbUsartMap[serial].data->rx_buffer_tail]; if (!peek) { usbUsartMap[serial].data->rx_buffer_tail = ring_wrap(usbUsartMap[serial].data->rx_buffer_length, usbUsartMap[serial].data->rx_buffer_tail + 1); } HAL_enable_irq(state); return data; } return -1; }
static inline void usbd_cdc_Change_Open_State(uint8_t state) { if (state != USB_Serial_Open) { DEBUG("USB Serial state: %d", state); if (state) { USB_Tx_failed_counter = 0; // Also flush everything in TX buffer uint32_t USB_Tx_length; USB_Tx_length = ring_data_contig(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_head, USB_Tx_Buffer_tail); if (USB_Tx_length) USB_Tx_Buffer_tail = ring_wrap(USB_TX_BUFFER_SIZE, USB_Tx_Buffer_tail + USB_Tx_length); USB_Tx_State = 0; USB_Rx_State = 1; } USB_Serial_Open = state; } }
int32_t HAL_USB_USART_Send_Data(HAL_USB_USART_Serial serial, uint8_t data) { int32_t ret = -1; int32_t available = 0; do { available = HAL_USB_USART_Available_Data_For_Write(serial); } while (available < 1 && available != -1 && HAL_USB_WillPreempt()); // Confirm once again that the Host is connected int32_t state = HAL_disable_irq(); if (HAL_USB_USART_Is_Connected(serial) && available > 0) { usbUsartMap[serial].data->tx_buffer[usbUsartMap[serial].data->tx_buffer_head] = data; usbUsartMap[serial].data->tx_buffer_head = ring_wrap(usbUsartMap[serial].data->tx_buffer_size, usbUsartMap[serial].data->tx_buffer_head + 1); ret = 1; } HAL_enable_irq(state); return ret; }