Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

using printf to print alt_u32 data

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?

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

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

    printf("%x", test) for hexadecimal output
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    %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...