hope this is the right spot to post this problem. I am trying to get CAN working on a custom board with a STM32F767. However, I am completely stuck and there is no progress anymore with try and error. Since the code of the project is not open, I am not able to share the whole project but I am able to share the needed code snippets.
I already studied the reference manual of this processor but I am not experienced with low level coding.
I hope this description is useful for you. I try to implement CAN like it is documented here:
https://github.com/stm32duino/Arduino_C ... _hal_can.c
I used this as help:
viewtopic.php?f=39&t=313&start=10
(I also tried to implement CAN using the following but was not able to get it working (first time using low level code and first time using CAN):
https://github.com/nopnop2002/Arduino-STM32-CAN
but as far as I understand, this has nothing to do with the implementation above.)
A MCP2551 is placed there (snipped from board layout) :
Snippets from variant.h of the custom board:
Code: Select all
#define HAL_CAN_MODULE_ENABLED
Code: Select all
WEAK const PinMap PinMap_CAN_RD[] = {
// {PA_8, CAN3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF11_CAN3)},
// {PA_11, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)},
// {PB_3, CAN3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF11_CAN3)}, // D23
// {PB_5, CAN2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN2)}, // D22 (D11)
// {PB_8, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, // D15 (A5)
{PB_12, CAN2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN2)}, // D19
// {PD_0, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, // D67 CAN_RX
{NC, NP, 0}};
#endif
#ifdef HAL_CAN_MODULE_ENABLED
WEAK const PinMap PinMap_CAN_TD[] = {
// {PA_12, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)},
// {PA_15, CAN3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF11_CAN3)}, // D20
// {PB_4, CAN3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF11_CAN3)}, // D25
// {PB_6, CAN2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN2)}, // D26
// {PB_9, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, // D14 (A4)
{PB_13, CAN2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN2)}, // D18
// {PD_1, CAN1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF9_CAN1)}, // D66 CAN_TX
{NC, NP, 0}};
#endif
Code: Select all
int CANSetup(void)
{
CAN_FilterTypeDef sFilterConfig = {0} ;
hCAN.State = HAL_CAN_STATE_RESET ;
can_rx_head = can_rx_tail = can_rx_lost = 0;
hCAN.Instance = CAN2;
hCAN.Init.Prescaler = 16; // we need 125 000 b/s
hCAN.Init.Mode = CAN_MODE_NORMAL;
hCAN.Init.SyncJumpWidth = CAN_SJW_4TQ;
hCAN.Init.TimeSeg1 = CAN_BS1_12TQ;
hCAN.Init.TimeSeg2 = CAN_BS2_5TQ;
hCAN.Init.TimeTriggeredMode = DISABLE;
hCAN.Init.AutoBusOff = DISABLE;
hCAN.Init.AutoWakeUp = DISABLE;
hCAN.Init.AutoRetransmission = ENABLE;
hCAN.Init.ReceiveFifoLocked = DISABLE;
hCAN.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hCAN) != HAL_OK)
{
return 1;
}
sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0; //set fifo assignment
sFilterConfig.FilterIdHigh = 0 ;
sFilterConfig.FilterIdLow = 0 ;
sFilterConfig.FilterMaskIdHigh = 0;
sFilterConfig.FilterMaskIdLow = 0;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; //set filter scale
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.FilterBank = 0 ;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK ;
HAL_CAN_ConfigFilter(&hCAN, &sFilterConfig); //configure CAN filter
//HAL_CAN_ActivateNotification(&hCAN, CAN_IT_RX_FIFO0_MSG_PENDING); //enable interrupts
if (HAL_CAN_Start(&hCAN) != HAL_OK)
{
return 2;
}
return 0;
}
void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
{
GPIO_InitTypeDef GPIO_InitStruct = {0} ;
__HAL_RCC_CAN1_CLK_ENABLE();
//__HAL_RCC_CAN2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// Configuration des pins PB8 (RX) et PB9 (TX)
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//__HAL_AFIO_REMAP_CAN1_2() ;
//HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0 , 0) ;
//HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn) ;
}
When calling
Code: Select all
int CANSetup(void)
I would really appreciate some help because I am totally stuck. If you need some further informations, just let me know.