/**************************************************************************** * 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 "misc.h" #include "stm32f10x.h" #include "stm32v5_usart.h" static USART_TypeDef* debug_usart=USART1; /* ĬÈÏʹÓô®¿Ú1×÷Ϊµ÷ÊÔ´®¿Ú */ uint8_t g_RxBuf[RXBUF_SIZE]; uint8_t g_RxLen = 0; uint8_t g_RxEnd_Flag = 0; /* º¯Êý˵Ã÷: ÅäÖô®¿ÚGPIO¿ÚºÍʱÖÓ£» * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1 »ò USART_PORT2 * ·µ»ØÖµ: ÎÞ */ void init_usart_gpio(int which) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, 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); } /* º¯Êý˵Ã÷: ÅäÖô®¿ÚµÄ²¨ÌØÂÊ¡¢Êý¾Ýλ¡¢ÆæÅ¼Ð£Ñéλ¡¢Í£Ö¹Î»¡¢Á÷¿ØµÈ£» * ²ÎÊý˵Ã÷: whichÖ¸¶¨ÒªÅäÖõĴ®¿Ú,ÆäÖµÓ¦¸ÃΪ USART_PORT1 »ò USART_PORT2 * baudrate: ²¨ÌØÂÊ,ÆäֵΪ: 115200,9600,4800,2400,1200.... * rxirq: ÊÇ·ñʹÄܽÓÊÕÖжÏ, 1: ʹÄÜ 0: ²»Ê¹ÄÜ * ·µ»ØÖµ: ÎÞ */ void config_usart(uint8_t which, uint32_t baudrate, uint8_t rxirq) { USART_InitTypeDef USART_InitStructure; USART_TypeDef* USARTx = NULL; NVIC_InitTypeDef NVIC_InitStructure; /* ¸ù¾Ý´®¿Ú²»Í¬ÉèÖÃÏàÓ¦²ÎÊý */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); if( USART_PORT2 == which ) { USARTx = USART2; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } else { USARTx = USART1; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } /* ÅäÖÃÏàÓ¦´®¿Ú Êý¾Ýλ¡¢ÆæÅ¼Ð£ÑéλµÈ */ 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); //ÅäÖô®¿Ú²ÎÊýº¯Êý /* ÅäÖÃÏàÓ¦´®¿ÚÖÐ¶Ï */ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); if( USART_PORT2 == which ) { USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ClearFlag(USART2, USART_FLAG_TC); /* ½â¾öµÚ1¸ö×Ö½ÚÎÞ·¨ÕýÈ··¢ËͳöÈ¥µÄÎÊÌâ */ } else { USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ClearFlag(USART1, USART_FLAG_TC); /* ½â¾öµÚ1¸ö×Ö½ÚÎÞ·¨ÕýÈ··¢ËͳöÈ¥µÄÎÊÌâ */ } /* ʹÄÜ´®¿Ú */ USART_Cmd(USARTx, ENABLE); } /* º¯Êý˵Ã÷: ͨ¹ýÖ¸¶¨´®¿Ú·¢ËÍÒ»¸ö×Ö·û * ²ÎÊý˵Ã÷: usartÖ¸¶¨Äĸö´®¿Ú, USART1 »ò USART2 * ch ÊÇÒª·¢Ë͵Ä×Ö·û * ·µ»ØÖµ: ÎÞ */ __inline void usart_putchar(USART_TypeDef* usart, uint8_t ch) { USART_ClearFlag(usart, USART_FLAG_TC); /* ½â¾öµÚ1¸ö×Ö½ÚÎÞ·¨ÕýÈ··¢ËͳöÈ¥µÄÎÊÌâ */ 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(which, baudrate, RXIRQ_ENB); /* Êä³öÒ»¸ö»Ø³µ, 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); } } /* º¯Êý˵Ã÷: Íùµ÷ÊÔ´®¿Ú·¢ËÍ×Ö·û´® * ²ÎÊý˵Ã÷: data Ö¸ÏòҪͨ¹ý´®¿Ú·¢Ë͵ÄÊý¾Ý * len Ö¸ÏòҪͨ¹ý´®¿Ú·¢Ë͵ÄÊý¾Ý³¤¶È * ·µ»ØÖµ£º ÎÞ */ void usart_send_data(uint8_t *data, uint32_t len) { uint32_t i = 0; if( !data || len==0 ) return ; for(i=0; i= RXBUF_SIZE ) g_RxLen = 0; /* Ò»¸öÒ»¸ö×Ö½Ú´Ó´®¿Ú½ÓÊÕ×Ö·û£¬ÊÕµ½µÄ×Ö·û´æ·Åµ½bufferÖУ¬Èç¹ûÊÕµ½\r\n»»ÐÐÔò±êʶÊý¾Ý½ÓÊÕÍê³É */ g_RxBuf[g_RxLen++] = USART_ReceiveData(USART1); if( g_RxLen >= 2 && g_RxBuf [g_RxLen-2]=='\r' && g_RxBuf [g_RxLen-1]=='\n') { g_RxEnd_Flag = 1; /* ÉèÖÃÒ»Ö¡½ÓÊÕÍê³É±êʶ */ } } OUT: USART_ClearITPendingBit(USART1, USART_IT_RXNE); } /* ´®¿Ú2 ÖжϽÓÊÕº¯Êý */ void USART2_IRQHandler(void) { }