zhaijunyuan
5 years ago
8 changed files with 256 additions and 30 deletions
@ -0,0 +1,195 @@ |
|||
#include <stdio.h> |
|||
#include "stm32f4xx.h" |
|||
|
|||
/* query APB1 clocks */ |
|||
static uint32_t get_PCLK1_clocks() |
|||
{ |
|||
RCC_ClocksTypeDef clks; |
|||
|
|||
RCC_GetClocksFreq(&clks); |
|||
|
|||
return clks.PCLK1_Frequency; |
|||
} |
|||
|
|||
int can_set_baudrate(CAN_InitTypeDef *conf, unsigned int baudrate) |
|||
{ |
|||
int found = 0; |
|||
int calcBaudrate; |
|||
uint8_t sjw = 1, bs1 = 1, bs2 = 1; |
|||
uint16_t prescaler = 1; |
|||
uint32_t frequency; |
|||
|
|||
frequency = get_PCLK1_clocks(); |
|||
for (; sjw <= 4 && !found; ) { |
|||
for (; bs1 <= 16 && !found; ) { |
|||
for (; bs2 <= 8 && !found; ) { |
|||
for (; prescaler <= 1024 && !found; ) { |
|||
calcBaudrate = (int)(frequency / (prescaler * (sjw + bs1 + bs2))); |
|||
if (calcBaudrate == baudrate) { |
|||
if (sjw == 1) |
|||
conf->CAN_SJW = CAN_SJW_1tq; |
|||
else if (sjw == 2) |
|||
conf->CAN_SJW = CAN_SJW_2tq; |
|||
else if (sjw == 3) |
|||
conf->CAN_SJW = CAN_SJW_3tq; |
|||
else if (sjw == 4) |
|||
conf->CAN_SJW = CAN_SJW_4tq; |
|||
|
|||
if (bs1 == 1) |
|||
conf->CAN_BS1 = CAN_BS1_1tq; |
|||
else if (bs1 == 2) |
|||
conf->CAN_BS1 = CAN_BS1_2tq; |
|||
else if (bs1 == 3) |
|||
conf->CAN_BS1 = CAN_BS1_3tq; |
|||
else if (bs1 == 4) |
|||
conf->CAN_BS1 = CAN_BS1_4tq; |
|||
else if (bs1 == 5) |
|||
conf->CAN_BS1 = CAN_BS1_5tq; |
|||
else if (bs1 == 6) |
|||
conf->CAN_BS1 = CAN_BS1_6tq; |
|||
else if (bs1 == 7) |
|||
conf->CAN_BS1 = CAN_BS1_7tq; |
|||
else if (bs1 == 8) |
|||
conf->CAN_BS1 = CAN_BS1_8tq; |
|||
else if (bs1 == 9) |
|||
conf->CAN_BS1 = CAN_BS1_9tq; |
|||
else if (bs1 == 10) |
|||
conf->CAN_BS1 = CAN_BS1_10tq; |
|||
else if (bs1 == 11) |
|||
conf->CAN_BS1 = CAN_BS1_11tq; |
|||
else if (bs1 == 12) |
|||
conf->CAN_BS1 = CAN_BS1_12tq; |
|||
else if (bs1 == 13) |
|||
conf->CAN_BS1 = CAN_BS1_13tq; |
|||
else if (bs1 == 14) |
|||
conf->CAN_BS1 = CAN_BS1_14tq; |
|||
else if (bs1 == 15) |
|||
conf->CAN_BS1 = CAN_BS1_15tq; |
|||
else if (bs1 == 16) |
|||
conf->CAN_BS1 = CAN_BS1_16tq; |
|||
|
|||
if (bs2 == 1) |
|||
conf->CAN_BS2 = CAN_BS2_1tq; |
|||
else if (bs2 == 2) |
|||
conf->CAN_BS2 = CAN_BS2_2tq; |
|||
else if (bs2 == 3) |
|||
conf->CAN_BS2 = CAN_BS2_2tq; |
|||
else if (bs2 == 4) |
|||
conf->CAN_BS2 = CAN_BS2_2tq; |
|||
|
|||
conf->CAN_Prescaler = prescaler; |
|||
found = 1; |
|||
} |
|||
|
|||
prescaler++; |
|||
} |
|||
if (!found) { |
|||
prescaler = 1; |
|||
bs2++; |
|||
} |
|||
} |
|||
if (!found) { |
|||
bs2 = 1; |
|||
bs1++; |
|||
} |
|||
} |
|||
if (!found) { |
|||
bs1 = 1; |
|||
sjw++; |
|||
} |
|||
} |
|||
return found; |
|||
} |
|||
|
|||
void can_setup() |
|||
{ |
|||
GPIO_InitTypeDef GPIO_InitStructure; |
|||
CAN_InitTypeDef CAN_InitStructure; |
|||
CAN_FilterInitTypeDef CAN_FilterInitStructure; |
|||
|
|||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); |
|||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); |
|||
|
|||
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1); |
|||
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1); |
|||
|
|||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12; |
|||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
|||
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
|||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; |
|||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
|||
GPIO_Init(GPIOA, &GPIO_InitStructure); |
|||
|
|||
// CAN_DeInit(CAN1);
|
|||
// CAN_StructInit(&CAN_InitStructure);
|
|||
CAN_InitStructure.CAN_TTCM = DISABLE; |
|||
CAN_InitStructure.CAN_ABOM = DISABLE; |
|||
CAN_InitStructure.CAN_AWUM = DISABLE; |
|||
CAN_InitStructure.CAN_NART = ENABLE; |
|||
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_InitStructure.CAN_BS1 = CAN_BS1_3tq; |
|||
CAN_InitStructure.CAN_BS2 = CAN_BS2_1tq; |
|||
CAN_InitStructure.CAN_Prescaler = 9; |
|||
|
|||
printf("IN BS1=%d, BS2=%d, prescaler=%d\r\n", CAN_InitStructure.CAN_BS1, CAN_InitStructure.CAN_BS2, CAN_InitStructure.CAN_Prescaler); |
|||
// can_set_baudrate(&CAN_InitStructure, 200000);
|
|||
printf("OU BS1=%d, BS2=%d, prescaler=%d\r\n", CAN_InitStructure.CAN_BS1, CAN_InitStructure.CAN_BS2, CAN_InitStructure.CAN_Prescaler); |
|||
CAN_Init(CAN1, &CAN_InitStructure); |
|||
|
|||
CAN_FilterInitStructure.CAN_FilterNumber = 0; |
|||
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; |
|||
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; |
|||
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0009; |
|||
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000; |
|||
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0001; |
|||
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0001; |
|||
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0; |
|||
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; |
|||
CAN_FilterInit(&CAN_FilterInitStructure); |
|||
} |
|||
|
|||
int can_test() |
|||
{ |
|||
uint8_t mbox, buf; |
|||
int i; |
|||
CanTxMsg msg; |
|||
CanRxMsg msg1; |
|||
|
|||
msg.StdId = 0x11; |
|||
msg.IDE = CAN_ID_STD; |
|||
msg.RTR = CAN_RTR_Data; |
|||
msg.DLC = 0x01; |
|||
msg.Data[0] = 0x55; |
|||
|
|||
|
|||
mbox = CAN_Transmit(CAN1, &msg); |
|||
i = 0; |
|||
printf("mbox = %d\r\n",mbox); |
|||
while((CAN_TransmitStatus(CAN1, mbox) == CAN_TxStatus_Failed) && (i < 0xFFF)) |
|||
{ |
|||
i++; |
|||
// printf("delay\r\n");
|
|||
} |
|||
if(i > 0xFFF) |
|||
{ |
|||
printf("err 1\r\n"); |
|||
return mbox; |
|||
} |
|||
|
|||
i = 0; |
|||
while ((CAN_MessagePending(CAN1,CAN_FIFO0) == 0) && (i < 0xfff)) |
|||
{ |
|||
++i; |
|||
printf("err 2\r\n"); |
|||
return 0; |
|||
} |
|||
CAN_Receive(CAN1,CAN_FIFO0,&msg1); |
|||
buf = msg1.Data[0]; |
|||
printf("receive data = %x\n", buf); |
|||
return 0; |
|||
} |
|||
|
@ -0,0 +1,8 @@ |
|||
#ifndef CAN_H |
|||
#define CAN_H |
|||
|
|||
void can_setup(); |
|||
int can_test(); |
|||
|
|||
#endif |
|||
|
Loading…
Reference in new issue