How can I resolve the "CAN message send failed" error in my AVR microcontroller project using Zephyr

@Middleware & OS hey guys, I am working on an AVR microcontroller project using Zephyr OS for an IoT device that controls motors via CAN Bus/SPI. The initial error I encountered was "CAN Bus initialization failed." To address this, I verified the wiring and configuration settings in the Zephyr device tree. Despite this, the error "CAN message send failed" persists. I have studied the Zephyr documentation and relevant AVR datasheets extensively, yet the issue remains unresolved. the outcome should be able to send and receive message via the CAN Bus. Here is a sample code snippet of it:
#include <zephyr.h>
#include <device.h>
#include <drivers/can.h>
#include <drivers/spi.h>

// Function prototypes
void can_init(void);
int can_send(const struct zcan_frame *msg);
int can_receive(struct zcan_frame *msg);
void spi_init(void);
#include <zephyr.h>
#include <device.h>
#include <drivers/can.h>
#include <drivers/spi.h>

// Function prototypes
void can_init(void);
int can_send(const struct zcan_frame *msg);
int can_receive(struct zcan_frame *msg);
void spi_init(void);
3 Replies
Dtynin
Dtynin4mo ago
void main(void)
{
struct zcan_frame message;
int ret;

// Initialize CAN Bus
can_init();

// Initialize SPI
spi_init();

// Example CAN message to send
message.id_type = CAN_STANDARD_IDENTIFIER;
message.rtr = CAN_DATAFRAME;
message.dlc = 4;
message.data[0] = 0xAA;
message.data[1] = 0xBB;
message.data[2] = 0xCC;
message.data[3] = 0xDD;

// Send CAN message
ret = can_send(&message);
if (ret < 0) {
printk("Error sending CAN message\n");
}

// Receive CAN message
ret = can_receive(&message);
if (ret < 0) {
printk("Error receiving CAN message\n");
} else {
printk("Received CAN message: ID=0x%X DLC=%d Data=%X %X %X %X\n",
message.id, message.dlc,
message.data[0], message.data[1],
message.data[2], message.data[3]);
}

while (1) {
k_sleep(K_FOREVER);
}
}

void can_init(void)
{
// Implement CAN initialization here
// Example:
// struct device *can_dev = device_get_binding(DT_LABEL(DT_NODELABEL(can0)));
// can_configure(can_dev, ...);
// Verify initialization and handle errors
}

int can_send(const struct zcan_frame *msg)
{
// Implement CAN message send functionality
// Example:
// return can_send_message(can_dev, msg, K_NO_WAIT);
}

int can_receive(struct zcan_frame *msg)
{
// Implement CAN message receive functionality
// Example:
// return can_receive_message(can_dev, msg, K_FOREVER);
}

void spi_init(void)
{
// Implement SPI initialization here
// Example:
// struct device *spi_dev = device_get_binding(DT_LABEL(DT_NODELABEL(spi0)));
// spi_configure(spi_dev, ...);
// Verify initialization and handle errors
}
void main(void)
{
struct zcan_frame message;
int ret;

// Initialize CAN Bus
can_init();

// Initialize SPI
spi_init();

// Example CAN message to send
message.id_type = CAN_STANDARD_IDENTIFIER;
message.rtr = CAN_DATAFRAME;
message.dlc = 4;
message.data[0] = 0xAA;
message.data[1] = 0xBB;
message.data[2] = 0xCC;
message.data[3] = 0xDD;

// Send CAN message
ret = can_send(&message);
if (ret < 0) {
printk("Error sending CAN message\n");
}

// Receive CAN message
ret = can_receive(&message);
if (ret < 0) {
printk("Error receiving CAN message\n");
} else {
printk("Received CAN message: ID=0x%X DLC=%d Data=%X %X %X %X\n",
message.id, message.dlc,
message.data[0], message.data[1],
message.data[2], message.data[3]);
}

while (1) {
k_sleep(K_FOREVER);
}
}

void can_init(void)
{
// Implement CAN initialization here
// Example:
// struct device *can_dev = device_get_binding(DT_LABEL(DT_NODELABEL(can0)));
// can_configure(can_dev, ...);
// Verify initialization and handle errors
}

int can_send(const struct zcan_frame *msg)
{
// Implement CAN message send functionality
// Example:
// return can_send_message(can_dev, msg, K_NO_WAIT);
}

int can_receive(struct zcan_frame *msg)
{
// Implement CAN message receive functionality
// Example:
// return can_receive_message(can_dev, msg, K_FOREVER);
}

void spi_init(void)
{
// Implement SPI initialization here
// Example:
// struct device *spi_dev = device_get_binding(DT_LABEL(DT_NODELABEL(spi0)));
// spi_configure(spi_dev, ...);
// Verify initialization and handle errors
}
ZacckOsiemo
ZacckOsiemo4mo ago
Your can recieve and can send seen to be commented out, I havent used CAN with Zephyr but I can look at this later in the day if you want.
Dark AI
Dark AI4mo ago
Hello @Dtynin have you tried enabling the logging for the CAN driver to get more detailed error messages?
Want results from more Discord servers?
Add your server