-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpicostation.cpp
304 lines (248 loc) · 10.9 KB
/
picostation.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "picostation.h"
#include <stdio.h>
#include <time.h>
#include "cmd.h"
#include "disc_image.h"
#include "hardware/pwm.h"
#include "hardware/vreg.h"
#include "i2s.h"
#include "logging.h"
#include "main.pio.h"
#include "pico/multicore.h"
#include "subq.h"
#include "third_party/RP2040_Pseudo_Atomic/Inc/RP2040Atomic.hpp"
#include "utils.h"
#include "values.h"
#if DEBUG_MAIN
#define DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINT(...) while (0)
#endif
// To-do: Establish thread safety: identify variables that are shared between cores, wrap them in mutexes or spin locks,
// maybe in a class?
// To-do: Implement lid switch behavior
// To-do: Implement a console side menu to select the cue file
// To-do: Implement level meter mode to command $AX - AudioCTRL
// To-do: Implement UART(250 baud) for psnee
// To-do: Fix seeks that go into the lead-in + track 1 pregap areas, possibly sending bad data over I2S
patom::types::patomic_bool picostation::g_soctEnabled; // core0: r/w, core1: r
uint picostation::g_countTrack = 0; // core0: r/w, move to class?
int picostation::g_track = 0; // core0: r/w, move to class?
int picostation::g_originalTrack = 0; // core0: r/w, move to class?
int picostation::g_sledMoveDirection = SledMove::STOP; // core0: r/w, move to class?
uint64_t picostation::g_sledTimer = 0; // core0: r/w, move to class?
patom::types::patomic_int picostation::g_sector; // core0: r/w, core1: r
int picostation::g_sectorForTrackUpdate = 0; // core0: r/w, move to class?
patom::types::patomic_int picostation::g_sectorSending; // core0: r, core1: w
bool picostation::g_subqDelay = false; // core0: r/w
static int s_currentPlaybackSpeed = 1;
int picostation::g_targetPlaybackSpeed = 1; // core0: r/w
mutex_t picostation::g_mechaconMutex;
bool picostation::g_coreReady[2] = {false, false};
uint picostation::g_audioCtrlMode = audioControlModes::NORMAL;
// volatile int32_t picostation::g_audioPeak = 0;
// volatile int32_t picostation::g_audioLevel = 0;
static uint s_mechachonOffset;
uint picostation::g_soctOffset;
uint picostation::g_subqOffset;
static picostation::PWMSettings pwmDataClock = {
.gpio = Pin::DA15, .wrap = (1 * 32) - 1, .clkdiv = 4, .invert = true, .level = (32 / 2)};
static picostation::PWMSettings pwmLRClock = {
.gpio = Pin::LRCK, .wrap = (48 * 32) - 1, .clkdiv = 4, .invert = false, .level = (48 * (32 / 2))};
static picostation::PWMSettings pwmMainClock = {.gpio = Pin::CLK, .wrap = 1, .clkdiv = 2, .invert = false, .level = 1};
static void initPWM(picostation::PWMSettings *settings);
[[noreturn]] void __time_critical_func(picostation::core0Entry)() {
static constexpr uint c_MaxTrackMoveTime = 15; // uS
static constexpr uint c_MaxSubqDelayTime = 3333; // uS
SubQ subq(&g_discImage);
uint64_t subqDelayTime = 0;
int sector_per_track = sectorsPerTrack(0);
g_coreReady[0] = true;
while (!g_coreReady[1]) {
tight_loop_contents();
}
while (true) {
// Update latching, output SENS
if (mutex_try_enter(&g_mechaconMutex, 0)) {
mechcommand::updateMechSens();
mutex_exit(&g_mechaconMutex);
}
const auto currentSector = g_sector.Load();
// Limit Switch
gpio_put(Pin::LMTSW, currentSector > 3000);
updatePlaybackSpeed();
// Check for reset signal
maybeReset();
// Soct/Sled/seek
if (g_soctEnabled.Load()) {
uint interrupts = save_and_disable_interrupts();
// waiting for RX FIFO entry does not work.
sleep_us(300);
g_soctEnabled = false;
pio_sm_set_enabled(PIOInstance::SOCT, SM::SOCT, false);
restore_interrupts(interrupts);
} else if (g_sledMoveDirection != SledMove::STOP) {
if ((time_us_64() - g_sledTimer) > c_MaxTrackMoveTime) {
g_track = clamp(g_track + g_sledMoveDirection, c_trackMin, c_trackMax); // +1 or -1
g_sectorForTrackUpdate = trackToSector(g_track);
g_sector = g_sectorForTrackUpdate;
const int tracks_moved = g_track - g_originalTrack;
if (abs(tracks_moved) >= g_countTrack) {
g_originalTrack = g_track;
mechcommand::setSens(SENS::COUT, !mechcommand::getSens(SENS::COUT));
}
g_sledTimer = time_us_64();
}
} else if (mechcommand::getSens(SENS::GFS)) {
if (g_subqDelay) {
if ((time_us_64() - subqDelayTime) > c_MaxSubqDelayTime) {
g_subqDelay = false;
subq.start_subq(currentSector);
gpio_put(Pin::SCOR, 1);
add_alarm_in_us(
135,
[](alarm_id_t id, void *user_data) -> int64_t {
gpio_put(Pin::SCOR, 0);
return 0;
},
NULL, true);
}
} else if (g_sectorSending.Load() == currentSector) {
g_sector = clamp(currentSector + 1, c_sectorMin, c_sectorMax);
if ((currentSector - g_sectorForTrackUpdate) >= sector_per_track) // Moved to next track?
{
g_sectorForTrackUpdate = currentSector;
g_track = clamp(g_track + 1, c_trackMin, c_trackMax);
sector_per_track = sectorsPerTrack(g_track);
}
g_subqDelay = true;
subqDelayTime = time_us_64();
}
}
}
}
[[noreturn]] void picostation::core1Entry() {
I2S g_i2s;
g_i2s.start();
while (1) asm("");
__builtin_unreachable();
}
void picostation::initHW() {
#if DEBUG_LOGGING_ENABLED
stdio_init_all();
stdio_set_chars_available_callback(NULL, NULL);
sleep_ms(1250);
#endif
DEBUG_PRINT("Initializing...\n");
vreg_set_voltage(VREG_VOLTAGE_1_15);
sleep_ms(100);
// srand(time(NULL)); // Causes an insane size increase of the binary for some reason?
// To-do: Investigate this later...
// srand(4); // chosen by fair dice roll.
// guaranteed to be random.
srand(time_us_32());
mutex_init(&g_mechaconMutex);
for (const auto pin : Pin::allPins) {
gpio_init(pin);
}
gpio_set_dir(Pin::XLAT, GPIO_IN);
gpio_set_dir(Pin::SQCK, GPIO_IN);
gpio_set_dir(Pin::LMTSW, GPIO_OUT);
gpio_set_dir(Pin::SCEX_DATA, GPIO_OUT);
gpio_put(Pin::SCEX_DATA, 1);
gpio_set_dir(Pin::DOOR, GPIO_IN);
gpio_set_dir(Pin::RESET, GPIO_IN);
gpio_set_dir(Pin::SENS, GPIO_OUT);
gpio_set_dir(Pin::DA15, GPIO_OUT);
gpio_set_dir(Pin::DA16, GPIO_OUT);
gpio_set_dir(Pin::LRCK, GPIO_OUT);
gpio_set_dir(Pin::SCOR, GPIO_OUT);
gpio_put(Pin::SCOR, 0);
gpio_set_dir(Pin::SQSO, GPIO_OUT);
gpio_put(Pin::SQSO, 0);
gpio_set_dir(Pin::CLK, GPIO_OUT);
gpio_set_dir(Pin::CMD_CK, GPIO_IN);
gpio_set_dir(Pin::CMD_DATA, GPIO_IN);
gpio_set_input_hysteresis_enabled(Pin::XLAT, true);
gpio_set_input_hysteresis_enabled(Pin::SQCK, true);
gpio_set_input_hysteresis_enabled(Pin::RESET, true);
gpio_set_input_hysteresis_enabled(Pin::CMD_CK, true);
initPWM(&pwmMainClock);
initPWM(&pwmDataClock);
initPWM(&pwmLRClock);
uint i2s_pio_offset = pio_add_program(PIOInstance::I2S_DATA, &i2s_data_program);
i2s_data_program_init(PIOInstance::I2S_DATA, SM::I2S_DATA, i2s_pio_offset, Pin::DA15, Pin::DA16);
s_mechachonOffset = pio_add_program(PIOInstance::MECHACON, &mechacon_program);
mechacon_program_init(PIOInstance::MECHACON, SM::MECHACON, s_mechachonOffset, Pin::CMD_DATA);
g_soctOffset = pio_add_program(PIOInstance::SOCT, &soct_program);
g_subqOffset = pio_add_program(PIOInstance::SUBQ, &subq_program);
pio_sm_set_enabled(PIOInstance::I2S_DATA, SM::I2S_DATA, true);
pwm_set_mask_enabled((1 << pwmLRClock.sliceNum) | (1 << pwmDataClock.sliceNum) | (1 << pwmMainClock.sliceNum));
uint64_t start_time = time_us_64();
gpio_set_dir(Pin::RESET, GPIO_OUT);
gpio_put(Pin::RESET, 0);
sleep_ms(300);
gpio_set_dir(Pin::RESET, GPIO_IN);
while ((time_us_64() - start_time) < 30000) {
if (gpio_get(Pin::RESET) == 0) {
start_time = time_us_64();
}
}
while ((time_us_64() - start_time) < 30000) {
if (gpio_get(Pin::CMD_CK) == 0) {
start_time = time_us_64();
}
}
gpio_set_irq_enabled_with_callback(Pin::XLAT, GPIO_IRQ_EDGE_FALL, true, &mechcommand::interrupt_xlat);
pio_sm_set_enabled(PIOInstance::MECHACON, SM::MECHACON, true);
DEBUG_PRINT("ON!\n");
}
void initPWM(picostation::PWMSettings *settings) {
gpio_set_function(settings->gpio, GPIO_FUNC_PWM);
settings->sliceNum = pwm_gpio_to_slice_num(settings->gpio);
settings->config = pwm_get_default_config();
pwm_config_set_clkdiv_mode(&settings->config, PWM_DIV_FREE_RUNNING);
pwm_config_set_wrap(&settings->config, settings->wrap);
pwm_config_set_clkdiv(&settings->config, settings->clkdiv);
pwm_config_set_output_polarity(&settings->config, settings->invert, settings->invert);
pwm_init(settings->sliceNum, &settings->config, false);
pwm_set_both_levels(settings->sliceNum, settings->level, settings->level);
}
void picostation::updatePlaybackSpeed() {
if (s_currentPlaybackSpeed != g_targetPlaybackSpeed) {
s_currentPlaybackSpeed = g_targetPlaybackSpeed;
const uint clock_div = (s_currentPlaybackSpeed == 1) ? 4 : 2;
pwm_set_mask_enabled(0);
pwm_config_set_clkdiv_int(&pwmDataClock.config, clock_div);
pwm_config_set_clkdiv_int(&pwmLRClock.config, clock_div);
pwm_hw->slice[pwmDataClock.sliceNum].div = pwmDataClock.config.div;
pwm_hw->slice[pwmLRClock.sliceNum].div = pwmLRClock.config.div;
pwm_set_mask_enabled((1 << pwmLRClock.sliceNum) | (1 << pwmDataClock.sliceNum) | (1 << pwmMainClock.sliceNum));
DEBUG_PRINT("x%i\n", s_currentPlaybackSpeed);
}
}
void picostation::maybeReset() {
if (gpio_get(Pin::RESET) == 0) {
DEBUG_PRINT("RESET!\n");
pio_sm_set_enabled(PIOInstance::SUBQ, SM::SUBQ, false);
pio_sm_set_enabled(PIOInstance::SOCT, SM::SOCT, false);
pio_sm_restart(PIOInstance::MECHACON, SM::MECHACON);
mechacon_program_init(PIOInstance::MECHACON, SM::MECHACON, s_mechachonOffset, Pin::CMD_DATA);
g_subqDelay = false;
g_soctEnabled = false;
gpio_put(Pin::SCOR, 0);
gpio_put(Pin::SQSO, 0);
uint64_t start_time = time_us_64();
while ((time_us_64() - start_time) < 30000) {
if (gpio_get(Pin::RESET) == 0) {
start_time = time_us_64();
}
}
while ((time_us_64() - start_time) < 30000) {
if (gpio_get(Pin::CMD_CK) == 0) {
start_time = time_us_64();
}
}
pio_sm_set_enabled(PIOInstance::MECHACON, SM::MECHACON, true);
}
}