This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Lightweight Debug Printf using nrfx_uart Drivers

Any suggestions for the simplest lightest way to add basic printf to a debug uart?

nRF52840, SDK 17.0.2, Nordic SES 5.30a, using nrfx_* drivers

I've looked at app_uart_fifo.c/h (as used in SDK UART example) but it seems overkill, and uses the legacy nrf_drv_uart drivers. When I tried implementing it in my app, I hit silly problems such as compiler couldn't see nrf_uarte_baudrate_t etc. I'm sure I could sort it given time, but seems messy.

I'm trying to keep my code clean and simple, and am already using the nrfx_uart drivers elsewhere, don't really want to bring in legacy stuff as well.

I also looked at nrf_libuarte but again seems complete overkill for a simple debug UART, I just want to send simple data to a terminal

I have the basic uart running using nrfx_uart drivers, simples.

I'm tempted to just write my own putchar and printf, maybe using nrf_fprintf.

But wanted to check first, is there a better/simpler way?

Many thanks

  • Hi benmark

    I'm tempted to just write my own putchar and printf, maybe using nrf_fprintf.

    But wanted to check first, is there a better/simpler way?

     We used to have a very simple putc and getc functions in SDK7.2 that did a very basic version of that. We did not have nRFX then but maybe it is something you can still use. 

    Attaching the SDK zip for your convinience. Take a look at 

    SDK\components\libraries\uart\retarget.c
    SDK\components\drivers_nrf\uart\

    The app_uart.c is lightweight here since it accesses the registers directly. You can still strip away a lot of things here that you do not want to use making it even lightweight. But if you want to use nRFX driver and make something lightweight, then please do care to share that here so others will be able to utilize your efforts.

    1222.nRF51_SDK_7.2.0_cf547b5.zip

  • Hi Susheel

    Thanks for your reply, at least you have confirmed I won't be reinventing the wheel. I'll have a go, and share my progress on here.

    And thanks for the info about the old putc/getc stuff, I'll take a look.

    Just curious, how do most people tackle this? app_uart_fifo? nrf_libuarte? Or just stick with the RTT NRF_LOG stuff?

    Thanks, Ben

  • In case it helps anyone, here is an example of code I've been using so far. Not even using printf, just dumping 12bit hex to the UART. Very light (and of course rather crude, no error checking), but just what I wanted and easy to implement

       // serial logging of u16 amg_pix[i] in 12 bit ASCII HEX - \r\nxABCxABC....
       #define AMG_DBG_BUFF AMG_PIXELS*4 + 2
       uint8_t junk[AMG_DBG_BUFF];
       u32 jp = 0;
       junk[jp++] = '\r';
       junk[jp++] = '\n';
       for(i=0; i<AMG_PIXELS; i++)
       {
          // each pixel sent as xABC 12 bit hex
          junk[jp++] = 'x';
    
          u8 nib = (u8)(amg_pix[i] >> 8) & 0x000F;
          if (nib > 9) nib+=('A'-10); else nib+='0';
          junk[jp++] = nib;
    
          nib = (u8)(amg_pix[i] >> 4) & 0x000F;
          if (nib > 9) nib+=('A'-10); else nib+='0';
          junk[jp++] = nib;
    
          nib = (u8)(amg_pix[i]) & 0x000F;
          if (nib > 9) nib+=('A'-10); else nib+='0';
          junk[jp++] = nib;
       }
       debug_uart_tx(junk, jp);

    void debug_uart_tx(uint8_t const * buff, size_t len)
    {
       while (nrfx_uart_tx_in_progress(&uart_debug));
       nrfx_uart_tx(&uart_debug, buff, len);
    }
    

  • Thank you for posting it here, the title is very relevant and easy for others to find such good info.

Related