STM32F4(USART+DMA+动态内存)

STM32F4(USART+DMA+动态内存)

GitHub仓库:https://github.com/XinLiGitHub/STM32F4xx_USART_Example

PS:博文不再更新,后续更新会在GitHub仓库进行。


1,开发环境

      1,适用芯片:STM32F4全部芯片

      2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0

      3,IDE:MDK517


2,驱动源码

      USART.h文件

/****************************************************************
 * Copyright (C) 2016, XinLi, all right reserved.
 * File name:    USART.h
 * Date:         2016.03.22
 * Description:  USART Driver
*****************************************************************/

#ifndef __USART_H
#define __USART_H

/****************************************************************
 *                        Header include
*****************************************************************/
#include "stm32f4xx.h"

/****************************************************************
 *                       Macro definition
*****************************************************************/

/********************* USART1 Configuration *********************/

#define RCC_AHB1Periph_USART1_Tx    RCC_AHB1Periph_GPIOA
#define RCC_AHB1Periph_USART1_Rx    RCC_AHB1Periph_GPIOA

#define GPIO_USART1_Tx              GPIOA
#define GPIO_USART1_Rx              GPIOA

#define GPIO_Pin_USART1_Tx          GPIO_Pin_9
#define GPIO_Pin_USART1_Rx          GPIO_Pin_10

#define GPIO_PinSource_USART1_Tx    GPIO_PinSource9
#define GPIO_PinSource_USART1_Rx    GPIO_PinSource10

/****************************************************************/

/********************* USART2 Configuration *********************/

#define RCC_AHB1Periph_USART2_Tx    RCC_AHB1Periph_GPIOA
#define RCC_AHB1Periph_USART2_Rx    RCC_AHB1Periph_GPIOA

#define GPIO_USART2_Tx              GPIOA
#define GPIO_USART2_Rx              GPIOA

#define GPIO_Pin_USART2_Tx          GPIO_Pin_2
#define GPIO_Pin_USART2_Rx          GPIO_Pin_3

#define GPIO_PinSource_USART2_Tx    GPIO_PinSource2
#define GPIO_PinSource_USART2_Rx    GPIO_PinSource3

/****************************************************************/

#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F446xx) || defined(STM32F412xG) || defined(STM32F413_423xx)
/********************* USART3 Configuration *********************/

#define RCC_AHB1Periph_USART3_Tx    RCC_AHB1Periph_GPIOB
#define RCC_AHB1Periph_USART3_Rx    RCC_AHB1Periph_GPIOB

#define GPIO_USART3_Tx              GPIOB
#define GPIO_USART3_Rx              GPIOB

#define GPIO_Pin_USART3_Tx          GPIO_Pin_10
#define GPIO_Pin_USART3_Rx          GPIO_Pin_11

#define GPIO_PinSource_USART3_Tx    GPIO_PinSource10
#define GPIO_PinSource_USART3_Rx    GPIO_PinSource11

/****************************************************************/
#endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F446xx || STM32F412xG || STM32F413_423xx */

#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F446xx) || defined(STM32F413_423xx)
/********************* USART4 Configuration *********************/

#define RCC_AHB1Periph_USART4_Tx    RCC_AHB1Periph_GPIOC
#define RCC_AHB1Periph_USART4_Rx    RCC_AHB1Periph_GPIOC

#define GPIO_USART4_Tx              GPIOC
#define GPIO_USART4_Rx              GPIOC

#define GPIO_Pin_USART4_Tx          GPIO_Pin_10
#define GPIO_Pin_USART4_Rx          GPIO_Pin_11

#define GPIO_PinSource_USART4_Tx    GPIO_PinSource10
#define GPIO_PinSource_USART4_Rx    GPIO_PinSource11

/****************************************************************/

/********************* USART5 Configuration *********************/

#define RCC_AHB1Periph_USART5_Tx    RCC_AHB1Periph_GPIOC
#define RCC_AHB1Periph_USART5_Rx    RCC_AHB1Periph_GPIOD

#define GPIO_USART5_Tx              GPIOC
#define GPIO_USART5_Rx              GPIOD

#define GPIO_Pin_USART5_Tx          GPIO_Pin_12
#define GPIO_Pin_USART5_Rx          GPIO_Pin_2

