1 /*
2 * Copyright (c) 2015-2017, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== SPI.c ========
34 */
35
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40
41 #include <ti/drivers/dpl/HwiP.h>
42 #include <ti/drivers/SPI.h>
43
44 extern const SPI_Config SPI_config[];
45 extern const uint_least8_t SPI_count;
46
47 /* Default SPI parameters structure */
48 const SPI_Params SPI_defaultParams = {
49 SPI_MODE_BLOCKING, /* transferMode */
50 SPI_WAIT_FOREVER, /* transferTimeout */
51 NULL, /* transferCallbackFxn */
52 SPI_MASTER, /* mode */
53 1000000, /* bitRate */
54 8, /* dataSize */
55 SPI_POL0_PHA0, /* frameFormat */
56 NULL /* custom */
57 };
58
59 static bool isInitialized = false;
60
61 /*
62 * ======== SPI_close ========
63 */
SPI_close(SPI_Handle handle)64 void SPI_close(SPI_Handle handle)
65 {
66 handle->fxnTablePtr->closeFxn(handle);
67 }
68
69 /*
70 * ======== SPI_control ========
71 */
SPI_control(SPI_Handle handle,uint_fast16_t cmd,void * controlArg)72 int_fast16_t SPI_control(SPI_Handle handle, uint_fast16_t cmd, void *controlArg)
73 {
74 return (handle->fxnTablePtr->controlFxn(handle, cmd, controlArg));
75 }
76
77 /*
78 * ======== SPI_init ========
79 */
SPI_init(void)80 void SPI_init(void)
81 {
82 uint_least8_t i;
83 uint_fast8_t key;
84
85 key = HwiP_disable();
86
87 if (!isInitialized) {
88 isInitialized = (bool) true;
89
90 /* Call each driver's init function */
91 for (i = 0; i < SPI_count; i++) {
92 SPI_config[i].fxnTablePtr->initFxn((SPI_Handle)&(SPI_config[i]));
93 }
94 }
95
96 HwiP_restore(key);
97 }
98
99 /*
100 * ======== SPI_open ========
101 */
SPI_open(uint_least8_t index,SPI_Params * params)102 SPI_Handle SPI_open(uint_least8_t index, SPI_Params *params)
103 {
104 SPI_Handle handle = NULL;
105
106 if (isInitialized && (index < SPI_count)) {
107 /* If params are NULL use defaults */
108 if (params == NULL) {
109 params = (SPI_Params *) &SPI_defaultParams;
110 }
111
112 /* Get handle for this driver instance */
113 handle = (SPI_Handle)&(SPI_config[index]);
114 handle = handle->fxnTablePtr->openFxn(handle, params);
115 }
116
117 return (handle);
118 }
119
120 /*
121 * ======== SPI_Params_init ========
122 */
SPI_Params_init(SPI_Params * params)123 void SPI_Params_init(SPI_Params *params)
124 {
125 *params = SPI_defaultParams;
126 }
127
128 /*
129 * ======== SPI_transfer ========
130 */
SPI_transfer(SPI_Handle handle,SPI_Transaction * transaction)131 bool SPI_transfer(SPI_Handle handle, SPI_Transaction *transaction)
132 {
133 return (handle->fxnTablePtr->transferFxn(handle, transaction));
134 }
135
136 /*
137 * ======== SPI_transferCancel ========
138 */
SPI_transferCancel(SPI_Handle handle)139 void SPI_transferCancel(SPI_Handle handle)
140 {
141 handle->fxnTablePtr->transferCancelFxn(handle);
142 }
143