[Completed] CAN for libemaple + F4
michael_l Sat Sep 08, 2018 1:27 pm
Here’s the original code: https://github.com/Phonog/Arduino_STM32 … areCAN/src
I’ve added RCC_CAN into rccF4.h/rccF4.c
[RCC_CAN] = { .clk_domain = APB1, .line_num = 25 },and also changed NVIC namings so it compiles OK. Also AFIO RCC is not used in F4.
Currently the problem is that CAN goes into init mode but does not leave it and it timeouts. Init is performed in can_init function.
From reference:
Once the initialization is complete, the software must request the hardware to enter Normal
mode to be able to synchronize on the CAN bus and start reception and transmission.
The request to enter Normal mode is issued by clearing the INRQ bit in the CAN_MCR
register. The bxCAN enters Normal mode and is ready to take part in bus activities when it
has synchronized with the data transfer on the CAN bus. This is done by waiting for the
occurrence of a sequence of 11 consecutive recessive bits (Bus Idle state). The switch to
Normal mode is confirmed by the hardware by clearing the INAK bit in the CAN_MSR
register.
I have a small test bench CAN network with 2 devices terminated with 120ohm resistors. First one is USBTin CAN-BUS adapter and the other is “mini blue board” aka VCC_GND_F407. I tested with micropython that can driver works and USBTin receives messages from F4 and shows them in the viewer program so CAN BUS is proven to work and CAN hardware in F4 is working.
python code:
from pyb import CAN
import time
#250kBit, F4, PB8 (RX), PB9 (TX)
can = CAN(1, CAN.NORMAL, extframe=False, prescaler=12, sjw=1, bs1=11, bs2=2)
can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) # set a filter to receive messages with id=123, 124, 125 and 126
while True:
print("sending...")
can.send(b'00000000', 1) # send a message with id 123
time.sleep(.005)
print(can.info())
