Function similar to printf for software UART, without the need for a large temporary buffer?
-
I'm programming in C in an embedded environment and I have a bit-bang software UART I would like to be able to hook up to a myPrintf function, that supports the standard printf syntax, e.g.
myPrintf("%s %3.2f\n", "Pi:", 3.14159);
I would like to achieve this without having to use a big (=worst case length of "sentence" to transmit in a single myPrintf call) temporary buffer (otherwise, I could just go via sprintf). What I want to achieve is that my characters are sent over my UART "on the fly", so in the example above "Pi: " would be sent out over my UART before the float number has been converted to ASCII characters. Does anybody know how I can achieve this in the easiest way possible? Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
-
I'm programming in C in an embedded environment and I have a bit-bang software UART I would like to be able to hook up to a myPrintf function, that supports the standard printf syntax, e.g.
myPrintf("%s %3.2f\n", "Pi:", 3.14159);
I would like to achieve this without having to use a big (=worst case length of "sentence" to transmit in a single myPrintf call) temporary buffer (otherwise, I could just go via sprintf). What I want to achieve is that my characters are sent over my UART "on the fly", so in the example above "Pi: " would be sent out over my UART before the float number has been converted to ASCII characters. Does anybody know how I can achieve this in the easiest way possible? Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
Quote:
Does anybody know how I can achieve this in the easiest way possible?
Not sure if easiest but one common way of doing it is to have
printf
call aputc
-type function to send out each succesive character. In your case, the "putc" function would be the UART output function.Quote:
Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
Plenty of those. One I've used before in an embedded environment is for Rabbit microcontrollers. You can find the relevant code here: DCRabbit_10/STDIO.LIB at master · digidotcom/DCRabbit_10 · GitHub[^]
Mircea
-
I'm programming in C in an embedded environment and I have a bit-bang software UART I would like to be able to hook up to a myPrintf function, that supports the standard printf syntax, e.g.
myPrintf("%s %3.2f\n", "Pi:", 3.14159);
I would like to achieve this without having to use a big (=worst case length of "sentence" to transmit in a single myPrintf call) temporary buffer (otherwise, I could just go via sprintf). What I want to achieve is that my characters are sent over my UART "on the fly", so in the example above "Pi: " would be sent out over my UART before the float number has been converted to ASCII characters. Does anybody know how I can achieve this in the easiest way possible? Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
Probably existing
printf
code is too general for your system, so, it is probably better to make yourself an over-simplified version of it, tailored on your needs. Anyway, you know, in order to use just a small buffer you need to append characters to the buffer while there is still available space and wait patiently the transmission of characters in order to use the buffer again. This means you can't actually use a function call similar to theprintf
one."In testa che avete, Signor di Ceprano?" -- Rigoletto
-
Quote:
Does anybody know how I can achieve this in the easiest way possible?
Not sure if easiest but one common way of doing it is to have
printf
call aputc
-type function to send out each succesive character. In your case, the "putc" function would be the UART output function.Quote:
Is the source code for printf available somewhere for public use so I can modify it to fit my needs?
Plenty of those. One I've used before in an embedded environment is for Rabbit microcontrollers. You can find the relevant code here: DCRabbit_10/STDIO.LIB at master · digidotcom/DCRabbit_10 · GitHub[^]
Mircea
Mircea Neacsu wrote:
Not sure if easiest but one common way of doing it is to have
printf
call aputc
-type function to send out each succesive character. In your case, the "putc" function would be the UART output function.That sounds very convenient. I am working with STM32F-microcontrollers, would you happen to know if it's easy to replace the putc-function for their printf-function?
Mircea Neacsu wrote:
Plenty of those. One I've used before in an embedded environment is for Rabbit microcontrollers. You can find the relevant code here: DCRabbit_10/STDIO.LIB at master · digidotcom/DCRabbit_10 · GitHub[^]
Awesome!
-
Probably existing
printf
code is too general for your system, so, it is probably better to make yourself an over-simplified version of it, tailored on your needs. Anyway, you know, in order to use just a small buffer you need to append characters to the buffer while there is still available space and wait patiently the transmission of characters in order to use the buffer again. This means you can't actually use a function call similar to theprintf
one."In testa che avete, Signor di Ceprano?" -- Rigoletto
CPallini wrote:
in order to use just a small buffer you need to append characters to the buffer while there is still available space and wait patiently the transmission of characters in order to use the buffer again. This means you can't actually use a function call similar to the
printf
one.It's a completely synchronuous bit-bang UART where I'm counting clock cycles. There is no DMA, timer interrupts, 1 byte FIFO, threading or anything fancy.
-
Mircea Neacsu wrote:
Not sure if easiest but one common way of doing it is to have
printf
call aputc
-type function to send out each succesive character. In your case, the "putc" function would be the UART output function.That sounds very convenient. I am working with STM32F-microcontrollers, would you happen to know if it's easy to replace the putc-function for their printf-function?
Mircea Neacsu wrote:
Plenty of those. One I've used before in an embedded environment is for Rabbit microcontrollers. You can find the relevant code here: DCRabbit_10/STDIO.LIB at master · digidotcom/DCRabbit_10 · GitHub[^]
Awesome!
Quote:
I am working with STM32F-microcontrollers, would you happen to know if it's easy to replace the putc-function for their printf-function?
No idea! you said you have the bit banging UART code; you just have to use it to send out one character. Keep in mind also what @CPallini suggested: if you don't need a very general
printf
function you can have with a much simpler implementation. Look at this one for an example: sdcc/printf_small.c at master · darconeous/sdcc · GitHub[^] Also, along the same lines, you scan the format specifier string and every time you encounter a '%' character you can usesprintf
to print it in a small buffer.Mircea
-
CPallini wrote:
in order to use just a small buffer you need to append characters to the buffer while there is still available space and wait patiently the transmission of characters in order to use the buffer again. This means you can't actually use a function call similar to the
printf
one.It's a completely synchronuous bit-bang UART where I'm counting clock cycles. There is no DMA, timer interrupts, 1 byte FIFO, threading or anything fancy.
So you have to it in place. Forgot the
printf
signature and write a function to output a string, a character at time. Then write a function to output properly a floating point number (that would be more difficult, but it depends on your requirements) and combine the two function in order to obtain the expected result."In testa che avete, Signor di Ceprano?" -- Rigoletto