[Solved]How to use USB micro port on BluePill

Post here all questions related to STM32 core if you can't find a relevant section!
Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

[Solved]How to use USB micro port on BluePill

Post by Jimbo13 »

Advice please for a BluePill beginner.
I am programming with an ST-LINK.
How do I setup and use the micro USB port on the BluPill board to output data from my programme?
There are 2 optional settings in the Arduino Tools section, USB support and USB speed.
I am not sure how to set these.
Then in the programme how do I start the serial and use it.
I tried without success:
SERIAL_PORT_MONITOR.begin(9600);
SERIAL_PORT_MONITOR.println("ASCII Table");
Last edited by Jimbo13 on Tue Mar 31, 2020 10:32 am, edited 1 time in total.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use USB micro port on BluePill

Post by fpiSTM »

You should have this:
BP.png
BP.png (21.22 KiB) Viewed 15016 times
Then simply use "Serial".
Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

Re: How to use USB micro port on BluePill

Post by Jimbo13 »

Hi fpiSTM, Still not working, the only difference to what you suggest is that I am not using a bootloader as in your example, I am using STM32CubeProgrammer v2.3.0 with a Chinese ST-LINK.

I am using CoolTerm but cannot see a USB port other than the ST-LINK.

This is my programme:

Code: Select all

void setup() {
    pinMode(PC13, OUTPUT);// Set up the built-in LED pin as an output:
    Serial.begin(9600);
}
void loop() {
    digitalWrite(PC13, !digitalRead(PC13));
    delay(5000);         
    Serial.println("Hello Jim");
}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use USB micro port on BluePill

Post by fpiSTM »

Right, up to you to select the best upload method for you. ;)
BP is known to have an issue with a wrong resistor value for USB detection so it can't enumerate properly.
And there is also some fake BP.

on which os you run? Linux? windows ?
Try to see if there is an unknow device.
Using dmeg on linux or the device manager on Windows.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: How to use USB micro port on BluePill

Post by ag123 »

in linux try something like this
https://github.com/jkulesza/usbreset

in windows, i'm not too sure if 'scan for new hardware' helps, i think it is accessible from device manager
https://answers.microsoft.com/en-us/win ... 2f51929e3c

what is missing is apparently a 'usb reset' (single ended zero) signal is not received by the host or some such problem
so if the host can send a 'usb reset' to all the devices, it may enumerate and show up
Jimbo13
Posts: 12
Joined: Sun Feb 16, 2020 6:10 pm
Location: UK

Re: How to use USB micro port on BluePill

Post by Jimbo13 »

OK I think I have sorted this out.
BP is known to have an issue with a wrong resistor value for USB detection so it can't enumerate properly.
"USB Enumeration is the process of detecting, identifying and loading drivers for a USB device". I replaced resistor R10 on the underside of the BluePill. It was an 006 size resistor marked as 103 i.e 10k. I replaced it with a 008 size 1.5k. I don't think this made any difference, the fault was laziness on my part for not looking carefully at the problem.
To programme the BluePill, Arduino appeared to require a port number for the ST-LINK In "Arduino - Tool - Port" so I selected the only visible COM port, COM3 thinking it was the ST-LINK. The upload process worked and the led blink rate changed as proof. When I opened CoolTerm I could only see COM3 which I assumed was the ST-LINK and that the BluePill was not visible.
When I looked in Win 10 Device Manager I could see the ST-LINK under "Universal Serial Bus Devices" and under "Ports(COM and lpt)" just one "USB serial Device COM 3" with no description to identify it, but by unplugging the ST-LINK the COM3 remained visible so identifying the BluePill as the remaining plugged in COM3 and not the ST-LINK as I had assumed. I now opened CoolTerm and selected COM3 and was able to see the data from my BluePill.
I deduced that the STM32CubeProgrammer finds the ST-LINK ignoring the selection in "Arduino - Tool - Port".
One final thing which I don't yet understand is that whatever baud I select in "Serial.begin(baud);" and whatever receiving board rate I select in the Arduino Monitor or CoolTerm (9600 or 115200 etc) the data from BluePill is still displayed correctly. Probably something on my computer, any suggestions welcome.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: How to use USB micro port on BluePill

Post by ag123 »

if you are using roger's (maple usb dfu) boot loader. the boot loader first enumerates as
1EAF:0003
this normally works correctly

then the boot loader starts the core. the core then tries to enumerate the same port as
1EAF:0004 - that is for libmaple (maple mini, et.al)

the trouble is while the core is doing that it needs to send a usb reset (single ended zero) to the host and tell the host to enumerate
i'm not sure why this is happening in that it seem this single ended zero is either not transmitted or that the host simply didn't recognise the single ended zero.

i've tested in linux usbreset works, it gets the port to enumerate. this is even true for ST internal on chip DFU boot loader
https://github.com/jkulesza/usbreset
i'm not too sure how to do that in winndows, maybe someone can add to that
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: How to use USB micro port on BluePill

Post by fpiSTM »

Baudrate is not relevant usine USB VCP.
pwrgreg007
Posts: 13
Joined: Sun Jan 09, 2022 2:56 am
Location: LaGrange, GA

