Skip to content

Commit e0ea234

Browse files
annaw212colescott
authored andcommitted
Add Mbed CAN code
1 parent 05e21da commit e0ea234

File tree

3 files changed

+15
-32
lines changed

3 files changed

+15
-32
lines changed

bms/src/BmsConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
// This allows for all files to access the serial output
99
extern Serial* serial;
1010

11+
// Global pointer to can bus object
12+
//
13+
// This allows for all files to access the can bus output
14+
extern CAN* canBus;
15+
1116

1217
//
1318
// BMS Master Configuration

bms/src/BmsThread.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,17 @@ class BMSThread {
202202
serial->printf("Current: %d\n", gCurrent);
203203
canTransmit(&BMS_CAN_DRIVER, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
204204
}
205-
{
206-
auto txmsg = BMSStatMessage(allBanksVoltage / 10, maxVoltage,
207-
minVoltage, maxTemp, minTemp);
208-
canTransmit(&BMS_CAN_DRIVER, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
209-
}
210-
205+
*/
206+
canBus->write(BMSStatMessage(allBanksVoltage / 10, maxVoltage, minVoltage, maxTemp, minTemp));
207+
211208
// Send CAN
212209
for (size_t i = 0; i < BMS_BANK_COUNT; i++) {
213-
auto txmsg = BMSTempMessage(i, allTemps + (BMS_BANK_TEMP_COUNT * i));
214-
canTransmit(&BMS_CAN_DRIVER, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
210+
canBus->write(BMSTempMessage(i, allTemps + (BMS_BANK_TEMP_COUNT * i)));
215211
}
216212

217213
for (size_t i = 0; i < 7; i++) {
218-
auto txmsg = BMSVoltageMessage(i, allVoltages + (4 * i));
219-
canTransmit(&BMS_CAN_DRIVER, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100));
214+
canBus->write(BMSVoltageMessage(i, allVoltages + (4 * i)));
220215
}
221-
*/
222216

223217
// Compute time elapsed since beginning of measurements and sleep for
224218
// m_delay accounting for elapsed time

bms/src/Main.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "BmsConfig.h"
2-
//#include "CanOpenPdo.h"
32
//#include "common.h"
3+
#include "Can.h"
44

55
#include "mbed.h"
66
#include "rtos.h"
@@ -33,29 +33,15 @@
3333
// CANOptions::config<CANOptions::BaudRate::k500k, false>();
3434

3535
Serial* serial;
36+
CAN* canBus;
3637

3738
void initIO();
3839

3940
int main() {
4041
// Init all io pins
4142
initIO();
4243

43-
/*
44-
canStart(&BMS_CAN_DRIVER, &cancfg);
45-
CANTxFrame txmsg;
46-
txmsg.IDE = CAN_IDE_STD;
47-
txmsg.RTR = CAN_RTR_DATA;
48-
txmsg.SID = kFuncIdCellStartup;
49-
txmsg.DLC = 8;
50-
uint8_t msg[8] = {'S', 'P', 'I', 'C', 'Y', 'B', 'O', 'I'};
51-
for (size_t i = 0; i < 8; i++) {
52-
txmsg.data8[i] = msg[i];
53-
}
54-
if (canTransmit(&BMS_CAN_DRIVER, CAN_ANY_MAILBOX, &txmsg, TIME_MS2I(100)) !=
55-
MSG_OK) {
56-
sdWrite(&SD2, (const uint8_t*)"Can startup failed\r\n", 20);
57-
}
58-
*/
44+
canBus->write(BMSCellStartup());
5945

6046
ThisThread::sleep_for(1000);
6147

@@ -89,6 +75,8 @@ void initIO() {
8975
serial = new Serial(USBTX, USBRX);
9076
serial->printf("INIT\n");
9177

78+
canBus = new CAN(BMS_PIN_CAN_TX, BMS_PIN_CAN_RX, BMS_CAN_FREQUENCY);
79+
9280
// Set modes for IO
9381
/*
9482
palSetLineMode(LINE_BMS_FLT, PAL_MODE_OUTPUT_PUSHPULL);
@@ -117,8 +105,4 @@ void initIO() {
117105
palSetLineMode(LINE_SPI_SSEL,
118106
PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
119107
*/
120-
121-
// Set modes for CAN
122-
//palSetLineMode(LINE_CAN_TX, PAL_MODE_ALTERNATE(9) | PAL_STM32_OSPEED_HIGHEST);
123-
//palSetLineMode(LINE_CAN_RX, PAL_MODE_ALTERNATE(9));
124108
}

0 commit comments

Comments
 (0)