2019-02-07 03:23 AM
Hello.
I try init CAN bus. My CAN transmitter is MCP2551.
Connecting:
RX -> nucleoPB8
TX -> nucleo PB9
RS -> -----| 10K |----- GND
CANH - CANL 120R between
Vss - GND
Vdd - +5V
First step is:
init GPIO
void CAN_gpio(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
LL_GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = LL_GPIO_PIN_8 | LL_GPIO_PIN_9;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = LL_GPIO_AF_9;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
Second Step is init CAN bus:
void CANBUS_Init () {
L_AHB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_CAN1);
CAN_gpio();
CAN_FilterConfTypeDef sFilterConfig;
static CanTxMsgTypeDef TxMessage;
static CanRxMsgTypeDef RxMessage;
hcan.Instance = CAN1;
/**
* @param Freq_can = AHB1 = 25MHz
* @param Prescaler = 5
* @param BS1 = 2
* @param BS2 = 2
*
* @note tq = 1/(Freq_can/prescaler) = 1/(25E+6/5) = 0,2us
* @note Baudrate = 1/((1+BS1+BS2)*tq) = 1/((1+2+2)*0,2E-6) = 1000000 => 1Mbit/s
*/
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_2TQ;
hcan.Init.BS2 = CAN_BS2_2TQ;
hcan.Init.Prescaler = 5;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
if (CAN_Init(&hcan) != CAN_OK) {}
hcan.pTxMsg = &TxMessage;
hcan.pRxMsg = &RxMessage;
sFilterConfig.FilterNumber = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = 0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.BankNumber = 14;
if (CAN_ConfigFilter(&hcan, &sFilterConfig) != CAN_OK) {}
hcan.pTxMsg->StdId = 0x321;
hcan.pTxMsg->ExtId = 0x01;
hcan.pTxMsg->RTR = CAN_RTR_DATA;
hcan.pTxMsg->IDE = CAN_ID_STD;
hcan.pTxMsg->DLC = 1;
hcan.pTxMsg->Data[0] = 0;
}
And when I go to this line:
if (CAN_Init(&hcan) != CAN_OK)
I stop on this line:
while ((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK){
/* -TODO- timeout. Now only debug, init, ..... */
}
And I don't know why. Any idea, what is wrong? I try looked for mistake in hardware.
On picture is pins TXD and RXD on mcu side.
Yellow is: TXD
Green is: RXD
2019-02-07 09:57 PM
On the picture is I2C init. When I disable I2C, CAN bus is deaf. And I still stay on this line:
while ((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK){
/* -TODO- timeout. Now only debug, init, ..... */
}