Iprintf adding parameters causes issues.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
D_Sd_3235936
Level 4
Level 4
25 sign-ins 50 questions asked 25 replies posted

I am using the compact iprintf function created by you guys.

The function looks like this :

void iprintf(char8 *pszFmt,...)

{..

}

I wanted to add some more parameters to the function so I could use it for both uart and flash.

But when adding the new parameters, the variables i am adding to %d start to get distorted on UART print.

e.g

void iprintf(uint8 flag1, uint8 flag2, char8 *pszFmt,...)

{..

}

when i do :

uint32 variable =100;

iprintf(flag1,flag2, "some string : %d \r\n", varibale);

the value of the variable is printed wrong.

0 Likes
1 Solution
GeonaP_26
Moderator
Moderator
Moderator
250 solutions authored 100 solutions authored 50 solutions authored

Make sure that the heap size required to support iprintf is set in your project (.cydwr > System > Heap Size). The default value will not be enough to support these functions. You can change it to a higher value.

Please refer to https://iotexpert.com/2017/05/19/implementing-psoc-printf-part-deux/ for more details.  Updated

View solution in original post

3 Replies
GeonaP_26
Moderator
Moderator
Moderator
250 solutions authored 100 solutions authored 50 solutions authored

Make sure that the heap size required to support iprintf is set in your project (.cydwr > System > Heap Size). The default value will not be enough to support these functions. You can change it to a higher value.

Please refer to https://iotexpert.com/2017/05/19/implementing-psoc-printf-part-deux/ for more details.  Updated

How will it affect the system, anything it can harm?

Any thumb rule on how much should i increase it? It is currently on 0x800 bytes

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

> I am using the compact iprintf function created by you guys.

Are you talking about the "iprintf" described in the following URL?

Alternate printf Function for PSoC® 4 - KBA87093

Just like you might have done, I down loaded iprintf.zip

and modified the iprintf.h and iprintf.c as below and it seems to be working.

(I set heap to 0x400)

000-TeraTerm-log.JPG

But I noticed that when I added #include "iprintf.h", the compiler failed as there are multiple define(s) of iprintf().

I suppose that in newlib-nano, there is an iprintf() already defined.

In my case as iprintf.c and iprintf.h was included in the project, it was linked before the newlib-nano's one.

IMHO, this is not a recommendable situation, I'd rather recommend you to modify the iprint.h and iprintf.c to create something like my_printf() so that there won't be a conflict.

[ iprintf.h ]

========================

/******************************************************************************

*This file is for iprintf()

*******************************************************************************/

#ifndef IPRINTF_H

#define IPRINTF_H

#include <project.h>

void iprintf(uint8 flag1, uint8 flag2, char8 *pszFmt,...);

#endif

/* [] END OF FILE */

========================

[ iprintf.c ]

========================

/******************************************************************************

*This file is for iprintf()

*The iprintf() is a simple printf() and only can print string with %s,%d,%c,%x.

*******************************************************************************/

#include "iprintf.h"

static void iputc(char8 ch)

{

/*This function has to be replaced by user*/

  UART_UartPutChar(ch); // <---  Modified for my CY8CKIT-044

}

static uint8_t *change(uint32 Index)

{

    return (uint8_t *)("0123456789abcdef"+Index);

}

void iprintf(uint8 flag1, uint8 flag2, char8 *pszFmt,...)

{

    uint8 *pszVal;

    uint32 iVal, xVal, i = 0, buffer[12], index = 1;

    uint8 cVal;

    uint32 *pArg;

    pArg =(uint32 *)&pszFmt;

  

    char str[32] ;

  

    sprintf(str, "flag1 = %d flag2 = %d\n", flag1, flag2) ; // <--- Added

    UART_UartPutString(str) ; // <--- Added

    while(*pszFmt)

    {

        if('%' != *pszFmt)

        {

            iputc(*pszFmt);

            pszFmt++;

            continue;

        }

        pszFmt++;

        if(*pszFmt == 's')

        {

            pszVal = (uint8*)pArg[index++];

            for(; *pszVal != '\0'; pszVal++)

                iputc(*pszVal);

            pszFmt++;

            continue;

        }

        if(*pszFmt == 'd')

        {

            iVal = pArg[index++];

            i = 0;

            do{

                buffer[i++] = iVal % 10;

                iVal /= 10;

            }while(iVal);

            while(i > 0)

            {

                i--;

                iputc(*change(buffer));

            }

            pszFmt++;

            continue;

        }

        if(*pszFmt == 'c')

        {

            cVal = (uint8)pArg[index++];

            iputc(cVal);

            pszFmt++;

            continue;

        }

        if(*pszFmt == 'x')

        {

            xVal = pArg[index++];

            i = 0;

            do{

                buffer[i++] = xVal % 16;

                xVal /= 16;

            }while(xVal);

            if(i%2!=0)

                buffer[i++]=0;

            if(i<2)

                buffer[i++]=0;

            while(i > 0)

            {

                i--;

                iputc(*change(buffer));

            }

            pszFmt++;

            continue;

        }

        if(pszFmt == '\0')

        {

            break;

        }

    }

}

========================

[ main.c ]

========================

#include "project.h"

#include "stdio.h"

#if 0 /* _write required for iprintf in newlib-nano */

int _write(int file, char *ptr, int len)

{

    int i ;

  

    file = file ;

    for (i = 0; i < len ; i++ ) {

        UART_UartPutChar(*ptr++) ;

    }

    return len ;

}

#endif

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

}

int main(void)

{

    init_hardware() ;

  

    iprintf(12, 25, "Hello World %s %s\n", __DATE__, __TIME__) ;

  

    for(;;)

    {

        /* Place your application code here. */

    }

}

/* [] END OF FILE */

========================

moto

0 Likes