⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm32f10x_usart.c

📁 中文固件库.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
/**
  ******************************************************************************
  * @文件    stm32f10x_usart.c
  * @作者    MCD 应用程序组
  * @版本    V3.1.0
  * @日期    06/19/2009
  * @简述    这个文件 提供所有 USART 固件函数.
  * @翻译    ANSON/sweet1985 Email:airanson110@126.com
  * @日期    14/07/2009
  ******************************************************************************
  * @复件
  *
  * 这个固件仅仅是提供给客户作为设计产品而编写程序的参考目的使客户节约时间。由于
  * 客户使用本固件在开发产品编程上产生的结果意法半导体公司不承担任何直接的和间接
  * 的责任,也不承担任何损害而引起的赔偿。
  *
  * <h2><center>&复制; 版权所有 2009 意法半导体公司</center></h2>
  * 翻译版本仅供学习,如与英文原版有出入应以英文原版为准。
  */ 

/* 包含 ------------------------------------------------------------------*/
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"

/* 自用类型 --------------------------------------------------------------*/
/* 自用定义 --------------------------------------------------------------*/

#define CR1_UE_Set                ((uint16_t)0x2000)  /*!< USART Enable Mask */
#define CR1_UE_Reset              ((uint16_t)0xDFFF)  /*!< USART Disable Mask */

#define CR1_WAKE_Mask             ((uint16_t)0xF7FF)  /*!< USART WakeUp Method Mask */

#define CR1_RWU_Set               ((uint16_t)0x0002)  /*!< USART mute mode Enable Mask */
#define CR1_RWU_Reset             ((uint16_t)0xFFFD)  /*!< USART mute mode Enable Mask */
#define CR1_SBK_Set               ((uint16_t)0x0001)  /*!< USART Break Character send Mask */
#define CR1_CLEAR_Mask            ((uint16_t)0xE9F3)  /*!< USART CR1 Mask */
#define CR2_Address_Mask          ((uint16_t)0xFFF0)  /*!< USART address Mask */

#define CR2_LINEN_Set              ((uint16_t)0x4000)  /*!< USART LIN Enable Mask */
#define CR2_LINEN_Reset            ((uint16_t)0xBFFF)  /*!< USART LIN Disable Mask */

#define CR2_LBDL_Mask             ((uint16_t)0xFFDF)  /*!< USART LIN Break detection Mask */
#define CR2_STOP_CLEAR_Mask       ((uint16_t)0xCFFF)  /*!< USART CR2 STOP Bits Mask */
#define CR2_CLOCK_CLEAR_Mask      ((uint16_t)0xF0FF)  /*!< USART CR2 Clock Mask */

#define CR3_SCEN_Set              ((uint16_t)0x0020)  /*!< USART SC Enable Mask */
#define CR3_SCEN_Reset            ((uint16_t)0xFFDF)  /*!< USART SC Disable Mask */

#define CR3_NACK_Set              ((uint16_t)0x0010)  /*!< USART SC NACK Enable Mask */
#define CR3_NACK_Reset            ((uint16_t)0xFFEF)  /*!< USART SC NACK Disable Mask */

#define CR3_HDSEL_Set             ((uint16_t)0x0008)  /*!< USART Half-Duplex Enable Mask */
#define CR3_HDSEL_Reset           ((uint16_t)0xFFF7)  /*!< USART Half-Duplex Disable Mask */

#define CR3_IRLP_Mask             ((uint16_t)0xFFFB)  /*!< USART IrDA LowPower mode Mask */
#define CR3_CLEAR_Mask            ((uint16_t)0xFCFF)  /*!< USART CR3 Mask */

#define CR3_IREN_Set              ((uint16_t)0x0002)  /*!< USART IrDA Enable Mask */
#define CR3_IREN_Reset            ((uint16_t)0xFFFD)  /*!< USART IrDA Disable Mask */
#define GTPR_LSB_Mask             ((uint16_t)0x00FF)  /*!< Guard Time Register LSB Mask */
#define GTPR_MSB_Mask             ((uint16_t)0xFF00)  /*!< Guard Time Register MSB Mask */
#define IT_Mask                   ((uint16_t)0x001F)  /*!< USART Interrupt Mask */

/* 自用宏 -------------------------------------------------------------*/
/* 自用变量 -----------------------------------------------------------*/
/* 自用函数原型 -------------------------------------------------------*/
/* 自用函数 -----------------------------------------------------------*/

