printf from VirtualSerial

20 views
Skip to first unread message

smi...@gach.us

unread,
Jun 8, 2012, 8:26:49 AM6/8/12
to Micropendous
Hello,

I have a Mega32U2. I built the VirtualSerial Demo and I can control
the demo from a serial console like TerraTerm. From within the
MainTaskLoop, I can print messages and they appear in the serial
console. However, when I add more source files to the project and
then attempt to print from the functions in the other source files,
the output never appears. The code does not crash and when I look at
the listing file, it appears that these lower code sections are
calling the same fputs() as the prints in the MainTaskLoop. If I
search the elf file, the message strings from the printfs of the lower
code levels are included. With the additional code added, printf
from the MainTaskLoop still works. I tried suppressing all printfs
from the MainTaskLoop to see if it was stuck in a buffer somewhere
that was getting overwritten, but still no joy.

What is the trick to get printf to work from any level of the code?

Thanks.

--- Steve

P.S. I tried to switch over the the RTOS version. While I can get
that version to compile, it will not enumerate as a Serial Port in my
Windows XP.

Opendous Inc.

unread,
Jun 8, 2012, 8:54:13 PM6/8/12
to Micropendous
The way the print system is initialized in the code:
http://code.google.com/p/micropendous/source/browse/trunk/Micropendous/Firmware/USBVirtualSerial/USBVirtualSerial.c#81
http://code.google.com/p/micropendous/source/browse/trunk/Micropendous/libs/LUFA/LUFA/Drivers/USB/Class/Device/CDCClassDevice.c#307

You need access to the USBSerialStream FILE:
http://code.google.com/p/micropendous/source/browse/trunk/Micropendous/Firmware/USBVirtualSerial/USBVirtualSerial.c#66

Move the "static FILE USBSerialStream;" line to a header file such as
USBVirtualSerial.h and include that header in your other source files.

This sort of configuration allows the default stream files, 0(stdin),
1(stdout), and 2(stderr) to be used for another stream such as UART.
I suggest you port all your print code to the stream versions (f...).
It helps with readability and allows you to print to any number of
different files. Since the print system just requires custom putchar
and getchar functions, you can print to SPI, memory, GPIO, or anything
you can think of.
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html

I will look into WinXP and the RTOS version. I too have had problems
but I thought they were solved in this version.

smi...@gach.us

unread,
Jun 10, 2012, 11:29:31 PM6/10/12
to Micropendous
Ok,

I tried moving the {static FILE USBSerialStream; } into the header
file and then including that header file. However, this too fails to
print for me from the lower level of code. I created a simple test
case that displays what I am seeing. Unfortunately, there does not
appear to be a way to attach the zip file to this group. Where can I
upload the test case?

Thanks.

---- Steve

Opendous Support

unread,
Jun 11, 2012, 2:15:20 PM6/11/12
to microp...@googlegroups.com
If you reply to the group email with an attachment it should work but
please avoid large files (>100kb).

Example code is always best to aid debugging.
helloworld.txt

smi...@gach.us

unread,
Jun 12, 2012, 10:04:09 AM6/12/12
to microp...@googlegroups.com

Switched to the "new" Google Groups and that version has an upload option.  Attached is modified files that have the fprintf issue when I run them.  My board is a Mega32U2, so you will have to modify the makefile to reference your HW version. 
PrintTest.zip

Opendous Support

unread,
Jun 12, 2012, 9:52:57 PM6/12/12
to microp...@googlegroups.com
The solution was to make the FILE variable extern in the header,
"extern FILE USBSerialStream;", and then just "FILE USBSerialStream;"
in the source. I spaced on the fact that static limits the scope of a
variable to the enclosing source file.
http://en.wikipedia.org/wiki/Static_variable

The zip contents are meant to replace the VirtualSerial directory
from the latest Micropendous release (Micropendous-2011-11-26). I
made some minor changes to your test print. Non-variable text should
really be in fprintf_P(PSTR ... ) to limit RAM usage.
VirtualSerial.zip
VirtualSerial_printf_Working_Externally.jpg

smi...@gach.us

unread,
Jun 13, 2012, 8:20:00 AM6/13/12
to microp...@googlegroups.com


Great!!    I made the change you suggested and it is working on my hardware as well.   Thank you very much for the help.

---- Steve
Reply all
Reply to author
Forward
0 new messages