I'm learning the ins and outs of an MSPM0 controller. I'm using an MSPM01105, that's low on power use. But also low on available memory.
I tried to use the standard C library's sprintf() function in a project, to format a string with a few integers. That's not possible. The function exhausts this Arm M0's resources.
So I went looking for alternatives. One of those is the printf library. It does a great job at delivering a lot of the stdlib functionality, cheaper. Of course, it's all based on compromises. But definitely usable.
Using printf
Your project will need 2 files: printf.c and printf.h. Once you add those to your project, you can start using the sprintf() function. It has the same formatting commands as the stdlib one. Just less options. That's what I use. I don't use this library to immediately printf() to UART. I use sprintf() to format memory buffer strings, that can later be sent by native buffered UART.
If you also want to use the printf() to print formatted text directly to UART, you'll have to write a putchar_() function that knows how to output data to your UART. Else a printf() will do nothing.
Example:
#include "printf.h" #define buf_len 16 #define fifo_len 4 char txPacket[buf_len]; void stream_data() { sprintf(txPacket, "[%+5i %+5i]\n", myIntA, myIntB); for(uint32_t i = 0; i < buf_len / fifo_len; i++) { /* Fills TX FIFO with data and transmits the data */ DL_UART_Main_fillTXFIFO(UART_0_INST, &txPacket[0 + i * fifo_len], fifo_len); /* Wait until all bytes have been transmitted and the TX FIFO is empty */ while (DL_UART_Main_isBusy(UART_0_INST)) ; } }
Lowering the footprint further:
If you don't need to format floats and exponential, you can set two symbols to 0:
PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS=0
PRINTF_SUPPORT_DECIMAL_SPECIFIERS=0
This takes care that the most expensive chunks of code don't get added to your project, if you don't need them.
Example
Here is example output, created with sprintf(txPacket, "[%+5i %+5i]\n", myIntA, myIntB):
thank you for reading.