#define GPIO_PinSource_USART5_Tx    GPIO_PinSource12
#define GPIO_PinSource_USART5_Rx    GPIO_PinSource2

/****************************************************************/
#endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F446xx || STM32F413_423xx */

/********************* USART6 Configuration *********************/

#define RCC_AHB1Periph_USART6_Tx    RCC_AHB1Periph_GPIOC
#define RCC_AHB1Periph_USART6_Rx    RCC_AHB1Periph_GPIOC

#define GPIO_USART6_Tx              GPIOC
#define GPIO_USART6_Rx              GPIOC

#define GPIO_Pin_USART6_Tx          GPIO_Pin_6
#define GPIO_Pin_USART6_Rx          GPIO_Pin_7

#define GPIO_PinSource_USART6_Tx    GPIO_PinSource6
#define GPIO_PinSource_USART6_Rx    GPIO_PinSource7

/****************************************************************/

#if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F413_423xx)
/********************* USART7 Configuration *********************/

#define RCC_AHB1Periph_USART7_Tx    RCC_AHB1Periph_GPIOE
#define RCC_AHB1Periph_USART7_Rx    RCC_AHB1Periph_GPIOE

#define GPIO_USART7_Tx              GPIOE
#define GPIO_USART7_Rx              GPIOE

#define GPIO_Pin_USART7_Tx          GPIO_Pin_8
#define GPIO_Pin_USART7_Rx          GPIO_Pin_7

#define GPIO_PinSource_USART7_Tx    GPIO_PinSource8
#define GPIO_PinSource_USART7_Rx    GPIO_PinSource7

/****************************************************************/

/********************* USART8 Configuration *********************/

#define RCC_AHB1Periph_USART8_Tx    RCC_AHB1Periph_GPIOE
#define RCC_AHB1Periph_USART8_Rx    RCC_AHB1Periph_GPIOE

#define GPIO_USART8_Tx              GPIOE
#define GPIO_USART8_Rx              GPIOE

#define GPIO_Pin_USART8_Tx          GPIO_Pin_1
#define GPIO_Pin_USART8_Rx          GPIO_Pin_0

#define GPIO_PinSource_USART8_Tx    GPIO_PinSource1
#define GPIO_PinSource_USART8_Rx    GPIO_PinSource0

/****************************************************************/
#endif /* STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F413_423xx */

#if defined(STM32F413_423xx)
/********************* USART9 Configuration *********************/

#define RCC_AHB1Periph_USART9_Tx    RCC_AHB1Periph_GPIOG
#define RCC_AHB1Periph_USART9_Rx    RCC_AHB1Periph_GPIOG

#define GPIO_USART9_Tx              GPIOG
#define GPIO_USART9_Rx              GPIOG

#define GPIO_Pin_USART9_Tx          GPIO_Pin_1
#define GPIO_Pin_USART9_Rx          GPIO_Pin_0

#define GPIO_PinSource_USART9_Tx    GPIO_PinSource1
#define GPIO_PinSource_USART9_Rx    GPIO_PinSource0

/****************************************************************/

/******************** USART10 Configuration *********************/

#define RCC_AHB1Periph_USART10_Tx    RCC_AHB1Periph_GPIOE
#define RCC_AHB1Periph_USART10_Rx    RCC_AHB1Periph_GPIOE

#define GPIO_USART10_Tx              GPIOE
#define GPIO_USART10_Rx              GPIOE

#define GPIO_Pin_USART10_Tx          GPIO_Pin_3
#define GPIO_Pin_USART10_Rx          GPIO_Pin_2

#define GPIO_PinSource_USART10_Tx    GPIO_PinSource3
#define GPIO_PinSource_USART10_Rx    GPIO_PinSource2

/****************************************************************/
#endif /* STM32F413_423xx */

#define BAUD_RATE_2400      ((uint32_t)2400)
#define BAUD_RATE_4800      ((uint32_t)4800)
#define BAUD_RATE_9600      ((uint32_t)9600)
#define BAUD_RATE_19200     ((uint32_t)19200)
#define BAUD_RATE_38400     ((uint32_t)38400)
#define BAUD_RATE_57600     ((uint32_t)57600)
#define BAUD_RATE_115200    ((uint32_t)115200)
#define BAUD_RATE_230400    ((uint32_t)230400)
#define BAUD_RATE_460800    ((uint32_t)460800)
#define BAUD_RATE_921600    ((uint32_t)921600)

