Page 2 of 2

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Wed Feb 24, 2016 5:47 pm
by sheepdoll
Interesting thread.

I was at the local makerspace last night and picked up a cheap Arduino clone that they were selling. I still have Yousemite on my laptop. Spent most of the time installing the driver. The first board failed to upload blink, the second worked. I basically wanted a burner Arduino. I was also curious about this bridge chip. I may pull parts off of the clone if the CH-340 is working. (It is likely the clone is missing a bootloader.)

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Wed Feb 24, 2016 6:27 pm
by mrburnette
sheepdoll wrote:Interesting thread.
<...>
I may pull parts off of the clone if the CH-340 is working. (It is likely the clone is missing a bootloader.)
That would be my guess too as I have received clones with no boothoader or perhaps a corrupted bootloader... guess I I could have read the fuses to determine if the configuration was even attempted.

Ray

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Thu Feb 25, 2016 12:49 am
by jbforrer
As pointed out by @mrburnette, drivers can be downloaded from the manufacturer's site:
http://www.wch.cn/download/CH341SER_EXE.html

Details are in Chinese. The Linux package contains driver source code (.cpp) and a Makefile.
Being Linux, one need to build a loadable module that will work with your specific kernel.

I understand some Linux distros might already have the correct driver. Plug the device into a USB port and check the list of kernal modules, i.e.,
>$ lsmod | grep usbserial.
If you find "ch341" is being associated with "usbserial", that will be an issue with the CH-340 USB-to-Serial part and STM32duino serial download.

Unfortunately, still seems tricky to get the thing working properly. I'm no Linux expert so your mileage may vary. This what I found:
On my system, "make" fails due to incompatible "printk's" sprinkled through the driver source. Guess these serve for debugging purposes.
Commenting all affected "printk" lines succeeded in building the driver, finishing with "ch34x.ko".

Although the Makefile allows one to load/unload the new driver, one first need to unload the old driver, "ch341" otherwise it will clash with the new one, "ch34x". There are various ways to clean this up, but be very careful how you proceed.

My kernel driver modules are in /lib/modules/3.13.0-24-generic/kernel/drivers/usb/serial ... (you will likely have a different kernel).
"ch341.ko" was renamed "ch341.ko.OLD", then the new driver copied in and named as "ch341.ko".

Plugging the device into a USB port automatically loads the correct kernel drivers. Verify actions with "lsmod", as shown below.
----------------------------------
$ lsmod | grep ch34x
ch34x 17618 0
usbserial 45014 1 ch34x
-----------------------------------

Example results of a serial download:
Sketch uses 23,872 bytes (4%) of program storage space. Maximum is 524,288 bytes.
Global variables use 2,760 bytes of dynamic memory.
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Using Parser : Raw BINARY
Interface serial_posix: 230400 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0414 (High-density)
- RAM : 64KiB (512b reserved by bootloader)
- Flash : 512KiB (sector size: 2x2048)
- Option RAM : 16b
- System RAM : 2KiB
Write to memory
Erasing memory
Wrote address 0x08005d40 (100.00%) Done.

Starting execution at address 0x08000000... done.
------------------------------------------------------------------


Hope that helps.
Best Regards.
JBF.

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Thu Feb 25, 2016 1:20 am
by zmemw16
aren't printk calls controlled by a writing a number to somewhere under /proc?

since i played with kernel code, when did the pci interface get a massive change 2.2/2.4/2.6?? eons ago - i had to
update/rewrite my pci device drivers. O'Reilly reissued their biblical tome on the subject, pretty sure they're both on my bookshelf. updated since then as well. 3rd 2000 probably not, i'd moved on to a different project by then.

so i googled 'linux kernel controlling printk output'

first 2
http://elinux.org/Kernel_Debugging_Tips
http://elinux.org/Debugging_by_printing
and the search drop down has in it
/proc/sys/kernel/printk

yielding
http://man7.org/linux/man-pages/man2/syslog.2.html

stephen

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Fri Jun 02, 2017 8:27 pm
by keypunch
I believe I have the same problem after I have tried many different combinations (TX/RX pins, PA9/10 pins, reset button, invert TX/RX that should not need to be inverted, 3.3 via USB TTL Adaptor, 3.3V via HID USB without HID USB, connected, et al).

