Browse Source

test for can bus hardware

Thomas Buck 8 months ago
parent
commit
53fe41b308
8 changed files with 154 additions and 8 deletions
  1. 3
    0
      .gitmodules
  2. 1
    0
      README.md
  3. 19
    8
      firmware/CMakeLists.txt
  4. 12
    0
      firmware/README.md
  5. 1
    0
      firmware/can2040
  6. 26
    0
      firmware/include/can.h
  7. 87
    0
      firmware/src/can.c
  8. 5
    0
      firmware/src/main.c

+ 3
- 0
.gitmodules View File

7
 [submodule "firmware/pico-ssd1306"]
7
 [submodule "firmware/pico-ssd1306"]
8
 	path = firmware/pico-ssd1306
8
 	path = firmware/pico-ssd1306
9
 	url = https://github.com/xythobuz/pico-ssd1306
9
 	url = https://github.com/xythobuz/pico-ssd1306
10
+[submodule "firmware/can2040"]
11
+	path = firmware/can2040
12
+	url = https://github.com/KevinOConnor/can2040

+ 1
- 0
README.md View File

47
 It uses the [Pi Pico SDK](https://github.com/raspberrypi/pico-sdk), licensed as BSD 3-clause, and therefore also [TinyUSB](https://github.com/hathach/tinyusb), licensed under the MIT license.
47
 It uses the [Pi Pico SDK](https://github.com/raspberrypi/pico-sdk), licensed as BSD 3-clause, and therefore also [TinyUSB](https://github.com/hathach/tinyusb), licensed under the MIT license.
48
 Some code is adapted from the TinyUSB examples.
48
 Some code is adapted from the TinyUSB examples.
49
 To control the OLED display, [pico-ssd1306](https://github.com/daschr/pico-ssd1306) is used, licensed under the MIT license.
49
 To control the OLED display, [pico-ssd1306](https://github.com/daschr/pico-ssd1306) is used, licensed under the MIT license.
50
+The CAN bus PIO implementation is [can2040](https://github.com/KevinOConnor/can2040), licensed under the GPLv3.
50
 Repo metadata is embedded into the project using [cmake-git-version-tracking](https://github.com/andrew-hardin/cmake-git-version-tracking), licensed under the MIT license.
51
 Repo metadata is embedded into the project using [cmake-git-version-tracking](https://github.com/andrew-hardin/cmake-git-version-tracking), licensed under the MIT license.
51
 
52
 
52
     This program is free software: you can redistribute it and/or modify
53
     This program is free software: you can redistribute it and/or modify

+ 19
- 8
firmware/CMakeLists.txt View File

41
     ${CMAKE_CURRENT_LIST_DIR}/src/lcd.c
41
     ${CMAKE_CURRENT_LIST_DIR}/src/lcd.c
42
     ${CMAKE_CURRENT_LIST_DIR}/src/ring.c
42
     ${CMAKE_CURRENT_LIST_DIR}/src/ring.c
43
     ${CMAKE_CURRENT_LIST_DIR}/src/hw_id.c
43
     ${CMAKE_CURRENT_LIST_DIR}/src/hw_id.c
44
+    ${CMAKE_CURRENT_LIST_DIR}/src/can.c
44
 
45
 
45
     ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306/ssd1306.c
46
     ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306/ssd1306.c
47
+
48
+    ${CMAKE_CURRENT_LIST_DIR}/can2040/src/can2040.c
46
 )
49
 )
47
 
50
 
48
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
51
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
52
+
49
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306)
53
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306)
50
 
54
 
55
+target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/can2040/src)
56
+target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Device/RaspberryPi/RP2040/Include)
57
+target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Core/Include)
58
+
51
 # enable generous warnings
59
 # enable generous warnings
52
 target_compile_options(dispensy PUBLIC
60
 target_compile_options(dispensy PUBLIC
53
     -Wall
61
     -Wall
54
     -Wextra
62
     -Wextra
55
     -Werror
63
     -Werror
56
-    -Wshadow
64
+
65
+    -O3
57
 
66
 
58
     -DSSD1306_DEBUG_PRINT=debug
67
     -DSSD1306_DEBUG_PRINT=debug
59
     -DSSD1306_DEBUG_INCLUDE="log.h"
68
     -DSSD1306_DEBUG_INCLUDE="log.h"
60
 )
69
 )
61
 
70
 
62
 # suppress some warnings for borked 3rd party files in Pico SDK
71
 # suppress some warnings for borked 3rd party files in Pico SDK
63
-set_source_files_properties(pico-sdk/lib/btstack/src/ble/sm.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
64
-set_source_files_properties(pico-sdk/lib/btstack/src/btstack_hid_parser.c PROPERTIES COMPILE_FLAGS -Wno-maybe-uninitialized)
65
-set_source_files_properties(pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_driver.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
66
-set_source_files_properties(pico-sdk/lib/btstack/src/classic/avdtp_util.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
67
-set_source_files_properties(pico-sdk/lib/btstack/src/classic/goep_client.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
68
-set_source_files_properties(pico-sdk/lib/btstack/src/classic/goep_server.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
69
-set_source_files_properties(pico-sdk/src/rp2_common/hardware_flash/flash.c PROPERTIES COMPILE_FLAGS -Wno-shadow)
72
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/ble/sm.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
73
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/btstack_hid_parser.c PROPERTIES COMPILE_FLAGS -Wno-maybe-uninitialized)
74
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_driver.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
75
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/classic/avdtp_util.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
76
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/classic/goep_client.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
77
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/classic/goep_server.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
78
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/hardware_flash/flash.c PROPERTIES COMPILE_FLAGS -Wno-shadow)
79
+
80
+set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/can2040/src/can2040.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
70
 
81
 
71
 # repo meta data
82
 # repo meta data
72
 include(FetchContent)
83
 include(FetchContent)

+ 12
- 0
firmware/README.md View File

32
 
32
 
33
     sudo pacman -S arm-none-eabi-gcc arm-none-eabi-newlib picocom cmake cxxtest
33
     sudo pacman -S arm-none-eabi-gcc arm-none-eabi-newlib picocom cmake cxxtest
34
 
34
 
35
+## Sniffing the CAN Bus
36
+
37
+Using a USB CAN adapter that is supported in Linux, [like the UCAN](https://www.fysetc.com/en-de/products/fysetc-ucan-board-based-on-stm32f072-usb-to-can-adapter-support-with-canable-candlelight-klipper-firmware), you can easily interact with the CAN bus.
38
+
39
+You need to install the [can-utils](https://github.com/linux-can/can-utils) in your Linux distro.
40
+
41
+After plugging the adapter in, bring up the CAN network interface and dump all received packets:
42
+
43
+    sudo ip link set can0 type can bitrate 1000000
44
+    sudo ip link set up can0
45
+    candump can0
46
+
35
 ## Proper Debugging
47
 ## Proper Debugging
36
 
48
 
37
 You can also use the SWD interface for proper hardware debugging.
49
 You can also use the SWD interface for proper hardware debugging.

+ 1
- 0
firmware/can2040

1
+Subproject commit 9e761ae353ea473e24c8bba226e10afad8a919ff

+ 26
- 0
firmware/include/can.h View File

1
+/*
2
+ * can.h
3
+ *
4
+ * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#ifndef __CAN_H__
20
+#define __CAN_H__
21
+
22
+void can_init(void);
23
+
24
+void can_hello(void);
25
+
26
+#endif // __CAN_H__

+ 87
- 0
firmware/src/can.c View File

1
+/*
2
+ * can.c
3
+ *
4
+ * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * See <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+#include "can2040.h"
20
+#include "RP2040.h"
21
+
22
+#include "config.h"
23
+#include "log.h"
24
+#include "can.h"
25
+
26
+#define PIO_IRQ PIO0_IRQ_0_IRQn
27
+static const uint pio_num = 0;
28
+static const uint sys_clock = 125UL * 1000UL * 1000UL;
29
+static const uint bitrate = 1000UL * 1000UL;
30
+static const uint gpio_rx = 25;
31
+static const uint gpio_tx = 24;
32
+
33
+static struct can2040 bus;
34
+
35
+static void can2040_cb(struct can2040 *cd, uint32_t notify, struct can2040_msg *msg) {
36
+    (void)cd;
37
+
38
+    switch (notify) {
39
+        case CAN2040_NOTIFY_RX: {
40
+            debug("rx id=%" PRIX32 " dlc=%" PRIX32 " data=0x%08" PRIX32 "%08" PRIX32,
41
+                  msg->id, msg->dlc, msg->data32[0], msg->data32[1]);
42
+            break;
43
+        }
44
+
45
+        case CAN2040_NOTIFY_TX: {
46
+            debug("tx id=%" PRIX32 " dlc=%" PRIX32 " data=0x%08" PRIX32 "%08" PRIX32,
47
+                  msg->id, msg->dlc, msg->data32[0], msg->data32[1]);
48
+            break;
49
+        }
50
+
51
+        case CAN2040_NOTIFY_ERROR: {
52
+            debug("err id=%" PRIX32 " dlc=%" PRIX32 " data=0x%08" PRIX32 "%08" PRIX32,
53
+                  msg->id, msg->dlc, msg->data32[0], msg->data32[1]);
54
+            break;
55
+        }
56
+    }
57
+}
58
+
59
+static void PIOx_IRQHandler(void) {
60
+    can2040_pio_irq_handler(&bus);
61
+}
62
+
63
+void can_init(void) {
64
+    can2040_setup(&bus, pio_num);
65
+    can2040_callback_config(&bus, can2040_cb);
66
+
67
+    irq_set_exclusive_handler(PIO_IRQ, PIOx_IRQHandler);
68
+    NVIC_SetPriority(PIO_IRQ, 1);
69
+    NVIC_EnableIRQ(PIO_IRQ);
70
+
71
+    can2040_start(&bus, sys_clock, bitrate, gpio_rx, gpio_tx);
72
+}
73
+
74
+void can_hello(void) {
75
+    static uint32_t prev = 0;
76
+    uint32_t now = to_ms_since_boot(get_absolute_time());
77
+
78
+    if ((now - prev) >= 1000) {
79
+        prev = now;
80
+        struct can2040_msg msg = {0};
81
+        msg.id = 0x42;
82
+        int r = can2040_transmit(&bus, &msg);
83
+        if (r != 0) {
84
+            debug("error sending msg: %d", r);
85
+        }
86
+    }
87
+}

+ 5
- 0
firmware/src/main.c View File

25
 #include "buttons.h"
25
 #include "buttons.h"
26
 #include "lcd.h"
26
 #include "lcd.h"
27
 #include "hw_id.h"
27
 #include "hw_id.h"
28
+#include "can.h"
28
 #include "main.h"
29
 #include "main.h"
29
 
30
 
30
 void main_loop_hw(void) {
31
 void main_loop_hw(void) {
55
     lcd_init();
56
     lcd_init();
56
     lcd_splash_version();
57
     lcd_splash_version();
57
 
58
 
59
+    can_init();
60
+
58
     debug("go");
61
     debug("go");
59
 
62
 
60
     while (1) {
63
     while (1) {
64
+        can_hello();
65
+
61
         main_loop_hw();
66
         main_loop_hw();
62
         //buttons_run();
67
         //buttons_run();
63
         cnsl_run();
68
         cnsl_run();

Loading…
Cancel
Save