-1

My APB1 clock is reported by the STM32 library as being 36MHz.

I used a website to calculate a prescaler value of 3 (4 with the automatic +1), BS1 of CAN_BS1_15tq and BS2 of CAN_BS2_2tq. When I use the values in a quick spreadsheet calculation they come out right for a 500 Kbit/s baud rate.

I used different values, but assuming the same clock speed of 36 MHz to talk at 250 Kbit/s baud rate to NMEA 2000 devices successfully. When I run my code at 250 Kbit/s it works correctly and talks to my test board (which is using the same code) successfully.

I wondered if the TX and RX pin GPIO speed mattered. Here is my configuration for those pins:

        gpio_init_data.GPIO_Speed = GPIO_Speed_10MHz;
        gpio_init_data.GPIO_Pin = CAN1_RX;
        gpio_init_data.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_Init(CAN1_PIN_GROUP, &gpio_init_data);

        gpio_init_data.GPIO_Pin = CAN1_TX;
        gpio_init_data.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(CAN1_PIN_GROUP, &gpio_init_data);

When I run at 500 KBit/s baud rate I get all transmissions failing and arbitration lost flagged: TSR=41000004. This happens even with the RX and TX pins at GPIO speed 50 MHz.

The CAN transceiver is an ISO1050 which, according to the data sheet, can handle up to 1 Mbit/s.

Does anyone have any idea what I could be doing wrong? Could it be a problem in the circuitry?

AlastairG
  • 4,119
  • 5
  • 26
  • 41
  • What clock source are you using? Internal RC oscillators are not recommended for CAN. In case you are using that, then what is its accuracy? Some modern MCUs have quite good accuracy on them, but as far as I remember that's not the case for most STM32 parts. – Lundin Aug 31 '22 at 12:30
  • 1
    [What are the most common causes of CAN bus communication errors?](https://electrical.codidact.com/posts/276251/276252) might be helpful. – Lundin Aug 31 '22 at 12:32
  • "CAN_BS1_15tq and BS2 of CAN_BS2_2tq" This sounds strange. Either this means 15 tq phase seg1 + prop seg and 2 tq phase seg2, for a total of 17 tq. Or it means 15+1 tq in case the 1 tq sync segment isn't counted. Either way it's a bad setting; you should have 14 tq (sync + phase1 + propagation) then sample point, then 2 tq phase2. – Lundin Aug 31 '22 at 12:36
  • @Lundin, I am using HSE. Also you miscounted. Sync seg + 15 tq seg1 + 2 tq seg 2 = 18 tq. I used the website I linked to calculate it and then took the values it recommended and put them into a spreadsheet to double check. – AlastairG Sep 02 '22 at 05:53
  • I am starting to wonder if the cause is bus termination. I am using a dev board and a matching test board. They are connected (for CAN) one to one, rather than a traditional bus, with about 8 cm long cable. – AlastairG Sep 02 '22 at 06:06
  • Oh. I simply was too busy to notice there was a response. Sorry. – AlastairG Sep 02 '22 at 08:43
  • Well anyway, I'd strongly recommend to use 1tq sync + 13tq phase1 and prop + 2tq phase2 then clock accordingly. This will give you the optimal sample point of 87.5%. and 16tq is suitable for most CAN standard baudrates. It may also be a hardware problem of course, in which case you should post the schematic + code both at https://electronics.stackexchange.com. CAN transceivers need an ideal impedance of 60ohm to work properly. Don't worry about characteristics of the GPIO pins. However, things like pull resistors on the RX line can mess things up seriously. – Lundin Sep 02 '22 at 11:47
  • I cannot easily change the APB frequency. It will mess up a whole load of other code in a shared library that we have and break far more than it fixes. Also we would need to redesign the board to get a different external clock rate. It is a standard board that we use for a number of purposes. I can play with the prescaler value. According to the web site I linked, the values that give the closest to 87.5% are the ones I quoted about. I think your ideas about pull resistors are the next thing to investigate. – AlastairG Sep 04 '22 at 07:41

1 Answers1

0

As Lundin said, "CAN transceivers need an ideal impedance of 60 ohm to work properly."

The system I am using is a test rig with a board to be tested connected to a test board by about 8cm of CAN bus cable pair. Up to speeds of 250 Kbits this works perfectly well, but not at 500 Kbits.

Adding a 56 ohm resistor (2 x 120 ohm may be better) solves the problem.

Many thanks to Lundin for his patience and excellent information.

AlastairG
  • 4,119
  • 5
  • 26
  • 41