CAN on STM32 "Starting CAN failed!"

Hi there,

I am working on a project in which I am using an STM32 Nucleo32 L412KB (NUCLEO-L412KB - STM32 Nucleo-32 development board with STM32L412KB MCU, supports Arduino nano connectivity - STMicroelectronics) to communicate over the CAN Bus. To do so, I decided to program the STM32 board with the Arduino IDE and installed the arduino-CAN library (GitHub - sandeepmistry/arduino-CAN: An Arduino library for sending and receiving data using CAN bus.) through the library manager. I have programmed a whole project that is specific to my use of this, but beforehand I wanted to try the CANSender example. It compiled and uploaded fine, but when it runs, it prints
"CAN Sender
Starting CAN failed!" on the serial monitor, which is what happens if if (!CAN.begin(500E3)) is true. However, I don't really know what would make that happen. I tried to look through the library but didn't find anything to enlighten me!
If anyone could give me an idea of what's going wrong that would be amazing!

Cheers,
eeyml8

PS: Here's the full example code:

// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <CAN.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("CAN Sender");

  // start the CAN bus at 500 kbps
  if (!CAN.begin(500E3)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }
}

void loop() {
  // send packet: id is 11 bits, packet can contain up to 8 bytes of data
  Serial.print("Sending packet ... ");

  CAN.beginPacket(0x12);
  CAN.write('h');
  CAN.write('e');
  CAN.write('l');
  CAN.write('l');
  CAN.write('o');
  CAN.endPacket();

  Serial.println("done");

  delay(1000);

  // send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
  Serial.print("Sending extended packet ... ");

  CAN.beginExtendedPacket(0xabcdef);
  CAN.write('w');
  CAN.write('o');
  CAN.write('r');
  CAN.write('l');
  CAN.write('d');
  CAN.endPacket();

  Serial.println("done");

  delay(1000);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.