-
Notifications
You must be signed in to change notification settings - Fork 28
/
spi.c
705 lines (555 loc) · 16.9 KB
/
spi.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#include "attribute.h"
#include "application.h"
#include "spi.h"
#include "util.h"
#include "io_gpio.h"
#include "sys_time.h"
#include "stats.h"
typedef struct
{
const spi_clock_t clock;
const unsigned int pre_div;
const unsigned int div;
} spi_clock_map_t;
typedef struct
{
unsigned int fill:1;
unsigned int bits_available;
unsigned int word;
unsigned int bit;
unsigned int bits;
uint32_t data[SPI_W0_REGISTERS];
} spi_send_buffer_t;
assert_size(spi_send_buffer_t, 84);
typedef struct attr_packed
{
unsigned int inited:1;
unsigned int configured:1;
unsigned int cs_hold:1;
unsigned int spi_mode:4;
unsigned int receive_bytes:8;
struct attr_packed
{
unsigned int enabled:1;
unsigned int io:4;
unsigned int pin:4;
} user_cs;
} spi_state_t;
assert_size(spi_state_t, 4);
static roflash const spi_clock_map_t spi_clock_map[] =
{
{ spi_clock_80M, 1, 1, },
{ spi_clock_40M, 1, 2, },
{ spi_clock_27M, 1, 3, },
{ spi_clock_20M, 1, 4, },
{ spi_clock_16M, 1, 5, },
{ spi_clock_13M, 1, 6, },
{ spi_clock_11M, 1, 7, },
{ spi_clock_10M, 1, 8, },
{ spi_clock_9M, 1, 9, },
{ spi_clock_8M, 1, 10, },
{ spi_clock_7M, 1, 11, },
{ spi_clock_6M, 1, 13, },
{ spi_clock_5M, 1, 16, },
{ spi_clock_4M, 1, 20, },
{ spi_clock_3M, 2, 13, },
{ spi_clock_2M, 1, 40, },
{ spi_clock_1M, 2, 40, },
{ spi_clock_500k, 4, 40, },
{ spi_clock_250k, 8, 40, },
{ spi_clock_100k, 20, 40, },
{ spi_clock_50k, 40, 40, },
{ spi_clock_10k, 200, 40, },
{ spi_clock_1k, 2000, 40, },
{ spi_clock_none, 0, 0, },
};
static roflash const unsigned int mode_table[spi_mode_size][2] =
{
{ 0, 0 },
{ 0, 0 },
{ 0, 1 },
{ 1, 1 },
{ 1, 0 },
};
static spi_send_buffer_t send_buffer;
static spi_state_t state;
attr_inline void wait_completion(void)
{
stat_spi_wait_cycles = 0;
while((read_peri_reg(SPI_CMD(1)) & SPI_USR))
stat_spi_wait_cycles++;
}
bool spi_init(string_t *error, unsigned int io)
{
state.inited = 0;
state.configured = 0;
if(io != 0)
{
if(error)
string_append(error, "spi: only supported on gpio I/O (0)\n");
return(false);
}
send_buffer.bits_available = SPI_W0_REGISTERS * SPI_W0_REGISTER_BIT_WIDTH;
stat_spi_largest_chunk = stat_spi_wait_cycles = 0;
state.inited = 1;
return(true);
}
bool spi_configure(string_t *error, spi_mode_t mode, bool cs_hold, int user_cs_io, int user_cs_pin)
{
unsigned int reg_pin_mode;
unsigned int reg_spi_config;
if(!state.inited)
{
if(error)
string_append(error, "spi: not inited");
return(false);
}
if((mode <= spi_mode_none) || (mode >= spi_mode_size))
{
if(error)
string_append(error, "spi: invalid mode");
return(false);
}
state.user_cs.enabled = 0;
if((user_cs_io >= 0) && (user_cs_pin >= 0))
{
if((user_cs_io >= io_id_size) || (user_cs_pin >= max_pins_per_io))
{
if(error)
string_append(error, "spi: invalid io/pin for user cs\n");
}
else
{
state.user_cs.enabled = 1;
state.user_cs.io = user_cs_io;
state.user_cs.pin = user_cs_pin;
}
}
reg_spi_config = read_peri_reg(PERIPHS_IO_MUX);
reg_spi_config &= ~(PERIPHS_IO_MUX_HSPI_SYSCLK);
reg_spi_config |= PERIPHS_IO_MUX_HSPI_ENABLE;
write_peri_reg(PERIPHS_IO_MUX, reg_spi_config);
state.spi_mode = mode;
reg_pin_mode |= SPI_CS1_DIS | SPI_CS2_DIS;
if(state.user_cs.enabled)
reg_pin_mode |= SPI_CS0_DIS;
state.cs_hold = cs_hold;
state.configured = 1;
if(!spi_start(error))
return(false);
return(true);
}
bool spi_start(string_t *error)
{
unsigned int current;
if(!state.inited || !state.configured)
{
if(error)
string_append(error, "spi start: not inited\n");
return(false);
}
send_buffer.bits = 0;
send_buffer.word = 0;
send_buffer.bit = 0;
send_buffer.fill = 0;
for(current = 0; current < SPI_W0_REGISTERS; current++)
send_buffer.data[current] = 0;
return(true);
}
attr_result_used static bool spi_write_8(unsigned int value)
{
stat_spi_8++;
send_buffer.data[send_buffer.word] |= (value & 0xff) << send_buffer.bit;
send_buffer.bits += 8;
if(send_buffer.bit < 24)
send_buffer.bit += 8;
else
{
send_buffer.bit = 0;
send_buffer.word++;
}
return(true);
}
attr_result_used static bool spi_write_16(unsigned int value)
{
stat_spi_16++;
value = ((value & 0x00ff) << 8) |
((value & 0xff00) >> 8);
send_buffer.data[send_buffer.word] |= (value & 0xffff) << send_buffer.bit;
send_buffer.bits += 16;
if(send_buffer.bit < 16)
send_buffer.bit += 16;
else
{
send_buffer.bit = 0;
send_buffer.word++;
}
return(true);
}
unsigned int spi_write_bits_available(void)
{
if(!state.inited || !state.configured)
return(0);
return(send_buffer.bits_available - send_buffer.bits);
}
unsigned int spi_write_bits_used(void)
{
if(!state.inited || !state.configured)
return(0);
return(send_buffer.bits);
}
bool spi_write(unsigned int bits, uint32_t value)
{
int bit;
roflash static const int buffer_bit_index_map[32] =
{
7, 6, 5, 4, 3, 2, 1, 0,
15, 14, 13, 12, 11, 10, 9, 8,
23, 22, 21, 20, 19, 18, 17, 16,
31, 30, 29, 28, 27, 26, 25, 24,
};
if(!state.inited || !state.configured)
return(false);
if((send_buffer.bits + bits) > send_buffer.bits_available)
return(false);
if((bits == 8) && (send_buffer.bit % 8) == 0)
return(spi_write_8(value));
if((bits == 16) && (send_buffer.bit % 16) == 0)
return(spi_write_16(value));
for(bit = bits - 1; bit >= 0; bit--)
{
if(value & (1UL << bit))
send_buffer.data[send_buffer.word] |= (1UL << buffer_bit_index_map[send_buffer.bit]);
send_buffer.bits++;
if(send_buffer.bit < 31)
send_buffer.bit++;
else
{
send_buffer.bit = 0;
send_buffer.word++;
}
}
return(true);
}
attr_result_used bool spi_transmit(string_t *error, spi_clock_t clock,
unsigned int command_length_bits, unsigned int command,
unsigned int address_length_bits, unsigned int address,
unsigned int skip_bits,
unsigned int receive_bytes)
{
const spi_clock_map_t *clock_map_ptr;
unsigned int clock_pre_div, clock_div;
unsigned int clock_high, clock_low;
unsigned int w0cur, w0size;
unsigned int spi_user;
unsigned int spi_user1;
unsigned int spi_user2;
unsigned int spi_addr;
unsigned int spi_pin_mode;
unsigned int spi_clock;
if(!state.inited || !state.configured)
{
if(error)
string_append(error, "spi transmit: not inited or not configured");
return(false);
}
if((command_length_bits > 16) || (address_length_bits > 31) || (skip_bits > 8) || (receive_bytes > 64))
{
if(error)
string_append(error, "spi transmit: parameter error");
return(false);
}
if((command_length_bits == 0) && (address_length_bits == 0) && (send_buffer.bits == 0) && (receive_bytes == 0))
return(true);
for(clock_map_ptr = spi_clock_map; clock_map_ptr->clock != spi_clock_none; clock_map_ptr++)
if(clock_map_ptr->clock == clock)
break;
if(clock_map_ptr->clock == spi_clock_none)
{
if(error)
string_append(error, "spi transmit: invalid speed");
return(false);
}
if(send_buffer.bits > stat_spi_largest_chunk)
stat_spi_largest_chunk = send_buffer.bits;
spi_user = mode_table[state.spi_mode][1] ? SPI_CK_OUT_EDGE : 0; // CPHA
spi_user1 = 0x00;
spi_user2 = 0x00;
spi_addr = 0x00;
spi_pin_mode = mode_table[state.spi_mode][0] ? SPI_IDLE_EDGE : 0; // CPOL
if(command_length_bits > 0)
{
spi_user |= SPI_USR_COMMAND;
if(command_length_bits < 9)
command <<= (8 - command_length_bits);
else
{
command <<= (16 - command_length_bits);
command = ((command & 0x00ff) << 8) | ((command & 0xff00) >> 8);
}
spi_user2 |= ((command_length_bits - 1) & SPI_USR_COMMAND_BITLEN) << SPI_USR_COMMAND_BITLEN_S;
spi_user2 |= (command & SPI_USR_COMMAND_VALUE) << SPI_USR_COMMAND_VALUE_S;
}
if(address_length_bits > 0)
{
spi_user |= SPI_USR_ADDR;
address <<= (32 - address_length_bits);
spi_user1 |= ((address_length_bits - 1) & SPI_USR_ADDR_BITLEN) << SPI_USR_ADDR_BITLEN_S;
spi_addr = address;
}
if(skip_bits > 0)
{
spi_user |= SPI_USR_DUMMY;
spi_user1 |= ((skip_bits - 1) & SPI_USR_DUMMY_CYCLELEN) << SPI_USR_DUMMY_CYCLELEN_S;
}
if(send_buffer.bits > 0)
{
spi_user |= SPI_USR_MOSI;
spi_user1 |= ((send_buffer.bits - 1) & SPI_USR_MOSI_BITLEN) << SPI_USR_MOSI_BITLEN_S;
}
if(receive_bytes > 0)
{
spi_user |= SPI_USR_MISO;
spi_user1 |= (((receive_bytes * 8) - 1) & SPI_USR_MISO_BITLEN) << SPI_USR_MISO_BITLEN_S;
state.receive_bytes = receive_bytes;
}
clock_pre_div = clock_map_ptr->pre_div - 1;
clock_div = clock_map_ptr->div - 1;
clock_high = ((clock_div + 1) / 2) - 1;
clock_low = clock_div;
spi_clock = ((clock_pre_div & SPI_CLKDIV_PRE) << SPI_CLKDIV_PRE_S) |
((clock_div & SPI_CLKCNT_N) << SPI_CLKCNT_N_S) |
((clock_high & SPI_CLKCNT_H) << SPI_CLKCNT_H_S) |
((clock_low & SPI_CLKCNT_L) << SPI_CLKCNT_L_S);
if(clock == spi_clock_80M)
spi_clock |= SPI_CLK_EQU_SYSCLK;
if(state.cs_hold)
spi_user |= SPI_CS_SETUP | SPI_CS_HOLD;
wait_completion();
if(send_buffer.bits > 0)
{
w0size = (send_buffer.bits / SPI_W0_REGISTER_BIT_WIDTH) + 1;
if(w0size > SPI_W0_REGISTERS)
w0size = SPI_W0_REGISTERS;
for(w0cur = 0; w0cur < w0size; w0cur++)
write_peri_reg(SPI_W0(1) + (w0cur * 4), send_buffer.data[send_buffer.fill ? 0 : w0cur]);
}
write_peri_reg(SPI_ADDR(1), spi_addr);
write_peri_reg(SPI_USER(1), spi_user);
write_peri_reg(SPI_USER1(1), spi_user1);
write_peri_reg(SPI_USER2(1), spi_user2);
write_peri_reg(SPI_CLOCK(1), spi_clock);
write_peri_reg(SPI_PIN(1), spi_pin_mode);
if(state.user_cs.enabled && (io_write_pin(error, state.user_cs.io, state.user_cs.pin, 1) != io_ok))
{
if(error)
string_append(error, "spi: user cs issue (1)");
return(false);
}
set_peri_reg_mask(SPI_CMD(1), SPI_USR);
return(true);
}
attr_result_used bool spi_receive(string_t *error, unsigned int size, uint8_t *receive_data)
{
unsigned int current, current_value;
if(!state.inited || !state.configured)
{
if(error)
string_append(error, "spi receive: not inited or not configured");
return(false);
}
if(state.receive_bytes == 0)
{
if(error)
string_append(error, "spi receive: no data\n");
return(false);
}
wait_completion();
for(current = 0, current_value = 0; (current < size) && (current < state.receive_bytes); current++)
{
if((current & 0x03) == 0x00)
current_value = read_peri_reg(SPI_W0(1) + (current & ~0x03));
receive_data[current] = (current_value >> ((current & 0x03) << 3)) & 0xff;
}
state.receive_bytes = 0;
return(true);
}
attr_result_used bool spi_finish(string_t *error)
{
if(!state.inited || !state.configured)
{
if(error)
string_append(error, "spi finish: not inited or not configured");
return(false);
}
wait_completion();
if(state.user_cs.enabled)
{
if(io_write_pin(error, state.user_cs.io, state.user_cs.pin, 0) != io_ok)
{
if(error)
string_append(error, "spi finish: user cs issue");
return(false);
}
}
state.receive_bytes = 0;
return(true);
}
roflash const char help_description_spi_configure[] = "configure SPI interface\n"
"> usage: spc <mode=0-3> <cs delay=0|1> [<user cs io> <user cs pin>]\n";
app_action_t application_function_spi_configure(app_params_t *parameters)
{
string_new(, error, 64);
unsigned int spi_mode;
spi_mode_t spi_mode_enum;
unsigned int cs_delay;
int user_cs_io, user_cs_pin;
if(parse_uint(1, parameters->src, &spi_mode, 0, ' ') != parse_ok)
goto usage;
spi_mode_enum = (spi_mode_t)(spi_mode + spi_mode_0);
if((spi_mode_enum <= spi_mode_none) || (spi_mode_enum >= spi_mode_size))
goto usage;
if(parse_uint(2, parameters->src, &cs_delay, 0, ' ') != parse_ok)
goto usage;
if(parse_int(3, parameters->src, &user_cs_io, 0, ' ') != parse_ok)
user_cs_io = -1;
if((user_cs_io < -1) || (user_cs_io > 15))
goto usage;
if(parse_int(4, parameters->src, &user_cs_pin, 0, ' ') != parse_ok)
user_cs_pin = -1;
if((user_cs_pin < -1) || (user_cs_pin >= max_pins_per_io))
goto usage;
if(!spi_configure(&error, spi_mode_enum, cs_delay, user_cs_io, user_cs_pin))
{
string_format(parameters->dst, "spi configure failed: %s\n", string_to_cstr(&error));
return(app_action_error);
}
string_append(parameters->dst, "spi configure ok\n");
return(app_action_normal);
usage:
string_append_cstr_flash(parameters->dst, help_description_spi_configure);
string_append(parameters->dst, "> spi mode:\n");
string_append(parameters->dst, "> 0 clk=0 pha=0\n");
string_append(parameters->dst, "> 1 clk=0 pha=1\n");
string_append(parameters->dst, "> 2 clk=1 pha=0\n");
string_append(parameters->dst, "> 3 clk=1 pha=1\n");
return(app_action_error);
}
roflash const char help_description_spi_start[] = "prepare writing SPI send buffer data\n"
"usage: sps\n";
app_action_t application_function_spi_start(app_params_t *parameters)
{
string_new(, error, 64);
if(!spi_start(&error))
{
string_format(parameters->dst, "spi start failed: %s\n", string_to_cstr(&error));
return(app_action_error);
}
string_append(parameters->dst, "spi start ok\n");
return(app_action_normal);
}
roflash const char help_description_spi_write[] = "write data to SPI send buffer\n"
"usage: spw <bits=0-32> <value>\n";
app_action_t application_function_spi_write(app_params_t *parameters)
{
unsigned int bits, value;
unsigned int current;
if(parse_uint(1, parameters->src, &bits, 0, ' ') != parse_ok)
goto usage;
for(current = 2; current < 66; current++)
{
if(parse_uint(current, parameters->src, &value, 16, ' ') != parse_ok)
break;
if(!spi_write(bits, value))
{
string_format(parameters->dst, "spi write failed\n");
return(app_action_error);
}
}
string_append(parameters->dst, "spi write ok\n");
return(app_action_normal);
usage:
string_append_cstr_flash(parameters->dst, help_description_spi_write);
return(app_action_error);
}
roflash const char help_description_spi_transmit[] = "execute the SPI transaction (send and receive)\n"
"usage: spt <clock speed index>\n"
" <command length bits (0-15 bits)> <command value (hex)>\n"
" <address length bits (0-31 bits)> <address value (hex)>\n"
" <skip bytes\n"
" <receive bytes (0-64)\n";
app_action_t application_function_spi_transmit(app_params_t *parameters)
{
string_new(, error, 64);
unsigned int command_length, command;
unsigned int address_length, address;
unsigned int skip_length;
unsigned int receive_bytes;
unsigned int clock_speed;
spi_clock_t clock_speed_enum;
const spi_clock_map_t *clock_map_ptr;
if(parse_uint(1, parameters->src, &clock_speed, 0, ' ') != parse_ok)
goto usage;
clock_speed_enum = (spi_clock_t)clock_speed;
if((clock_speed_enum <= spi_clock_none) || (clock_speed_enum >= spi_clock_size))
goto usage;
if((parse_uint(2, parameters->src, &command_length, 0, ' ') != parse_ok) || (parse_uint(3, parameters->src, &command, 16, ' ') != parse_ok))
goto usage;
if((parse_uint(4, parameters->src, &address_length, 0, ' ') != parse_ok) || (parse_uint(5, parameters->src, &address, 16, ' ') != parse_ok))
goto usage;
if(parse_uint(6, parameters->src, &skip_length, 0, ' ') != parse_ok)
goto usage;
if(parse_uint(7, parameters->src, &receive_bytes, 0, ' ') != parse_ok)
goto usage;
if(!spi_transmit(&error, clock_speed_enum, command_length, command, address_length, address, skip_length, receive_bytes))
{
string_format(parameters->dst, "spi transmit failed: %s\n", string_to_cstr(&error));
return(app_action_error);
}
string_format(parameters->dst, "spi transmit ok\n");
return(app_action_normal);
usage:
string_append_cstr_flash(parameters->dst, help_description_spi_transmit);
string_append(parameters->dst, ">\n");
for(clock_map_ptr = spi_clock_map; clock_map_ptr->clock != spi_clock_none; clock_map_ptr++)
string_format(parameters->dst, "> %2u %10u\n", clock_map_ptr->clock, (unsigned int)(80000000.0 / clock_map_ptr->pre_div / clock_map_ptr->div));
return(app_action_error);
}
roflash const char help_description_spi_receive[] = "fetch data received by SPI transaction\n"
"usage: spr <number of bytes to read (0 - 63)>\n";
app_action_t application_function_spi_receive(app_params_t *parameters)
{
string_new(, error, 64);
unsigned int current, length;
uint8_t buffer[64];
if(parse_uint(1, parameters->src, &length, 0, ' ') != parse_ok)
goto usage;
if(length > 64)
goto usage;
if(!spi_receive(&error, length, buffer))
{
string_format(parameters->dst, "spi receive failed: %s\n", string_to_cstr(&error));
return(app_action_error);
}
string_append(parameters->dst, "spi receive ok, received: ");
for(current = 0; current < length; current++)
string_format(parameters->dst, "%02x ", buffer[current]);
string_append(parameters->dst, "\n");
return(app_action_normal);
usage:
string_append_cstr_flash(parameters->dst, help_description_spi_receive);
return(app_action_error);
}
roflash const char help_description_spi_finish[] = "finish SPI transaction\n"
"usage: spf\n";
app_action_t application_function_spi_finish(app_params_t *parameters)
{
string_new(, error, 64);
if(!spi_finish(&error))
{
string_format(parameters->dst, "spi finish failed: %s\n", string_to_cstr(&error));
return(app_action_error);
}
string_append(parameters->dst, "spi finish ok\n");
return(app_action_normal);
}