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

📄 stm32f10x_can.c

📁 stm32f10xx的中文译文例程
💻 C
📖 第 1 页 / 共 3 页
字号:
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* 文件名称: stm32f10x_can.c
* 程序作者: MCD Application Team
* 程序版本: V2.0.2
* 编制日期: 07/11/2008
* 功能描述: 这个文件提供了所有CAN的固件函数.
* 本文汉化: 孙可 2008.08.15
********************************************************************************
* 目前的固件的目的是为客户提供关于他们的产品的编码信息以便于节省他们时间。
* 结果, 意法半导体将不会为此承担任何直接,间接或相应的损害赔偿方面的任何索赔要求,
* 例如固件或使用所作的客户编码中他涉嫌包含与他们的相关产品的信息。
*******************************************************************************/

/* 包涵的文件 ------------------------------------------------------------------*/
#include "stm32f10x_can.h"
#include "stm32f10x_rcc.h"

/* 自用类型 -----------------------------------------------------------*/

/* 自用定义 ------------------------------------------------------------*/
/* CAN Master Control Register bits [CAN主控寄存器位]*/
#define CAN_MCR_INRQ     ((u32)0x00000001) /* Initialization request [初始化请求]*/
#define CAN_MCR_SLEEP    ((u32)0x00000002) /* Sleep mode request [睡眠模式请求]*/
#define CAN_MCR_TXFP     ((u32)0x00000004) /* Transmit FIFO priority [发送FIFO优先级]*/
#define CAN_MCR_RFLM     ((u32)0x00000008) /* Receive FIFO locked mode [接收FIFO锁定模式]*/
#define CAN_MCR_NART     ((u32)0x00000010) /* No automatic retransmission [非自动重发]*/
#define CAN_MCR_AWUM     ((u32)0x00000020) /* Automatic wake up mode [自动唤醒模式]*/
#define CAN_MCR_ABOM     ((u32)0x00000040) /* Automatic bus-off management [自动总线关断管理]*/
#define CAN_MCR_TTCM     ((u32)0x00000080) /* time triggered communication [定时器触发通讯]*/

/* CAN Master Status Register bits [CAN主控模式状态寄存器位]*/
#define CAN_MSR_INAK     ((u32)0x00000001)    /* Initialization acknowledge [初始化确认]*/
#define CAN_MSR_WKUI     ((u32)0x00000008)    /* Wake-up interrupt [唤醒中断]*/
#define CAN_MSR_SLAKI    ((u32)0x00000010)    /* Sleep acknowledge interrupt [睡眠确认中断]*/

/* CAN Transmit Status Register bits [CAN发送状态寄存器位]*/
#define CAN_TSR_RQCP0    ((u32)0x00000001)    /* Request completed mailbox0 [信箱0完成请求]*/
#define CAN_TSR_TXOK0    ((u32)0x00000002)    /* Transmission OK of mailbox0 [信箱0发送OK]*/
#define CAN_TSR_ABRQ0    ((u32)0x00000080)    /* Abort request for mailbox0 [信箱0请求失败]*/
#define CAN_TSR_RQCP1    ((u32)0x00000100)    /* Request completed mailbox1 [信箱1完成请求]*/
#define CAN_TSR_TXOK1    ((u32)0x00000200)    /* Transmission OK of mailbox1 [信箱1发送OK]*/
#define CAN_TSR_ABRQ1    ((u32)0x00008000)    /* Abort request for mailbox1 [信箱1请求失败]*/
#define CAN_TSR_RQCP2    ((u32)0x00010000)    /* Request completed mailbox2 [信箱2完成请求]*/
#define CAN_TSR_TXOK2    ((u32)0x00020000)    /* Transmission OK of mailbox2 [信箱1发送OK]*/
#define CAN_TSR_ABRQ2    ((u32)0x00800000)    /* Abort request for mailbox2 [信箱2请求失败]*/
#define CAN_TSR_TME0     ((u32)0x04000000)    /* Transmit mailbox 0 empty [发送信箱0空]*/
#define CAN_TSR_TME1     ((u32)0x08000000)    /* Transmit mailbox 1 empty [发送信箱1空]*/
#define CAN_TSR_TME2     ((u32)0x10000000)    /* Transmit mailbox 2 empty [发送信箱2空]*/

/* CAN Receive FIFO 0 Register bits [CAN接收FIFO 0寄存器位]*/
#define CAN_RF0R_FULL0   ((u32)0x00000008)    /* FIFO 0 full [FIFO 0满]*/
#define CAN_RF0R_FOVR0   ((u32)0x00000010)    /* FIFO 0 overrun [FIFO 0溢出]*/
#define CAN_RF0R_RFOM0   ((u32)0x00000020)    /* Release FIFO 0 output mailbox [释放FIFO 0输出信箱]*/