/****************************************************************
 *                       Type definition
*****************************************************************/


/****************************************************************
 *                     Structure definition
*****************************************************************/



#ifdef __cplusplus
 extern "C" {
#endif  /* __cplusplus */

/****************************************************************
 *                     Variable declaration
*****************************************************************/


/****************************************************************
 *                     Function declaration
*****************************************************************/
void USART_Configuration(USART_TypeDef* USARTx, uint32_t BaudRate, uint16_t USART_IT, uint16_t BufferSize_Tx, uint16_t BufferSize_Rx);
void USART_Unconfigure(USART_TypeDef* USARTx);
uint16_t USART_SetSendData(USART_TypeDef* USARTx, void* Buffer, uint16_t BufferSize);
uint16_t USART_GetReceiveData(USART_TypeDef* USARTx, void* Buffer, uint16_t BufferSize);

#ifdef __cplusplus
}
#endif  /* __cplusplus */

#endif 	/* __USART_H */


     USART.c文件

/****************************************************************
 * Copyright (C) 2016, XinLi, all right reserved.
 * File name:    USART.c
 * Date:         2016.03.22
 * Description:  USART Driver
*****************************************************************/

/****************************************************************
 *                        Header include
*****************************************************************/
#include "USART.h"
#include <string.h>
#include <stdlib.h>

/****************************************************************
 *                       Global variables
*****************************************************************/
static uint16_t USART1_BufferSize_Tx = NULL;
static uint16_t USART1_BufferSize_Rx = NULL;
static uint8_t* USART1_Buffer_Tx = NULL;
static uint8_t* USART1_Buffer_Rx = NULL;

static uint16_t USART2_BufferSize_Tx = NULL;
static uint16_t USART2_BufferSize_Rx = NULL;
static uint8_t* USART2_Buffer_Tx = NULL;
static uint8_t* USART2_Buffer_Rx = NULL;

#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F446xx) || defined(STM32F412xG) || defined(STM32F413_423xx)
static uint16_t USART3_BufferSize_Tx = NULL;
static uint16_t USART3_BufferSize_Rx = NULL;
static uint8_t* USART3_Buffer_Tx = NULL;
static uint8_t* USART3_Buffer_Rx = NULL;
#endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F446xx || STM32F412xG || STM32F413_423xx */

#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F446xx) || defined(STM32F413_423xx)
static uint16_t USART4_BufferSize_Tx = NULL;
static uint16_t USART4_BufferSize_Rx = NULL;
static uint8_t* USART4_Buffer_Tx = NULL;
static uint8_t* USART4_Buffer_Rx = NULL;

static uint16_t USART5_BufferSize_Tx = NULL;
static uint16_t USART5_BufferSize_Rx= NULL;
static uint8_t* USART5_Buffer_Tx = NULL;
static uint8_t* USART5_Buffer_Rx = NULL;
#endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F446xx || STM32F413_423xx */

static uint16_t USART6_BufferSize_Tx = NULL;
static uint16_t USART6_BufferSize_Rx = NULL;
static uint8_t* USART6_Buffer_Tx = NULL;
static uint8_t* USART6_Buffer_Rx = NULL;

#if defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F413_423xx)
static uint16_t USART7_BufferSize_Tx = NULL;
static uint16_t USART7_BufferSize_Rx = NULL;
static uint8_t* USART7_Buffer_Tx = NULL;
static uint8_t* USART7_Buffer_Rx = NULL;

static uint16_t USART8_BufferSize_Tx = NULL;
static uint16_t USART8_BufferSize_Rx = NULL;
static uint8_t* USART8_Buffer_Tx = NULL;
static uint8_t* USART8_Buffer_Rx = NULL;
#endif /* STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F413_423xx */

#if defined(STM32F413_423xx)
static uint16_t USART9_BufferSize_Tx = NULL;
static uint16_t USART9_BufferSize_Rx = NULL;
static uint8_t* USART9_Buffer_Tx = NULL;
static uint8_t* USART9_Buffer_Rx = NULL;