Re: [Solved]How to use USB micro port on BluePill

Post by pwrgreg007 »

Hi all, I'm new to the forum. I see this thread is a couple years old, but didn't want to open a new one for the same issue. If I should do so, or post elsewhere, please advise and accept my apologies in advance. I have been programming for 40 years but this is a completely new environment, so my experience isn't worth much as far as the new programs and hardware go. But I have been programming in C or C# for a long time so the code, libraries, compiling, and linking is not a mystery.
I am on Windows 10 Home. I use STM32 ST-LINK Utility v4.6.0.0 and the ST-Link V2 (SWD) to download programs. I'm primarily concerned with using the on-board USB as a serial port, not so much with downloading a program with Arduino, although that would be nice (bootloader is another subject entirely). I am using Arduino 1.8.19 and the latest 'STM32 MCU based boards' board files, v2.2.0. There are a bunch of 'STM32...' files in the library manager but I don't see anything specific to USB. I am trying to stay away from the Maple drivers since my understanding is that Roger Clark's code is no longer supported. I am hoping that there is native STM library support for the USB port.
I got a couple of Blue Pills from "aideepen" on Amazon, figuring I had a better chance at the real deal vs ebay (apologies to ebay), since I had heard about 'fakes' being sold. The chip appears to be a genuine STM32F103C8T6, although the mfg. code is "CHN". It has a single spot at the pin 1 corner I would call 'rougher in texture', not a dimple.
Everything that I've tested so far works except USB. I have an ILI9341 TFT display and a WWVB receiver attached, and am using the "WWVB Clock" program from Bruce Hall (search for "Bruce Hall WWVB Clock Project" to find the pfd file for this project). This is a great project to demonstrate the power of this chip and board, and I'm sure there are many others. For this particular project, the PA12 input for WWVB signal was moved to PA10 since PA12 & PA11 are used for the USB port. The program functions as shown in the project file, syncing & displaying the time. At power-up, PA12 is high, but that's almost certainly due to the 152 (1500 ohm) pull-up resistor at R10.
To test the correct USB operation only, I have a simple test program from earlier in this thread (actually it may be from another thread), setting up 'Serial' the same but slightly modified to echo any serial input to the output:

Code: Select all

void setup() {
  Serial.begin();
  digitalWrite(LED_BUILTIN,0); //on blue pill it is active low, so this is 'ON'
  while(! Serial.available() );
  digitalWrite(LED_BUILTIN,1); 
  
  Serial.println("you are connected");
}

void loop() {
  // if a character is available, echo to output
  if (Serial.available())
  {
    Serial.write(Serial.read());
  }
}
I compiled and downloaded this program using ST-LINK Utility. When I plug the board into a USB board, I get 'device unrecognized'. The LED stays on, so is not getting past the call to Serial.available(). I don't see how this code knows that 'Serial' is referring to the USB device. This chip supports U(S)ART channels as well as the USB. Is there a specific file I need to include, or library calls to make, to specify that the port I want is the USB port? I'm probably overlooking something really simple.
Thanks for taking a look!
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: [Solved]How to use USB micro port on BluePill

Post by GonzoG »

pwrgreg007 wrote: Sat Jan 29, 2022 10:02 pm (...) I'm primarily concerned with using the on-board USB as a serial port, (...)
I am using Arduino 1.8.19 and the latest 'STM32 MCU based boards' board files, v2.2.0. There are a bunch of 'STM32...' files in the library manager but I don't see anything specific to USB. I am trying to stay away from the Maple drivers since my understanding is that Roger Clark's code is no longer supported. I am hoping that there is native STM library support for the USB port.
You don't need any additional libraries for Serial USB. It's a built-in feature, just enable USB CDC support (generic "Serial" supersede USART) in board confing in Arduino IDE.
pwrgreg007 wrote: Sat Jan 29, 2022 10:02 pm To test the correct USB operation only, I have a simple test program from earlier in this thread (actually it may be from another thread), setting up 'Serial' the same but slightly modified to echo any serial input to the output:

Code: Select all

void setup() {
  Serial.begin();
  digitalWrite(LED_BUILTIN,0); //on blue pill it is active low, so this is 'ON'
  while(! Serial.available() );
  digitalWrite(LED_BUILTIN,1); 
  
  Serial.println("you are connected");
}

void loop() {
  // if a character is available, echo to output
  if (Serial.available())
  {
    Serial.write(Serial.read());
  }
}
First, the code is wrong.
It should be:

Code: Select all

while (!Serial);
not

Code: Select all

while(!Serial.available());
pwrgreg007 wrote: Sat Jan 29, 2022 10:02 pm I compiled and downloaded this program using ST-LINK Utility. When I plug the board into a USB board, I get 'device unrecognized'. The LED stays on, so is not getting past the call to Serial.available(). I don't see how this code knows that 'Serial' is referring to the USB device. This chip supports U(S)ART channels as well as the USB. Is there a specific file I need to include, or library calls to make, to specify that the port I want is the USB port? I'm probably overlooking something really simple.
Thanks for taking a look!
If you've set up CDC (generic "Serial" supersede USART) then you've told compiler that "Serial" means USB port.
Post Reply

Return to “General discussion”