/* CAN Receive FIFO 1 Register bits [CAN接收FIFO 1寄存器位]*/
#define CAN_RF1R_FULL1   ((u32)0x00000008)    /* FIFO 1 full [FIFO 1满]*/
#define CAN_RF1R_FOVR1   ((u32)0x00000010)    /* FIFO 1 overrun [FIFO 1溢出]*/
#define CAN_RF1R_RFOM1   ((u32)0x00000020)    /* Release FIFO 1 output mailbox [释放FIFO 1输出信箱]*/

/* CAN Error Status Register bits [CAN错误状态寄存器位]*/
#define CAN_ESR_EWGF     ((u32)0x00000001)    /* Error warning flag [错误警告标志]*/
#define CAN_ESR_EPVF     ((u32)0x00000002)    /* Error passive flag [错误被动标志]*/
#define CAN_ESR_BOFF     ((u32)0x00000004)    /* Bus-off flag [总线关断标志]*/

/* CAN Mailbox Transmit Request [CAN信箱发送请求]*/
#define CAN_TMIDxR_TXRQ    ((u32)0x00000001) /* Transmit mailbox request [发送信箱请求]*/

/* CAN Filter Master Register bits [CAN滤波器主寄存器位]*/
#define CAN_FMR_FINIT ((u32)0x00000001) /* Filter init mode [滤波器初始化模式]*/


/* 自用宏 -------------------------------------------------------------*/
/* 自用变量 ---------------------------------------------------------*/
/* 自用函数原型 -----------------------------------------------*/
static ITStatus CheckITStatus(u32 CAN_Reg, u32 It_Bit);

/* 自用函数 ---------------------------------------------------------*/
/*******************************************************************************
* 函数名称: CAN_DeInit
* 功能描述: D将CAN外设寄存器复位为它们的默认值
* 输入参数: 无.
* 输出参数: 无.
* 返回参数: 无.
*******************************************************************************/
void CAN_DeInit(void)
{
  /* Enable CAN reset state */
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, ENABLE);
  /* Release CAN from reset state */
  RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, DISABLE);
}

