How to transmit and receive data over USB composite Serial

Post here first, or if you can't find a relevant section!
kejuqeju
Posts: 7
Joined: Sat May 09, 2020 6:55 am

How to transmit and receive data over USB composite Serial

Post by kejuqeju »

Hello there...
i succeeded to install roger's bootloader for bluepill, and succeeded to upload compiled sketch code via bluepill's USB (maple serial).
Now i need to receive and send byte data over USB (maple serial)
the receive must be interrupt type, so i dont need to poll for new data.
Can anyone help me with sample code?
thank you
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: How to transmit and receive data over USB composite Serial

Post by stevestrong »

You don not need USB composite library for that.
Simply :

Code: Select all

Serial.println("Hello!");

Code: Select all

while(Serial.available())
    Serial.write(Serial.read()); // echo back the read data
At the end, you have to somehow check for received the data.
The USB serial has a 256 bytes receive buffer, so it can store 255 bytes before losing data.
ag123
Posts: 1668
Joined: Thu Dec 19, 2019 5:30 am
Answers: 25

Re: How to transmit and receive data over USB composite Serial

Post by ag123 »

just do that in loop(), there isn't anything else running, arduino is bare metal codes unlike desktop pcs.
interrupts etc isn't necessary
kejuqeju
Posts: 7
Joined: Sat May 09, 2020 6:55 am

Re: How to transmit and receive data over USB composite Serial

Post by kejuqeju »

i tested it, and it worked fine. thank you.
i tried Serial1, Serial2, Serial3 and they worked fine too.
i assume that each hardware serial port has 256 bytes receive buffer too, same as USB serial?
kejuqeju
Posts: 7
Joined: Sat May 09, 2020 6:55 am

Re: How to transmit and receive data over USB composite Serial

Post by kejuqeju »

ag123 wrote: Sun May 10, 2020 10:43 am just do that in loop(), there isn't anything else running, arduino is bare metal codes unlike desktop pcs.
interrupts etc isn't necessary
ok, got it.
i used to create code for AVR family using CVAVR, i am quite new with arduino IDE. I like using CVAVR because i think i can control almost all the process, unlike arduino that depends much on libraries that i don't know how they work. i used to use interrupts and its handler, and i can set the receive buffer size myself. however, since i am new in arduino environment, i will just follow your and other's suggestions at first.

one more question.. if i use arduino IDE for bluepill, do you think arduino IDE can take advantage of all STM32 features? like CAN, DMA, USB device, RTC, PWM, etc..
or should i learn another IDE ?
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: How to transmit and receive data over USB composite Serial

Post by stevestrong »

You can program anything you want in Arduino.
The features do not depend on the used IDE.
ag123
Posts: 1668
Joined: Thu Dec 19, 2019 5:30 am
Answers: 25

Re: How to transmit and receive data over USB composite Serial

Post by ag123 »

like steve mentioned that's basically correct. Arduino IDE is just an IDE.
if u'd like you can write all the codes using a simple text editor ino, c++ files and make a batch file / script to compile them all.
for the actual functionality review documents provided with the core (e.g. on github, wiki etc). review the codes and examples in the core
and importantly review the reference manuals from ST.

a way i like to handle usb-serial is

Code: Select all

// Serial here is actually USB serial.
void loop() {
  //the PC send us something
  if(Serial.available()) {
    while(Serial.available()) {
      int c = Serial.read(); // 1 character
      switch(c) {
      case 'a':
      	//do this
      	break;
      case 'b':
      	//do that
      	break;
      	.etc
      }
    }
  }
  // else if the PC didn't send us anything, we send our data to the PC
  Serial.write(data); // 1 byte
  Serial.println("something"); // prints 'something' that whole word is send to the host/PC
  Serial.write(BUFFER, BUFFER_SIZE); //send the whole buffer
}
this is polling nevertheless, but so long as you don't hold things up in the sub routines, the turn around can be pretty fast so that the loop() keep running.
if you need to further separate things out, you can use interrupt handlers (e.g. in the hardware timers, you can use hardware timers to call interrupt handlers to handle other tasks periodically. and if you were to venture into DMA, which some consider an 'advanced' usage (i.e. not the simple hello world or blinky use case).
you would find that DMA can literally take care of reading / writing whole blocks data from pheriperials e.g. adc.
DMA is one of the more 'esoteric' features of stm32 (perhaps other ARM socs sport them too), it is one of the reasons you can do pretty high performance stuff with even the simple stm32f103.
e.g. for sending stuff to the host, i normally have DMA collect data in a buffer and i simply transmit the whole buffer to the host, that last statement
returning data to the host/PC can be interspersed in your codes that reads the data from the host. e.g. if each of those 'case' in switch is a single char command, you can Serial.print("something") in each case to revert the response to the pc.

there are a lot of materials about stm32 pheriperials and dma on the web and the other important source of info are the reference manuals from ST.
just google for your soc and have 'reference manual' in the keywords in addition.
kejuqeju
Posts: 7
Joined: Sat May 09, 2020 6:55 am

Re: How to transmit and receive data over USB composite Serial

Post by kejuqeju »

thank you for your suggestions, steve and ag123.
i think that arduino IDE is formerly purposed for beginners to "learn to program microcontroller in easy way" (CMIIW), so i doubt about its capability to compile complex code for such powerful and sophisticated controller like ARMs
i know there are other powerful IDE like eclipse, keil uvision, etc. can arduino IDE match their capability?
i use CVAVR to create code for AVR microcontrollers. It's BIN file usually smaller than arduino's BIN
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: How to transmit and receive data over USB composite Serial

Post by stas2z »

arduino is just a framework (or library, no matter) for very easy and fast start, it configures hardware for you and provides simple api for basic things
arduino ide is not an ide to be honest, it's just a text editor with syntax highlighting and basic core selection/compile/upload uptions
it can't be compared with real ides tbh
ag123
Posts: 1668
Joined: Thu Dec 19, 2019 5:30 am
Answers: 25

Re: How to transmit and receive data over USB composite Serial

Post by ag123 »

for IDE i'm using eclipse, there is also vscode which is pretty good. search the forum for eclipse and vscode and even platformio.
they are the more popular IDEs. i think even Arduino is coming out with a new Arduino 'pro' IDE that is based on the similar javascript technology as VS Code.

If you are starting out with Arduino, you should make sketches work in the Arduino IDE. e.g. program with it. That is kind of a baseline
After a while, i wanted features like code reference jumps and debug, and the more elaborate IDEs has them.
in addition, i'm using a makefile rather than arduino IDE. these can be deemed 'advanced' use case,
the risk of taking that detour is one may end up with a setup that builds with the core but doesn't build with Arduino IDE.
hence Arduino IDE is still deemed a baseline and if i want to distribute codes, i'd normally build that again in the Arduino IDE to see that it builds.
Post Reply

Return to “General discussion”