|
@@ -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());
|