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

Add CH341A programmer support. #1215

Merged
merged 8 commits into from
Apr 6, 2023
Merged

Add CH341A programmer support. #1215

merged 8 commits into from
Apr 6, 2023

Conversation

ghost
Copy link

@ghost ghost commented Dec 12, 2022

Add CH341A programmer support.
Based on the issue: [patch #9127] Support for CH341A programmer.
Using the patch sent by @avrs-admin

@mcuee mcuee added the enhancement New feature or request label Dec 13, 2022
@mcuee
Copy link
Collaborator

mcuee commented Dec 13, 2022

@am-ar
Thanks for the help. Can you post some test results as well?

@MCUdude
Copy link
Collaborator

MCUdude commented Dec 14, 2022

Thanks for submitting the PR. I highly doubt it will make it into Avrdude v7.1, but it may be a nice addition if we're able to adress some of the issues pointed out below.

A few things:

  • Why just the CH341A? Won't the other CH341 variants in different packages with different pin configurations work?
  • The code formatting are not quite by todays standards. We've settled for two spaces for indents, not four.
  • Alot has happened to the Avrdude codebase since 2016. prog_modes are missing in avrdude.conf, and all messages has been replaced by msg_, imsg_ or pmsg_.
  • The CH341's SPI mode is runing with a 1.4 MHz clock, which means that an AVR would have to be running at 5.6 MHz or greater to be able to communicate with the CH341. This means that a blank AVR, running at 1 MHz cannot be programmed by a CH341 until the fuses are programmed to either run from the 8 MHz internal ocillator, or with a fast-enough external oscillator. An alternative here is SPI bit banging, but this PR doesn't adress this. Here is some more info: https://github.com/gschorcht/spi-ch341-usb/blob/master/README.md

Add `prog_mode` configuration in `avrdude.conf`;
Replace message functions;
@ghost
Copy link
Author

ghost commented Jan 5, 2023

Hello @mcuee, here is my test log:

test.log

C:\Users\Dados\Environment\eclipse-cpp\avrdude>build\src\avrdude.exe -vvvvv -p attiny25 -C build\src\avrdude.conf -c ch341a -e -D -F -U flash:w:firmware_teste.hex:i 
sys_config = build\src\avrdude.conf
sys_config_found = false


avrdude: Version 7.0-20221212 (0ae63b0)
         Copyright the AVRDUDE authors;
         see https://github.com/avrdudes/avrdude/blob/main/AUTHORS

         System wide configuration file is C:\Users\Dados\Environment\eclipse-cpp\avrdude\build\src\avrdude.conf

         Using Port                    : usb
         Using Programmer              : ch341a
avrdude: ch341a_open("usb")
         AVR Part                      : ATtiny25
         Chip Erase delay              : 4500 us
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         Serial program mode           : yes
         Parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom                 65     6     4    0 no        128    4      0  4000  4500 0xff 0xff
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      1
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14   ADDRESS      6      0
                                  13   ADDRESS      5      0
                                  12   ADDRESS      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               WRITE              31     VALUE      7      1
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      0
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14   ADDRESS      6      0
                                  13   ADDRESS      5      0
                                  12   ADDRESS      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7     INPUT      7      0
                                   6     INPUT      6      0
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
               LOADPAGE_LO        31     VALUE      7      1
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      0
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      1
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15     VALUE      7      0
                                  14     VALUE      6      0
                                  13     VALUE      5      0
                                  12     VALUE      4      0
                                  11     VALUE      3      0
                                  10     VALUE      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7     INPUT      7      0
                                   6     INPUT      6      0
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
               WRITEPAGE          31     VALUE      7      1
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      0
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      1
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21    IGNORE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14   ADDRESS      6      0
                                  13   ADDRESS      5      0
                                  12   ADDRESS      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9     VALUE      1      0
                                   8     VALUE      0      0
                                   7    IGNORE      7      0
                                   6    IGNORE      6      0
                                   5    IGNORE      5      0
                                   4    IGNORE      4      0
                                   3    IGNORE      3      0
                                   2    IGNORE      2      0
                                   1    IGNORE      1      0
                                   0    IGNORE      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash                  65     6    32    0 yes      2048   32     64  4500  4500 0xff 0xff
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ_LO            31     VALUE      7      0
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17   ADDRESS      9      0
                                  16   ADDRESS      8      0
                                  15   ADDRESS      7      0
                                  14   ADDRESS      6      0
                                  13   ADDRESS      5      0
                                  12   ADDRESS      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               READ_HI            31     VALUE      7      0
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17   ADDRESS      9      0
                                  16   ADDRESS      8      0
                                  15   ADDRESS      7      0
                                  14   ADDRESS      6      0
                                  13   ADDRESS      5      0
                                  12   ADDRESS      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               LOADPAGE_LO        31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      0
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7     INPUT      7      0
                                   6     INPUT      6      0
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
               LOADPAGE_HI        31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11   ADDRESS      3      0
                                  10   ADDRESS      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7     INPUT      7      0
                                   6     INPUT      6      0
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
               WRITEPAGE          31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      1
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17   ADDRESS      9      0
                                  16   ADDRESS      8      0
                                  15   ADDRESS      7      0
                                  14   ADDRESS      6      0
                                  13   ADDRESS      5      0
                                  12   ADDRESS      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7    IGNORE      7      0
                                   6    IGNORE      6      0
                                   5    IGNORE      5      0
                                   4    IGNORE      4      0
                                   3    IGNORE      3      0
                                   2    IGNORE      2      0
                                   1    IGNORE      1      0
                                   0    IGNORE      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse                   0     0     0    0 no          1    1      0  9000  9000 0x00 0x00
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      1
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               WRITE              31     VALUE      7      1
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      1
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      1
                                  22     VALUE      6      0
                                  21     VALUE      5      1
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7     INPUT      7      0
                                   6     INPUT      6      0
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse                   0     0     0    0 no          1    1      0  9000  9000 0x00 0x00
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      1
                                  27     VALUE      3      1
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      1
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               WRITE              31     VALUE      7      1
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      1
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      1
                                  22     VALUE      6      0
                                  21     VALUE      5      1
                                  20     VALUE      4      0
                                  19     VALUE      3      1
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7     INPUT      7      0
                                   6     INPUT      6      0
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse                   0     0     0    0 no          1    1      0  9000  9000 0x00 0x00
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      1
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      1
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               WRITE              31     VALUE      7      1
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      1
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      1
                                  22     VALUE      6      0
                                  21     VALUE      5      1
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      1
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7    IGNORE      7      0
                                   6    IGNORE      6      0
                                   5    IGNORE      5      0
                                   4    IGNORE      4      0
                                   3    IGNORE      3      0
                                   2    IGNORE      2      0
                                   1    IGNORE      1      0
                                   0     INPUT      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock                    0     0     0    0 no          1    1      0  9000  9000 0x00 0x00
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      0
                                  30     VALUE      6      1
                                  29     VALUE      5      0
                                  28     VALUE      4      1
                                  27     VALUE      3      1
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20     VALUE      4      0
                                  19     VALUE      3      0
                                  18     VALUE      2      0
                                  17     VALUE      1      0
                                  16     VALUE      0      0
                                  15     VALUE      7      0
                                  14     VALUE      6      0
                                  13     VALUE      5      0
                                  12     VALUE      4      0
                                  11     VALUE      3      0
                                  10     VALUE      2      0
                                   9     VALUE      1      0
                                   8     VALUE      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
               WRITE              31     VALUE      7      1
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      0
                                  27     VALUE      3      1
                                  26     VALUE      2      1
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      1
                                  22     VALUE      6      1
                                  21     VALUE      5      1
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9    IGNORE      1      0
                                   8    IGNORE      0      0
                                   7     VALUE      7      1
                                   6     VALUE      6      1
                                   5     INPUT      5      0
                                   4     INPUT      4      0
                                   3     INPUT      3      0
                                   2     INPUT      2      0
                                   1     INPUT      1      0
                                   0     INPUT      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature               0     0     0    0 no          3    1      0     0     0 0x00 0x00
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      0
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      1
                                  27     VALUE      3      0
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15    IGNORE      7      0
                                  14    IGNORE      6      0
                                  13    IGNORE      5      0
                                  12    IGNORE      4      0
                                  11    IGNORE      3      0
                                  10    IGNORE      2      0
                                   9   ADDRESS      1      0
                                   8   ADDRESS      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0
                                           Block Poll               Page                       Polled
           Memory Type Alias    Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- -------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration             0     0     0    0 no          1    1      0     0     0 0x00 0x00
             Memory Ops:
               Oeration     Inst Bit  Bit Type  Bitno  Value
               -----------  --------  --------  -----  -----
               READ               31     VALUE      7      0
                                  30     VALUE      6      0
                                  29     VALUE      5      1
                                  28     VALUE      4      1
                                  27     VALUE      3      1
                                  26     VALUE      2      0
                                  25     VALUE      1      0
                                  24     VALUE      0      0
                                  23     VALUE      7      0
                                  22     VALUE      6      0
                                  21     VALUE      5      0
                                  20    IGNORE      4      0
                                  19    IGNORE      3      0
                                  18    IGNORE      2      0
                                  17    IGNORE      1      0
                                  16    IGNORE      0      0
                                  15     VALUE      7      0
                                  14     VALUE      6      0
                                  13     VALUE      5      0
                                  12     VALUE      4      0
                                  11     VALUE      3      0
                                  10     VALUE      2      0
                                   9     VALUE      1      0
                                   8   ADDRESS      0      0
                                   7    OUTPUT      7      0
                                   6    OUTPUT      6      0
                                   5    OUTPUT      5      0
                                   4    OUTPUT      4      0
                                   3    OUTPUT      3      0
                                   2    OUTPUT      2      0
                                   1    OUTPUT      1      0
                                   0    OUTPUT      0      0

         Programmer Type : ch341a
         Description     : ch341a programmer

avrdude: ch341a_initialize()
avrdude: ch341a_ChipSelect()
avrdude: ch341a_ChipSelect()
avrdude: ch341a_program_enable() 
avrdude: program_enable(): sending command. Resp = ff ff 53 0 
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: device signature = 0x1e9108 (probably t25)
avrdude: erasing chip
avrdude: ch341a_initialize()
avrdude: ch341a_ChipSelect()
avrdude: ch341a_ChipSelect()
avrdude: ch341a_program_enable() 
avrdude: program_enable(): sending command. Resp = ff ff 53 0 
avrdude: reading input file firmware_teste.hex for flash
         with 1944 bytes in 1 section within [0, 0x797]
         using 61 pages and 8 pad bytes
avrdude: writing 1944 bytes flash ...

Writing | ################################################## | 100% 1.46s

avrdude: 1944 bytes of flash written
avrdude: verifying flash memory against firmware_teste.hex
avrdude: reading on-chip flash data ...

Reading | ################################################## | 100% 0.54s

avrdude: verifying ...
avrdude: 1944 bytes of flash verified
avrdude: ch341a_close()
avrdude: ch341a_ChipSelect()

avrdude done.  Thank you.

@ghost
Copy link
Author

ghost commented Jan 5, 2023

Hello @MCUdude,

  • Why just the CH341A? Won't the other CH341 variants in different packages with different pin configurations work?

Because I'm using a CH341A based programmer, all my tests were done with it and I didn't study the datasheet in depth to see if it's compatible with other variants of the chip. It is likely to be compatible with other variants.
My CH341A Mini Programmer

  • The code formatting are not quite by todays standards. We've settled for two spaces for indents, not four.
  • Alot has happened to the Avrdude codebase since 2016. prog_modes are missing in avrdude.conf, and all messages has been replaced by msg_, imsg_ or pmsg_.

I refactored the code. Check if you agree now, please.

  • The CH341's SPI mode is runing with a 1.4 MHz clock, which means that an AVR would have to be running at 5.6 MHz or greater to be able to communicate with the CH341. This means that a blank AVR, running at 1 MHz cannot be programmed by a CH341 until the fuses are programmed to either run from the 8 MHz internal ocillator, or with a fast-enough external oscillator. An alternative here is SPI bit banging, but this PR doesn't adress this. Here is some more info: https://github.com/gschorcht/spi-ch341-usb/blob/master/README.md

@KaeLL, when you have time. Can you help me with this?

@stefanrueger
Copy link
Collaborator

What is the state of this PR? @MCUdude Has the revision addressed your concerns?

| I didn't study the datasheet in depth to see if it's compatible with other variants of the chip. It is likely to be compatible with other variants

@am-ar We normally like to add code that generalises over the class of chips it deals with, just to ensure the code base does not become too fragmented. It would be great if the PR could address CH341 variants in different packages with different pin configurations.

@MCUdude
Copy link
Collaborator

MCUdude commented Mar 3, 2023

I've just ordered a CH341A programmer to test this PR. It will probably arrive within a few weeks, so I was hoping I could give it a test before merging this PR.

However, it appears that all CH341 variants share the same USB VID/PID, so maybe we should rename the programmer to ch341 instead of ch341a?

It would be neat if the CH341 could run at a lower clock speed in order to program AVRs that are running slow. However, this would have to be done by implementing a dedicated bit-banging option, perhaps ch341_bb. I'd argue that this probably is out of the scope of this PR.

@KaeLL
Copy link

KaeLL commented Mar 3, 2023

I'd argue that this probably is out of the scope of this PR.

Not necessarily. But I'm sure if and when I'll be able to work on this.

@stefanrueger
Copy link
Collaborator

@KaeLL @MCUdude Where are we with this PR?

@MCUdude
Copy link
Collaborator

MCUdude commented Mar 22, 2023

I'm still waiting for the CH341A hardware I ordered a few weeks ago. I'll like to give it a try myself before we merge this PR.
@mcuee do you have a CH341-based programmer you can test with?

@mcuee
Copy link
Collaborator

mcuee commented Mar 22, 2023

I'm still waiting for the CH341A hardware I ordered a few weeks ago. I'll like to give it a try myself before we merge this PR. @mcuee do you have a CH341-based programmer you can test with?

I do not have the programmer for now. I should have something in about a month.

@mcuee
Copy link
Collaborator

mcuee commented Mar 24, 2023

I'm still waiting for the CH341A hardware I ordered a few weeks ago. I'll like to give it a try myself before we merge this PR. @mcuee do you have a CH341-based programmer you can test with?

I do not have the programmer for now. I should have something in about a month.

@MCUdude

Hmm, I actually found a CH341A programmer like what @am-ar has. And I can now test this PR.

@mcuee
Copy link
Collaborator

mcuee commented Mar 24, 2023

Initial test seems to be a success. Verified with USBASP. Tested on a Uno clone with ATmega328PB, and using a hex file from MiniCore blink hex file.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v1 -C .\avrdude_pr1215v1.conf -p m328pb -c ch341a
 -U .\Blink.ino.with_bootloader_atmega328pb_16000000L.hex

avrdude_pr1215v1: AVR device initialized and ready to accept instructions
avrdude_pr1215v1: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1215v1: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1215v1: erasing chip
avrdude_pr1215v1: reading input file .\Blink.ino.with_bootloader_atmega328pb_16000000L.hex for flash
                  with 1754 bytes in 3 sections within [0, 0x7fff]
                  using 14 pages and 38 pad bytes
avrdude_pr1215v1: writing 1754 bytes flash ...

Writing | ################################################## | 100% 0.68 s

avrdude_pr1215v1: 1754 bytes of flash written
avrdude_pr1215v1: verifying flash memory against .\Blink.ino.with_bootloader_atmega328pb_16000000L.hex

Reading | ################################################## | 100% 0.51 s

avrdude_pr1215v1: 1754 bytes of flash verified

avrdude_pr1215v1 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_git -p m328pb -c usbasp
 -U flash:v:.\Blink.ino.with_bootloader_atmega328pb_16000000L.hex:i

avrdude_git: AVR device initialized and ready to accept instructions
avrdude_git: device signature = 0x1e9516 (probably m328pb)
avrdude_git: verifying flash memory against .\Blink.ino.with_bootloader_atmega328pb_16000000L.hex

Reading | ################################################## | 100% 0.21 s

avrdude_git: 1754 bytes of flash verified

avrdude_git done.  Thank you.

Blink.ino.with_bootloader_atmega328pb_16000000L.hex.txt

@mcuee
Copy link
Collaborator

mcuee commented Mar 24, 2023

A few notes.

  1. Under Windows, there is no driver found on the programmer by Windows and you will have a device called USB UART-LPT with no driver loaded. And official CH341SER driver (https://www.wch.cn/downloads/CH341SER_EXE.html) does not have this 0x1A86:0x5512 VID/PID combination.

Edit: this is not correct, there is another driver called CH341PAR which is the right driver for this programmer with VID/PID of 0x1A86:0x5512). It will appear under Windows Device Manager as Interface -> USB-EPP/I2C...CH341A.
Driver download: https://www.wch.cn/download/CH341PAR_EXE.html

  1. But anyway we are not using the vendor driver for this PR. We need to use libusb and need to use libusb supported driver under Windows: WinUSB is recommended but I am using libusbK.sys without a problem. You can use Zadig to install WinUSB driver whch will replace the vendor driver. But you can switch between WinUSB or Vendor driver using Windows Device Manager.
    https://github.com/pbatard/libwdi/releases/download/v1.5.0/zadig-2.8.exe

  2. Connection scheme:

CH341A -- AVR ISP Connector
CLK -- SCK
CS -- RST
MOSI -- MOSI
MISO -- MISO
GND -- GND
5V -- Vcc

Note: MOSI is called SDO in avrdude; MISO is called SDI.

@mcuee
Copy link
Collaborator

mcuee commented Mar 25, 2023

However, it appears that all CH341 variants share the same USB VID/PID, so maybe we should rename the programmer to ch341 instead of ch341a?

@MCUdude

I found some info about the programmer here, including the schematics.
https://www.onetransistor.eu/2017/08/ch341a-mini-programmer-schematic.html

ch341a_miniprogrammer

From the inf file inside CH341PAR driver, it seems to me there are two VID/PIDs, 1A86:5512 is for CH341A, 4348:5512 is for the older CH341.

[WinChipHead.NTamd64]
%CH341A.DeviceDesc% = CH341.Install.NTamd64, USB\VID_1A86&PID_5512
%CH341.DeviceDesc% = CH341.Install.NTamd64, USB\VID_4348&PID_5512
%CH347M1A.DeviceDesc% = CH347.Install.NTamd64, USB\VID_1A86&PID_55DB&MI_02
%CH347M1B.DeviceDesc% = CH347.Install.NTamd64, USB\VID_1A86&PID_55DD&MI_02

As for the Serial port configuration, different VID/PID combinations are used. 1A86:5523 is for CH341 and 4348:5523 is for the older CH341.

[WinChipHead.NTamd64]
%CH340SER.DeviceDesc% = CH341SER_Inst.NTamd64, USB\VID_1A86&PID_7523
%CH341ASER.DeviceDesc% = CH341SER_Inst.NTamd64, USB\VID_1A86&PID_5523
%CH340KSER.DeviceDesc% = CH341SER_Inst.NTamd64, USB\VID_1A86&PID_7522
%CH330SER.DeviceDesc% = CH341SER_Inst.NTamd64, USB\VID_1A86&PID_E523
%CH341SER.DeviceDesc% = CH341SER_Inst.NTamd64, USB\VID_4348&PID_5523
%CH340SER.DeviceDesc% = CH341SER_Inst.NTamd64, USB\VID_4348&PID_5523&REV_0250

@mcuee
Copy link
Collaborator

mcuee commented Mar 25, 2023

WCH product info page is a bit confusing though. They mention CH341B and CH341A which may work in all modes. CH341H may work in SPI mode but it is not recommended for new design, CH341C and CH341T can only used in UART mode so they can not be used for SPI.

So in then end only CH341A/B/F can be used. CH341B/F have built-in internal clock whreas CH341A does not have.
http://www.wch-ic.com/products/CH341.html

Datasheet: http://www.wch-ic.com/downloads/CH341DS1_PDF.html

I will still suggest that we call this programmer ch341a to match the VID/PID description in the CH341PAR driver package.

@mcuee
Copy link
Collaborator

mcuee commented Mar 25, 2023

Under Linux, WCH provides SER and PAR driver but we need to detach the driver in order to use libusb.
http://www.wch-ic.com/downloads/CH341SER_LINUX_ZIP.html
http://www.wch-ic.com/downloads/CH341PAR_LINUX_ZIP.html

Once we detach the driver, I can see this PR is good.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1205v1 -C ./avrdude_pr1205v1.conf -p m328pb
 -c ch341a -U ./Blink.ino.with_bootloader_atmega328pb_16000000L.hex 

avrdude_pr1205v1: AVR device initialized and ready to accept instructions
avrdude_pr1205v1: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1205v1: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1205v1: erasing chip
avrdude_pr1205v1: reading input file ./Blink.ino.with_bootloader_atmega328pb_16000000L.hex for flash
                  with 1754 bytes in 3 sections within [0, 0x7fff]
                  using 14 pages and 38 pad bytes
avrdude_pr1205v1: writing 1754 bytes flash ...

Writing | ################################################## | 100% 0.26 s 

avrdude_pr1205v1: 1754 bytes of flash written
avrdude_pr1205v1: verifying flash memory against ./Blink.ino.with_bootloader_atmega328pb_16000000L.hex

Reading | ################################################## | 100% 0.19 s 

avrdude_pr1205v1: 1754 bytes of flash verified

avrdude_pr1205v1 done.  Thank you.


mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1205v1 -C ./avrdude_pr1205v1.conf -p m328pb
 -c arduino -P /dev/ttyUSB1

avrdude_pr1205v1: AVR device initialized and ready to accept instructions
avrdude_pr1205v1: device signature = 0x1e9516 (probably m328pb)

avrdude_pr1205v1 done.  Thank you.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1205v1 -C ./avrdude_pr1205v1.conf -p m328pb
 -c urclock -P /dev/ttyUSB1 -xshowall -xbootsize=512

avrdude_pr1205v1 error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1205v1 error: initialization failed, rc=-1
                 - double check the connections and try again
                 - use -B to set lower ISP clock frequency, e.g. -B 200kHz
                 - use -F to override this check

avrdude_pr1205v1 error: programmer is not responding; try -xstrict and/or vary -xdelay=100

avrdude_pr1205v1 done.  Thank you.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_pr1205v1 -C ./avrdude_pr1205v1.conf -p m328pb
 -c urclock -P /dev/ttyUSB1 -xshowall -xbootsize=512 -xstrict

avrdude_pr1205v1: AVR device initialized and ready to accept instructions
0 0000-00-00 00.00  application 0 store 0 meta 0 boot 512 o8.0 -?s-?-r-- vector 0 (RESET) ATmega328PB

@stefanrueger
BTW, I do not need to use -xstrict for quite a while until now with the Optiboot hex files from @MCUdude. Just FYI.

@mcuee
Copy link
Collaborator

mcuee commented Mar 26, 2023

No issues under macOS as well. Take note I did not install the vendor driver under macOS. The worry is that it may interfere with libusb.

mcuee@mcuees-Mac-mini avrdude_bin % ./avrdude_pr1215 -C ./avrdude_pr1215.conf -p m328pb
 -c ch341a -U ./Blink.ino.with_bootloader_atmega328pb_16000000L.hex

avrdude_pr1215: AVR device initialized and ready to accept instructions
avrdude_pr1215: device signature = 0x1e9516 (probably m328pb)
avrdude_pr1215: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1215: erasing chip
avrdude_pr1215: reading input file ./Blink.ino.with_bootloader_atmega328pb_16000000L.hex for flash
                with 1754 bytes in 3 sections within [0, 0x7fff]
                using 14 pages and 38 pad bytes
avrdude_pr1215: writing 1754 bytes flash ...

Writing | ################################################## | 100% 0.43 s 

avrdude_pr1215: 1754 bytes of flash written
avrdude_pr1215: verifying flash memory against ./Blink.ino.with_bootloader_atmega328pb_16000000L.hex

Reading | ################################################## | 100% 0.32 s 

avrdude_pr1215: 1754 bytes of flash verified

avrdude_pr1215 done.  Thank you.

mcuee@mcuees-Mac-mini avrdude_bin % ./avrdude_git -p m328pb -c urclock -P /dev/cu.usbserial-22440
 -xshowall -xbootsize=512

avrdude_git: AVR device initialized and ready to accept instructions
0 0000-00-00 00.00  application 0 store 0 meta 0 boot 512 o8.0 -?s-?-r-- vector 0 (RESET) ATmega328PB
mcuee@mcuees-Mac-mini avrdude_bin % ./avrdude_git -p m328pb -c arduino -P /dev/cu.usbserial-22440                  

avrdude_git: AVR device initialized and ready to accept instructions
avrdude_git: device signature = 0x1e9516 (probably m328pb)

avrdude_git done.  Thank you.

mcuee@mcuees-Mac-mini avrdude_bin % 

@mcuee
Copy link
Collaborator

mcuee commented Mar 26, 2023

No issues under macOS as well. Take note I did not install the vendor driver under macOS. The worry is that it may interfere with libusb.

But what if I have installed WCH macOS driver? (Note: there is not CH341PAR driver for macOS)
http://www.wch-ic.com/downloads/CH341SER_MAC_ZIP.html

Unfortunately it no longer works.
Edit: this is incorrect. I did not connect the target properly.

@mcuee
Copy link
Collaborator

mcuee commented Mar 26, 2023

Once I removed it. And then reboot the machine, it will work.

Edit: this is incorrect. I did not connect the target properly.

Update:
Even with the kernel VCP driver installed, it still works fine. This is good.

mcuee@mcuees-Mac-mini avrdude_bin % systemextensionsctl list                                      
4 extension(s)
--- com.apple.system_extension.driver_extension
enabled	active	teamID	bundleID (version)	name	[state]
*	*	52444FG85C	com.silabs.cp210x (6.0.2/1)	com.silabs.cp210x	[activated enabled]
*	*	5JZGQTGU4W	cn.wch.CH34xVCPDriver (1.0/1)	cn.wch.CH34xVCPDriver	[activated enabled]
*	*	658CPPCMJJ	com.ftdi.vcp.dext (1.1/010)	NullDriver	[activated enabled]
*	*	QED4VVPZWA	com.logi.options.hidfilter (1.0.5914/5914)	Logitech Options HID Driver Extension	[activated enabled]

mcuee@mcuees-Mac-mini avrdude_bin % ./avrdude_pr1215 -C ./avrdude_pr1215.conf -p m328pb -c ch341a

avrdude_pr1215: AVR device initialized and ready to accept instructions
avrdude_pr1215: device signature = 0x1e9516 (probably m328pb)

avrdude_pr1215 done.  Thank you.

@mcuee mcuee added this to the AVRDUDE 7.2 milestone Mar 26, 2023
@MCUdude
Copy link
Collaborator

MCUdude commented Apr 1, 2023

I just received the CH341-based programmer, and the initial test looks good. I'm currently on vacation, so I've only brought a few chips. I've only tested this on an ATmega16

It looks like there is no support for caching in terminal mode. I'm also not able to write to flash, probably because of the missing cache support. Occasionally, it also failed to write data to EEPROM in terminal mode. See the output below.
It is also not possible to change the ISP clock speed. We know that it can't go lower than 1.2 MHz, but I think it should be possible to increase this. AVRs running at 20 MHz support ISP clocks up to 5 MHz.

$ ./avrdude -cch341a -patmega16 -t

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9403 (probably m16)
avrdude> write flash 0 0x1234
>>> write flash 0 0x1234 

Writing | -------------------------------------------------- | 0% 0.00 s 

avrdude error: (write) verification error writing 0x34 at 0x00000 cell=0xff
avrdude error: (write) verification error writing 0x12 at 0x00001 cell=0xff
avrdude> write eeprom 0 "Hello World!"
>>> write eeprom 0 "Hello World!" 

Writing | -------------------------------------------------- | 0% 0.06 s 

avrdude error: (write) error writing 0x48 at 0x00000, rc=-6
avrdude> write eeprom 0 "Hello World"
>>> write eeprom 0 "Hello World" 

Writing | ################################################## | 100% 0.02 s 

avrdude> quit
>>> quit 
avrdude> 
avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator

stefanrueger commented Apr 1, 2023

Thanks for testing @MCUdude.

It looks like there is no support for caching in terminal mode.

The programmer must provide paged_read() and paged_write() for the terminal to support caching. There is no reason why this programmer cannot implement paged_read() and paged_write().

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 2, 2023

I suggest we merge this PR as it is, and address the oter things paged r/w, clock speed and documentation. The fact that ~1.6MHz is the slowest clock it can provide should be documented. the CH341 chip selects pin (connected to the AVR reset pin) is also hard-coded to 0. This could be overridden using -x chipselect, -x resetpin or similar. However, this can easily be added in another PR.

@mcuee
Copy link
Collaborator

mcuee commented Apr 2, 2023

I suggest we merge this PR as it is, and address the oter things paged r/w, clock speed and documentation. The fact that ~1.6MHz is the slowest clock it can provide should be documented. the CH341 chip selects pin (connected to the AVR reset pin) is also hard-coded to 0. This could be overridden using -x chipselect, -x resetpin or similar. However, this can easily be added in another PR.

I think your proposal is good one and I will agree with you here.

@stefanrueger
Copy link
Collaborator

Atmel's ISP interface

This reminds me: the ch341a programmer has only PM_ISP set as programming mode. I was wondering whether this can be set to PM_ISP | PM_TPI and whether TPI parts can be programmed as well? There may need to be a small modification: only do a avr_write_page() for ISP parts, so a change in the paged write to the effect of

 if(isflash && (p->prog_modes & PM_ISP) && avr_write_page(pgm, p, m, addr-n_bytes) < 0)
    return -1;

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 4, 2023

I think CH341A should physically be capable of programming parts using TPI, but a resistor between SDI and SDO may be necessary. But it will require some additional code I'm sure.

#include "ch341a.h"
#include "usbdevs.h"

#if defined(HAVE_LIBUSB_1_0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does that line read #if defined(HAVE_LIBUSB_1_0) instead of #if defined(HAVE_LIBUSB) || defined(HAVE_LIBUSB_1_0)?

Does it have to be libusb1.0?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as of now the codes only support libusb-1.0.

@@ -62,6 +62,8 @@
# endif
#endif

#include <sys/time.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was it needed to move #include <sys/time.h> from line 34 to here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably no good reason to move this. That's my guess...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not related to this PR but rather related to MSVC build. So yes it is out of scope for this PR but it is nice to have.

This commit
 - Updates and moves the avrdude.conf entry for the ch341a programmer
 - Provides paged access functions so terminal cache works
 - Reformats ch341a.[ch] as these are new additions

Also
 - Removes unused function ch341a_spi_transfer()
 - Changes several fprintf(stderr, ...) to msg_error()
@stefanrueger
Copy link
Collaborator

I think CH341A should physically be capable of programming parts using TPI, but a resistor between SDI and SDO may be necessary. But it will require some additional code I'm sure.

You are right @MCUdude! The current codebase cannot deal with TPI and this is a lot more involved than I thought.

@stefanrueger
Copy link
Collaborator

Made the changes addressing the review:

  • Updated and moved the avrdude.conf entry for the ch341a programmer
  • Provided paged access functions so terminal cache works
  • Reformatted ch341a.[ch] as these are new additions

Also

  • Removed unused function ch341a_spi_transfer()
  • Changed several fprintf(stderr, ...) to msg_error()

Only two things to clarify re libusb/libusb-1.0 and why the include was moved

Other than that, I'd be happy to merge.

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 4, 2023

I'm not sure how I can build Avrdude without libusb 1.0 without completely uninstalling libusb on my computer.

Maybe @mcuee can give it a try.

For reference, I found a trace that tells us that the CH341A may not work with libusb 0.1:

From https://patchwork.coreboot.org/patch/4364/

>  .BR dediprog ", " ft2232_spi ", " usbblaster_spi " and " pickit2_spi
>  need access to the respective USB device via libusb-0.1.
>  .sp
> +.BR ch341a_spi
> +needs access to the respective USB device via libusb-1.0.

@stefanrueger
Copy link
Collaborator

CH341A may not work with libusb 0.1

That's fair enough and I'd be happy to go with HAVE_LIBUSB_1_0 alone. I'd then just remove the in this case superfluous second #ifdef HAVE_LIBUSB_1_0 in that segment as we are already in HAVE_LIBUSB_1_0 territory at this point.

I am thinking of undoing the change in usbasp.c unless someone can come up with a good reason. Like @MCUdude, I also don't see how this makes a difference to the PR.

@mcuee
Copy link
Collaborator

mcuee commented Apr 4, 2023

I am thinking of undoing the change in usbasp.c unless someone can come up with a good reason. Like @MCUdude, I also don't see how this makes a difference to the PR.

It is not related to this PR but rather related to MSVC build. So yes it is out of scope for this PR but it is nice to have.

@stefanrueger
Copy link
Collaborator

nice to have

OK, good. I'll leave it then. Just needed a reason for the change.

@mcuee
Copy link
Collaborator

mcuee commented Apr 5, 2023

I'm not sure how I can build Avrdude without libusb 1.0 without completely uninstalling libusb on my computer.

Maybe @mcuee can give it a try.

The code as of now only works with libusb-1.0. This should not be an issue for majority of the platform except Windows. For majority of the Linux distros, macOS (Homebrew or macPorts), OpenBSD, NetBSD, Windows MinGW, libusb-compat-0.1 is dependant on libusb-1.0 (just a wrapper) and provide libusb-0.1 API for the platform. FreeBSD provides wrapper for both libusb-1.0 and libusb-0.1 API by using a system library.

The only real issue is with avrdude MSVC build which uses avrdude specific avrdude-libusb library, which only provides libusb-0.1 API.

PS C:\work\avr\avrdude_test\avrdude_bin\avrdude-msvc-x64> .\avrdude -c ch341a -p m2560
avrdude error: no usb support, please compile again with libusb installed
avrdude error: unable to open programmer ch341a on port usb

avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Apr 5, 2023

That's fair enough and I'd be happy to go with HAVE_LIBUSB_1_0 alone. I'd then just remove the in this case superfluous second #ifdef HAVE_LIBUSB_1_0 in that segment as we are already in HAVE_LIBUSB_1_0 territory at this point.

Yes I agree.

@mcuee
Copy link
Collaborator

mcuee commented Apr 5, 2023

@mariusgreuel

Any objection for this PR as it is not supporting MSVC build now (it only supports libusb-1.0 API and not avrdude-libusb)?

Is it okay to merge this first and then to address the issue for MSVC build later?

  - Return error in ch431a_open() when unable to claim usb interface
  - Fix return value of CH341ChipSelect()
  - Return error in ch431a_initialize() when CH341ChipSelect() fails
  - Minor fixes in error messages
  - Remove redundant #ifdef HAVE_LIBUSB_1_0
  - Silence compiler for integer/unsigned warnings
@stefanrueger
Copy link
Collaborator

stefanrueger commented Apr 5, 2023

is it okay to merge this first and then to address the issue for MSVC build later?

I for one am happy to merge unless there is an imminent fix to the MSVC libusb issue.

My final sweep through the ch341a programmer uncovered a few omissions error handling:

  • Return error in ch431a_open() when unable to claim usb interface
  • Fix return value of CH341ChipSelect()
  • Return error in ch431a_initialize() when CH341ChipSelect() fails
  • Minor fixes in error messages
  • Remove redundant #ifdef HAVE_LIBUSB_1_0
  • Silence compiler for integer/unsigned warnings

That warrants a final sanity test of the programmer. Other than that it would be good to go.

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 5, 2023

@stefanrueger while you're at it, could you mention in the docs that The CH341A ISP bit clock is approx. 1.7 MHz (0.714us), and cannot be increased or decreased. This means that the CH341A can't program "stock" AVRs ?

(Some sources say 1.4 MHz, while other states 1.6 or 1.7 MHz. I haven't measured mine yet)

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 5, 2023

The CH341 chip selects pin (connected to the AVR reset pin) is hard-coded to 0.

Should we address this in this PR, or should we deal with this later?
I suggested earlier that we should add -xchipselect=<0..2> or -xresetpin=<0..2>

@mcuee
Copy link
Collaborator

mcuee commented Apr 5, 2023

@stefanrueger

Looks good.

  1. Error messgae with MSVC build
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v5_msvc -C .\avrdude_pr1215v5_msvc.conf -c ch341a -p m2560
avrdude_pr1215v5_msvc error: no usb support, please compile again with libusb installed
avrdude_pr1215v5_msvc error: unable to open programmer ch341a on port usb

avrdude_pr1215v5_msvc done.  Thank you.
  1. Error when using vendor driver with MinGW build
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v5 -C .\avrdude_pr1215v5.conf -c ch341a -p m2560
avrdude_pr1215v5 warning: cannot open USB device: Function not implemented
avrdude_pr1215v5 error: could not find USB device with vid=0x1a86 pid=0x5512
avrdude_pr1215v5 error: unable to open programmer ch341a on port usb

avrdude_pr1215v5 done.  Thank you.
  1. With correct libusb-1.0 supported driver (WinUSB) with MinGW build
PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v5 -C .\avrdude_pr1215v5.conf -c ch341a
 -p m2560 -U .\hex\blink-mega2560_lext-test.hex

avrdude_pr1215v5: AVR device initialized and ready to accept instructions
avrdude_pr1215v5: device signature = 0x1e9801 (probably m2560)
avrdude_pr1215v5: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1215v5: erasing chip
avrdude_pr1215v5: reading input file .\hex\blink-mega2560_lext-test.hex for flash
                  with 1346 bytes in 4 sections within [0, 0x3106d]
                  using 7 pages and 446 pad bytes
avrdude_pr1215v5: writing 1346 bytes flash ...

Writing | ################################################## | 100% 0.61 s

avrdude_pr1215v5: 1346 bytes of flash written
avrdude_pr1215v5: verifying flash memory against .\hex\blink-mega2560_lext-test.hex

Reading | ################################################## | 100% 0.38 s

avrdude_pr1215v5: 1346 bytes of flash verified

avrdude_pr1215v5 done.  Thank you.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v5 -C .\avrdude_pr1215v5.conf -c ch341a
 -p m2560 -t

avrdude_pr1215v5: AVR device initialized and ready to accept instructions
avrdude_pr1215v5: device signature = 0x1e9801 (probably m2560)
avrdude> dump flash 0x4000 0x10
>>> dump flash 0x4000 0x10

Reading | ################################################## | 100% 0.14 s

4000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> write flash 0x4000 0x10 0x55 ...
>>> write flash 0x4000 0x10 0x55 ...

Caching | ################################################## | 100% 0.04 s

avrdude> flush
>>> flush
avrdude_pr1215v5: synching cache to device ...
Writing | ################################################## | 100% 0.15 s
avrdude> dump flash 0x4000 0x10
>>> dump flash 0x4000 0x10

Reading | ################################################## | 100% 0.04 s

4000  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|

avrdude> write flash 0x4000 0x10 0xaa ...
>>> write flash 0x4000 0x10 0xaa ...

Caching | ################################################## | 100% 0.04 s

avrdude> flush
>>> flush
avrdude_pr1215v5: synching cache to device ...
Reading | ################################################## | 100% 96.88 s
Erasing | ################################################## | 100% 0.06 s
Writing | ################################################## | 100% 1.25 s
avrdude> dump flash 0x4000 0x10
>>> dump flash 0x4000 0x10

Reading | ################################################## | 100% 0.04 s

4000  aa aa aa aa aa aa aa aa  aa aa aa aa aa aa aa aa  |................|

avrdude> quit
>>> quit
avrdude>
avrdude_pr1215v5 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Apr 5, 2023

@stefanrueger while you're at it, could you mention in the docs that The CH341A ISP bit clock is approx. 1.7 MHz (0.714us), and cannot be increased or decreased. This means that the CH341A can't program "stock" AVRs ?

(Some sources say 1.4 MHz, while other states 1.6 or 1.7 MHz. I haven't measured mine yet)

Indeed.

Just a quick test to enable CLKDIV8 for my ATmega2560 with external 16Mhz crystal (running at 2MHz) and it will fail to connect.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v5 -C .\avrdude_pr1215v5.conf -c ch341a -p m2560

avrdude_pr1215v5 error: initialization failed, rc=-2
                 - double check the connections and try again
                 - use -B to set lower ISP clock frequency, e.g. -B 200kHz
                 - use -F to override this check


avrdude_pr1215v5 done.  Thank you.

Restore the clock to 16MHz and then there will be no issue.

PS C:\work\avr\avrdude_test\avrdude_bin> .\avrdude_pr1215v5 -C .\avrdude_pr1215v5.conf -c ch341a -p m2560

avrdude_pr1215v5: AVR device initialized and ready to accept instructions
avrdude_pr1215v5: device signature = 0x1e9801 (probably m2560)

avrdude_pr1215v5 done.  Thank you.

@MCUdude
Copy link
Collaborator

MCUdude commented Apr 5, 2023

Excellent work @stefanrueger! It's a good idea to set the reset pin in avrdude.conf.

@stefanrueger
Copy link
Collaborator

Closes #714

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

Successfully merging this pull request may close these issues.

5 participants