Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTS DTR auto boot enter? #2

Open
NoNamedCat opened this issue Nov 8, 2023 · 60 comments
Open

RTS DTR auto boot enter? #2

NoNamedCat opened this issue Nov 8, 2023 · 60 comments

Comments

@NoNamedCat
Copy link

Hello again.
It is possible to implement this type of schema for the boot-loader in the code?

image

I'm designing a new board for the PY32F002A micro-controller, wondering about using the CH340X as a USB-Serial communication IC for the board. If this could be implemented, I think is a no brainer to choose that IC instead of the CH340N (That doesn't have DTR or RTS)

@NoNamedCat
Copy link
Author

This work well with the ESP8266 or the ESP32.
The EspTool (The program to burn the firmware for the ESP series) handle the reset line and the boot pin.
Do you think that is possible to implement this?

@wagiminator
Copy link
Owner

In PySerial you can directly set the control lines, e.g. "self.rts = True" (which means LOW on the RTS pin). So you can customize the software the way you want. By default RTS and DTR will be pulled low as soon as the communication begins. Keep in mind that this way of connection WILL cause problems, if you want to use it also for normal serial communication using a different serial monitor. By the way, the CH340N has an RTS pin. Take a look at the CH340E, which is quite small and has RTS, DTR and an LED pin.

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 9, 2023

Maybe adding a contraption based on transistors will help with this issue?
Like the DEV boards for the esp32 micro-controllers do?
Something like this:
Screenshot_2023-11-09_15-33-41

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 9, 2023

The CH340X is designed for not needing that transistor contraption...
That's why i think about using that usb-serial IC.
Please see chapter 5.3. DTR and multi-mode MCU download

Of this datasheet: https://cdn.sparkfun.com/assets/5/0/a/8/5/CH340DS1.PDF

BTW... I noted your MCU boards repository. I really like it and that's why I'm designing a new board. I am really grateful for you sharing all of this.

Screenshot_2023-11-09_15-46-27

@wagiminator
Copy link
Owner

This looks like a clever solution. Just give it a try!

wagiminator added a commit that referenced this issue Nov 10, 2023
@wagiminator
Copy link
Owner

I added the additional control functions of the CH340X for BOOT0 and nRST according to the datasheet, but I can't test it at the moment. If you have implemented your board, please give me feedback. At the same time, I ordered a few CH340Xs, but I probably won't get around to experimenting with them until next year.

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 10, 2023

Hi! I really appreciate this. I'm working on the design first. I think I will finish it this weekend. I work in a company that buys PCBs from jlcpcb and normally I wait till the day that they buy many boards to not pay a cent for the shipping. So I don't know when I will get the boards in my hands. I really appreciate your work with all of this MCUs. If you want the design that I'm working on please just let me know. The board is double sided but the components are only on one side(to use the service of jlcpcb for putting all the components on the board for me). You can select the working voltage with a jumper (3.3v or 5v) it has an onboard 3.3v regulator, USB type c with 5.1k resistors so it can work with USB type c to USB type c cables to trigger the host mode.

wagiminator added a commit that referenced this issue Nov 10, 2023
@wagiminator
Copy link
Owner

I am very curious already!

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 11, 2023

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 11, 2023

In the zip file you can find the schematic, KiCad project (if you want to modify it), many .zip files containing all the gerber files for production in many PCB houses (jlcpcb, pcbway, fusionpcb, elecrow). It also have an interactive BOM...

I can do a panelization of the board for you if you want to make multiple boards. Yoy can fit many of this tiny boards on a 10cm * 10cm board (8 or 12 i think will fit in that space) So if you order 5 boards of 10* 10 you will get 40 minimum.

I hope you like this.

@wagiminator
Copy link
Owner

Cool! I made some changes to my DevBoard as well and gave an order to JLCPCB. Let's see when everything arrives and I find the time to try it out.

@wagiminator
Copy link
Owner

Hi, CH340X and board are here. I was able to do a few tests. ISP software works - so far so good. Apart from that, unfortunately the problems I feared arise. If you try to use the CH340X as a "normal" USB-to-serial converter with any serial monitor, the PY32 will be reset because, as usual, the RTS pin is automatically set to LOW when data is sent. Second problem is that despite the pulldown, BOOT0 is pulled to HIGH by default by the CH340X. A normal reset using a button is therefore not possible - you always end up in the bootloader. I currently don't know how to work around the problem...

@NoNamedCat
Copy link
Author

Hello Wagiminator. Maybe we can add some components to make the thing works. Can you share the schematic of your design so I can analyze it?

