Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Function similar to printf for software UART, without the need for a large temporary buffer?

Function similar to printf for software UART, without the need for a large temporary buffer?

Scheduled Pinned Locked Moved C / C++ / MFC
hardwaretutorialquestionworkspace
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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?

    M C 2 Replies Last reply
    0
    • A arnold_w

      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?

      M Offline
      M Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      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 a putc-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

      A 1 Reply Last reply
      0
      • A arnold_w

        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?

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        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 the printf one.

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        A 1 Reply Last reply
        0
        • M Mircea Neacsu

          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 a putc-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

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #4

          Mircea Neacsu wrote:

          Not sure if easiest but one common way of doing it is to have printf call a putc-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!

          M 1 Reply Last reply
          0
          • C CPallini

            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 the printf one.

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            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.

            C 1 Reply Last reply
            0
            • A arnold_w

              Mircea Neacsu wrote:

              Not sure if easiest but one common way of doing it is to have printf call a putc-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!

              M Offline
              M Offline
              Mircea Neacsu
              wrote on last edited by
              #6

              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 use sprintf to print it in a small buffer.

              Mircea

              1 Reply Last reply
              0
              • A arnold_w

                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.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups