Browse Source

simple msg queue for can

Thomas Buck 1 week ago
parent
commit
7c66120d07
4 changed files with 44 additions and 32 deletions
  1. 10
    4
      firmware/CMakeLists.txt
  2. 1
    0
      firmware/include/can.h
  3. 33
    6
      firmware/src/can.c
  4. 0
    22
      firmware/src/console.c

+ 10
- 4
firmware/CMakeLists.txt View File

@@ -44,15 +44,14 @@ target_sources(dispensy PUBLIC
44 44
     ${CMAKE_CURRENT_LIST_DIR}/src/can.c
45 45
 
46 46
     ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306/ssd1306.c
47
-
48 47
     ${CMAKE_CURRENT_LIST_DIR}/can2040/src/can2040.c
49 48
 )
50 49
 
51 50
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
52
-
53 51
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306)
54
-
55 52
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/can2040/src)
53
+
54
+# https://github.com/KevinOConnor/can2040/blob/master/docs/API.md
56 55
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Device/RaspberryPi/RP2040/Include)
57 56
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Core/Include)
58 57
 
@@ -62,6 +61,7 @@ target_compile_options(dispensy PUBLIC
62 61
     -Wextra
63 62
     -Werror
64 63
 
64
+    # can2040 requires at least O2
65 65
     -O3
66 66
 
67 67
     -DSSD1306_DEBUG_PRINT=debug
@@ -76,7 +76,6 @@ set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/c
76 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 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 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 79
 set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/can2040/src/can2040.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
81 80
 
82 81
 # repo meta data
@@ -103,3 +102,10 @@ target_link_libraries(dispensy
103 102
 target_compile_definitions(dispensy PUBLIC PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1)
104 103
 
105 104
 pico_add_extra_outputs(dispensy)
105
+
106
+# print binary size
107
+# https://github.com/raspberrypi/pico-sdk/issues/1196#issuecomment-1429404558
108
+add_custom_command(TARGET dispensy
109
+    POST_BUILD
110
+    COMMAND ${PICO_GCC_TRIPLE}-size --format=gnu ${CMAKE_CURRENT_BINARY_DIR}/dispensy.elf
111
+)

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

@@ -20,6 +20,7 @@
20 20
 #define __CAN_H__
21 21
 
22 22
 void can_init(void);
23
+void can_run(void);
23 24
 
24 25
 void can_hello(void);
25 26
 

+ 33
- 6
firmware/src/can.c View File

@@ -21,6 +21,7 @@
21 21
 
22 22
 #include "config.h"
23 23
 #include "log.h"
24
+#include "ring.h"
24 25
 #include "can.h"
25 26
 
26 27
 #define PIO_IRQ PIO0_IRQ_0_IRQn
@@ -32,25 +33,38 @@ static const uint gpio_tx = 24;
32 33
 
33 34
 static struct can2040 bus;
34 35
 
36
+#define MSG_BUF_LEN 128
37
+static struct can2040_msg msg_buff[MSG_BUF_LEN] = {0};
38
+static struct ring_buffer msg_rb = RB_INIT(msg_buff, MSG_BUF_LEN, sizeof(struct can2040_msg));
39
+
40
+static bool rx_buff_overflow = false;
41
+static bool can_err = false;
42
+
35 43
 static void can2040_cb(struct can2040 *cd, uint32_t notify, struct can2040_msg *msg) {
36 44
     (void)cd;
37 45
 
38 46
     switch (notify) {
39 47
         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]);
48
+            //debug("rx id=%" PRIX32 " dlc=%" PRIX32 " data=0x%08" PRIX32 "%08" PRIX32,
49
+            //      msg->id, msg->dlc, msg->data32[0], msg->data32[1]);
50
+
51
+            if (rb_space(&msg_rb) > 0) {
52
+                rb_push(&msg_rb, msg);
53
+            } else {
54
+                rx_buff_overflow = true;
55
+            }
42 56
             break;
43 57
         }
44 58
 
45 59
         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]);
60
+            //debug("tx id=%" PRIX32 " dlc=%" PRIX32 " data=0x%08" PRIX32 "%08" PRIX32,
61
+            //      msg->id, msg->dlc, msg->data32[0], msg->data32[1]);
48 62
             break;
49 63
         }
50 64
 
51 65
         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]);
66
+            debug("can err");
67
+            can_err = true;
54 68
             break;
55 69
         }
56 70
     }
@@ -71,6 +85,19 @@ void can_init(void) {
71 85
     can2040_start(&bus, sys_clock, bitrate, gpio_rx, gpio_tx);
72 86
 }
73 87
 
88
+static void handle_message(struct can2040_msg *msg) {
89
+    debug("rx id=%" PRIX32 " dlc=%" PRIX32 " data=0x%08" PRIX32 "%08" PRIX32,
90
+          msg->id, msg->dlc, msg->data32[0], msg->data32[1]);
91
+}
92
+
93
+void can_run(void) {
94
+    while (rb_len(&msg_rb) > 0) {
95
+        struct can2040_msg msg = {0};
96
+        rb_pop(&msg_rb, &msg);
97
+        handle_message(&msg);
98
+    }
99
+}
100
+
74 101
 void can_hello(void) {
75 102
     static uint32_t prev = 0;
76 103
     uint32_t now = to_ms_since_boot(get_absolute_time());

+ 0
- 22
firmware/src/console.c View File

@@ -31,20 +31,6 @@
31 31
 #define CNSL_BUFF_SIZE 64
32 32
 #define CNSL_REPEAT_MS 500
33 33
 
34
-#define DEV_AUTO_CONNECT(s) {                                     \
35
-    ble_scan(BLE_SCAN_OFF);                                       \
36
-    bd_addr_t addr;                                               \
37
-    bd_addr_type_t type;                                          \
38
-    const char *foo = s;                                          \
39
-    sscanf(foo, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX %hhu", \
40
-            &addr[0], &addr[1], &addr[2], &addr[3],               \
41
-            &addr[4], &addr[5], &type);                           \
42
-    ble_connect(addr, type);                                      \
43
-    while (!ble_is_connected()) {                                 \
44
-        sleep_ms(1);                                              \
45
-    }                                                             \
46
-}
47
-
48 34
 static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
49 35
 static uint32_t cnsl_buff_pos = 0;
50 36
 
@@ -166,18 +152,10 @@ void cnsl_handle_input(const void *buf, size_t len) {
166 152
 
167 153
             usb_cdc_write((const uint8_t *)"\b \b", 3);
168 154
 
169
-#ifndef NDEBUG
170
-            serial_write((const uint8_t *)"\b \b", 3);
171
-#endif
172
-
173 155
             // check for another backspace in this space
174 156
             i--;
175 157
         } else {
176 158
             usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
177
-
178
-#ifndef NDEBUG
179
-            serial_write((const uint8_t *)(cnsl_line_buff + i), 1);
180
-#endif
181 159
         }
182 160
     }
183 161
 

Loading…
Cancel
Save