2025-01-29 02:12 AM - last edited on 2025-01-29 03:30 AM by SofLit
Hi, let me clarify more my situation ... I am coding this from scratch i am using a CAN transciever...My problem is that the CAN_Start function always returns an Error and while debugging i noticed that my code is stuck in the CAN initialisation what i see is that the BIT INAK in the MSR register stayd always 1 when it should switch to 0...probably i have a problem in the config ...This is my config code :
void can_gpio_init(void) {
/*1 Enable Clock Access to GPIOB*/
RCC->AHB1ENR |= GPIODEN;
/*2 Set PD0 and PD1 to alternate function mode*/
GPIOD->MODER &=~(1U<<0);
GPIOD->MODER |=(1U<<1);
GPIOD->MODER &=~(1U<<2);
GPIOD->MODER |=(1U<<3);
GPIOD->PUPDR |=(1U<<0);
GPIOD->PUPDR &=~(1U<<1);
GPIOD->PUPDR |=(1U<<2);
GPIOD->PUPDR &=~(1U<<3);
GPIOD->OTYPER &=~(1U<<0);
GPIOD->OTYPER &=~(1U<<1);
/*3 SET PD0 and PD1 alternate function to CAN1 RX and TX*/
GPIOD->AFR[0] = (CAN_AF<<0);
GPIOD->AFR[0]= (CAN_AF<<4);
/*4 Enable CAN RX0 Interrupt for message reception*/
NVIC_EnableIRQ(CAN1_RX0_IRQn);
}
void can_parms_init(uint8_t mode){
/*1 Enable Clock Access to CAN1*/
RCC->APB1ENR |= RCC_APB1ENR_CAN1EN;
/*2 Enter Initialization mode*/
CAN1->MCR |= CAN_MCR_INRQ;
/*3 Wait until CAN1 is in Initialization mode*/
while ((CAN1->MSR & CAN_MSR_INAK)==0) {}
/*4 Exit Sleep Mode*/
CAN1->MCR &=~ CAN_MCR_SLEEP;
/*5 Wait until CAN1 is OUT of Sleep Mode*/
while ((CAN1->MSR & CAN_MSR_SLAK)) {}
/*5 Configure timing parameters including baudrate by configuring time segment 1 and 2 and prescaler*/
CAN1->BTR = (11<< CAN_BTR_TS1_Pos) | (2 << CAN_BTR_TS2_Pos) | (5 << CAN_BTR_BRP_Pos);
/*6 Select Mode*/
if (mode) {
/*Normal Mode*/
CAN1->BTR &=~(1<<30);
}
else {
//loopBack mode
CAN1->BTR |=(1<<30);
}
}
Solved! Go to Solution.
2025-01-29 08:10 AM
While SET PD0 and PD1 alternate function to CAN1 RX and TX
I wrote : = instead of |=
2025-01-29 03:37 AM - edited 2025-01-29 03:38 AM
Hello @HaythemKh01 and welcome to the community.
If you are trying to play around with baremetal implementation for education purposes I invite you first to use the HAL/CubeMx to at least validate the hardware. I don't recommend at this stage the direct access to the register even if your professor is requesting that (it will be the last thing to do). As I said, start by validating you HAW using HAL, if it does works, inspire from the HAL on how the registers were accessed and try to reproduce the same thing.
If you are using the Loopback mode:
1- No need for a transceiver usage.
2- You need to set a pull up resistor on CAN_Rx pin.
If you are using the Normal mode you need at least two CAN nodes to establish a CAN bus.
Hope that helps.
2025-01-29 04:18 AM
Thank you for your advice sir. I resoloved this problem but i stay have a problem with the RX message when the FMP is always 0 while Tx is working
2025-01-29 05:06 AM - edited 2025-01-29 07:10 AM
You didn't tell how you did solved the issue.
For a new question please open a new thread.
2025-01-29 08:10 AM
While SET PD0 and PD1 alternate function to CAN1 RX and TX
I wrote : = instead of |=
2025-01-29 08:41 AM
@HaythemKh01 wrote:
While SET PD0 and PD1 alternate function to CAN1 RX and TX
I wrote : = instead of |=
That's why I recommended you to use the HAL and then inspiring from it. Doing the shifts your self will induce you to errors.