1:
通过retarget-io实现printf的功能:
可以参考历程:
mtb-example-psoc4-safety-uart-loopback-test-master.zip
(29.45 KB)
2:如果不用retarget-io,可以参考下面基于GCC编译器的实现代码。
/******************************************************************************
* File Name: main.c
*
* Description: This code avoids the usage of retarget-io and prints UART
* messages using printf() function.
*
******************************************************************************/
#include "cy_pdl.h"
#include "cybsp.h"
cy_stc_scb_uart_context_t CYBSP_DEBUG_UART_context;
void handle_error(void)
{
/* Disable all interrupts. */
__disable_irq();
CY_ASSERT(0);
}
int main(void)
{
cy_rslt_t result;
cy_en_scb_uart_status_t initstatus;
uint8_t read_data; /* Variable to store the received character
* through terminal */
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
handle_error();
}
/* Enable global interrupts */
__enable_irq();
/* Initialize the UART */
initstatus = Cy_SCB_UART_Init(CYBSP_DEBUG_UART_HW, &CYBSP_DEBUG_UART_config, &CYBSP_DEBUG_UART_context);
/* Initialization failed. Handle error */
if(initstatus!=CY_SCB_UART_SUCCESS)
{
handle_error();
}
/* Enable UART */
Cy_SCB_UART_Enable(CYBSP_DEBUG_UART_HW);
/* Transmit Header to the Terminal */
printf("\x1b[2J\x1b[;H");
printf("***********************************************************\r\n");
printf("PSoC MCU UART Transmit and Receive\r\n");
printf("***********************************************************\r\n\n");
printf(">> Start typing to see the echo on the screen \r\n\n");
for (;;)
{
/* Check if there is a received character from user console */
if (0UL != Cy_SCB_UART_GetNumInRxFifo(CYBSP_DEBUG_UART_HW))
{
/* Re-transmit whatever the user types on the console */
read_data = Cy_SCB_UART_Get(CYBSP_DEBUG_UART_HW);
printf("%c\r\n",read_data);
}
}
}
/*******************************************************************************
* When printf function is called it is redirected to the following functions
* depending on compiler used.
*******************************************************************************/
#if defined (__GNUC__)
/*******************************************************************************
* Function Name: _write
********************************************************************************
* Summary:
* NewLib C library is used to retarget printf to _write. printf is redirected to
* this function when GCC compiler is used to print data to terminal using UART.
*
* \param file
* This variable is not used.
* \param *ptr
* Pointer to the data which will be transfered through UART.
* \param len
* Length of the data to be transfered through UART.
*
* \return
* returns the number of characters transferred using UART.
* \ref int
*******************************************************************************/
int _write(int file, char *ptr, int len)
{
/* Suppress the compiler warning about an unused variable. */
if (0 != file)
{
}
for(uint8_t i = 0; i
通过retarget-io的方法消耗的Flash/RAM空间要大一点,所以可以结合自己的需要选择适合的方法。
|