can send test bætt við

This commit is contained in:
2026-10-15 14:16:14 +00:00
parent e41fed4ea0
commit 9762afb003
6 changed files with 83 additions and 15 deletions
+6
View File
@@ -21,3 +21,9 @@ upload_flags =
; CSV-útprentun í Operational fyrir step/load response (sjá capture_serial.py) ; CSV-útprentun í Operational fyrir step/load response (sjá capture_serial.py)
; build_flags = -D LOG_CSV=1 ; 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
+61
View File
@@ -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 <zephyr/device.h>
#include <zephyr/drivers/can.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
// 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));
}
}
+5
View File
@@ -1,9 +1,14 @@
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
void motor_application_main(void); void motor_application_main(void);
void can_send_test_main(void);
int main(void) int main(void)
{ {
#if CAN_SEND_TEST
can_send_test_main();
#else
motor_application_main(); motor_application_main();
#endif
return 0; return 0;
} }
-5
View File
@@ -6,11 +6,6 @@
#include <zephyr/devicetree.h> #include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio.h>
//#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() RotaryEncoderDriver::RotaryEncoderDriver()
: count(0), : count(0),
delta(0), delta(0),
+9 -9
View File
@@ -45,11 +45,6 @@
gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; /* A1, PA1 */ gpios = <&gpioa 1 GPIO_ACTIVE_HIGH>; /* A1, PA1 */
label = "Motor sleep"; label = "Motor sleep";
}; };
encoder_isr_probe: encoder_isr_probe {
gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */
label = "Encoder ISR probe";
};
}; };
motor_pwm_outputs { motor_pwm_outputs {
@@ -68,9 +63,6 @@
label = "Motor BIN2 PWM"; label = "Motor BIN2 PWM";
}; };
}; };
zephyr,user {
encoder-isr-probe-gpios = <&gpioa 12 GPIO_ACTIVE_HIGH>; /* D2, PA12 */
};
}; };
&timers1 { &timers1 {
@@ -83,4 +75,12 @@
pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch3n_pb1>; pinctrl-0 = <&tim1_ch1_pa8 &tim1_ch3n_pb1>;
pinctrl-names = "default"; pinctrl-names = "default";
}; };
}; };
/* 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";
};
+2 -1
View File
@@ -8,4 +8,5 @@ CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y CONFIG_UART_CONSOLE=y
CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_PRINTK=y CONFIG_PRINTK=y
CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_CAN=y