/**
  * @简述  将外设 USARTx 寄存器重设为缺省值.
  * @参数  USARTx: 选择 USART 外围设备. 
  *                这个参数可以取下列值之一: USART1, USART2, USART3, UART4 或 UART5.
  * @返回  没有
  */
void USART_DeInit(USART_TypeDef* USARTx)
{
  /* 检查参数 */
  assert_param(IS_USART_ALL_PERIPH(USARTx));

  if (USARTx == USART1)
  {
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  }
  else if (USARTx == USART2)
  {
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
  }
  else if (USARTx == USART3)
  {
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
  }    
  else if (USARTx == UART4)
  {
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
    RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
  }    
  else
  {
    if (USARTx == UART5)
    { 
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
      RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
    }
  }
}

/**
  * @简述  根据 USART_InitStruct 中指定的参数初始化外设 USARTx 寄存器.
  * @参数  USARTx: 选择 USART 外围设备. 
  *                这个参数可以取下列值之一: USART1, USART2, USART3, UART4 或 UART5.
  * @参数  USART_InitStruct: 指向结构 USART_InitTypeDef 的指针,包含了外设 USART 的配置信息.
  * @返回  没有
  */
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
{
  uint32_t tmpreg = 0x00, apbclock = 0x00;
  uint32_t integerdivider = 0x00;
  uint32_t fractionaldivider = 0x00;
  uint32_t usartxbase = 0;
  RCC_ClocksTypeDef RCC_ClocksStatus;
  /* 检查参数 */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));  
  assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
  assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
  assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
  assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
  assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
  /* The hardware flow control is available only for USART1, USART2 and USART3 */
  if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
  {
    assert_param(IS_USART_123_PERIPH(USARTx));
  }

  usartxbase = (uint32_t)USARTx;

/*---------------------------- USART CR2 Configuration -----------------------*/
  tmpreg = USARTx->CR2;
  /* 清除 STOP[13:12] 位 */
  tmpreg &= CR2_STOP_CLEAR_Mask;
  /* 配置 USART 结束位, 时钟, 时钟极性, 时钟相位 和 末位 ------------*/
  /* 设置 STOP[13:12] 位,按照 USART_StopBits 的值 */
  tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
  
  /* 写 USART CR2 */
  USARTx->CR2 = (uint16_t)tmpreg;

/*---------------------------- USART CR1 Configuration -----------------------*/
  tmpreg = USARTx->CR1;
  /* 清除 M, PCE, PS, TE 和 RE 位 */
  tmpreg &= CR1_CLEAR_Mask;
  /* 配置 USART 字长, 奇偶 和 模式 ----------------------- */
  /* 设置 M   位,按照 USART_WordLength 的值 */
  /* 设置 PCE 和 PS 位,按照 USART_Parity 的值 */
  /* 设置 TE  和 RE 位,按照 USART_Mode 的值 */
  tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
            USART_InitStruct->USART_Mode;
  /* 写 USART CR1 */
  USARTx->CR1 = (uint16_t)tmpreg;

/*---------------------------- USART CR3 Configuration -----------------------*/  
  tmpreg = USARTx->CR3;
  /* 清除 CTSE 和 RTSE 位 */
  tmpreg &= CR3_CLEAR_Mask;
  /* 配置 USART HFC -------------------------------------------------*/
  /* 设置 CTSE 和 RTSE 位,按照 USART_HardwareFlowControl 的值 */
  tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
  /* 写 USART CR3 */
  USARTx->CR3 = (uint16_t)tmpreg;

/*---------------------------- USART BRR Configuration -----------------------*/
  /* 配置 USART 波特率 -------------------------------------------*/
  RCC_GetClocksFreq(&RCC_ClocksStatus);
  if (usartxbase == USART1_BASE)
  {
    apbclock = RCC_ClocksStatus.PCLK2_Frequency;
  }
  else
  {
    apbclock = RCC_ClocksStatus.PCLK1_Frequency;
  }
  /* 确定整数部分 */
  integerdivider = ((0x19 * apbclock) / (0x04 * (USART_InitStruct->USART_BaudRate)));
  tmpreg = (integerdivider / 0x64) << 0x04;
  /* 确定分数部分 */
  fractionaldivider = integerdivider - (0x64 * (tmpreg >> 0x04));
  tmpreg |= ((((fractionaldivider * 0x10) + 0x32) / 0x64)) & ((uint8_t)0x0F);
  /* 写 USART BRR */
  USARTx->BRR = (uint16_t)tmpreg;
}

