Help with Odd 328p UART Problem

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings All,

I'm not sure what is causing this problem, so I wasn't sure what to search for. I didn't notice anything in the errata thread that might cover this.

I am using an ATMega328P dev board (Arduino Nano), programming it using Eclipse and uploading code using the built in AVRDude interface. The program basically queries several I2C devices and displays their formatted values on a terminal screen. There are different operating modes which changes the data and menus displayed on screen. The program has been performing as expected to this point. Today I was attempting to add a new menu selection to the code, using the same format as the other menus. However, once I added a particular printf statement, the code no longer runs. It begins to run and it has some serial output but then it locks up. I have tried simplifying the code and reducing duplication in the code, but the problem remains. I have attached all the files in the project but the problem occurs around line 90 of 328comms.c. There are presently 3 lines commented out in this section. Uncommenting any of these lines causes the program to hang. Not sure what is causing this especially since those lines are not active at the time the chip boots.
Can anyone think of a reason the code would cause the chip to hang?

 

The main file is 328comms.c
 

Greydesk

(Yes, the code isn't the prettiest yet. Please no comments on code neatness. :)

Attachment(s): 

This topic has a solution.
Last Edited: Wed. Mar 21, 2018 - 03:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

running out of ram (stack space) would be the first place to look!

Is there an option to automatically place strings from printf in flash using gcc?

 

Jim

 

 

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

running out of ram (stack space) would be the first place to look!

 

There's something I hadn't thought about.

Invoking: Print Size
avr-size --format=avr --mcu=atmega328p ATMega328-Comms.elf
AVR Memory Usage
----------------
Device: atmega328p

Program:   12978 bytes (39.6% Full)
(.text + .data + .bootloader)

Data:       1919 bytes (93.7% Full)
(.data + .bss + .noinit)


Finished building: sizedummy

So it looks like data might be getting full. Researching moving printf to program memory

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

running out of ram (stack space) would be the first place to look!

Is there an option to automatically place strings from printf in flash using gcc?

 

I was unable to find an option to automatically place strings from printf in flash using gcc. However, I did discover the fprint_P statement, which looks in program space for the data for fprint, and I found the PSTR() function which stores the data within in program memory. This has fixed the problem. I haven't fixed all the appropriate printf statements yet, but:

2:26:33 **** Incremental Build of configuration Release for project ATMega328-Comms ****
make all 
Invoking: Print Size
avr-size --format=avr --mcu=atmega328p ATMega328-Comms.elf
AVR Memory Usage
----------------
Device: atmega328p

Program:   13494 bytes (41.2% Full)
(.text + .data + .bootloader)

Data:       1367 bytes (66.7% Full)
(.data + .bss + .noinit)


Finished building: sizedummy

You can see that Data memory is now not entirely full. I won't be running out of program space either.

 

Thanks!

 

I can't seem to find a 'solved' checkbox.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greydesk wrote:
I can't seem to find a 'solved' checkbox.

 

awneil wrote:
See Tip #5
awneil wrote:
When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...

https://www.avrfreaks.net/commen...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

arduino folks do not tend to tell beginners that RAM is a precious resource in small uC's and constant strings should be put in FLASH only.

It is not really needed to make a blinking led program.

 

Sometimes I use sprintf(); because I thought is was an easy thing to do to output some debugging data.

Then I once got into trouble because my flash was running out (on a atmega8 project, just 8kB of flash).

I already had some number formatting routines written for an LCD display.

So I modified it to write to a RAM buffer and return a pointer to the last character written.

In this way you can build complex strings in bits and pieces by adding code lines.

In the code snippet below I use one of my number formatting functions to build such a string in the data buffer of an RS485 network node.

But because of this modular way of function calls you can easily use different functions to build your output string.

Adding or removing a part of the formatted numbers (temporarily by commenting it out) is also easy.

Single character consants such as '\n' and ' ' are also not ram variables and loaded directly from flash without the P_STR hassle.

	ptext = MsgOut.Data;
	*ptext++ = '\n';
	*ptext++ = ' ';
	ptext = SPrintFixed( ptext, RunTime,		5, 0);
	ptext = SPrintFixed( ptext, NewPosition,	5, 0);
	ptext = SPrintFixed( ptext, Speed,		5, 0);
	ptext = SPrintFixed( ptext, SpeedTarget,	5, 0);
	ptext = SPrintFixed( ptext, Power,		5, 0);
	ptext = SPrintFixed( ptext, OCR2,		5, 0);
	ptext = SPrintFixed( ptext, PP,			5, 0);
	ptext = SPrintFixed( ptext, PS,			5, 0);
	ptext = SPrintFixed( ptext, IS / K_I_S_DIV,	5, 0);
	MsgOut.Dest = NODE_ADDRESS_NETWORK_MANAGEMENT;
	MsgOut.Flags = 0;
	MsgOut.Size = ptext - MsgOut.Data;
	MsgOut.SendN( 10);

For your convenience I've attached snippets.zip with a few of these string funcions (you will probably have to edit them a bit for your liking, this is not a finished project).

Attachment(s): 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Wed. Mar 21, 2018 - 04:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
For your convenience I've attached snippets.zip

Seems to be missing a few headers.....     header.h, mumar_data.h ....

Perhaps they can be found on the website listed

 

 

Jim

 

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GCC has sprintf_p() as well as normal printf(). Also %S can be used in place of %s when text should be LPMd. Finally, as you found, PSTR() is the way to coerce text into "flash only" in the first place fir use by those mechanisms.
.
BTW as a general rule, at build time, "Data:" should never exceed about 75% as you need the rest for stack frames though this will depend on how heavily you use stack frame automatics.

Last Edited: Wed. Mar 21, 2018 - 04:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Also %S can be used in place of %s when text should be LPMd

That seems easy, thanks!

 

Jim

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One day someone will write a user manual and write all this stuff down ;-)
.
(oh, wait a minute...)