static uint16_t USART10_BufferSize_Tx = NULL;
static uint16_t USART10_BufferSize_Rx = NULL;
static uint8_t* USART10_Buffer_Tx = NULL;
static uint8_t* USART10_Buffer_Rx = NULL;
#endif /* STM32F413_423xx */

static uint16_t gpioFlag  = NULL; /* Used to record the GPIO initialization flag. */
static uint16_t usartFlag = NULL; /* Used to record the USART initialization flag. */
static uint16_t dmaTxFlag = NULL; /* Used to record the DMA TX initialization flag. */
static uint16_t dmaRxFlag = NULL; /* Used to record the DMA RX initialization flag. */
static uint16_t firstFlag = NULL; /* Used to record the USART first send data flag. */

/****************************************************************
 *                     Function declaration
*****************************************************************/
static void USART_DMA_TX_Configuration(USART_TypeDef* USARTx);
static void USART_DMA_RX_Configuration(USART_TypeDef* USARTx);
static void USART_DMA_TX_Unconfigure(USART_TypeDef* USARTx);
static void USART_DMA_RX_Unconfigure(USART_TypeDef* USARTx);

/****************************************************************
 *                     Function definition
*****************************************************************/

/****************************************************************
 * Function:    USART_Configuration
 * Description: USART Configuration.
 * Input:       USARTx
 *              BaudRate
 *              USART_IT
 *              BufferSize_Tx
 *              BufferSize_Rx
 * Output:
 * Return:
*****************************************************************/
void USART_Configuration(USART_TypeDef* USARTx, uint32_t BaudRate, uint16_t USART_IT, uint16_t BufferSize_Tx, uint16_t BufferSize_Rx)
{
  uint16_t USART_Mode = NULL;
  
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  
  if( (BufferSize_Tx != NULL) && (BufferSize_Rx == NULL) )
  {
    USART_Mode = USART_Mode_Tx;
  }
  else if( (BufferSize_Tx == NULL) && (BufferSize_Rx != NULL) )
  {
    USART_Mode = USART_Mode_Rx;
  }
  else if( (BufferSize_Tx != NULL) && (BufferSize_Rx != NULL) )
  {
    USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  }
  else
  {
    USART_Unconfigure(USARTx);  /* Unconfigure USARTx */
    
    return;
  }
  
  if(USARTx == USART1)
  {
    firstFlag &= 0xFFFE;
    
    if(USART1_Buffer_Tx != NULL)
    {
      free(USART1_Buffer_Tx); /* Release memory */
      USART1_Buffer_Tx = NULL;
      USART1_BufferSize_Tx = NULL;
    }
    
    if(USART1_Buffer_Rx != NULL)
    {
      free(USART1_Buffer_Rx); /* Release memory */
      USART1_Buffer_Rx = NULL;
      USART1_BufferSize_Rx = NULL;
    }
    
    USART1_BufferSize_Tx = BufferSize_Tx;
    USART1_BufferSize_Rx = BufferSize_Rx;
    
    if(USART1_BufferSize_Tx != NULL)
    {
      USART1_Buffer_Tx = malloc(USART1_BufferSize_Tx);      /* Apply for memory */
      memset(USART1_Buffer_Tx, NULL, USART1_BufferSize_Tx); /* Empty memory */
    }
    
    if(USART1_BufferSize_Rx != NULL)
    {
      USART1_Buffer_Rx = malloc(USART1_BufferSize_Rx);      /* Apply for memory */
      memset(USART1_Buffer_Rx, NULL, USART1_BufferSize_Rx); /* Empty memory */
    }
    
    if( (usartFlag & 0x0001) != 0x0001 )
    {
      usartFlag |= 0x0001;
      
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);  /* Enable USART1 clock */
    }
    
    if( (gpioFlag & 0x0001) != 0x0001 )
    {
      gpioFlag |= 0x0001;
      
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_USART1_Tx, ENABLE);  /* Enable USART1_Tx GPIO clock */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_USART1_Rx, ENABLE);  /* Enable USART1_Rx GPIO clock */
      
      GPIO_PinAFConfig(GPIO_USART1_Tx, GPIO_PinSource_USART1_Tx, GPIO_AF_USART1); /* Connect GPIOxn to USART1_Tx */
      GPIO_PinAFConfig(GPIO_USART1_Rx, GPIO_PinSource_USART1_Rx, GPIO_AF_USART1); /* Connect GPIOxn to USART1_Rx */
      
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_USART1_Tx;   /* Specifies the GPIO pins to be configured. */
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        /* Specifies the operating mode for the selected pins. */
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  /* Specifies the speed for the selected pins. */
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      /* Specifies the operating output type for the selected pins. */
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        /* Specifies the operating Pull-up/Pull down for the selected pins. */
      GPIO_Init(GPIO_USART1_Tx, &GPIO_InitStructure);     /* Initializes the GPIOx peripheral. */
      
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_USART1_Rx;   /* Specifies the GPIO pins to be configured. */
      GPIO_Init(GPIO_USART1_Rx, &GPIO_InitStructure);     /* Initializes the GPIOx peripheral. */
    }
  }
  else if(USARTx == USART2)
  {
    firstFlag &= 0xFFFD;
    
    if(USART2_Buffer_Tx != NULL)
    {
      free(USART2_Buffer_Tx); /* Release memory */
      USART2_Buffer_Tx = NULL;
      USART2_BufferSize_Tx = NULL;
    }
    
    if(USART2_Buffer_Rx != NULL)
    {
      free(USART2_Buffer_Rx); /* Release memory */
      USART2_Buffer_Rx = NULL;
      USART2_BufferSize_Rx = NULL;
    }
    
    USART2_BufferSize_Tx = BufferSize_Tx;
    USART2_BufferSize_Rx = BufferSize_Rx;
    
    if(USART2_BufferSize_Tx != NULL)
    {
      USART2_Buffer_Tx = malloc(USART2_BufferSize_Tx);      /* Apply for memory */
      memset(USART2_Buffer_Tx, NULL, USART2_BufferSize_Tx); /* Empty memory */
    }
    
    if(USART2_BufferSize_Rx != NULL)
    {
      USART2_Buffer_Rx = malloc(USART2_BufferSize_Rx);      /* Apply for memory */
      memset(USART2_Buffer_Rx, NULL, USART2_BufferSize_Rx); /* Empty memory */
    }
    
    if( (usartFlag & 0x0002) != 0x0002 )
    {
      usartFlag |= 0x0002;
      
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  /* Enable USART2 clock */
    }
    
    if( (gpioFlag & 0x0002) != 0x0002 )
    {
      gpioFlag |= 0x0002;
      
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_USART2_Tx, ENABLE);  /* Enable USART2_Tx GPIO clock */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_USART2_Rx, ENABLE);  /* Enable USART2_Rx GPIO clock */
      
      GPIO_PinAFConfig(GPIO_USART2_Tx, GPIO_PinSource_USART2_Tx, GPIO_AF_USART2);	/* Connect GPIOxn to USART2_Tx */
      GPIO_PinAFConfig(GPIO_USART2_Rx, GPIO_PinSource_USART2_Rx, GPIO_AF_USART2);	/* Connect GPIOxn to USART2_Rx */
      
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_USART2_Tx;   /* Specifies the GPIO pins to be configured. */
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        /* Specifies the operating mode for the selected pins. */
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  /* Specifies the speed for the selected pins. */
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      /* Specifies the operating output type for the selected pins. */
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        /* Specifies the operating Pull-up/Pull down for the selected pins. */
      GPIO_Init(GPIO_USART2_Tx, &GPIO_InitStructure);     /* Initializes the GPIOx peripheral. */
      
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_USART2_Rx;   /* Specifies the GPIO pins to be configured. */
      GPIO_Init(GPIO_USART2_Rx, &GPIO_InitStructure);     /* Initializes the GPIOx peripheral. */
    }
  }
