Hot!PIC32MZ2018EFM064 : snprintf problem

Author
Emcy
Super Member
  • Total Posts : 566
  • Reward points : 0
  • Joined: 2008/01/09 03:37:06
  • Location: 0
  • Status: online
2018/10/29 09:17:24 (permalink)
0

PIC32MZ2018EFM064 : snprintf problem

Hello,


I have a little project where sprintf and snprintf functions don't operate (printf operates).

https://www.sendspace.com/file/vs8kz0

=> to see directory : Demo\PIC32MZ_MPLAB


How can I do  ?


my code :



    char dgb_buf[30];
    snprintf(dgb_buf, sizeof(dgb_buf), "%u", 123);
    printf("dgb_buf1 : %s\r\n", dgb_buf);
    snprintf(dgb_buf, sizeof(dgb_buf), "%f", 1.23);
    printf("dgb_buf2 : %s\r\n", dgb_buf);
    snprintf(dgb_buf, sizeof(dgb_buf), "%s", "yolo");
    printf("dgb_buf3 : %s\r\n", dgb_buf);
    
    printf("dgb_buf1b : %u\r\n", 123);
    printf("dgb_buf2b : %f\r\n", 1.23);
    
    sprintf(dgb_buf, "%u", 123);
    printf("dgb_buf1c : %s\r\n", dgb_buf);
    sprintf(dgb_buf, "%f", 1.23);
    printf("dgb_buf2c : %s\r\n", dgb_buf);
    sprintf(dgb_buf, "%s", "yolo");
    printf("dgb_buf3c : %s\r\n", dgb_buf);  




It is displayed :



dgb_buf1 :
dgb_buf2 :
dgb_buf3 : yolo
dgb_buf1b : 123
dgb_buf2b : 1.230000
dgb_buf1c :
dgb_buf2c :
dgb_buf3c : yolo









#1

