Device initialization when serial port is connected

Post here first, or if you can't find a relevant section!
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Device initialization when serial port is connected

Post by hyur »

I am using the stm32f103c8t6 board.

I'm developing middleware right now, and I want to connect stm32.
Here I found the problem, there are two in total.

1. Open the Arduino IDE serial monitor window and enable serial communication to send and receive messages when connecting middleware.
(After uploading the source, when connecting directly from the middleware without opening the Arduino IDE serial monitor, the message does not pass to the middleware.)

2. Arduino knows that the device is initialized when connected to serial, but my STM32 is not.

These are the two issues I'm having.

Help.
by GonzoG » Tue Aug 16, 2022 8:19 am
hyur wrote: Tue Aug 16, 2022 2:18 am I understood your words.
Nevertheless, I want to restart the device from setup() when connecting to the serial port, is that possible?

If possible, is there a software way to make the device restart like pressing the reset button?
It won't be that easy.
Hardware USB support works different. On Arduino boards it's done in bootloader. It reboots board in different mode and the connection between main MCU and USB MCU is established even if there is no connection with PC, so for your program connection is "always-on".
If you put reset in your program, it will reset MCU and this will terminate USB-Serial connection, so after reboot it will be off and will reinitialize again during boot.
You would need to save a flag in registers so during reboot it would know that this reboot was caused by serial connection.


But first, why you need your board to reset when USB connection is established ??
As I wrote before, you can check from program if connection is active, or not and you can write a program that does different things if there is active USB connection or not.
You can write a function that runs when there is an active connection, and one that runs when there is no connection, or you can write a loop that waits for connection set program to run only when there is active connection.
Go to full post
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Device initialization when serial port is connected

Post by GonzoG »

ad. 1. Looks like some error in code.

ad 2. I don't understand what you mean.
Is it about that when you open serial monitor (some) Arduino boards reset ??
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: Device initialization when serial port is connected

Post by hyur »

@GonzoG

Thank you for answer.

Q 2 : It is said that the Arduino board is automatically reset whenever the serial monitor is opened through the DTR signal line.
Does the STM32duino also have this feature?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Device initialization when serial port is connected

Post by GonzoG »

hyur wrote: Tue Aug 16, 2022 12:25 am Q 2 : It is said that the Arduino board is automatically reset whenever the serial monitor is opened through the DTR signal line.
Does the STM32duino also have this feature?
It's true, but mostly false.
Some Arduino boards are reset when you initialize USB-serial communication. But it is needed as those boards do not have USB support in MCU and use additional MCU for USB.
It's not a feature, it's mostly a problem.

Arduino boards with USB support in MCU do not reset when you initialize USB-Serial communication.

STM32 chips have USB support and don't need to reset. You can connect to them at any time without resetting running program.
You can also check from program if USB connection is open, which cannot be done on Arduino boards that do not have USB (Uno, Nano, Mega).
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: Device initialization when serial port is connected

Post by hyur »

It's true, but mostly false.
Some Arduino boards are reset when you initialize USB-serial communication. But it is needed as those boards do not have USB support in MCU and use additional MCU for USB.
It's not a feature, it's mostly a problem.

Arduino boards with USB support in MCU do not reset when you initialize USB-Serial communication.

STM32 chips have USB support and don't need to reset. You can connect to them at any time without resetting running program.
You can also check from program if USB connection is open, which cannot be done on Arduino boards that do not have USB (Uno, Nano, Mega).
I understood your words.
Nevertheless, I want to restart the device from setup() when connecting to the serial port, is that possible?

If possible, is there a software way to make the device restart like pressing the reset button?
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Device initialization when serial port is connected

Post by ag123 »

you can call HAL_NVIC_SystemReset().
one way is to write your own serial command processor
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: Device initialization when serial port is connected

Post by hyur »

you can call HAL_NVIC_SystemReset().
one way is to write your own serial command processor
I looked up the content related to NVIC_SystemReset().

I expect this function to achieve what I want, but The board I'm using is Roger's, so I can't use the function.
Can I get a hint about the registers used inside NVIC_SystemReset() ?
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Device initialization when serial port is connected

Post by ag123 »

it is there as well, simply do a text search in the source codes, nvic or systemreset would turn it up, ignore case for the search so that it is easier to get a hit.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Device initialization when serial port is connected

Post by GonzoG »

hyur wrote: Tue Aug 16, 2022 2:18 am I understood your words.
Nevertheless, I want to restart the device from setup() when connecting to the serial port, is that possible?

If possible, is there a software way to make the device restart like pressing the reset button?
It won't be that easy.
Hardware USB support works different. On Arduino boards it's done in bootloader. It reboots board in different mode and the connection between main MCU and USB MCU is established even if there is no connection with PC, so for your program connection is "always-on".
If you put reset in your program, it will reset MCU and this will terminate USB-Serial connection, so after reboot it will be off and will reinitialize again during boot.
You would need to save a flag in registers so during reboot it would know that this reboot was caused by serial connection.


But first, why you need your board to reset when USB connection is established ??
As I wrote before, you can check from program if connection is active, or not and you can write a program that does different things if there is active USB connection or not.
You can write a function that runs when there is an active connection, and one that runs when there is no connection, or you can write a loop that waits for connection set program to run only when there is active connection.
hyur
Posts: 36
Joined: Fri May 27, 2022 7:42 am
Answers: 2

Re: Device initialization when serial port is connected

Post by hyur »

It won't be that easy.
Hardware USB support works different. On Arduino boards it's done in bootloader. It reboots board in different mode and the connection between main MCU and USB MCU is established even if there is no connection with PC, so for your program connection is "always-on".
If you put reset in your program, it will reset MCU and this will terminate USB-Serial connection, so after reboot it will be off and will reinitialize again during boot.
You would need to save a flag in registers so during reboot it would know that this reboot was caused by serial connection.


But first, why you need your board to reset when USB connection is established ??
As I wrote before, you can check from program if connection is active, or not and you can write a program that does different things if there is active USB connection or not.
You can write a function that runs when there is an active connection, and one that runs when there is no connection, or you can write a loop that waits for connection set program to run only when there is active connection.
I understood what you were saying.
I don't want to proceed with a device reset as you said.

Thanks for helping me a lot.
Post Reply

Return to “General discussion”