Skip to content

Commit d14e9f7

Browse files
committed
Added can baud selection to all boards
1 parent 18db167 commit d14e9f7

10 files changed

Lines changed: 222 additions & 45 deletions

File tree

firmware/boards/f0_module/bootloader/bootloader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ THD_FUNCTION(BootloaderThread, arg)
225225
(void)arg;
226226

227227
// turn on CAN
228-
canStart(&CAND1, &GetCanConfig(0));
228+
canStart(&CAND1, &GetCanConfig(CanBaudRate::Baud500Kbps));
229229

230230
WaitForBootloaderCmd();
231231

firmware/boards/f0_module/port_shared.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
// board-specific stuff shared between bootloader and firmware
44

5+
static const CANConfig canConfig125 =
6+
{
7+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
8+
/*
9+
For 48MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=24, Seq 1=13 and Seq 2=2. Subtract '1' for register values
10+
*/
11+
CAN_BTR_SJW(0) | CAN_BTR_BRP(23) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
12+
};
13+
14+
static const CANConfig canConfig250 =
15+
{
16+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
17+
/*
18+
For 48MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=12, Seq 1=13 and Seq 2=2. Subtract '1' for register values
19+
*/
20+
CAN_BTR_SJW(0) | CAN_BTR_BRP(11) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
21+
};
22+
523
static const CANConfig canConfig500 =
624
{
725
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
@@ -11,6 +29,27 @@ static const CANConfig canConfig500 =
1129
CAN_BTR_SJW(0) | CAN_BTR_BRP(5) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
1230
};
1331