@NoNamedCat
Copy link
Author

maybe with some capacitors and transistors we can make it works

@NoNamedCat
Copy link
Author

Also... Do you have a logic analyzer or some scope that can help us to debug the DTR and RST lines?

@wagiminator
Copy link
Owner

Yes, I have a logic analyzer and I also used it here. You can certainly create a solution with additional components, but of course they make the whole thing more complex and then they also have to fit on the board.

@wagiminator
Copy link
Owner

ch340x

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 25, 2023

A resistor and capacitor on the BOOT0 will add a delay in the DTR of the CH340X. It may be the solution on the boot state of your board?

@NoNamedCat
Copy link
Author

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 25, 2023

so... If you add that, you need to make a delay in time on the code for programming the device. You need to put the DTR line in the level for flashing state. Then you add a delay so the capacitor will reach the logic level of the boot state. Then you reset the MCU. The solution is two small components. What do you think?

@wagiminator
Copy link
Owner

That won't help. The situation looks like this: If the board is connected via USB, then RTS and DTR are high. If I open the connection to the CH340X for example with CuteCom, RTS and DTR are immediately set to low and remain low until I close the connection.

@wagiminator
Copy link
Owner

I suspect that the CH340X is pulling the DTR line hard to VCC (open-source), if I overpress that there will probably be a short.

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 25, 2023

Can you try it with the serial debugger in Arduino IDE?
I think that the software that you are using is more focused on communicating with modems and other kind of hardware. Please give it a try and tell me how it works please.

@NoNamedCat
Copy link
Author

Also... Here is a link explaining the CH340X IC:
https://www.youtube.com/watch?v=CGLHfyW0rYE

@NoNamedCat
Copy link
Author

Have you tried to connect T-NOW to CTS with a 4.7K resistor instead of using the 4.7 pulldown resistor on the T-NOW?

@wagiminator
Copy link
Owner

It's the same with the Arduino IDE (at least on Linux, if that has any influence). Thanks for the video link. I think it works great if you make a programming device with the chip. However, the problems remain for a full on-board solution. Connecting the resistor between DTR and CTS makes no sense here. DTR is then only set push-pull instead of open-source. This certainly makes sense with other MCUs.

@NoNamedCat
Copy link
Author

I feel sad about that. Sorry to hear that the solution didn't work.

@wagiminator
Copy link
Owner

This would be the easy solution ... if DTR were LOW on default.

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 28, 2023

Hello again Wagiminator. Hope you are fine.
I want to tell you about the new repo of HalfSweet (the creator of the PY32DUINO).
She designed a board for the PY32F030 MCU and she have done a solution based on transistors like this:
Screenshot_2023-11-28_17-10-39

@NoNamedCat
Copy link
Author

@NoNamedCat
Copy link
Author

I think that you can replace the mosfets for NPN transistors like the 2n2222 or 2n3904

@NoNamedCat
Copy link
Author

it's the same circuit that i posted before_
image
but it uses the third transistor to negate the BOOT0 state

@NoNamedCat
Copy link
Author

The ch340x function and behavior is like having the two first transistors, do you think that if we put the third transistor only on the boot line to negate the state, we can manage to make the board work?

@wagiminator
Copy link
Owner

When I look at the behavior of the CH340X, I come to the conclusion that the only difference to the CH340E is that the DTR signal can be ouput on a pin if necessary. This means that the circuit with the transistors is necessary here if you want to use the chip also as a USB-to-serial converter and not just as a programming device.
In principle, you can also build the circuit with BJTs instead of MOSFETs. You then need resistors for the base. The body diode of the MOSFETs might also have to be taken into account.

@NoNamedCat
Copy link
Author

If I make the board design with all of this stuff, I will post it on this topic. Are you still interested? It will be my pleasure to give you that

@wagiminator
Copy link
Owner

Oh yes, I'm looking forward to it.

@NoNamedCat
Copy link
Author

I finish the board design. I grabbed the transistors for the boot pins from the repo above. Can you tell me what do you think about it? There is a space left on the board that I can use for some leds on the TX and RX pins (Like the Arduino boards).
All the passive elements (Caps and Resistors) are 0805, smaller ones are difficult for me.
If you want some personalized silkscreen I can do it for you (And this repo)

PY32F002

@NoNamedCat
Copy link
Author

bottom
top

@wagiminator
Copy link
Owner

Nice Logo! Is there a reason why you used CTS instead of DTR? I don't think that will work.

@wagiminator
Copy link
Owner

