-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathracechrono-canbus.ino
152 lines (129 loc) · 3.83 KB
/
racechrono-canbus.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// MIT License
//
// Copyright (c) 2022 Joe Roback <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "src/racechrono-canbus.hpp"
#include "src/canbus/controller.hpp"
#include "src/canbus/decoder.hpp"
#include "src/canbus/frame.hpp"
#include "src/led/led.hpp"
#include "src/racechrono/device.hpp"
#include <cstdint>
using namespace logging;
namespace
{
constexpr uint32_t core0_stack_size = 6 * 1024;
StaticTask_t core0_buffer;
StackType_t core0_stack[core0_stack_size];
TaskHandle_t core0_handle;
volatile bool core0_started = false;
void core0(void*);
}
void setup()
{
LED.builtin_off();
Serial.begin(115200);
delay(1500);
Serial.print("Starting up on core ");
Serial.println(xPortGetCoreID());
Serial.flush();
#if defined(DEBUG)
delay(5000);
#endif
// set logging level
logger::get().set_level(log_level::info);
//
// ATTENTION:
// All Bluetooth LE related activity must be pinned to core 0
// All CAN-bus related activity must be pinned to core 1
//
assert(xPortGetCoreID() == 1);
core0_handle = xTaskCreateStaticPinnedToCore(
core0,
"racechrono",
core0_stack_size,
nullptr,
tskIDLE_PRIORITY,
core0_stack,
&core0_buffer,
0
);
RCASSERT(core0_handle);
// wait for core 0 to start (not strictly necessary)
while (!core0_started) { delay(100); }
// setup can-bus on core 1 (default), interrupt handler will be serviced on core 1
if (CANCTLR.install())
{
if (CANCTLR.start())
{
LED.builtin_on();
}
else
{
errorln("ERROR: CAN bus controller startup failed!");
esp_restart();
}
}
else
{
errorln("ERROR: CAN bus driver install failed!");
esp_restart();
}
}
namespace
{
// core 0 - receive can frames from queue, send over bluetooth le
void core0(void*)
{
// xQueue copies all data, so we can use a static buffer here
static canbus::frame f;
// start up bluetooth le connection
if (RCDEV.start(&CANDEC))
{
core0_started = true;
while (true)
{
while (CANCTLR.recv(f))
{
#if defined(DEBUG)
uint32_t id = f.id;
uint8_t len = f.info.dlc;
verboseln("PID 0x%03x LEN %u", id, len);
#endif
RCDEV.send(reinterpret_cast<uint8_t*>(&f.id), sizeof(uint32_t) + f.info.dlc);
}
RCDEV.stats();
}
}
else
{
errorln("ERROR: RaceChrono bluetooth device startup failed!");
esp_restart();
}
// should never reach here...
RCASSERT(false);
}
} // namespace
// core 1 - can-bus interrupt handler is running here, but also print some stats
void loop()
{
// print out stats
CANCTLR.stats();
}