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

nRF52840 SDK16 - Print an array of hex values using NRF_LOG_HEXDUMP_INFO

Hi everyone,

I want to serial print an array of bytes in hex format. The array size is 200bytes. I tried to use the NRF_LOG_HEXDUMP_INFO but it doesn't print all the array, it prints just the first 72bytes

The second approach works fine. How I would like to use the  NRF_LOG_HEXDUMP_INFO since it is an out of the box function.

// First approach
  NRF_LOG_HEXDUMP_INFO(read_raw_data,read_payload);

// Second approach
  char changeline = 20;
  for (int i = 0; i < read_payload; i++) {
    NRF_LOG_RAW_INFO("0x%x ", read_raw_data[i]);
    NRF_LOG_FLUSH();
    if (i == changeline-1) {
      NRF_LOG_RAW_INFO("\r\n");
      changeline += 20;
    }
  }

Thanks in advance

Nick

Parents
  • Hi Vidar and thank you for your support,

    This merely solve the problem.. By increasing the NRF_LOG_MSGPOOL_ELEMENT_COUNT  I can print "all" the buffer but some of the data are not get printed as shown below

    This is how I call the function

    #define read_payload 100
    char read_raw_data[read_payload] = {0};
    
    NRF_LOG_HEXDUMP_INFO(read_raw_data,read_payload);
    NRF_LOG_FLUSH();

  • Hi,

    I should have tested this first. I think the NRF_LOG_PUSH() macro should be used like this: NRF_LOG_HEXDUMP_INFO(NRF_LOG_PUSH(read_raw_data),read_payload);. However, that just lead to corrupted data when I tested it. Can you try to make  'read_raw_data' "static" instead?

    I also observed lost logs when I used the RTT backend, but not with UART. Are you using RTT or UART?

    C code

        NRF_LOG_INFO("-- Byte Array --");
        static uint8_t hex_dump[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe,
            0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
            0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
            0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
            0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
            0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
            0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
            0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
            0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
            0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
            0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
            0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
            0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2,
            0xc3, 0xc4, 0xc5, 0xc6, 0xc7};
    
        NRF_LOG_HEXDUMP_INFO(hex_dump, sizeof(hex_dump));

    UART log

    <info> app: -- Byte Array --
    <info> app:  00 01 02 03 04 05 06 07|........
    <info> app:  08 09 0A 0B 0C 0D 0E 0F|.  ......
    <info> app:  10 11 12 13 14 15 16 17|........
    <info> app:  18 19 1A 1B 1C 1D 1E 1F|........
    <info> app:  20 21 22 23 24 25 26 27| !"#$%&'
    <info> app:  28 29 2A 2B 2C 2D 2E 2F|()*+,-./
    <info> app:  30 31 32 33 34 35 36 37|01234567
    <info> app:  38 39 3A 3B 3C 3D 3E 3F|89:;<=>?
    <info> app:  40 41 42 43 44 45 46 47|@ABCDEFG
    <info> app:  48 49 4A 4B 4C 4D 4E 4F|HIJKLMNO
    <info> app:  50 51 52 53 54 55 56 57|PQRSTUVW
    <info> app:  58 59 5A 5B 5C 5D 5E 5F|XYZ[\]^_
    <info> app:  60 61 62 63 64 65 66 67|`abcdefg
    <info> app:  68 69 6A 6B 6C 6D 6E 6F|hijklmno
    <info> app:  70 71 72 73 74 75 76 77|pqrstuvw
    <info> app:  78 79 7A 7B 7C 7D 7E 7F|xyz{|}~.
    <info> app:  80 81 82 83 84 85 86 87|........
    <info> app:  88 89 8A 8B 8C 8D 8E 8F|........
    <info> app:  90 91 92 93 94 95 96 97|........
    <info> app:  98 99 9A 9B 9C 9D 9E 9F|........
    <info> app:  A0 A1 A2 A3 A4 A5 A6 A7|........
    <info> app:  A8 A9 AA AB AC AD AE AF|........
    <info> app:  B0 B1 B2 B3 B4 B5 B6 B7|........
    <info> app:  B8 B9 BA BB BC BD BE BF|........
    <info> app:  C0 C1 C2 C3 C4 C5 C6 C7|........

  • Hi Vidar,

    Are you using RTT or UART

    I am using RTT

    Indeed, I used Putty and I do not lose logs

    Can you try to make  'read_raw_data' "static" instead

    I did the  'read_raw_data' "static"  but there is no effect on RTT backend

  • So you are not losing logs if you use Putty, or did you meant to say that you are still losing logs?

  • No I don't losing data thanks. I will proceed with UART.

    However I was wondering whyI lose data with RTT... but if this is complicated to investigate nevermind :)

  • Ok, that's good to know, thanks for confirming. But were you using Putty with RTT or UART?  I guess it has something to do with the Jlink RTT settings. I will try to look into it. Maybe it would help to increase the RTT buffers.

Reply Children
  • Increasing the size of the RTT buffers seem to do the trick:

  • xmm interesting, I miss something because it's still don't work for me. I don't know if this is related, but I read the data from a .hex file and then I fill the read_raw_data buffer. Lastly I print the logs using the  NRF_LOG_HEXDUMP_INFO.

    This is the complete function

    FRESULT ff_result;
    #define read_payload 100
      static char read_raw_data[read_payload] = {0};
      UINT br = 0;
    
      // NA - Open the file to write
      ff_result = f_open(&file, "RAWDATA.hex", FA_READ);
      if (ff_result != FR_OK) {
        NRF_LOG_INFO("Unable to open file: %u", ff_result);
        NRF_LOG_FLUSH();
        return;
      }
    
      ff_result = f_read(&file, read_raw_data, read_payload, &br);
      if (ff_result != FR_OK) {
        NRF_LOG_INFO("\r\nUnable to read the file: %u", ff_result);
        NRF_LOG_FLUSH();
        return;
      }
    
      // NA - Close the file
      ff_result = f_close(&file); // NA - The f_close function closes an open file. Ref: http://elm-chan.org/fsw/ff/doc/close.html
      if (ff_result != FR_OK) {
        NRF_LOG_ERROR("\r\nUnable to close file: %u", ff_result);
        NRF_LOG_FLUSH();
        return;
      }
      NRF_LOG_INFO("Close the file");
    
      // Print the data
      NRF_LOG_HEXDUMP_INFO(read_raw_data,read_payload);
      NRF_LOG_FLUSH();
    
    }

    #define NRF_LOG_BACKEND_RTT_ENABLED 1
    
    #define NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE 64
    
    #define NRF_LOG_BACKEND_UART_ENABLED 0
    
    #define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 660
    
    #define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 14
    
    #define NRF_LOG_MSGPOOL_ELEMENT_SIZE 20
    
    #define NRF_LOG_MSGPOOL_ELEMENT_COUNT 40

    With the current settings I receive the following

    I also tried your example, however with the same results.. So I suppose that the it's not something wrond with my function.

  • Hi,

    Please try to repeat the test with nRF5 SDK 17.0.2, if you have time. I tried to repeat the test now with SKD 16.0.0, but the message got capped at 167 bytes for some reason. I don't remember if I used SDK 16 or 17 when it worked ( I thought it was with SDK 16 but not sure anymore)

Related