/** * @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); result = USBD_CDC_TransmitPacket(hUsbDevice_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 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; }
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 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_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; }
/** * @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 */ }
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_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_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 */ }
/** * @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; }
static uint8_t USBD_CDC_SOF (struct _USBD_HandleTypeDef *pdev) { uint32_t buffsize, write_index; USBD_CDC_HandleTypeDef *hcdc = context; unsigned index; for (index = 0; index < NUM_OF_CDC_UARTS; index++,hcdc++) { write_index = INBOUND_BUFFER_SIZE - hcdc->hdma_rx.Instance->CNDTR; /* the circular DMA should reset CNDTR when it reaches zero, but just in case it is briefly zero, we fix the value */ if (INBOUND_BUFFER_SIZE == write_index) write_index = 0; if(hcdc->InboundBufferReadIndex != write_index) { if(hcdc->InboundBufferReadIndex > write_index) { /* write index has looped around, so send partial data from the write index to the end of the buffer */ buffsize = INBOUND_BUFFER_SIZE - hcdc->InboundBufferReadIndex; } else { /* send all data between read index and write index */ buffsize = write_index - hcdc->InboundBufferReadIndex; } if(USBD_CDC_TransmitPacket(pdev, index, hcdc->InboundBufferReadIndex, buffsize) == USBD_OK) { hcdc->InboundBufferReadIndex += buffsize; /* if we've reached the end of the buffer, loop around to the beginning */ if (hcdc->InboundBufferReadIndex == INBOUND_BUFFER_SIZE) { hcdc->InboundBufferReadIndex = 0; } } } if (hcdc->OutboundTransferNeedsRenewal) /* if there is a lingering request needed due to a HAL_BUSY, retry it */ USBD_CDC_ReceivePacket(pdev, index); } return USBD_OK; }
/** * @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 6 */ uint8_t result = USBD_OK; static uint8_t buff_RX[512]; static uint8_t buff_TX[512]; int i = 0; for (i = 0; i < *Len; i++) buff_TX[i] = buff_RX[i]; USBD_CDC_SetTxBuffer(hUsbDevice_0, &buff_TX[0], *Len); USBD_CDC_TransmitPacket(hUsbDevice_0); USBD_CDC_SetRxBuffer(hUsbDevice_0, &buff_RX[0]); USBD_CDC_ReceivePacket(hUsbDevice_0); return (result); /* USER CODE END 6 */ }
/** * @brief CDC_Transmit_FS * Not a callback. Call this function to try and transmit * largest contiguous chunk of usart2cdc buffer via USB. * * @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 */ static uint8_t CDC_Transmit_FS() { uint8_t result = USBD_OK; if(usart2cdc_rx == usart2cdc_tx) /* nothing to do */ return USBD_OK; int tx_len = usart2cdc_rx - usart2cdc_tx; if(tx_len < 0) { /* RX pointer has wrapped, so just TX to end of buffer */ tx_len = usart2cdc_buf + usart2cdc_SIZE - usart2cdc_tx; } USBD_CDC_SetTxBuffer(husb, usart2cdc_tx, tx_len); result = USBD_CDC_TransmitPacket(husb); if(result == USBD_OK) { /* advance the TX pointer only if we managed to send something */ usart2cdc_tx += tx_len; if(usart2cdc_tx >= usart2cdc_buf + usart2cdc_SIZE) usart2cdc_tx -= usart2cdc_SIZE; } return result; }
/* timer callback */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { uint32_t buffptr; uint32_t buffsize; if (UserTxBufPtrOut != UserTxBufPtrIn) { if (UserTxBufPtrOut > UserTxBufPtrIn) { 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 = 0; } } } }
void cdc_timer_isr(void) { uint32_t n; if(tx_wr != tx_rd) { if(tx_wr < tx_rd) { // The write index has wrapped around. // Tx from tx_rd to the end of fifo. n = TX_FIFO_SIZE - tx_rd; } else { // The write index is ahead of the read index // Tx from tx_rd to tx_wr - 1 n = tx_wr - tx_rd; } USBD_CDC_SetTxBuffer(&hUSBDDevice, &tx_fifo[tx_rd], n); if(USBD_CDC_TransmitPacket(&hUSBDDevice) == USBD_OK) { tx_rd += n; if (tx_rd == TX_FIFO_SIZE) { tx_rd = 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 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 ) { if ( ( last_tx_not_busy_timestamp != 0 ) && ( HAL_GetTick() - last_tx_not_busy_timestamp > (uint32_t)1000) ) { last_tx_not_busy_timestamp = 0; /* Empirical evidence shows that in this state, the transmit */ /* is stuck, and an endless "setup" interrupt is being received. */ /* As a result, we need to completely reinitialize the USB */ /* circuitry/driver to get this working again. */ //MX_USB_DEVICE_DeInit(); //MX_USB_DEVICE_Init(); //pCDC->TxState = 0; return USBD_BUSY; } else { return USBD_BUSY; } } 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; }
void CDC_Itf_Send(uint8_t* pbuf, uint32_t Len){ USBD_CDC_SetTxBuffer(&USBD_Device, pbuf, Len); if(USBD_CDC_TransmitPacket(&USBD_Device) == USBD_OK) { } }
/** * @brief CDC_Itf_Control * Manage the CDC class requests * @param Cmd: Command code * @param Buf: Buffer containing command data (request parameters) * @param Len: Number of data to be sent (in bytes) * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL */ static int8_t CDC_Itf_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length) { switch (cmd) { case CDC_SEND_ENCAPSULATED_COMMAND: USBD_CDC_SetTxBuffer(&USBD_Device, pbuf, length); if(USBD_CDC_TransmitPacket(&USBD_Device) != USBD_OK) return (USBD_FAIL); break; case CDC_GET_ENCAPSULATED_RESPONSE: /* Add your code here */ break; case CDC_SET_COMM_FEATURE: /* Add your code here */ break; case CDC_GET_COMM_FEATURE: /* Add your code here */ break; case CDC_CLEAR_COMM_FEATURE: /* Add your code here */ break; case CDC_SET_LINE_CODING: LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\ (pbuf[2] << 16) | (pbuf[3] << 24)); LineCoding.format = pbuf[4]; LineCoding.paritytype = pbuf[5]; LineCoding.datatype = pbuf[6]; break; case CDC_GET_LINE_CODING: pbuf[0] = (uint8_t)(LineCoding.bitrate); pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8); pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16); pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24); pbuf[4] = LineCoding.format; pbuf[5] = LineCoding.paritytype; pbuf[6] = LineCoding.datatype; /* Add your code here */ break; case CDC_SET_CONTROL_LINE_STATE: /* Add your code here */ break; case CDC_SEND_BREAK: /* Add your code here */ break; default: break; } return (USBD_OK); }
void USB_Handler(uint8_t* Buf, uint32_t *Len) { int sz; RTC_DateTypeDef sDate; RTC_TimeTypeDef sTime; if (UserRxBufferFS[0] == 0xAB) { settingsPacket.startByte = UserRxBufferFS[0]; settingsPacket.command = UserRxBufferFS[1]; for (int i = 0; i < 16; i++) settingsPacket.data[i] = UserRxBufferFS[i + 2]; switch (settingsPacket.command) { case READ_ID: settingsPacket.data[0] = 1; strcpy((char *) &settingsPacket.data[1], (char *) &device.settings->firmware.VERSION); break; case SET_TIME: sDate.Year = settingsPacket.data[0]; sDate.Month = settingsPacket.data[1]; sDate.Date = settingsPacket.data[2]; sTime.Hours = settingsPacket.data[3]; sTime.Minutes = settingsPacket.data[4]; sTime.Seconds = settingsPacket.data[5]; settingsPacket.data[0] = 1; #ifdef __RTC__ HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN); HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN); #endif break; case GET_DOOR_EVENT: settingsPacket.data[0] = 1; HAL_I2C_Mem_Read(&hi2c1, EEPROM_CODE, INIT_EEPROM + 4, I2C_MEMADD_SIZE_8BIT, (uint8_t*) device.door, sizeof(DOOR_T), 1000); device.door->STATUS = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3); settingsPacket.data[1] = device.door->STATUS; #ifdef __DEDUG__ printf("door->STATUS %u\n", device.door->STATUS); #endif settingsPacket.data[2] = device.door->COUNT; settingsPacket.data[3] = device.door->TIME.Hours; settingsPacket.data[4] = device.door->TIME.Minutes; settingsPacket.data[5] = device.door->TIME.Seconds; settingsPacket.data[6] = device.door->DATE.Date; settingsPacket.data[7] = device.door->DATE.Month; settingsPacket.data[8] = device.door->DATE.Year; break; case READ_VERSION: settingsPacket.data[0] = 1; strcpy(&settingsPacket.data[1], &device.settings->firmware.VERSION[0]); break; case RESET_DOOR_EVENT: memset(device.door, 0, sizeof(DOOR_T)); HAL_I2C_Mem_Write(&hi2c1, EEPROM_CODE, INIT_EEPROM + 4, I2C_MEMADD_SIZE_8BIT, (uint8_t*) device.door, sizeof(DOOR_T), 1000); // HAL_I2C_Mem_Read(&hi2c1, EEPROM_CODE, INIT_EEPROM + 4, I2C_MEMADD_SIZE_8BIT, // (uint8_t*) device.door, sizeof(DOOR_T), 1000); break; } // for (int i = 0; i < *Len; i++) // UserTxBufferFS[i] = UserRxBufferFS[i]; // // USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &UserTxBufferFS[0], *Len); USBD_CDC_SetTxBuffer(&hUsbDeviceFS, (uint8_t*) &settingsPacket, sizeof(settingsPacket)); USBD_CDC_TransmitPacket(&hUsbDeviceFS); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &UserRxBufferFS[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); } else { settingsPacket.startByte = UserRxBufferFS[0]; USBD_CDC_SetTxBuffer(&hUsbDeviceFS, (uint8_t*) &settingsPacket, 1); USBD_CDC_TransmitPacket(&hUsbDeviceFS); USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &UserRxBufferFS[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); } }