Hello,
Does anyone had problem with receiving CAN message to STM32F1.
I have multiple devices on bus, some of them are ESP32(receiving and sending is working), STM32F1(only sending packet is working).
CDC is disabled. Core that I am using has added changes to support HardwareCAN library, just to say again, sending part is working with same init.
https://github.com/coddingtonbear/Ardui ... ardwareCAN
I am missing some important. IRQ or POLL mode maybe? Sending standard format ID.
Here is receiving code for stm:
Code: Select all
#include <HardwareCAN.h>
//#include "Changes.h"
HardwareCAN canBus(CAN1_BASE);
//CanMsg msg ;
void CANSetup(void)
{
CAN_STATUS Stat ;
// Initialize CAN module
canBus.map(CAN_GPIO_PA11_PA12); // PA11-CRX/ PA12-CTX
Stat = canBus.begin(CAN_SPEED_500, CAN_MODE_NORMAL );
canBus.filter(0, 0, 0);
canBus.set_irq_mode(); // Use irq mode (recommended), so the handling of incoming messages
// will be performed at ease in a task or in the loop. The software fifo is 16 cells long,
// allowing at least 15 ms before processing the fifo is needed at 125 kbps
Stat = canBus.status();
if (Stat != CAN_OK) {
/* Your own error processing here */ ; // Initialization failed
}
}
// Process incoming messages
// Note : frames are not fully checked for correctness: DLC value is not checked, neither are the IDE and RTR fields. However, the data is guaranteed to be corrrect.
void ProcessMessages(void)
{
CanMsg *r_msg;
if ((r_msg = canBus.recv()) != NULL){
Serial2.print(r_msg->ID);
Serial2.print("#");
Serial2.print(r_msg->Data[0]);
Serial2.print(".");
Serial2.print(r_msg->Data[1]);
Serial2.print(".");
Serial2.print(r_msg->Data[2]);
Serial2.print(".");
Serial2.print(r_msg->Data[3]);
Serial2.print(".");
Serial2.print(r_msg->Data[4]);
Serial2.print(".");
Serial2.print(r_msg->Data[5]);
Serial2.print(".");
Serial2.print(r_msg->Data[6]);
Serial2.print(".");
Serial2.println(r_msg->Data[7]);
canBus.free(); // Remove processed message from buffer, whatever the identifier
}
}
// The application program starts here
void setup() {
*((uint32_t*)0x40005c40) = 3 ; // USB_CNTR
*((uint32_t*)0x40005c44) = 0 ; // USB_ISTR
CANSetup() ; // Initialize the CAN module and prepare the message structures.
Serial2.begin(115200); //PA9 UART1 RX //PA10 UART1 TX
Serial2.println("Serial2");
}
void loop() {
ProcessMessages() ; // Process all incoming messages, update local variables accordingly
delay(20) ; // The delay must not be greater than the time to overflow the incoming fifo (here about 15 ms)
}
BR,
Stefan