Part Number:MSPM0L1306
我在基于MSPM0L1306芯片上开发UART代码,想使用重载fputc来最终支持将printf函数重定向到UART输出。目前做了如下尝试:
1、如附件所示,编辑了C代码,在基于Keil构建的MSPM0L1306工程,目前测试是可以实现将printf函数重定向到UART输出的。
2、但在用CCS12.5下,使用同样的C代码,编译下载运行,所有的printf是不起作用的。
想咨询如何修改CCS工程配置或者修改C代码,达到支持printf函数重定向到UART上?
uart_loopback.c
/* * Copyright (c) 2023, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright *notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright *notice, this list of conditions and the following disclaimer in the *documentation and/or other materials provided with the distribution. * * * Neither the name of Texas Instruments Incorporated nor the names of *its contributors may be used to endorse or promote products derived *from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // To Using Chinese correctly on UART, please use GBK encoding. #include "ti_msp_dl_config.h" #include "string.h" #include "stdio.h" #define LineLength 64 /* This results in approximately 10ms of delay assuming 32MHz CPU_CLK */ #define DELAY (320000) #if 1 //#pragma import(__use_no_semihosting) //������Ҫ��֧�ֺ��� //struct __FILE //{ // int handle; //}; FILE __stdout; //����_sys_exit()�Ա���ʹ�ð�����ģʽ void _sys_exit(int x) { x = x; } //�ض���fputc���� int fputc(int ch, FILE *f) { DL_UART_Main_transmitDataBlocking(UART_0_INST, ch); return ch; } #endif // UART Received Buffer, Max Length = LineLength volatile uint8_t LineBuffer[LineLength]; // Flag for the UART Received Status // Bit15 - Finished for Received One Line // Bit14 - Got '\r' Flag // Bit13 ~ Bit0 - The Length of the Received Char volatile uint16_t UART_RX_STA = 0; int main(void) {unsigned short times = 0;unsigned short len;unsigned short n;/* Power on GPIO, initialize pins as digital outputs */SYSCFG_DL_init();/* Default: LED1 OFF */DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN );NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);//Clear the IRQ FlagNVIC_EnableIRQ(UART_0_INST_INT_IRQN);//Enable the Received Interruptwhile (1) {if(UART_RX_STA & 0x8000)//check the received finish flag{len = UART_RX_STA & 0x3FFF; //Got the Length of the receivedprintf("\r\n�����͵���ϢΪ:\r\n");//Send the received Linefor(n=0; n<len; n++)DL_UART_Main_transmitDataBlocking(UART_0_INST, LineBuffer[n]);printf("\r\n\r\n");UART_RX_STA = 0;//Clear the Flag of the Received}else {times ++;if(times % 5000 == 0)//Per 50S{printf("\r\nMSPM0+ UART����ʵ��\r\n");printf("HardwareLab@���пƴ�\r\n");}if(times %200 == 0) printf("���������ݣ��Իس�����: \r\n"); //Per 2Sif(times % 30 == 0) DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN ); //Per 300msdelay_cycles(DELAY);}} } volatile uint8_t gRxData = 0; void UART_0_INST_IRQHandler(void) {switch (DL_UART_Main_getPendingInterrupt(UART_0_INST)) {case DL_UART_MAIN_IIDX_RX:gRxData = DL_UART_Main_receiveData(UART_0_INST);if((UART_RX_STA & 0x8000)==0) //Receive Not Finished{if(UART_RX_STA&0x4000) //Received 0x0D('\r'){if(gRxData!=0x0a)UART_RX_STA=0;//Received Errorelse UART_RX_STA|=0x8000; //Received Finished}else //Not Got 0x0D('\r'){if(gRxData==0x0d)UART_RX_STA|=0x4000;else{LineBuffer[UART_RX_STA&0X3FFF]=gRxData ;// Got One Valid CharUART_RX_STA++;if(UART_RX_STA>(LineLength-1))UART_RX_STA=0;//Received Error, Restart Again}}}break;default:break;} }
Helic Chi:
Hi, June
Please try this repository on GitHub:
GitHub – mpaland/printf: Tiny, fast, non-dependent and fully loaded printf implementation for embedded systems. Extensive test suite passing.
Just add two files to project: printf.c and printf.h
And then follow the Usage: https://www.ti2k.com/wp-content/uploads/ti2k/DeyiSupport_OtherMCU_printf
Regards,
Helic
,
June:
我使用了提供的printf开源项目,测试已经ok。但我的问题是原工程在keil构建的工程编译是ok的,这么说keil自己内部做了libc的修改(为了适应嵌入式),还是说CCS限制了stdio函数(因为安全问题)。或者说keil工程直接使用libc的printf可能存在风险?