#include "can.h" //eg_t55
#include "stdio.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_can.h"
volatile u8 can_tx_success_flag = 0;
void can1_init(void)
{
u8 ret;
GPIO_InitTypeDef GPIO_InitStruct;
CAN_InitTypeDef CAN_InitStruct;
CAN_FilterInitTypeDef CAN_FilterInitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
//APB1 36MHz
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_CAN_Rx;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
//GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_TYPE_CAN_Rx, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_CAN_Tx;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIO_TYPE_CAN_Tx, &GPIO_InitStruct);
//GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE);
//can mode related
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStruct);
CAN_InitStruct.CAN_TTCM = DISABLE;
CAN_InitStruct.CAN_ABOM = DISABLE;
CAN_InitStruct.CAN_AWUM = ENABLE;
CAN_InitStruct.CAN_NART = DISABLE;
CAN_InitStruct.CAN_RFLM = DISABLE;
CAN_InitStruct.CAN_TXFP = DISABLE;
CAN_InitStruct.CAN_Mode = CAN_Mode_Normal;
//bound = 36M/(1+6+1)/9 = 500kbps
CAN_InitStruct.CAN_SJW = CAN_SJW_1tq;
CAN_InitStruct.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStruct.CAN_BS2 = CAN_BS2_2tq;
CAN_InitStruct.CAN_Prescaler = 6;
ret = CAN_Init(CAN1, &CAN_InitStruct);
if(ret != CAN_InitStatus_Success)
printf("Failed to initiate CAN1.\n");
//CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
/*
CAN_FilterInitStruct.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStruct.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStruct.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStruct.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStruct.CAN_FilterMaskIdHigh= 0x0000;
CAN_FilterInitStruct.CAN_FilterMaskIdLow= 0x0000;
CAN_FilterInitStruct.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;
CAN_FilterInitStruct.CAN_FilterNumber = 1;
CAN_FilterInitStruct.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStruct);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_Init(&NVIC_InitStruct);
*/
}
u8 can_sent_msg(u32 id, u8* data)
{
u8 ret = CAN_TxStatus_NoMailBox;
CanTxMsg msg;
msg.StdId = id,
msg.ExtId = 0,
msg.IDE = CAN_Id_Standard,
msg.RTR = CAN_RTR_DATA;
msg.DLC = 8;
msg.Data[0] = data[0];
msg.Data[1] = data[1];
msg.Data[2] = data[2];
msg.Data[3] = data[3];
msg.Data[4] = data[4];
msg.Data[5] = data[5];
msg.Data[6] = data[6];
msg.Data[7] = data[7];
ret = CAN_Transmit(CAN1, &msg);
if(ret == CAN_TxStatus_NoMailBox)
printf("No mailbox.\n");
return ret;
}
void CAN1_RX0_IRQHandler(void)
{
int i;
ITStatus Status;
CanRxMsg msg;
Status = CAN_GetITStatus(CAN1, CAN_IT_FMP0);
if(Status == SET)
{
CAN_Receive(CAN1, CAN_FIFO0, &msg);
printf("id: %d, data:", msg.StdId);
for(i=0; i<8; i++)
printf("%d ", msg.Data[i]);
}
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
}