-
Notifications
You must be signed in to change notification settings - Fork 28
/
sequencer.c
401 lines (307 loc) · 9.03 KB
/
sequencer.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "sequencer.h"
#include "sys_time.h"
#include "io.h"
#include "dispatch.h"
#include <stdint.h>
#include <stdbool.h>
typedef struct
{
bool flash_valid;
int start;
int current;
uint64_t current_end_time;
int repeats;
} sequencer_t;
static sequencer_t sequencer;
typedef struct
{
union
{
struct
{
unsigned int active:1;
unsigned int io:4;
unsigned int pin:4;
unsigned int duration:23;
unsigned int value:32;
};
struct
{
unsigned int magic:32;
unsigned int version:32;
};
struct
{
uint32_t word[2];
};
};
} sequencer_entry_t;
assert_size(sequencer_entry_t, 8);
enum
{
sequencer_flash_magic = 0x4afc4afb,
sequencer_flash_version = 0,
sequencer_flash_sectors = 4,
sequencer_flash_size = sequencer_flash_sectors * SPI_FLASH_SEC_SIZE, // 4 sectors of 4k bytes each
sequencer_flash_entries = sequencer_flash_size / sizeof(sequencer_entry_t),
sequencer_flash_entries_per_sector = sequencer_flash_entries / sequencer_flash_sectors,
sequencer_flash_memory_map_start = 0x40200000,
};
_Static_assert(sequencer_flash_entries == 2048, "flash sequencer size incorrect");
_Static_assert(sequencer_flash_entries_per_sector == 512, "flash sequencer per sector size incorrect");
static bool clear_all_flash_entries(unsigned int mirror)
{
bool success;
sequencer_entry_t *entry;
unsigned int offset, sector, current = 0;
string_t *buffer_string;
char *buffer_cstr;
unsigned int size;
success = false;
if(mirror > 1)
goto error2;
if(mirror == 0)
offset = SEQUENCER_FLASH_OFFSET_0;
else
offset = SEQUENCER_FLASH_OFFSET_1;
if(offset == 0) // plain image, no mirror offset
{
success = true;
goto error2;
}
flash_buffer_request(fsb_sequencer, true, "sequencer clear all flash entries", &buffer_string, &buffer_cstr, &size);
if(!buffer_string)
goto error2;
for(sector = 0; sector < sequencer_flash_sectors; sector++)
{
entry = (sequencer_entry_t *)(void *)buffer_cstr;
if(sector == 0)
{
entry->magic = sequencer_flash_magic;
entry->version = sequencer_flash_version;
entry++;
}
for(; ((char *)entry - buffer_cstr) < (int)size; entry++)
{
entry->active = 0;
entry->io = 0;
entry->pin = 0;
entry->duration = 0;
entry->value = current++;
}
log("sequencer clear: offset: %x, sector %u, entries written to flash: %u, entryp = %d\n",
offset + (sector * SPI_FLASH_SEC_SIZE),
sector,
current,
(char *)entry - buffer_cstr);
if(spi_flash_erase_sector((offset + (sector * size)) / size) != SPI_FLASH_RESULT_OK)
goto error1;
if(spi_flash_write(offset + (sector * size), buffer_cstr, size) != SPI_FLASH_RESULT_OK)
goto error1;
}
success = true;
error1:
flash_buffer_release(fsb_sequencer, "sequencer clear all flash entries");
error2:
return(success);
}
static bool get_flash_entry(unsigned int index, sequencer_entry_t *entry)
{
const sequencer_entry_t *entries_in_flash;
if(index >= sequencer_flash_entries)
return(false);
// note: this will always use either mirror 0 or mirror 1 depending on which image/slot is loaded, due to the flash mapping window
entries_in_flash = (const sequencer_entry_t *)(sequencer_flash_memory_map_start + SEQUENCER_FLASH_OFFSET_0);
// careful to only read complete 32 bits words from mapped flash
entry->word[0] = entries_in_flash[index].word[0];
entry->word[1] = entries_in_flash[index].word[1];
return(true);
}
static bool update_flash_entry(unsigned int index, unsigned int mirror, const sequencer_entry_t *entry)
{
bool success;
sequencer_entry_t *entries_in_buffer, *entry_in_buffer;
unsigned int flash_start_offset, sector;
string_t *buffer_string;
char *buffer_cstr;
unsigned int size;
success = false;
if(!sequencer.flash_valid)
goto error2;
if(index >= sequencer_flash_entries)
goto error2;
if(mirror > 1)
goto error2;
flash_buffer_request(fsb_sequencer, true, "sequencer update flash entry", &buffer_string, &buffer_cstr, &size);
if(!buffer_string)
goto error2;
if(mirror == 0)
flash_start_offset = SEQUENCER_FLASH_OFFSET_0;
else
flash_start_offset = SEQUENCER_FLASH_OFFSET_1;
if(flash_start_offset == 0) // plain image, no mirror offset
{
success = true;
goto error1;
}
sector = (index * sizeof(sequencer_entry_t)) / size;
//log("update flash entry: update entry: %u, sector: %u, offset index: %u, flash start offset: %x\n",
//index,
//sector,
//index - (sector * sequencer_flash_entries_per_sector),
//flash_start_offset);
if(spi_flash_read(flash_start_offset + (sector * size), buffer_cstr, size) != SPI_FLASH_RESULT_OK)
goto error1;
entries_in_buffer = (sequencer_entry_t *)(void *)buffer_cstr;
entry_in_buffer = &entries_in_buffer[index - (sector * sequencer_flash_entries_per_sector)];
//log("* buffer offset: %d\n", (char *)&entries_in_buffer[index - (sector * sequencer_flash_entries_per_sector)] - buffer);
//log("* entry1: io: %d, pin: %d, duration: %d, value: %u\n", entry_in_buffer->io, entry_in_buffer->pin, entry_in_buffer->duration, entry_in_buffer->value);
*entry_in_buffer = *entry;
//log("* entry2: io: %d, pin: %d, duration: %d, value: %u\n", entry_in_buffer->io, entry_in_buffer->pin, entry_in_buffer->duration, entry_in_buffer->value);
if(spi_flash_erase_sector((flash_start_offset + (sector * size)) / size) != SPI_FLASH_RESULT_OK)
goto error1;
if(spi_flash_write(flash_start_offset + (sector * size), buffer_cstr, size) != SPI_FLASH_RESULT_OK)
goto error1;
success = true;
error1:
flash_buffer_release(fsb_sequencer, "sequencer update flash entry");
error2:
return(success);
}
attr_pure int sequencer_get_start(void)
{
return(sequencer.start);
}
attr_pure int sequencer_get_current(void)
{
return(sequencer.current);
}
iram attr_pure uint64_t sequencer_get_current_end_time(void)
{
return(sequencer.current_end_time);
}
iram attr_pure int sequencer_get_repeats(void)
{
return(sequencer.repeats);
}
void sequencer_get_status(bool *running, unsigned int *start, unsigned int *flash_size, unsigned int *flash_size_entries,
unsigned int *flash_offset_flash0, unsigned int *flash_offset_flash1, unsigned int *flash_offset_mapped)
{
*running = sequencer.repeats > 0;
*start = sequencer.start;
*flash_size = sequencer_flash_sectors * SPI_FLASH_SEC_SIZE;
*flash_size_entries = sequencer_flash_entries;
*flash_offset_flash0 = SEQUENCER_FLASH_OFFSET_0;
*flash_offset_flash1 = SEQUENCER_FLASH_OFFSET_1;
*flash_offset_mapped = sequencer_flash_memory_map_start + SEQUENCER_FLASH_OFFSET_0;
}
bool sequencer_clear(void)
{
if(!clear_all_flash_entries(0))
return(false);
if(!clear_all_flash_entries(1))
return(false);
sequencer_init();
return(sequencer.flash_valid);
}
bool sequencer_get_entry(unsigned int index, bool *active, int *io, int *pin, unsigned int *value, unsigned int *duration)
{
sequencer_entry_t entry;
if(!sequencer.flash_valid)
return(false);
index++; // index = 0 is header
if(index >= sequencer_flash_entries)
return(false);
if(!get_flash_entry(index, &entry))
return(false);
if(active)
*active = entry.active;
if(io)
*io = entry.io;
if(pin)
*pin = entry.pin;
if(duration)
*duration = entry.duration;
if(value)
*value = entry.value;
return(true);
}
bool sequencer_set_entry(unsigned int index, int io, int pin, uint32_t value, int duration)
{
sequencer_entry_t entry;
if(!sequencer.flash_valid)
return(false);
index++;
if(index >= sequencer_flash_entries)
return(false);
entry.active = 1;
entry.io = io;
entry.pin = pin;
entry.duration = duration;
entry.value = value;
if(!update_flash_entry(index, 0, &entry))
return(false);
return(update_flash_entry(index, 1, &entry));
}
bool sequencer_remove_entry(unsigned int index)
{
sequencer_entry_t entry;
if(!sequencer.flash_valid)
return(false);
index++;
if(index >= sequencer_flash_entries)
return(false);
entry.active = 0;
entry.io = 0;
entry.pin = 0;
entry.duration = 0;
entry.value = 0;
if(!update_flash_entry(index, 0, &entry))
return(false);
return(update_flash_entry(index, 1, &entry));
}
void sequencer_init(void)
{
sequencer_entry_t header;
sequencer_stop();
sequencer.flash_valid = 0;
if(get_flash_entry(0, &header) && (header.magic == sequencer_flash_magic) && (header.version == sequencer_flash_version))
sequencer.flash_valid = 1;
}
void sequencer_start(unsigned int start, unsigned int repeats)
{
sequencer.start = start;
sequencer.current = sequencer.start - 1;
sequencer.current_end_time = 0;
sequencer.repeats = repeats;
}
void sequencer_stop(void)
{
sequencer.start = 0;
sequencer.current = -1;
sequencer.current_end_time = 0;
sequencer.repeats = 0;
}
void sequencer_run(void)
{
int io, pin;
unsigned int value, duration;
bool active;
sequencer.current++;
if(!sequencer_get_entry(sequencer.current, &active, &io, &pin, &value, &duration) || !active)
{
if(--sequencer.repeats <= 0)
{
sequencer_stop();
return;
}
sequencer.current = sequencer.start;
if(!sequencer_get_entry(sequencer.current, &active, &io, &pin, &value, &duration) || !active)
{
sequencer_stop();
return;
}
}
sequencer.current_end_time = (time_get_us() / 1000) + duration;
io_write_pin((string_t *)0, io, pin, value);
}