From 9762afb00391832f1cf300ea2ca4798552f34ea3 Mon Sep 17 00:00:00 2001 From: helgie22 Date: Thu, 15 Oct 2026 14:16:14 +0000 Subject: [PATCH] =?UTF-8?q?can=20send=20test=20b=C3=A6tt=20vi=C3=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformio.ini | 6 +++ src/can_send_test.c | 61 +++++++++++++++++++++++++++++ src/main.c | 5 +++ src/rotary_encoder_driver.cpp | 5 --- zephyr/boards/nucleo_l432kc.overlay | 18 ++++----- zephyr/prj.conf | 3 +- 6 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 src/can_send_test.c diff --git a/platformio.ini b/platformio.ini index a360d14..75ecb08 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,3 +21,9 @@ upload_flags = ; CSV-útprentun í Operational fyrir step/load response (sjá capture_serial.py) ; build_flags = -D LOG_CSV=1 + +; Part 2.1: CAN bring-up test (src/can_send_test.c) instead of the motor app. +; Build/upload: pio run -e can_test -t upload +[env:can_test] +extends = env:nucleo_l432kc +build_flags = -D CAN_SEND_TEST=1 diff --git a/src/can_send_test.c b/src/can_send_test.c new file mode 100644 index 0000000..24b1558 --- /dev/null +++ b/src/can_send_test.c @@ -0,0 +1,61 @@ +// Part 2.1: CAN bring-up. Sends one frame every 100 ms so it can be seen +// on the oscilloscope and with `candump can0` on the Raspberry Pi. +// Built only with the can_test environment (-D CAN_SEND_TEST=1). + +#include +#include +#include +#include + +// 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 + +static const struct device *const can_dev = DEVICE_DT_GET(DT_NODELABEL(can1)); + +void can_send_test_main(void) +{ + if (!device_is_ready(can_dev)) { + printk("CAN device is not ready\n"); + return; + } + + int err = can_set_bitrate(can_dev, CAN_BITRATE); + if (err != 0) { + printk("can_set_bitrate failed: %d\n", err); + return; + } + + err = can_start(can_dev); + if (err != 0) { + printk("can_start failed: %d\n", err); + return; + } + + printk("CAN started at %d bit/s, sending id 0x%03X\n", + CAN_BITRATE, TEST_CAN_ID); + + struct can_frame frame = { + .id = TEST_CAN_ID, + .dlc = TEST_CAN_DLC, + .flags = 0, // Standard id, data frame + }; + uint8_t counter = 0; + + while (true) { + // Byte 0 counts up so you can see new frames arriving. + frame.data[0] = counter++; + for (int i = 1; i < TEST_CAN_DLC; i++) { + frame.data[i] = i; + } + + // Blocks until the frame is sent (and acknowledged by another node). + err = can_send(can_dev, &frame, K_MSEC(100), NULL, NULL); + if (err != 0) { + printk("can_send failed: %d\n", err); + } + + k_sleep(K_MSEC(100)); + } +} diff --git a/src/main.c b/src/main.c index cc8f040..ecdd481 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,14 @@ #include void motor_application_main(void); +void can_send_test_main(void); int main(void) { +#if CAN_SEND_TEST + can_send_test_main(); +#else motor_application_main(); +#endif return 0; } diff --git a/src/rotary_encoder_driver.cpp b/src/rotary_encoder_driver.cpp index 44d3415..db8211e 100644 --- a/src/rotary_encoder_driver.cpp +++ b/src/rotary_encoder_driver.cpp @@ -6,11 +6,6 @@ #include #include -//#define ISR_PROBE_NODE DT_PATH(zephyr_user) - -static const struct gpio_dt_spec isr_probe = - GPIO_DT_SPEC_GET(DT_ALIAS(encoder_isr_probe), gpios); - RotaryEncoderDriver::RotaryEncoderDriver() : count(0), delta(0), diff --git a/zephyr/boards/nucleo_l432kc.overlay b/zephyr/boards/nucleo_l432kc.overlay index b8070f4..e102e8e 100644 --- a/zephyr/boards/nucleo_l432kc.overlay +++ b/zephyr/boards/nucleo_l432kc.overlay @@ -45,11 +45,6 @@ gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; /* A1, PA1 */ label = "Motor sleep"; }; - - encoder_isr_probe: encoder_isr_probe { - gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */ - label = "Encoder ISR probe"; - }; }; motor_pwm_outputs { @@ -68,9 +63,6 @@ label = "Motor BIN2 PWM"; }; }; - zephyr,user { - encoder-isr-probe-gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */ - }; }; &timers1 { @@ -83,4 +75,12 @@ pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch3n_pb1>; pinctrl-names = "default"; }; -}; \ No newline at end of file +}; + +/* CAN1 to the transceiver: RX on D10 (PA11), TX on D2 (PA12). + * The bitrate is set in the code with can_set_bitrate(). */ +&can1 { + pinctrl-0 = <&can1_rx_pa11 &can1_tx_pa12>; + pinctrl-names = "default"; + status = "okay"; +}; diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 14b9d3f..29b2f94 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -8,4 +8,5 @@ CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_PRINTK=y -CONFIG_CBPRINTF_FP_SUPPORT=y \ No newline at end of file +CONFIG_CBPRINTF_FP_SUPPORT=y +CONFIG_CAN=y