rt-thread-official/bsp/synwit/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_can.c

605 lines
27 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : SWM320_can.c
2021-09-13 21:04:42 +08:00
* : SWM320单片机的CAN模块驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
2021-02-18 13:29:12 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
2021-09-13 21:04:42 +08:00
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
2021-02-18 13:29:12 +08:00
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_can.h"
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_Init()
* : CAN接口初始化
* : CAN_TypeDef * CANx CAN接口CAN
* CAN_InitStructure * initStruct CAN接口相关设定值的结构体
2021-05-06 10:42:41 +08:00
* :
* :
******************************************************************************************************************************************/
void CAN_Init(CAN_TypeDef * CANx, CAN_InitStructure * initStruct)
2021-05-06 10:42:41 +08:00
{
switch((uint32_t)CANx)
2021-05-06 10:42:41 +08:00
{
case ((uint32_t)CAN):
SYS->CLKEN |= (0x01 << SYS_CLKEN_CAN_Pos);
break;
}
CAN_Close(CANx); //一些关键寄存器只能在CAN关闭时设置
2021-05-06 10:42:41 +08:00
CANx->CR &= ~(CAN_CR_LOM_Msk | CAN_CR_STM_Msk | CAN_CR_AFM_Msk);
CANx->CR |= (initStruct->Mode << CAN_CR_LOM_Pos) |
(initStruct->FilterMode << CAN_CR_AFM_Pos);
CANx->FILTER.AMR[3] = initStruct->FilterMask32b & 0xFF;
CANx->FILTER.AMR[2] = (initStruct->FilterMask32b >> 8) & 0xFF;
2021-05-06 10:42:41 +08:00
CANx->FILTER.AMR[1] = (initStruct->FilterMask32b >> 16) & 0xFF;
CANx->FILTER.AMR[0] = (initStruct->FilterMask32b >> 24) & 0xFF;
CANx->FILTER.ACR[3] = initStruct->FilterCheck32b & 0xFF;
CANx->FILTER.ACR[2] = (initStruct->FilterCheck32b >> 8) & 0xFF;
2021-05-06 10:42:41 +08:00
CANx->FILTER.ACR[1] = (initStruct->FilterCheck32b >> 16) & 0xFF;
CANx->FILTER.ACR[0] = (initStruct->FilterCheck32b >> 24) & 0xFF;
CANx->BT1 = (0 << CAN_BT1_SAM_Pos) |
(initStruct->CAN_BS1 << CAN_BT1_TSEG1_Pos) |
(initStruct->CAN_BS2 << CAN_BT1_TSEG2_Pos);
CANx->BT0 = (initStruct->CAN_SJW << CAN_BT0_SJW_Pos) |
((SystemCoreClock/2/initStruct->Baudrate/(1 + (initStruct->CAN_BS1 + 1) + (initStruct->CAN_BS2 + 1)) - 1) << CAN_BT0_BRP_Pos);
2021-05-06 10:42:41 +08:00
CANx->RXERR = 0; //只能在复位模式下清除
2021-05-06 10:42:41 +08:00
CANx->TXERR = 0;
CANx->IE = (initStruct->RXNotEmptyIEn << CAN_IE_RXDA_Pos) |
(initStruct->RXOverflowIEn << CAN_IE_RXOV_Pos) |
2021-05-06 10:42:41 +08:00
(initStruct->ArbitrLostIEn << CAN_IE_ARBLOST_Pos) |
(initStruct->ErrPassiveIEn << CAN_IE_ERRPASS_Pos);
switch((uint32_t)CANx)
2021-05-06 10:42:41 +08:00
{
case ((uint32_t)CAN):
if(initStruct->RXNotEmptyIEn | initStruct->RXOverflowIEn | initStruct->ArbitrLostIEn | initStruct->ErrPassiveIEn)
2021-05-06 10:42:41 +08:00
{
NVIC_EnableIRQ(CAN_IRQn);
}
else
{
NVIC_DisableIRQ(CAN_IRQn);
}
break;
}
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_Open()
* : CAN接口打开
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
******************************************************************************************************************************************/
void CAN_Open(CAN_TypeDef * CANx)
2021-05-06 10:42:41 +08:00
{
CANx->CR &= ~(0x01 << CAN_CR_RST_Pos); //退出复位模式,进入工作模式
2021-05-06 10:42:41 +08:00
}
2021-02-18 13:29:12 +08:00
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_Close()
* : CAN接口关闭
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
******************************************************************************************************************************************/
void CAN_Close(CAN_TypeDef * CANx)
2021-05-06 10:42:41 +08:00
{
CANx->CR |= (0x01 << CAN_CR_RST_Pos); //进入复位模式,不能发送和接收数据
2021-05-06 10:42:41 +08:00
}
2021-02-18 13:29:12 +08:00
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_Transmit()
* : CAN发送数据
* : CAN_TypeDef * CANx CAN接口CAN
* uint32_t format CAN_FRAME_STD CAN_FRAME_EXT
* uint32_t id ID
* uint8_t data[]
* uint32_t size
* uint32_t once 使NAK
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_Transmit(CAN_TypeDef * CANx, uint32_t format, uint32_t id, uint8_t data[], uint32_t size, uint32_t once)
2021-05-06 10:42:41 +08:00
{
uint32_t i;
if(format == CAN_FRAME_STD)
2021-05-06 10:42:41 +08:00
{
CANx->FRAME.INFO = (0 << CAN_INFO_FF_Pos) |
2021-09-13 20:19:14 +08:00
(0 << CAN_INFO_RTR_Pos) |
(size << CAN_INFO_DLC_Pos);
2021-05-06 10:42:41 +08:00
2021-09-13 20:19:14 +08:00
CANx->FRAME.DATA[0] = id >> 3;
CANx->FRAME.DATA[1] = id << 5;
2021-05-06 10:42:41 +08:00
for(i = 0; i < size; i++)
2021-05-06 10:42:41 +08:00
{
CANx->FRAME.DATA[i+2] = data[i];
2021-05-06 10:42:41 +08:00
}
}
else //if(format == CAN_FRAME_EXT)
{
CANx->FRAME.INFO = (1 << CAN_INFO_FF_Pos) |
2021-09-13 20:19:14 +08:00
(0 << CAN_INFO_RTR_Pos) |
(size << CAN_INFO_DLC_Pos);
2021-05-06 10:42:41 +08:00
2021-09-13 20:19:14 +08:00
CANx->FRAME.DATA[0] = id >> 21;
CANx->FRAME.DATA[1] = id >> 13;
CANx->FRAME.DATA[2] = id >> 5;
CANx->FRAME.DATA[3] = id << 3;
2021-05-06 10:42:41 +08:00
for(i = 0; i < size; i++)
2021-05-06 10:42:41 +08:00
{
CANx->FRAME.DATA[i+4] = data[i];
2021-05-06 10:42:41 +08:00
}
}
if(CANx->CR & CAN_CR_STM_Msk)
2021-05-06 10:42:41 +08:00
{
CANx->CMD = (1 << CAN_CMD_SRR_Pos);
}
else
{
if(once == 0)
2021-05-06 10:42:41 +08:00
{
CANx->CMD = (1 << CAN_CMD_TXREQ_Pos);
}
else
{
CANx->CMD = (1 << CAN_CMD_TXREQ_Pos) | (1 << CAN_CMD_ABTTX_Pos);
}
}
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_TransmitRequest()
* : CAN发送远程请求
* : CAN_TypeDef * CANx CAN接口CAN
* uint32_t format CAN_FRAME_STD CAN_FRAME_EXT
* uint32_t id ID
* uint32_t once 使NAK
2021-05-06 10:42:41 +08:00
* :
* :
******************************************************************************************************************************************/
void CAN_TransmitRequest(CAN_TypeDef * CANx, uint32_t format, uint32_t id, uint32_t once)
2021-05-06 10:42:41 +08:00
{
if(format == CAN_FRAME_STD)
2021-05-06 10:42:41 +08:00
{
CANx->FRAME.INFO = (0 << CAN_INFO_FF_Pos) |
2021-09-13 20:19:14 +08:00
(1 << CAN_INFO_RTR_Pos) |
(0 << CAN_INFO_DLC_Pos);
2021-05-06 10:42:41 +08:00
2021-09-13 20:19:14 +08:00
CANx->FRAME.DATA[0] = id >> 3;
CANx->FRAME.DATA[1] = id << 5;
2021-05-06 10:42:41 +08:00
}
else //if(format == CAN_FRAME_EXT)
{
CANx->FRAME.INFO = (1 << CAN_INFO_FF_Pos) |
2021-09-13 20:19:14 +08:00
(1 << CAN_INFO_RTR_Pos) |
(0 << CAN_INFO_DLC_Pos);
2021-05-06 10:42:41 +08:00
2021-09-13 20:19:14 +08:00
CANx->FRAME.DATA[0] = id >> 21;
CANx->FRAME.DATA[1] = id >> 13;
CANx->FRAME.DATA[2] = id >> 5;
CANx->FRAME.DATA[3] = id << 3;
2021-05-06 10:42:41 +08:00
}
if(once == 0)
2021-05-06 10:42:41 +08:00
{
CANx->CMD = (1 << CAN_CMD_TXREQ_Pos);
}
else
{
CANx->CMD = (1 << CAN_CMD_TXREQ_Pos) | (1 << CAN_CMD_ABTTX_Pos);
}
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_Receive()
* : CAN接收数据
* : CAN_TypeDef * CANx CAN接口CAN
* CAN_RXMessage *msg
2021-05-06 10:42:41 +08:00
* :
* :
******************************************************************************************************************************************/
void CAN_Receive(CAN_TypeDef * CANx, CAN_RXMessage *msg)
2021-05-06 10:42:41 +08:00
{
uint32_t i;
2021-09-13 20:19:14 +08:00
msg->format = (CANx->FRAME.INFO & CAN_INFO_FF_Msk) >> CAN_INFO_FF_Pos;
2021-05-06 10:42:41 +08:00
2021-09-13 20:19:14 +08:00
msg->remote = (CANx->FRAME.INFO & CAN_INFO_RTR_Msk) >> CAN_INFO_RTR_Pos;
msg->size = (CANx->FRAME.INFO & CAN_INFO_DLC_Msk) >> CAN_INFO_DLC_Pos;
2021-05-06 10:42:41 +08:00
if(msg->format == CAN_FRAME_STD)
2021-05-06 10:42:41 +08:00
{
2021-09-13 20:19:14 +08:00
msg->id = (CANx->FRAME.DATA[0] << 3) | (CANx->FRAME.DATA[1] >> 5);
2021-05-06 10:42:41 +08:00
for(i = 0; i < msg->size; i++)
2021-05-06 10:42:41 +08:00
{
msg->data[i] = CANx->FRAME.DATA[i+2];
2021-05-06 10:42:41 +08:00
}
}
else //if(msg->format == CAN_FRAME_EXT)
{
2021-09-13 20:19:14 +08:00
msg->id = (CANx->FRAME.DATA[0] << 21) | (CANx->FRAME.DATA[1] << 13) | (CANx->FRAME.DATA[2] << 5) | (CANx->FRAME.DATA[3] >> 3);
2021-05-06 10:42:41 +08:00
for(i = 0; i < msg->size; i++)
2021-05-06 10:42:41 +08:00
{
msg->data[i] = CANx->FRAME.DATA[i+4];
2021-05-06 10:42:41 +08:00
}
}
CANx->CMD = (1 << CAN_CMD_RRB_Pos);
}
2021-02-18 13:29:12 +08:00
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_TXComplete()
* :
* : CAN_TypeDef * CANx CAN接口CAN
* : uint32_t 1 0
2021-05-06 10:42:41 +08:00
* : Abort也会触发发送完成
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t CAN_TXComplete(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (CANx->SR & CAN_SR_TXBR_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_TXSuccess()
* :
* : CAN_TypeDef * CANx CAN接口CAN
* : uint32_t 1 0
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t CAN_TXSuccess(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (CANx->SR & CAN_SR_TXOK_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_AbortTransmit()
* :
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_AbortTransmit(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->CMD = (1 << CAN_CMD_ABTTX_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_TXBufferReady()
* : TX Buffer是否准备好可以写入消息
* : CAN_TypeDef * CANx CAN接口CAN
* : uint32_t 1 0
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t CAN_TXBufferReady(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (CANx->SR & CAN_SR_TXBR_Msk) ? 1 : 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_RXDataAvailable()
* : RX FIFO中是否有数据可读出
* : CAN_TypeDef * CANx CAN接口CAN
* : uint32_t 1 0
2021-05-06 10:42:41 +08:00
* :
******************************************************************************************************************************************/
uint32_t CAN_RXDataAvailable(CAN_TypeDef * CANx)
2021-05-06 10:42:41 +08:00
{
return (CANx->SR & CAN_SR_RXDA_Msk) ? 1 : 0;
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_SetBaudrate()
* :
* : CAN_TypeDef * CANx CAN接口CAN
* uint32_t baudrate
* uint32_t CAN_BS1 CAN_BS1_1tqCAN_BS1_2tq... ... CAN_BS1_16tq
* uint32_t CAN_BS2 CAN_BS2_1tqCAN_BS2_2tq... ... CAN_BS2_8tq
* uint32_t CAN_SJW CAN_SJW_1tqCAN_SJW_2tqCAN_SJW_3tqCAN_SJW_4tq
2021-05-06 10:42:41 +08:00
* :
* : CAN_Close()CAN模块
******************************************************************************************************************************************/
void CAN_SetBaudrate(CAN_TypeDef * CANx, uint32_t baudrate, uint32_t CAN_BS1, uint32_t CAN_BS2, uint32_t CAN_SJW)
2021-05-06 10:42:41 +08:00
{
CANx->BT1 = (0 << CAN_BT1_SAM_Pos) |
(CAN_BS1 << CAN_BT1_TSEG1_Pos) |
(CAN_BS2 << CAN_BT1_TSEG2_Pos);
CANx->BT0 = (CAN_SJW << CAN_BT0_SJW_Pos) |
((SystemCoreClock/2/baudrate/(1 + (CAN_BS1 + 1) + (CAN_BS2 + 1)) - 1) << CAN_BT0_BRP_Pos);
2021-05-06 10:42:41 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_SetFilter32b()
* : 132
* : CAN_TypeDef * CANx CAN接口CAN
* uint32_t check mask一起决定了接收到的Message是否是自己需要的check & (~mask) == ID & (~mask)Message通过过滤
* uint32_t mask
2021-05-06 10:42:41 +08:00
* :
* : CAN_Close()CAN模块
******************************************************************************************************************************************/
void CAN_SetFilter32b(CAN_TypeDef * CANx, uint32_t check, uint32_t mask)
2021-05-06 10:42:41 +08:00
{
CANx->CR &= ~CAN_CR_AFM_Msk;
CANx->CR |= (CAN_FILTER_32b << CAN_CR_AFM_Pos);
CANx->FILTER.AMR[3] = mask & 0xFF;
CANx->FILTER.AMR[2] = (mask >> 8) & 0xFF;
2021-05-06 10:42:41 +08:00
CANx->FILTER.AMR[1] = (mask >> 16) & 0xFF;
CANx->FILTER.AMR[0] = (mask >> 24) & 0xFF;
CANx->FILTER.ACR[3] = check & 0xFF;
CANx->FILTER.ACR[2] = (check >> 8) & 0xFF;
2021-05-06 10:42:41 +08:00
CANx->FILTER.ACR[1] = (check >> 16) & 0xFF;
CANx->FILTER.ACR[0] = (check >> 24) & 0xFF;
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_SetFilter16b()
* : 216
* : CAN_TypeDef * CANx CAN接口CAN
* uint16_t check1 mask一起决定了接收到的Message是否是自己需要的check & (~mask) == ID & (~mask)Message通过过滤
* uint16_t mask1
* uint16_t check2
* uint16_t mask2
2021-05-06 10:42:41 +08:00
* :
* : CAN_Close()CAN模块
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_SetFilter16b(CAN_TypeDef * CANx, uint16_t check1, uint16_t mask1, uint16_t check2, uint16_t mask2)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->CR &= ~CAN_CR_AFM_Msk;
CANx->CR |= (CAN_FILTER_16b << CAN_CR_AFM_Pos);
CANx->FILTER.AMR[3] = mask1 & 0xFF;
CANx->FILTER.AMR[2] = (mask1 >> 8) & 0xFF;
CANx->FILTER.AMR[1] = mask2 & 0xFF;
CANx->FILTER.AMR[0] = (mask2 >> 8) & 0xFF;
2021-05-06 10:42:41 +08:00
CANx->FILTER.ACR[3] = check1 & 0xFF;
CANx->FILTER.ACR[2] = (check1 >> 8) & 0xFF;
CANx->FILTER.ACR[1] = check2 & 0xFF;
CANx->FILTER.ACR[0] = (check2 >> 8) & 0xFF;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTRXNotEmptyEn()
* : RX FIFO中有数据时使
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTRXNotEmptyEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_RXDA_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTRXNotEmptyDis()
* : RX FIFO中有数据时
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTRXNotEmptyDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_RXDA_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTTXBufEmptyEn()
* : TX Buffer空时触发中断使能
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTTXBufEmptyEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_TXBR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTTXBufEmptyDis()
* : TX Buffer空时触发中断禁止
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTTXBufEmptyDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_TXBR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTErrWarningEn()
* : TXERR/RXERR计数值达到Error Warning Limit时触发中断使能
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTErrWarningEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_ERRWARN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTErrWarningDis()
* : TXERR/RXERR计数值达到Error Warning Limit时触发中断禁止
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTErrWarningDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_ERRWARN_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTRXOverflowEn()
* : RX FIFO 使
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTRXOverflowEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_RXOV_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTRXOverflowDis()
* : RX FIFO
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTRXOverflowDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_RXOV_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTRXOverflowClear()
* : RX FIFO
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTRXOverflowClear(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->CMD = (1 << CAN_CMD_CLROV_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTWakeupEn()
* : 使
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTWakeupEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_WKUP_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTWakeupDis()
* :
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTWakeupDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_WKUP_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTErrPassiveEn()
* : TXERR/RXERR计数值达到127时中断使能
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTErrPassiveEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_ERRPASS_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTErrPassiveDis()
* : TXERR/RXERR计数值达到127时中断禁止
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTErrPassiveDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_ERRPASS_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTArbitrLostEn()
* : 使
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTArbitrLostEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_ARBLOST_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTArbitrLostDis()
* :
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTArbitrLostDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_ARBLOST_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTBusErrorEn()
* : 线使
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTBusErrorEn(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE |= (1 << CAN_IE_BUSERR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTBusErrorDis()
* : 线
* : CAN_TypeDef * CANx CAN接口CAN
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void CAN_INTBusErrorDis(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
CANx->IE &= ~(1 << CAN_IE_BUSERR_Pos);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : CAN_INTStat()
* :
* : CAN_TypeDef * CANx CAN接口CAN
* : uint32_t
2021-05-06 10:42:41 +08:00
* : CANx->IF读取清零ISR中只能读取一次
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t CAN_INTStat(CAN_TypeDef * CANx)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return CANx->IF;
2021-02-18 13:29:12 +08:00
}