#include "main.h"
CanTxMsg TxMessage = {0},TxMessage1={0};
CanRxMsg RxMessage0 = {0},RxMessage1={0};
uint16_t crc=0xffff;
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
TestStatus status;
uint32_t ret = 0; /* for return of the interrupt handling */
/* Private function prototypes -----------------------------------------------*/
static void CAN_Config(void);
void calcrc(uint8_t *crcbuf,uint8_t size,uint8_t count);
TestStatus Receive(CanRxMsg *RxMessage0);
TestStatus CAN_Polling(void);
TestStatus CAN_Interrupt(void);
int main(void)
{
TestStatus status;
/* CAN configuration */
CAN_Config();
status=CAN_Polling();
if(status)
{
CAN_Transmit(CAN1,&TxMessage);
}
status=CAN_Interrupt();
if(status)
{
CAN_Transmit(CAN1,&TxMessage);
}
}
static void CAN_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
// CAN_FilterInitTypeDef CAN_FilterInitStructure;
/* CAN GPIOs configuration **************************************************/
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE);
/* Connect CAN pins to AF7 */
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT);
/* Configure CAN RX and TX pins */
GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
/* NVIC configuration *******************************************************/
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* CAN configuration ********************************************************/
/* Enable CAN clock */
RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
/* CAN register init */
CAN_DeInit(CANx);
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_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
/* CAN Baudrate = 100KBps (CAN clocked at 36 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 20;
CAN_Init(CANx, &CAN_InitStructure);
/* CAN filter init "FIFO0" */
/*
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0001;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0001;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
*/
/* Transmit Structure preparation */
TxMessage.StdId = 0x1;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.IDE = CAN_ID_STD;
TxMessage.DLC = 8;
TxMessage.Data[0] = 0x01;
TxMessage.Data[1] = 0x03;
TxMessage.Data[2] = 0x00;
TxMessage.Data[3] = 0x25;
TxMessage.Data[4] = 0x00;
TxMessage.Data[5] = 0x03;
calcrc(&TxMessage.Data[0],20,6);
/* Enable FIFO 0 message pending Interrupt */
CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* CRC
*/
void calcrc(uint8_t *crcbuf,uint8_t size,uint8_t count)
{
uint8_t bit=0;
uint8_t i=0;
uint8_t pos=0;
if(size-count<2)
while(1);
while(pos<count)
{
crc ^= crcbuf[pos];
for(i=0; i<8; i++)
{
bit = crc&0x1;
crc >>= 1;
crc=crc & 0x7fff;
if(1==bit)
{
crc ^= 0xa001;
}
crc=crc &0xffff;
}
pos++;
}
crcbuf[pos++]=crc & 0x00ff;
crcbuf[pos]=crc & 0xff00;
}
/**
*
*/
TestStatus CAN_Polling(void)
{
uint8_t TransmitMailbox = 0;
int i=0;
TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFF))
{
i++;
}
i = 0;
while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFFFF))
{
i++;
}
/* receive */
RxMessage0.StdId = 0x00;
RxMessage0.IDE = CAN_ID_STD;
RxMessage0.DLC = 0;
RxMessage0.Data[0] = 0x00;
RxMessage0.Data[1] = 0x00;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage0);
if (RxMessage0.StdId != 0x01)
{
return FAILED;
}
if (RxMessage0.IDE != CAN_ID_STD)
{
return FAILED;
}
if (RxMessage0.DLC != 8)
{
return FAILED;
}
return PASSED; /* Test Passed */
}
TestStatus CAN_Interrupt(void)
{
uint32_t i = 0;
/* transmit 1 message */
TxMessage.StdId = 0;
TxMessage.IDE = CAN_ID_STD;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.DLC = 2;
TxMessage.Data[0] = 0xDE;
TxMessage.Data[1] = 0xCA;
CAN_Transmit(CAN1, &TxMessage);
/* initialize the value that will be returned */
ret = 0xFF;
/* Receive message with interrupt handling */
i = 0;
while((ret == 0xFF) && (i < 0xFFF))
{
i++;
}
if (i == 0xFFF)
{
ret = 0;
}
/* disable interrupt handling */
CAN_ITConfig(CAN1, CAN_IT_FMP0, DISABLE);
return (TestStatus)ret;
}
|