Skip to content

Commit ae848c8

Browse files
committed
Merge branch 'main' into ETC
2 parents 6ec15f9 + 36bff0f commit ae848c8

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed

Charger/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.19)
2+
cmake_policy(VERSION 3.19...3.22)
3+
4+
set(MBED_APP_JSON_PATH mbed_app.json5)
5+
6+
include(../mbed-os/tools/cmake/mbed_toolchain_setup.cmake)
7+
project(Charger # here you can change your project name
8+
LANGUAGES C CXX ASM)
9+
include(mbed_project_setup)
10+
11+
add_subdirectory(../mbed-os ../mbed-os-build)
12+
13+
add_executable(${PROJECT_NAME} main.cpp
14+
config.h)
15+
target_link_libraries(${PROJECT_NAME} mbed-os) # Can also link to mbed-baremetal here
16+
mbed_set_post_build(${PROJECT_NAME})

Charger/config.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Created by coles on 5/15/2025.
3+
//
4+
5+
#ifndef CONFIG_H
6+
#define CONFIG_H
7+
8+
#ifndef PIN_CAN1_TD
9+
#define PIN_CAN1_TD PA_12
10+
#endif
11+
12+
13+
#ifndef PIN_CAN1_RD
14+
#define PIN_CAN1_RD PA_11
15+
#endif
16+
17+
#ifndef PIN_CONTROL_PILOT
18+
#define PIN_CONTROL_PILOT PA_7
19+
#endif
20+
21+
#ifndef PIN_PROXIMITY_PILOT
22+
#define PIN_PROXIMITY_PILOT PA_6
23+
#endif
24+
25+
#ifndef CAN_FREQUENCY
26+
#define CAN_FREQUENCY 500000
27+
#endif
28+
29+
#ifndef MAX_AC_CURRENT
30+
#define MAX_AC_CURRENT 32
31+
#endif
32+
33+
#ifndef VOLTAGE_TARGET_MV
34+
#define VOLTAGE_TARGET_MV 120000
35+
#endif
36+
37+
#ifndef CURRENT_MAX_MA
38+
#define CURRENT_MAX_MA 52000 // 52 amps is 1C for now, battery can support up to 120 amps according to datasheet
39+
#endif
40+
41+
42+
#endif //CONFIG_H

