forked from paulocode/picostation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
290 lines (252 loc) · 8.77 KB
/
main.c
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
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "hardware/irq.h"
#include "hardware/pio.h"
#include "pico/stdlib.h"
#include "main.pio.h"
#include "pico/multicore.h"
#include "hardware/pll.h"
#include "hardware/clocks.h"
#include "hardware/structs/pll.h"
#include "hardware/structs/clocks.h"
#include "utils.h"
#include "values.h"
#include "subq.h"
#include "cmd.h"
#include "i2s.h"
// globals
mutex_t mechacon_mutex;
volatile uint latched = 0;
volatile uint mechachon_sm_offset;
volatile uint soct_offset;
volatile uint subq_offset;
volatile bool soct = 0;
volatile bool hasData = 0;
volatile uint sled_move_direction = SLED_MOVE_STOP;
volatile uint count_track = 0;
volatile uint track = 0;
volatile uint original_track = 0;
volatile uint sector = 0;
volatile uint sector_sending = -1;
volatile uint sector_for_track_update = 0;
volatile uint64_t subq_start_time = 0;
volatile uint64_t sled_timer = 0;
volatile uint8_t current_sens;
volatile int num_logical_tracks = 0;
int *logical_track_to_sector;
bool *is_data_track;
volatile int current_logical_track = 0;
volatile int mode = 1;
bool SENS_data[16] = {
0,0,0,0,0,
1, // FOK
0,0,0,0,
0, // GFS
0, // COMP
0, // COUT
0,0,0
};
void select_sens(uint8_t new_sens)
{
current_sens = new_sens;
}
void set_sens(uint8_t what, bool new_value)
{
SENS_data[what] = new_value;
if (what == current_sens) {
gpio_put(SENS, new_value);
}
}
void initialize() {
set_sys_clock_pll(948000000, 7, 1); // 135428571 Hz, 67714286 Hz PS1 clock
srand(time(NULL));
mutex_init(&mechacon_mutex);
gpio_init(SCEX_DATA);
gpio_init(SENS);
gpio_init(LMTSW);
gpio_init(XLAT);
gpio_init(DOOR);
gpio_init(RESET);
gpio_init(SQCK);
gpio_init(SQSO);
gpio_init(CMD_CK);
gpio_init(LRCK);
gpio_set_dir(SCEX_DATA, GPIO_OUT);
gpio_put(SCEX_DATA, 0);
gpio_set_dir(SENS, GPIO_OUT);
gpio_set_dir(LMTSW, GPIO_OUT);
gpio_set_dir(XLAT, GPIO_IN);
gpio_set_dir(DOOR, GPIO_IN);
gpio_set_dir(RESET, GPIO_IN);
gpio_set_dir(SQCK, GPIO_IN);
gpio_set_dir(SQSO, GPIO_OUT);
gpio_set_dir(CMD_CK, GPIO_IN);
gpio_put(SQSO, 0);
gpio_set_input_hysteresis_enabled(RESET,true);
gpio_set_input_hysteresis_enabled(SQCK,true);
gpio_set_input_hysteresis_enabled(XLAT,true);
gpio_set_input_hysteresis_enabled(CMD_CK,true);
gpio_set_drive_strength(LRCK, GPIO_DRIVE_STRENGTH_12MA);
uint i2s_pio_offset = pio_add_program(pio0, &i2s_data_program);
i2s_data_program_init(pio0, I2S_DATA_SM, i2s_pio_offset, DA15);
uint offset2 = pio_add_program(pio0, &i2s_lrck_program);
i2s_lrck_program_init(pio0, LRCK_DATA_SM, offset2, DA15+2);
mechachon_sm_offset = pio_add_program(pio1, &mechacon_program);
mechacon_program_init(pio1, MECHACON_SM, mechachon_sm_offset, CMD_DATA);
uint offset3 = pio_add_program(pio0, &cpu_clk_program);
cpu_clk_program_init(pio0, CPU_CLK_SM, offset3, CLK);
uint offset5 = pio_add_program(pio1, &scor_program);
scor_program_init(pio1, SCOR_SM, offset5, SCOR);
soct_offset = pio_add_program(pio1, &soct_program);
subq_offset = pio_add_program(pio1, &subq_program);
uint64_t startTime = time_us_64();
pio_enable_sm_mask_in_sync(pio0, (1u << CPU_CLK_SM) | (1u << I2S_DATA_SM) | (1u << LRCK_DATA_SM));
gpio_set_dir(RESET, GPIO_OUT);
gpio_put(RESET,0);
sleep_ms(300);
gpio_set_dir(RESET, GPIO_IN);
while((time_us_64() - startTime) < 30000) {
if (gpio_get(RESET) == 0) {
startTime = time_us_64();
}
}
while((time_us_64() - startTime) < 30000) {
if (gpio_get(CMD_CK) == 0) {
startTime = time_us_64();
}
}
printf("ON!\n");
multicore_launch_core1(i2s_data_thread);
gpio_set_irq_enabled_with_callback(XLAT, GPIO_IRQ_EDGE_RISE, true, &interrupt_xlat);
pio_enable_sm_mask_in_sync(pio1, (1u << SCOR_SM) | (1u << MECHACON_SM));
}
int main() {
stdio_init_all();
initialize();
int prevMode = 1;
int sectors_per_track_i = sectors_per_track(0);
bool subq_delay = 0;
uint64_t subq_delay_time = 0;
while (true) {
gpio_put(LMTSW, sector > 3000);
if (mutex_try_enter(&mechacon_mutex,0)) {
while(!pio_sm_is_rx_fifo_empty(pio1, MECHACON_SM)) {
uint c = reverseBits(pio_sm_get_blocking(pio1, MECHACON_SM),8);
latched >>= 8;
latched |= c << 16;
}
select_sens(latched >> 20);
gpio_put(SENS, SENS_data[latched >> 20]);
mutex_exit(&mechacon_mutex);
}
if (prevMode == 1 && mode == 2) {
pio_sm_set_clkdiv(pio0, I2S_DATA_SM, 1);
pio_sm_set_clkdiv(pio1, SCOR_SM, 1);
prevMode = 2;
printf("x2\n");
} else if (prevMode == 2 && mode == 1) {
pio_sm_set_clkdiv(pio0, I2S_DATA_SM, 2);
pio_sm_set_clkdiv(pio1, SCOR_SM, 2);
prevMode = 1;
printf("x1\n");
}
if (track < 0 || sector < 0) {
track = 0;
sector = 0;
sector_for_track_update = 0;
}
if (track > 24000 || sector > 440000) {
track = 24000;
sector = track_to_sector(track);
sector_for_track_update = sector;
}
if (gpio_get(RESET) == 0) {
printf("RESET!\n");
pio_sm_set_enabled(pio1, SUBQ_SM, false);
pio_sm_set_enabled(pio1, SOCT_SM, false);
mechacon_program_init(pio1, MECHACON_SM, mechachon_sm_offset, CMD_DATA);
subq_delay = 0;
soct = 0;
gpio_init(SQSO);
gpio_set_dir(SQSO, GPIO_OUT);
gpio_put(SQSO, 0);
uint64_t startTime = time_us_64();
while ((time_us_64() - startTime) < 30000) {
if (gpio_get(RESET) == 0) {
startTime = time_us_64();
}
}
while ((time_us_64() - startTime) < 30000) {
if (gpio_get(CMD_CK) == 0) {
startTime = time_us_64();
}
}
pio_sm_set_enabled(pio1, MECHACON_SM, true);
}
if (soct) {
uint interrupts = save_and_disable_interrupts();
// waiting for RX FIFO entry does not work.
sleep_us(300);
soct = 0;
pio_sm_set_enabled(pio1, SOCT_SM, false);
subq_start_time = time_us_64();
restore_interrupts(interrupts);
} else if (sled_move_direction == SLED_MOVE_FORWARD) {
if ((time_us_64() - sled_timer) > TRACK_MOVE_TIME_US) {
sled_timer = time_us_64();
track++;
sector = track_to_sector(track);
sector_for_track_update = sector;
if ((track - original_track) >= count_track) {
original_track = track;
set_sens(SENS_COUT, !SENS_data[SENS_COUT]);
}
}
} else if (sled_move_direction == SLED_MOVE_REVERSE) {
if ((time_us_64() - sled_timer) > TRACK_MOVE_TIME_US) {
sled_timer = time_us_64();
track--;
sector = track_to_sector(track);
sector_for_track_update = sector;
if ((original_track - track) >= count_track) {
original_track = track;
set_sens(SENS_COUT, !SENS_data[SENS_COUT]);
}
}
} else if (SENS_data[SENS_GFS]) {
if (sector < 4650 && (time_us_64() - subq_start_time) > 13333) {
subq_start_time = time_us_64();
start_subq();
sector++;
if ((sector - sector_for_track_update) >= sectors_per_track_i) {
sector_for_track_update = sector;
track++;
sectors_per_track_i = sectors_per_track(track);
}
} else {
if (sector_sending == sector) {
if (!subq_delay) {
sector++;
if ((sector - sector_for_track_update) >= sectors_per_track_i) {
sector_for_track_update = sector;
track++;
sectors_per_track_i = sectors_per_track(track);
}
subq_delay = 1;
subq_delay_time = time_us_64();
}
}
if (subq_delay && (sector >= 4650 && (time_us_64() - subq_delay_time) > 3333)) {
subq_delay = 0;
start_subq();
}
}
} else {
subq_delay = 0;
}
}
}