1 /**
2  * @file    spixr.h
3  * @brief   Registers, Bit Masks and Bit Positions for the SPI RAM XIP Data module.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_SPIXR_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_SPIXR_H_
29 
30 /* **** Includes **** */
31 #include "spixr_regs.h"
32 #include "mxc_sys.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @defgroup spixr SPI External Ram (SPIXR)
40  * @ingroup periphlibs
41  * @{
42  */
43 
44 /* **** Definitions **** */
45 /**
46  * @brief       Enum to define SPIXR data width.
47  */
48 typedef enum {
49     MXC_SPIXR_SINGLE_SDIO = 0,
50     MXC_SPIXR_DUAL_SDIO,
51     MXC_SPIXR_QUAD_SDIO,
52 } mxc_spixr_width_t;
53 
54 /**
55  * @brief       SPIXR mode.
56  * @note        modes 1 and 2 are not supported
57  */
58 typedef enum {
59     MXC_SPIXR_MODE_0,
60     MXC_SPIXR_MODE_1,
61     MXC_SPIXR_MODE_2,
62     MXC_SPIXR_MODE_3,
63 } mxc_spixr_mode_t;
64 
65 /**
66  * @brief       Configuration parameters of SPIXR
67  */
68 typedef struct {
69     uint32_t
70         numbits; /**< Number of Bits per character. In slave mode 9-bit character length is not supported. */
71     mxc_spixr_width_t data_width; /**< SPI Data width */
72 
73     uint32_t ssel_act_1; /**< Slave Select Action delay 1 */
74     uint32_t ssel_act_2; /**< Slave Select Action delay 2 */
75     uint32_t ssel_inact; /**< Slave Select Inactive delay */
76 
77     uint32_t baud_freq; /**< Desired baud rate duty cycle control */
78 } mxc_spixr_cfg_t;
79 
80 /* **** Function Prototypes **** */
81 
82 /**
83  * @brief       Unloads bytes from the FIFO
84  *
85  * @param       buf   The buffer to read the data into
86  * @param       len     The number of bytes to read
87  *
88  * @return     #E_NULL_PTR if NULL buffer passed, #E_NO_ERROR otherwise
89  */
90 int MXC_SPIXR_ReadRXFIFO(uint8_t *buf, int len);
91 
92 /**
93  * @brief       Loads bytes into the FIFO
94  *
95  * @param       buf   The buffer containing data to write
96  * @param       len     The number of bytes to write
97  *
98  * @return     #E_NULL_PTR if NULL buffer passed, #E_NO_ERROR otherwise
99  */
100 int MXC_SPIXR_WriteTXFIFO(uint8_t *buf, int len);
101 
102 /**
103  * @brief       Select which SS pin is used in SPIXR
104  */
105 void MXC_SPIXR_SetSS(void);
106 
107 /**
108  * @brief       Returns the SS line selected
109  *
110  * @return      The index of the SS pin to use
111  */
112 int MXC_SPIXR_GetSS(void);
113 
114 /**
115  * @brief       Control the deassertion of the SS line
116  *
117  * @param       stayActive  Keep the SS line asserted between
118  *                          sequential transmissions
119  */
120 void MXC_SPIXR_SetSSCtrl(int stayActive);
121 
122 /**
123  * @brief       Get the setting that controls deassertion of the SS line
124  *
125  * @return      1 to keep the SS line asserted between sequential transmissions
126  */
127 int MXC_SPIXR_GetSSCtrl(void);
128 
129 /**
130  * @brief       Enable the SPI RAM XIP Data module.
131  */
132 void MXC_SPIXR_Enable(void);
133 
134 /**
135  * @brief       Disable the SPI RAM XIP Data module.
136  */
137 void MXC_SPIXR_Disable(void);
138 
139 /**
140  * @brief       Get if SPIXR is enabled
141  *
142  * @return      1 = enabled, 0 = disabled
143  */
144 int MXC_SPIXR_IsEnabled(void);
145 
146 /**
147  * @brief       Enable the TXFIFO
148  *
149  */
150 void MXC_SPIXR_TXFIFOEnable(void);
151 
152 /**
153  * @brief       Disable the TXFIFO
154  *
155  */
156 void MXC_SPIXR_TXFIFODisable(void);
157 
158 /**
159  * @brief       Get if TXFIFO is enabled
160  *
161  * @return      1 = enabled, 0 = disabled
162  */
163 int MXC_SPIXR_TXFIFOIsEnabled(void);
164 
165 /**
166  * @brief       Enable the TX DMA
167  *
168  */
169 void MXC_SPIXR_DMATXFIFOEnable(void);
170 
171 /**
172  * @brief       Disable the TX DMA
173  *
174  */
175 void MXC_SPIXR_DMATXFIFODisable(void);
176 
177 /**
178  * @brief       Get if TX DMA is enabled
179  *
180  * @return      1 = enabled, 0 = disabled
181  */
182 int MXC_SPIXR_DMATXFIFOIsEnabled(void);
183 
184 /**
185  * @brief       Enable the RXFIFO
186  *
187  */
188 void MXC_SPIXR_RXFIFOEnable(void);
189 
190 /**
191  * @brief       Disable the RXFIFO
192  *
193  */
194 void MXC_SPIXR_RXFIFODisable(void);
195 
196 /**
197  * @brief       Get if RXFIFO is enabled
198  *
199  * @return      1 = enabled, 0 = disabled
200  */
201 int MXC_SPIXR_RXFIFOIsEnabled(void);
202 
203 /**
204  * @brief       Enable the RX DMA
205  *
206  */
207 void MXC_SPIXR_DMARXFIFOEnable(void);
208 
209 /**
210  * @brief       Disable the RX DMA
211  *
212  */
213 void MXC_SPIXR_DMARXFIFODisable(void);
214 
215 /**
216  * @brief       Get if RX DMA is enabled
217  *
218  * @return      1 = enabled, 0 = disabled
219  */
220 int MXC_SPIXR_DMARXFIFOIsEnabled(void);
221 
222 /**
223  * @brief       Enable three wire mode
224  *
225  */
226 void MXC_SPIXR_ThreeWireModeEnable(void);
227 
228 /**
229  * @brief       Disable three wire mode
230  *
231  */
232 void MXC_SPIXR_ThreeWireModeDisable(void);
233 
234 /**
235  * @brief       Get if three wire mode is enabled
236  *
237  * @return      1 = enabled, 0 = disabled
238  */
239 int MXC_SPIXR_ThreeWireModeIsEnabled(void);
240 
241 /**
242  * @brief       Get the number of bytes currently in the TX FIFO
243  *
244  * @return      The number of bytes currently in the TX FIFO
245  */
246 int MXC_SPIXR_GetTXFIFOCount(void);
247 
248 /**
249  * @brief       Get the number of bytes currently in the RX FIFO
250  *
251  * @return      The number of bytes currently in the RX FIFO
252  */
253 int MXC_SPIXR_GetRXFIFOCount(void);
254 
255 /**
256  * @brief       Clear TX FIFO
257  *
258  */
259 void MXC_SPIXR_TXFIFOClear(void);
260 
261 /**
262  * @brief       Clear RX FIFO
263  *
264  */
265 void MXC_SPIXR_RXFIFOClear(void);
266 
267 /**
268  * @brief       Set the SPI Width used
269  *
270  * @param       width   The width to be used
271  *
272  * @return      E_NO_ERROR if successful, E_BAD_PARAM otherwise.
273  */
274 int MXC_SPIXR_SetWidth(mxc_spixr_width_t width);
275 
276 /**
277  * @brief       Set the SPI Mode used
278  *
279  * @param       mode   The mode to be used
280  *
281  * @return      E_NO_ERROR if successful, E_BAD_PARAM otherwise.
282  */
283 int MXC_SPIXR_SetSPIMode(mxc_spixr_mode_t mode);
284 
285 /**
286  * @brief       Set the active state of the SS line
287  *
288  * @param       activeLow   Make the slave select line active low
289  *
290  * @return      E_NO_ERROR
291  */
292 int MXC_SPIXR_SetSSPolarity(int activeLow);
293 
294 /**
295  * @brief       Set the SS Timing Parameters
296  * @note        All timing is in units of system clocks
297  *
298  * @param       ssIActDelay Delay between end of transaction and start of next
299  * @param       postActive  Time after last SCLK  that SS remains active
300  * @param       preActive   Time after SS becomes active until first SCLK
301  *
302  */
303 void MXC_SPIXR_SetSSTiming(unsigned int ssIActDelay, unsigned int postActive,
304                            unsigned int preActive);
305 
306 /**
307  * @brief       Set the SPI Frequency
308  *
309  * @param       hz  The requested SCLK frequency
310  *
311  * @return      The actual speed set in Hz
312  */
313 int MXC_SPIXR_SetFrequency(int hz);
314 
315 /**
316  * @brief       Get the SPI Frequency
317  *
318  * @return      The current speed in Hz
319  */
320 int MXC_SPIXR_GetFrequency(void);
321 
322 /**
323  * @brief       Get the active interrupt flags
324  * @note        See \ref mxc_spixr_regs_t for a detailed list of flags
325  *
326  * @return      The SPIXR interrupt flags
327  */
328 int MXC_SPIXR_GetIntFlags(void);
329 
330 /**
331  * @brief       Enable SPIXR interrupts
332  *
333  * @param       flags   The flags to enable
334  */
335 void MXC_SPIXR_EnableInt(int flags);
336 
337 /**
338  * @brief       Disable SPIXR interrupts
339  *
340  * @param       flags   The flags to disable
341  */
342 void MXC_SPIXR_DisableInt(int flags);
343 
344 /**
345  * @brief       Get the active wake up flags
346  * @note        See \ref mxc_spixr_regs_t for a detailed list of flags
347  *
348  * @return      The SPIXR wake up flags
349  */
350 int MXC_SPIXR_GetWakeUpFlags(void);
351 
352 /**
353  * @brief       Enable Wake up for SPIXR
354  *
355  * @param       flags   The flags to disable
356  */
357 void MXC_SPIXR_EnableWakeUp(int flags);
358 
359 /**
360  * @brief       Enable Wake up for SPIXR
361  *
362  * @param       flags   The flags to disable
363  */
364 void MXC_SPIXR_DisableWakeUp(int flags);
365 
366 /**
367  * @brief       Enable the external memory mode
368  */
369 void MXC_SPIXR_ExMemEnable(void);
370 
371 /**
372  * @brief       Disable the SPI RAM XIP Data module.
373  */
374 void MXC_SPIXR_ExMemDisable(void);
375 
376 /**
377  * @brief       Put 255 characters worth of clocks between address
378  *              and read phase of external memory transactions
379  *
380  * @param       delay255 add the delay
381  */
382 void MXC_SPIXR_ExMemUseDummy(int delay255);
383 
384 /**
385  * @brief       Set the write command used for external memory mode
386  *
387  * @param       command The command to be used
388  */
389 void MXC_SPIXR_ExMemSetWriteCommand(uint8_t command);
390 
391 /**
392  * @brief       Get the write command used for external memory mode
393  *
394  * @return      the command to be used
395  */
396 uint8_t MXC_SPIXR_ExMemGetWriteCommand(void);
397 
398 /**
399  * @brief       Set the read command used for external memory mode
400  *
401  * @param       command The command to be used
402  */
403 void MXC_SPIXR_ExMemSetReadCommand(uint8_t command);
404 
405 /**
406  * @brief       Get the read command used for external memory mode
407  *
408  * @return      the command to be used
409  */
410 uint8_t MXC_SPIXR_ExMemGetReadCommand(void);
411 
412 /**
413  * @brief   SPI active status.
414  *
415  * In Master mode, set when transaction starts, cleared when last bit of last
416  * character is acted upon and Slave Select de-assertion would occur.
417  * @return  0 if inactive, 1 if active
418  */
419 int MXC_SPIXR_Busy(void);
420 
421 /**
422  * @brief       Initialize the SPI RAM XIP Data module.
423  * @param       cfg   initialize SPIXR parameters
424  * @return      #E_NO_ERROR if the SPIXR is initialized successfully,
425  *              @ref MXC_Error_Codes "error" if unsuccessful.
426  */
427 int MXC_SPIXR_Init(mxc_spixr_cfg_t *cfg);
428 
429 /**
430  * @brief       Shut Down the SPI RAM XIP Data Module
431  *
432  * @return      #E_NO_ERROR if the SPIXR is shutdown successfully,
433  *              @ref MXC_Error_Codes "error" if unsuccessful.
434  */
435 int MXC_SPIXR_Shutdown(void);
436 
437 /**
438  * @brief       Send a SPI formatted instruction to external RAM
439  * @param       cmd         Array of instructions to send
440  * @param       length      number of bytes to send
441  * @param       tx_num_char number of bytes to send
442  */
443 void MXC_SPIXR_SendCommand(uint8_t *cmd, uint32_t length, uint32_t tx_num_char);
444 
445 /**@} end of group spixr */
446 
447 #ifdef __cplusplus
448 }
449 #endif
450 
451 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_SPIXR_H_
452