/**
  * @简述  把 USART_InitStruct 中的每一个参数按缺省值填入.
  * @参数  USART_InitStruct: 指向结构 USART_InitTypeDef 的指针,待初始化.
  * @返回  没有
  */
void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
{
  /* USART_InitStruct 结构体的缺省值 */
  USART_InitStruct->USART_BaudRate = 9600;
  USART_InitStruct->USART_WordLength = USART_WordLength_8b;
  USART_InitStruct->USART_StopBits = USART_StopBits_1;
  USART_InitStruct->USART_Parity = USART_Parity_No ;
  USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;  
}

/**
  * @简述  初始化 USARTx 外围时钟,按照 USART_ClockInitStruct 内的参数.
  * @参数  USARTx: 这里x可以取 1, 2, 3 来选择 USART 外围设备.
  * @参数  USART_ClockInitStruct: 指向 USART_ClockInitTypeDef 的指针,包含了外设 USART 的配置信息.  
  * @注解  这个智能卡模式不能应用于 UART4 和 UART5.
  * @返回  没有
  */
void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
{
  uint32_t tmpreg = 0x00;
  /* 检查参数 */
  assert_param(IS_USART_123_PERIPH(USARTx));
  assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
  assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
  assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
  assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
  
/*---------------------------- USART CR2 Configuration -----------------------*/
  tmpreg = USARTx->CR2;
  /* 清除 CLKEN, CPOL, CPHA 和 LBCL 位 */
  tmpreg &= CR2_CLOCK_CLEAR_Mask;
  /* 配置 USART Clock, CPOL, CPHA and LastBit ------------*/
  /* 设置 CLKEN 位,按照 USART_Clock 的值 */
  /* 设置 CPOL  位,按照 USART_CPOL 的值 */
  /* 设置 CPHA  位,按照 USART_CPHA 的值 */
  /* 设置 LBCL  位,按照 USART_LastBit 的值 */
  tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL | 
                 USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
  /* 写 USART CR2 */
  USARTx->CR2 = (uint16_t)tmpreg;
}

/**
  * @简述  用默认值填充每一个 USART_ClockInitStruct 的成员.
  * @参数  USART_ClockInitStruct: 指向 USART_ClockInitTypeDef 结构的指针,用于初始化.
  * @返回  没有
  */
void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
{
  /* USART_ClockInitStruct 结构体的缺省值 */
  USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
  USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
  USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
  USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
}

/**
  * @简述  使能或者失能 USART 外设.
  * @参数  USARTx: 选择 USART 或者 UART 的外围设备. 
  *                这个参数可以是下面的值之一 : USART1, USART2, USART3, UART4 或 UART5.
  * @参数  NewState: USARTx 外围设备的新状态.
  *                  这个参数可以是: ENABLE or DISABLE.
  * @返回  没有
  */
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
  /* 检查参数 */
  assert_param(IS_USART_ALL_PERIPH(USARTx));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  
  if (NewState != DISABLE)
  {
    /* Enable the selected USART by setting the UE bit in the CR1 register */
    USARTx->CR1 |= CR1_UE_Set;
  }
  else
  {
    /* Disable the selected USART by clearing the UE bit in the CR1 register */
    USARTx->CR1 &= CR1_UE_Reset;
  }
}

/**
  * @简述  使能或者失能指定的 USART 中断.
  * @参数  USARTx: 选择 USART 或者 UART 的外围设备. 
  *                这个参数可以是下面的值之一 : USART1, USART2, USART3, UART4 or UART5.
  * @参数  USART_IT: 指定要使能或失能的 USART 中断源.
  *          这个参数可以是下面的值之一 :
  *          USART_IT_CTS:  CTS 改变中断 (不能应用于 UART4 和 UART5)
  *          USART_IT_LBD:  LIN 间隔侦测中断
  *          USART_IT_TXE:  传输数据寄存器空中断
  *          USART_IT_TC:   传输完成中断
  *          USART_IT_RXNE: 接收数据寄存器不为空中断
  *          USART_IT_IDLE: 空闲总线中断
  *          USART_IT_PE:   奇偶错误中断
  *          USART_IT_ERR:  错误中断(桢错误, 噪声错误, 溢出错误)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -