1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 /****** Includes *******/
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "mxc_device.h"
25 #include "mxc_assert.h"
26 #include "mxc_lock.h"
27 #include "mxc_sys.h"
28 #include "spixf.h"
29 #include "spixfc_fifo_reva_regs.h"
30 #include "spixfc_reva_regs.h"
31 #include "spixfm_reva_regs.h"
32 #include "spixf_reva.h"
33 
34 /***** Definitions *****/
35 #define MXC_SPIXF_HEADER_DEASS_SS 1
36 #define MAX_SCLK 0x10
37 #define MXC_SPIXF_HEADER_TX_DIR 1
38 #define NOT_HEADER_DATA 0xF000 // 0xF makes sure it is not a header
39 #define MXC_SPIXF_MAX_BYTE_LEN 32
40 #define MXC_SPIXF_MAX_PAGE_LEN 32
41 #define MXC_SPIXF_NUM_BYTES_PER_PAGE 32
42 
43 /* Bit positions for the 16-BIT Header. */
44 #define MXC_SPIXF_HEADER_DIRECTION_POS 0
45 #define MXC_SPIXF_HEADER_UNITS_POS 2
46 #define MXC_SPIXF_HEADER_SIZE_POS 4
47 #define MXC_SPIXF_HEADER_WIDTH_POS 9
48 #define MXC_SPIXF_HEADER_DEASS_SS_POS 13
49 
50 static int SPIXFC_ReadRXFIFO(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfc_fifo_reva_regs_t *fifo,
51                              uint8_t *data, int len);
52 static void SPIXFC_TransHandler(mxc_spixfc_reva_regs_t *spixfc,
53                                 mxc_spixfc_fifo_reva_regs_t *spixfc_fifo, mxc_spixf_req_t *req);
54 
55 #if defined(SPIXF_RAM)
56 static int MXC_GetLock_SPIXF(uint32_t *lock, uint32_t value);
57 static void MXC_FreeLock_SPIXF(uint32_t *lock);
58 #endif
59 
60 /******* Globals *******/
61 typedef struct {
62     mxc_spixf_req_t *req;
63     int head_rem;
64 } spixf_req_head_t;
65 static spixf_req_head_t states;
66 
67 /****** Functions ******/
68 
69 #if defined(SPIXF_RAM) && IAR_PRAGMAS
70 #pragma section = ".spix_config"
71 #elif defined(SPIXF_RAM)
72 __attribute__((section(".spix_config")))
73 #endif
MXC_SPIXF_RevA_Init(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,uint32_t cmdval,uint32_t frequency)74 int MXC_SPIXF_RevA_Init(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
75                         uint32_t cmdval, uint32_t frequency)
76 {
77     states.req = NULL;
78     states.head_rem = 0;
79     spixfc->gen_ctrl = 0;
80     MXC_SPIXF_RevA_RXFIFOEnable(spixfc);
81     MXC_SPIXF_RevA_TXFIFOEnable(spixfc);
82     MXC_SPIXF_RevA_SCKFeedbackEnable(spixfc, spixfm);
83     MXC_SPIXF_RevA_SetSPIFrequency(spixfc, spixfm, frequency);
84 #if defined(SPIXF_RAM)
85     MXC_SPIXF_RevA_SetSSActiveTime(spixfc, spixfm, MXC_SPIXF_SYS_CLOCKS_2);
86     MXC_SPIXF_RevA_SetSSInactiveTime(spixfc, spixfm, MXC_SPIXF_SYS_CLOCKS_9);
87 #else
88     MXC_SPIXF_RevA_SetSSActiveTime(spixfc, spixfm, MXC_SPIXF_SYS_CLOCKS_0);
89     MXC_SPIXF_RevA_SetSSInactiveTime(spixfc, spixfm, MXC_SPIXF_SYS_CLOCKS_1);
90 #endif
91     MXC_SPIXF_RevA_SetCmdValue(spixfm, cmdval);
92     MXC_SPIXF_RevA_SetCmdWidth(spixfm, MXC_SPIXF_SINGLE_SDIO);
93     MXC_SPIXF_RevA_SetAddrWidth(spixfm, MXC_SPIXF_SINGLE_SDIO);
94     MXC_SPIXF_RevA_SetDataWidth(spixfm, MXC_SPIXF_SINGLE_SDIO);
95     MXC_SPIXF_RevA_Set3ByteAddr(spixfm);
96     MXC_SPIXF_RevA_SetMode(spixfc, spixfm, MXC_SPIXF_MODE_0);
97     MXC_SPIXF_RevA_SetPageSize(spixfc, MXC_SPIXF_32B);
98     MXC_SPIXF_RevA_SetSSPolActiveLow(spixfc, spixfm);
99     return E_NO_ERROR;
100 }
101 
MXC_SPIXF_RevA_Shutdown(mxc_spixfc_reva_regs_t * spixfc)102 void MXC_SPIXF_RevA_Shutdown(mxc_spixfc_reva_regs_t *spixfc)
103 {
104     mxc_spixf_req_t *temp_req;
105     MXC_SPIXF_RevA_DisableInt(spixfc, 0x3F);
106     MXC_SPIXF_RevA_ClearFlags(spixfc, spixfc->int_fl);
107     MXC_SPIXF_RevA_Disable(spixfc);
108     MXC_SPIXF_RevA_TXFIFODisable(spixfc);
109     MXC_SPIXF_RevA_RXFIFODisable(spixfc);
110 
111     // Call all of the pending callbacks for this SPIXFC
112     if (states.req != NULL) {
113         // Save the request
114         temp_req = states.req;
115 
116         // Unlock this SPIXFC
117         MXC_FreeLock((uint32_t *)&states.req);
118 
119         // Callback if not NULL
120         if (temp_req->callback != NULL) {
121             temp_req->callback(temp_req, E_SHUTDOWN);
122         }
123     }
124 }
125 
MXC_SPIXF_RevA_IOCtrl(mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_ds_t sclk_ds,mxc_spixf_ds_t ss_ds,mxc_spixf_ds_t sdio_ds,mxc_spixf_padctrl_t padctrl)126 void MXC_SPIXF_RevA_IOCtrl(mxc_spixfm_reva_regs_t *spixfm, mxc_spixf_ds_t sclk_ds,
127                            mxc_spixf_ds_t ss_ds, mxc_spixf_ds_t sdio_ds,
128                            mxc_spixf_padctrl_t padctrl)
129 {
130     spixfm->io_ctrl = 0;
131 
132     if (sclk_ds) {
133         MXC_SPIXF_RevA_SetIoctrlSCLKDriveHigh(spixfm);
134     } else {
135         MXC_SPIXF_RevA_SetIoctrlSCLKDriveLow(spixfm);
136     }
137 
138     if (ss_ds) {
139         MXC_SPIXF_RevA_SetIoctrlSSDriveHigh(spixfm);
140     } else {
141         MXC_SPIXF_RevA_SetIoctrlSSDriveLow(spixfm);
142     }
143 
144     if (sdio_ds) {
145         MXC_SPIXF_RevA_SetIoctrlSDIODriveHigh(spixfm);
146     }
147 
148     MXC_SPIXF_RevA_SetPuPdCtrl(spixfm, padctrl);
149 }
150 
151 #if defined(SPIXF_RAM) && IAR_PRAGMAS
152 #pragma section = ".spix_config"
153 #elif defined(SPIXF_RAM)
154 __attribute__((section(".spix_config")))
155 #endif
MXC_SPIXF_RevA_Clocks(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,mxc_spixfc_fifo_reva_regs_t * spixfc_fifo,uint32_t len,uint8_t deass)156 int MXC_SPIXF_RevA_Clocks(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
157                           mxc_spixfc_fifo_reva_regs_t *spixfc_fifo, uint32_t len, uint8_t deass)
158 {
159     mxc_spixfc_fifo_reva_regs_t *fifo;
160     uint16_t header = MXC_SPIXF_HEADER_TX_DIR;
161     uint32_t num = len;
162 
163     // Make sure the SPIXFC has been initialized
164     MXC_ASSERT(MXC_SPIXF_RevA_IsEnabled(spixfc));
165 
166     if (len == 0) {
167         return E_NO_ERROR;
168     }
169 
170 #if defined(SPIXF_RAM)
171     // Lock this SPIXFC
172     if (MXC_GetLock_SPIXF((uint32_t *)&states.req, 1) != E_NO_ERROR) {
173         return E_BUSY;
174     }
175 #else
176     // Lock this SPIXFC
177     if (MXC_GetLock((uint32_t *)&states.req, 1) != E_NO_ERROR) {
178         return E_BUSY;
179     }
180 #endif
181 
182     // Wait for any previous data to transmit
183     while (spixfc->fifo_ctrl & MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT) {}
184 
185     // Disable the feedback clock
186     MXC_SPIXF_RevA_SCKFeedbackDisable(spixfc, spixfm);
187 
188     // Get the TX and RX FIFO for this SPIXFC
189     fifo = spixfc_fifo;
190 
191     // Send the headers to transmit the clocks
192     while (len > 32) {
193         fifo->tx_16 = header;
194         fifo->tx_16 = NOT_HEADER_DATA;
195         fifo->tx_16 = NOT_HEADER_DATA;
196         len -= 32;
197     }
198 
199     if (len) {
200         if (len < 32) {
201             header |= (len << MXC_SPIXF_HEADER_SIZE_POS);
202         }
203 
204         header |= (deass << MXC_SPIXF_HEADER_DEASS_SS_POS);
205 
206         fifo->tx_16 = header;
207 
208         if (len > 16) {
209             fifo->tx_16 = NOT_HEADER_DATA;
210         }
211 
212         fifo->tx_16 = NOT_HEADER_DATA;
213     }
214 
215     // Wait for all of the data to transmit
216     while (spixfc->fifo_ctrl & MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT) {}
217 
218     // Enable the feedback clock
219     MXC_SPIXF_RevA_SCKFeedbackEnable(spixfc, spixfm);
220 
221 #if defined(SPIXF_RAM)
222     // Unlock this SPIXFC
223     MXC_FreeLock_SPIXF((uint32_t *)&states.req);
224 #else
225     // Unlock this SPIXFC
226     MXC_FreeLock((uint32_t *)&states.req);
227 #endif
228 
229     return num;
230 }
231 
232 #if defined(SPIXF_RAM) && IAR_PRAGMAS
233 #pragma section = ".spix_config"
234 #elif defined(SPIXF_RAM)
235 __attribute__((section(".spix_config")))
236 #endif
MXC_SPIXF_RevA_Transaction(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfc_fifo_reva_regs_t * spixfc_fifo,mxc_spixf_req_t * req)237 int MXC_SPIXF_RevA_Transaction(mxc_spixfc_reva_regs_t *spixfc,
238                                mxc_spixfc_fifo_reva_regs_t *spixfc_fifo, mxc_spixf_req_t *req)
239 {
240     int remain, temp_read;
241     uint32_t pages, bytes;
242     uint8_t read, write;
243     mxc_spixfc_fifo_reva_regs_t *fifo;
244     uint16_t header;
245 
246     // Check the input parameters
247     MXC_ASSERT(req != NULL);
248     MXC_ASSERT((req->rx_data != NULL) || (req->tx_data != NULL));
249 
250     // Make sure the SPIXFC has been initialized
251     MXC_ASSERT(MXC_SPIXF_RevA_IsEnabled(spixfc));
252 
253     if (req->len == 0) {
254         return E_NO_ERROR;
255     }
256 
257 #if defined(SPIXF_RAM)
258     // Lock this SPIXFC
259     if (MXC_GetLock_SPIXF((uint32_t *)&states.req, 1) != E_NO_ERROR) {
260         return E_BUSY;
261     }
262 #else
263     // Lock this SPIXFC
264     if (MXC_GetLock((uint32_t *)&states.req, 1) != E_NO_ERROR) {
265         return E_BUSY;
266     }
267 #endif
268 
269     // Clear the number of bytes counter
270     req->read_num = 0;
271     req->write_num = 0;
272     states.head_rem = 0;
273 
274     // Figure out if we're reading and/or writing
275     read = (req->rx_data != NULL);
276     write = (req->tx_data != NULL);
277 
278     // Get the TX and RX FIFO for this SPIXFC
279     fifo = spixfc_fifo;
280 
281     remain = req->len;
282 
283     while (remain) {
284         // Set the transaction configuration in the header
285         header = ((write << MXC_SPIXF_HEADER_DIRECTION_POS) |
286                   (read << (MXC_SPIXF_HEADER_DIRECTION_POS + 1)) |
287                   (req->width << MXC_SPIXF_HEADER_WIDTH_POS));
288 
289         if (remain >= MXC_SPIXF_MAX_BYTE_LEN) {
290             // Send a 32 byte header
291             if (remain == MXC_SPIXF_MAX_BYTE_LEN) {
292                 // 0 maps to 32 in the header, ...
293                 header |= ((MXC_SPIXF_HEADER_UNITS_BYTES << MXC_SPIXF_HEADER_UNITS_POS) |
294                            (req->deass << MXC_SPIXF_HEADER_DEASS_SS_POS));
295 
296                 bytes = MXC_SPIXF_MAX_BYTE_LEN;
297 
298             } else {
299                 // Send in increments of 32 byte pages
300                 header |= (MXC_SPIXF_HEADER_UNITS_PAGES << MXC_SPIXF_HEADER_UNITS_POS);
301                 pages = remain / MXC_SPIXF_NUM_BYTES_PER_PAGE;
302 
303                 if (pages >= MXC_SPIXF_MAX_PAGE_LEN) {
304                     // No need to set num pages field in header since 0 maps to MXC_SPIXF_MAX_PAGE_LEN in the header
305                     // There are 32 bytes per page
306                     bytes = MXC_SPIXF_NUM_BYTES_PER_PAGE * MXC_SPIXF_MAX_PAGE_LEN;
307                 } else {
308                     header |= (pages << MXC_SPIXF_HEADER_SIZE_POS);
309                     bytes = pages * MXC_SPIXF_NUM_BYTES_PER_PAGE;
310                 }
311 
312                 // Check if this is the last header we will send
313                 if ((remain - bytes) == 0) {
314                     header |= (req->deass << MXC_SPIXF_HEADER_DEASS_SS_POS);
315                 }
316             }
317 
318             fifo->tx_16 = header;
319 
320             // Save the number of bytes we need to write to the FIFO
321             states.head_rem = bytes;
322             remain -= bytes;
323         } else {
324             // Send final header with the number of bytes remaining and if
325             // we want to de-assert the SS at the end of the transaction
326             header |= ((MXC_SPIXF_HEADER_UNITS_BYTES << MXC_SPIXF_HEADER_UNITS_POS) |
327                        (remain << MXC_SPIXF_HEADER_SIZE_POS) |
328                        (req->deass << MXC_SPIXF_HEADER_DEASS_SS_POS));
329 
330             fifo->tx_16 = header;
331 
332             states.head_rem = remain;
333             remain = 0;
334         }
335 
336         /* *** */
337         while (states.head_rem) {
338             if (write) {
339                 // Wait for there to be room in the TXFIFO
340                 while (((spixfc->fifo_ctrl & MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT) >>
341                         MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT_POS) >
342                        (MXC_CFG_SPIXFC_FIFO_DEPTH - 2)) {}
343 
344                 if (states.head_rem > 1) {
345                     fifo->tx_16 =
346                         ((req->tx_data[req->write_num + 1] << 8) | (req->tx_data[req->write_num]));
347                     req->write_num += 2;
348                 } else {
349                     fifo->tx_16 = (NOT_HEADER_DATA | req->tx_data[req->write_num]);
350                     req->write_num++;
351                 }
352             }
353 
354             if (read) {
355                 // Read two bytes
356                 if (states.head_rem > 1) {
357                     temp_read = 0;
358 
359                     do {
360                         temp_read =
361                             SPIXFC_ReadRXFIFO(spixfc, fifo, &req->rx_data[req->read_num], 1);
362                     } while (temp_read != 1);
363 
364                     req->read_num += 1;
365                 }
366 
367                 do {
368                     temp_read = SPIXFC_ReadRXFIFO(spixfc, fifo, &req->rx_data[req->read_num], 1);
369                 } while (temp_read != 1);
370 
371                 req->read_num += 1;
372             }
373 
374             if (states.head_rem > 1) {
375                 states.head_rem -= 1;
376             }
377 
378             states.head_rem -= 1;
379         }
380     } // end of while(remain)
381 
382 #if defined(SPIXF_RAM)
383     // Unlock this SPIXFC
384     MXC_FreeLock_SPIXF((uint32_t *)&states.req);
385 #else
386     // Unlock this SPIXFC
387     MXC_FreeLock((uint32_t *)&states.req);
388 #endif
389 
390     if (write) {
391         return req->write_num;
392     }
393 
394     return req->read_num;
395 }
396 
MXC_SPIXF_RevA_TransactionAsync(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfc_fifo_reva_regs_t * spixfc_fifo,mxc_spixf_req_t * req)397 int MXC_SPIXF_RevA_TransactionAsync(mxc_spixfc_reva_regs_t *spixfc,
398                                     mxc_spixfc_fifo_reva_regs_t *spixfc_fifo, mxc_spixf_req_t *req)
399 {
400     // Check the input parameters
401     MXC_ASSERT(req != NULL);
402     MXC_ASSERT((req->rx_data != NULL) || (req->tx_data != NULL));
403 
404     // Make sure the SPIXFC has been initialized
405     MXC_ASSERT(MXC_SPIXF_RevA_IsEnabled(spixfc));
406 
407     if (req->len == 0) {
408         return E_NO_ERROR;
409     }
410 
411     // Attempt to register this write request
412     if (MXC_GetLock((uint32_t *)&states.req, (uint32_t)req) != E_NO_ERROR) {
413         return E_BUSY;
414     }
415 
416     // Clear the number of bytes counter
417     req->read_num = 0;
418     req->write_num = 0;
419     states.head_rem = 0;
420 
421     // Start the write
422     SPIXFC_TransHandler(spixfc, spixfc_fifo, req);
423 
424     return E_NO_ERROR;
425 }
426 
MXC_SPIXF_RevA_AbortAsync(mxc_spixfc_reva_regs_t * spixfc,mxc_spixf_req_t * req)427 int MXC_SPIXF_RevA_AbortAsync(mxc_spixfc_reva_regs_t *spixfc, mxc_spixf_req_t *req)
428 {
429     mxc_spixf_req_t *temp_req;
430 
431     // Check the input parameters
432     if (req == NULL) {
433         return E_BAD_PARAM;
434     }
435 
436     // Find the request, set to NULL
437     if (req == states.req) {
438         spixfc->int_en = 0;
439         spixfc->int_fl = spixfc->int_fl;
440 
441         // Save the request
442         temp_req = states.req;
443 
444         // Unlock this SPIXFC
445         MXC_FreeLock((uint32_t *)&states.req);
446 
447         // Callback if not NULL
448         if (temp_req->callback != NULL) {
449             temp_req->callback(temp_req, E_ABORT);
450         }
451 
452         return E_NO_ERROR;
453     }
454 
455     return E_BAD_PARAM;
456 }
457 
MXC_SPIXF_RevA_Handler(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfc_fifo_reva_regs_t * spixfc_fifo)458 void MXC_SPIXF_RevA_Handler(mxc_spixfc_reva_regs_t *spixfc,
459                             mxc_spixfc_fifo_reva_regs_t *spixfc_fifo)
460 {
461     uint32_t flags;
462 
463     // Clear the interrupt flags
464     spixfc->int_en = 0;
465     flags = spixfc->int_fl;
466     spixfc->int_fl = flags;
467 
468     // Figure out if this SPIXFC has an active request
469     if ((states.req != NULL) && (flags)) {
470         SPIXFC_TransHandler(spixfc, spixfc_fifo, states.req);
471     }
472 }
473 
474 #if defined(SPIXF_RAM) && IAR_PRAGMAS
475 #pragma section = ".spix_config"
476 #elif defined(SPIXF_RAM)
477 __attribute__((section(".spix_config")))
478 #endif
SPIXFC_ReadRXFIFO(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfc_fifo_reva_regs_t * fifo,uint8_t * data,int len)479 __attribute__((noinline)) static int SPIXFC_ReadRXFIFO(mxc_spixfc_reva_regs_t *spixfc,
480                                                        mxc_spixfc_fifo_reva_regs_t *fifo,
481                                                        uint8_t *data, int len)
482 {
483     int num = 0;
484 
485     // Get data from the RXFIFO
486     while ((spixfc->fifo_ctrl & MXC_F_SPIXFC_REVA_FIFO_CTRL_RX_FIFO_CNT) && (len - num)) {
487         // Save data from the RXFIFO
488         data[num] = fifo->rx_8;
489         num++;
490     }
491 
492     return num;
493 }
494 
SPIXFC_TransHandler(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfc_fifo_reva_regs_t * spixfc_fifo,mxc_spixf_req_t * req)495 static void SPIXFC_TransHandler(mxc_spixfc_reva_regs_t *spixfc,
496                                 mxc_spixfc_fifo_reva_regs_t *spixfc_fifo, mxc_spixf_req_t *req)
497 {
498     int remain;
499     uint8_t read, write;
500     uint16_t header;
501     uint32_t pages, bytes, inten;
502     unsigned int bytes_read;
503     mxc_spixfc_fifo_reva_regs_t *fifo;
504     mxc_spixf_req_t *temp_req;
505 
506     inten = 0;
507 
508     // Get the FIFOS for this spi
509     fifo = spixfc_fifo;
510 
511     // Figure out if we're reading
512     if (req->rx_data != NULL) {
513         read = 1;
514     } else {
515         read = 0;
516     }
517 
518     // Figure out if we're writing
519     if (req->tx_data != NULL) {
520         write = 1;
521     } else {
522         write = 0;
523     }
524 
525     // Read byte from the FIFO if we are reading
526     if (read) {
527         // Read all of the data in the RXFIFO, or until we don't need anymore
528         bytes_read = SPIXFC_ReadRXFIFO(spixfc, fifo, &req->rx_data[req->read_num],
529                                        (req->len - req->read_num));
530 
531         req->read_num += bytes_read;
532 
533         // Adjust head_rem if we are only reading
534         if (!write && (states.head_rem > 0)) {
535             states.head_rem -= bytes_read;
536         }
537 
538         // Figure out how many byte we have left to read
539         if (states.head_rem > 0) {
540             remain = states.head_rem;
541         } else {
542             remain = req->len - req->read_num;
543         }
544 
545         if (remain) { // setting up int levels and RX interrupt flag...
546             // Set the RX interrupts
547             if (remain > MXC_CFG_SPIXFC_FIFO_DEPTH) { // FIFO Almost FULL level = 16-2 = 14;
548                 spixfc->fifo_ctrl =
549                     ((spixfc->fifo_ctrl & ~MXC_F_SPIXFC_REVA_FIFO_CTRL_RX_FIFO_AF_LVL) |
550                      ((MXC_CFG_SPIXFC_FIFO_DEPTH - 2)
551                       << MXC_F_SPIXFC_REVA_FIFO_CTRL_RX_FIFO_AF_LVL_POS));
552 
553             } else { // FIFO Almost Full level = remain-1;
554                 spixfc->fifo_ctrl =
555                     ((spixfc->fifo_ctrl & ~MXC_F_SPIXFC_REVA_FIFO_CTRL_RX_FIFO_AF_LVL) |
556                      ((remain - 1) << MXC_F_SPIXFC_REVA_FIFO_CTRL_RX_FIFO_AF_LVL_POS));
557             }
558 
559             inten |= MXC_F_SPIXFC_REVA_INT_EN_RX_FIFO_AF;
560         }
561     }
562 
563     // Figure out how many bytes we have left to send headers for
564     if (write) {
565         remain = req->len - req->write_num;
566     } else {
567         remain = req->len - req->read_num;
568     }
569 
570     // See if we need to send a new header
571     if (states.head_rem <= 0 && remain) {
572         // Set the transaction configuration in the header
573         header = ((write << MXC_SPIXF_HEADER_DIRECTION_POS) |
574                   (read << (MXC_SPIXF_HEADER_DIRECTION_POS + 1)) |
575                   (req->width << MXC_SPIXF_HEADER_WIDTH_POS));
576 
577         if (remain >= MXC_SPIXF_MAX_BYTE_LEN) {
578             // Send a 32 byte header
579             if (remain == MXC_SPIXF_MAX_BYTE_LEN) {
580                 header |= ((MXC_SPIXF_HEADER_UNITS_BYTES << MXC_SPIXF_HEADER_UNITS_POS) |
581                            (req->deass << MXC_SPIXF_HEADER_DEASS_SS_POS));
582                 // Save the number of bytes we need to write to the FIFO
583                 bytes = MXC_SPIXF_MAX_BYTE_LEN;
584             } else {
585                 // Send in increments of 32 byte pages
586                 header |= (MXC_SPIXF_HEADER_UNITS_PAGES << MXC_SPIXF_HEADER_UNITS_POS);
587                 pages = remain / MXC_SPIXF_NUM_BYTES_PER_PAGE;
588 
589                 if (pages >= MXC_SPIXF_MAX_PAGE_LEN) {
590                     // 0 maps to 32 in the header
591                     bytes = MXC_SPIXF_NUM_BYTES_PER_PAGE * MXC_SPIXF_MAX_PAGE_LEN;
592                 } else {
593                     header |= (pages << MXC_SPIXF_HEADER_SIZE_POS);
594                     bytes = pages * MXC_SPIXF_NUM_BYTES_PER_PAGE;
595                 }
596 
597                 // Check if this is the last header we will send
598                 if ((remain - bytes) == 0) {
599                     header |= (req->deass << MXC_SPIXF_HEADER_DEASS_SS_POS);
600                 }
601             }
602 
603             fifo->tx_16 = header;
604 
605             // Save the number of bytes we need to write to the FIFO
606             states.head_rem = bytes;
607 
608         } else {
609             // Send final header with the number of bytes remaining and if
610             // we want to de-assert the SS at the end of the transaction
611             header |= ((MXC_SPIXF_HEADER_UNITS_BYTES << MXC_SPIXF_HEADER_UNITS_POS) |
612                        (remain << MXC_SPIXF_HEADER_SIZE_POS) |
613                        (req->deass << MXC_SPIXF_HEADER_DEASS_SS_POS));
614             fifo->tx_16 = header;
615             states.head_rem = remain;
616         }
617     }
618 
619     // Put data into the FIFO if we are writing
620     remain = req->len - req->write_num;
621 
622     if (write && states.head_rem) { // from above... the num of bytes...
623         // Fill the FIFO
624         while ((((spixfc->fifo_ctrl & MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT) >>
625                  MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT_POS) < (MXC_CFG_SPIXFC_FIFO_DEPTH - 2)) &&
626                (states.head_rem)) {
627             if (states.head_rem > 1) {
628                 // Write 2 byte at a time
629                 fifo->tx_16 =
630                     ((req->tx_data[req->write_num + 1] << 8) | (req->tx_data[req->write_num]));
631 
632                 req->write_num += 2;
633                 states.head_rem -= 2;
634             } else {
635                 // Write the last byte
636                 fifo->tx_16 = (NOT_HEADER_DATA | req->tx_data[req->write_num]);
637 
638                 req->write_num += 1;
639                 states.head_rem -= 1;
640             }
641         }
642 
643         remain = req->len - req->write_num;
644 
645         // Set the TX interrupts
646         if (remain) {
647             // Set the TX FIFO almost empty interrupt if we have to refill
648             spixfc->fifo_ctrl = ((spixfc->fifo_ctrl & ~MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_AE_LVL) |
649                                  ((MXC_CFG_SPIXFC_FIFO_DEPTH - 2)
650                                   << MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_AE_LVL_POS));
651 
652             inten |= MXC_F_SPIXFC_REVA_INT_EN_TX_FIFO_AE;
653         } else if (req->wait_tx) {
654             // Set the TX Ready interrupt if we're waiting for the FIFO to empty
655             inten |= MXC_F_SPIXFC_REVA_INT_FL_TX_READY;
656             req->wait_tx = 0;
657 
658             // Wait for transaction to completely finish by just returning and waiting for next interrupt.
659             spixfc->int_en = inten;
660             return;
661         }
662     }
663 
664     // Check to see if we've finished reading and writing
665     if (((read && (req->read_num == req->len)) || !read) &&
666         (((req->write_num == req->len) && !req->wait_tx) || !write)) {
667         // clear interrupts
668         spixfc->int_fl = spixfc->int_fl;
669 
670         // Save the request
671         temp_req = states.req;
672 
673         // Unlock this SPIXFC
674         MXC_FreeLock((uint32_t *)&states.req);
675 
676         // Callback if not NULL
677         if (temp_req->callback != NULL) {
678             temp_req->callback(temp_req, E_NO_ERROR);
679         }
680     }
681 
682     // Enable the SPIXFC interrupts
683     spixfc->int_en = inten;
684 }
685 
MXC_SPIXF_RevA_ReadyForSleep(mxc_spixfc_reva_regs_t * spixfc)686 int MXC_SPIXF_RevA_ReadyForSleep(mxc_spixfc_reva_regs_t *spixfc)
687 {
688     // Check to see if there are any ongoing transactions
689     if ((states.req == NULL) && !(spixfc->fifo_ctrl & MXC_F_SPIXFC_REVA_FIFO_CTRL_TX_FIFO_CNT)) {
690         // Disable interrupts, clear flags
691         spixfc->int_en = 0;
692         spixfc->int_fl = spixfc->int_fl;
693 
694         return E_NO_ERROR;
695     }
696 
697     return E_BUSY;
698 }
699 
MXC_SPIXF_RevA_EnableInt(mxc_spixfc_reva_regs_t * spixfc,uint32_t mask)700 int MXC_SPIXF_RevA_EnableInt(mxc_spixfc_reva_regs_t *spixfc, uint32_t mask)
701 {
702     mask &= (MXC_F_SPIXFC_REVA_INT_EN_TX_STALLED | MXC_F_SPIXFC_REVA_INT_EN_RX_STALLED |
703              MXC_F_SPIXFC_REVA_INT_EN_TX_READY | MXC_F_SPIXFC_REVA_INT_EN_RX_DONE |
704              MXC_F_SPIXFC_REVA_INT_EN_TX_FIFO_AE | MXC_F_SPIXFC_REVA_INT_EN_RX_FIFO_AF);
705 
706     if (!mask) {
707         /* No bits set? Wasn't something we can enable. */
708         return E_BAD_PARAM;
709     }
710 
711     spixfc->int_en |= mask;
712     return E_NO_ERROR;
713 }
714 
MXC_SPIXF_RevA_DisableInt(mxc_spixfc_reva_regs_t * spixfc,uint32_t mask)715 int MXC_SPIXF_RevA_DisableInt(mxc_spixfc_reva_regs_t *spixfc, uint32_t mask)
716 {
717     mask &= (MXC_F_SPIXFC_REVA_INT_EN_TX_STALLED | MXC_F_SPIXFC_REVA_INT_EN_RX_STALLED |
718              MXC_F_SPIXFC_REVA_INT_EN_TX_READY | MXC_F_SPIXFC_REVA_INT_EN_RX_DONE |
719              MXC_F_SPIXFC_REVA_INT_EN_TX_FIFO_AE | MXC_F_SPIXFC_REVA_INT_EN_RX_FIFO_AF);
720 
721     if (!mask) {
722         /* No bits set? Wasn't something we can disable. */
723         return E_BAD_PARAM;
724     }
725 
726     spixfc->int_en &= (~mask);
727     return E_NO_ERROR;
728 }
729 
MXC_SPIXF_RevA_ClearFlags(mxc_spixfc_reva_regs_t * spixfc,uint32_t flags)730 int MXC_SPIXF_RevA_ClearFlags(mxc_spixfc_reva_regs_t *spixfc, uint32_t flags)
731 {
732     flags &= (MXC_F_SPIXFC_REVA_INT_FL_TX_STALLED | MXC_F_SPIXFC_REVA_INT_FL_RX_STALLED |
733               MXC_F_SPIXFC_REVA_INT_FL_TX_READY | MXC_F_SPIXFC_REVA_INT_FL_RX_DONE |
734               MXC_F_SPIXFC_REVA_INT_FL_TX_FIFO_AE | MXC_F_SPIXFC_REVA_INT_FL_RX_FIFO_AF);
735 
736     if (!flags) {
737         /* No bits set? Wasn't a flag we can clear. */
738         return E_BAD_PARAM;
739     }
740 
741     spixfc->int_fl = flags;
742     return E_NO_ERROR;
743 }
744 
MXC_SPIXF_RevA_GetFlags(mxc_spixfc_reva_regs_t * spixfc)745 int MXC_SPIXF_RevA_GetFlags(mxc_spixfc_reva_regs_t *spixfc)
746 {
747     return spixfc->int_fl;
748 }
749 
750 /* ************************************************ */
751 //Low level
752 /* ************************************************ */
753 
754 #if defined(SPIXF_RAM) && IAR_PRAGMAS
755 #pragma section = ".spix_config"
756 #elif defined(SPIXF_RAM)
757 __attribute__((section(".spix_config")))
758 #endif
MXC_SPIXF_RevA_SetMode(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_mode_t mode)759 int MXC_SPIXF_RevA_SetMode(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
760                            mxc_spixf_mode_t mode)
761 {
762     MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_MODE, mode << MXC_F_SPIXFM_REVA_CFG_MODE_POS);
763     MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_MODE, mode << MXC_F_SPIXFC_REVA_CFG_MODE_POS);
764     return E_NO_ERROR;
765 }
766 
MXC_SPIXF_RevA_GetMode(mxc_spixfc_reva_regs_t * spixfc)767 mxc_spixf_mode_t MXC_SPIXF_RevA_GetMode(mxc_spixfc_reva_regs_t *spixfc)
768 {
769     return (mxc_spixf_mode_t)((spixfc->cfg & MXC_F_SPIXFC_REVA_CFG_MODE) >>
770                               MXC_F_SPIXFC_REVA_CFG_MODE_POS);
771 }
772 
MXC_SPIXF_RevA_SetSSPolActiveHigh(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm)773 int MXC_SPIXF_RevA_SetSSPolActiveHigh(mxc_spixfc_reva_regs_t *spixfc,
774                                       mxc_spixfm_reva_regs_t *spixfm)
775 {
776     spixfm->cfg &= ~MXC_F_SPIXFM_REVA_CFG_SSPOL;
777     spixfc->ss_pol |= MXC_F_SPIXFC_REVA_SS_POL_SS_POLARITY;
778     return E_NO_ERROR;
779 }
780 
781 #if defined(SPIXF_RAM) && IAR_PRAGMAS
782 #pragma section = ".spix_config"
783 #elif defined(SPIXF_RAM)
784 __attribute__((section(".spix_config")))
785 #endif
MXC_SPIXF_RevA_SetSSPolActiveLow(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm)786 int MXC_SPIXF_RevA_SetSSPolActiveLow(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm)
787 {
788     spixfm->cfg |= MXC_F_SPIXFM_REVA_CFG_SSPOL;
789     spixfc->ss_pol &= (~MXC_F_SPIXFC_REVA_SS_POL_SS_POLARITY);
790     return E_NO_ERROR;
791 }
792 
MXC_SPIXF_RevA_GetSSPolarity(mxc_spixfc_reva_regs_t * spixfc)793 int MXC_SPIXF_RevA_GetSSPolarity(mxc_spixfc_reva_regs_t *spixfc)
794 {
795     return !!(spixfc->ss_pol & MXC_F_SPIXFC_REVA_SS_POL_SS_POLARITY);
796 }
797 
798 #if defined(SPIXF_RAM) && IAR_PRAGMAS
799 #pragma section = ".spix_config"
800 #elif defined(SPIXF_RAM)
801 __attribute__((section(".spix_config")))
802 #endif
MXC_SPIXF_RevA_SetSPIFrequency(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,unsigned int hz)803 int MXC_SPIXF_RevA_SetSPIFrequency(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
804                                    unsigned int hz)
805 {
806     uint32_t clocks, hi_clk, lo_clk;
807 
808     // Check if frequency is too high
809     if (hz > SystemCoreClock) {
810         return E_BAD_PARAM;
811     }
812 
813     // Make sure that we can generate this frequency
814     clocks = SystemCoreClock / hz;
815     hi_clk = clocks / 2;
816     lo_clk = clocks / 2;
817 
818     if (clocks % 2) {
819         hi_clk += 1;
820     }
821 
822     if (spixfm) {
823         if (hi_clk == 0) {
824             MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_LO_CLK,
825                          1 << MXC_F_SPIXFM_REVA_CFG_LO_CLK_POS);
826             MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_HI_CLK,
827                          1 << MXC_F_SPIXFM_REVA_CFG_HI_CLK_POS);
828         } else if (hi_clk > 15) {
829             spixfm->cfg |= MXC_F_SPIXFM_REVA_CFG_LO_CLK;
830             spixfm->cfg |= MXC_F_SPIXFM_REVA_CFG_HI_CLK;
831         } else {
832             MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_LO_CLK,
833                          lo_clk << MXC_F_SPIXFM_REVA_CFG_LO_CLK_POS);
834             MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_HI_CLK,
835                          hi_clk << MXC_F_SPIXFM_REVA_CFG_HI_CLK_POS);
836         }
837     }
838 
839     if (spixfc) {
840         if (hi_clk == 0) {
841             MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_LO_CLK,
842                          1 << MXC_F_SPIXFC_REVA_CFG_LO_CLK_POS);
843             MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_HI_CLK,
844                          1 << MXC_F_SPIXFC_REVA_CFG_HI_CLK_POS);
845         } else if (hi_clk > 15) {
846             spixfc->cfg |= MXC_F_SPIXFC_REVA_CFG_LO_CLK;
847             spixfc->cfg |= MXC_F_SPIXFC_REVA_CFG_HI_CLK;
848         } else {
849             MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_LO_CLK,
850                          lo_clk << MXC_F_SPIXFC_REVA_CFG_LO_CLK_POS);
851             MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_HI_CLK,
852                          hi_clk << MXC_F_SPIXFC_REVA_CFG_HI_CLK_POS);
853         }
854     }
855 
856     if (spixfc) {
857         return MXC_SPIXF_RevA_GetSPIFrequencyWrite(spixfc);
858     } else if (spixfm) {
859         return MXC_SPIXF_RevA_GetSPIFrequency(spixfm);
860     } else {
861         return E_BAD_PARAM;
862     }
863 }
864 
865 #if defined(SPIXF_RAM) && IAR_PRAGMAS
866 #pragma section = ".spix_config"
867 #elif defined(SPIXF_RAM)
868 __attribute__((section(".spix_config")))
869 #endif
MXC_SPIXF_RevA_GetSPIFrequency(mxc_spixfm_reva_regs_t * spixfm)870 uint32_t MXC_SPIXF_RevA_GetSPIFrequency(mxc_spixfm_reva_regs_t *spixfm)
871 {
872     uint32_t clocks;
873     clocks = ((spixfm->cfg & MXC_F_SPIXFM_REVA_CFG_HI_CLK) >> MXC_F_SPIXFM_REVA_CFG_HI_CLK_POS);
874 
875     // Avoid divide by zero
876     if (clocks == 0) {
877         clocks = 0x10;
878     }
879 
880     return SystemCoreClock / (2 * clocks);
881 }
882 
883 #if defined(SPIXF_RAM) && IAR_PRAGMAS
884 #pragma section = ".spix_config"
885 #elif defined(SPIXF_RAM)
886 __attribute__((section(".spix_config")))
887 #endif
MXC_SPIXF_RevA_GetSPIFrequencyWrite(mxc_spixfc_reva_regs_t * spixfc)888 uint32_t MXC_SPIXF_RevA_GetSPIFrequencyWrite(mxc_spixfc_reva_regs_t *spixfc)
889 {
890     uint32_t clocks;
891     clocks = ((spixfc->cfg & MXC_F_SPIXFC_REVA_CFG_HI_CLK) >> MXC_F_SPIXFC_REVA_CFG_HI_CLK_POS);
892 
893     // Avoid divide by zero
894     if (clocks == 0) {
895         clocks = 0x10;
896     }
897 
898     return PeripheralClock / (2 * clocks);
899 }
900 
901 #if defined(SPIXF_RAM) && IAR_PRAGMAS
902 #pragma section = ".spix_config"
903 #elif defined(SPIXF_RAM)
904 __attribute__((section(".spix_config")))
905 #endif
MXC_SPIXF_RevA_SetSSActiveTime(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_ssact_t ssact)906 int MXC_SPIXF_RevA_SetSSActiveTime(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
907                                    mxc_spixf_ssact_t ssact)
908 {
909     MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_SSACT,
910                  ssact << MXC_F_SPIXFM_REVA_CFG_SSACT_POS);
911     MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_SSACT,
912                  ssact << MXC_F_SPIXFC_REVA_CFG_SSACT_POS);
913     return E_NO_ERROR;
914 }
915 
MXC_SPIXF_RevA_GetSSActiveTime(mxc_spixfc_reva_regs_t * spixfc)916 mxc_spixf_ssact_t MXC_SPIXF_RevA_GetSSActiveTime(mxc_spixfc_reva_regs_t *spixfc)
917 {
918     return (mxc_spixf_ssact_t)((spixfc->cfg & MXC_F_SPIXFC_REVA_CFG_SSACT) >>
919                                MXC_F_SPIXFC_REVA_CFG_SSACT_POS);
920 }
921 
922 #if defined(SPIXF_RAM) && IAR_PRAGMAS
923 #pragma section = ".spix_config"
924 #elif defined(SPIXF_RAM)
925 __attribute__((section(".spix_config")))
926 #endif
MXC_SPIXF_RevA_SetSSInactiveTime(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_ssiact_t ssiact)927 int MXC_SPIXF_RevA_SetSSInactiveTime(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
928                                      mxc_spixf_ssiact_t ssiact)
929 {
930     MXC_SETFIELD(spixfm->cfg, MXC_F_SPIXFM_REVA_CFG_SSIACT,
931                  ssiact << MXC_F_SPIXFM_REVA_CFG_SSIACT_POS);
932     MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_SSIACT,
933                  ssiact << MXC_F_SPIXFC_REVA_CFG_SSIACT_POS);
934     return E_NO_ERROR;
935 }
936 
MXC_SPIXF_RevA_GetSSInactiveTime(mxc_spixfc_reva_regs_t * spixfc)937 mxc_spixf_ssiact_t MXC_SPIXF_RevA_GetSSInactiveTime(mxc_spixfc_reva_regs_t *spixfc)
938 {
939     return (mxc_spixf_ssiact_t)((spixfc->cfg & MXC_F_SPIXFC_REVA_CFG_SSIACT) >>
940                                 MXC_F_SPIXFC_REVA_CFG_SSIACT_POS);
941 }
942 
943 #if defined(SPIXF_RAM) && IAR_PRAGMAS
944 #pragma section = ".spix_config"
945 #elif defined(SPIXF_RAM)
946 __attribute__((section(".spix_config")))
947 #endif
MXC_SPIXF_RevA_SetCmdWidth(mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_spiwidth_t width)948 int MXC_SPIXF_RevA_SetCmdWidth(mxc_spixfm_reva_regs_t *spixfm, mxc_spixf_spiwidth_t width)
949 {
950     MXC_ASSERT(width < MXC_SPIXF_INVALID);
951 
952     MXC_SETFIELD(spixfm->fetch_ctrl, MXC_F_SPIXFM_REVA_FETCH_CTRL_CMD_WIDTH,
953                  width << MXC_F_SPIXFM_REVA_FETCH_CTRL_CMD_WIDTH_POS);
954     return E_NO_ERROR;
955 }
956 
MXC_SPIXF_RevA_GetCmdWidth(mxc_spixfm_reva_regs_t * spixfm)957 mxc_spixf_spiwidth_t MXC_SPIXF_RevA_GetCmdWidth(mxc_spixfm_reva_regs_t *spixfm)
958 {
959     return (mxc_spixf_spiwidth_t)((spixfm->fetch_ctrl & MXC_F_SPIXFM_REVA_FETCH_CTRL_CMD_WIDTH) >>
960                                   MXC_F_SPIXFM_REVA_FETCH_CTRL_CMD_WIDTH_POS);
961 }
962 
963 #if defined(SPIXF_RAM) && IAR_PRAGMAS
964 #pragma section = ".spix_config"
965 #elif defined(SPIXF_RAM)
966 __attribute__((section(".spix_config")))
967 #endif
MXC_SPIXF_RevA_SetAddrWidth(mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_spiwidth_t width)968 int MXC_SPIXF_RevA_SetAddrWidth(mxc_spixfm_reva_regs_t *spixfm, mxc_spixf_spiwidth_t width)
969 {
970     MXC_ASSERT(width < MXC_SPIXF_INVALID);
971 
972     MXC_SETFIELD(spixfm->fetch_ctrl, MXC_F_SPIXFM_REVA_FETCH_CTRL_ADDR_WIDTH,
973                  width << MXC_F_SPIXFM_REVA_FETCH_CTRL_ADDR_WIDTH_POS);
974     return E_NO_ERROR;
975 }
976 
MXC_SPIXF_RevA_GetAddrWidth(mxc_spixfm_reva_regs_t * spixfm)977 mxc_spixf_spiwidth_t MXC_SPIXF_RevA_GetAddrWidth(mxc_spixfm_reva_regs_t *spixfm)
978 {
979     return (mxc_spixf_spiwidth_t)((spixfm->fetch_ctrl & MXC_F_SPIXFM_REVA_FETCH_CTRL_ADDR_WIDTH) >>
980                                   MXC_F_SPIXFM_REVA_FETCH_CTRL_ADDR_WIDTH_POS);
981 }
982 
983 #if defined(SPIXF_RAM) && IAR_PRAGMAS
984 #pragma section = ".spix_config"
985 #elif defined(SPIXF_RAM)
986 __attribute__((section(".spix_config")))
987 #endif
MXC_SPIXF_RevA_SetDataWidth(mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_spiwidth_t width)988 int MXC_SPIXF_RevA_SetDataWidth(mxc_spixfm_reva_regs_t *spixfm, mxc_spixf_spiwidth_t width)
989 {
990     MXC_ASSERT(width < MXC_SPIXF_INVALID);
991 
992     MXC_SETFIELD(spixfm->fetch_ctrl, MXC_F_SPIXFM_REVA_FETCH_CTRL_DATA_WIDTH,
993                  width << MXC_F_SPIXFM_REVA_FETCH_CTRL_DATA_WIDTH_POS);
994     return E_NO_ERROR;
995 }
996 
MXC_SPIXF_RevA_GetDataWidth(mxc_spixfm_reva_regs_t * spixfm)997 mxc_spixf_spiwidth_t MXC_SPIXF_RevA_GetDataWidth(mxc_spixfm_reva_regs_t *spixfm)
998 {
999     return (mxc_spixf_spiwidth_t)((spixfm->fetch_ctrl & MXC_F_SPIXFM_REVA_FETCH_CTRL_DATA_WIDTH) >>
1000                                   MXC_F_SPIXFM_REVA_FETCH_CTRL_DATA_WIDTH_POS);
1001 }
1002 
MXC_SPIXF_RevA_Set4ByteAddr(mxc_spixfm_reva_regs_t * spixfm)1003 int MXC_SPIXF_RevA_Set4ByteAddr(mxc_spixfm_reva_regs_t *spixfm)
1004 {
1005     spixfm->fetch_ctrl |= MXC_F_SPIXFM_REVA_FETCH_CTRL_FOUR_BYTE_ADDR;
1006     return E_NO_ERROR;
1007 }
1008 
1009 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1010 #pragma section = ".spix_config"
1011 #elif defined(SPIXF_RAM)
1012 __attribute__((section(".spix_config")))
1013 #endif
MXC_SPIXF_RevA_Set3ByteAddr(mxc_spixfm_reva_regs_t * spixfm)1014 int MXC_SPIXF_RevA_Set3ByteAddr(mxc_spixfm_reva_regs_t *spixfm)
1015 {
1016     spixfm->fetch_ctrl &= (~MXC_F_SPIXFM_REVA_FETCH_CTRL_FOUR_BYTE_ADDR);
1017     return E_NO_ERROR;
1018 }
1019 
MXC_SPIXF_RevA_GetBytesPerAddr(mxc_spixfm_reva_regs_t * spixfm)1020 unsigned int MXC_SPIXF_RevA_GetBytesPerAddr(mxc_spixfm_reva_regs_t *spixfm)
1021 {
1022     if (!!(spixfm->fetch_ctrl & MXC_F_SPIXFM_REVA_FETCH_CTRL_FOUR_BYTE_ADDR)) {
1023         return 4;
1024     } else {
1025         return 3;
1026     }
1027 }
1028 
1029 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1030 #pragma section = ".spix_config"
1031 #elif defined(SPIXF_RAM)
1032 __attribute__((section(".spix_config")))
1033 #endif
MXC_SPIXF_RevA_SetModeClk(mxc_spixfm_reva_regs_t * spixfm,uint8_t mdclk)1034 int MXC_SPIXF_RevA_SetModeClk(mxc_spixfm_reva_regs_t *spixfm, uint8_t mdclk)
1035 {
1036     MXC_SETFIELD(spixfm->mode_ctrl, MXC_F_SPIXFM_REVA_MODE_CTRL_MDCLK, mdclk);
1037     return E_NO_ERROR;
1038 }
1039 
MXC_SPIXF_RevA_GetModeClk(mxc_spixfm_reva_regs_t * spixfm)1040 uint8_t MXC_SPIXF_RevA_GetModeClk(mxc_spixfm_reva_regs_t *spixfm)
1041 {
1042     return ((spixfm->mode_ctrl & MXC_F_SPIXFM_REVA_MODE_CTRL_MDCLK) >>
1043             MXC_F_SPIXFM_REVA_MODE_CTRL_MDCLK_POS);
1044 }
1045 
MXC_SPIXF_RevA_SetCmdModeEveryTrans(mxc_spixfm_reva_regs_t * spixfm)1046 int MXC_SPIXF_RevA_SetCmdModeEveryTrans(mxc_spixfm_reva_regs_t *spixfm)
1047 {
1048     spixfm->mode_ctrl &= ~(MXC_F_SPIXFM_REVA_MODE_CTRL_NO_CMD);
1049     return E_NO_ERROR;
1050 }
1051 
MXC_SPIXF_RevA_SetCmdModeFirstTrans(mxc_spixfm_reva_regs_t * spixfm)1052 int MXC_SPIXF_RevA_SetCmdModeFirstTrans(mxc_spixfm_reva_regs_t *spixfm)
1053 {
1054     spixfm->mode_ctrl |= MXC_F_SPIXFM_REVA_MODE_CTRL_NO_CMD;
1055     return E_NO_ERROR;
1056 }
1057 
MXC_SPIXF_RevA_GetCmdMode(mxc_spixfm_reva_regs_t * spixfm)1058 mxc_spixf_cmd_t MXC_SPIXF_RevA_GetCmdMode(mxc_spixfm_reva_regs_t *spixfm)
1059 {
1060     if (!!(spixfm->mode_ctrl & MXC_F_SPIXFM_REVA_MODE_CTRL_NO_CMD)) {
1061         return MXC_SPIXF_CMD_FIRST_TRANS;
1062     }
1063 
1064     return MXC_SPIXF_CMD_EVERY_TRANS;
1065 }
1066 
MXC_SPIXF_RevA_BBDataOutputEnable(mxc_spixfc_reva_regs_t * spixfc,uint8_t mask)1067 int MXC_SPIXF_RevA_BBDataOutputEnable(mxc_spixfc_reva_regs_t *spixfc, uint8_t mask)
1068 {
1069     spixfc->gen_ctrl |= (mask << MXC_F_SPIXFC_REVA_GEN_CTRL_BB_DATA_OUT_EN_POS);
1070     return E_NO_ERROR;
1071 }
1072 
MXC_SPIXF_RevA_BBDataOutputDisable(mxc_spixfc_reva_regs_t * spixfc,uint8_t mask)1073 int MXC_SPIXF_RevA_BBDataOutputDisable(mxc_spixfc_reva_regs_t *spixfc, uint8_t mask)
1074 {
1075     spixfc->gen_ctrl &= ~(mask << MXC_F_SPIXFC_REVA_GEN_CTRL_BB_DATA_OUT_EN_POS);
1076     return E_NO_ERROR;
1077 }
1078 
MXC_SPIXF_RevA_BBDataOutputIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1079 uint8_t MXC_SPIXF_RevA_BBDataOutputIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1080 {
1081     return ((spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_BB_DATA_OUT_EN) >>
1082             MXC_F_SPIXFC_REVA_GEN_CTRL_BB_DATA_OUT_EN_POS);
1083 }
1084 
MXC_SPIXF_RevA_GetBBDataOutputValue(mxc_spixfc_reva_regs_t * spixfc)1085 uint8_t MXC_SPIXF_RevA_GetBBDataOutputValue(mxc_spixfc_reva_regs_t *spixfc)
1086 {
1087     return ((spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_BB_DATA) >>
1088             MXC_F_SPIXFC_REVA_GEN_CTRL_BB_DATA_POS);
1089 }
1090 
MXC_SPIXF_RevA_GetBBDataInputValue(mxc_spixfc_reva_regs_t * spixfc)1091 uint8_t MXC_SPIXF_RevA_GetBBDataInputValue(mxc_spixfc_reva_regs_t *spixfc)
1092 {
1093     return ((spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_SDIO_DATA_IN) >>
1094             MXC_F_SPIXFC_REVA_GEN_CTRL_SDIO_DATA_IN_POS);
1095 }
1096 
1097 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1098 #pragma section = ".spix_config"
1099 #elif defined(SPIXF_RAM)
1100 __attribute__((section(".spix_config")))
1101 #endif
MXC_SPIXF_RevA_SetModeData(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm,uint16_t data)1102 int MXC_SPIXF_RevA_SetModeData(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm,
1103                                uint16_t data)
1104 {
1105     MXC_SETFIELD(spixfm->mode_data, MXC_F_SPIXFM_REVA_MODE_DATA_DATA,
1106                  data << MXC_F_SPIXFM_REVA_MODE_DATA_DATA_POS);
1107     return E_NO_ERROR;
1108 }
1109 
MXC_SPIXF_RevA_GetModeData(mxc_spixfm_reva_regs_t * spixfm)1110 uint16_t MXC_SPIXF_RevA_GetModeData(mxc_spixfm_reva_regs_t *spixfm)
1111 {
1112     return ((spixfm->mode_data & MXC_F_SPIXFM_REVA_MODE_DATA_DATA) >>
1113             MXC_F_SPIXFM_REVA_MODE_DATA_DATA_POS);
1114 }
1115 
MXC_SPIXF_RevA_SetSCKInverted(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm)1116 int MXC_SPIXF_RevA_SetSCKInverted(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm)
1117 {
1118     spixfm->fb_ctrl |= MXC_F_SPIXFM_REVA_FB_CTRL_INVERT_EN;
1119     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_FB_INVERT;
1120     return E_NO_ERROR;
1121 }
1122 
1123 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1124 #pragma section = ".spix_config"
1125 #elif defined(SPIXF_RAM)
1126 __attribute__((section(".spix_config")))
1127 #endif
MXC_SPIXF_RevA_SetSCKNonInverted(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm)1128 int MXC_SPIXF_RevA_SetSCKNonInverted(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm)
1129 {
1130     spixfm->fb_ctrl &= (~MXC_F_SPIXFM_REVA_FB_CTRL_INVERT_EN);
1131     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_FB_INVERT);
1132     return E_NO_ERROR;
1133 }
1134 
MXC_SPIXF_RevA_GetSCKInverted(mxc_spixfm_reva_regs_t * spixfm)1135 int MXC_SPIXF_RevA_GetSCKInverted(mxc_spixfm_reva_regs_t *spixfm)
1136 {
1137     return !!(spixfm->fb_ctrl & MXC_F_SPIXFM_REVA_FB_CTRL_INVERT_EN);
1138 }
1139 
1140 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1141 #pragma section = ".spix_config"
1142 #elif defined(SPIXF_RAM)
1143 __attribute__((section(".spix_config")))
1144 #endif
MXC_SPIXF_RevA_SCKFeedbackEnable(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm)1145 int MXC_SPIXF_RevA_SCKFeedbackEnable(mxc_spixfc_reva_regs_t *spixfc, mxc_spixfm_reva_regs_t *spixfm)
1146 {
1147     spixfm->fb_ctrl |= MXC_F_SPIXFM_REVA_FB_CTRL_FB_EN;
1148     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_FB;
1149     return E_NO_ERROR;
1150 }
1151 
1152 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1153 #pragma section = ".spix_config"
1154 #elif defined(SPIXF_RAM)
1155 __attribute__((section(".spix_config")))
1156 #endif
MXC_SPIXF_RevA_SCKFeedbackDisable(mxc_spixfc_reva_regs_t * spixfc,mxc_spixfm_reva_regs_t * spixfm)1157 int MXC_SPIXF_RevA_SCKFeedbackDisable(mxc_spixfc_reva_regs_t *spixfc,
1158                                       mxc_spixfm_reva_regs_t *spixfm)
1159 {
1160     spixfm->fb_ctrl &= (~MXC_F_SPIXFM_REVA_FB_CTRL_FB_EN);
1161     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_FB);
1162     return E_NO_ERROR;
1163 }
1164 
1165 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1166 #pragma section = ".spix_config"
1167 #elif defined(SPIXF_RAM)
1168 __attribute__((section(".spix_config")))
1169 #endif
MXC_SPIXF_RevA_SCKFeedbackIsEnabled(mxc_spixfm_reva_regs_t * spixfm)1170 int MXC_SPIXF_RevA_SCKFeedbackIsEnabled(mxc_spixfm_reva_regs_t *spixfm)
1171 {
1172     return !!(spixfm->fb_ctrl & MXC_F_SPIXFM_REVA_FB_CTRL_FB_EN);
1173 }
1174 
MXC_SPIXF_RevA_SetSCKSampleDelay(mxc_spixfc_reva_regs_t * spixfc,uint8_t delay)1175 int MXC_SPIXF_RevA_SetSCKSampleDelay(mxc_spixfc_reva_regs_t *spixfc, uint8_t delay)
1176 {
1177     MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_IOSMPL,
1178                  delay << MXC_F_SPIXFC_REVA_CFG_IOSMPL_POS);
1179     return E_NO_ERROR;
1180 }
1181 
MXC_SPIXF_RevA_GetSCKSampleDelay(mxc_spixfc_reva_regs_t * spixfc)1182 uint8_t MXC_SPIXF_RevA_GetSCKSampleDelay(mxc_spixfc_reva_regs_t *spixfc)
1183 {
1184     return ((spixfc->cfg & MXC_F_SPIXFC_REVA_CFG_IOSMPL) >> MXC_F_SPIXFC_REVA_CFG_IOSMPL_POS);
1185 }
1186 
1187 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1188 #pragma section = ".spix_config"
1189 #elif defined(SPIXF_RAM)
1190 __attribute__((section(".spix_config")))
1191 #endif
MXC_SPIXF_RevA_SetCmdValue(mxc_spixfm_reva_regs_t * spixfm,uint8_t cmdval)1192 int MXC_SPIXF_RevA_SetCmdValue(mxc_spixfm_reva_regs_t *spixfm, uint8_t cmdval)
1193 {
1194     MXC_SETFIELD(spixfm->fetch_ctrl, MXC_F_SPIXFM_REVA_FETCH_CTRL_CMDVAL,
1195                  cmdval << MXC_F_SPIXFM_REVA_FETCH_CTRL_CMDVAL_POS);
1196     return E_NO_ERROR;
1197 }
1198 
MXC_SPIXF_RevA_GetCmdValue(mxc_spixfm_reva_regs_t * spixfm)1199 uint8_t MXC_SPIXF_RevA_GetCmdValue(mxc_spixfm_reva_regs_t *spixfm)
1200 {
1201     return ((spixfm->fetch_ctrl & MXC_F_SPIXFM_REVA_FETCH_CTRL_CMDVAL) >>
1202             MXC_F_SPIXFM_REVA_FETCH_CTRL_CMDVAL_POS);
1203 }
1204 
1205 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1206 #pragma section = ".spix_config"
1207 #elif defined(SPIXF_RAM)
1208 __attribute__((section(".spix_config")))
1209 #endif
MXC_SPIXF_RevA_SetPageSize(mxc_spixfc_reva_regs_t * spixfc,mxc_spixf_page_size_t size)1210 void MXC_SPIXF_RevA_SetPageSize(mxc_spixfc_reva_regs_t *spixfc, mxc_spixf_page_size_t size)
1211 {
1212     MXC_SETFIELD(spixfc->cfg, MXC_F_SPIXFC_REVA_CFG_PAGE_SIZE,
1213                  size << MXC_F_SPIXFC_REVA_CFG_PAGE_SIZE_POS);
1214 }
1215 
MXC_SPIXF_RevA_GetPageSize(mxc_spixfc_reva_regs_t * spixfc)1216 mxc_spixf_page_size_t MXC_SPIXF_RevA_GetPageSize(mxc_spixfc_reva_regs_t *spixfc)
1217 {
1218     return (mxc_spixf_page_size_t)((spixfc->cfg & MXC_F_SPIXFC_REVA_CFG_PAGE_SIZE) >>
1219                                    MXC_F_SPIXFC_REVA_CFG_PAGE_SIZE_POS);
1220 }
1221 
MXC_SPIXF_RevA_SimpleRXEnabled(mxc_spixfc_reva_regs_t * spixfc)1222 int MXC_SPIXF_RevA_SimpleRXEnabled(mxc_spixfc_reva_regs_t *spixfc)
1223 {
1224     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_SIMPLE_RX;
1225     return E_NO_ERROR;
1226 }
1227 
MXC_SPIXF_RevA_SimpleRXDisable(mxc_spixfc_reva_regs_t * spixfc)1228 int MXC_SPIXF_RevA_SimpleRXDisable(mxc_spixfc_reva_regs_t *spixfc)
1229 {
1230     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_SIMPLE_RX);
1231     return E_NO_ERROR;
1232 }
1233 
MXC_SPIXF_RevA_SimpleRXIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1234 int MXC_SPIXF_RevA_SimpleRXIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1235 {
1236     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_SIMPLE_RX);
1237 }
1238 
MXC_SPIXF_RevA_SimpleModeEnable(mxc_spixfc_reva_regs_t * spixfc)1239 int MXC_SPIXF_RevA_SimpleModeEnable(mxc_spixfc_reva_regs_t *spixfc)
1240 {
1241     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_SIMPLE;
1242     return E_NO_ERROR;
1243 }
1244 
MXC_SPIXF_RevA_SimpleModeDisable(mxc_spixfc_reva_regs_t * spixfc)1245 int MXC_SPIXF_RevA_SimpleModeDisable(mxc_spixfc_reva_regs_t *spixfc)
1246 {
1247     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_SIMPLE);
1248     return E_NO_ERROR;
1249 }
1250 
MXC_SPIXF_RevA_SimpleModeIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1251 int MXC_SPIXF_RevA_SimpleModeIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1252 {
1253     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_SIMPLE);
1254 }
1255 
MXC_SPIXF_RevA_SampleOutputEnable(mxc_spixfc_reva_regs_t * spixfc,uint8_t mask)1256 int MXC_SPIXF_RevA_SampleOutputEnable(mxc_spixfc_reva_regs_t *spixfc, uint8_t mask)
1257 {
1258     spixfc->sp_ctrl |= (mask << MXC_F_SPIXFC_REVA_SP_CTRL_SDIO_OUT_EN_POS);
1259     return E_NO_ERROR;
1260     {
1261     }
1262 }
1263 
MXC_SPIXF_RevA_SampleOutputDisable(mxc_spixfc_reva_regs_t * spixfc,uint8_t mask)1264 int MXC_SPIXF_RevA_SampleOutputDisable(mxc_spixfc_reva_regs_t *spixfc, uint8_t mask)
1265 {
1266     spixfc->sp_ctrl &= ~(mask << MXC_F_SPIXFC_REVA_SP_CTRL_SDIO_OUT_EN_POS);
1267     return E_NO_ERROR;
1268 }
1269 
MXC_SPIXF_RevA_SampleOutputIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1270 uint8_t MXC_SPIXF_RevA_SampleOutputIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1271 {
1272     return ((spixfc->sp_ctrl & MXC_F_SPIXFC_REVA_SP_CTRL_SDIO_OUT_EN) >>
1273             MXC_F_SPIXFC_REVA_SP_CTRL_SDIO_OUT_EN_POS);
1274 }
1275 
MXC_SPIXF_RevA_GetSampleOutputValue(mxc_spixfc_reva_regs_t * spixfc)1276 uint8_t MXC_SPIXF_RevA_GetSampleOutputValue(mxc_spixfc_reva_regs_t *spixfc)
1277 {
1278     return ((spixfc->sp_ctrl & MXC_F_SPIXFC_REVA_SP_CTRL_SDIO_OUT) >>
1279             MXC_F_SPIXFC_REVA_SP_CTRL_SDIO_OUT_POS);
1280 }
1281 
MXC_SPIXF_RevA_SetIoctrlSDIODriveHigh(mxc_spixfm_reva_regs_t * spixfm)1282 void MXC_SPIXF_RevA_SetIoctrlSDIODriveHigh(mxc_spixfm_reva_regs_t *spixfm)
1283 {
1284     spixfm->io_ctrl |= MXC_F_SPIXFM_REVA_IO_CTRL_SDIO_DS;
1285 }
1286 
MXC_SPIXF_RevA_SetIoctrlSDIODriveLow(mxc_spixfm_reva_regs_t * spixfm)1287 void MXC_SPIXF_RevA_SetIoctrlSDIODriveLow(mxc_spixfm_reva_regs_t *spixfm)
1288 {
1289     spixfm->io_ctrl &= (~MXC_F_SPIXFM_REVA_IO_CTRL_SDIO_DS);
1290 }
1291 
MXC_SPIXF_RevA_GetIoctrlSDIODrive(mxc_spixfm_reva_regs_t * spixfm)1292 uint8_t MXC_SPIXF_RevA_GetIoctrlSDIODrive(mxc_spixfm_reva_regs_t *spixfm)
1293 {
1294     return !!(spixfm->io_ctrl & MXC_F_SPIXFM_REVA_IO_CTRL_SDIO_DS);
1295 }
1296 
MXC_SPIXF_RevA_SetIoctrlSCLKDriveHigh(mxc_spixfm_reva_regs_t * spixfm)1297 void MXC_SPIXF_RevA_SetIoctrlSCLKDriveHigh(mxc_spixfm_reva_regs_t *spixfm)
1298 {
1299     spixfm->io_ctrl |= MXC_F_SPIXFM_REVA_IO_CTRL_SCLK_DS;
1300 }
1301 
MXC_SPIXF_RevA_SetIoctrlSCLKDriveLow(mxc_spixfm_reva_regs_t * spixfm)1302 void MXC_SPIXF_RevA_SetIoctrlSCLKDriveLow(mxc_spixfm_reva_regs_t *spixfm)
1303 {
1304     spixfm->io_ctrl &= (~MXC_F_SPIXFM_REVA_IO_CTRL_SCLK_DS);
1305 }
1306 
MXC_SPIXF_RevA_GetIoctrlSCLKDrive(mxc_spixfm_reva_regs_t * spixfm)1307 uint8_t MXC_SPIXF_RevA_GetIoctrlSCLKDrive(mxc_spixfm_reva_regs_t *spixfm)
1308 {
1309     return !!(spixfm->io_ctrl & MXC_F_SPIXFM_REVA_IO_CTRL_SCLK_DS);
1310 }
1311 
MXC_SPIXF_RevA_SetIoctrlSSDriveHigh(mxc_spixfm_reva_regs_t * spixfm)1312 void MXC_SPIXF_RevA_SetIoctrlSSDriveHigh(mxc_spixfm_reva_regs_t *spixfm)
1313 {
1314     spixfm->io_ctrl |= MXC_F_SPIXFM_REVA_IO_CTRL_SS_DS;
1315 }
1316 
MXC_SPIXF_RevA_SetIoctrlSSDriveLow(mxc_spixfm_reva_regs_t * spixfm)1317 void MXC_SPIXF_RevA_SetIoctrlSSDriveLow(mxc_spixfm_reva_regs_t *spixfm)
1318 {
1319     spixfm->io_ctrl &= (~MXC_F_SPIXFM_REVA_IO_CTRL_SS_DS);
1320 }
1321 
MXC_SPIXF_RevA_GetIoctrlSSDrive(mxc_spixfm_reva_regs_t * spixfm)1322 uint8_t MXC_SPIXF_RevA_GetIoctrlSSDrive(mxc_spixfm_reva_regs_t *spixfm)
1323 {
1324     return !!(spixfm->io_ctrl & MXC_F_SPIXFM_REVA_IO_CTRL_SS_DS);
1325 }
1326 
MXC_SPIXF_RevA_SetPuPdCtrl(mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_padctrl_t pad)1327 void MXC_SPIXF_RevA_SetPuPdCtrl(mxc_spixfm_reva_regs_t *spixfm, mxc_spixf_padctrl_t pad)
1328 {
1329     MXC_SPIXF_RevA_SetPadCtrl(spixfm, pad);
1330 }
1331 
MXC_SPIXF_RevA_GetPuPdCtrl(mxc_spixfm_reva_regs_t * spixfm)1332 uint8_t MXC_SPIXF_RevA_GetPuPdCtrl(mxc_spixfm_reva_regs_t *spixfm)
1333 {
1334     return MXC_SPIXF_RevA_GetPadCtrl(spixfm);
1335 }
1336 
MXC_SPIXF_RevA_SetPadCtrl(mxc_spixfm_reva_regs_t * spixfm,mxc_spixf_padctrl_t pad)1337 void MXC_SPIXF_RevA_SetPadCtrl(mxc_spixfm_reva_regs_t *spixfm, mxc_spixf_padctrl_t pad)
1338 {
1339     spixfm->io_ctrl &= ~(MXC_F_SPIXFM_REVA_IO_CTRL_PU_PD_CTRL);
1340     spixfm->io_ctrl |= (pad & MXC_F_SPIXFM_REVA_IO_CTRL_PU_PD_CTRL);
1341 }
1342 
MXC_SPIXF_RevA_GetPadCtrl(mxc_spixfm_reva_regs_t * spixfm)1343 uint8_t MXC_SPIXF_RevA_GetPadCtrl(mxc_spixfm_reva_regs_t *spixfm)
1344 {
1345     return (spixfm->io_ctrl & MXC_F_SPIXFM_REVA_IO_CTRL_PU_PD_CTRL);
1346 }
1347 
MXC_SPIXF_RevA_SetSCKDriveHigh(mxc_spixfc_reva_regs_t * spixfc)1348 void MXC_SPIXF_RevA_SetSCKDriveHigh(mxc_spixfc_reva_regs_t *spixfc)
1349 {
1350     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_DR;
1351 }
1352 
MXC_SPIXF_RevA_SetSCKDriveLow(mxc_spixfc_reva_regs_t * spixfc)1353 void MXC_SPIXF_RevA_SetSCKDriveLow(mxc_spixfc_reva_regs_t *spixfc)
1354 {
1355     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_DR);
1356 }
1357 
MXC_SPIXF_RevA_GetSCKDrive(mxc_spixfc_reva_regs_t * spixfc)1358 uint8_t MXC_SPIXF_RevA_GetSCKDrive(mxc_spixfc_reva_regs_t *spixfc)
1359 {
1360     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_SCLK_DR);
1361 }
1362 
MXC_SPIXF_RevA_SetSSDriveOutputHigh(mxc_spixfc_reva_regs_t * spixfc)1363 void MXC_SPIXF_RevA_SetSSDriveOutputHigh(mxc_spixfc_reva_regs_t *spixfc)
1364 {
1365     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_SSDR;
1366 }
1367 
MXC_SPIXF_RevA_SetSSDriveOutputLow(mxc_spixfc_reva_regs_t * spixfc)1368 void MXC_SPIXF_RevA_SetSSDriveOutputLow(mxc_spixfc_reva_regs_t *spixfc)
1369 {
1370     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_SSDR);
1371 }
1372 
MXC_SPIXF_RevA_GetSSDriveOutput(mxc_spixfc_reva_regs_t * spixfc)1373 uint8_t MXC_SPIXF_RevA_GetSSDriveOutput(mxc_spixfc_reva_regs_t *spixfc)
1374 {
1375     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_SSDR);
1376 }
1377 
MXC_SPIXF_RevA_BitBangModeEnable(mxc_spixfc_reva_regs_t * spixfc)1378 int MXC_SPIXF_RevA_BitBangModeEnable(mxc_spixfc_reva_regs_t *spixfc)
1379 {
1380     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_BBMODE;
1381     return E_NO_ERROR;
1382 }
1383 
MXC_SPIXF_RevA_BitBangModeDisable(mxc_spixfc_reva_regs_t * spixfc)1384 int MXC_SPIXF_RevA_BitBangModeDisable(mxc_spixfc_reva_regs_t *spixfc)
1385 {
1386     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_BBMODE);
1387     return E_NO_ERROR;
1388 }
1389 
MXC_SPIXF_RevA_BitBangModeIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1390 int MXC_SPIXF_RevA_BitBangModeIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1391 {
1392     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_BBMODE);
1393 }
1394 
1395 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1396 #pragma section = ".spix_config"
1397 #elif defined(SPIXF_RAM)
1398 __attribute__((section(".spix_config")))
1399 #endif
MXC_SPIXF_RevA_RXFIFOEnable(mxc_spixfc_reva_regs_t * spixfc)1400 int MXC_SPIXF_RevA_RXFIFOEnable(mxc_spixfc_reva_regs_t *spixfc)
1401 {
1402     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_RX_FIFO_EN;
1403     return E_NO_ERROR;
1404 }
1405 
MXC_SPIXF_RevA_RXFIFODisable(mxc_spixfc_reva_regs_t * spixfc)1406 int MXC_SPIXF_RevA_RXFIFODisable(mxc_spixfc_reva_regs_t *spixfc)
1407 {
1408     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_RX_FIFO_EN);
1409     return E_NO_ERROR;
1410 }
1411 
MXC_SPIXF_RevA_RXFIFOIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1412 int MXC_SPIXF_RevA_RXFIFOIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1413 {
1414     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_RX_FIFO_EN);
1415 }
1416 
1417 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1418 #pragma section = ".spix_config"
1419 #elif defined(SPIXF_RAM)
1420 __attribute__((section(".spix_config")))
1421 #endif
MXC_SPIXF_RevA_TXFIFOEnable(mxc_spixfc_reva_regs_t * spixfc)1422 int MXC_SPIXF_RevA_TXFIFOEnable(mxc_spixfc_reva_regs_t *spixfc)
1423 {
1424     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_TX_FIFO_EN;
1425     return E_NO_ERROR;
1426 }
1427 
MXC_SPIXF_RevA_TXFIFODisable(mxc_spixfc_reva_regs_t * spixfc)1428 int MXC_SPIXF_RevA_TXFIFODisable(mxc_spixfc_reva_regs_t *spixfc)
1429 {
1430     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_TX_FIFO_EN);
1431     return E_NO_ERROR;
1432 }
1433 
MXC_SPIXF_RevA_TXFIFOIsEnabled(mxc_spixfc_reva_regs_t * spixfc)1434 int MXC_SPIXF_RevA_TXFIFOIsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1435 {
1436     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_TX_FIFO_EN);
1437 }
1438 
1439 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1440 #pragma section = ".spix_config"
1441 #elif defined(SPIXF_RAM)
1442 __attribute__((section(".spix_config")))
1443 #endif
MXC_SPIXF_RevA_Enable(mxc_spixfc_reva_regs_t * spixfc)1444 int MXC_SPIXF_RevA_Enable(mxc_spixfc_reva_regs_t *spixfc)
1445 {
1446     spixfc->gen_ctrl |= MXC_F_SPIXFC_REVA_GEN_CTRL_ENABLE;
1447     return E_NO_ERROR;
1448 }
1449 
1450 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1451 #pragma section = ".spix_config"
1452 #elif defined(SPIXF_RAM)
1453 __attribute__((section(".spix_config")))
1454 #endif
MXC_SPIXF_RevA_Disable(mxc_spixfc_reva_regs_t * spixfc)1455 int MXC_SPIXF_RevA_Disable(mxc_spixfc_reva_regs_t *spixfc)
1456 {
1457     spixfc->gen_ctrl &= (~MXC_F_SPIXFC_REVA_GEN_CTRL_ENABLE);
1458     return E_NO_ERROR;
1459 }
1460 
1461 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1462 #pragma section = ".spix_config"
1463 #elif defined(SPIXF_RAM)
1464 __attribute__((section(".spix_config")))
1465 #endif
MXC_SPIXF_RevA_IsEnabled(mxc_spixfc_reva_regs_t * spixfc)1466 int MXC_SPIXF_RevA_IsEnabled(mxc_spixfc_reva_regs_t *spixfc)
1467 {
1468     return !!(spixfc->gen_ctrl & MXC_F_SPIXFC_REVA_GEN_CTRL_ENABLE);
1469 }
1470 
1471 #if defined(SPIXF_RAM) && IAR_PRAGMAS
1472 #pragma section = ".spix_config"
1473 #elif defined(SPIXF_RAM)
1474 __attribute__((section(".spix_config")))
1475 #endif
MXC_SPIXF_RevA_SetBusIdle(mxc_spixfm_reva_regs_t * spixfm,unsigned int busidle)1476 int MXC_SPIXF_RevA_SetBusIdle(mxc_spixfm_reva_regs_t *spixfm, unsigned int busidle)
1477 {
1478     spixfm->bus_idle = busidle;
1479     return E_NO_ERROR;
1480 }
1481 
MXC_SPIXF_RevA_GetBusIdle(mxc_spixfm_reva_regs_t * spixfm)1482 unsigned int MXC_SPIXF_RevA_GetBusIdle(mxc_spixfm_reva_regs_t *spixfm)
1483 {
1484     return spixfm->bus_idle;
1485 }
1486 
1487 /* ************************************************************************** */
1488 
1489 // MXC_GetLock
1490 #if defined(SPIXF_RAM)
1491 #if IAR_PRAGMAS
1492 #pragma section = ".spix_config"
1493 #else
1494 __attribute__((section(".spix_config")))
1495 #endif
MXC_GetLock_SPIXF(uint32_t * lock,uint32_t value)1496 int MXC_GetLock_SPIXF(uint32_t *lock, uint32_t value)
1497 {
1498 #ifndef __riscv
1499     do {
1500         // Return if the lock is taken by a different thread
1501         if (__LDREXW((volatile uint32_t *)lock) != 0) {
1502             return E_BUSY;
1503         }
1504 
1505         // Attempt to take the lock
1506     } while (__STREXW(value, (volatile uint32_t *)lock) != 0);
1507 
1508     // Do not start any other memory access until memory barrier is complete
1509     __DMB();
1510 #endif // __riscv
1511 
1512     return E_NO_ERROR;
1513 }
1514 #endif
1515 
1516 // MXC_FreeLock
1517 #if defined(SPIXF_RAM)
1518 #if IAR_PRAGMAS
1519 #pragma section = ".spix_config"
1520 #else
1521 __attribute__((section(".spix_config")))
1522 #endif
MXC_FreeLock_SPIXF(uint32_t * lock)1523 void MXC_FreeLock_SPIXF(uint32_t *lock)
1524 {
1525 #ifndef __riscv
1526     // Ensure memory operations complete before releasing lock
1527     __DMB();
1528 #endif // __riscv
1529     *lock = 0;
1530 }
1531 #endif
1532