Charger/main.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include <thread>
2+
3+
#include "mbed.h"
4+
#include "config.h"
5+
6+
void initIO();
7+
void initChargerCAN();
8+
void sendCAN();
9+
10+
CAN* can;
11+
12+
uint32_t max_voltage_mV = 0;
13+
uint16_t max_dc_current_mA = 0;
14+
uint8_t max_ac_current_A = 0;
15+
16+
bool enable = false;
17+
18+
19+
AnalogIn control_pilot(PIN_CONTROL_PILOT);
20+
AnalogIn proximity_pilot(PIN_PROXIMITY_PILOT);
21+
22+
23+
int main()
24+
{
25+
26+
printf("main()\n");
27+
initIO();
28+
29+
ThisThread::sleep_for(500ms);
30+
31+
printf("Starting main loop\n");
32+
33+
bool prechargeDone = false;
34+
bool fault = false;
35+
bool shutdown_closed = false;
36+
bool cell_temps_fine = false;
37+
38+
while(true) {
39+
CANMessage msg;
40+
41+
42+
while (can->read(msg)) {
43+
switch (msg.id) {
44+
case 0x80: // sync
45+
sendCAN();
46+
break;
47+
case 0x188: // ACC_TPDO_STATUS
48+
prechargeDone = msg.data[0] & 0b00001000;
49+
fault = msg.data[0] & 0b00000011;
50+
shutdown_closed = msg.data[0] & 0b00000100;
51+
cell_temps_fine = !(msg.data[1] & 0b00010000);
52+
break;
53+
}
54+
}
55+
56+
57+
// if proximity pilot is about 2.7v then no EVSE connected
58+
// if proximity pilot is about 1.7v then EVSE connected and button pressed
59+
// if proximity pilot is about 0.9v then EVSE connected and button not pressed
60+
bool proximity_pilot_ready = (proximity_pilot.read() * 3.3 < 1.2);
61+
62+
// printf("pp: %f\n",proximity_pilot.read());
63+
64+
// Duty cycle is 31 times voltage on control pilot
65+
// Duty cycle times 0.6 is max allowed continuous current draw
66+
// control pilot voltage times 18.5 is the max allowed current
67+
int max_ac_current_CP = std::floor(control_pilot.read() * 3.3 * 19);
68+
69+
// printf ("proximity pilot: %x\n", proximity_pilot_ready);
70+
71+
// printf("cp: %f\n", control_pilot.read());
72+
//
73+
// printf("max ac current: %d\n", max_ac_current_CP);
74+
75+
max_ac_current_A = std::min(max_ac_current_CP, MAX_AC_CURRENT);
76+
77+
78+
79+
max_voltage_mV = VOLTAGE_TARGET_MV;
80+
max_dc_current_mA = CURRENT_MAX_MA;
81+
82+
printf("pp_ready: %x, precharge done: %x, fault: %x, sh closed: %x, cell temps fine: %x\n", proximity_pilot_ready, prechargeDone, fault, shutdown_closed, cell_temps_fine);
83+
enable = proximity_pilot_ready && prechargeDone && !fault && shutdown_closed && cell_temps_fine;
84+
printf("enable: %x\n", enable);
85+
}
86+
87+
// main() is expected to loop forever.
88+
// If main() actually returns the processor will halt
89+
return 0;
90+
}
91+
92+
void initIO() {
93+
printf("initIO()\n");
94+
can = new CAN(PIN_CAN1_RD, PIN_CAN1_TD, CAN_FREQUENCY);
95+
96+
// LSS assign charger
97+
initChargerCAN();
98+
}
99+
100+
void initChargerCAN() {
101+
printf("initChargerCAN()\n");
102+
103+
ThisThread::sleep_for(2500ms);
104+
105+
// Switch state global protocal, switch to LSS configuration state
106+
uint8_t lss0_data[8] = {0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
107+
CANMessage lss0_msg(0x7E5, lss0_data);
108+
can->write(lss0_msg);
109+
110+
ThisThread::sleep_for(500ms);
111+
112+
// Configurate node ID protocal, set node ID to 0x10
113+
uint8_t lss1_data[8] = {0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
114+
CANMessage lss1_msg(0x7E5, lss1_data);
115+
can->write(lss1_msg);
116+
117+
ThisThread::sleep_for(100ms);
118+
119+
}
120+
121+
122+
void sendCAN() {
123+
// send charge limits
124+
uint8_t charge_limits_data[8] = {
125+
0x10,
126+
static_cast<uint8_t>(max_voltage_mV),
127+
static_cast<uint8_t>(max_voltage_mV >> 8),
128+
static_cast<uint8_t>(max_voltage_mV >> 16),
129+
static_cast<uint8_t>(max_voltage_mV >> 24),
130+
static_cast<uint8_t>(max_dc_current_mA),
131+
static_cast<uint8_t>(max_dc_current_mA >> 8),
132+
max_ac_current_A
133+
};
134+
CANMessage charge_limits_msg(0x306, charge_limits_data);
135+
can->write(charge_limits_msg);
136+
137+
// ThisThread::sleep_for(1ms);
138+
139+
// send charge control
140+
uint8_t charge_control_data[8] = {
141+
0x10,
142+
static_cast<uint8_t>((enable << 1) + 0b00100000),
143+
0x00,
144+
0x00,
145+
0x00,
146+
0x00,
147+
0x00,
148+
0x00
149+
};
150+
CANMessage charge_control_msg(0x206, charge_control_data);
151+
can->write(charge_control_msg);
152+
ThisThread::sleep_for(1ms);
153+
}

Charger/mbed_app.json5

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"target_overrides": {
3+
"*": {
4+
"platform.stdio-baud-rate": 115200,
5+
"platform.stdio-buffered-serial": 1,
6+
7+
// Uncomment to use mbed-baremetal instead of mbed-os
8+
// "target.application-profile": "bare-metal"
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)