-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathCAN.cpp
More file actions
380 lines (334 loc) · 8.22 KB
/
CAN.cpp
File metadata and controls
380 lines (334 loc) · 8.22 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* CAN.cpp
*
* Created on: 21.06.2021
* Author: Yannick
*/
#include "target_constants.h"
#ifdef CANBUS
#include "CAN.h"
ClassIdentifier CANPort::info = {
.name = "Can port",
.id = CLSID_CANPORT,
.visibility = ClassVisibility::visible};
CANPort::CANPort(CAN_HandleTypeDef &hcan) : CommandHandler("can", CLSID_CANPORT, 0), hcan(&hcan), silentPin(nullptr) {
//HAL_CAN_Start(this->hcan);
restoreFlash();
#ifdef CAN_COMMANDS_DISABLED_IF_NOT_USED
this->setCommandsEnabled(false);
#endif
registerCommands();
}
CANPort::CANPort(CAN_HandleTypeDef &hcan,const OutputPin* silentPin) : CommandHandler("can", CLSID_CANPORT, 0), hcan(&hcan), silentPin(silentPin) {
//HAL_CAN_Start(this->hcan);
restoreFlash();
registerCommands();
}
void CANPort::registerCommands(){
CommandHandler::registerCommands();
registerCommand("speed", CanPort_commands::speed, "CAN speed preset (0:50k;1:100k;2:125k;3:250k;4:500k;5:1M)", CMDFLAG_GET|CMDFLAG_SET|CMDFLAG_INFOSTRING);
registerCommand("send", CanPort_commands::send, "Send CAN frame. Adr&Value required", CMDFLAG_SETADR);
registerCommand("len", CanPort_commands::len, "Set length of next frames", CMDFLAG_SET|CMDFLAG_GET);
}
void CANPort::saveFlash(){
if(this->getCommandHandlerInfo()->instance != 0){
return; // Only first instance can save
}
uint16_t data = (this->speedPreset & 0b111); // set the baudrate in 0..2 bit
Flash_Write(ADR_CANCONF1, data);
}
void CANPort::restoreFlash(){
if(this->getCommandHandlerInfo()->instance != 0){
return; // Only first instance can save
}
uint16_t data;
if(Flash_Read(ADR_CANCONF1, &data)){
setSpeedPreset(data & 0b111);
}
}
CANPort::~CANPort() {
// removes all filters
for (uint8_t i = 0; i < canFilters.size(); i++){
canFilters[i].FilterActivation = false;
HAL_CAN_ConfigFilter(this->hcan, &canFilters[i]);
}
canFilters.clear();
HAL_CAN_Stop(this->hcan);
if(silentPin){
silentPin->set(); // set pin high to disable
}
}
/**
* Signals that this port is being used.
* Increments the user counter
*/
void CANPort::takePort(){
if(portUsers++ == 0){
start();
}
}
/**
* Signals that the port is not needed anymore.
* Decrements the user counter
*/
void CANPort::freePort(){
if(portUsers>0){
portUsers--;
}
if(portUsers == 0){
stop();
}
}
/**
* Enables the can port
*/
bool CANPort::start(){
setSilentMode(false);
active = true;
#ifdef CAN_COMMANDS_DISABLED_IF_NOT_USED
this->setCommandsEnabled(true);
#endif
setSpeedPreset(this->speedPreset); // Set preset again for a safe state
return HAL_CAN_Start(this->hcan) == HAL_OK;
}
/**
* Disables the can port
*/
bool CANPort::stop(){
setSilentMode(true);
active = false;
#ifdef CAN_COMMANDS_DISABLED_IF_NOT_USED
this->setCommandsEnabled(false);
#endif
return HAL_CAN_Start(this->hcan) == HAL_OK;
}
uint32_t CANPort::getSpeed(){
return presetToSpeed(speedPreset);
}
uint8_t CANPort::getSpeedPreset(){
return (speedPreset);
}
/**
* Converts a numeric speed in bits/s to the matching preset ID
*/
uint8_t CANPort::speedToPreset(uint32_t speed){
uint8_t preset = 255;
switch(speed){
case 50000:
preset = CANSPEEDPRESET_50;
break;
case 100000:
preset = CANSPEEDPRESET_100;
break;
case 125000:
preset = CANSPEEDPRESET_125;
break;
case 250000:
preset = CANSPEEDPRESET_250;
break;
case 500000:
preset = CANSPEEDPRESET_500;
break;
case 1000000:
preset = CANSPEEDPRESET_1000;
break;
default:
break;
}
return preset;
}
/**
* Converts a preset to bits/s
*/
uint32_t CANPort::presetToSpeed(uint8_t preset){
uint32_t speed = 0;
switch(preset){
case CANSPEEDPRESET_50:
speed = 50000;
break;
case CANSPEEDPRESET_100:
speed = 100000;
break;
case CANSPEEDPRESET_125:
speed = 125000;
break;
case CANSPEEDPRESET_250:
speed = 250000;
break;
case CANSPEEDPRESET_500:
speed = 500000;
break;
case CANSPEEDPRESET_1000:
speed = 1000000;
break;
default:
break;
}
return speed;
}
/**
* Changes the speed of the CAN port to a preset
*/
void CANPort::setSpeedPreset(uint8_t preset){
if(preset > 5)
return;
speedPreset = preset;
takeSemaphore();
HAL_CAN_Stop(this->hcan);
HAL_CAN_AbortTxRequest(hcan, txMailbox);
#ifdef HW_ESP32SX
const uint32_t rate[6]={50000, 100000, 125000, 250000, 500000, 1000000}; //bit/s
glue_can_set_speed(this->hcan, rate[preset]);
#else
this->hcan->Instance->BTR = canSpeedBTR_preset[preset];
#endif
HAL_CAN_ResetError(hcan);
HAL_CAN_Start(this->hcan);
giveSemaphore();
}
/**
* Changes the speed of the CAN port in bits/s
* Must match a preset speed
*/
void CANPort::setSpeed(uint32_t speed){
uint8_t preset = speedToPreset(speed);
setSpeedPreset(preset);
}
void CANPort::takeSemaphore(){
bool isIsr = inIsr();
BaseType_t taskWoken = 0;
if(isIsr)
this->semaphore.TakeFromISR(&taskWoken);
else
this->semaphore.Take();
//isTakenFlag = true;
portYIELD_FROM_ISR(taskWoken);
}
void CANPort::giveSemaphore(){
bool isIsr = inIsr();
BaseType_t taskWoken = 0;
if(isIsr)
this->semaphore.GiveFromISR(&taskWoken);
else
this->semaphore.Give();
//isTakenFlag = false;
portYIELD_FROM_ISR(taskWoken);
}
/**
* Sets the can port passive if a transceiver with silent mode is available
*/
void CANPort::setSilentMode(bool silent){
this->silent = silent;
if(silentPin){
silentPin->write(silent);
}
}
/**
* Transmits a CAN frame on this port
* Wraps the internal transmit function
*/
bool CANPort::sendMessage(CAN_tx_msg& msg){
return this->sendMessage(&msg.header,msg.data,&this->txMailbox);
}
/**
* Transmits a CAN frame with separate data and header settings
*/
bool CANPort::sendMessage(CAN_TxHeaderTypeDef *pHeader, uint8_t aData[],uint32_t *pTxMailbox){
if(this->silent)
setSilentMode(false);
if(pTxMailbox == nullptr){
pTxMailbox = &this->txMailbox;
}
takeSemaphore();
//this->isTakenFlag = true;
if (HAL_CAN_AddTxMessage(this->hcan, pHeader, aData, pTxMailbox) != HAL_OK)
{
/* Transmission request Error */
giveSemaphore();
return false;
}
giveSemaphore();
return true;
}
/**
* Adds a filter to the can handle
* Returns a free bank id if successfull and -1 if all banks are full
* Use the returned id to disable the filter again
*/
int32_t CANPort::addCanFilter(CAN_FilterTypeDef sFilterConfig){
takeSemaphore();
int32_t lowestId = sFilterConfig.FilterFIFOAssignment == CAN_RX_FIFO0 ? 0 : slaveFilterStart;
int32_t highestId = sFilterConfig.FilterFIFOAssignment == CAN_RX_FIFO0 ? slaveFilterStart : 29;
int32_t foundId = -1;
for(uint8_t id = lowestId; id < highestId ; id++ ){
for(CAN_FilterTypeDef filter : canFilters){
if(filter.FilterFIFOAssignment == sFilterConfig.FilterFIFOAssignment && id == filter.FilterBank){
break;
}
}
foundId = id;
break;
}
if(foundId < highestId){
if (HAL_CAN_ConfigFilter(this->hcan, &sFilterConfig) == HAL_OK){
canFilters.push_back(sFilterConfig);
}
}
giveSemaphore();
return foundId;
}
/**
* Disables a can filter
* Use the id returned by the addCanFilter function
*/
void CANPort::removeCanFilter(uint8_t filterId){
semaphore.Take();
for (uint8_t i = 0; i < canFilters.size(); i++){
if(canFilters[i].FilterBank == filterId){
canFilters[i].FilterActivation = false;
HAL_CAN_ConfigFilter(this->hcan, &canFilters[i]);
canFilters.erase(canFilters.begin()+i);
break;
}
}
semaphore.Give();
}
CommandStatus CANPort::command(const ParsedCommand& cmd,std::vector<CommandReply>& replies){
switch(static_cast<CanPort_commands>(cmd.cmdId)){
case CanPort_commands::speed:
if(cmd.type == CMDtype::get){
replies.emplace_back(this->speedPreset);
}else if(cmd.type == CMDtype::set){
setSpeedPreset(cmd.val);
}else if(cmd.type == CMDtype::info){
for(uint8_t i = 0; i<SpeedNames.size();i++){
replies.emplace_back(SpeedNames[i] + ":" + std::to_string(i));
}
}
break;
case CanPort_commands::send:
{
if(cmd.type == CMDtype::setat){
if(!active){
start(); // If port is not used activate port at first use
}
CAN_tx_msg msg;
memcpy(msg.data,&cmd.val,8);
header.StdId = cmd.adr;
msg.header = header;
sendMessage(msg);
}else{
return CommandStatus::NOT_FOUND;
}
break;
}
case CanPort_commands::len:
handleGetSet(cmd, replies, header.DLC);
header.DLC = std::min<uint32_t>(header.DLC,8);
break;
default:
return CommandStatus::NOT_FOUND;
}
return CommandStatus::OK;
}
#endif