Skip to content

CAN initialisation not working on STM32G431C6T6 #2

@James-47

Description

@James-47

Hi, I am trying to use your code for a simple application on the STM32 listed in the title. I have used your SimpleCan.cpp and SimpleCan.h as supporting libraries, unaltered.

However, after debugging the code below, I have found that the program gets stuck in the init function of the can instance "can1" (in the startCan() function.

I was wondering if you could please tell me why this could be happening?

I apologise because I cannot give you any serial outputs. I am programming the STM32 with a SWD interface through an ST-Link, but the serial prints aren't being received through the SWDIO pin for some reason.

Sketch:

// Written for STM32G4C6T6

#include <Arduino.h>
#include <SimpleCan.h>
#include <pinDef.h>

#include <IWatchdog.h>

#define HAL_FDCAN_MODULE_ENABLED

#define debug true // set to true to enable debug messages

// A0 to A14, D0 to D4
const int adcPins[15] = {A0, A1, A2, A3, A4, A5 , A6, A7, A8, A9, A10, A11, A12, A13, A14};
const int digitalPins[5] = {D0, D1, D2, D3, D4};
const unsigned int LED = PB9;

uint8_t values[20];

SimpleCan can1(NC, NC);

void setup() {
  // set up LED
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  // start watchdog
  IWatchdog.begin(5000000); // 5 second watchdog
  
  if (IWatchdog.isReset(true)) {
    debugPrint("Watchdog reset detected");
    digitalWrite(LED, HIGH);
  }

  // set up ADCs and digital pins
  for (int i = 0; i < 20; i++) {
    pinMode(adcPins[i], INPUT_ANALOG);
  }
  for (int i = 0; i < 5; i++) {
    pinMode(digitalPins[i], INPUT);
  }
  // Blink twice
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(200);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(2000);

  // start serial for debugging
  if (debug) {
    Serial.begin(115200);
  }
  
  // start CAN bus
  startCan();  // Code getting stuck within this function<--------------------------------------------------------------------

  digitalWrite(LED, LOW);
  delay(100);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < 20; i++) {
    values[i] = map(analogRead(adcPins[i]), 0, 1024, 0, 12000); // map ADC to V*1000 value
  }

  for (int i = 0; i < 5; i++) {
    values[i] = digitalRead(digitalPins[i]);
  }

  // send CAN messages
  sendCan(0);
  sendCan(1);
  sendCan(2);
  sendCan(3);
  sendCan(4);

  IWatchdog.reload();
  delay(10);
}

void startCan() {
  // configure the CAN bus and start comunications
  can1.init(CanSpeed::Kbit250); // Code specifically getting stuck within this function<------------------------------------------

  if (can1.start () != HAL_OK) {
    debugPrint("CAN bus failed to start");
    digitalWrite(LED, HIGH);

    // wait 1 second and try again
    delay(1000);
    digitalWrite(LED, LOW);
    delay(1000);
    startCan();

  } else {
    debugPrint("CAN bus started");
  }
}

void debugPrint(char* message) {
  if (debug) {
    Serial.println(message);
  }
}

void sendCan(unsigned char messageNumber) {
  // send it on the CAN bus

  FDCAN_TxHeaderTypeDef TxHeader;
  uint8_t TxData[8];

  TxHeader.Identifier = 0x300 + messageNumber;
  TxHeader.IdType = FDCAN_STANDARD_ID;
  TxHeader.TxFrameType = FDCAN_DATA_FRAME;
  TxHeader.DataLength = FDCAN_DLC_BYTES_64;
  TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
  TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
  TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
  TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
  TxHeader.MessageMarker = 0;

  TxData[0] = lowByte(values[0 + messageNumber * 4]);
  TxData[1] = highByte(values[0 + messageNumber * 4]);
  TxData[2] = lowByte(values[1 + messageNumber * 4]);
  TxData[3] = highByte(values[1 + messageNumber * 4]);
  TxData[4] = lowByte(values[2 + messageNumber * 4]);
  TxData[5] = highByte(values[2 + messageNumber * 4]);
  TxData[6] = lowByte(values[3 + messageNumber * 4]);
  TxData[7] = highByte(values[3 + messageNumber * 4]);

  if (debug) {
    Serial.print("Sending CAN message ");
    Serial.print(messageNumber);
    Serial.print(" with data: ");
    Serial.print(TxData[0]);
    Serial.print(" ");
    Serial.print(TxData[1]);
    Serial.print(" ");
    Serial.print(TxData[2]);
    Serial.print(" ");
    Serial.print(TxData[3]);
    Serial.print(" ");
    Serial.print(TxData[4]);
    Serial.print(" ");
    Serial.print(TxData[5]);
    Serial.print(" ");
    Serial.print(TxData[6]);
    Serial.print(" ");
    Serial.println(TxData[7]);
  }

  if (can1.addMessageToTxFifoQ(&TxHeader, TxData) == HAL_OK) {
    debugPrint("Ok.");
  } else {
    debugPrint("Failed.");
    digitalWrite(LED, HIGH);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions