/** * @brief TIM period elapsed callback * @param htim: TIM handle * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(USBD_CDC_PACKET_SEND_OK == true) { USBD_CDC_PACKET_BYTES_COUNT = 0; for(; USBD_CDC_PACKET_BYTES_COUNT < APP_RX_DATA_SIZE; USBD_CDC_PACKET_BYTES_COUNT++) { fifo_pop_return_t data_return = fifo_pop(usb_cdc_dev_tx_fifo); if(data_return.status == false) break; UserTxBuffer[USBD_CDC_PACKET_BYTES_COUNT] = data_return.character; } } if(USBD_CDC_PACKET_BYTES_COUNT == 0) return; USBD_CDC_SetTxBuffer(&usb_cdc_dev_param, UserTxBuffer, USBD_CDC_PACKET_BYTES_COUNT); if(USBD_CDC_TransmitPacket(&usb_cdc_dev_param) == USBD_OK) { USBD_CDC_PACKET_SEND_OK = true; } else USBD_CDC_PACKET_SEND_OK = false; //} }
void serviceUSBWrite() { if (usbTransferHasCompleted) { //printf("USB Transfer Completed\r\n"); // Set USB as no longer active usbIsActive = 0; usbTransmitBufferLengths[activeUsbBuffer] = 0; // Set the active USB buffer to the next buffer activeUsbBuffer++; if (activeUsbBuffer >= USB_SEND_BUFFER_NUM) { activeUsbBuffer = 0; } // Clear the overrun status usbBufferOverrun = 0; usbTransferHasCompleted = 0; } // If usb in inactive and // the active buffer has data and // we can start another transfer if (usbIsActive == 0 && usbTransmitBufferLengths[activeUsbBuffer] > 0) { // Mark USB as active to not overwrite buffer usbIsActive = 1; // Increase the nextBuffer ptr if (nextUsbBuffer == activeUsbBuffer) { nextUsbBuffer++; if (nextUsbBuffer >= USB_SEND_BUFFER_NUM) { nextUsbBuffer = 0; } } //printf("Writing %lu bytes to USB stack\r\n", usbTransmitBufferLengths[activeUsbBuffer]); // Transmit the current active buffer across USB // Actual transmission will take place in a later USB IRQ USBD_CDC_SetTxBuffer(&USBD_Device, usbTransmitBuffers[activeUsbBuffer], usbTransmitBufferLengths[activeUsbBuffer]); USBD_CDC_TransmitPacket(&USBD_Device); } if (reinitUSB) { reinitUSB = 0; USBD_CDC_RegisterInterface(&USBD_Device, &cdcInterface); } }
/** * @brief VCP_Write * Transmit a len block of data to host through the IN Endpoint. * * @param pbuf: Buffer of data to be transmit * @param len: Number of to data be transmit (in bytes) * @retval The number of transmitted byte */ int VCP_Write(uint8_t *pbuf, uint16_t len) { if (len > CDC_DATA_HS_OUT_PACKET_SIZE) { int offset; for (offset = 0; offset < len; offset++) { int todo = MIN(CDC_DATA_HS_OUT_PACKET_SIZE, len - offset); int done = VCP_Write(((char *)pbuf) + offset, todo); if (done != todo) return offset + done; } return len; } USBD_CDC_HandleTypeDef *pCDC = (USBD_CDC_HandleTypeDef *)hUSBDDevice.pClassData; while(pCDC->TxState) { } //Wait for previous transfer USBD_CDC_SetTxBuffer(&hUSBDDevice, (uint8_t *)pbuf, len); if (USBD_CDC_TransmitPacket(&hUSBDDevice) != USBD_OK) return 0; while(pCDC->TxState) { } //Wait until transfer is done return len; }
/** * @brief TIM period elapsed callback * @param htim: TIM handle * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { uint32_t buffptr; uint32_t buffsize; if(UserTxBufPtrOut != UserTxBufPtrIn) { if(UserTxBufPtrOut > UserTxBufPtrIn) /* Rollback */ { buffsize = APP_RX_DATA_SIZE - UserTxBufPtrOut; } else { buffsize = UserTxBufPtrIn - UserTxBufPtrOut; } buffptr = UserTxBufPtrOut; USBD_CDC_SetTxBuffer(&USBD_Device, (uint8_t*)&UserTxBuffer[buffptr], buffsize); if(USBD_CDC_TransmitPacket(&USBD_Device) == USBD_OK) { UserTxBufPtrOut += buffsize; if (UserTxBufPtrOut >= APP_RX_DATA_SIZE) { UserTxBufPtrOut -= APP_RX_DATA_SIZE; } } } }
/** * @brief Initializes the CDC media low layer * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Itf_Init(void) { /*##-6- Enable TIM peripherals Clock #######################################*/ USBCDCTIMx_CLK_ENABLE(); /*##-7- Configure the NVIC for TIMx ########################################*/ /* Set Interrupt Group Priority */ HAL_NVIC_SetPriority(USBCDCTIMx_IRQn, 6, 0); /* Enable the TIMx global Interrupt */ HAL_NVIC_EnableIRQ(USBCDCTIMx_IRQn); /*##-3- Configure the TIM Base generation #################################*/ TIM_Config(); /*##-4- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ if(HAL_TIM_Base_Start_IT(&USBCDCTimHandle) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*##-5- Set Application Buffers ############################################*/ USBD_CDC_SetTxBuffer(&usb_cdc_dev_param, UserTxBuffer, 0); USBD_CDC_SetRxBuffer(&usb_cdc_dev_param, UserRxBuffer); return (USBD_OK); }
void VCP_SendDataSynch(const uint8 *buffer, int size) { if (gBF.connectToHost == 0) { return; } USBD_CDC_HandleTypeDef *pCDC = handleUSBD.pClassData; do { if (sizeBuffer + size > SIZE_BUFFER_VCP) { int reqBytes = SIZE_BUFFER_VCP - sizeBuffer; LIMITATION(reqBytes, reqBytes, 0, size); while (pCDC->TxState == 1) {}; memcpy(buffSend + sizeBuffer, buffer, reqBytes); USBD_CDC_SetTxBuffer(&handleUSBD, buffSend, SIZE_BUFFER_VCP); USBD_CDC_TransmitPacket(&handleUSBD); size -= reqBytes; buffer += reqBytes; sizeBuffer = 0; } else { memcpy(buffSend + sizeBuffer, buffer, size); sizeBuffer += size; size = 0; } } while (size); }
/** * @brief CDC_Itf_Init * Initializes the CDC media low layer * @param None * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Itf_Init(void) { #if 0 /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* USART configured as follow: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = No parity - BaudRate = 115200 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 115200; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_NONE; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; if(HAL_UART_Init(&UartHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Put UART peripheral in IT reception process ########################*/ /* Any data received will be stored in "UserTxBuffer" buffer */ if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)UserTxBuffer, 1) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-3- Configure the TIM Base generation #################################*/ now done in HAL_MspInit TIM_Config(); #endif /*##-4- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ __HAL_TIM_ENABLE_IT(&TIM3_Handle, TIM_IT_UPDATE); /*##-5- Set Application Buffers ############################################*/ USBD_CDC_SetTxBuffer(&hUSBDDevice, UserTxBuffer, 0); USBD_CDC_SetRxBuffer(&hUSBDDevice, UserRxBuffer); UserRxBufCur = 0; UserRxBufLen = 0; /* NOTE: we cannot reset these here, because USBD_CDC_SetInterrupt * may be called before this init function to set these values. * This can happen if the USB enumeration occurs after the call to * USBD_CDC_SetInterrupt. user_interrupt_char = VCP_CHAR_NONE; user_interrupt_data = NULL; */ return (USBD_OK); }
/** * @brief TIM period elapsed callback * @param htim: TIM handle * @retval None */ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) { if (!dev_is_connected) { // CDC device is not connected to a host, so we are unable to send any data return; } if (UserTxBufPtrOut == UserTxBufPtrIn && !UserTxNeedEmptyPacket) { // No outstanding data to send return; } if (UserTxBufPtrOut != UserTxBufPtrOutShadow) { // We have sent data and are waiting for the low-level USB driver to // finish sending it over the USB in-endpoint. // We have a 15 * 10ms = 150ms timeout if (UserTxBufPtrWaitCount < 15) { PCD_HandleTypeDef *hpcd = hUSBDDevice.pData; USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; if (USBx_INEP(CDC_IN_EP & 0x7f)->DIEPTSIZ & USB_OTG_DIEPTSIZ_XFRSIZ) { // USB in-endpoint is still reading the data UserTxBufPtrWaitCount++; return; } } UserTxBufPtrOut = UserTxBufPtrOutShadow; } if (UserTxBufPtrOutShadow != UserTxBufPtrIn || UserTxNeedEmptyPacket) { uint32_t buffptr; uint32_t buffsize; if (UserTxBufPtrOutShadow > UserTxBufPtrIn) { // rollback buffsize = APP_TX_DATA_SIZE - UserTxBufPtrOutShadow; } else { buffsize = UserTxBufPtrIn - UserTxBufPtrOutShadow; } buffptr = UserTxBufPtrOutShadow; USBD_CDC_SetTxBuffer(&hUSBDDevice, (uint8_t*)&UserTxBuffer[buffptr], buffsize); if (USBD_CDC_TransmitPacket(&hUSBDDevice) == USBD_OK) { UserTxBufPtrOutShadow += buffsize; if (UserTxBufPtrOutShadow == APP_TX_DATA_SIZE) { UserTxBufPtrOutShadow = 0; } UserTxBufPtrWaitCount = 0; // According to the USB specification, a packet size of 64 bytes (CDC_DATA_FS_MAX_PACKET_SIZE) // gets held at the USB host until the next packet is sent. This is because a // packet of maximum size is considered to be part of a longer chunk of data, and // the host waits for all data to arrive (ie, waits for a packet < max packet size). // To flush a packet of exactly max packet size, we need to send a zero-size packet. // See eg http://www.cypress.com/?id=4&rID=92719 UserTxNeedEmptyPacket = (buffsize == CDC_DATA_FS_MAX_PACKET_SIZE && UserTxBufPtrOutShadow == UserTxBufPtrIn); } } }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; if ( hUsbDevice_0 == NULL ) return USBD_FAIL; if ( hUsbDevice_0->dev_state != USBD_STATE_CONFIGURED ) return USBD_FAIL; USBD_CDC_HandleTypeDef *pCDC = (USBD_CDC_HandleTypeDef *)hUsbDevice_0->pClassData; /* Determine if transfer is already in progress. */ /* If transfer in progress is stuck in that state, */ /* then reset the interface and try again. */ if ( pCDC->TxState != 0 ) { return USBD_BUSY; } usb_last_tx_not_busy_timestamp = HAL_GetTick(); /* USER CODE BEGIN 8 */ if (Len > APP_TX_DATA_SIZE) { int offset; for (offset = 0; offset < Len; offset++) { int todo = MIN(APP_TX_DATA_SIZE, Len - offset); result = CDC_Transmit_FS(Buf + offset, todo); if ( ( result != USBD_OK ) && ( result != USBD_BUSY ) ) { /* Error: Break out now */ return result; } } return USBD_OK; } pCDC = (USBD_CDC_HandleTypeDef *)hUsbDevice_0->pClassData; uint32_t tx_completion_wait_timestamp = HAL_GetTick(); while(pCDC->TxState) { if ( HAL_GetTick() - tx_completion_wait_timestamp > (uint32_t)100 ) { /* Timeout */ return USBD_BUSY; } } //Wait for previous transfer to complete int i; for ( i = 0; i < Len; i++ ) { UserLowLevelTxBufferFS[i] = Buf[i]; } USBD_CDC_SetTxBuffer(hUsbDevice_0, &UserLowLevelTxBufferFS[0], Len); result = USBD_CDC_TransmitPacket(hUsbDevice_0); /* USER CODE END 8 */ return result; }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 8 */ USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBuffer, Len); result = USBD_CDC_TransmitPacket(hUsbDevice_0); /* USER CODE END 8 */ return result; }
/** * @brief CDC_Init_FS * Initializes the CDC media low layer over the FS USB IP * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Init_FS(void) { /* USER CODE BEGIN 3 */ /* Set Application Buffers */ USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS); return (USBD_OK); /* USER CODE END 3 */ }
/** * @brief CDC_Itf_Init * Initializes the CDC media low layer * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Itf_Init(void) { /*##-5- Set Application Buffers ############################################*/ USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0); USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer); return (USBD_OK); }
/** * @brief CDC_Transmit_HS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 12 */ USBD_CDC_SetTxBuffer(hUsbDevice_1, Buf, Len); result = USBD_CDC_TransmitPacket(hUsbDevice_1); /* USER CODE END 12 */ return result; }
/** * @brief CDC_Init_HS * Initializes the CDC media low layer over the USB HS IP * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Init_HS(void) { hUsbDevice_1 = &hUsbDeviceHS; /* USER CODE BEGIN 8 */ /* Set Application Buffers */ USBD_CDC_SetTxBuffer(hUsbDevice_1, UserTxBufferHS, 0); USBD_CDC_SetRxBuffer(hUsbDevice_1, UserRxBufferHS); return (USBD_OK); /* USER CODE END 8 */ }
/** * @brief CDC_Init_FS * Initializes the CDC media low layer over the FS USB IP * @param None * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Init_FS(void) { hUsbDevice_0 = &hUsbDeviceFS; /* USER CODE BEGIN 4 */ /* Set Application Buffers */ USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBuffer, 0); USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBuffer); return (USBD_OK); /* USER CODE END 4 */ }
/** * @brief CDC_Itf_Init * Initializes the CDC media low layer * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Itf_Init(void) { #if 0 /*##-1- Configure the UART peripheral ######################################*/ /* Put the USART peripheral in the Asynchronous mode (UART Mode) */ /* USART configured as follow: - Word Length = 8 Bits - Stop Bit = One Stop bit - Parity = No parity - BaudRate = 115200 baud - Hardware flow control disabled (RTS and CTS signals) */ UartHandle.Instance = USARTx; UartHandle.Init.BaudRate = 115200; UartHandle.Init.WordLength = UART_WORDLENGTH_8B; UartHandle.Init.StopBits = UART_STOPBITS_1; UartHandle.Init.Parity = UART_PARITY_NONE; UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; if(HAL_UART_Init(&UartHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Put UART peripheral in IT reception process ########################*/ /* Any data received will be stored in "UserTxBuffer" buffer */ if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)UserTxBuffer, 1) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } #endif /*##-3- Configure the TIM Base generation #################################*/ TIM_Config(); /*##-4- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*##-5- Set Application Buffers ############################################*/ USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0); USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer); return (USBD_OK); }
void VCP_Flush() { if (sizeBuffer) { USBD_CDC_HandleTypeDef *pCDC = handleUSBD.pClassData; while (pCDC->TxState == 1) {}; USBD_CDC_SetTxBuffer(&handleUSBD, buffSend, sizeBuffer); USBD_CDC_TransmitPacket(&handleUSBD); while (pCDC->TxState == 1) {}; } sizeBuffer = 0; }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 8 */ // HERE was bug! Kill them! // USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, Len); // I do it for you USBD_CDC_SetTxBuffer(hUsbDevice_0, Buf, Len); result = USBD_CDC_TransmitPacket(hUsbDevice_0); /* USER CODE END 8 */ return result; }
/* initializes the CDC media low level layer */ static int8_t CDC_Itf_Init(void) { TIM_Config(); if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { Error_Handler(); } USBD_CDC_SetTxBuffer(&USBD_Device, UserTxBuffer, 0); USBD_CDC_SetRxBuffer(&USBD_Device, UserRxBuffer); return USBD_OK; }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 7 */ USBD_CDC_SetTxBuffer(hUsbDevice_0, Buf, Len); while(result != USBD_OK); do { result = USBD_CDC_TransmitPacket(hUsbDevice_0); } while(result != USBD_OK); /* USER CODE END 7 */ return result; }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 7 */ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData; if (hcdc->TxState != 0){ return USBD_BUSY; } USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); /* USER CODE END 7 */ return result; }
/** * @brief CDC_Init_FS * Initializes the CDC media low layer over the FS USB IP * @param None * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Init_FS(void) { hUsbDevice_0 = &hUsbDeviceFS; /* USER CODE BEGIN 4 */ /* Set Application Buffers */ USBD_CDC_SetTxBuffer(hUsbDevice_0, UserLowLevelTxBufferFS, 0); USBD_CDC_SetRxBuffer(hUsbDevice_0, UserLowLevelRxBufferFS); AppRxBufferHeadIndex = 0; AppRxBufferTailIndex = 0; if ( hUsbDevice_0 == NULL ) return USBD_FAIL; usb_init_count++; return (USBD_OK); /* USER CODE END 4 */ }
/** * @brief CDC_Receive_FS * Data received over USB OUT endpoint are sent over CDC interface * through this function. * * @note * This function will block any OUT packet reception on USB endpoint * untill exiting this function. If you exit this function before transfer * is complete on CDC interface (ie. using DMA controller) it will result * in receiving more data while previous ones are still not sent. * * @param Buf: Buffer of data to be received * @param Len: Number of data received (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) { uint32_t i; /* USER CODE BEGIN 7 */ for (i = 0; i < *Len; i++) UserTxBufferFS[i] = UserRxBufferFS[i]; USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &UserTxBufferFS[0], *Len); USBD_CDC_TransmitPacket(&hUsbDeviceFS); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &UserRxBufferFS[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); return (USBD_OK); /* USER CODE END 7 */ }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* buffer, uint16_t size) { /* USER CODE BEGIN 7 */ uint16_t idx = 0; uint16_t usbBufferSize = APP_TX_DATA_SIZE - 1; while( size > usbBufferSize ) { USBD_CDC_SetTxBuffer( hUsbDevice_0, buffer + idx, usbBufferSize ); size -= usbBufferSize; idx += usbBufferSize; while( USBD_CDC_TransmitPacket( hUsbDevice_0 ) != USBD_OK ); } if( size != 0 ) { USBD_CDC_SetTxBuffer( hUsbDevice_0, buffer + idx, size ); while( USBD_CDC_TransmitPacket( hUsbDevice_0 ) != USBD_OK ); } /* USER CODE END 7 */ return USBD_OK; }
static int8_t CDC_Itf_Init(void) { tx_wr = 0; tx_rd = 0; tx_overflow = 0; rx_wr = 0; rx_rd = 0; rx_overflow = 0; USBD_CDC_SetTxBuffer(&hUSBDDevice, tx_fifo, 0); USBD_CDC_SetRxBuffer(&hUSBDDevice, rx_buffer); cdc_timer_start(); return USBD_OK; }
void VCP_SendDataAsinch(uint8 *buffer, int size) { const int SIZE_BUFFER = 64; #ifdef WIN32 static uint8 *trBuf; #else static uint8 trBuf[SIZE_BUFFER]; #endif size = Math_MinInt(size, SIZE_BUFFER); while (!PrevSendingComplete()) {}; memcpy(trBuf, buffer, size); USBD_CDC_SetTxBuffer(&handleUSBD, trBuf, size); USBD_CDC_TransmitPacket(&handleUSBD); }
/** * @brief CDC_Init_FS * Initializes the CDC media low layer over the FS USB IP * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Init_FS(void) { husb = &hUsbDeviceFS; /* Configure the USART with some sensible defaults TODO: If possible, don't drive TX high while CDC is "closed"? */ huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; HAL_UART_Init(&huart2); /* Enable USART2 in NVIC, set priority to high */ NVIC_SetPriority(USART2_IRQn, USART_IRQ_PRIORITY); NVIC_EnableIRQ(USART2_IRQn); /* UART2 receives data to the CDC transmit buffer, byte at a time :( */ if(HAL_UART_Receive_IT(&huart2, usart2cdc_rx, 1) != HAL_OK) return USBD_FAIL; /* Configure USB transmit timer */ husbtimer.Instance = TIM3; husbtimer.Init.Period = 10000 - 1; /* approx 10ms, I think... */ husbtimer.Init.Prescaler = 48-1; husbtimer.Init.ClockDivision = 0; husbtimer.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_Base_Init(&husbtimer) != HAL_OK) return USBD_FAIL; __TIM3_CLK_ENABLE(); NVIC_SetPriority(TIM3_IRQn, USB_TIMER_IRQ_PRIORITY); NVIC_EnableIRQ(TIM3_IRQn); /* Set Application USB Buffers */ USBD_CDC_SetTxBuffer(husb, usart2cdc_tx, 0); /* don't send anything now */ USBD_CDC_SetRxBuffer(husb, cdc2usart_buf_a); /* read into here if CDC data comes */ return USBD_OK; }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; int i =0; /* USER CODE BEGIN 7 */ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDevice_0->pClassData; for(i=0;i<Len;i++){ TxBuffer[i] = Buf[i]; } while (hcdc->TxState != 0); USBD_CDC_SetTxBuffer(hUsbDevice_0, TxBuffer, Len); result = USBD_CDC_TransmitPacket(hUsbDevice_0); while (hcdc->TxState != 0); /* USER CODE END 7 */ return result; }
/** * @brief CDC_Transmit_FS * Data send over USB IN endpoint are sent over CDC interface * through this function. * @note * * * @param Buf: Buffer of data to be send * @param Len: Number of data to be send (in bytes) * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY */ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) { uint8_t result = USBD_OK; /* USER CODE BEGIN 8 */ // Если устройство не подключено if( hUsbDevice_0 == 0 ) return USBD_FAIL; // Скопировать буффер memcpy ( UserTxBufferFS, Buf, Len ); // Отправить буфер USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, Len); result = USBD_CDC_TransmitPacket(hUsbDevice_0); /* USER CODE END 8 */ return result; }
/** * @brief CDC_Receive_FS * Data received over USB OUT endpoint are sent over CDC interface * through this function. * * @note * This function will block any OUT packet reception on USB endpoint * untill exiting this function. If you exit this function before transfer * is complete on CDC interface (ie. using DMA controller) it will result * in receiving more data while previous ones are still not sent. * * @param Buf: Buffer of data to be received * @param Len: Number of data received (in bytes) * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) { /* USER CODE BEGIN 7 */ // uncomment this for you know what //#define CDCecho #ifdef CDCecho for (int i = 0; i < *Len; i++) UserTxBufferFS[i] = UserRxBufferFS[i]; USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &UserTxBufferFS[0], *Len); USBD_CDC_TransmitPacket(&hUsbDeviceFS); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &UserRxBufferFS[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); #endif return (USBD_OK); /* USER CODE END 7 */ }