19 Replies Related Threads

    jg_ee
    Super Member
    • Total Posts : 142
    • Reward points : 0
    • Joined: 2015/04/30 10:54:52
    • Location: Colorado
    • Status: offline
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 09:29:35 (permalink)
    0
    Are you using legacy libc in the compiler settings?  The other one has sprintf problems.
    #2
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 09:30:54 (permalink)
    0
    yes, I tried with and without : I have the same results
    #3
    jg_ee
    Super Member
    • Total Posts : 142
    • Reward points : 0
    • Joined: 2015/04/30 10:54:52
    • Location: Colorado
    • Status: offline
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 10:02:19 (permalink)
    0
    Something with how your project settings are configured.  I am able to reproduce it in the simulator with your project, but if I make a standalone project the function behaves correctly.
    #4
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 10:33:35 (permalink)
    0
    Yes but what the configuration problem  ? T_T
    Heap and stack size are 0 but when I change them, I have the same problem
    #5
    Jim Nickerson
    User 452
    • Total Posts : 5842
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 10:42:37 (permalink)
    0
    I wonder what happens if

     
    // you change sizeof(dgb_buf) to the absolute number 30
     

    #6
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 10:57:32 (permalink)
    0
    JANickerson
    I wonder what happens if

    // you change sizeof(dgb_buf) to the absolute number 30




    No change... I don't think problem come from C code.
    #7
    jg_ee
    Super Member
    • Total Posts : 142
    • Reward points : 0
    • Joined: 2015/04/30 10:54:52
    • Location: Colorado
    • Status: offline
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 11:41:40 (permalink)
    0
    Emcy
    Yes but what the configuration problem  ? T_T
    Heap and stack size are 0 but when I change them, I have the same problem



    I could not figure it out.
    #8
    davekw7x
    Entropy++
    • Total Posts : 1692
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/29 16:01:41 (permalink)
    4 (1)
    Well...
    Over the years I have had a devil of a time keeping up with XC32 changes, particularly with printing floats/doubles/long doubles and printing 64-bit integers.  Sometimes legacy libc worked but not the non-legacy.  Sometimes the other way around.  Trig functions in earlier versions had problems with short-doubles (or maybe it was with no-short-doubles; I forget which).  Sometimes things were fixed in later versions, but the results were that other things were unfixed.  (Like playing Whac-a-Mole!)
     
    Latest tests with XC32 version 2.10 were the most promising yet.

    Anyhow...
    I made up a little test program that does a few print tests and not much else.  I run it when I get a new version of XC32 or libraries or whatever.

    I pasted your statements into such a program that I currently run on my Curiosity PIC32MZEF board (has a PIC32MZ2048EFM100).

    For this test version I have four configurations

    XC32 version 2.10 with legacy libc
    XC32 version 2.10 with non-legacy libc
    XC32 version 1.42 with legacy libc
    XC32 version 1.42 with non-legcy libc.

    Bottom line: Your print statements gave expected output for all of these combinations.  Note that other compiler settings were default: No special settings for stack or heap.  Use "standard" XC32 doubles (32-bits), etc.

    Output from one run:

    Compiled on Oct 29 2018 at 15:38:51 PDT by XC32 version 2100
    sizeof(double) = 4
    Not using Legacy libc
    dgb_buf1 : 123
    dgb_buf2 : 1.230000
    dgb_buf3 : yolo
    dgb_buf1b : 123
    dgb_buf2b : 1.230000
    dgb_buf1c : 123
    dgb_buf2c : 1.230000
    dgb_buf3c : yolo


    Results from all of my configurations are in the Outputs.txt attachment. (Identical outputs for everything I tested.)

    Project package is in the .zip attachment

    Bottom line: For debugging specific functionality, I separate it out from all of the other stuff.  That helps me to eliminate certain things and focus on solving the real problem.

    Regards,
     
    Dave


    post edited by davekw7x - 2018/10/29 16:02:56

    Sometimes I just can't help myself...
    #9
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/30 03:18:50 (permalink)
    0
    davekw7x
    Well...
    Over the years I have had a devil of a time keeping up with XC32 changes, particularly with printing floats/doubles/long doubles and printing 64-bit integers.  Sometimes legacy libc worked but not the non-legacy.  Sometimes the other way around.  Trig functions in earlier versions had problems with short-doubles (or maybe it was with no-short-doubles; I forget which).  Sometimes things were fixed in later versions, but the results were that other things were unfixed.  (Like playing Whac-a-Mole!)
     
    Latest tests with XC32 version 2.10 were the most promising yet.

    Anyhow...
    I made up a little test program that does a few print tests and not much else.  I run it when I get a new version of XC32 or libraries or whatever.

    I pasted your statements into such a program that I currently run on my Curiosity PIC32MZEF board (has a PIC32MZ2048EFM100).

    For this test version I have four configurations

    XC32 version 2.10 with legacy libc
    XC32 version 2.10 with non-legacy libc
    XC32 version 1.42 with legacy libc
    XC32 version 1.42 with non-legcy libc.

    Bottom line: Your print statements gave expected output for all of these combinations.  Note that other compiler settings were default: No special settings for stack or heap.  Use "standard" XC32 doubles (32-bits), etc.

    Output from one run:

    Compiled on Oct 29 2018 at 15:38:51 PDT by XC32 version 2100
    sizeof(double) = 4
    Not using Legacy libc
    dgb_buf1 : 123
    dgb_buf2 : 1.230000
    dgb_buf3 : yolo
    dgb_buf1b : 123
    dgb_buf2b : 1.230000
    dgb_buf1c : 123
    dgb_buf2c : 1.230000
    dgb_buf3c : yolo


    Results from all of my configurations are in the Outputs.txt attachment. (Identical outputs for everything I tested.)

    Project package is in the .zip attachment

    Bottom line: For debugging specific functionality, I separate it out from all of the other stuff.  That helps me to eliminate certain things and focus on solving the real problem.

    Regards,
     
    Dave





    I thank you but I don't find a difference with IDE configuration between my projet and your project.
    The only one difference that I see it is additional options x32-asm : -I../../../Source/portable/MPLAB/PIC32MZ -I../ -DconfigUSE_TASK_FPU_SUPPORT=1
    => I must leave it to compile my project.
     
     
    #10
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/30 03:33:05 (permalink)
    0
    ... perhaps FreeRTOS code change snprintf ?
    #11
    davekw7x
    Entropy++
    • Total Posts : 1692
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/30 10:07:19 (permalink)
    5 (2)
    Emcy
    ...I don't find a difference with IDE configuration between my projet and your project.



    To eliminate the possibility of some quirk of the particular device type or of XC32's handling of the functions in question, here's what I did:
     
    I changed the chip type in your project to the one on my board and compiled and executed the "TEST" configuration.  I saw the same results that you reported.
     
    Bottom line: Since the sprintf/snprintf works OK on my barefoot test program but fails in the FreeRTOS project, I'm thinking it may have something to do with preserving floating point context when task switching.
    I don't use FreeRTOS, so I (probably) won't be able to follow up beyond this point.

    Regards,
     
    Dave
    post edited by davekw7x - 2018/10/30 10:16:10

    Sometimes I just can't help myself...
    #12
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/31 01:02:31 (permalink)
    0
    I thank you for your help :)
     
    I don't think problem comme from preserving floating context because I have the same problem if I don't create task in my project (I set a while(1); before call of main_full()).
     
    Also if I replace heap_4.c by heap_3.c (for freeRTOS memory management) and define heap and stack size, I have the same problem.
     
    I can fix the problem with snprintf.c/h (attached files) but I must include snprintf.h in all files which use snprintf/sprintf
    => Is it possible to overwrite snprintf/sprintf function without include a specific .h file ?
    post edited by Emcy - 2018/10/31 09:10:29
    #13
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/10/31 09:08:39 (permalink)
    0
    .... for %f, results aren't equal between custom snprintf and default printf function (to see test_snprintf function)
    #14
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/11/12 01:20:17 (permalink)
    0
    A person of FreeRTOS forum says me to verify if stack pointer is 8-byte aligned when I call snprintf function.
    Problem is I have this error message :
    Programming/Verify complete
    The target device is not ready for debugging. Please check your configuration bit settings and program the device before proceeding. The most common causes for this failure are oscillator and/or PGC/PGD settings.

    I see in Microchip forum, it is a problem of configuration bits but I don't find how to fix this problem.
    #15
    qhb
    Superb Member
    • Total Posts : 9667
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/11/12 01:22:36 (permalink)
    5 (2)
    Emcy
    ...
    I see in Microchip forum, it is a problem of configuration bits but I don't find how to fix this problem.

    Wouldn't it make sense to show us your configuration bits then?
     
    #16
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/11/12 03:05:52 (permalink)
    0
     

        #pragma config FMIIEN = OFF, FETHIO = ON, PGL1WAY = ON, PMDL1WAY = ON, IOL1WAY = ON, FUSBIDIO = OFF
        #pragma config FNOSC = SPLL, FSOSCEN = OFF, IESO = OFF, POSCMOD = OFF
        #pragma config OSCIOFNC = OFF, FCKSM = CSECME, FWDTEN = OFF, FDMTEN = OFF
        #pragma config DMTINTV = WIN_127_128, WDTSPGM = STOP, WINDIS = NORMAL
        #pragma config WDTPS = PS1048576, FWDTWINSZ = WINSZ_25, DMTCNT = DMT31
        #pragma config FPLLIDIV = DIV_1, FPLLRNG = RANGE_5_10_MHZ, FPLLICLK = PLL_FRC
        #pragma config FPLLMULT = MUL_50, FPLLODIV = DIV_2, UPLLFSEL = FREQ_24MHZ
        #pragma config EJTAGBEN = NORMAL, DBGPER = PG_ALL, FSLEEP = OFF, FECCCON = OFF_UNLOCKED
        #pragma config BOOTISA = MIPS32, TRCEN = OFF, ICESEL = ICS_PGx1, JTAGEN = OFF

        #pragma config DEBUG = OFF
        // same result with : #pragma config DEBUG = ON
     
        #pragma config CP = OFF  

        #pragma config USERID = 0xFFFF
        #pragma config SMCLR = MCLR_NORM
        #pragma config SOSCGAIN = GAIN_2X
        #pragma config SOSCBOOST = ON
        #pragma config POSCGAIN = GAIN_2X
        #pragma config POSCBOOST = ON

    #17
    qhb
    Superb Member
    • Total Posts : 9667
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/11/12 03:18:00 (permalink)
    4.5 (2)
        #pragma config DEBUG = OFF
        // same result with : #pragma config DEBUG = ON

    Get rid of that line altogether.
    i.e. don't try to set it OFF or ON

    This is the line that matters:

        #pragma config BOOTISA = MIPS32, TRCEN = OFF, ICESEL = ICS_PGx1, JTAGEN = OFF

    ICESEL = ICS_PGx1

    tells it to use PGED1 and PGEC1 as the debugging pins.
    #18
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/11/12 04:13:52 (permalink)
    0
    I thank you, it operates :)
     
    I must check :

    Can you check that your stack is 8-byte aligned. You can do that by
    putting break point on the call to the print formatting function and
    checking the stack pointer value in the debugger. If it is not 8-byte
    aligned then try placing a break point on the opening bracket of a
    function that implements a task entry point to see if it is aligned there.

    I must set a break point on snprintf call and very value of PC (I'm not sure) ?
    => PC value is 0x9D00CDDC
    post edited by Emcy - 2018/11/13 00:40:29
    #19
    Emcy
    Super Member
    • Total Posts : 566
    • Reward points : 0
    • Joined: 2008/01/09 03:37:06
    • Location: 0
    • Status: online
    Re: PIC32MZ2018EFM064 : snprintf problem 2018/12/04 08:31:58 (permalink)
    5 (1)
    Microchip support found the solution (snprintf.h/.c are useless) : problem come from fputc() in debug.c, it must replace this function by :
    void attribute((externally_visible)) _mon_putc (char c){
        //Wait for the transmitter to be ready
        while(U2STA & _U2STA_UTXBF_MASK);
        //Send character
       U2TXREG = c;
        //Wait for the transfer to complete
       while(!(U2STA & _U2STA_TRMT_MASK));
    }

    #20
    Jump to:
    © 2019 APG vNext Commercial Version 4.5