Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20705 Discussions

using printf to print alt_u32 data

Altera_Forum
Honored Contributor II
3,615 Views

Hello, 

 

I am trying print the result from a FFT calculation using DMA. However, I am not sure how to use printf to accomplish that. I suspect tha it would look something like this: 

 

alt_u32 test = 1; 

printf("%alt_u32", test); 

 

But I am not sure how printf would handle the 32 bit data. 

 

I also read somewhere that it may be better to just print the data byte by byte.  

I just want to verify that the FFT component is actually calculating the data corectly 

 

Any suggestions?
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
2,294 Views

printf("%d", test) for decimal output 

printf("%x", test) for hexadecimal output
0 Kudos
Altera_Forum
Honored Contributor II
2,294 Views

Thanks! Is there a way to describe the alt_u32, alt_32, or other altera types directly? Or is it better to just use the builtin type, which defines the altera type?  

 

For example: 

 

%f for float 

%i for integer 

%????? for altera types?? 

 

Thanks
0 Kudos
Altera_Forum
Honored Contributor II
2,294 Views

The % format codes are somewhat a C language standard. Although printf() actually is a library function and is not strictly related to the language itself, almost every compiler/C tools defines the printf function and these formats in the exactly same way. 

IMHO there is no need for extra formats for alt_xx type, since they are simple integer types. 

So you will always use %d or %x depending if you want them displayed in dec or hex. You could possibly use %f for float format but this makes no sense for integers. 

Maybe you'd like extra format directives, i.e. %04x for printing 4 digits and padding with zeros on the left.  

Example: 

alt_u32 a; 

a = 100; 

printf("%d", a); displays "100" 

printf("%x", a); displays "64" 

printf("%f", a); displays "64.000000" (I'm not sure) 

printf("%06x", a); displays "000064" 

 

Look in any C library manual and you'll find all the format codes. 

 

 

Cris
0 Kudos
Altera_Forum
Honored Contributor II
2,294 Views

Thanks Cris!

0 Kudos
Altera_Forum
Honored Contributor II
2,294 Views

%d %i %u and %x are in fact for the C "int" and "unsigned int" types. This also works with alt_u32 because in the C compiler used with the Nios architecture, the int type is defined as 32 bits. This is true for most architectures, but if you want to be sure that your code is really portable, it would be best to cast it to (int) or (unsigned int) in the printf call:printf("%d", (int)a);If you'll only run your code on Nios 2 you don't need to do that, but you never know when your code will be reused elsewhere...

0 Kudos
Altera_Forum
Honored Contributor II
2,294 Views

Nice! I checked the typedef for alt_*, and you are correct.

0 Kudos
Reply