Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12747 Discussions

printf on Nios2 not working properly

Altera_Forum
Honored Contributor II
1,852 Views

Hi,  

 

I am working on a custom board with Nios2. I am facing a weird problem with printf.  

When I do any printf I am getting an inversed output. 

 

i.e. If I do printf("Hello World"); I get eHll ooWlrd. Take 2 chars from H and reverse. U get the o/p I am getting. 

 

All this is happening in vsprintf which is called by printf for formatting. 

 

Below I will give the code snippet where things are going wrong. 

 

int vsprintf(char *buf, const char *fmt, va_list args) 

................... 

char * str; 

................... 

 

for (temp=str=buf ; *fmt ; ++fmt) { 

if (*fmt != '%') { 

*str++ = *fmt; 

continue; 

 

..................... 

 

case 's': 

s = va_arg(args, char *); 

if (!s) 

s = "<NULL>"; 

 

len = strnlen(s, precision); 

 

if (!(flags & LEFT)) 

while (len < field_width--) 

*str++ = &#39; &#39;; 

 

for (i = 0; i < len ; ++i) 

*str++ = *s++; 

 

 

while (len < field_width--) 

*str++ = &#39; &#39;; 

 

continue; 

.................... 

 

 

 

 

for (i = 0; i < len ; ++i) 

*str++ = *s++; 

 

This is where every thing goes wrong. 

 

 

 

suppose s="Hello from NiosII!" it coppies correctly to str but it appears as mentioned above in buf i.e "eHll oofmoN oiIsI.  

 

Clue: 

 

For loop which is suppose to incerement from 0 to len, actually decrements from len to 0. I beleive it is compiler optimization.
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
941 Views

 

--- Quote Start ---  

originally posted by shakthi@Mar 28 2006, 03:12 AM 

hi,  

 

i am working on a custom board with nios2. i am facing a weird problem with printf.  

when i do any printf i am getting an inversed output. 

 

i.e. if i do printf("hello world"); i get ehll oowlrd. take 2 chars from h and reverse. u get the o/p i am getting. 

 

all this is happening in vsprintf which is called by printf for formatting. 

 

below i will give the code snippet where things are going wrong. 

 

int vsprintf(char *buf, const char *fmt, va_list args) 

................... 

char * str; 

................... 

 

for (temp=str=buf ; *fmt ; ++fmt) { 

  if (*fmt != &#39;%&#39;) { 

*str++ = *fmt; 

continue; 

  } 

 

..................... 

 

  case &#39;s&#39;: 

s = va_arg(args, char *); 

if (!s) 

    s = "<null>"; 

 

len = strnlen(s, precision); 

 

if (!(flags & left)) 

    while (len < field_width--) 

  *str++ = &#39; &#39;; 

 

for (i = 0; i < len ; ++i) 

    *str++ = *s++; 

 

 

while (len < field_width--) 

    *str++ = &#39; &#39;; 

 

continue; 

.................... 

 

 

 

 

for (i = 0; i < len ; ++i) 

      *str++ = *s++; 

 

this is where every thing goes wrong. 

 

 

 

suppose s="hello from niosii!" it coppies correctly to str but it appears as mentioned above in buf i.e "ehll oofmon oiisi.  

 

clue: 

 

      for loop which is suppose to incerement from 0 to len, actually decrements from len to 0. i beleive it is compiler optimization. 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=13861) 

--- quote end ---  

 

--- Quote End ---  

 

 

 

Looks like it&#39;s byte swapped to me...
0 Kudos
Altera_Forum
Honored Contributor II
941 Views

Hi shakthi, 

 

> When I do any printf I am getting an inversed output. 

 

Ok, this is the same problem you posted WRT u-boot. Are you now using the 

system library and seeing the same problems? 

 

And the big question: are you using a custom memory component, or a custom 

bridge between your system module and an on/off-chip memory? 

 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
941 Views

 

--- Quote Start ---  

originally posted by fmcmurra+mar 28 2006, 10:41 pm--><div class='quotetop'>quote (fmcmurra @ mar 28 2006, 10:41 pm)</div> 

--- quote start ---  

<!--quotebegin-shakthi@Mar 28 2006, 03:12 AM 

hi,  

 

i am working on a custom board with nios2. i am facing a weird problem with printf.  

when i do any printf i am getting an inversed output. 

 

i.e. if i do printf("hello world"); i get ehll oowlrd. take 2 chars from h and reverse. u get the o/p i am getting. 

 

all this is happening in vsprintf which is called by printf for formatting. 

 

below i will give the code snippet where things are going wrong. 

 

int vsprintf(char *buf, const char *fmt, va_list args) 

................... 

char * str; 

................... 

 

for (temp=str=buf ; *fmt ; ++fmt) { 

  if (*fmt != &#39;%&#39;) { 

*str++ = *fmt; 

continue; 

  } 

 

..................... 

 

  case &#39;s&#39;: 

s = va_arg(args, char *); 

if (!s) 

    s = "<null>"; 

 

len = strnlen(s, precision); 

 

if (!(flags & left)) 

    while (len < field_width--) 

  *str++ = &#39; &#39;; 

 

for (i = 0; i < len ; ++i) 

    *str++ = *s++; 

 

 

while (len < field_width--) 

    *str++ = &#39; &#39;; 

 

continue; 

.................... 

 

 

 

 

for (i = 0; i < len ; ++i) 

      *str++ = *s++; 

 

this is where every thing goes wrong. 

 

 

 

suppose s="hello from niosii!" it coppies correctly to str but it appears as mentioned above in buf i.e "ehll oofmon oiisi.  

 

clue: 

 

      for loop which is suppose to incerement from 0 to len, actually decrements from len to 0. i beleive it is compiler optimization. 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=13861) 

--- quote end ---  

 

--- Quote End ---  

 

 

 

Looks like it&#39;s byte swapped to me... 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=13873)</div> 

[/b] 

--- Quote End ---  

 

 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

Yappppp its a byte swap[/b] 

--- Quote End ---  

0 Kudos
Altera_Forum
Honored Contributor II
941 Views

Yes. I had posted the same wrt U-Boot.  

 

It is a custom board with Nios II, Intel strata flash and Samsung SDRAM and few more components. Off chip SDRAM is connected to Nios directly through Avalon switch fabric and no tristate bridge is used in between. While configuring SDRAM controller in SOPC since samsung memory is available in dropdown list of the controller wizard the SDRAM controller is generated for custom memory. 

 

We are using SDRAM controller and Avalon switch fabric from SOPC builder. 

 

Here is what exactly happens: 

 

Assume I am trying to write &#39;a&#39; at 0x0 using a char *. It puts &#39;a&#39; at 0x0. When I put &#39;b&#39; at 0x1, It moves &#39;a&#39; from 0x0 to 0x1 and puts &#39;b&#39; at 0x0. 

 

 

I have tested It with Nios II IDE&#39;s sample programs, I get the same result. 

 

I could even port linux kernel to the board, but got the same result. 

 

 

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/unsure.gif
0 Kudos
Altera_Forum
Honored Contributor II
941 Views

Hi shakthi, 

 

> I have tested It with Nios II IDE&#39;s sample programs, I get the same result. 

 

I did not think it was a u-boot problem. 

 

> It puts &#39;a&#39; at 0x0. When I put &#39;b&#39; at 0x1, It moves &#39;a&#39; from 0x0 to 0x1 and puts &#39;b&#39; 

> at 0x0. 

 

Your hardware is broken -- you need to fix it and stop wasting time looking for 

problems in software that has been stable for years. Your byte enable signals 

are probably reversed, maybe shorted -- and check your SDRAM configuration 

in SOPCBuilder as well. 

 

Regards, 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
941 Views

Yapp. Now I know its not a problem in U-Boot or Nios II programs or the kernel. 

 

Its a problem of byte swap due to some wrong hardware configuration. We are trying to figure out the problem in the hardware. 

 

We have already checked the configuration data for SDRAM and looks correct. We are trying with different phase shifts.  

 

Now we have noticed that there is swapping in nibbles too. 

 

Thanks for your help. 

 

Looking forward for more support. 

 

Regards, 

Shakthi
0 Kudos
Reply