📄 can.c
字号:
/********************************************************************
* Project: STM32-Stick
* File: can.c
*
* System: Cortex M3
* Compiler: TASKING
*
* Date: 2007-04-9
* Author: Application@Hitex.de
*
* Rights: Hitex Development Tools GmbH
* Greschbachstr. 12
* D-76229 Karlsruhe
********************************************************************
* Description:
*
* This file is part of the STM32-Stick Example chain
* The code is based on usage of the STmicro library functions
* This is a small implementation of different features
* The application runs in Thumb mode with high optimization level.
*
********************************************************************
* History:
*
* Revision 1.0 2006/12/20 Gn Initial revision
* Revision 1.1 2007/04/9 HS Updated for STM32-Stick
*
********************************************************************
* This is a preliminary version.
*
* WARRANTY: HITEX warrants that the media on which the SOFTWARE is
* furnished is free from defects in materials and workmanship under
* normal use and service for a period of ninety (90) days. HITEX entire
* liability and your exclusive remedy shall be the replacement of the
* SOFTWARE if the media is defective. This Warranty is void if failure
* of the media resulted from unauthorized modification, accident, abuse,
* or misapplication.
*
* DISCLAIMER: OTHER THAN THE ABOVE WARRANTY, THE SOFTWARE IS FURNISHED
* "AS IS" WITHOUT WARRANTY OF ANY KIND. HITEX DISCLAIMS ALL OTHER WARRANTIES,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* NEITHER HITEX NOR ITS AFFILIATES SHALL BE LIABLE FOR ANY DAMAGES ARISING
* OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, INCLUDING DAMAGES FOR
* LOSS OF PROFITS, BUSINESS INTERRUPTION, OR ANY SPECIAL, INCIDENTAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES EVEN IF HITEX HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
********************************************************************/
#include "system.h"
#include "can.h"
#include "time48.h"
/* Private typedef -----------------------------------------------------------*/
typedef unsigned long long u64;
#define global extern /* to declare external variables and functions */
/* Private variables ---------------------------------------------------------*/
ucan_msg_t ucan_msg;
u8 CAN_Mode = Prot_CAN_Mode_LoopSilent;
TaskInfo CANState = {FALSE, FALSE};
static u64 CAN_StartTimerValue = 0LL;
volatile u16 cangenerate_countdown = 0; // Timer
volatile u16 cangenerate_repeat = 0; // Reload value
void CAN_ProtocolInit(void)
{
cangenerate_repeat = 0;
}
/*******************************************************************************
* Function Name : TIMER_SystTickInit
* Description : Return the current SysTick elapsed time in microseconds
* Input : None
* Output : None
* Return : None
*******************************************************************************/
u64 TIMER_ElapsedTime(void)
{
return (TimeTick * 1000LL) + (SysTick_GetCounter() / 4);
}
/*******************************************************************************
* Function Name : CAN_Start
* Description : Start CAN
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_Start(void)
{
if (CANState.enabled)
{
return;
}
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
AFIO->MAPR &= ~0x6000;
AFIO->MAPR |= 0x4000; /*Remap Can to PB8 and PB9*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN,ENABLE);
/* CAN register init */
CAN_DeInit();
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler= 20;
switch (CAN_Mode)
{
case Prot_CAN_Mode_Normal:
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
break;
case Prot_CAN_Mode_Silent:
CAN_InitStructure.CAN_Mode=CAN_Mode_Silent;
break;
case Prot_CAN_Mode_Loopback:
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
break;
case Prot_CAN_Mode_LoopSilent:
CAN_InitStructure.CAN_Mode=CAN_Mode_Silent_LoopBack;
break;
default :
break;
}
CAN_Init(&CAN_InitStructure);
/* CAN filter init */
CAN_FilterInitStructure.CAN_FilterNumber=1;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* CAN FIFO0 message pending interrupt enable */
CAN_ITConfig(CAN_IT_FMP0, ENABLE);
CANState.enabled = TRUE;
CAN_StartTimerValue = TIMER_ElapsedTime();
CAN_ProtocolInit();
}
/*******************************************************************************
* Function Name : CAN_Stop
* Description : Stop CAN
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_Stop(void)
{
if (!CANState.enabled)
{
return;
}
CAN_DeInit();
/* disable interrupt handling */
CAN_ITConfig(CAN_IT_FMP0, DISABLE);
CANState.enabled = FALSE;
}
/*******************************************************************************
* Function Name : Send_UCAN_frame
* Description : Send UCAN frame
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void Send_UCAN_frame (u8 ucan_id, u8 *ucan_data, u16 ucan_len)
{
int i;
u8 len = 0; // Data length
u8 data[64]; // Data buffer
u8 checksum = 0; // Checksum
data[len++] = ucan_id; // UCAN ID
data[len++] = (u8)ucan_len; // UCAN Length l
data[len++] = (u8)(ucan_len>>8); // UCAN Length h
for (i = 0; i < ucan_len; i++)
{
data[len++] = ucan_data[i]; // UCAN Data
}
for (i = 0; i < len; i++)
{
checksum -= data[len]; // Calculate Checksum
}
data[len++] = checksum; // UCAN Checksum
protocol_SendFrame (Rep_CAN_Msg, data, len);
}
/*******************************************************************************
* Function Name : Message_to_UART
* Description : Send UCAN message
* Input : None
* Output : None
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -