diff --git a/src/CAN_FRAME_TEST.c b/src/CAN_FRAME_TEST.c new file mode 100644 index 0000000..b579da6 --- /dev/null +++ b/src/CAN_FRAME_TEST.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include + +/* Devicetree node selected by zephyr,canbus in the overlay */ +#define CANBUS_NODE DT_CHOSEN(zephyr_canbus) + +int main(void) +{ + const struct device *dev = DEVICE_DT_GET(CANBUS_NODE); + + if (!device_is_ready(dev)) { + printk("CAN device not ready\n"); + return 0; + } + + int err = can_set_bitrate(dev, 125000); + if (err != 0) { + printk("Error setting CAN bitrate (err %d)\n", err); + return 0; + } + + err = can_start(dev); + if (err != 0) { + printk("Error starting CAN controller (err %d)\n", err); + return 0; + } + + struct can_frame frame = { + .flags = 0, + .id = 0x123, + .dlc = 8, + .data = {1, 2, 3, 4, 5, 6, 7, 8}, + }; + + while (1) { + k_msleep(500); + + err = can_send(dev, &frame, K_MSEC(100), NULL, NULL); + if (err != 0) { + printk("Sending failed (err %d)\n", err); + return 0; + } + } +} \ No newline at end of file diff --git a/src/can_send_test.c b/src/can_send_test.c index 24b1558..2883991 100644 --- a/src/can_send_test.c +++ b/src/can_send_test.c @@ -10,7 +10,7 @@ // Change these to see the difference in candump. #define TEST_CAN_ID 0x123 // 11-bit standard id (0x000 - 0x7FF) #define TEST_CAN_DLC 8 // Number of data bytes (0 - 8) -#define CAN_BITRATE 250000 // Must match the bitrate of can0 on the Pi +#define CAN_BITRATE 125000 // Must match the bitrate of can0 on the Pi static const struct device *const can_dev = DEVICE_DT_GET(DT_NODELABEL(can1));