/**************************************************************************** * Copyright: (C)2018 Î人ÁèÔÆÎïÍøÖÇ¿ÆÊµÑéÊÒ www.iot-yun.com * Author: GuoWenxue QQ: 281143292 * Description: STM32L151C8T6 CubeMX ¿ª·¢°å printfºÍ´®¿Ú³õʼ»¯º¯Êý * δʹÓÃÖжÏģʽ´¦Àí·¢ËͺͽÓÊÕ * * ChangeLog: * °æ±¾ºÅ ÈÕÆÚ ×÷Õß ËµÃ÷ * V1.0.0 2018.11.05 GuoWenxue ·¢²¼¸Ã°æ±¾ ****************************************************************************/ #include #include "stm32_usart.h" static USART_TypeDef* debug_usart=USART1; /* ĬÈÏʹÓô®¿Ú1×÷Ϊµ÷ÊÔ´®¿Ú */ /* ¿ª·¢°åÉϵĸ÷¸ö´®¿ÚËùʹÓõĴ®¿Ú ¹Ü½Å¡¢Ê±ÖÓ¶¨Òå */ stm32_usart_pins_t usart_pins[USART_MAX] = { /* USARTx GPIOx Tx_Pin Rx_Pin Tx_Pin_source Rx_Pin_Source GPIO_AF_USARTx USART_Clock TxRx_GPIO_Clock */ {USART1, GPIOA, GPIO_Pin_9, GPIO_Pin_10, GPIO_PinSource9, GPIO_PinSource10, GPIO_AF_USART1, RCC_APB2Periph_USART1, RCC_AHBPeriph_GPIOA}, {USART2, GPIOA, GPIO_Pin_2, GPIO_Pin_3, GPIO_PinSource2, GPIO_PinSource3, GPIO_AF_USART2, RCC_APB1Periph_USART2, RCC_AHBPeriph_GPIOA}, {USART3, GPIOB, GPIO_Pin_10, GPIO_Pin_11, GPIO_PinSource10, GPIO_PinSource11, GPIO_AF_USART3, RCC_APB1Periph_USART3, RCC_AHBPeriph_GPIOB}, }; /* º¯Êý˵Ã÷: ÅäÖô®¿ÚGPIO¿ÚºÍʱÖÓ£» * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1¡¢USART_PORT2¡¢USART_PORT3£¬¶¨ÒåÔÚstm32_usart.hÖУ» * ·µ»ØÖµ: ÎÞ */ void init_usart_gpio(uint8_t which) { GPIO_InitTypeDef GPIO_InitStructure; if( which >= USART_MAX ) return ; /* Enable USART GPIO and USART clock */ RCC_AHBPeriphClockCmd(usart_pins[which].gpio_clk, ENABLE); if( USART_PORT1 == which ) RCC_APB2PeriphClockCmd(usart_pins[which].usart_clk, ENABLE); else RCC_APB1PeriphClockCmd(usart_pins[which].usart_clk, ENABLE); /* Connect PXx to USARTx_Tx, PXx to USARTx_Rx */ GPIO_PinAFConfig(usart_pins[which].group, usart_pins[which].txpin_src, usart_pins[which].gpio_af); GPIO_PinAFConfig(usart_pins[which].group, usart_pins[which].rxpin_src, usart_pins[which].gpio_af); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = usart_pins[which].txpin; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(usart_pins[which].group, &GPIO_InitStructure); /* Configure USART1 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = usart_pins[which].rxpin; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(usart_pins[which].group, &GPIO_InitStructure); } /* º¯Êý˵Ã÷: ÅäÖô®¿ÚµÄ²¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»¡¢Á÷¿ØµÈ£» * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1¡¢USART_PORT2¡¢USART_PORT3£¬¶¨ÒåÔÚstm32_usart.hÖУ» * baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200.... * rxirq: ÊÇ·ñʹÄܽÓÊÕÖжϣ» 1->ʹÄÜ 0->²»Ê¹ÄÜ * txirq: ÊÇ·ñʹÄÜ·¢ËÍÖжϣ» 1->ʹÄÜ 0->²»Ê¹ÄÜ * ·µ»ØÖµ: ÎÞ */ void stm32_init_usart(uint8_t which, uint32_t baudrate, uint8_t txirq, uint8_t rxirq) { USART_InitTypeDef USART_InitStructure; if( which >= USART_MAX ) return ; init_usart_gpio(which); /* 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(usart_pins[which].USARTx, &USART_InitStructure); //ÅäÖô®¿Ú²ÎÊýº¯Êý #if 0 /* Not implement yet */ if( rxirq ) //ʹÄܽÓÊÕÖÐ¶Ï USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); if( txirq ) //ʹÄÜ·¢ËÍ»º³å¿ÕÖÐ¶Ï USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); #endif /* Enable the USARTx */ USART_Cmd(usart_pins[which].USARTx, ENABLE); } /* º¯Êý˵Ã÷: ³õʼ»¯µ÷ÊÔ´®¿Ú,²¢Ö¸¶¨ÏàÓ¦µÄ´®¿ÚΪprintfº¯ÊýµÄÊä³ö´®¿Ú * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1¡¢USART_PORT2¡¢USART_PORT3£¬¶¨ÒåÔÚstm32_usart.hÖУ» * baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200.... * ·µ»ØÖµ£º ÎÞ */ void stm32_init_printf(uint8_t which, uint32_t baudrate) { if( which >= USART_MAX ) return ; debug_usart = usart_pins[which].USARTx; /* ³õʼ»¯ USART1µÄGPIO¿Ú */ stm32_init_usart(which, baudrate, TXIRQ_DISABLE, RXIRQ_DISABLE); /* Êä³öÒ»¸ö»Ø³µ, secureCRTÈí¼þ²Ù×÷´®¿ÚµÄʱºòÐèÒª */ USART_PUTCHR(debug_usart, '\n'); } /* º¯Êý˵Ã÷: printfº¯ÊýʵÏֵĹ³×Óº¯Êý * ²ÎÊý˵Ã÷: ch: Òª·¢Ë͵Ä×Ö·û FILE *f: δʹÓà * ·µ»ØÖµ£º ·¢Ë͵Ä×Ö·û */ int fputc(int ch, FILE *f) { /* windowsÏ»»ÐзûÊÇ\r\n,Èç¹ûÅöµ½\nÔòÔÚÇ°ÃæÌí¼Ó\r */ if('\n' == ch) { USART_PUTCHR(debug_usart, '\r'); } USART_PUTCHR(debug_usart, ch); return ch; }