14-
const CANConfig& GetCanConfig(uint8_t mode) {
32+
static const CANConfig canConfig1000 =
33+
{
34+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
35+
/*
36+
For 48MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=3, Seq 1=13 and Seq 2=2. Subtract '1' for register values
37+
*/
38+
CAN_BTR_SJW(0) | CAN_BTR_BRP(2) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
39+
};
40+
41+
const CANConfig& GetCanConfig(CanBaudRate baudRate) {
42+
switch (baudRate) {
43+
case CanBaudRate::Baud500Kbps:
44+
return canConfig500;
45+
case CanBaudRate::Baud1Mbps:
46+
return canConfig1000;
47+
case CanBaudRate::Baud125Kbps:
48+
return canConfig125;
49+
case CanBaudRate::Baud250Kbps:
50+
return canConfig250;
51+
}
52+
53+
// default to 500kbps
1554
return canConfig500;
1655
}

firmware/boards/f1_dual/port_shared.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
// board-specific stuff shared between bootloader and firmware
44

5+
static const CANConfig canConfig125 =
6+
{
7+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
8+
/*
9+
For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=12, Seq 1=13 and Seq 2=2. Subtract '1' for register values
10+
*/
11+
CAN_BTR_SJW(0) | CAN_BTR_BRP(11) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
12+
};
13+
14+
static const CANConfig canConfig250 =
15+
{
16+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
17+
/*
18+
For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=6, Seq 1=13 and Seq 2=2. Subtract '1' for register values
19+
*/
20+
CAN_BTR_SJW(0) | CAN_BTR_BRP(5) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
21+
};
22+
523
static const CANConfig canConfig500 =
624
{
725
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
@@ -11,6 +29,27 @@ static const CANConfig canConfig500 =
1129
CAN_BTR_SJW(0) | CAN_BTR_BRP(2) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
1230
};
1331

14-
const CANConfig& GetCanConfig(uint8_t mode) {
32+
static const CANConfig canConfig1000 =
33+
{
34+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
35+
/*
36+
For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=2, Seq 1=10 and Seq 2=1. Subtract '1' for register values
37+
*/
38+
CAN_BTR_SJW(0) | CAN_BTR_BRP(1) | CAN_BTR_TS1(9) | CAN_BTR_TS2(0),
39+
};
40+
41+
const CANConfig& GetCanConfig(CanBaudRate baudRate) {
42+
switch (baudRate) {
43+
case CanBaudRate::Baud500Kbps:
44+
return canConfig500;
45+
case CanBaudRate::Baud1Mbps:
46+
return canConfig1000;
47+
case CanBaudRate::Baud125Kbps:
48+
return canConfig125;
49+
case CanBaudRate::Baud250Kbps:
50+
return canConfig250;
51+
}
52+
53+
// default to 500kbps
1554
return canConfig500;
1655
}

firmware/boards/f1_dual_rev1/port_shared.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ static const CANConfig canConfig1000 =
3939
};
4040

4141

42-
const CANConfig& GetCanConfig(uint8_t mode) {
43-
mode &= 0x03;
44-
switch (mode) {
45-
case 0:
42+
const CANConfig& GetCanConfig(CanBaudRate baudRate) {
43+
switch (baudRate) {
44+
case CanBaudRate::Baud500Kbps:
4645
return canConfig500;
47-
case 1:
46+
case CanBaudRate::Baud1Mbps:
4847
return canConfig1000;
48+
case CanBaudRate::Baud125Kbps:
49+
return canConfig125;
50+
case CanBaudRate::Baud250Kbps:
51+
return canConfig250;
4952
}
53+
5054
// default to 500kbps
5155
return canConfig500;
5256
}

firmware/boards/f1_rev2/port_shared.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
// board-specific stuff shared between bootloader and firmware
44

5+
static const CANConfig canConfig125 =
6+
{
7+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
8+
/*
9+
For 32MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=16, Seq 1=13 and Seq 2=2. Subtract '1' for register values
10+
*/
11+
CAN_BTR_SJW(0) | CAN_BTR_BRP(16 - 1) | CAN_BTR_TS1(13 - 1) | CAN_BTR_TS2(2 - 1),
12+
};
13+
14+
static const CANConfig canConfig250 =
15+
{
16+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
17+
/*
18+
For 32MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=8, Seq 1=13 and Seq 2=2. Subtract '1' for register values
19+
*/
20+
CAN_BTR_SJW(0) | CAN_BTR_BRP(8 - 1) | CAN_BTR_TS1(13 - 1) | CAN_BTR_TS2(2 - 1),
21+
};
22+
523
static const CANConfig canConfig500 =
624
{
725
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
@@ -11,6 +29,27 @@ static const CANConfig canConfig500 =
1129
CAN_BTR_SJW(0) | CAN_BTR_BRP(4 - 1) | CAN_BTR_TS1(13 - 1) | CAN_BTR_TS2(2 - 1),
1230
};
1331

14-
const CANConfig& GetCanConfig(uint8_t mode) {
32+
static const CANConfig canConfig1000 =
33+
{
34+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
35+
/*
36+
For 32MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=2, Seq 1=13 and Seq 2=2. Subtract '1' for register values
37+
*/
38+
CAN_BTR_SJW(0) | CAN_BTR_BRP(2 - 1) | CAN_BTR_TS1(13 - 1) | CAN_BTR_TS2(2 - 1),
39+
};
40+
41+
const CANConfig& GetCanConfig(CanBaudRate baudRate) {
42+
switch (baudRate) {
43+
case CanBaudRate::Baud500Kbps:
44+
return canConfig500;
45+
case CanBaudRate::Baud1Mbps:
46+
return canConfig1000;
47+
case CanBaudRate::Baud125Kbps:
48+
return canConfig125;
49+
case CanBaudRate::Baud250Kbps:
50+
return canConfig250;
51+
}
52+
53+
// default to 500kbps
1554
return canConfig500;
1655
}

firmware/boards/f1_rev3/port_shared.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
// board-specific stuff shared between bootloader and firmware
44

5+
static const CANConfig canConfig125 =
6+
{
7+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
8+
/*
9+
For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=12, Seq 1=13 and Seq 2=2. Subtract '1' for register values
10+
*/
11+
CAN_BTR_SJW(0) | CAN_BTR_BRP(11) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
12+
};
13+
14+
static const CANConfig canConfig250 =
15+
{
16+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
17+
/*
18+
For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=6, Seq 1=13 and Seq 2=2. Subtract '1' for register values
19+
*/
20+
CAN_BTR_SJW(0) | CAN_BTR_BRP(5) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
21+
};
22+
523
static const CANConfig canConfig500 =
624
{
725
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
@@ -11,6 +29,27 @@ static const CANConfig canConfig500 =
1129
CAN_BTR_SJW(0) | CAN_BTR_BRP(2) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1),
1230
};
1331

14-
const CANConfig& GetCanConfig(uint8_t mode) {
32+
static const CANConfig canConfig1000 =
33+
{
34+
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
35+
/*
36+
For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=2, Seq 1=10 and Seq 2=1. Subtract '1' for register values
37+
*/
38+
CAN_BTR_SJW(0) | CAN_BTR_BRP(1) | CAN_BTR_TS1(9) | CAN_BTR_TS2(0),
39+
};
40+
41+
const CANConfig& GetCanConfig(CanBaudRate baudRate) {
42+
switch (baudRate) {
43+
case CanBaudRate::Baud500Kbps:
44+
return canConfig500;
45+
case CanBaudRate::Baud1Mbps:
46+
return canConfig1000;
47+
case CanBaudRate::Baud125Kbps:
48+
return canConfig125;
49+
case CanBaudRate::Baud250Kbps:
50+
return canConfig250;
51+
}
52+
53+
// default to 500kbps
1554
return canConfig500;
1655
}

firmware/boards/port.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,37 +65,6 @@ enum class AuxOutputMode : uint8_t {
6565
IOExpander = 6,
6666
};
6767

68-
// These values are kept in sync just for convenience
69-
enum class CanAfrProtocol : uint8_t {
70-
None = 0,
71-
AemNet = 1,
72-
LinkEcu = 2,
73-
Haltech = 3,
74-
EcuMaster = 4,
75-
Motec = 6,
76-
Emtron = 7,
77-
};
78-
79-
enum class CanEgtProtocol : uint8_t {
80-
None = 0,
81-
AemNet0305 = 1,
82-
LinkEcu = 2,
83-
Haltech = 3,
84-
EcuMasterClassic = 4,
85-
EcuMasterBlack = 5,
86-
Motec = 6,
87-
Emtron = 7,
88-
AemNet2224 = 8,
89-
};
90-
91-
enum class CanIoProtocol : uint8_t {
92-
None = 0,
93-
Haltech = 3,
94-
EcuMaster = 4,
95-
Motec = 6,
96-
Emtron = 7,
97-
MsIoBox = 9,
98-
};
9968

10069
class Configuration {
10170
private:
@@ -118,7 +87,7 @@ class Configuration {
11887
*this = {};
11988

12089
NoLongerUsed0 = 0;
121-
CanMode = 0;
90+
BaudRate = CanBaudRate::Baud500Kbps;
12291
sensorType = BOARD_DEFAULT_SENSOR_TYPE;
12392

12493
/* default auxout curve is 0..5V for AFR 8.5 to 18.0
@@ -172,7 +141,7 @@ class Configuration {
172141
union {
173142
struct {
174143
uint8_t NoLongerUsed0 : 6 = 0;
175-
uint8_t CanMode : 2;
144+
CanBaudRate BaudRate : 2;
176145
// AUX0 and AUX1 curves
177146
float auxOutBins[2][8];
178147
float auxOutValues[2][8];

firmware/boards/port_shared.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#if WB_PROD
44

55
#include "hal.h"
6+
#include "../can_base.h"
67

7-
const CANConfig& GetCanConfig(uint8_t mode);
8+
const CANConfig& GetCanConfig(CanBaudRate baudRate);
89

910
#endif // WB_PROD

firmware/can.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void InitCan()
229229
{
230230
configuration = GetConfiguration();
231231

232-
canStart(&CAND1, &GetCanConfig(configuration->CanMode));
232+
canStart(&CAND1, &GetCanConfig(configuration->BaudRate));
233233
chThdCreateStatic(waCanTxThread, sizeof(waCanTxThread), NORMALPRIO, CanTxThread, nullptr);
234234
chThdCreateStatic(waCanRxThread, sizeof(waCanRxThread), NORMALPRIO - 4, CanRxThread, nullptr);
235235
}

firmware/can_base.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
/*
6+
* This file defines base hardware-independent types for CAN communication.
7+
*/
8+
9+
// Can speeds
10+
enum class CanBaudRate : uint8_t {
11+
Baud500Kbps = 0, // Default value
12+
Baud1Mbps = 1, // Next most common speed
13+
Baud125Kbps = 2,
14+
Baud250Kbps = 3,
15+
};
16+
17+
// These values are kept in sync just for convenience
18+
enum class CanAfrProtocol : uint8_t {
19+
None = 0,
20+
AemNet = 1,
21+
LinkEcu = 2,
22+
Haltech = 3,
23+
EcuMaster = 4,
24+
Motec = 6,
25+
Emtron = 7,
26+
};
27+
28+
enum class CanEgtProtocol : uint8_t {
29+
None = 0,
30+
AemNet0305 = 1,
31+
LinkEcu = 2,
32+
Haltech = 3,
33+
EcuMasterClassic = 4,
34+
EcuMasterBlack = 5,
35+
Motec = 6,
36+
Emtron = 7,
37+
AemNet2224 = 8,
38+
};
39+
40+
enum class CanIoProtocol : uint8_t {
41+
None = 0,
42+
Haltech = 3,
43+
EcuMaster = 4,
44+
Motec = 6,
45+
Emtron = 7,
46+
MsIoBox = 9,
47+
};

0 commit comments

Comments
 (0)