A search did not turn up this topic my search about the problem I was having over last day of different attempt combinations I tried. I looked about where to post the issue. I thought USB to Serial adaptors would be where I place my question. I looked about and found this topic that I am sure matches what and why I have the problem I do.

The constant problem result:

Code: Select all

Sketch uses 8828 bytes (13%) of program storage space. Maximum is 65536 bytes.
Global variables use 1984 bytes of dynamic memory.
Failed to init device.
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Using Parser : Raw BINARY
Interface serial_posix: 230400 8E1

An error occurred while uploading the sketch
Linux Kernel

Code: Select all

uname -a
Linux lubuntu 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

Code: Select all

lsmod | grep usbserial. 
usbserial              53248  1 ch341

Code: Select all

dmesg | grep usbserial
[46695.816375] usbcore: registered new interface driver usbserial
[46695.816406] usbcore: registered new interface driver usbserial_generic
[46695.816431] usbserial: USB Serial support registered for generic
[46695.831028] usbserial: USB Serial support registered for ch341-uart
I have attached pics of the TTL USB to Serial Adaptor:
TTLUSBtoSerialCH340-Bottom.jpg
TTLUSBtoSerialCH340-Bottom.jpg (74.06 KiB) Viewed 675 times
USBtoSerialTTL-CH340-Top.jpg
USBtoSerialTTL-CH340-Top.jpg (127.07 KiB) Viewed 675 times
I am capable of configuring/building kernels, kernel modules, et al that appears to be one solution to the CH340 issue. I just like things to work. I do not want to have to keep customizing or fixing the many live images I often use and the time it consumes each time I boot a system (often by design) to customize on fly. I often use various live images for various reasons and needs. Rebooting to an image I customize for the CH340 issue is always going to be when it is not compatiable to the task at hand of image I am using at that time that does not have the solution for the CH340 issue. New live images will not likely not have the solution for the CH340 issue. I wish to avoid more time to customize a new live image, or on fly time to customize that is lost on each reboot, both options have to a live image.

I will find a FTDI FT232RL USB to Serial TTL adaptor and order one. When I receive the a FTDI FT232RL USB to Serial TTL adaptor I will post if this solved the issue.

I can download sketches just fine using a STLink.


Regards,

John L. Males
Toronto, Ontario
Canada
02 June 2017 16:27 EDT

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Fri Jun 02, 2017 9:59 pm
by keypunch
A quick update.

I looked for the ch341 driver:

Code: Select all

ls -ls
total 44
36 -rw-r--r-- 1 root root 35398 Sep 12  2015 ch34x.c
 4 -rw-r--r-- 1 root root   331 Aug 12  2015 Makefile
 4 -rw-r--r-- 1 root root   531 Sep 12  2015 readme.txt
Compiled and loaded driver module:

Code: Select all

sudo make
make -C /lib/modules/4.4.0-31-generic/build  M=/home/lubuntu/Downloads/CH341SER_LINUX  
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-31-generic'
  LD      /home/lubuntu/Downloads/CH341SER_LINUX/built-in.o
  CC [M]  /home/lubuntu/Downloads/CH341SER_LINUX/ch34x.o
/home/lubuntu/Downloads/CH341SER_LINUX/ch34x.c: In function ‘ch34x_close’:
/home/lubuntu/Downloads/CH341SER_LINUX/ch34x.c:566:15: warning: unused variable ‘wait’ [-Wunused-variable]
  wait_queue_t wait;
               ^
/home/lubuntu/Downloads/CH341SER_LINUX/ch34x.c:565:7: warning: unused variable ‘timeout’ [-Wunused-variable]
  long timeout;
       ^
/home/lubuntu/Downloads/CH341SER_LINUX/ch34x.c:564:6: warning: unused variable ‘bps’ [-Wunused-variable]
  int bps;
      ^
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/lubuntu/Downloads/CH341SER_LINUX/ch34x.mod.o
  LD [M]  /home/lubuntu/Downloads/CH341SER_LINUX/ch34x.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-31-generic'

Code: Select all

ls -ls
total 104
 4 -rw-r--r-- 1 root root     8 Jun  2 21:45 built-in.o
