Skip to content

Commit fe06457

Browse files
committed
Add initial support for STM32F303K8 board
1 parent b82c389 commit fe06457

File tree

7 files changed

+60
-217
lines changed

7 files changed

+60
-217
lines changed

bms/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ flash: deps $(BUILD_DIR)
5555
#
5656

5757
.PHONY: debug
58-
debug: build-debug
58+
debug:
5959
arm-none-eabi-gdb -x .gdbinit ./$(BUILD_DIR)/$(MBED_TARGET)/$(MBED_BUILD_PROFILE)/$(MBED_TOOLCHAIN)/bms.elf
6060

6161
#

bms/mbed_app.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
{
2+
"config": {
3+
"main-stack-size": {
4+
"value": 2048
5+
},
6+
"stack-size": {
7+
"value": 1024
8+
}
9+
},
210
"target_overrides": {
311
"LPC1768": {
412
"platform.stdio-baud-rate": 115200,

bms/src/BmsConfig.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
// Global pointer to can bus object
66
//
77
// This allows for all files to access the can bus output
8-
extern CAN* canBus;
8+
//extern CAN* canBus;
99

1010
//Global pointer to DigitalOut objects for BMS_FAULT and CHARGER_CONTROL pins
1111
//
1212
//This allows for all files to access BMS_FAULT and CHARGER_CONTROL pins
13-
extern DigitalOut* bmsFault;
14-
extern DigitalOut* chargerControl;
13+
//extern DigitalOut* bmsFault;
14+
//extern DigitalOut* chargerControl;
1515

1616
//
1717
// BMS Master Configuration

bms/src/BmsThread.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
#include "LTC681xCommand.h"
55
#include "ThisThread.h"
66

7-
BMSThread::BMSThread(LTC681xBus& bus, unsigned int frequency, std::vector<Queue<BmsEvent, mailboxSize>*> mailboxes) : m_bus(bus), mailboxes(mailboxes) {
7+
BMSThread::BMSThread(LTC681xBus& bus, unsigned int frequency, std::array<BmsEventMailbox*, 2> mailboxes)
8+
: m_bus(bus)
9+
, m_chips {
10+
LTC6811(bus, 0),
11+
LTC6811(bus, 1),
12+
LTC6811(bus, 2),
13+
LTC6811(bus, 3)
14+
}
15+
, mailboxes(mailboxes)
16+
{
817
m_delay = 1000 / frequency;
9-
for (int i = 0; i < BMS_BANK_COUNT; i++) {
10-
m_chips.push_back(LTC6811(bus, i));
11-
}
1218
for (int i = 0; i < BMS_BANK_COUNT; i++) {
1319
//m_chips[i].getConfig().gpio5 = LTC6811::GPIOOutputState::kLow;
1420
//m_chips[i].getConfig().gpio4 = LTC6811::GPIOOutputState::kPassive;
@@ -19,7 +25,7 @@ BMSThread::BMSThread(LTC681xBus& bus, unsigned int frequency, std::vector<Queue<
1925

2026
void BMSThread::threadWorker() {
2127
// Perform self tests
22-
28+
//
2329
// Cell Voltage self test
2430
m_bus.WakeupBus();
2531
m_bus.SendCommand(LTC681xBus::BuildBroadcastBusCommand(StartSelfTestCellVoltage(AdcMode::k7k, SelfTestMode::kSelfTest1)));

bms/src/BmsThread.h

Lines changed: 3 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class BMSThread {
2323
public:
2424

25-
BMSThread(LTC681xBus& bus, unsigned int frequency, std::vector<BmsEventMailbox*> mailboxes);
25+
BMSThread(LTC681xBus& bus, unsigned int frequency, std::array<BmsEventMailbox*, 2> mailboxes);
2626

2727
// Function to allow for starting threads from static context
2828
static void startThread(BMSThread *p) {
@@ -92,169 +92,12 @@ class BMSThread {
9292
private:
9393
unsigned int m_delay;
9494
LTC681xBus& m_bus;
95-
std::vector<LTC6811> m_chips;
96-
std::vector<BmsEventMailbox*> mailboxes;
95+
std::array<LTC6811, BMS_BANK_COUNT> m_chips;
96+
std::array<BmsEventMailbox*, 2> mailboxes;
9797

9898
// Things that need to go away
9999
bool m_discharging = false;
100100

101101
void throwBmsFault();
102102
void threadWorker();
103-
/*
104-
void threadWorker() {
105-
std::array<uint16_t, BMS_BANK_COUNT * BMS_BANK_CELL_COUNT> allVoltages;
106-
std::array<std::optional<int8_t>, BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT>
107-
allTemps;
108-
109-
while (true) {
110-
for (int i = 0; i < BMS_BANK_COUNT; i++) {
111-
// Get a reference to the config for toggling gpio
112-
LTC6811::Configuration& conf = m_chips[i].getConfig();
113-
114-
// Turn on status LED
115-
conf.gpio5 = LTC6811::GPIOOutputState::kLow;
116-
m_chips[i].updateConfig();
117-
118-
uint16_t* voltages = m_chips[i].getVoltages();
119-
120-
int temperatures[BMS_BANK_TEMP_COUNT];
121-
122-
// Measure all temp sensors
123-
for (unsigned int j = 0; j < BMS_BANK_TEMP_COUNT; j++) {
124-
conf.gpio1 = (j & 0x01) ? LTC6811::GPIOOutputState::kHigh
125-
: LTC6811::GPIOOutputState::kLow;
126-
conf.gpio2 = (j & 0x02) ? LTC6811::GPIOOutputState::kHigh
127-
: LTC6811::GPIOOutputState::kLow;
128-
conf.gpio3 = (j & 0x04) ? LTC6811::GPIOOutputState::kHigh
129-
: LTC6811::GPIOOutputState::kLow;
130-
conf.gpio4 = LTC6811::GPIOOutputState::kPassive;
131-
m_chips[i].updateConfig();
132-
133-
// Wait for config changes to take effect
134-
ThisThread::sleep_for(3);
135-
136-
uint16_t* temps = m_chips[i].getGpioPin(GpioSelection::k4);
137-
temperatures[j] = temps[3];
138-
139-
delete temps;
140-
}
141-
142-
// Turn off status LED
143-
conf.gpio5 = LTC6811::GPIOOutputState::kHigh;
144-
m_chips[i].updateConfig();
145-
146-
// Done with communication at this point
147-
// Now time to crunch numbers
148-
149-
// Process voltages
150-
for (int j = 0; j < 12; j++) {
151-
uint16_t voltage = voltages[j] / 10;
152-
153-
int index = BMS_CELL_MAP[j];
154-
if (index != -1) {
155-
allVoltages[(BMS_BANK_CELL_COUNT * i) + index] = voltage;
156-
157-
if (voltage >= BMS_FAULT_VOLTAGE_THRESHOLD_HIGH) {
158-
// Set fault line
159-
printf("***** BMS LOW VOLTAGE FAULT *****\nVoltage at
160-
%d\n\n", voltage); throwBmsFault();
161-
}
162-
if (voltage <= BMS_FAULT_VOLTAGE_THRESHOLD_LOW) {
163-
// Set fault line
164-
printf("***** BMS HIGH VOLTAGE FAULT *****\nVoltage at
165-
%d\n\n", voltage); throwBmsFault();
166-
}
167-
168-
// Discharge cells if enabled
169-
if(m_discharging) {
170-
if((voltage > prevMinVoltage) && (voltage - prevMinVoltage >
171-
BMS_DISCHARGE_THRESHOLD)) {
172-
// Discharge
173-
174-
printf("DISCHARGE CELL %d: %dmV (%dmV)\n", index,
175-
voltage, (voltage - prevMinVoltage));
176-
177-
// Enable discharging
178-
conf.dischargeState.value |= (1 << j);
179-
} else {
180-
// Disable discharging
181-
conf.dischargeState.value &= ~(1 << j);
182-
}
183-
} else {
184-
// Disable discharging
185-
conf.dischargeState.value &= ~(1 << j);
186-
}
187-
}
188-
}
189-
190-
delete voltages;
191-
192-
193-
for (unsigned int j = 0; j < BMS_BANK_TEMP_COUNT; j++) {
194-
auto temp = convertTemp(temperatures[j] / 10);
195-
allTemps[(BMS_BANK_TEMP_COUNT * i) + j] = temp;
196-
197-
temp.map([&](auto t){
198-
if (t < minTemp) minTemp = t;
199-
if (t > maxTemp) maxTemp = t;
200-
});
201-
}
202-
allBanksVoltage += totalVoltage;
203-
}
204-
205-
averageVoltage = allBanksVoltage / (BMS_BANK_COUNT * BMS_BANK_CELL_COUNT);
206-
prevMinVoltage = minVoltage;
207-
208-
printf("Temperatures: \n");
209-
for(int i = 0; i < BMS_BANK_COUNT * BMS_BANK_CELL_COUNT; i++){
210-
allTemps[i].map_or_else([&](auto temp) {
211-
if (temp >= BMS_FAULT_TEMP_THRESHOLD_HIGH) {
212-
printf("***** BMS HIGH TEMP FAULT *****\nTemp at %d\n\n",
213-
temp); throwBmsFault(); } else if (temp <= BMS_FAULT_TEMP_THRESHOLD_LOW) {
214-
printf("***** BMS LOW TEMP FAULT *****\nTemp at %d\n\n",
215-
temp); throwBmsFault();
216-
}
217-
218-
printf("%3d ", temp);
219-
},
220-
[&]() {
221-
printf("ERR ");
222-
//printf("***** BMS INVALID TEMP FAULT *****\n");
223-
//throwBmsFault();
224-
});
225-
if((i + 1) % BMS_BANK_CELL_COUNT == 0)
226-
printf("\n");
227-
}
228-
229-
canBus->write(BMSStatMessage(allBanksVoltage / 10, maxVoltage, minVoltage,
230-
maxTemp, minTemp));
231-
232-
// Send CAN
233-
for (size_t i = 0; i < BMS_BANK_COUNT; i++) {
234-
// Convert from optional temp values to values with default of -127 (to
235-
indicate error) auto temps = std::array<int8_t, BMS_BANK_TEMP_COUNT>();
236-
std::transform(allTemps.begin() + (BMS_BANK_TEMP_COUNT * i),
237-
allTemps.begin() + (BMS_BANK_TEMP_COUNT * (i + 1)),
238-
temps.begin(),
239-
[](std::optional<int8_t> t) { return t.value_or(-127); });
240-
241-
canBus->write(BMSTempMessage(i, (uint8_t*)temps.data()));
242-
}
243-
244-
for (size_t i = 0; i < 7; i++) {
245-
canBus->write(BMSVoltageMessage(i, allVoltages + (4 * i)));
246-
}
247-
248-
// Compute time elapsed since beginning of measurements and sleep for
249-
// m_delay accounting for elapsed time
250-
// TODO: use a hardware timer or a virtual timer or literally anything
251-
// else. kek.
252-
//unsigned int timeElapsed = TIME_I2MS(chVTTimeElapsedSinceX(timeStart));
253-
#ifdef DEBUG
254-
//printf("BMS Thread time elapsed: %dms\n", timeElapsed);
255-
#endif
256-
ThisThread::sleep_for(m_delay);
257-
}
258-
}
259-
*/
260103
};

bms/src/Event.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class BmsEvent {
2525
virtual ~BmsEvent() {};
2626
};
2727

28-
static constexpr auto mailboxSize = 4;
28+
static constexpr auto mailboxSize = 1;
2929
using BmsEventMailbox = Queue<BmsEvent, mailboxSize>;
3030

3131
// Measurement

bms/src/Main.cpp

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,46 @@
1111
#include "LTC681xBus.h"
1212
#include "Event.h"
1313

14-
// When car is off maybe one reading every 10 sec
15-
// when car is on 10 readings per second
16-
17-
// TODO: map 12 cells to number of cells
18-
// TODO: change m_chips to array rather than vector
19-
// TODO: publish fault states
20-
// TODO: SoC tracking using Ah counting and voltage measuring
21-
22-
// Fault states:
23-
// - AMS
24-
// - IMD
25-
//
26-
// * Over temp
27-
// - Under temp
28-
// * Over voltage
29-
// * Under voltage
30-
//
31-
// * Failed init
32-
// * Comm fail
33-
34-
35-
CAN* canBus;
36-
DigitalOut* bmsFault;
37-
DigitalOut* chargerControl;
14+
#ifdef TARGET_NUCLEO_F303K8
15+
// Required for setting MISO to pull up on STM32
16+
#include <targets/TARGET_STM/TARGET_STM32F3/STM32Cube_FW/STM32F3xx_HAL_Driver/stm32f3xx_ll_gpio.h>
17+
#endif
18+
19+
CAN canBus = CAN(BMS_PIN_CAN_RX, BMS_PIN_CAN_TX, BMS_CAN_FREQUENCY);
20+
DigitalOut bmsFault = DigitalOut(BMS_PIN_BMS_FLT);
21+
DigitalOut chargerControl = DigitalOut(BMS_PIN_CHARGER_CONTROL);
3822

3923
void initIO();
4024

25+
// TODO: We need a better way of statically allocating thread stack
26+
constexpr size_t STACK_SIZE = 1024;
27+
unsigned char bmsThreadStack[STACK_SIZE];
28+
Thread bmsThreadThread = Thread(osPriorityNormal, STACK_SIZE, bmsThreadStack);
29+
4130
int main() {
4231
// Init all io pins
4332
initIO();
4433

45-
canBus->write(BMSCellStartup());
34+
canBus.write(BMSCellStartup());
4635

47-
ThisThread::sleep_for(1s);
36+
SPI spiDriver = SPI(BMS_PIN_SPI_MOSI,
37+
BMS_PIN_SPI_MISO,
38+
BMS_PIN_SPI_SCLK,
39+
BMS_PIN_SPI_SSEL,
40+
use_gpio_ssel);
41+
42+
#ifdef TARGET_NUCLEO_F303K8
43+
// This is a hack required to set the MISO pin to use a pull up resistor
44+
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_6, LL_GPIO_PULL_UP);
45+
#endif
4846

49-
SPI* spiDriver = new SPI(BMS_PIN_SPI_MOSI,
50-
BMS_PIN_SPI_MISO,
51-
BMS_PIN_SPI_SCLK,
52-
BMS_PIN_SPI_SSEL,
53-
use_gpio_ssel);
54-
spiDriver->format(8, 0);
55-
auto ltcBus = LTC681xParallelBus(spiDriver);
47+
spiDriver.format(8, 0);
48+
auto ltcBus = LTC681xParallelBus(&spiDriver);
5649

57-
auto canMailbox = new BmsEventMailbox();
58-
auto uiMailbox = new BmsEventMailbox();
59-
std::vector<BmsEventMailbox*> mailboxes = { canMailbox, uiMailbox };
50+
auto canMailbox = BmsEventMailbox();
51+
auto uiMailbox = BmsEventMailbox();
52+
std::array<BmsEventMailbox*, 2> mailboxes = { &canMailbox, &uiMailbox };
6053

61-
Thread bmsThreadThread;
6254
BMSThread bmsThread(ltcBus, 1, mailboxes);
6355
bmsThreadThread.start(callback(&BMSThread::startThread, &bmsThread));
6456

@@ -72,7 +64,7 @@ int main() {
7264
}
7365

7466
while(true) {
75-
auto event = canMailbox->get();
67+
auto event = canMailbox.get();
7668
if (event.status == osEventMessage) {
7769
BmsEvent* msg = (BmsEvent*)event.value.p;
7870

@@ -120,12 +112,6 @@ int main() {
120112
void initIO() {
121113
printf("INIT\n");
122114

123-
canBus = new CAN(BMS_PIN_CAN_RX, BMS_PIN_CAN_TX, BMS_CAN_FREQUENCY);
124-
125-
bmsFault = new DigitalOut(BMS_PIN_BMS_FLT);
126-
127-
chargerControl = new DigitalOut(BMS_PIN_CHARGER_CONTROL);
128-
129115
// Set modes for IO
130116
/*
131117
palSetLineMode(LINE_BMS_FLT_LAT, PAL_MODE_INPUT);
@@ -135,9 +121,9 @@ void initIO() {
135121
*/
136122

137123
// Reset BMS fault line
138-
bmsFault->write(1);
139-
bmsFault->write(0);
124+
bmsFault.write(1);
125+
bmsFault.write(0);
140126

141127
// Enable charging
142-
chargerControl->write(0);
128+
chargerControl.write(0);
143129
}

0 commit comments

Comments
 (0)