-
Notifications
You must be signed in to change notification settings - Fork 2
/
teslaBMSBL.ino
135 lines (112 loc) · 3.62 KB
/
teslaBMSBL.ino
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
/**@file teslaBMSBL.ino */
#include <Arduino.h>
#include "Controller.hpp"
#include "Cons.hpp"
#include "Logger.hpp"
#include "Oled.hpp"
#include <Snooze.h>
#include <TimeLib.h>
#include <TeensyView.h>
/*! \mainpage teslaBMSBL
\section intro_sec Introduction
A teensy based battery management system for Tesla battery modules.
\section install_sec Installation
\subsection step1 Step 1: Opening the box
etc...
*/
//instantiate all objects
TeensyView teensyView_inst(OLED_PIN_RESET, OLED_PIN_DC, OLED_PIN_CS, OLED_PIN_SCK, OLED_PIN_MOSI);
//static Settings settings;
static Controller controller_inst; ///< The controller is responsible for orchestrating all major functions of the BMS.
static Cons cons_inst(&controller_inst); ///< The console is a 2 way user interface available on usb serial port at baud 115200.
static Oled oled_inst(&controller_inst, &teensyView_inst); ///< The oled is a 1 way user interface displaying the most critical information.
// Load drivers
//SnoozeTouch touch;
SnoozeDigital digital;
SnoozeTimer timer;
SnoozeUSBSerial usbSerial;
// install drivers to a SnoozeBlock
SnoozeBlock config(timer, digital, usbSerial);
time_t getTeensy3Time() {
return Teensy3Clock.get();
}
/////////////////////////////////////////////////
/// \brief The setup function runs once when you press reset or power the board.
/////////////////////////////////////////////////
void setup() {
Serial.println("setup");
pinMode(INL_SOFT_RST, INPUT_PULLUP);
Serial.println("setup pinmode");
// set the Time library to use Teensy 3.0's RTC to keep time
setSyncProvider(getTeensy3Time);
Serial.println("setup setSyncProvider");
delay(100);
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
} else {
Serial.println("RTC has set the system time");
}
Serial.println("setup");
LOG_CONSOLE("BMS> ");
}
/////////////////////////////////////////////////
/// Holds all the code that runs every period.
/////////////////////////////////////////////////
void phase1main() {
if (digitalRead(INL_SOFT_RST) == LOW) {
//_reboot_Teensyduino_();
CPU_RESTART;
}
cons_inst.doConsole();
}
/////////////////////////////////////////////////
/// Holds code that runs every two periods.
/////////////////////////////////////////////////
void phase1A() {
controller_inst.doController();
}
/////////////////////////////////////////////////
/// Holds code that runs every two periods.
/////////////////////////////////////////////////
void phase1B() {
oled_inst.doOled();
}
/////////////////////////////////////////////////
/// Once setup is complete, loop is called for ever.
/////////////////////////////////////////////////
void loop() {
uint32_t starttime, endtime, delaytime, timespent, period;
bool phaseA = true;
for (;;) {
starttime = millis();
phase1main();
if (phaseA)
phase1A();
else
phase1B();
phaseA = !phaseA;
digital.pinMode(INL_SOFT_RST, INPUT_PULLUP, FALLING); //pin, mode, type
//get loop period from controller
period = controller_inst.getPeriodMillis();
endtime = millis();
if (endtime > starttime) {
timespent = endtime - starttime;
} else {
starttime = 0xffffffff - starttime;
timespent = starttime + endtime;
}
if (timespent >= period) {
delaytime = 0;
} else {
delaytime = period - timespent;
}
//sleep board instead of delay, if not in active state
if (delaytime > LOOP_PERIOD_ACTIVE_MS) {
timer.setTimer(delaytime); // milliseconds
//who = Snooze.deepSleep( config );
(void)Snooze.deepSleep(config);
} else {
delay(delaytime);
}
}
}