wagiminator / MCU-Flash-Tools

Simple ISP Flash Tools for various Microcontrollers
MIT License
45 stars 6 forks source link

RTS DTR auto boot enter? #2

Open NoNamedCat opened 8 months ago

NoNamedCat commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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

wagiminator commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

I am very curious already!

NoNamedCat commented 8 months ago

PY32F002AF15P.zip PY32F002AF15P.pdf Screenshot_2023-11-11_03-43-39 Screenshot_2023-11-11_03-44-15

NoNamedCat commented 8 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

maybe with some capacitors and transistors we can make it works

NoNamedCat commented 7 months ago

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

wagiminator commented 7 months ago

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 commented 7 months ago

ch340x

NoNamedCat commented 7 months ago

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 commented 7 months ago

https://www.build-electronic-circuits.com/rc-delay-element/

NoNamedCat commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

NoNamedCat commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

wagiminator commented 7 months ago

Yes, that's a real shame. It would have been a really elegant solution. Well, maybe I can think of something else.

wagiminator commented 7 months ago

If DTR were LOW in its default state, I would immediately have a simple solution. It's really not clear to me why this is solved this way with the chip.

NoNamedCat commented 7 months ago

Maybe if you put a capacitor like a DC filter on the rts line? I don't want to lose hope :c

wagiminator commented 7 months ago

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

NoNamedCat commented 7 months ago

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 commented 7 months ago

Here is the repo: https://github.com/HalfSweet/PY32F030K28U_Arduino_Nano

NoNamedCat commented 7 months ago

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

NoNamedCat commented 7 months ago

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

NoNamedCat commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

Oh yes, I'm looking forward to it.

NoNamedCat commented 7 months ago

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 commented 7 months ago

bottom top

wagiminator commented 7 months ago

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

wagiminator commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

NoNamedCat commented 7 months ago

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 commented 7 months ago

Screenshot_2023-11-30_02-04-31

NoNamedCat commented 7 months ago

Screenshot_2023-11-30_02-04-31

NoNamedCat commented 7 months ago

bottom bottomCopper top topCopper

wagiminator commented 7 months ago

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