/*******************************************************************************
* 函数名称: CAN_Init
* 功能描述: 根据CAN_InitStruct中指定的参数初始化CAN外围模块。
* 输入参数: CAN_InitStruct: 指向结构体CAN_InitTypeDef的指针,包含了CAN外围模块的配置信息。
* 输出参数: 无.
* 返回参数: 一个说明CAN初始化是否成功的常量。CANINITFAILED = 初始化失败
*                                            CANINITOK = 初始化成功
*******************************************************************************/
u8 CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
{
  u8 InitStatus = 0;
  u16 WaitAck = 0;

  /* Check the parameters [检查参数]*/
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
  assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
  assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
  assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
  assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
  assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
  assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));

  /* Request initialisation [请求初始化]*/
  CAN->MCR = CAN_MCR_INRQ;

  /* ...and check acknowledged [检测应答]*/
  if ((CAN->MSR & CAN_MSR_INAK) == 0)
  {
    InitStatus = CANINITFAILED;
  }
  else
  {
    /* Set the time triggered communication mode [设置定时器触发通讯模式]*/
    if (CAN_InitStruct->CAN_TTCM == ENABLE)
    {
      CAN->MCR |= CAN_MCR_TTCM;
    }
    else
    {
      CAN->MCR &= ~CAN_MCR_TTCM;
    }

    /* Set the automatic bus-off management [设置自动总线关闭管理]*/
    if (CAN_InitStruct->CAN_ABOM == ENABLE)
    {
      CAN->MCR |= CAN_MCR_ABOM;
    }
    else
    {
      CAN->MCR &= ~CAN_MCR_ABOM;
    }

    /* Set the automatic wake-up mode [设置自动唤醒模式]*/
    if (CAN_InitStruct->CAN_AWUM == ENABLE)
    {
      CAN->MCR |= CAN_MCR_AWUM;
    }
    else
    {
      CAN->MCR &= ~CAN_MCR_AWUM;
    }

    /* Set the no automatic retransmission [设置非自动重发]*/
    if (CAN_InitStruct->CAN_NART == ENABLE)
    {
      CAN->MCR |= CAN_MCR_NART;
    }
    else
    {
      CAN->MCR &= ~CAN_MCR_NART;
    }

    /* Set the receive FIFO locked mode [设置接收FIFO锁定模式]*/
    if (CAN_InitStruct->CAN_RFLM == ENABLE)
    {
      CAN->MCR |= CAN_MCR_RFLM;
    }
    else
    {
      CAN->MCR &= ~CAN_MCR_RFLM;
    }

    /* Set the transmit FIFO priority [设置发送FIFO优先级]*/
    if (CAN_InitStruct->CAN_TXFP == ENABLE)
    {
      CAN->MCR |= CAN_MCR_TXFP;
    }
    else
    {
      CAN->MCR &= ~CAN_MCR_TXFP;
    }

    /* Set the bit timing register [设置定时器的位]*/
    CAN->BTR = (u32)((u32)CAN_InitStruct->CAN_Mode << 30) | ((u32)CAN_InitStruct->CAN_SJW << 24) |
               ((u32)CAN_InitStruct->CAN_BS1 << 16) | ((u32)CAN_InitStruct->CAN_BS2 << 20) |
               ((u32)CAN_InitStruct->CAN_Prescaler - 1);

    InitStatus = CANINITOK;

    /* Request leave initialisation [请求退出初始化]*/
    CAN->MCR &= ~CAN_MCR_INRQ;

    /* Wait the acknowledge [等待肯定的应答]*/
    for(WaitAck = 0x400; WaitAck > 0x0; WaitAck--)
    {
    }
    
    /* ...and check acknowledged [检测应答]*/
    if ((CAN->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
    {
      InitStatus = CANINITFAILED;
    }
  }

  /* At this step, return the status of initialization [在这步,返回初始化状态]*/
  return InitStatus;
}

/*******************************************************************************
* 函数名称: CAN_FilterInit
* 功能描述: 根据CAN_FilterInitStruct中指定的参数初始化CAN外围模块。
* 输入参数: CAN_FilterInitStruct: 指向结构体CAN_FilterInitTypeDef的指针,它包含了配置信息。
* 输出参数: 无.
* 返回参数: 无.
*******************************************************************************/
void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
{
  u16 FilterNumber_BitPos = 0;

  /* Check the parameters [检查参数]*/
  assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
  assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
  assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
  assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
  assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));

  FilterNumber_BitPos = 
  (u16)((u16)0x0001 << ((u16)CAN_FilterInitStruct->CAN_FilterNumber));

  /* Initialisation mode for the filter [初始化滤波器模式]*/
  CAN->FMR |= CAN_FMR_FINIT;

  /* Filter Deactivation [禁止滤波器]*/
  CAN->FA1R &= ~(u32)FilterNumber_BitPos;

  /* Filter Scale [滤波器尺度]*/
  if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
  {
    /* 16-bit scale for the filter [16位的滤波器尺]*/
    CAN->FS1R &= ~(u32)FilterNumber_BitPos;

    /* First 16-bit identifier and First 16-bit mask [第一个16位标示符和第一个16位的屏蔽]*/
    /* Or First 16-bit identifier and Second 16-bit identifier [或第一个16位标示符和第二个16位的标示符]*/
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = 
    ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);

    /* Second 16-bit identifier and Second 16-bit mask [第二个16位标示符和第一个16位的屏蔽]*/
    /* Or Third 16-bit identifier and Fourth 16-bit identifier [或第三个16位标示符和第四个16位的标示符]*/
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 = 
    ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh);
  }
  if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
  {
    /* 32-bit scale for the filter [32位的滤波器尺]*/
    CAN->FS1R |= FilterNumber_BitPos;

    /* 32-bit identifier or First 32-bit identifier [32位标示符和第一个32位的标示符]*/
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = 
    ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);

    /* 32-bit mask or Second 32-bit identifier [32位的屏蔽或第二个32位标示符]*/
    CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 = 
    ((u32)((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
        ((u32)0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow);

  }

  /* Filter Mode [滤波器模式]*/
  if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
  {
    /*Id/Mask mode for the filter[滤波器Id/Mask模式]*/
    CAN->FM1R &= ~(u32)FilterNumber_BitPos;
  }
  else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
  {
    /*Identifier list mode for the filter滤波器标示符列表模式*/
    CAN->FM1R |= (u32)FilterNumber_BitPos;
  }

  /* Filter FIFO assignment [滤波器FIFO分配]*/
  if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO0)
  {
    /* FIFO 0 assignation for the filter [FIFO 0分配给滤波器]*/
    CAN->FFA1R &= ~(u32)FilterNumber_BitPos;
  }
  if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO1)
  {
    /* FIFO 1 assignation for the filter [FIFO 1分配给滤波器]*/

⌨️ 快捷键说明

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