#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F446xx) || defined(STM32F412xG) || defined(STM32F413_423xx)
  else if(USARTx == USART3)
  {
    firstFlag &= 0xFFFB;
    
    if(USART3_Buffer_Tx != NULL)
    {
      free(USART3_Buffer_Tx); /* Release memory */
      USART3_Buffer_Tx = NULL;
      USART3_BufferSize_Tx = NULL;
    }
    
    if(USART3_Buffer_Rx != NULL)
    {
      free(USART3_Buffer_Rx); /* Release memory */
      USART3_Buffer_Rx = NULL;
      USART3_BufferSize_Rx = NULL;
    }
    
    USART3_BufferSize_Tx = BufferSize_Tx;
    USART3_BufferSize_Rx = BufferSize_Rx;
    
    if(USART3_BufferSize_Tx != NULL)
    {
      USART3_Buffer_Tx = malloc(USART3_BufferSize_Tx);      /* Apply for memory */
      memset(USART3_Buffer_Tx, NULL, USART3_BufferSize_Tx); /* Empty memory */
    }
    
    if(USART3_BufferSize_Rx != NULL)
    {
      USART3_Buffer_Rx = malloc(USART3_BufferSize_Rx);      /* Apply for memory */
      memset(USART3_Buffer_Rx, NULL, USART3_BufferSize_Rx); /* Empty memory */
    }
    
    if( (usartFlag & 0x0004) != 0x0004 )
    {
      usartFlag |= 0x0004;
      
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  /* Enable USART3 clock */
    }
    
    if( (gpioFlag & 0x0004) != 0x0004 )
    {
      gpioFlag |= 0x0004;
      
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_USART3_Tx, ENABLE);  /* Enable USART3_Tx GPIO clock */
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_USART3_Rx, ENABLE);  /* Enable USART3_Rx GPIO clock */
      
      GPIO_PinAFConfig(GPIO_USART3_Tx, GPIO_PinSource_USART3_Tx, GPIO_AF_USART3); /* Connect GPIOxn to USART3_Tx */
      GPIO_PinAFConfig(GPIO_USART3_Rx, GPIO_PinSource_USART3_Rx, GPIO_AF_USART3); /* Connect GPIOxn to USART3_Rx */
      
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_USART3_Tx;   /* Specifies the GPIO pins to be configured. */
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        /* Specifies the operating mode for the selected pins. */
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  /* Specifies the speed for the selected pins. */
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      /* Specifies the operating output type for the selected pins. */
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        /* Specifies the operating Pull-up/Pull down for the selected pins. */
      GPIO_Init(GPIO_USART3_Tx, &GPIO_InitStructure);     /* Initializes the GPIOx peripheral. */
      
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_USART3_Rx;   /* Specifies the GPIO pins to be configured. */
      GPIO_Init(GPIO_USART3_Rx, &GPIO_InitStructure);     /* Initializes the GPIOx peripheral. */
    }
  }
#endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F469_479xx || STM32F446xx || STM32F412xG || STM32F413_423xx */
#if defined(STM32F40_41xxx) || defined(STM32F427_437xx) || defined(STM32F429_439xx) || defined(STM32F469_479xx) || defined(STM32F446xx) || defined(STM32F413_423xx)
  else if(USARTx == UART4)
  {
    firstFlag &= 0xFFF7;
    
    if(USART4_Buffer_Tx != NULL)
    {
      free(USART4_Buffer_Tx); /* Release memory */
      USART4_Buffer_Tx = NULL;
      USART4_BufferSize_Tx = NULL;
    }
    
    if(USART4_Buffer_Rx != NULL)
    {
      free(USART4_Buffer_Rx); /* Release memory */
      USART4_Buffer_Rx = NULL;
      USART4_BufferSize_Rx = NULL;
    }
    
    USART4_BufferSize_Tx = BufferSize_Tx;
    USART4_BufferSize_Rx = BufferSize_Rx;
    
    if(USART4_BufferSize_Tx != NULL)
    {
      USART4_Buffer_Tx = malloc(USART4_BufferSize_Tx);      /* Apply for memory */
      memset(USART4_Buffer_Tx, NULL, USART4_BufferSize_Tx); /* Empty memory */
    }
    
    if(USART4_BufferSize_Rx != NULL)
    {
      USART4_Buffer_Rx = malloc(USART4_BufferSize_Rx);      /* Apply for memory */
      memset(USART4_Buffer_Rx, NULL, USART4_BufferSize_Rx); /* Empty memory 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值