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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
|
/**
* \file
*
* \brief SAM Serial Peripheral Interface Driver
*
* Copyright (C) 2012-2016 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include "spi.h"
/**
* \brief Resets the SPI module
*
* This function will reset the SPI module to its power on default values and
* disable it.
*
* \param[in,out] module Pointer to the software instance struct
*/
void spi_reset(
struct spi_module *const module)
{
/* Sanity check arguments */
Assert(module);
Assert(module->hw);
SercomSpi *const spi_module = &(module->hw->SPI);
/* Disable the module */
spi_disable(module);
while (spi_is_syncing(module)) {
/* Wait until the synchronization is complete */
}
/* Software reset the module */
spi_module->CTRLA.reg |= SERCOM_SPI_CTRLA_SWRST;
}
/**
* \brief Set the baudrate of the SPI module
*
* This function will set the baudrate of the SPI module.
*
* \param[in] module Pointer to the software instance struct
* \param[in] baudrate The baudrate wanted
*
* \return The status of the configuration.
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
* \retval STATUS_OK If the configuration was written
*/
enum status_code spi_set_baudrate(
struct spi_module *const module,
uint32_t baudrate)
{
/* Sanity check arguments */
Assert(module);
Assert(baudrate);
Assert(module->hw);
/* Value to write to BAUD register */
uint16_t baud = 0;
SercomSpi *const spi_module = &(module->hw->SPI);
/* Disable the module */
spi_disable(module);
while (spi_is_syncing(module)) {
/* Wait until the synchronization is complete */
}
/* Find frequency of the internal SERCOMi_GCLK_ID_CORE */
uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
uint32_t gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
uint32_t internal_clock = system_gclk_chan_get_hz(gclk_index);
/* Get baud value, based on baudrate and the internal clock frequency */
enum status_code error_code = _sercom_get_sync_baud_val(
baudrate, internal_clock, &baud);
if (error_code != STATUS_OK) {
/* Baud rate calculation error, return status code */
return STATUS_ERR_INVALID_ARG;
}
spi_module->BAUD.reg = (uint8_t)baud;
while (spi_is_syncing(module)) {
/* Wait until the synchronization is complete */
}
/* Enable the module */
spi_enable(module);
while (spi_is_syncing(module)) {
/* Wait until the synchronization is complete */
}
return STATUS_OK;
}
# if CONF_SPI_SLAVE_ENABLE == true
/**
* \internal Clears the Transmit Complete interrupt flag.
*
* \param[in] module Pointer to the software instance struct
*/
static void _spi_clear_tx_complete_flag(
struct spi_module *const module)
{
/* Sanity check arguments */
Assert(module);
Assert(module->hw);
SercomSpi *const spi_module = &(module->hw->SPI);
/* Clear interrupt flag */
spi_module->INTFLAG.reg = SPI_INTERRUPT_FLAG_TX_COMPLETE;
}
# endif
/**
* \internal Writes an SPI SERCOM configuration to the hardware module.
*
* This function will write out a given configuration to the hardware module.
* Can only be done when the module is disabled.
*
* \param[in] module Pointer to the software instance struct
* \param[in] config Pointer to the configuration struct
*
* \return The status of the configuration.
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
* \retval STATUS_OK If the configuration was written
*/
static enum status_code _spi_set_config(
struct spi_module *const module,
const struct spi_config *const config)
{
/* Sanity check arguments */
Assert(module);
Assert(config);
Assert(module->hw);
SercomSpi *const spi_module = &(module->hw->SPI);
Sercom *const hw = module->hw;
struct system_pinmux_config pin_conf;
system_pinmux_get_config_defaults(&pin_conf);
pin_conf.direction = SYSTEM_PINMUX_PIN_DIR_INPUT;
if(config->mode == SPI_MODE_SLAVE) {
pin_conf.input_pull = SYSTEM_PINMUX_PIN_PULL_NONE;
}
uint32_t pad_pinmuxes[] = {
config->pinmux_pad0, config->pinmux_pad1,
config->pinmux_pad2, config->pinmux_pad3
};
/* Configure the SERCOM pins according to the user configuration */
for (uint8_t pad = 0; pad < 4; pad++) {
uint32_t current_pinmux = pad_pinmuxes[pad];
if (current_pinmux == PINMUX_DEFAULT) {
current_pinmux = _sercom_get_default_pad(hw, pad);
}
if (current_pinmux != PINMUX_UNUSED) {
pin_conf.mux_position = current_pinmux & 0xFFFF;
system_pinmux_pin_set_config(current_pinmux >> 16, &pin_conf);
}
}
module->mode = config->mode;
module->character_size = config->character_size;
module->receiver_enabled = config->receiver_enable;
# ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
module->master_slave_select_enable = config->master_slave_select_enable;
# endif
# if CONF_SPI_MASTER_ENABLE == true
/* Value to write to BAUD register */
uint16_t baud = 0;
# endif
/* Value to write to CTRLA register */
uint32_t ctrla = 0;
/* Value to write to CTRLB register */
uint32_t ctrlb = 0;
# if CONF_SPI_MASTER_ENABLE == true
/* Find baud value and write it */
if (config->mode == SPI_MODE_MASTER) {
/* Find frequency of the internal SERCOMi_GCLK_ID_CORE */
uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
uint32_t gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
uint32_t internal_clock = system_gclk_chan_get_hz(gclk_index);
/* Get baud value, based on baudrate and the internal clock frequency */
enum status_code error_code = _sercom_get_sync_baud_val(
config->mode_specific.master.baudrate,
internal_clock, &baud);
if (error_code != STATUS_OK) {
/* Baud rate calculation error, return status code */
return STATUS_ERR_INVALID_ARG;
}
spi_module->BAUD.reg = (uint8_t)baud;
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
if (config->mode == SPI_MODE_SLAVE) {
/* Set frame format */
ctrla = config->mode_specific.slave.frame_format;
/* Set address mode */
ctrlb = config->mode_specific.slave.address_mode;
/* Set address and address mask*/
spi_module->ADDR.reg |=
(config->mode_specific.slave.address << SERCOM_SPI_ADDR_ADDR_Pos) |
(config->mode_specific.slave.address_mask << SERCOM_SPI_ADDR_ADDRMASK_Pos);
if (config->mode_specific.slave.preload_enable) {
/* Enable pre-loading of shift register */
ctrlb |= SERCOM_SPI_CTRLB_PLOADEN;
}
}
# endif
/* Set data order */
ctrla |= config->data_order;
/* Set clock polarity and clock phase */
ctrla |= config->transfer_mode;
/* Set MUX setting */
ctrla |= config->mux_setting;
/* Set SPI character size */
ctrlb |= config->character_size;
/* Set whether module should run in standby. */
if (config->run_in_standby || system_is_debugger_present()) {
ctrla |= SERCOM_SPI_CTRLA_RUNSTDBY;
}
if (config->receiver_enable) {
/* Enable receiver */
ctrlb |= SERCOM_SPI_CTRLB_RXEN;
}
# ifdef FEATURE_SPI_SLAVE_SELECT_LOW_DETECT
if (config->select_slave_low_detect_enable) {
/* Enable Slave Select Low Detect */
ctrlb |= SERCOM_SPI_CTRLB_SSDE;
}
# endif
# ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
if (config->master_slave_select_enable) {
/* Enable Master Slave Select */
ctrlb |= SERCOM_SPI_CTRLB_MSSEN;
}
# endif
/* Write CTRLA register */
spi_module->CTRLA.reg |= ctrla;
/* Write CTRLB register */
spi_module->CTRLB.reg |= ctrlb;
return STATUS_OK;
}
#if SPI_CALLBACK_MODE == false
/**
* \internal Checks an SPI config against current set config
*
* This function will check that the config does not alter the
* configuration of the module. If the new config changes any
* setting, the initialization will be discarded.
*
* \param[in] module Pointer to the software instance struct
* \param[in] config Pointer to the configuration struct
*
* \return The status of the configuration.
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
* \retval STATUS_ERR_DENIED If configuration was different from previous
* \retval STATUS_OK If the configuration was written
*/
static enum status_code _spi_check_config(
struct spi_module *const module,
const struct spi_config *const config)
{
/* Sanity check arguments */
Assert(module);
Assert(config);
Assert(module->hw);
SercomSpi *const spi_module = &(module->hw->SPI);
Sercom *const hw = module->hw;
uint32_t pad_pinmuxes[] = {
config->pinmux_pad0, config->pinmux_pad1,
config->pinmux_pad2, config->pinmux_pad3
};
/* Compare the current SERCOM pins against the user configuration */
for (uint8_t pad = 0; pad < 4; pad++) {
uint32_t current_pinmux = pad_pinmuxes[pad];
if (current_pinmux == PINMUX_DEFAULT) {
current_pinmux = _sercom_get_default_pad(hw, pad);
}
if (current_pinmux == PINMUX_UNUSED) {
continue;
}
if ((current_pinmux & 0xFFFF) !=
system_pinmux_pin_get_mux_position(current_pinmux >> 16)) {
module->hw = NULL;
return STATUS_ERR_DENIED;
}
}
# if CONF_SPI_MASTER_ENABLE == true
/* Value to read BAUD register */
uint16_t baud;
uint32_t external_clock = system_gclk_chan_get_hz(SERCOM_GCLK_ID);
# endif
/* Value to read CTRLA, CTRLB and ADDR register */
uint32_t ctrla = 0;
uint32_t ctrlb = 0;
# if CONF_SPI_SLAVE_ENABLE == true
uint32_t addr = 0;
# endif
# if CONF_SPI_MASTER_ENABLE == true
/* Find baud value and compare it */
if (config->mode == SPI_MODE_MASTER) {
enum status_code error_code = _sercom_get_sync_baud_val(
config->mode_specific.master.baudrate,
external_clock, &baud);
if (error_code != STATUS_OK) {
/* Baud rate calculation error, return status code */
return STATUS_ERR_INVALID_ARG;
}
if (spi_module->BAUD.reg != (uint8_t)baud) {
return STATUS_ERR_DENIED;
}
ctrla |= SERCOM_SPI_CTRLA_MODE(0x3);
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
if (config->mode == SPI_MODE_SLAVE) {
/* Set frame format */
ctrla |= config->mode_specific.slave.frame_format;
/* Set address mode */
ctrlb |= config->mode_specific.slave.address_mode;
/* Set address and address mask*/
addr |= (config->mode_specific.slave.address << SERCOM_SPI_ADDR_ADDR_Pos) |
(config->mode_specific.slave.address_mask << SERCOM_SPI_ADDR_ADDRMASK_Pos);
if (spi_module->CTRLA.reg != addr) {
return STATUS_ERR_DENIED;
}
if (config->mode_specific.slave.preload_enable) {
/* Enable pre-loading of shift register */
ctrlb |= SERCOM_SPI_CTRLB_PLOADEN;
}
ctrla |= SERCOM_SPI_CTRLA_MODE(0x2);
}
# endif
/* Set data order */
ctrla |= config->data_order;
/* Set clock polarity and clock phase */
ctrla |= config->transfer_mode;
/* Set MUX setting */
ctrla |= config->mux_setting;
/* Set SPI character size */
ctrlb |= config->character_size;
if (config->run_in_standby) {
/* Enable in sleep mode */
ctrla |= SERCOM_SPI_CTRLA_RUNSTDBY;
}
if (config->receiver_enable) {
/* Enable receiver */
ctrlb |= SERCOM_SPI_CTRLB_RXEN;
}
# ifdef FEATURE_SPI_SLAVE_SELECT_LOW_DETECT
if (config->select_slave_low_detect_enable) {
/* Enable Slave Select Low Detect */
ctrlb |= SERCOM_SPI_CTRLB_SSDE;
}
# endif
# ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
if (config->master_slave_select_enable) {
/* Enable Master Slave Select */
ctrlb |= SERCOM_SPI_CTRLB_MSSEN;
}
# endif
ctrla |= SERCOM_SPI_CTRLA_ENABLE;
/* Check that same config is set */
if (spi_module->CTRLA.reg == ctrla &&
spi_module->CTRLB.reg == ctrlb) {
module->mode = config->mode;
module->character_size = config->character_size;
return STATUS_OK;
}
/* Not same config, wipe module pointer and return */
module->hw = NULL;
return STATUS_ERR_DENIED;
}
#endif
/**
* \brief Initializes the SERCOM SPI module
*
* This function will initialize the SERCOM SPI module, based on the values
* of the config struct.
*
* \param[out] module Pointer to the software instance struct
* \param[in] hw Pointer to hardware instance
* \param[in] config Pointer to the config struct
*
* \return Status of the initialization.
* \retval STATUS_OK Module initiated correctly
* \retval STATUS_ERR_DENIED If module is enabled
* \retval STATUS_BUSY If module is busy resetting
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
*/
enum status_code spi_init(
struct spi_module *const module,
Sercom *const hw,
const struct spi_config *const config)
{
/* Sanity check arguments */
Assert(module);
Assert(hw);
Assert(config);
/* Initialize device instance */
module->hw = hw;
SercomSpi *const spi_module = &(module->hw->SPI);
/* Check if module is enabled. */
if (spi_module->CTRLA.reg & SERCOM_SPI_CTRLA_ENABLE) {
# if SPI_CALLBACK_MODE == false
/* Check if config is valid */
return _spi_check_config(module, config);
# else
return STATUS_ERR_DENIED;
# endif
}
/* Check if reset is in progress. */
if (spi_module->CTRLA.reg & SERCOM_SPI_CTRLA_SWRST){
return STATUS_BUSY;
}
uint32_t sercom_index = _sercom_get_sercom_inst_index(module->hw);
uint32_t pm_index, gclk_index;
#if (SAML21) || (SAMR30)
if (sercom_index == 5) {
# ifdef ID_SERCOM5
pm_index = MCLK_APBDMASK_SERCOM5_Pos;
gclk_index = SERCOM5_GCLK_ID_CORE;
# else
return STATUS_ERR_INVALID_ARG;
# endif
} else {
pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
}
#elif (SAMC21)
if (sercom_index == 5) {
# ifdef ID_SERCOM5
pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
gclk_index = SERCOM5_GCLK_ID_CORE;
# else
return STATUS_ERR_INVALID_ARG;
# endif
} else {
pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
}
#elif (SAMC20) || (SAML22)
pm_index = sercom_index + MCLK_APBCMASK_SERCOM0_Pos;
gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
#else
pm_index = sercom_index + PM_APBCMASK_SERCOM0_Pos;
gclk_index = sercom_index + SERCOM0_GCLK_ID_CORE;
#endif
/* Turn on module in PM */
#if (SAML21) || (SAMR30)
if (sercom_index == 5) {
# ifdef ID_SERCOM5
system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBD, 1 << pm_index);
# else
return STATUS_ERR_INVALID_ARG;
# endif
} else {
system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, 1 << pm_index);
}
#else
system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, 1 << pm_index);
#endif
/* Set up the GCLK for the module */
struct system_gclk_chan_config gclk_chan_conf;
system_gclk_chan_get_config_defaults(&gclk_chan_conf);
gclk_chan_conf.source_generator = config->generator_source;
system_gclk_chan_set_config(gclk_index, &gclk_chan_conf);
system_gclk_chan_enable(gclk_index);
sercom_set_gclk_generator(config->generator_source, false);
# if CONF_SPI_MASTER_ENABLE == true
if (config->mode == SPI_MODE_MASTER) {
/* Set the SERCOM in SPI master mode */
spi_module->CTRLA.reg |= SERCOM_SPI_CTRLA_MODE(0x3);
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
if (config->mode == SPI_MODE_SLAVE) {
/* Set the SERCOM in SPI slave mode */
spi_module->CTRLA.reg |= SERCOM_SPI_CTRLA_MODE(0x2);
}
# endif
#if SPI_CALLBACK_MODE == true
/* Temporary variables */
uint8_t i;
uint8_t instance_index;
/* Initialize parameters */
for (i = 0; i < SPI_CALLBACK_N; i++) {
module->callback[i] = NULL;
}
module->tx_buffer_ptr = NULL;
module->rx_buffer_ptr = NULL;
module->remaining_tx_buffer_length = 0x0000;
module->remaining_rx_buffer_length = 0x0000;
module->registered_callback = 0x00;
module->enabled_callback = 0x00;
module->status = STATUS_OK;
module->dir = SPI_DIRECTION_IDLE;
module->locked = false;
/*
* Set interrupt handler and register SPI software module struct in
* look-up table
*/
instance_index = _sercom_get_sercom_inst_index(module->hw);
_sercom_set_handler(instance_index, _spi_interrupt_handler);
_sercom_instances[instance_index] = module;
#endif
/* Write configuration to module and return status code */
return _spi_set_config(module, config);
}
/**
* \brief Reads buffer of \c length SPI characters
*
* This function will read a buffer of data from an SPI peripheral by sending
* dummy SPI character if in master mode, or by waiting for data in slave mode.
*
* \note If address matching is enabled for the slave, the first character
* received and placed in the buffer will be the address.
*
* \param[in] module Pointer to the software instance struct
* \param[out] rx_data Data buffer for received data
* \param[in] length Length of data to receive
* \param[in] dummy 8- or 9-bit dummy byte to shift out in master mode
*
* \return Status of the read operation.
* \retval STATUS_OK If the read was completed
* \retval STATUS_ABORTED If transaction was ended by master before
* the entire buffer was transferred
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
* \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
* timeout in slave mode
* \retval STATUS_ERR_DENIED If the receiver is not enabled
* \retval STATUS_ERR_OVERFLOW If the data is overflown
*/
enum status_code spi_read_buffer_wait(
struct spi_module *const module,
uint8_t *rx_data,
uint16_t length,
uint16_t dummy)
{
/* Sanity check arguments */
Assert(module);
Assert(module->hw);
# if SPI_CALLBACK_MODE == true
if (module->status == STATUS_BUSY) {
/* Check if the SPI module is busy with a job */
return STATUS_BUSY;
}
# endif
/* Sanity check arguments */
if (length == 0) {
return STATUS_ERR_INVALID_ARG;
}
if (!(module->receiver_enabled)) {
return STATUS_ERR_DENIED;
}
# if CONF_SPI_SLAVE_ENABLE == true
if ((module->mode == SPI_MODE_SLAVE) && (spi_is_write_complete(module))) {
/* Clear TX complete flag */
_spi_clear_tx_complete_flag(module);
}
# endif
uint16_t rx_pos = 0;
while (length--) {
# if CONF_SPI_MASTER_ENABLE == true
if (module->mode == SPI_MODE_MASTER) {
/* Wait until the module is ready to write a character */
while (!spi_is_ready_to_write(module)) {
}
/* Send dummy SPI character to read in master mode */
spi_write(module, dummy);
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (spi_is_ready_to_read(module)) {
break;
}
}
/* Check if master has ended the transaction */
if (spi_is_write_complete(module)) {
_spi_clear_tx_complete_flag(module);
return STATUS_ABORTED;
}
if (!spi_is_ready_to_read(module)) {
/* Not ready to read data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
# endif
/* Wait until the module is ready to read a character */
while (!spi_is_ready_to_read(module)) {
}
uint16_t received_data = 0;
enum status_code retval = spi_read(module, &received_data);
if (retval != STATUS_OK) {
/* Overflow, abort */
return retval;
}
/* Read value will be at least 8-bits long */
rx_data[rx_pos++] = received_data;
/* If 9-bit data, write next received byte to the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
rx_data[rx_pos++] = (received_data >> 8);
}
}
return STATUS_OK;
}
/**
* \brief Sends and reads a single SPI character
*
* This function will transfer a single SPI character via SPI and return the
* SPI character that is shifted into the shift register.
*
* In master mode the SPI character will be sent immediately and the received
* SPI character will be read as soon as the shifting of the data is
* complete.
*
* In slave mode this function will place the data to be sent into the transmit
* buffer. It will then block until an SPI master has shifted a complete
* SPI character, and the received data is available.
*
* \note The data to be sent might not be sent before the next transfer, as
* loading of the shift register is dependent on SCK.
* \note If address matching is enabled for the slave, the first character
* received and placed in the buffer will be the address.
*
* \param[in] module Pointer to the software instance struct
* \param[in] tx_data SPI character to transmit
* \param[out] rx_data Pointer to store the received SPI character
*
* \return Status of the operation.
* \retval STATUS_OK If the operation was completed
* \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
* timeout in slave mode
* \retval STATUS_ERR_DENIED If the receiver is not enabled
* \retval STATUS_ERR_OVERFLOW If the incoming data is overflown
*/
enum status_code spi_transceive_wait(
struct spi_module *const module,
uint16_t tx_data,
uint16_t *rx_data)
{
/* Sanity check arguments */
Assert(module);
if (!(module->receiver_enabled)) {
return STATUS_ERR_DENIED;
}
# if SPI_CALLBACK_MODE == true
if (module->status == STATUS_BUSY) {
/* Check if the SPI module is busy with a job */
return STATUS_BUSY;
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
uint16_t j;
# endif
enum status_code retval = STATUS_OK;
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (j = 0; j <= SPI_TIMEOUT; j++) {
if (spi_is_ready_to_write(module)) {
break;
} else if (j == SPI_TIMEOUT) {
/* Not ready to write data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
}
# endif
/* Wait until the module is ready to write the character */
while (!spi_is_ready_to_write(module)) {
}
/* Write data */
spi_write(module, tx_data);
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (j = 0; j <= SPI_TIMEOUT; j++) {
if (spi_is_ready_to_read(module)) {
break;
} else if (j == SPI_TIMEOUT) {
/* Not ready to read data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
}
# endif
/* Wait until the module is ready to read the character */
while (!spi_is_ready_to_read(module)) {
}
/* Read data */
retval = spi_read(module, rx_data);
return retval;
}
/**
* \brief Selects slave device
*
* This function will drive the slave select pin of the selected device low or
* high depending on the select Boolean.
* If slave address recognition is enabled, the address will be sent to the
* slave when selecting it.
*
* \param[in] module Pointer to the software module struct
* \param[in] slave Pointer to the attached slave
* \param[in] select Boolean stating if the slave should be selected or
* deselected
*
* \return Status of the operation.
* \retval STATUS_OK If the slave device was selected
* \retval STATUS_ERR_UNSUPPORTED_DEV If the SPI module is operating in slave
* mode
* \retval STATUS_BUSY If the SPI module is not ready to write
* the slave address
*/
enum status_code spi_select_slave(
struct spi_module *const module,
struct spi_slave_inst *const slave,
const bool select)
{
/* Sanity check arguments */
Assert(module);
Assert(module->hw);
Assert(slave);
/* Check that the SPI module is operating in master mode */
if (module->mode != SPI_MODE_MASTER) {
return STATUS_ERR_UNSUPPORTED_DEV;
}
# ifdef FEATURE_SPI_HARDWARE_SLAVE_SELECT
if(!(module->master_slave_select_enable))
# endif
{
if (select) {
/* Check if address recognition is enabled */
if (slave->address_enabled) {
/* Check if the module is ready to write the address */
if (!spi_is_ready_to_write(module)) {
/* Not ready, do not select slave and return */
port_pin_set_output_level(slave->ss_pin, true);
return STATUS_BUSY;
}
/* Drive Slave Select low */
port_pin_set_output_level(slave->ss_pin, false);
/* Write address to slave */
spi_write(module, slave->address);
if (!(module->receiver_enabled)) {
/* Flush contents of shift register shifted back from slave */
while (!spi_is_ready_to_read(module)) {
}
uint16_t flush = 0;
spi_read(module, &flush);
}
} else {
/* Drive Slave Select low */
port_pin_set_output_level(slave->ss_pin, false);
}
} else {
/* Drive Slave Select high */
port_pin_set_output_level(slave->ss_pin, true);
}
}
return STATUS_OK;
}
/**
* \brief Sends a buffer of \c length SPI characters
*
* This function will send a buffer of SPI characters via the SPI
* and discard any data that is received. To both send and receive a buffer of
* data, use the \ref spi_transceive_buffer_wait function.
*
* Note that this function does not handle the _SS (slave select) pin(s) in
* master mode; this must be handled by the user application.
*
* \param[in] module Pointer to the software instance struct
* \param[in] tx_data Pointer to the buffer to transmit
* \param[in] length Number of SPI characters to transfer
*
* \return Status of the write operation.
* \retval STATUS_OK If the write was completed
* \retval STATUS_ABORTED If transaction was ended by master before
* entire buffer was transferred
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
* \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
* timeout in slave mode
*/
enum status_code spi_write_buffer_wait(
struct spi_module *const module,
const uint8_t *tx_data,
uint16_t length)
{
/* Sanity check arguments */
Assert(module);
# if SPI_CALLBACK_MODE == true
if (module->status == STATUS_BUSY) {
/* Check if the SPI module is busy with a job */
return STATUS_BUSY;
}
# endif
if (length == 0) {
return STATUS_ERR_INVALID_ARG;
}
# if CONF_SPI_SLAVE_ENABLE == true
if ((module->mode == SPI_MODE_SLAVE) && (spi_is_write_complete(module))) {
/* Clear TX complete flag */
_spi_clear_tx_complete_flag(module);
}
# endif
uint16_t tx_pos = 0;
uint16_t flush_length = length;
/* Write block */
while (length--) {
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (spi_is_ready_to_write(module)) {
break;
}
}
/* Check if master has ended the transaction */
if (spi_is_write_complete(module)) {
_spi_clear_tx_complete_flag(module);
return STATUS_ABORTED;
}
if (!spi_is_ready_to_write(module)) {
/* Not ready to write data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
# endif
/* Wait until the module is ready to write a character */
while (!spi_is_ready_to_write(module)) {
}
/* Write value will be at least 8-bits long */
uint16_t data_to_send = tx_data[tx_pos++];
/* If 9-bit data, get next byte to send from the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
data_to_send |= (tx_data[tx_pos++] << 8);
}
/* Write the data to send */
spi_write(module, data_to_send);
if (module->receiver_enabled) {
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (length && spi_is_ready_to_write(module)) {
data_to_send = tx_data[tx_pos++];
/* If 9-bit data, get next byte to send from the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
data_to_send |= (tx_data[tx_pos++] << 8);
}
/* Write the data to send */
spi_write(module, data_to_send);
length--;
}
if (spi_is_ready_to_read(module)) {
break;
}
}
/* Check if master has ended the transaction */
if (spi_is_write_complete(module)) {
_spi_clear_tx_complete_flag(module);
return STATUS_ABORTED;
}
if (!spi_is_ready_to_read(module)) {
/* Not ready to read data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
# endif
while (!spi_is_ready_to_read(module)) {
}
/* Flush read buffer */
uint16_t flush;
spi_read(module, &flush);
flush_length--;
}
}
# if CONF_SPI_MASTER_ENABLE == true
if (module->mode == SPI_MODE_MASTER) {
/* Wait for last byte to be transferred */
while (!spi_is_write_complete(module)) {
}
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
if (module->mode == SPI_MODE_SLAVE) {
if (module->receiver_enabled) {
while (flush_length) {
/* Start timeout period for slave */
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (spi_is_ready_to_read(module)) {
break;
}
}
if (!spi_is_ready_to_read(module)) {
/* Not ready to read data within timeout period */
return STATUS_ERR_TIMEOUT;
}
/* Flush read buffer */
uint16_t flush;
spi_read(module, &flush);
flush_length--;
}
}
}
# endif
return STATUS_OK;
}
/**
* \brief Sends and receives a buffer of \c length SPI characters
*
* This function will send and receive a buffer of data via the SPI.
*
* In master mode the SPI characters will be sent immediately and the
* received SPI character will be read as soon as the shifting of the SPI
* character is complete.
*
* In slave mode this function will place the data to be sent into the transmit
* buffer. It will then block until an SPI master has shifted the complete
* buffer and the received data is available.
*
* \param[in] module Pointer to the software instance struct
* \param[in] tx_data Pointer to the buffer to transmit
* \param[out] rx_data Pointer to the buffer where received data will be stored
* \param[in] length Number of SPI characters to transfer
*
* \return Status of the operation.
* \retval STATUS_OK If the operation was completed
* \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were provided
* \retval STATUS_ERR_TIMEOUT If the operation was not completed within the
* timeout in slave mode
* \retval STATUS_ERR_DENIED If the receiver is not enabled
* \retval STATUS_ERR_OVERFLOW If the data is overflown
*/
enum status_code spi_transceive_buffer_wait(
struct spi_module *const module,
uint8_t *tx_data,
uint8_t *rx_data,
uint16_t length)
{
/* Sanity check arguments */
Assert(module);
# if SPI_CALLBACK_MODE == true
if (module->status == STATUS_BUSY) {
/* Check if the SPI module is busy with a job */
return STATUS_BUSY;
}
# endif
/* Sanity check arguments */
if (length == 0) {
return STATUS_ERR_INVALID_ARG;
}
if (!(module->receiver_enabled)) {
return STATUS_ERR_DENIED;
}
# if CONF_SPI_SLAVE_ENABLE == true
if ((module->mode == SPI_MODE_SLAVE) && (spi_is_write_complete(module))) {
/* Clear TX complete flag */
_spi_clear_tx_complete_flag(module);
}
# endif
uint16_t tx_pos = 0;
uint16_t rx_pos = 0;
uint16_t rx_length = length;
/* Send and receive buffer */
while (length--) {
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (spi_is_ready_to_write(module)) {
break;
}
}
/* Check if master has ended the transaction */
if (spi_is_write_complete(module)) {
_spi_clear_tx_complete_flag(module);
return STATUS_ABORTED;
}
if (!spi_is_ready_to_write(module)) {
/* Not ready to write data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
# endif
/* Wait until the module is ready to write a character */
while (!spi_is_ready_to_write(module)) {
}
/* Write value will be at least 8-bits long */
uint16_t data_to_send = tx_data[tx_pos++];
/* If 9-bit data, get next byte to send from the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
data_to_send |= (tx_data[tx_pos++] << 8);
}
/* Write the data to send */
spi_write(module, data_to_send);
# if CONF_SPI_SLAVE_ENABLE == true
/* Start timeout period for slave */
if (module->mode == SPI_MODE_SLAVE) {
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (spi_is_ready_to_write(module)) {
data_to_send = tx_data[tx_pos++];
/* If 9-bit data, get next byte to send from the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
data_to_send |= (tx_data[tx_pos++] << 8);
}
/* Write the data to send */
spi_write(module, data_to_send);
length--;
}
if (spi_is_ready_to_read(module)) {
break;
}
}
/* Check if master has ended the transaction */
if (spi_is_write_complete(module)) {
_spi_clear_tx_complete_flag(module);
return STATUS_ABORTED;
}
if (!spi_is_ready_to_read(module)) {
/* Not ready to read data within timeout period */
return STATUS_ERR_TIMEOUT;
}
}
# endif
/* Wait until the module is ready to read a character */
while (!spi_is_ready_to_read(module)) {
}
enum status_code retval;
uint16_t received_data = 0;
rx_length--;
retval = spi_read(module, &received_data);
if (retval != STATUS_OK) {
/* Overflow, abort */
return retval;
}
/* Read value will be at least 8-bits long */
rx_data[rx_pos++] = received_data;
/* If 9-bit data, write next received byte to the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
rx_data[rx_pos++] = (received_data >> 8);
}
}
# if CONF_SPI_MASTER_ENABLE == true
if (module->mode == SPI_MODE_MASTER) {
/* Wait for last byte to be transferred */
while (!spi_is_write_complete(module)) {
}
}
# endif
# if CONF_SPI_SLAVE_ENABLE == true
if (module->mode == SPI_MODE_SLAVE) {
while (rx_length) {
/* Start timeout period for slave */
for (uint32_t i = 0; i <= SPI_TIMEOUT; i++) {
if (spi_is_ready_to_read(module)) {
break;
}
}
if (!spi_is_ready_to_read(module)) {
/* Not ready to read data within timeout period */
return STATUS_ERR_TIMEOUT;
}
enum status_code retval;
uint16_t received_data = 0;
rx_length--;
retval = spi_read(module, &received_data);
if (retval != STATUS_OK) {
/* Overflow, abort */
return retval;
}
/* Read value will be at least 8-bits long */
rx_data[rx_pos++] = received_data;
/* If 9-bit data, write next received byte to the buffer */
if (module->character_size == SPI_CHARACTER_SIZE_9BIT) {
rx_data[rx_pos++] = (received_data >> 8);
}
}
}
# endif
return STATUS_OK;
}
|