2017-05-11 06:20 AM
Hi,
I am trying to send simple commands via USB to generate four PWM signals. Two USB cables are plugged-in: USB ST-Link and USB User. Everything works when hex code is freshly programmed. Serial device is present in the system, I can open it with Real Term/putty and all commands are executed correctly. When I click 'Reset' after a less than a second I can reconnect and everything is still working. However, when I disconnect ST-Link cabe (Power off) and connect it again COM port cannot be open anymore despite the fact that Serial device (COM3) is still present in the Device Manager. In order to make it working again I have to upload some other hex code and correct program afterwards. I was trying with external power but I 've got the same issue.
Some code below:
In 'usbd_cdc_if.c':
#define APP_RX_DATA_SIZE 4
#define APP_TX_DATA_SIZE 4
static int8_t CDC_Init_FS(void)
{
hUsbDevice_0 = &hUsbDeviceFS;
/* USER CODE BEGIN 3 */
/* Set Application Buffers */
USBD_CDC_SetTxBuffer(hUsbDevice_0, UserTxBufferFS, 0);
USBD_CDC_SetRxBuffer(hUsbDevice_0, UserRxBufferFS);
USBD_CDC_ReceivePacket(hUsbDevice_0);
return (USBD_OK);
/* USER CODE END 3 */
}
static int8_t CDC_DeInit_FS(void)
{
/* USER CODE BEGIN 4 */
hUsbDevice_0 = NULL;
return (USBD_OK);
/* USER CODE END 4 */
}
static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
extern uint8_t ReceivedData[40];
extern uint8_t ReceivedDataFlag;
uint8_t iter;
for(iter = 0; iter<40; ++iter){
ReceivedData[iter] = 0;
}
strlcpy(ReceivedData, Buf, (*Len) + 1); // Copy data to our table
ReceivedDataFlag++; // Set Received Flag
USBD_CDC_ReceivePacket(hUsbDevice_0);
return (USBD_OK);
/* USER CODE END 6 */
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
In main.c I am only checking if the ReceivedDataFlag is set and parse the command:
(I 've skipped code that was not related to the USB communication)
#include 'stm32f4xx_hal.h'
#include 'usb_device.h'
uint8_t ReceivedData[40];
uint8_t ReceivedDataFlag = 0;
MX_USB_DEVICE_Init();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(ReceivedDataFlag == 1){
ReceivedDataFlag = 0;
//SERVO 1 - CHAN 1 - ABC
if( strcmp( (char *)ReceivedData, 'a') == 0 ){
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 6);
}
if( strcmp( (char *)ReceivedData, 'b') == 0 ){
__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 15);
}
etc ....
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
Any advice most welcome!
Thank you for your help.
B.
#cdc_receive_fs #cdc #usb