Printing Floats

Due to restrictions of the ARM EABI, printing floats correctly requires a couple of adjustments to the project and code:

  1. The printf formatter must support float types. The Large or Full printf formatter must be selected in the IAR project menu under Options... > General Options  > Library Options. Similarly, if scanf is used to parse float inputs, the appropriate scanf formatter should be selected.

  2. The uC/OS-II task stack from which printf is called must be 8-byte aligned.

In most cases while prototyping, we will want to print float values in response to CLI commands. In order to print floating point values from the CLI task in an On-Chip SDK application, adjust the following declaration of cli_task_v in src/app/common/cli_task.c:

//=========================== variables ======================================= typedef struct { OS_STK cliTaskStack[CLI_TASK_STK_SIZE]; char* appName; dnm_ucli_cmdDef_t const* cliCmds; INT32U cliChannelBuffer[1+DN_CH_ASYNC_RXBUF_SIZE(DN_CLI_NOTIF_SIZE)/sizeof(INT32U)]; CH_DESC cliChannelDesc; INT8U numCliCommands; } cli_task_vars_t; #pragma data_alignment = 8 cli_task_vars_t cli_task_v;

Notice that the cliTaskStack field is moved to the beginning of the structure so that it is sufficient to align the cli_task_v variable. If the stack field was not at the beginning of the structure, we would have to align the cli_task_v variable and ensure that the fields before the stack occupied a multiple of 8 bytes.

In order to print floats from other tasks, it’s necessary to align the stack of the appropriate task. In the simplest case, when the task stack is declared separately, it’s straightforward to specify the data alignment.

// app variables #pragma data_alignment = 8 OS_STK myTaskStack[STACK_SIZE];

Otherwise, if the stack is declared in a structure containing several fields, we can use the same approach as demonstrated above, moving the task stack to the beginning of the structure and aligning the whole structure.

// app variables typedef struct { OS_STK myTaskStack[STACK_SIZE]; ... } my_task_vars_t; #pragma data_alignment = 8 static my_task_vars_t my_task_v;

See also http://supp.iar.com/Support/?Note=85413.