From e9eadbf1bed3bb1511d7fed33f51e901339d0811 Mon Sep 17 00:00:00 2001 From: isakg22 Date: Thu, 15 Oct 2026 13:13:52 +0000 Subject: [PATCH] =?UTF-8?q?Breytt=20Overlay=20fyrir=20Can=20stillingar,=20?= =?UTF-8?q?virkja=C3=B0=20can=20=C3=AD=20prj=20conf=20og=20test=20k=C3=B3?= =?UTF-8?q?=C3=B0i=20b=C3=BAinn=20til=20fyrir=20CAN=20FRAME=20st=C3=B6ffi?= =?UTF-8?q?=C3=B0=20=C3=BAr=20L4.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CAN_FRAME_TEST.c | 47 +++++++++++++++++++++++++++++ src/main.c | 16 +++++----- src/rotary_encoder_driver.cpp | 18 +++-------- zephyr/boards/nucleo_l432kc.overlay | 21 +++++++------ zephyr/prj.conf | 4 ++- 5 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 src/CAN_FRAME_TEST.c diff --git a/src/CAN_FRAME_TEST.c b/src/CAN_FRAME_TEST.c new file mode 100644 index 0000000..b585207 --- /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(10); + + 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/main.c b/src/main.c index 08da793..19109b5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,12 @@ -#include +// #include -#ifndef PIO_UNIT_TESTING +// #ifndef PIO_UNIT_TESTING -void motor_application_main(void); +// void motor_application_main(void); -void main(void) -{ - motor_application_main(); -} +// void main(void) +// { +// motor_application_main(); +// } -#endif \ No newline at end of file +// #endif \ No newline at end of file diff --git a/src/rotary_encoder_driver.cpp b/src/rotary_encoder_driver.cpp index 002fc5c..c3c7d88 100644 --- a/src/rotary_encoder_driver.cpp +++ b/src/rotary_encoder_driver.cpp @@ -6,10 +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); static const struct gpio_dt_spec encoder_c1 = GPIO_DT_SPEC_GET(DT_ALIAS(encoder_c1), gpios); @@ -31,17 +27,11 @@ RotaryEncoderDriver::RotaryEncoderDriver() int RotaryEncoderDriver::begin() { if (!gpio_is_ready_dt(&encoder_c1) || - !gpio_is_ready_dt(&encoder_c2) || - !gpio_is_ready_dt(&isr_probe)) { + !gpio_is_ready_dt(&encoder_c2)) { return -ENODEV; } - int ret = gpio_pin_configure_dt(&isr_probe, GPIO_OUTPUT_INACTIVE); - if (ret < 0) { - return ret; - } - - ret = gpio_pin_configure_dt(&encoder_c1, GPIO_INPUT); + int ret = gpio_pin_configure_dt(&encoder_c1, GPIO_INPUT); if (ret < 0) { return ret; } @@ -51,6 +41,7 @@ int RotaryEncoderDriver::begin() return ret; } + gpio_init_callback( &encoder_c1_callback, gpio_edge_handler, @@ -89,8 +80,7 @@ void RotaryEncoderDriver::gpio_edge_handler( if ((pins & BIT(encoder_c1.pin)) != 0U) { encoder->update(); // Reads C2 and updates the count - gpio_pin_set_dt(&isr_probe, 1); // Marker rising edge - gpio_pin_set_dt(&isr_probe, 0); // Reset for the next interrupt + } } diff --git a/zephyr/boards/nucleo_l432kc.overlay b/zephyr/boards/nucleo_l432kc.overlay index cca7b88..07d335a 100644 --- a/zephyr/boards/nucleo_l432kc.overlay +++ b/zephyr/boards/nucleo_l432kc.overlay @@ -2,6 +2,10 @@ #include / { + chosen { + zephyr,canbus = &can1; + }; + aliases { encoder-c1 = &encoder_c1; encoder-c2 = &encoder_c2; @@ -11,7 +15,6 @@ motor-flt = &motor_flt; motor-slp = &motor_slp; fault-test-button = &fault_test_button; - encoder-isr-probe = &encoder_isr_probe; }; buttons { @@ -31,7 +34,7 @@ gpios = <&gpioa 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; /* A0, PA0 */ label = "Motor fault"; }; - + fault_test_button: fault_test_button { gpios = <&gpiob 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; /* D3, PB0 */ label = "Fault test button"; @@ -50,11 +53,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 { @@ -65,9 +63,6 @@ label = "Motor BIN1 PWM"; }; }; - zephyr,user { - encoder-isr-probe-gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */ - }; }; &timers1 { @@ -79,4 +74,10 @@ pinctrl-0 = <&tim1_ch1_pa8>; pinctrl-names = "default"; }; +}; + +&can1 { + pinctrl-0 = <&can1_rx_pa11 &can1_tx_pa12>; + pinctrl-names = "default"; + status = "okay"; }; \ No newline at end of file diff --git a/zephyr/prj.conf b/zephyr/prj.conf index d18c3e9..090ff2b 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -7,4 +7,6 @@ CONFIG_SERIAL=y CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y CONFIG_PRINTK=y -CONFIG_CBPRINTF_FP_SUPPORT=y \ No newline at end of file +CONFIG_CBPRINTF_FP_SUPPORT=y + +CONFIG_CAN=y \ No newline at end of file