/****************************************************************************** * 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空间要大一点,所以可以结合自己的需要选择适合的方法。
您需要 登录 才可以下载或查看,没有账号?注册
举报
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
等级类勋章
发帖类勋章
时间类勋章
人才类勋章
120
208
6
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号