forked from corygrant/dingoFW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailbox.cpp
More file actions
147 lines (130 loc) · 4.05 KB
/
Copy pathmailbox.cpp
File metadata and controls
147 lines (130 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "mailbox.h"
#include "ch.hpp"
static chibios_rt::Mailbox<CANRxFrame*, MAILBOX_SIZE> rxMb;
static chibios_rt::Mailbox<CANTxFrame*, MAILBOX_SIZE> txMb;
static chibios_rt::Mailbox<CANTxFrame*, MAILBOX_SIZE> txUsbMb;
//Mailbox buffer of CAN frames
//Not managed by mailbox
CANRxFrame rxFrames[MAILBOX_SIZE];
CANTxFrame txFrames[MAILBOX_SIZE];
CANTxFrame txUsbFrames[MAILBOX_SIZE];
//Used to manage the memory used by the mailbox
bool rxMsgUsed[MAILBOX_SIZE];
bool txMsgUsed[MAILBOX_SIZE];
bool txUsbMsgUsed[MAILBOX_SIZE];
msg_t PostTxFrame(CANTxFrame *frame)
{
PostTxUsbFrame(frame); // Post to USB mailbox first
for (int i = 0; i < MAILBOX_SIZE; i++) {
if (!txMsgUsed[i]) {
// Find a free slot in can_messages[] to store the data
txFrames[i] = *frame;
txMsgUsed[i] = true;
// Try to post the pointer to the mailbox
msg_t result = txMb.post(&txFrames[i], TIME_IMMEDIATE);
if (result == MSG_OK) {
return result; // Successfully posted
} else {
txMsgUsed[i] = false; // Free the slot if mailbox is full
return result;
}
}
}
return MSG_TIMEOUT; // No free slots
}
msg_t FetchTxFrame(CANTxFrame *frame)
{
CANTxFrame *txFrame;
// Fetch a pointer from the mailbox
msg_t result = txMb.fetch(&txFrame, TIME_IMMEDIATE);
if (result == MSG_OK) {
// Mark the slot in memory as free
for (int i = 0; i < MAILBOX_SIZE; i++) {
if (txFrame == &txFrames[i]) {
txMsgUsed[i] = false;
break;
}
}
*frame = *txFrame;
return result;
}
return result;
}
msg_t PostTxUsbFrame(CANTxFrame *frame)
{
for (int i = 0; i < MAILBOX_SIZE; i++) {
if (!txUsbMsgUsed[i]) {
// Find a free slot in can_messages[] to store the data
txUsbFrames[i] = *frame;
txUsbMsgUsed[i] = true;
// Try to post the pointer to the mailbox
msg_t result = txUsbMb.post(&txUsbFrames[i], TIME_IMMEDIATE);
if (result == MSG_OK) {
return result; // Successfully posted
} else {
txUsbMsgUsed[i] = false; // Free the slot if mailbox is full
return result;
}
}
}
return MSG_TIMEOUT; // No free slots
}
msg_t FetchTxUsbFrame(CANTxFrame *frame)
{
CANTxFrame *txFrame;
// Fetch a pointer from the mailbox
msg_t result = txUsbMb.fetch(&txFrame, TIME_IMMEDIATE);
if (result == MSG_OK) {
// Mark the slot in memory as free
for (int i = 0; i < MAILBOX_SIZE; i++) {
if (txFrame == &txUsbFrames[i]) {
txUsbMsgUsed[i] = false;
break;
}
}
*frame = *txFrame;
return result;
}
return result;
}
msg_t PostRxFrame(CANRxFrame *frame)
{
for (int i = 0; i < MAILBOX_SIZE; i++) {
if (!rxMsgUsed[i]) {
// Find a free slot in can_messages[] to store the data
rxFrames[i] = *frame;
rxMsgUsed[i] = true;
// Try to post the pointer to the mailbox
msg_t result = rxMb.post(&rxFrames[i], TIME_IMMEDIATE);
if (result == MSG_OK) {
return result; // Successfully posted
} else {
rxMsgUsed[i] = false; // Free the slot if mailbox is full
return result;
}
}
}
return MSG_TIMEOUT; // No free slots
}
msg_t FetchRxFrame(CANRxFrame *frame)
{
CANRxFrame *rxFrame;
// Fetch a pointer from the mailbox
msg_t result = rxMb.fetch(&rxFrame, TIME_IMMEDIATE);
if (result == MSG_OK) {
// Mark the slot in memory as free
for (int i = 0; i < MAILBOX_SIZE; i++) {
if (rxFrame == &rxFrames[i]) {
rxMsgUsed[i] = false;
break;
}
}
*frame = *rxFrame;
return result;
}
return result;
}
bool RxFramesEmpty()
{
return (rxMb.getUsedCountI() == 0);
}