Browse Source

simple msg queue for can

Thomas Buck 4 weeks 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
     ${CMAKE_CURRENT_LIST_DIR}/src/can.c
44
     ${CMAKE_CURRENT_LIST_DIR}/src/can.c
45
 
45
 
46
     ${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
47
     ${CMAKE_CURRENT_LIST_DIR}/can2040/src/can2040.c
49
 )
48
 )
50
 
49
 
51
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
50
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
52
-
53
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306)
51
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-ssd1306)
54
-
55
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/can2040/src)
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
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Device/RaspberryPi/RP2040/Include)
55
 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)
56
 target_include_directories(dispensy PUBLIC ${CMAKE_CURRENT_LIST_DIR}/pico-sdk/src/rp2_common/cmsis/stub/CMSIS/Core/Include)
58
 
57
 
62
     -Wextra
61
     -Wextra
63
     -Werror
62
     -Werror
64
 
63
 
64
+    # can2040 requires at least O2
65
     -O3
65
     -O3
66
 
66
 
67
     -DSSD1306_DEBUG_PRINT=debug
67
     -DSSD1306_DEBUG_PRINT=debug
76
 set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/pico-sdk/lib/btstack/src/classic/goep_client.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)
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)
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)
79
 set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/can2040/src/can2040.c PROPERTIES COMPILE_FLAGS -Wno-unused-parameter)
81
 
80
 
82
 # repo meta data
81
 # repo meta data
103
 target_compile_definitions(dispensy PUBLIC PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1)
102
 target_compile_definitions(dispensy PUBLIC PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1)
104
 
103
 
105
 pico_add_extra_outputs(dispensy)
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
 #define __CAN_H__
20
 #define __CAN_H__
21
 
21
 
22
 void can_init(void);
22
 void can_init(void);
23
+void can_run(void);
23
 
24
 
24
 void can_hello(void);
25
 void can_hello(void);
25
 
26
 

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

21
 
21
 
22
 #include "config.h"
22
 #include "config.h"
23
 #include "log.h"
23
 #include "log.h"
24
+#include "ring.h"
24
 #include "can.h"
25
 #include "can.h"
25
 
26
 
26
 #define PIO_IRQ PIO0_IRQ_0_IRQn
27
 #define PIO_IRQ PIO0_IRQ_0_IRQn
32
 
33
 
33
 static struct can2040 bus;
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
 static void can2040_cb(struct can2040 *cd, uint32_t notify, struct can2040_msg *msg) {
43
 static void can2040_cb(struct can2040 *cd, uint32_t notify, struct can2040_msg *msg) {
36
     (void)cd;
44
     (void)cd;
37
 
45
 
38
     switch (notify) {
46
     switch (notify) {
39
         case CAN2040_NOTIFY_RX: {
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
             break;
56
             break;
43
         }
57
         }
44
 
58
 
45
         case CAN2040_NOTIFY_TX: {
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
             break;
62
             break;
49
         }
63
         }
50
 
64
 
51
         case CAN2040_NOTIFY_ERROR: {
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
             break;
68
             break;
55
         }
69
         }
56
     }
70
     }
71
     can2040_start(&bus, sys_clock, bitrate, gpio_rx, gpio_tx);
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
 void can_hello(void) {
101
 void can_hello(void) {
75
     static uint32_t prev = 0;
102
     static uint32_t prev = 0;
76
     uint32_t now = to_ms_since_boot(get_absolute_time());
103
     uint32_t now = to_ms_since_boot(get_absolute_time());

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

31
 #define CNSL_BUFF_SIZE 64
31
 #define CNSL_BUFF_SIZE 64
32
 #define CNSL_REPEAT_MS 500
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
 static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
34
 static char cnsl_line_buff[CNSL_BUFF_SIZE + 1];
49
 static uint32_t cnsl_buff_pos = 0;
35
 static uint32_t cnsl_buff_pos = 0;
50
 
36
 
166
 
152
 
167
             usb_cdc_write((const uint8_t *)"\b \b", 3);
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
             // check for another backspace in this space
155
             // check for another backspace in this space
174
             i--;
156
             i--;
175
         } else {
157
         } else {
176
             usb_cdc_write((const uint8_t *)(cnsl_line_buff + i), 1);
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