nopnop2002 / Arduino-STM32-CAN

Can Example for Arduino Core STM32
210 stars 53 forks source link

Bit timings #33

Closed pazi88 closed 3 years ago

pazi88 commented 3 years ago

Not really issue, but still. I would like to know how you have calculated the bit timings, so I could calculate those for other F4 boards too that have different clock frequency. Like F401 and F411

nopnop2002 commented 3 years ago

By executing this function, you can find out the timing according to the CPU clock and CAN SPEED.

https://github.com/UAVCAN/libcanard/blob/9d2e2904d25c0df10dc78a17ba915cdb13d54292/drivers/stm32/canard_stm32.h#L190

void setup() {
  // initialize Serial
  Serial.begin(115200);
  // set CAN bit rate
  initCanard(50*1000);
  initCanard(100*1000);
  initCanard(125*1000);
  initCanard(250*1000);
  initCanard(500*1000);
  initCanard(1000*1000);
}

void initCanard(uint32_t speed)
{
  Serial.print("speed=");
  Serial.print(speed);
  Serial.println();

  CanardSTM32CANTimings timings;
  //int result = canardSTM32ComputeCANTimings(HAL_RCC_GetPCLK1Freq(), 1000000, &timings);
  int result = canardSTM32ComputeCANTimings(HAL_RCC_GetPCLK1Freq(), speed, &timings);

  Serial.print("timings.bit_rate_prescaler=");
  Serial.print(timings.bit_rate_prescaler);
  Serial.println();
  Serial.print("timings.bit_segment_1=");
  Serial.print(timings.bit_segment_1);
  Serial.println();
  Serial.print("timings.bit_segment_2=");
  Serial.print(timings.bit_segment_2);
  Serial.println();
  Serial.print("timings.max_resynchronization_jump_width=");
  Serial.print(timings.max_resynchronization_jump_width);
  Serial.println();

}
pazi88 commented 3 years ago

Great thanks! I also found this site which had working calculator. Once I figured out I need to use APB1 clock rate: http://www.bittiming.can-wiki.info/