how to use the printf function in S32DS.3.5 ?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to use the printf function in S32DS.3.5 ?

Jump to solution
2,635 Views
Gama
Contributor II

Hi nxp community, 

I want to use the printf funcrtion in S32S.3.5 but i con't see results "hello world" at the console or terminal. so far i can not see results whenever i run the code. is there a special configuration required to use this function ? i am using the GBD SEGGER J-Link Debugging line and SWD. 

Gama_0-1694594498623.png

#S32DS #printf @jiri_kral

 

 

Tags (1)
0 Kudos
Reply
1 Solution
2,626 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

easiest way is enable semihosting console. Anyway, what's your target device and debugger? 

jiri_kral_0-1694598302706.png

 

jiri_kral_1-1694598603541.png

 

jiri_kral_2-1694598849018.png

 

View solution in original post

6 Replies
2,520 Views
Gama
Contributor II

@jiri_kral sorry for so many questions, I'm really new to the S32D IDE. 

I want to try using the UART port instead since for the printf, because I want to send messages over UART for my project. I have a USB serial on my board, I want to try the [S32K1 AUTOSAR 4.4 RTD 1.0.1 D2202 Example Projects] example name is [Uart_Example_S32K144]. But so far I have some issues with the Port_ci_Port_lp_Cfg files. I can't change the PIN based on my board. I want to use Pin PTC6 as RX and Pin PTC7 as Tx. But I'm having problems as shown below..How can I change the pins? or what am I missing? 

Which of the following headers can I use to configure the Pins 

/* User includes */
#include "Mcl.h"
#include "Mcu.h"
#include "Uart.h"
#include "Port.h"
#include "Platform.h"
#include "Lpuart_Uart_Ip_Irq.h"
#include "Flexio_Uart_Ip_Irq.h"
#include "check_example.h"
#include <string.h>
 
Bellow Is my example main.c code. 
 

"''''''''''''''''''''''''''''''''''''''''''''''

/*
*   (c) Copyright 2021 NXP
*/
 
/* User includes */
#include "Mcl.h"
#include "Mcu.h"
#include "Uart.h"
#include "Port.h"
#include "Platform.h"
#include "Lpuart_Uart_Ip_Irq.h"
#include "Flexio_Uart_Ip_Irq.h"
#include "check_example.h"
#include <string.h>
 
#include "stdio.h"
 
#define UART_LPUART_INTERNAL_CHANNEL  0U
#define UART_FLEXIO_RX_CHANNEL  1U
#define UART_FLEXIO_TX_CHANNEL  2U
 
/* Welcome messages displayed at the console */
#define WELCOME_MSG_1 "Hello, This message is sent via Uart!\r\n"
#define WELCOME_MSG_2 "Have a nice day!\r\n"
 
/* Error message displayed at the console, in case data is received erroneously */
#define ERROR_MSG "An error occurred! The application will stop!\r\n"
 
/* Length of the message to be received from the console */
#define MSG_LEN  50U
 
boolean User_Str_Cmp(const uint8 * pBuffer1, const uint8 * pBuffer2, const uint32 length)
{
    uint32 idx = 0;
    for (idx = 0; idx < length; idx++)
    {
        if(pBuffer1[idx] != pBuffer2[idx])
        {
            return FALSE;
        }
    }
    return TRUE;
}
 
Std_ReturnType Send_Data(uint8 transChannel, uint8 recvChannel, const uint8* pBuffer, uint32 length)
{
    volatile Std_ReturnType T_Uart_Status;
    volatile Uart_StatusType Uart_ReceiveStatus = UART_STATUS_TIMEOUT;
    volatile Uart_StatusType Uart_TransmitStatus = UART_STATUS_TIMEOUT;
    uint32 T_bytesRemaining;
    uint32 T_timeout = 0xFFFFFF;
    uint8 Rx_Buffer[MSG_LEN];
 
    /* Uart_AsyncReceive transmit data */
    T_Uart_Status = Uart_AsyncReceive(recvChannel, Rx_Buffer, length);
    if (E_OK != T_Uart_Status)
    {
        return E_NOT_OK;
    }
    /* Uart_AsyncSend transmit data */
    T_Uart_Status = Uart_AsyncSend(transChannel, pBuffer, length);
    if (E_OK != T_Uart_Status)
    {
        return E_NOT_OK;
    }
 
    /* Check for no on-going transmission */
    do
    {
        Uart_TransmitStatus = Uart_GetStatus(transChannel, &T_bytesRemaining, UART_SEND);
    } while (UART_STATUS_NO_ERROR != Uart_TransmitStatus && 0 < T_timeout--);
 
    T_timeout = 0xFFFFFF;
 
    do
    {
        Uart_ReceiveStatus = Uart_GetStatus(recvChannel, &T_bytesRemaining, UART_RECEIVE);
    } while (UART_STATUS_NO_ERROR != Uart_ReceiveStatus && 0 < T_timeout--);
 
    if ((UART_STATUS_NO_ERROR != Uart_TransmitStatus) || (UART_STATUS_NO_ERROR != Uart_ReceiveStatus))
    {
        return E_NOT_OK;
    }
 
    if(User_Str_Cmp(pBuffer, (const uint8 *)Rx_Buffer, length) == FALSE)
    {
        return E_NOT_OK;
    }
 
    return E_OK;
}
 
/*!
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
 
int main(void)
{
printf("hello world! \n ");
 
    volatile Std_ReturnType T_Uart_Status1;
    volatile Std_ReturnType T_Uart_Status2;
 
    /* Initialize the Mcu driver */
    Mcu_Init(NULL_PTR);
 
    Mcu_InitClock(McuClockSettingConfig_0);
#if (MCU_NO_PLL == STD_OFF)
    while ( MCU_PLL_LOCKED != Mcu_GetPllStatus() )
    {
        /* Busy wait until the System PLL is locked */
    }
 
    Mcu_DistributePllClock();
#endif
    Mcu_SetMode(McuModeSettingConf_0);
 
    /* Initialize Mcl module */
    Mcl_Init(NULL_PTR);
 
    /* Initialize all pins using the Port driver */
    Port_Init(NULL_PTR);
 
    /* Initialize IRQs */
    Platform_Init(NULL_PTR);
    Platform_InstallIrqHandler(LPUART0_RxTx_IRQn, LPUART_UART_IP_0_IRQHandler, NULL_PTR);
    Platform_InstallIrqHandler(FLEXIO_IRQn, MCL_FLEXIO_ISR, NULL_PTR);
 
    /* Initializes an UART driver*/
    Uart_Init(NULL_PTR);
 
    /* Send greeting string 1 from Flexio_0_Tx to Lpuart_0 */
    T_Uart_Status1 = Send_Data(UART_FLEXIO_TX_CHANNEL, UART_LPUART_INTERNAL_CHANNEL, (const uint8 *)WELCOME_MSG_1, strlen(WELCOME_MSG_1));
 
    /* Send greeting string 2 from Lpuart_0 to Flexio_1_Rx */
    T_Uart_Status2 = Send_Data(UART_LPUART_INTERNAL_CHANNEL, UART_FLEXIO_RX_CHANNEL, (const uint8 *)WELCOME_MSG_2, strlen(WELCOME_MSG_2));
 
    Uart_Deinit();
    Mcl_DeInit();
 
    Exit_Example((T_Uart_Status1 == E_OK) && (T_Uart_Status2 == E_OK));
 
    return (0U);
}
 
/** @} */

 

''''''''''''''''''''''''''''''''''''''''''''

 

 

0 Kudos
Reply
2,431 Views
jiri_kral
NXP Employee
NXP Employee

Hi,

you need to implement the _write function which is called by printf at the end for example like this: 

int _write(int file, char *ptr, int len)
{
    int i;
    file = file;
    for (i = 0; i < len; i++)
    {
        UART_PutByte(*ptr++);
    }
    return len;
}

Instead UART_PutByte you can use something like UART_Send(buffer,len), but the implementation is up to you. You can re-use uart send code from an existing RTD example. 

 

Anyway, the STD C printf  is pretty complex and even for "hello word"  message it uses a lot of instructions. In my opinion - If you like to send some simple status/log messages it is better implement your own print which directly sends buffer into uart.

0 Kudos
Reply
2,627 Views
jiri_kral
NXP Employee
NXP Employee

Hi, 

easiest way is enable semihosting console. Anyway, what's your target device and debugger? 

jiri_kral_0-1694598302706.png

 

jiri_kral_1-1694598603541.png

 

jiri_kral_2-1694598849018.png

 

2,617 Views
Gama
Contributor II

My target device is S32K144 and I am using a GBD SEGGER J-Link Debugging line to SWD.

Gama_0-1694601281253.png

I have enable semihosting as you suggested but no print showing .

here 

Gama_2-1694601756886.png

after build and flash no results .

Gama_1-1694601713460.png

 

 

0 Kudos
Reply
2,609 Views
jiri_kral
NXP Employee
NXP Employee

Did you switched to semihosting console? On your screenshot is shown debugger console:

jiri_kral_0-1694602204097.png

 

0 Kudos
Reply
2,565 Views
Gama
Contributor II

I have switch to the console but no results yet.  just to let you know I'm using a JTAG 20pin connection.

Gama_0-1694660463186.png

 

0 Kudos
Reply