-
Notifications
You must be signed in to change notification settings - Fork 12
/
comm_can.c
executable file
·174 lines (146 loc) · 4.82 KB
/
comm_can.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "comm_can.h"
#include "ch.h"
#include "hal.h"
#include "hw_conf.h"
#include "datatypes.h"
#include <string.h>
#include "config.h"
#include "utils.h"
#include "controller.h"
#define RX_FRAMES_SIZE 100
static const CANConfig cancfg = {
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
};
static THD_WORKING_AREA(can_read_thread_wa, 512);
static THD_WORKING_AREA(can_status_thread_wa, 1024);
static THD_WORKING_AREA(can_process_thread_wa, 4096);
static THD_FUNCTION(can_read_thread, arg);
static THD_FUNCTION(can_status_thread, arg);
static THD_FUNCTION(can_process_thread, arg);
static CANRxFrame rx_frames[RX_FRAMES_SIZE];
static int rx_frame_read;
static int rx_frame_write;
static thread_t *process_tp;
static mutex_t can_mtx;
static volatile Config *config;
static bool can_command;
void comm_can_init(void)
{
config = config_get_configuration();
chMtxObjectInit(&can_mtx);
canStart(&CAN_DEV, &cancfg);
chThdCreateStatic(can_read_thread_wa, sizeof(can_read_thread_wa), NORMALPRIO + 1, can_read_thread, NULL);
chThdCreateStatic(can_status_thread_wa, sizeof(can_status_thread_wa), NORMALPRIO, can_status_thread, NULL);
if (config->commInterface == CAN)
{
process_tp = chThdGetSelfX();
can_command = true;
}
else
{
can_command = false;
chThdCreateStatic(can_process_thread_wa, sizeof(can_process_thread_wa), NORMALPRIO, can_process_thread, NULL);
}
}
static THD_FUNCTION(can_read_thread, arg) {
(void)arg;
chRegSetThreadName("CAN read");
event_listener_t el;
CANRxFrame rxmsg;
chEvtRegister(&CAN_DEV.rxfull_event, &el, 0);
while(!chThdShouldTerminateX()) {
if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(10)) == 0) {
continue;
}
msg_t result = canReceive(&CAN_DEV, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
while (result == MSG_OK) {
rx_frames[rx_frame_write++] = rxmsg;
if (rx_frame_write == RX_FRAMES_SIZE) {
rx_frame_write = 0;
}
chEvtSignal(process_tp, (eventmask_t) 1);
result = canReceive(&CAN_DEV, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
}
}
chEvtUnregister(&CAND1.rxfull_event, &el);
}
static THD_FUNCTION(can_status_thread, arg) {
(void)arg;
chRegSetThreadName("CAN status");
for(;;) {
systime_t time = chVTGetSystemTimeX();
if (config->sendStatusCAN) {
uint32_t send_index = 0;
uint8_t buffer[8];
utils_append_float32(buffer, controller_get_erpm(), &send_index);
utils_append_float32(buffer, controller_get_current_q(), &send_index);
/*utils_append_uint32(buffer, (uint32_t)controller_get_fault(), &send_index);*/
comm_can_transmit(CAN_BROADCAST, CAN_PACKET_INFINITY_STATUS, buffer, send_index);
}
systime_t sleep_time = CH_CFG_ST_FREQUENCY / config->CANStatusRate;
if (sleep_time == 0) {
sleep_time = 1;
}
chThdSleepUntil(time + sleep_time);
}
}
static THD_FUNCTION(can_process_thread, arg) {
(void)arg;
chRegSetThreadName("CAN process");
process_tp = chThdGetSelfX();
int32_t ind = 0;
for(;;) {
comm_can_update();
}
}
void comm_can_update(void)
{
chEvtWaitAny((eventmask_t) 1);
static int32_t ind = 0;
while (rx_frame_read != rx_frame_write) {
CANRxFrame rxmsg = rx_frames[rx_frame_read++];
if (rxmsg.IDE == CAN_IDE_EXT) {
uint8_t sender = rxmsg.EID & 0xFF;
uint8_t receiver = (rxmsg.EID >> 8) & 0xFF;
CANPacketID id = rxmsg.EID >> 16;
if ((receiver == CAN_BROADCAST || receiver == config->CANDeviceID) && sender != config->CANDeviceID) {
switch (id) {
case CAN_PACKET_INFINITY_SET_CURRENT:
if (can_command)
{
ind = 0;
}
break;
case CAN_PACKET_INFINITY_STATUS:
break;
default:
break;
}
}
}
if (rx_frame_read == RX_FRAMES_SIZE) {
rx_frame_read = 0;
}
}
}
void comm_can_forward_commands(void)
{
uint32_t len = 0;
uint8_t buffer[4];
utils_append_float32(buffer, controller_get_command_current(), &len);
comm_can_transmit(CAN_BROADCAST, CAN_PACKET_INFINITY_SET_CURRENT, buffer, len);
}
void comm_can_transmit(uint8_t receiver, CANPacketID packetID, uint8_t *data, uint8_t len)
{
CANTxFrame txmsg;
txmsg.IDE = CAN_IDE_EXT;
txmsg.EID = (uint32_t)(config->CANDeviceID | ((uint32_t)receiver << 8) | ((uint32_t)packetID << 16));
txmsg.RTR = CAN_RTR_DATA;
txmsg.DLC = len;
memcpy(txmsg.data8, data, len);
chMtxLock(&can_mtx);
canTransmit(&CAN_DEV, CAN_ANY_MAILBOX, &txmsg, MS2ST(20));
chMtxUnlock(&can_mtx);
}