PC not detect the USB port of a stm32f4discovery

Oct 24, 2012 at 1:02 PM
Edited Oct 24, 2012 at 1:03 PM

I take an example from http://guruce.com/blogpost/communicating-with-your-microframework-application-over-usb it works (I check it in debuger) but PC not find a new device. What could be causing this ? Whether stm32f4discovery can work simultaneously under the bootloader and as usb device ?

Nov 1, 2012 at 4:34 AM
Edited Nov 1, 2012 at 4:37 AM

Hyperterminal cant open (virtual) COM port. Pls, advise something to fix it

using System;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware.UsbClient;

namespace MFConsoleApplication1
{
	public class Program
	{
		private const int WRITE_EP = 0x01;
		private const int READ_EP = 0x02;
		private const int CTRL_EP = 0x03;

		static Microsoft.SPOT.Hardware.OutputPort mGreenLed = new Microsoft.SPOT.Hardware.OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)60, false);
		static Microsoft.SPOT.Hardware.OutputPort mOrangeLed = new Microsoft.SPOT.Hardware.OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)61, false);
		static Microsoft.SPOT.Hardware.OutputPort mRedLed = new Microsoft.SPOT.Hardware.OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)62, false);
		static Microsoft.SPOT.Hardware.OutputPort mBlueLed = new Microsoft.SPOT.Hardware.OutputPort((Microsoft.SPOT.Hardware.Cpu.Pin)63, false);

		static Microsoft.SPOT.Hardware.InputPort mUserButton = new Microsoft.SPOT.Hardware.InputPort(Microsoft.SPOT.Hardware.STM32.Pins.GPIO_PIN_A_
0, true, Microsoft.SPOT.Hardware.Port.ResistorMode.PullUp);

		public static void Main()
		{
			mGreenLed.Write(true);

			try
			{
				// See if the hardware supports USB
				UsbController[] controllers = UsbController.GetControllers();

				// Bail out if USB is not supported on this hardware!
				if (0 == controllers.Length)
					throw new SystemException("USB is not supported on this hardware!");

				// Find a free USB controller
				UsbController usbController = null;
				foreach (UsbController controller in controllers)
				{
					usbController = controller;
					if (UsbController.PortState.Stopped == controller.Status)
						break;
				}

				// If no free USB controller
				if (null == usbController)
					throw new System.Exception("All available USB controllers already in use. Set the device to use Ethernet or Serial debugging.");

				mBlueLed.Write(true);
				// Configure the USB controller
				if (!ConfigureUSBController(usbController))
					throw new System.Exception("USB stream could not be created, error " + usbController.ConfigurationError.ToString());
				using (UsbStream usbDataStream = usbController.CreateUsbStream(WRITE_EP, READ_EP))
				using (UsbStream usbControlStream = usbController.CreateUsbStream(Microsoft.SPOT.Hardware.UsbClient.UsbStream.NullEn
dpoint, CTRL_EP))
				{
				// Start our communication loop
					USBLoop(usbControlStream, usbDataStream);
				//usbDataStream.Close();
				}
			}
			catch (System.Exception aException)
			{
				mRedLed.Write(true);
				Debug.Print(aException.Message);
			}
		}

		static void USBLoop(UsbStream aCtrlStream, UsbStream aDataStream)
		{
			byte[] vDatas = new byte[100];
			byte[] vCtrls = new byte[100];
			for (;; )
			{
				int dataRead = aDataStream.Read(vDatas, 0, vDatas.Length);
				int ctrlRead = aCtrlStream.Read(vCtrls, 0, vCtrls.Length);
				if (dataRead > 0)
				{   // echo back!
					aDataStream.Write(vDatas, 0, dataRead);
				}
			}
		}

		private static bool ConfigureUSBController(UsbController usbController)
		{
/*
Device Descriptor:
  bLength				18
  bDescriptorType		 1
  bcdUSB			   2.00
  bDeviceClass			2 Communications
  bDeviceSubClass		 0 
  bDeviceProtocol		 0 
  bMaxPacketSize0		 8
  idVendor		   0x04d8 Microchip Technology, Inc.
  idProduct		  0x000a 
  bcdDevice			0.01
  iManufacturer		   1 
  iProduct				2 
  iSerial				 0 
  bNumConfigurations	  1
*/
			// Create the device descriptor
			Configuration.DeviceDescriptor device = new Configuration.DeviceDescriptor(0x0000, 0x0131, 0x0000);

			device.bcdUSB = 0x110;
			device.bDeviceClass = 0x02;	 // Vendor defined class
			device.bDeviceSubClass = 0x00;	 // Vendor defined subclass
			device.bDeviceProtocol = 0x00;
			device.bMaxPacketSize0 = 8;		// Maximum packet size of EP0
			device.iManufacturer = 1;		// String #1 is manufacturer name (see string descriptors below)
			device.iProduct = 2;		// String #2 is product name
			device.iSerialNumber = 3;		// String #3 is the serial number

/*
	Interface Descriptor:
	  bLength				 9
	  bDescriptorType		 4
	  bInterfaceNumber		0
	  bAlternateSetting	   0
	  bNumEndpoints		   1
	  bInterfaceClass		 2 Communications
	  bInterfaceSubClass	  2 Abstract (modem)
	  bInterfaceProtocol	  1 AT-commands (v.25ter)
	  iInterface			  0 
	  CDC Header:
		bcdCDC			   1.10
	  CDC ACM:
		bmCapabilities	   0x02
		  line coding and serial state
	  CDC Union:
		bMasterInterface		0
		bSlaveInterface		 1 
	  CDC Call Management:
		bmCapabilities	   0x00
		bDataInterface		  1
	  Endpoint Descriptor:
		bLength				 7
		bDescriptorType		 5
		bEndpointAddress	 0x82  EP 2 IN
		bmAttributes			3
		  Transfer Type			Interrupt
		  Synch Type			   None
		  Usage Type			   Data
		wMaxPacketSize	 0x0008  1x 8 bytes
		bInterval			   2
*/
			Configuration.Endpoint ctrlEP = new Configuration.Endpoint(CTRL_EP, Configuration.Endpoint.ATTRIB_Interrupt | Configuration.Endpoint.ATTRIB_Read);
			ctrlEP.wMaxPacketSize = 8;
			ctrlEP.bInterval = 2;

			Configuration.Endpoint[] usbCtrlEndpoints = new Configuration.Endpoint[] { ctrlEP };

						// Set up the USB interface
			Configuration.UsbInterface usbCtrlInterface = new Configuration.UsbInterface(0, usbCtrlEndpoints);
			usbCtrlInterface.bInterfaceClass = 0x02; // Communications
			usbCtrlInterface.bInterfaceSubClass = 0x02; // Abstract (modem)
			usbCtrlInterface.bInterfaceProtocol = 0x01; // 

/*
	Interface Descriptor:
	  bLength				 9
	  bDescriptorType		 4
	  bInterfaceNumber		1
	  bAlternateSetting	   0
	  bNumEndpoints		   2
	  bInterfaceClass		10 CDC Data
	  bInterfaceSubClass	  0 Unused
	  bInterfaceProtocol	  0 
	  iInterface			  0 
	  Endpoint Descriptor:
		bLength				 7
		bDescriptorType		 5
		bEndpointAddress	 0x03  EP 3 OUT
		bmAttributes			2
		  Transfer Type			Bulk
		  Synch Type			   None
		  Usage Type			   Data
		wMaxPacketSize	 0x0040  1x 64 bytes
		bInterval			   0
	  Endpoint Descriptor:
		bLength				 7
		bDescriptorType		 5
		bEndpointAddress	 0x83  EP 3 IN
		bmAttributes			2
		  Transfer Type			Bulk
		  Synch Type			   None
		  Usage Type			   Data
		wMaxPacketSize	 0x0040  1x 64 bytes
		bInterval			   0
*/
			// Create the endpoints
			Configuration.Endpoint writeEP = new Configuration.Endpoint(WRITE_EP, Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Write);
			writeEP.wMaxPacketSize = 64;
			writeEP.bInterval = 0;

			Configuration.Endpoint readEP = new Configuration.Endpoint(READ_EP, Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Read);
			readEP.wMaxPacketSize = 64;
			readEP.bInterval = 0;

			Configuration.Endpoint[] usbDataEndpoints = new Configuration.Endpoint[] { writeEP, readEP };


			Configuration.UsbInterface usbDataInterface = new Configuration.UsbInterface(1, usbDataEndpoints);
// ???
			usbDataInterface.bInterfaceClass = 10; // CDC Data
			usbDataInterface.bInterfaceSubClass = 0x00; // Unused
			usbDataInterface.bInterfaceProtocol = 0x00; // 

			// Create array of USB interfaces
			Configuration.UsbInterface[] usbInterfaces = new Configuration.UsbInterface[] { usbCtrlInterface, usbDataInterface };

			// Create configuration descriptor
			Configuration.ConfigurationDescriptor config = new Configuration.ConfigurationDescriptor(0, usbInterfaces);
			config.bmAttributes = 0xC0; // self powered

			// Create the string descriptors
			Configuration.StringDescriptor manufacturerName = new Configuration.StringDescriptor(1, "POSUA");
			Configuration.StringDescriptor productName = new Configuration.StringDescriptor(2, "POSUA LPOS-II-VFD USB CDC");
			Configuration.StringDescriptor serialNumber = new Configuration.StringDescriptor(3, "0000-0000-0000-0001");
			Configuration.StringDescriptor displayName = new Configuration.StringDescriptor(4, "POSUA LPOS-II-VFD USB CDC Driver");
			Configuration.StringDescriptor friendlyName = new Configuration.StringDescriptor(5, "POSUA_USB_CDC");

			// Create the final configuration
			Configuration configuration = new Configuration();
			configuration.descriptors = new Configuration.Descriptor[]
			{
				device,
				config,
				manufacturerName,
				productName,
				serialNumber,
				displayName,
				friendlyName
			};

			usbController.Configuration = configuration;
			return usbController.Start();
		}
	}
}
Nov 7, 2012 at 7:05 AM
VShaclein wrote:

Hyperterminal cant open (virtual) COM port. Pls, advise something to fix it

Note that the stm32f4 cpu has two USB ports including a high speed USB port that is not broken out to a socket (only pins) on the stm32f4discovery board, the stlink/v2 mini-USB port doesn't count because that's a different CPU, and the netmf full speed micro-USB port isn't available because that is dedicated for deploying and debugging.  You may have better luck if you wire another USB port to PB14 (D-, OTG_HS_DMUSART), PB15 (D+, OTG_HS_DPUSART), and GND.  But I haven't tried your code myself.

Nov 7, 2012 at 9:28 AM

Yes, it is right. I run an example in bulk user mode with 2 endpoints properly, but cant find an example or write a code of a standard cdc device to run it properly. Possible, an error is caused by using the usb port #0 and a collision with the bootloader.