1 Commits
5 changed files with 73 additions and 33 deletions
+47
View File
@@ -0,0 +1,47 @@
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/can.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
/* 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;
}
}
}
+8 -8
View File
@@ -1,12 +1,12 @@
#include <zephyr/kernel.h>
// #include <zephyr/kernel.h>
#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
// #endif
+4 -14
View File
@@ -6,10 +6,6 @@
#include <zephyr/devicetree.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);
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
}
}
+11 -10
View File
@@ -2,6 +2,10 @@
#include <zephyr/dt-bindings/pwm/pwm.h>
/ {
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";
};
+3 -1
View File
@@ -7,4 +7,6 @@ CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_CAN=y