36 -rw-r--r-- 1 root root 35398 Sep 12  2015 ch34x.c
20 -rw-r--r-- 1 root root 20368 Jun  2 21:45 ch34x.ko
 4 -rw-r--r-- 1 root root  2578 Jun  2 21:45 ch34x.mod.c
 8 -rw-r--r-- 1 root root  4944 Jun  2 21:45 ch34x.mod.o
20 -rw-r--r-- 1 root root 18000 Jun  2 21:45 ch34x.o
 4 -rw-r--r-- 1 root root   331 Aug 12  2015 Makefile
 4 -rw-r--r-- 1 root root    55 Jun  2 21:45 modules.order
 0 -rw-r--r-- 1 root root     0 Jun  2 21:45 Module.symvers
 4 -rw-r--r-- 1 root root   531 Sep 12  2015 readme.txt

Code: Select all

lsmod | grep ch34
ch341                  16384  0
usbserial              53248  1 ch341
The problem still exits:

Code: Select all

Sketch uses 8828 bytes (13%) of program storage space. Maximum is 65536 bytes.
Global variables use 1984 bytes of dynamic memory.
Failed to init device.
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Using Parser : Raw BINARY
Interface serial_posix: 230400 8E1

An error occurred while uploading the sketch
So much for short term solution I attempted.


Regards,

John L. Males
Toronto, Ontario
Canada
02 June 2017 17:59

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Sat Jun 03, 2017 3:43 am
by keypunch
I found stm32flash Ticket#81 that may be helpful.

It appears to be a Linux related. Issue was on same machine bare metal Windows, Windows under VM of Linux, and Linux fine with the stm32 board in Windows and Arduino, then selected issues stm32 related in Linux. It appears the issue was related to Even Parity where suggestion to apply a Kernel patch that maybe did not make it to the mainline Linux Kernel. The OP for stm32flash Ticket #81 reported the suggestion to apply a Kernel patch resolved the problem of using stm32flash in Linux.

One notable difference is OP error for stm32flash Ticket #81 indicated

Code: Select all

Interface serial_posix: 57600 8E1
whereas OP and I of this topic had

Code: Select all

Interface serial_posix: 230400 8E1
I do not know if that may be a factor or not.

Does anyone have any experience with success or not using stm32flash in Linux? The stm32flash Ticket #81 seems to suggest there has been issues for stm32flash due to what is believed the kernel patch noted in ticket#81 for stm32flash that may not be in various kernel versions.


Regards,

John L. Males
Toronto, Ontario
Canada
02 June 2017 23:42 EDT

Re: (SOLVED) CH-340 USB to Serial Download issue

Posted: Sat Jun 03, 2017 6:28 pm
by keypunch
I managed to test the Even Parity Kernel patch noted in stm32flash Ticket#81. The Even Parity Kernel patch would not apply clean to the most recent CH340/1 source from chip maker. I took the ch340 source from the the Kernel and ran the Even Parity patch as dry run first and the Even Parity patch applied no issues. I applied the patch, compiled, unloaded original kernel ch340 driver, moved the ordigianl kernel driver out of the kernel modules tree, placed the new complied ch340 driver into the kernel tree and loaded the new ch340 driver.

Sadly the same problem

Code: Select all

Sketch uses 8828 bytes (13%) of program storage space. Maximum is 65536 bytes.
Global variables use 1984 bytes of dynamic memory.
Failed to init device.
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Using Parser : Raw BINARY
Interface serial_posix: 230400 8E1

An error occurred while uploading the sketch
I have had no problems a few months ago using a Maple Mini, Uno and Nano, but these were all via USB bootloader and not serial sketch uploads on exact same Linux system and OS image. I have no Windows system. I had not had a Windows system since the short disaster of 2 years I had Windows 32 bit as my primary system until 1999. Prior to the 2 years of many Windows disasters I used OS/2 since 1989 that was just fine thank you very much. I have used Linux since 1999 as my primary system with my Windows limited to professional situations and solving Windows problems for others.

Thoughts or suggestions?

Regards,

John L. Males
Toronto, Ontario
Canada
03 June 2017 14:21 EDT
03 June 2017 14:30 EDT Minor typo correction
03 June 2017 21:11 EDT Typo corrections and OS/2 omission corrected