/**************************************************************************** * Copyright: (C)2018 Î人ÁèÔÆÎïÍøÖÇ¿ÆÊµÑéÊÒ www.iot-yun.com * Author: GuoWenxue QQ: 281143292 * Description: ·Ü¶·STM32v5¿ª·¢°å ´®¿Ú²Ù×÷º¯Êý½Ó¿Ú,printf¹³×Óº¯Êýfputc * * ChangeLog: * °æ±¾ºÅ ÈÕÆÚ ×÷Õß ËµÃ÷ * V1.0.0 2018.05.11 GuoWenxue ·¢²¼¸Ã°æ±¾ ****************************************************************************/ #include #include "stm32f10x_usart.h" #include "stm32v5_usart.h" static USART_TypeDef* debug_usart=USART1; /* ĬÈÏʹÓô®¿Ú1×÷Ϊµ÷ÊÔ´®¿Ú */ /* º¯Êý˵Ã÷: ÅäÖô®¿ÚGPIO¿ÚºÍʱÖÓ£» * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1 »ò USART_PORT2 * ·µ»ØÖµ: ÎÞ */ void init_usart_gpio(int which) { GPIO_InitTypeDef GPIO_InitStructure; /* ʹÄÜUSART1ËùÓÃGPIO¹Ü½ÅʱÖӺʹ®¿Ú¹¦ÄÜʱÖÓ */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); if( USART_PORT2 == which ) RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); else RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); if( USART_PORT2 == which ) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /* USART2 TXD Á¬ PA2 */ else GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /* USART1 TXD Á¬ PA9 */ GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* ¸´ÓÃÍÆÍìÊä³ö */ GPIO_Init(GPIOA, &GPIO_InitStructure); if( USART_PORT2 == which ) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; /* USART2 RXD Á¬ PA3 */ else GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; /* USART1 RXD Á¬ PA10 */ GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* ¸´Óø¡¿ÕÊäÈë */ GPIO_Init(GPIOA, &GPIO_InitStructure); } /* º¯Êý˵Ã÷: ÅäÖô®¿ÚµÄ²¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»¡¢Á÷¿ØµÈ£» * ²ÎÊý˵Ã÷: USARTxÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART1 »ò USART2 * baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200.... * ·µ»ØÖµ: ÎÞ */ void config_usart(USART_TypeDef* USARTx, uint32_t baudrate) { USART_InitTypeDef USART_InitStructure; /* Configure USART1 */ USART_InitStructure.USART_BaudRate = baudrate; //²¨ÌØÂÊ USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Êý¾Ýλ8λ USART_InitStructure.USART_StopBits = USART_StopBits_1; //ֹͣλ1λ USART_InitStructure.USART_Parity = USART_Parity_No; //ÎÞУÑéλ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ÎÞÓ²¼þÁ÷¿Ø USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½ USART_Init(USARTx, &USART_InitStructure); //ÅäÖô®¿Ú²ÎÊýº¯Êý #if 0 /* Enable USARTx Receive and Transmit interrupts */ USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); //ʹÄܽÓÊÕÖÐ¶Ï USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); //ʹÄÜ·¢ËÍ»º³å¿ÕÖÐ¶Ï #endif /* Enable the USARTx */ USART_Cmd(USARTx, ENABLE); } /* º¯Êý˵Ã÷: ͨ¹ýÖ¸¶¨´®¿Ú·¢ËÍÒ»¸ö×Ö·û * ²ÎÊý˵Ã÷: usartÖ¸¶¨Äĸö´®¿Ú, USART1 »ò USART2 * ch ÊÇÒª·¢Ë͵Ä×Ö·û * ·µ»ØÖµ: ÎÞ */ __inline void usart_putchar(USART_TypeDef* usart, uint8_t ch) { USART_SendData(usart, ch); while (USART_GetFlagStatus(usart, USART_FLAG_TC) == RESET) ; } /* º¯Êý˵Ã÷: ³õʼ»¯µ÷ÊÔ´®¿Ú,²¢Ö¸¶¨ÏàÓ¦µÄ´®¿ÚΪµ÷ÊÔ´®¿Ú * ²ÎÊý˵Ã÷: which²ÎÊýÖ¸¶¨Ê¹ÓÃÄĸö´®¿Ú, ÆäÖµÓ¦¸ÃÊÇ USART_PORT1 »ò USART_PORT2 * ·µ»ØÖµ£º ÎÞ */ void init_dbg_uart(int which, uint32_t baudrate) { /* ÉèÖõ÷ÊÔ´®¿Ú */ debug_usart = USART_PORT2==which ? USART2 : USART1; /* ³õʼ»¯ USART1µÄGPIO¿Ú */ init_usart_gpio(which); /* ÅäÖô®¿ÚͨÐŵIJ¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»µÈ */ config_usart(debug_usart, baudrate); /* Êä³öÒ»¸ö»Ø³µ, secureCRTÈí¼þ²Ù×÷´®¿ÚµÄʱºòÐèÒª */ usart_putchar(debug_usart, '\n'); } /* º¯Êý˵Ã÷: Íùµ÷ÊÔ´®¿Ú·¢ËÍ×Ö·û´® * ²ÎÊý˵Ã÷: str Ö¸ÏòÒª·¢Ë͵Ä×Ö·û´®ÄÚÈÝ * ·µ»ØÖµ£º ÎÞ */ void usart_puts(const char *str) { if( !str ) return ; for( ; *str!='\0'; str++) { /* windowsÏ»»ÐзûÊÇ\r\n,Èç¹ûÅöµ½\nÔòÔÚÇ°ÃæÌí¼Ó\r */ if('\n' == *str) { usart_putchar(debug_usart, '\r'); } usart_putchar(debug_usart, *str); } } /* º¯Êý˵Ã÷: printfº¯ÊýʵÏֵĹ³×Óº¯Êý * ²ÎÊý˵Ã÷: ch: Òª·¢Ë͵Ä×Ö·û FILE *f: δʹÓà * ·µ»ØÖµ£º ·¢Ë͵Ä×Ö·û */ int fputc(int ch, FILE *f) { /* windowsÏ»»ÐзûÊÇ\r\n,Èç¹ûÅöµ½\nÔòÔÚÇ°ÃæÌí¼Ó\r */ if('\n' == ch) { usart_putchar(debug_usart, '\r'); } usart_putchar(debug_usart, ch); return ch; }