forked from analogdevicesinc/msdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspi_ai87.c
More file actions
626 lines (502 loc) · 15 KB
/
Copy pathspi_ai87.c
File metadata and controls
626 lines (502 loc) · 15 KB
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
/******************************************************************************
*
* Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
* Analog Devices, Inc.),
* Copyright (C) 2023-2024 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "mxc_device.h"
#include "mxc_assert.h"
#include "mxc_lock.h"
#include "mxc_sys.h"
#include "mxc_delay.h"
#include "spi_reva1.h"
#include "dma.h"
/* **** Definitions **** */
/* ************************************************************************** */
int MXC_SPI_Init(mxc_spi_regs_t *spi, mxc_spi_type_t controller_target, mxc_spi_interface_t if_mode,
int numTargets, uint8_t ts_active_pol_mask, uint32_t freq, mxc_spi_pins_t pins)
{
int spi_num;
// Remap input parameters for v1 implementation.
int masterMode = controller_target;
int quadModeUsed = if_mode;
int numSlaves = numTargets;
int ssPolarity = ts_active_pol_mask;
int hz = freq;
spi_num = MXC_SPI_GET_IDX(spi);
MXC_ASSERT(spi_num >= 0);
if (numSlaves > MXC_SPI_SS_INSTANCES) {
return E_BAD_PARAM;
}
// Check if frequency is too high
if ((spi_num == 0) && (hz > PeripheralClock)) {
return E_BAD_PARAM;
}
if ((spi_num == 1) && (hz > SystemCoreClock)) {
return E_BAD_PARAM;
}
mxc_gpio_cfg_t gpio_cfg_spi;
gpio_cfg_spi.pad = MXC_GPIO_PAD_NONE;
gpio_cfg_spi.port = MXC_GPIO0;
// Set VDDIO level
if (pins.vddioh) {
gpio_cfg_spi.vssel = MXC_GPIO_VSSEL_VDDIOH;
} else {
gpio_cfg_spi.vssel = MXC_GPIO_VSSEL_VDDIO;
}
// Configure GPIO for spi
if (spi == MXC_SPI1) {
MXC_SYS_Reset_Periph(MXC_SYS_RESET0_SPI1);
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_SPI1);
//Define pins
if (pins.ss1) {
gpio_cfg_spi.mask = MXC_GPIO_PIN_26;
gpio_cfg_spi.func = MXC_GPIO_FUNC_ALT2;
MXC_GPIO_Config(&gpio_cfg_spi);
}
if (pins.ss2) {
gpio_cfg_spi.func = MXC_GPIO_FUNC_ALT2;
gpio_cfg_spi.mask = MXC_GPIO_PIN_27;
MXC_GPIO_Config(&gpio_cfg_spi);
}
//clear mask
gpio_cfg_spi.mask = 0;
// check rest of the pins
if (pins.clock) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_23;
}
if (pins.miso) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_22;
}
if (pins.mosi) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_21;
}
if (pins.sdio2) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_24;
}
if (pins.sdio3) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_25;
}
if (pins.ss0) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_20;
}
gpio_cfg_spi.func = MXC_GPIO_FUNC_ALT1;
#ifdef MXC_SPI0
} else if (spi == MXC_SPI0) {
MXC_SYS_Reset_Periph(MXC_SYS_RESET1_SPI0);
MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_SPI0);
//Define pins
if (pins.ss1) {
gpio_cfg_spi.mask = MXC_GPIO_PIN_11;
gpio_cfg_spi.func = MXC_GPIO_FUNC_ALT2;
MXC_GPIO_Config(&gpio_cfg_spi);
}
if (pins.ss2) {
gpio_cfg_spi.func = MXC_GPIO_FUNC_ALT2;
gpio_cfg_spi.mask = MXC_GPIO_PIN_10;
MXC_GPIO_Config(&gpio_cfg_spi);
}
//clear mask
gpio_cfg_spi.mask = 0;
// check rest of the pins
if (pins.clock) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_7;
}
if (pins.miso) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_6;
}
if (pins.mosi) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_5;
}
if (pins.sdio2) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_8;
}
if (pins.sdio3) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_9;
}
if (pins.ss0) {
gpio_cfg_spi.mask |= MXC_GPIO_PIN_4;
}
gpio_cfg_spi.func = MXC_GPIO_FUNC_ALT1;
#endif
} else {
return E_NO_DEVICE;
}
MXC_GPIO_Config(&gpio_cfg_spi);
return MXC_SPI_RevA1_Init((mxc_spi_reva_regs_t *)spi, masterMode, quadModeUsed, numSlaves,
ssPolarity, hz);
}
int MXC_SPI_Shutdown(mxc_spi_regs_t *spi)
{
int spi_num;
spi_num = MXC_SPI_GET_IDX(spi);
MXC_ASSERT(spi_num >= 0);
(void)spi_num;
MXC_SPI_RevA1_Shutdown((mxc_spi_reva_regs_t *)spi);
if (spi == MXC_SPI1) {
MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_SPI1);
#ifdef MXC_SPI0
} else if (spi == MXC_SPI0) {
MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_SPI0);
#endif
} else {
return E_NO_DEVICE;
}
return E_NO_ERROR;
}
int MXC_SPI_ReadyForSleep(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_ReadyForSleep((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_GetPeripheralClock(mxc_spi_regs_t *spi)
{
int retval;
if (spi == MXC_SPI1) {
retval = PeripheralClock;
#ifdef MXC_SPI0 // SPI0 is not accessible from the RISC core.
} else if (spi == MXC_SPI0) {
int sys_clk = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_SEL) >>
MXC_F_GCR_CLKCTRL_SYSCLK_SEL_POS;
switch (sys_clk) {
case MXC_SYS_CLOCK_IPO:
retval = IPO_FREQ;
break;
case MXC_SYS_CLOCK_IBRO:
retval = IBRO_FREQ;
break;
case MXC_SYS_CLOCK_ISO:
retval = ISO_FREQ;
break;
case MXC_SYS_CLOCK_INRO:
retval = INRO_FREQ;
break;
case MXC_SYS_CLOCK_ERTCO:
retval = ERTCO_FREQ;
break;
case MXC_SYS_CLOCK_EXTCLK:
retval = EXTCLK_FREQ;
break;
default:
return E_BAD_STATE;
}
#endif // MXC_SPI0
} else {
return E_BAD_PARAM;
}
retval /= 2;
return retval;
}
int MXC_SPI_SetFrequency(mxc_spi_regs_t *spi, unsigned int hz)
{
return MXC_SPI_RevA1_SetFrequency((mxc_spi_reva_regs_t *)spi, hz);
}
unsigned int MXC_SPI_GetFrequency(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetFrequency((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_SetDataSize(mxc_spi_regs_t *spi, int dataSize)
{
return MXC_SPI_RevA1_SetDataSize((mxc_spi_reva_regs_t *)spi, dataSize);
}
int MXC_SPI_GetDataSize(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetDataSize((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_SetSlave(mxc_spi_regs_t *spi, int ssIdx)
{
return MXC_SPI_RevA1_SetSlave((mxc_spi_reva_regs_t *)spi, ssIdx);
}
int MXC_SPI_GetSlave(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetSlave((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_SetWidth(mxc_spi_regs_t *spi, mxc_spi_width_t spiWidth)
{
return MXC_SPI_RevA1_SetWidth((mxc_spi_reva_regs_t *)spi, spiWidth);
}
mxc_spi_width_t MXC_SPI_GetWidth(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetWidth((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_SetMode(mxc_spi_regs_t *spi, mxc_spi_mode_t spiMode)
{
return MXC_SPI_RevA1_SetMode((mxc_spi_reva_regs_t *)spi, spiMode);
}
mxc_spi_mode_t MXC_SPI_GetMode(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetMode((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_StartTransmission(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_StartTransmission((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_GetActive(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetActive((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_AbortTransmission(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_AbortTransmission((mxc_spi_reva_regs_t *)spi);
}
unsigned int MXC_SPI_ReadRXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len)
{
return MXC_SPI_RevA1_ReadRXFIFO((mxc_spi_reva_regs_t *)spi, bytes, len);
}
unsigned int MXC_SPI_GetRXFIFOAvailable(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetRXFIFOAvailable((mxc_spi_reva_regs_t *)spi);
}
unsigned int MXC_SPI_WriteTXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len)
{
return MXC_SPI_RevA1_WriteTXFIFO((mxc_spi_reva_regs_t *)spi, bytes, len);
}
unsigned int MXC_SPI_GetTXFIFOAvailable(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetTXFIFOAvailable((mxc_spi_reva_regs_t *)spi);
}
void MXC_SPI_ClearRXFIFO(mxc_spi_regs_t *spi)
{
MXC_SPI_RevA1_ClearRXFIFO((mxc_spi_reva_regs_t *)spi);
}
void MXC_SPI_ClearTXFIFO(mxc_spi_regs_t *spi)
{
MXC_SPI_RevA1_ClearTXFIFO((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_SetRXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes)
{
return MXC_SPI_RevA1_SetRXThreshold((mxc_spi_reva_regs_t *)spi, numBytes);
}
unsigned int MXC_SPI_GetRXThreshold(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetRXThreshold((mxc_spi_reva_regs_t *)spi);
}
int MXC_SPI_SetTXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes)
{
return MXC_SPI_RevA1_SetTXThreshold((mxc_spi_reva_regs_t *)spi, numBytes);
}
unsigned int MXC_SPI_GetTXThreshold(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetTXThreshold((mxc_spi_reva_regs_t *)spi);
}
unsigned int MXC_SPI_GetFlags(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetFlags((mxc_spi_reva_regs_t *)spi);
}
void MXC_SPI_ClearFlags(mxc_spi_regs_t *spi)
{
MXC_SPI_RevA1_ClearFlags((mxc_spi_reva_regs_t *)spi);
}
unsigned int MXC_SPI_GetAndClearFlags(mxc_spi_regs_t *spi)
{
return MXC_SPI_RevA1_GetAndClearFlags((mxc_spi_reva_regs_t *)spi);
}
void MXC_SPI_EnableInt(mxc_spi_regs_t *spi, unsigned int intEn)
{
MXC_SPI_RevA1_EnableInt((mxc_spi_reva_regs_t *)spi, intEn);
}
void MXC_SPI_DisableInt(mxc_spi_regs_t *spi, unsigned int intDis)
{
MXC_SPI_RevA1_DisableInt((mxc_spi_reva_regs_t *)spi, intDis);
}
int MXC_SPI_MasterTransaction(mxc_spi_req_t *req)
{
return MXC_SPI_RevA1_MasterTransaction((mxc_spi_reva_req_t *)req);
}
int MXC_SPI_MasterTransactionAsync(mxc_spi_req_t *req)
{
return MXC_SPI_RevA1_MasterTransactionAsync((mxc_spi_reva_req_t *)req);
}
int MXC_SPI_MasterTransactionDMA(mxc_spi_req_t *req)
{
int reqselTx = -1;
int reqselRx = -1;
int spi_num;
spi_num = MXC_SPI_GET_IDX(req->spi);
MXC_ASSERT(spi_num >= 0);
switch (spi_num) {
case 0:
reqselTx = MXC_DMA_REQUEST_SPI1TX;
reqselRx = MXC_DMA_REQUEST_SPI1RX;
break;
case 1:
reqselTx = MXC_DMA_REQUEST_SPI0TX;
reqselRx = MXC_DMA_REQUEST_SPI0RX;
break;
default:
return E_BAD_PARAM;
}
return MXC_SPI_RevA1_MasterTransactionDMA((mxc_spi_reva_req_t *)req, reqselTx, reqselRx,
MXC_DMA);
}
int MXC_SPI_SlaveTransaction(mxc_spi_req_t *req)
{
return MXC_SPI_RevA1_SlaveTransaction((mxc_spi_reva_req_t *)req);
}
int MXC_SPI_SlaveTransactionAsync(mxc_spi_req_t *req)
{
return MXC_SPI_RevA1_SlaveTransactionAsync((mxc_spi_reva_req_t *)req);
}
int MXC_SPI_SlaveTransactionDMA(mxc_spi_req_t *req)
{
int reqselTx = -1;
int reqselRx = -1;
int spi_num;
spi_num = MXC_SPI_GET_IDX(req->spi);
MXC_ASSERT(spi_num >= 0);
switch (spi_num) {
case 0:
reqselTx = MXC_DMA_REQUEST_SPI1TX;
reqselRx = MXC_DMA_REQUEST_SPI1RX;
break;
case 1:
reqselTx = MXC_DMA_REQUEST_SPI0TX;
reqselRx = MXC_DMA_REQUEST_SPI0RX;
break;
default:
return E_BAD_PARAM;
break;
}
return MXC_SPI_RevA1_SlaveTransactionDMA((mxc_spi_reva_req_t *)req, reqselTx, reqselRx,
MXC_DMA);
}
int MXC_SPI_SetDefaultTXData(mxc_spi_regs_t *spi, unsigned int defaultTXData)
{
return MXC_SPI_RevA1_SetDefaultTXData((mxc_spi_reva_regs_t *)spi, defaultTXData);
}
void MXC_SPI_AbortAsync(mxc_spi_regs_t *spi)
{
MXC_SPI_RevA1_AbortAsync((mxc_spi_reva_regs_t *)spi);
}
void MXC_SPI_AsyncHandler(mxc_spi_regs_t *spi)
{
MXC_SPI_RevA1_AsyncHandler((mxc_spi_reva_regs_t *)spi);
}
void MXC_SPI_HWSSControl(mxc_spi_regs_t *spi, int state)
{
MXC_SPI_RevA1_HWSSControl((mxc_spi_reva_regs_t *)spi, state);
}
/* ** SPI v2 functions to prevent build errors ** */
int MXC_SPI_Config(mxc_spi_cfg_t *cfg)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_ConfigStruct(mxc_spi_cfg_t *cfg, bool use_dma_tx, bool use_dma_rx)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_SetTSControl(mxc_spi_regs_t *spi, mxc_spi_tscontrol_t ts_control)
{
return E_NOT_SUPPORTED;
}
mxc_spi_tscontrol_t MXC_SPI_GetTSControl(mxc_spi_regs_t *spi)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_SetFrameSize(mxc_spi_regs_t *spi, int frame_size)
{
return MXC_SPI_SetDataSize(spi, frame_size);
}
int MXC_SPI_GetFrameSize(mxc_spi_regs_t *spi)
{
return MXC_SPI_GetDataSize(spi);
}
int MXC_SPI_SetInterface(mxc_spi_regs_t *spi, mxc_spi_interface_t mode)
{
return E_NOT_SUPPORTED;
}
mxc_spi_interface_t MXC_SPI_GetInterface(mxc_spi_regs_t *spi)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_SetClkMode(mxc_spi_regs_t *spi, mxc_spi_clkmode_t clk_mode)
{
return E_NOT_SUPPORTED;
}
mxc_spi_clkmode_t MXC_SPI_GetClkMode(mxc_spi_regs_t *spi)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_SetCallback(mxc_spi_regs_t *spi, mxc_spi_callback_t callback, void *data)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_SetDummyTX(mxc_spi_regs_t *spi, uint16_t tx_value)
{
return MXC_SPI_SetDefaultTXData(spi, tx_value);
}
/* ** DMA-Specific Functions ** */
int MXC_SPI_DMA_Init(mxc_spi_regs_t *spi, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx)
{
return E_NOT_SUPPORTED;
}
bool MXC_SPI_DMA_GetInitialized(mxc_spi_regs_t *spi)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_DMA_GetTXChannel(mxc_spi_regs_t *spi)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_DMA_GetRXChannel(mxc_spi_regs_t *spi)
{
return E_NOT_SUPPORTED;
}
int MXC_SPI_DMA_SetRequestSelect(mxc_spi_regs_t *spi, bool use_dma_tx, bool use_dma_rx)
{
return E_NOT_SUPPORTED;
}
/* ** Transaction Functions ** */
int MXC_SPI_ControllerTransaction(mxc_spi_req_t *req)
{
return MXC_SPI_MasterTransaction(req);
}
int MXC_SPI_ControllerTransactionAsync(mxc_spi_req_t *req)
{
return MXC_SPI_MasterTransactionAsync(req);
}
int MXC_SPI_ControllerTransactionDMA(mxc_spi_req_t *req)
{
return MXC_SPI_MasterTransactionDMA(req);
}
int MXC_SPI_TargetTransaction(mxc_spi_req_t *req)
{
return MXC_SPI_SlaveTransaction(req);
}
int MXC_SPI_TargetTransactionAsync(mxc_spi_req_t *req)
{
return MXC_SPI_SlaveTransactionAsync(req);
}
int MXC_SPI_TargetTransactionDMA(mxc_spi_req_t *req)
{
return MXC_SPI_SlaveTransactionDMA(req);
}
/* ** Handler Functions ** */
void MXC_SPI_Handler(mxc_spi_regs_t *spi)
{
MXC_SPI_AsyncHandler(spi);
}
void MXC_SPI_DMA_TX_Handler(mxc_spi_regs_t *spi)
{
return;
}
void MXC_SPI_DMA_RX_Handler(mxc_spi_regs_t *spi)
{
return;
}