There might be som problems with the CH340X if you select 3V3 as MCU VCC. If you power the CH340X with 3V3 on the VCC pin, you should also power it with 3V3 on the 3V pin, I think. You can solve that with a permanent connection of the 3V pin to +3.3V of the LDO.

@wagiminator
Copy link
Owner

Please take a closer look at the MOSFET circuit and compare it with that of HalfSweet. How do you want to pull the NRST pin to LOW?

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 30, 2023

Tks for the hints. I will correct those and share the design with you.

@NoNamedCat
Copy link
Author

I will need to redo many things with the changes that you proposed. But is very late here right now. Tomorrow I will continue to design the board. If you like my logo... I can design one for you if you want.

@NoNamedCat
Copy link
Author

Screenshot_2023-11-30_02-04-31

@NoNamedCat
Copy link
Author

Screenshot_2023-11-30_02-04-31

@NoNamedCat
Copy link
Author

bottom
bottomCopper
top
topCopper

@wagiminator
Copy link
Owner

I still believe you have to swap PF2 and PA6 as well as DTR and RTS at the MOSFET circuit.

@wagiminator
Copy link
Owner

If you have some room left on the board, some 100nF capacitors for the power pins of the CH340 and PY32 will make the whole thing a little more stable, although it certainly works without it.

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 30, 2023

TX-RX
TX-RX2
Ok. I swapped the DTR and RTS lines and added 100nf caps on 5v and 3.3v lines. But I don't understand why we need to swap the RX and TX pins on the board.
With serial ports you need to connect the RX pin (on the serial ic) to TX pin (on the MCU) and the TX pin (on the serial IC) to the RX pin (on the MCU).
The TX pin on the CH320 transmit, and the PY32F002 PA3 receive
The RX pin on the CH320 receive, and the PY32F002 PA2 transmit.

I will test if the TX and RX are fine with my old design and will make the changes if the lines are truncated on the design.
Tks a lot again Wagiminator.

@NoNamedCat
Copy link
Author

The RX and TX lines works fine for me.

@NoNamedCat
Copy link
Author

NoNamedCat commented Nov 30, 2023

I have a problem with the current design. I want to make a panelization (Array of boards) of the design. The USB port and the buttons for reset and boot crash in the panel. The idea is to make a panel of 3*2 on the standard size of the JLCPCB. If the board works I think that we don't need the buttons in the board. Do you think that we need the buttons?

@wagiminator
Copy link
Owner

I never mentioned TX/RX. You should swap PF2 (NRST) PB6 (BOOT0) on the MOSFET circuit (see HalfSweet's circuit).

Well, if it works you don't necessarily need the buttons. If necessary, you can connect some via the pinouts.

I'll probably have to adjust the ISP software...

@NoNamedCat
Copy link
Author

I'm sorry for the spam. This design would not exist if it weren't for you. Tks a lot.

@HalfSweet
Copy link

HalfSweet commented Dec 4, 2023

Actually it's a very simple question...the condition for py32 to enter the bootloader is very simple, just have the boot0 pin high at reset. So we may easily find a similar schematic online.
图片
But there is a problem, when we switch the serial port on and off, the level of DTR and RTS may not be fixed, which may cause us to enter a different mode than expected by mistake.
And in the Arduino IDE, DTR and RTS are set to true when the serial port monitor is turned on - a very bad design, and to avoid this problem, back when I was designing the Air001 development board I included the An ESP32 autodownload-like design using two BJT/MOS to implement a hardware heterodyne circuit, which we can see in https://gitee.com/openLuat/luatos-broads/tree/master/broads/Air001-Board at the time.

Of course there is a slight problem with this circuit, which means we can't control the levels of boot0 and RST at the same time, so we compensated for this by connecting a capacitor in parallel to GND for both boot0 and RST, and performing pull-downs and pull-ups according to the needs of the running app.

Alternatively, you can view a typical PY32F030 development board design https://github.com/HalfSweet/PY32F030K28U_Arduino_Nano

At AirISP, I implemented the automatic download logic for the relevant circuits

@NoNamedCat
Copy link
Author

You are a FullSweet ;) tks a lot for sharing your knowledge with me. Can I share this conversation with Wagiminator so he can design his board too?

@HalfSweet
Copy link

You are a FullSweet ;) tks a lot for sharing your knowledge with me. Can I share this conversation with Wagiminator so he can design his board too?

Of course, our sessions are open and we can have discussions together if we can

@wagiminator
Copy link
Owner

That looks promising.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants