This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LP-MSPM0L2228: CANNOT use printf() to print variable by using CCS 20.0.1.4__1.6.1

Part Number: LP-MSPM0L2228
Other Parts Discussed in Thread: LP-MSPM0G3507

Tool/software:

Hi sir,

I tried to use printf() to print variable but it seems be hang, I have tried to add heap size but still got the same issue. Is there any possible way to solve it?

The code is to print received character from UART.

printf("got %x\n", recvChar);

My target board is LP-MSPM0L2228.

Thanks,

Edware

  • Hi Helic,

    It works to me. But I have a question about the output interface. the default output of printf() is UART0, may I know how to change it to CIO?

    Thanks,

    Edware

  • Hi, 

    For the UART interface change, please refer to demo's comments.

     * @Tips:
     * You can modify the hardware of MSPM0 in printf.c file, Line 132:
     * Function: static inline void _putchar(const char _c).
     * You can use the macro definitions in printf.h to turn off
     * complex printf functions such as float point numbers,
     * longlong type variables, to reduce the program size.

    Regards,

    Helic

  • Hi Helic,

    Thank you. I knew I could modify _putchar() function.

    static inline void _putchar(const char _c) {

        /* Wait until TX FIFO is transmitted and the TX FIFO is not full */
        while (DL_UART_isTXFIFOFull(UART_0_INST));
        DL_UART_transmitData(UART_0_INST, _c);
    }

    But I don't know the instance for CIO. Is there any example to output messages to CIO?

    Thanks,

    Edware

  • Hi, 

    You can find some related code in ticlang such as fputs.c shown below:
    This part function do the CIO output job to CCS console.

    /*****************************************************************************/
    /* FPUTS -  Write a string to a stream                                       */
    /*                                                                           */
    /*    This function writes string _PTR to stream _FP, returning the number   */
    /*    of characters written upon success, or an EOF upon failure.            */
    /*                                                                           */
    /*****************************************************************************/
    _CODE_ACCESS int fputs(const char * __restrict _ptr, FILE * __restrict _fp)
    {
       /*------------------------------------------------------------------------*/
       /* Local variables                                                        */
       /*------------------------------------------------------------------------*/
       size_t   num_left, ptr_strlen;
       char     *fpos          = (char *)_ptr;
       int      room_left,
                flush_flag     = 0,
                num_to_write;
    
       /*------------------------------------------------------------------------*/
       /* The current thread in a multi-threaded application must protect access */
       /* to the file stream. In this case, _fp may be updated, so we must       */
       /* ensure that the local copy of _fp is flushed to shared memory before   */
       /* leaving the critical section (invalidated if it is not modified).      */
       /*------------------------------------------------------------------------*/
       __TI_file_lock(_fp);
    
       /*------------------------------------------------------------------------*/
       /* Make sure that the stream is writeable.                                */
       /*------------------------------------------------------------------------*/
       if (!__TI_wrt_ok(_fp)) 
       { 
          __TI_data_synch_INV(_fp, sizeof(FILE));
          __TI_file_unlock(_fp);
          return (EOF);
       }
     
       room_left = (int)(_fp->bufend - _fp->pos);
       ptr_strlen = num_left = strlen(_ptr);
    
       /*------------------------------------------------------------------------*/
       /* If the stream is non-buffered, call the lowlevel WRITE function.       */
       /*------------------------------------------------------------------------*/
       if (_BUFFMODE(_fp) == _IONBF) 
       {
           int num_written = 0;
    
           while (num_left > 0)
           {
    	   int write_return = write(_fp->fd, _ptr + num_written, num_left);
    	   if (write_return < 0) 
    	   { 
    	       _SET(_fp, _STATERR); 
                   __TI_data_synch_WBINV(_fp, sizeof(FILE));
                   __TI_file_unlock(_fp);
    	       return (EOF);
    	   }
    	   else
    	   {
    	       num_written += write_return;
    	       num_left    -= write_return;
    	   }
           }
    
           __TI_data_synch_WBINV(_fp, sizeof(FILE));
           __TI_file_unlock(_fp);
           return ptr_strlen;
       }
     
       /*------------------------------------------------------------------------*/
       /* Write the string into the buffer, flushing it when full.               */
       /*------------------------------------------------------------------------*/
       while (num_left > 0)
       {
          num_to_write = (num_left > room_left) ? room_left : num_left;
          if ((_BUFFMODE(_fp) == _IOLBF) && memchr(fpos, '\n', num_to_write))
          { 
             num_to_write = (char *)memchr(fpos, '\n', num_to_write) - fpos + 1;
             flush_flag = 1;
          }
          memcpy(_fp->pos, fpos, num_to_write);
    
          /*---------------------------------------------------------------------*/
          /* Update pointers and counters.                                       */
          /*---------------------------------------------------------------------*/
          _fp->pos  += num_to_write;
          fpos      += num_to_write;
          num_left  -= num_to_write;
          room_left -= num_to_write;
    
          /*---------------------------------------------------------------------*/
          /* If the buffer is full, flush it.  Any I/O errors cause this         */
          /* function to exit, returning an EOF.                                 */
          /*---------------------------------------------------------------------*/
          if (room_left == 0 || flush_flag)
          {
             if (__TI_doflush(_fp))
             {
                _SET(_fp, _STATERR);
                __TI_data_synch_WBINV(_fp, sizeof(FILE));
                __TI_file_unlock(_fp);
                return (EOF);
             }
             room_left = (int)(_fp->bufend - _fp->pos);
             _SET(_fp, _MODEW);
             flush_flag = 0;
          }
       }
    
       __TI_data_synch_WBINV(_fp, sizeof(FILE));
       __TI_file_unlock(_fp);
       return ptr_strlen;
    }

    But I never try to merge tinyprintf with CIO.

    Regards,

    Helic

  • Hi, Edware

    printf not test on LP-MSPM0L2228, but printf works well on LP-MSPM0G3507.

    On G3507, printf will print the debug log to CCS console windows while in debug.

    You can have a try on L2228.

    Also, you can use this demo code from github:

    printf_to_uart_LP_MSPM0G3507_nortos_ticlang.zip

    This uses old version SDK and syscfg, need to switch to latest version, also need to change from G3507 to L2228.

    Regards,

    Helic