AVR109 (butterfly) My Bootloader failed to read back at 2Mbps #1671
Replies: 20 comments
-
What if you reduce the speed? The gain of flash writing/reading speed gets less and less from 115200bps to 250kbps to 500kbps and becomes almost non-existend at 1Mbps. |
Beta Was this translation helpful? Give feedback.
-
Here is a screenshot at 2Mbps, and of course read-back failed. I want to write a better bootloader/programmer, and if I can't figure out how to get 2Mbps to readback correctly, I can't get them done. 0.6 second write speed different is significant to me. 1, I need to know the exactly delay between these 2 lines. |
Beta Was this translation helpful? Give feedback.
-
... But you aren't using the latest version of Avrdude! Why would we care about the 8-year-old 6.3 version? Try Avrdude 7.2. If that doesn't help, we might look at why this is... |
Beta Was this translation helpful? Give feedback.
-
Also, can you provide the source code for the bootloader, so it's possible to reproduce the limitation you've pointed out? |
Beta Was this translation helpful? Give feedback.
-
It may be related to your PC or your custom bootloader. 2M baud works fine on my Mac using the Xboot bootloader connected to an ATmega328p using a genuine FT232RL chip:
|
Beta Was this translation helpful? Give feedback.
-
16384/4=4096. that is slow to the extreme. Here is a screen of my atmega328 upload. 2M baud works on my Atmega8 too. I know exactly why, because reading back an Atmega8 is faster than an Atmega128. It's not an issue with my CH340G chip. The bootloader is not the issue as well. If I change it to write back random data, then it read back fine, just doesn't pass verification, so instead of giving me suggestions. Why not just answer my 2 simple questions. It's probably a bug in AVRdude. The timeout delay is probably related to the baud rate, so higher 2M baud rate probably has a shorter timeout period. I am too dumb to actually understand how serial works, so I am here to ask for help. |
Beta Was this translation helpful? Give feedback.
-
You absolutely have to care because if this bug is in 6.3 version, then it's probably still in Avrdude 7.2. Can you provide me with that timeout period that I am asking for? Also, I have checked the source code for AVR109 on 6.3 and 7.2. Guess what? They are the same, so another reason why you should can care. |
Beta Was this translation helpful? Give feedback.
-
Please read the following FAQ when you are reporting an issue. Please try out avrdude 7.2 and report the output in any case, even if you think there is no change. |
Beta Was this translation helpful? Give feedback.
-
Another thing, the avrdude avr109 code may be for generic AVR109 bootloader. If your bootloader needs something different, you may have to raise a PR to adapt to your programmer. BTW, where is the source code of your AVR109 implementation? |
Beta Was this translation helpful? Give feedback.
-
Here is nothing generic about an AVR109 bootloader, but if it really requires to be labeled as generic, then it is slow, outdated and dumb. If AVR109 bootloader is half good, then I wouldn't need to write my own. AVR109 bootloader is absolutely trash in many senses, but AVR109 protocol can still be salvageable to a very useful level, and that is what FDxboot is all about. I am struck at 1.51x because of this not able to read back at 2Mbps problem, and this can be a bug in avrdude or just bad coding on my part, so this is really a on-going project in progress, and I can't move forward unless I overcome this obstacle, as I am not the type of person whom is content at doing thing slightly above mediocre. It works perfectly fine at 1Mbps, but if it works stable at 2Mbps, then it would be absolutely perfection at 1Mbps. If you want to test my bootloader, and help me confirm that it's working properly and have your mind blown doing so, here are few compiled v1.50 firmware. 1.50 is outdated in comparison to the optimizations done on 1.51, but it's still fast. |
Beta Was this translation helpful? Give feedback.
-
Because I didn't write the avr109 Avrdude implementation and don't know the avr109 code that well.
@stefanrueger please correct me if I'm wrong, but AFAIK Avrdude doesn't add a delay between these lines. Lines 77 to 79 in 2f89aec Line 848 in 2f89aec Lines 384 to 416 in 2f89aec Lines 82 to 91 in 2f89aec Line 849 in 2f89aec Lines 495 to 537 in 2f89aec |
Beta Was this translation helpful? Give feedback.
-
It is unclear whether what you observe is a bug in avrdude: 2 Mbps UART comms requires a hardware that runs at pretty much exactly 16 MHz, it requires a host PC with a driver that can do pretty much exactly 2 Mbps and, if the USART getchar() routine in the bootloader polls (as I guess it does) it will require a pretty tight bootloader code and assiduous comms planning. Assuming a data frame of 8N1 you have 5 µs to pick up a byte sent by the host before the next byte from the host overwrites it. If the bootloader code fails to manage that, the USART will exhibit a frame error, the bootloader will miss the next host byte and, hey presto, the protocol is out of sync and eventually times out. Does FDxboot 1.51 check for comms error? It could set a board LED if it encounters one.
The maximal delay between the following lines Lines 660 to 661 in 2f89aec is determined by the external variable serial_recv_timeout (and probably between 100 ms and 5 s). This won't be your problem if the bootloader works at 1 Mbps. I guess the bootloader isn't quick enough to pick up the 4 command bytes sent in the first line (as discussed above).
As much as we like to help with AVRDUDE issues, we won't be able to debug fresh bootloader code for you. Let us know if you have evidence of avrdude problems but please give us useful info. |
Beta Was this translation helpful? Give feedback.
-
@mcuee I have checked out V7.2, and it still doesn't work with 2Mbps, but it's impressively faster than older version. Now my FDxboot 1.51x can upload at a speed of 126226/3.28=38483 bytes/second At 1Mbps and read back correctly, but if I can get it read back at 2Mbps, the upload speed is 59261 bytes/second, and this is a little unreal, but that is what V7.2 reported. @MCUdude I have confirmed that the cause the problem is not from those 2 lines of code, but there could still be a bug from AVRdude, and here is my logic: The 1Mbps bootloader works on one USB port, but if plugged into another USB port, the same type of not reading back error occurs. Normally it could just be the problem for that USB port, but when writing is good, but reading is not good, this could not just be the problem of the USB port, and I have tested that USB port, it works fine on even 2Mpbs. Of course, you can then assume my bootloader is not working probably, but remember I have tested it on other USB port at 1Mbps, and it works perfectly. @stefanrueger At 2Mbps, the time to read back a command after a page has send out is: 1/2000000*(8+2+8+2+8+1)=0.0000145s or 14.5us. You forget to account for the 2-byte buffer and 1 byte shift-register. This is irreverent, but the numbers of CPU clock cycles are more important in practice. 16/2*(8+2+8+2+8+1)=232 clock cycles to read back next command, and that is plenty of headroom. The cause of problem is probably from how AVRdude read back the page data. There appears to be an out of sync bug. 2Mbps still doesn't work with AVRdude 7.2, but it's upload speed has increased to 2.13 second or 126226/2.13=59261 Bytes/second. The Write is done correctly, but readback is the issue. |
Beta Was this translation helpful? Give feedback.
-
Good point about the HW FIFO buffer in the AVR part. Effectively three bytes buffer still fall short of the 4-byte command that the host sends in line 660 of avrdude/src/butterfly.c. You could try a small delay before that line to test whether the bootloader is too slow to catch the comms. And you could also test reading only Other than that, given that
where do you think we would recommend to keep looking for a solution? |
Beta Was this translation helpful? Give feedback.
-
Just read back doesn't work either. Here is the log for it. What the log doesn't show is how slow it's. The cause of the problem is probably how AVRdude's serial driver is not properly tuned for different USB port at high baud rate. I will try to get a new computer to see if it works or not, but either way, this should work on any USB 2.0 port.
|
Beta Was this translation helpful? Give feedback.
-
The logs look good and initially the setup reads and compares OK. Except that at some point the host doesn't get a response from the board. So, in principle, comms seems to work, the protocol seems to work etc. Is it always at the same point at which read fails? Or do different runs break at different percentages of reading back? How much current does your board draw? How good is your connecting cable? Perhaps the setup is at the edge of what the USB port can cope with? Have you measured the voltage of your board/processor? Is it well above what the brownout fuse setting sees? It is not a good sign that sometimes you have to switch to a different USB port: in theory the port shouldn't matter as it's a bus, but it could matter that some ports can drive more current than others or have slightly higher voltage etc. Ripple voltages and other hardware effects show more at higher speed.
It shouldn't: once the recv() function has gotten all the bytes it requests it returns. If not it waits until it times out. Look at the |
Beta Was this translation helpful? Give feedback.
-
It's the lack of clarity that is stopping me from moving forward. My hardware should be fine, so as my bootloader. I am expert in Atmega128. Here is the setup using my Mega pro board. The log looks good, but it's not good. It's too slow for it to be consider good even if it's really good, and I am not sure if it's really good, so here I try to rewrite the block send back code and try to spoof the AVRdude a bit, and I instantly have some clarity....
Here is the returned log in image. You can see that AVRdude failed to receive one byte from previous block, and that could be the reason or the bug: AVRdude is probably not optimized to receive at fast rate. It has no problem sending at 2Mbps because sending data is easier than receiving. Of course this is only my guess. At 2Mbps, AVRdude is responding so slow that this log is probably not valid, and even if AVRdude failed to receive one byte, my bootloader should still be able to respond with an new address request. Here is the actual log for 2M: To make sure my code is fine, I have a 1M log too, this 1M log has been done very fast, and error free: |
Beta Was this translation helpful? Give feedback.
-
If the answers to 1 and 2 are yes and to 3 is no then you may have a case. Note that sometimes the |
Beta Was this translation helpful? Give feedback.
-
Asking the correct question could be the key to solve this bug. 1, why is avrdude responding that slow at 2Mbps? what could possibly caused it to slow down? 2, more importantly how even after it has been freezing that long and still managed to receive that many pages without errors? Here I added another option to send back 1000 bytes, and 1000 bytes are received fine. PS. this is tested on my slower USB 2.0 port hub. The hardware seems perfectly fine. If I take a look at RX and TX pin, it's probably the same as shown in the log file. I did an observation of -v -v -v -v at real time, and AVRdude is randomly paused at g 00 00 00, so the block command has been sent, but it's not receiving back the block data. Can you provide me a AVRDUDE ser_recv() for me to test it out? I don't think this is the cause of the bug. The same bootloader and hardware at 1Mbps work fine on my USB 3.0 hub, but not on this USB 2.0 hub. By not working, it's just read back that is not reliable. The upload is fine.
|
Beta Was this translation helpful? Give feedback.
-
Move this to discussion as it is not confirmed to be an avrdude issue. |
Beta Was this translation helpful? Give feedback.
-
I have achieved a maximum upload speed of 31K bytes per second on my FDxboot_1.51x on an Atmega128a at 1Mbps, but when trying to operate at 2Mbps, it failed to read back correctly. Writing is good.
I read the source code of the butterfly, and I suspect these 2 lines are the cause of the problem. What is the exact delay between these 2 lines of code? Since it's very slow to read back a page of data, even one byte of flash memory, at 2Mbps, it simply timeouts and failed to response quickly.
Here when 4 command bytes have sent, it then expects to receive n amount of byte, but what exactly is the delay before it times out. It's very time consuming to read back 1 page of flash memory, and even sending back one byte to one byte basis doesn't seem to work.
Beta Was this translation helpful? Give feedback.
All reactions