1 /***************************************************************************//**
2 * \file cy_scb_spi.h
3 * \version 3.20
4 *
5 * Provides SPI API declarations of the SCB driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * Copyright 2016-2021 Cypress Semiconductor Corporation
10 * SPDX-License-Identifier: Apache-2.0
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 * \addtogroup group_scb_spi
27 * \{
28 * Driver API for SPI Peripheral.
29 *
30 * The functions and other declarations used in this part of the driver are in
31 * cy_scb_spi.h. You can also include cy_pdl.h to get access
32 * to all functions and declarations in the PDL.
33 *
34 * The SPI protocol is a synchronous serial interface protocol. Devices operate
35 * in either master or slave mode. The master initiates the data transfer.
36 * The SCB supports single-master-multiple-slaves topology for SPI. Multiple
37 * slaves are supported with individual slave select lines.
38 *
39 * Features:
40 * * Supports master and slave functionalitySupports three types of SPI protocols:
41 * * Motorola SPI - modes 0, 1, 2, and 3
42 * * Texas Instruments SPI, with coinciding and preceding data frame indicator - mode 1 only
43 * * National Semiconductor (MicroWire) SPI - mode 0 only
44 * * Master supports up to four slave select lines
45 * * Each slave select has configurable active polarity (high or low)
46 * * Slave select can be programmed to stay active for a whole transfer, or
47 * just for each byte
48 * * Master supports late sampling for better timing margin
49 * * Master supports continuous SPI clock
50 * * Data frame size programmable from 4 bits to 32 bits
51 * * Programmable oversampling
52 * * MSb or LSb first
53 * * Median filter available for inputs
54 *
55 ********************************************************************************
56 * \section group_scb_spi_configuration Configuration Considerations
57 ********************************************************************************
58 * The SPI driver configuration can be divided to number of sequential
59 * steps listed below:
60 * * \ref group_scb_spi_config
61 * * \ref group_scb_spi_pins
62 * * \ref group_scb_spi_clock
63 * * \ref group_scb_spi_data_rate
64 * * \ref group_scb_spi_intr
65 * * \ref group_scb_spi_enable
66 *
67 * \note
68 * The SPI driver is built on top of the SCB hardware block. The SCB1 instance is
69 * used as an example for all code snippets. Modify the code to match your
70 * design.
71 *
72 ********************************************************************************
73 * \subsection group_scb_spi_config Configure SPI
74 ********************************************************************************
75 * To set up the SPI driver, provide the configuration parameters in the
76 * \ref cy_stc_scb_spi_config_t structure. For example: provide spiMode,
77 * subMode, sclkMode, oversample, rxDataWidth, and txDataWidth. The other
78 * parameters are optional for operation. To initialize the driver, call
79 * \ref Cy_SCB_SPI_Init function providing a pointer to the populated
80 * \ref cy_stc_scb_spi_config_t structure and the allocated
81 * \ref cy_stc_scb_spi_context_t structure.
82 *
83 * \snippet scb/spi_snippet/main.c SPI_CFG
84 *
85 ********************************************************************************
86 * \subsection group_scb_spi_pins Assign and Configure Pins
87 ********************************************************************************
88 * Only dedicated SCB pins can be used for SPI operation. The HSIOM
89 * register must be configured to connect dedicated SCB SPI pins to the
90 * SCB block. Also, the SPI output pins must be configured in Strong Drive Input
91 * Off mode and SPI input pins in Digital High-Z.
92 *
93 * \snippet scb/spi_snippet/main.c SPI_CFG_PINS
94 *
95 ********************************************************************************
96 * \subsection group_scb_spi_clock Assign Clock Divider
97 ********************************************************************************
98 * A clock source must be connected to the SCB block to oversample input and
99 * output signals, in this document this clock will be referred as clk_scb.
100 * You must use one of the 8-bit or 16-bit dividers. Use the \ref group_sysclk
101 * driver API to do this.
102 *
103 * \snippet scb/spi_snippet/main.c SPI_CFG_ASSIGN_CLOCK
104 *
105 ********************************************************************************
106 * \subsection group_scb_spi_data_rate Configure Data Rate
107 ********************************************************************************
108 * To get the SPI slave to operate with the desired data rate, the clk_scb must be
109 * fast enough to provide sufficient oversampling. Use the \ref group_sysclk driver
110 * API to do that.
111 *
112 * \snippet scb/spi_snippet/main.c SPI_CFG_DATA_RATE_SLAVE
113 *
114 * To get the SPI master to operate with the desired data rate, multiply the
115 * oversample factor by the desired data rate to determine the required
116 * frequency for clk_scb. Use the \ref group_sysclk driver API to configure
117 * clk_scb frequency. Set the <em><b>oversample parameter in configuration
118 * structure</b></em> to define number of SCB clocks in one SCLK period.
119 * When this value is even, the first and second phases of the SCLK period are
120 * the same. Otherwise, the first phase is one SCB clock cycle longer than the
121 * second phase. The level of the first phase of the clock period depends on CPOL
122 * settings: 0 - low level and 1 - high level.
123 *
124 * \snippet scb/spi_snippet/main.c SPI_CFG_DATA_RATE_MASTER
125 *
126 * \note
127 * In CAT1D devices, to avoid potential metastable conditions at 50 MHz, set
128 * clock to 200 MHz, oversample to 4 and median filter to 1.
129 *
130 * <b>Refer to the technical reference manual (TRM) section SPI sub-section
131 * Oversampling and Bit Rate to get information about how to configure SPI to run
132 * with desired data rate</b>.
133 *
134 ********************************************************************************
135 * \subsection group_scb_spi_intr Configure Interrupt
136 ********************************************************************************
137 * The interrupt is optional for the SPI operation. To configure the interrupt,
138 * the \ref Cy_SCB_SPI_Interrupt function must be called in the interrupt
139 * handler for the selected SCB instance. Also, this interrupt must be enabled
140 * in the NVIC.
141 * The interrupt must be configured when \ref group_scb_spi_hl will be used.
142 *
143 * \snippet scb/spi_snippet/main.c SPI_INTR_A
144 * \snippet scb/spi_snippet/main.c SPI_INTR_B
145 * \snippet scb/spi_snippet/main.c SPI_INTR_C
146 *
147 ********************************************************************************
148 * \subsection group_scb_spi_enable Enable SPI
149 ********************************************************************************
150 * Finally, enable the SPI operation by calling \ref Cy_SCB_SPI_Enable.
151 * For the slave, this means that SPI device starts responding to the transfers.
152 * For the master, it is ready to execute transfers.
153 *
154 * \snippet scb/spi_snippet/main.c SPI_ENABLE
155 *
156 ********************************************************************************
157 * \section group_scb_spi_use_cases Common Use Cases
158 ********************************************************************************
159 * The SPI API is the same for the master and slave mode operation and
160 * is divided into two categories: \ref group_scb_spi_low_level_functions
161 * and \ref group_scb_spi_high_level_functions. \n
162 * <em>Do not mix <b>High-Level</b> and <b>Low-Level</b> API because a Low-Level
163 * API can adversely affect the operation of a High-Level API.</em>
164 *
165 ********************************************************************************
166 * \subsection group_scb_spi_ll Low-Level API
167 ********************************************************************************
168 * The \ref group_scb_spi_low_level_functions functions allow
169 * interacting directly with the hardware and do not use \ref Cy_SCB_SPI_Interrupt.
170 * These functions do not require context for operation. Thus, NULL can be
171 * passed for context parameter in \ref Cy_SCB_SPI_Init and \ref Cy_SCB_SPI_Disable
172 * instead of a pointer to the context structure.
173 *
174 * * To write data into the TX FIFO, use one of the provided functions:
175 * \ref Cy_SCB_SPI_Write, \ref Cy_SCB_SPI_WriteArray or
176 * \ref Cy_SCB_SPI_WriteArrayBlocking.
177 * Note that in the master mode, putting data into the TX FIFO starts a
178 * transfer. Due to the nature of SPI, the received data is put into the RX FIFO.
179 *
180 * * To read data from the RX FIFO, use one of the provided functions:
181 * \ref Cy_SCB_SPI_Read or \ref Cy_SCB_SPI_ReadArray. Again due to the nature
182 * of SPI these functions do not start a transfer on the bus, they only read
183 * data out of the RX FIFO that has already been received.
184 *
185 * * The statuses can be polled using: \ref Cy_SCB_SPI_GetRxFifoStatus,
186 * \ref Cy_SCB_SPI_GetTxFifoStatus and \ref Cy_SCB_SPI_GetSlaveMasterStatus.
187 * <em>The statuses are <b>W1C (Write 1 to Clear)</b> and after a status
188 * is set, it must be cleared.</em> Note that there are statuses evaluated as level.
189 * These statuses remain set until an event is true. Therefore, after the clear
190 * operation, the status is cleared but then it is restored (if the event is still
191 * true).
192 * Also, the following functions can be used for polling as well
193 * \ref Cy_SCB_SPI_IsBusBusy, \ref Cy_SCB_SPI_IsTxComplete,
194 * \ref Cy_SCB_SPI_GetNumInRxFifo and \ref Cy_SCB_SPI_GetNumInTxFifo.
195 *
196 * \snippet scb/spi_snippet/main.c SPI_TRANFER_DATA_LL
197 *
198 ********************************************************************************
199 * \subsection group_scb_spi_hl High-Level API
200 ********************************************************************************
201 * The \ref group_scb_spi_high_level_functions API use \ref Cy_SCB_SPI_Interrupt
202 * to execute the transfer. Call \ref Cy_SCB_SPI_Transfer to start communication: for master
203 * mode calling this function starts a transaction with the slave. For slave mode
204 * the read and write buffers are prepared for the communication with the master.
205 * After a transfer is started, the \ref Cy_SCB_SPI_Interrupt handles the
206 * transfer until its completion. Therefore, the \ref Cy_SCB_SPI_Interrupt function
207 * must be called inside the user interrupt handler to make the High-Level API work.
208 * To monitor the status of the transfer operation, use \ref Cy_SCB_SPI_GetTransferStatus.
209 * Alternatively, use \ref Cy_SCB_SPI_RegisterCallback to register a callback
210 * function to be notified about \ref group_scb_spi_macros_callback_events.
211 *
212 * \snippet scb/spi_snippet/main.c SPI_TRANFER_DATA
213 *
214 ********************************************************************************
215 * \section group_scb_spi_dma_trig DMA Trigger
216 ********************************************************************************
217 * The SCB provides TX and RX output trigger signals that can be routed to the
218 * DMA controller inputs. These signals are assigned based on the data availability
219 * in the TX and RX FIFOs appropriately.
220 *
221 * * The RX trigger signal is active while the number of data
222 * elements in the RX FIFO is greater than the value of RX FIFO level. Use
223 * function \ref Cy_SCB_SetRxFifoLevel or set configuration structure
224 * rxFifoTriggerLevel parameter to configure RX FIFO level value. \n
225 * <em>For example, the RX FIFO has 8 data elements and the RX FIFO level is 0.
226 * The RX trigger signal is active until DMA reads all data from
227 * the RX FIFO.</em>
228 *
229 * * The TX trigger signal is active while the number of data elements
230 * in the TX FIFO is less than the value of TX FIFO level. Use function
231 * \ref Cy_SCB_SetTxFifoLevel or set configuration structure txFifoTriggerLevel
232 * parameter to configure TX FIFO level value. \n
233 * <em>For example, the TX FIFO has 0 data elements (empty) and the TX FIFO level
234 * is 7. The TX trigger signal is active until DMA loads TX FIFO
235 * with 8 data elements (note that after the first TX load operation, the data
236 * element goes to the shift register and TX FIFO is empty).</em>
237 *
238 * To route SCB TX or RX trigger signals to the DMA controller, use \ref group_trigmux
239 * driver API.
240 *
241 * \note
242 * To properly handle DMA level request signal activation and de-activation from the SCB
243 * peripheral block the DMA Descriptor typically must be configured to re-trigger
244 * after 16 Clk_Slow cycles.
245 *
246 ********************************************************************************
247 * \section group_scb_spi_lp Low Power Support
248 ********************************************************************************
249 * The SPI driver provides callback functions to handle power mode transitions.
250 * The callback \ref Cy_SCB_SPI_DeepSleepCallback must be called
251 * during execution of \ref Cy_SysPm_CpuEnterDeepSleep; \ref Cy_SCB_SPI_HibernateCallback
252 * must be called during execution of \ref Cy_SysPm_SystemEnterHibernate. To trigger the
253 * callback execution, the callback must be registered before calling the
254 * power mode transition function. Refer to \ref group_syspm driver for more
255 * information about power mode transitions and callback registration.
256 *
257 * The SPI master is disabled during Deep Sleep and Hibernate and stops driving
258 * the output pins. The state of the SPI master output pins SCLK, SS, and MOSI is
259 * High-Z, which can cause unexpected behavior of the SPI Slave due to possible
260 * glitches on these lines. These pins must keep the inactive level (the same state
261 * when SPI master is enabled and does not transfer data) before entering Deep
262 * Sleep or Hibernate mode. To do that, write the GPIO data register of each pin
263 * to the inactive level for each output pin. Then configure High-Speed Input
264 * Output Multiplexer (HSIOM) of each pin to be controlled by the GPIO (use
265 * \ref group_gpio driver API). After after exiting Deep Sleep mode the SPI
266 * master must be enabled and the pins configuration restored to return the
267 * SPI master control of the pins (after exiting Hibernate mode, the
268 * system initialization code does the same). Copy either or
269 * both \ref Cy_SCB_SPI_DeepSleepCallback and \ref Cy_SCB_SPI_HibernateCallback
270 * as appropriate, and make the changes described above inside the function.
271 * Alternately, external pull-up or pull-down resistors can be connected
272 * to the appropriate SPI lines to keep them inactive during Deep-Sleep or
273 * Hibernate.
274 *
275 * \note
276 * Only applicable for <b>rev-08 of the CY8CKIT-062-BLE</b>.
277 * For proper operation, when the SPI slave is configured to be a wakeup
278 * source from Deep Sleep mode, the \ref Cy_SCB_SPI_DeepSleepCallback must be
279 * copied and modified. Refer to the function description to get the details.
280 *
281 * \defgroup group_scb_spi_macros Macros
282 * \defgroup group_scb_spi_functions Functions
283 * \{
284 * \defgroup group_scb_spi_general_functions General
285 * \defgroup group_scb_spi_high_level_functions High-Level
286 * \defgroup group_scb_spi_low_level_functions Low-Level
287 * \defgroup group_scb_spi_interrupt_functions Interrupt
288 * \defgroup group_scb_spi_low_power_functions Low Power Callbacks
289 * \}
290 * \defgroup group_scb_spi_data_structures Data Structures
291 * \defgroup group_scb_spi_enums Enumerated Types
292 */
293
294 #if !defined(CY_SCB_SPI_H)
295 #define CY_SCB_SPI_H
296
297 #include "cy_device.h"
298
299 #if (defined (CY_IP_MXSCB) || defined (CY_IP_MXS22SCB))
300
301 #include "cy_scb_common.h"
302
303 #if defined(__cplusplus)
304 extern "C" {
305 #endif
306
307 /*******************************************************************************
308 * Enumerated Types
309 *******************************************************************************/
310
311 /**
312 * \addtogroup group_scb_spi_enums
313 * \{
314 */
315
316 /** SPI status codes */
317 typedef enum
318 {
319 /** Operation completed successfully */
320 CY_SCB_SPI_SUCCESS = 0U,
321
322 /** One or more of input parameters are invalid */
323 CY_SCB_SPI_BAD_PARAM = (CY_SCB_ID | CY_PDL_STATUS_ERROR | CY_SCB_SPI_ID | 1U),
324
325 /**
326 * SPI is busy processing a transfer.
327 */
328 CY_SCB_SPI_TRANSFER_BUSY = (CY_SCB_ID | CY_PDL_STATUS_ERROR | CY_SCB_SPI_ID | 2U)
329 } cy_en_scb_spi_status_t;
330
331 /** SPI Modes */
332 typedef enum
333 {
334 CY_SCB_SPI_SLAVE, /**< Configures SCB for SPI Slave operation */
335 CY_SCB_SPI_MASTER, /**< Configures SCB for SPI Master operation */
336 } cy_en_scb_spi_mode_t;
337
338 /** SPI Submodes */
339 typedef enum
340 {
341 /** Configures an SPI for a standard Motorola SPI operation */
342 CY_SCB_SPI_MOTOROLA = 0x0U,
343
344 /**
345 * Configures the SPI for the TI SPI operation. In the TI mode, the slave
346 * select is a pulse. In this case, the pulse coincides with the first bit.
347 */
348 CY_SCB_SPI_TI_COINCIDES = 0x01U,
349
350 /**
351 * Configures an SPI for the National SPI operation. This is a half-duplex
352 * mode of operation.
353 */
354 CY_SCB_SPI_NATIONAL = 0x02U,
355
356 /**
357 * Configures an SPI for the TI SPI operation, in the TI mode. The slave
358 * select is a pulse. In this case the pulse precedes the first bit.
359 */
360 CY_SCB_SPI_TI_PRECEDES = 0x05U,
361 } cy_en_scb_spi_sub_mode_t;
362
363 /** SPI SCLK Modes */
364 typedef enum
365 {
366 CY_SCB_SPI_CPHA0_CPOL0 = 0U, /**< Clock is active low, data is changed on first edge */
367 CY_SCB_SPI_CPHA1_CPOL0 = 1U, /**< Clock is active low, data is changed on second edge */
368 CY_SCB_SPI_CPHA0_CPOL1 = 2U, /**< Clock is active high, data is changed on first edge */
369 CY_SCB_SPI_CPHA1_CPOL1 = 3U, /**< Clock is active high, data is changed on second edge */
370 } cy_en_scb_spi_sclk_mode_t;
371
372 /** SPI Slave Selects */
373 typedef enum
374 {
375 CY_SCB_SPI_SLAVE_SELECT0 = 0U, /**< Master will use Slave Select 0 */
376 CY_SCB_SPI_SLAVE_SELECT1 = 1U, /**< Master will use Slave Select 1 */
377 CY_SCB_SPI_SLAVE_SELECT2 = 2U, /**< Master will use Slave Select 2 */
378 CY_SCB_SPI_SLAVE_SELECT3 = 3U, /**< Master will use Slave Select 3 */
379 } cy_en_scb_spi_slave_select_t;
380
381 /** SPI Polarity */
382 typedef enum
383 {
384 CY_SCB_SPI_ACTIVE_LOW = 0U, /**< Signal in question is active low */
385 CY_SCB_SPI_ACTIVE_HIGH = 1U, /**< Signal in question is active high */
386 } cy_en_scb_spi_polarity_t;
387
388 /** SPI Parity */
389 #if(((defined (CY_IP_MXSCB_VERSION) && (CY_IP_MXSCB_VERSION>=2)) || defined (CY_IP_MXS22SCB)) || defined (CY_DOXYGEN))
390 /**
391 * \note
392 * This enum is available for CAT1B, CAT1C and CAT1D devices.
393 **/
394 typedef enum
395 {
396 CY_SCB_SPI_PARITY_NONE = 0U, /**< SPI has no parity check */
397 CY_SCB_SPI_PARITY_EVEN = 2U, /**< SPI has even parity check */
398 CY_SCB_SPI_PARITY_ODD = 3U, /**< SPI has odd parity check */
399 } cy_en_scb_spi_parity_t;
400 #endif /* CY_IP_MXSCB_VERSION */
401
402 /** \} group_scb_spi_enums */
403
404
405 /*******************************************************************************
406 * Type Definitions
407 *******************************************************************************/
408
409 /**
410 * \addtogroup group_scb_spi_data_structures
411 * \{
412 */
413
414 /**
415 * Provides the typedef for the callback function called in the
416 * \ref Cy_SCB_SPI_Interrupt to notify the user about occurrences of
417 * \ref group_scb_spi_macros_callback_events.
418 */
419 typedef void (* cy_cb_scb_spi_handle_events_t)(uint32_t event);
420
421
422 /** SPI configuration structure */
423 typedef struct cy_stc_scb_spi_config
424 {
425 /** Specifies the mode of operation */
426 cy_en_scb_spi_mode_t spiMode;
427
428 /** Specifies the submode of SPI operation */
429 cy_en_scb_spi_sub_mode_t subMode;
430
431 /**
432 * Configures the SCLK operation for Motorola sub-mode, ignored for all
433 * other submodes
434 */
435 cy_en_scb_spi_sclk_mode_t sclkMode;
436 #if(((defined (CY_IP_MXSCB_VERSION) && (CY_IP_MXSCB_VERSION>=2)) || defined (CY_IP_MXS22SCB)) || defined (CY_DOXYGEN))
437 /** Configures the SPI parity */
438 /**
439 * \note
440 * This parameter is available for CAT1B, CAT1C and CAT1D devices.
441 **/
442 cy_en_scb_spi_parity_t parity;
443 /**
444 * Enables the hardware to drop data in the RX FIFO when a parity error is
445 * detected
446 */
447 /**
448 * \note
449 * This parameter is available for CAT1B, CAT1C and CAT1D devices.
450 **/
451 bool dropOnParityError;
452 #endif /* CY_IP_MXSCB_VERSION */
453 /**
454 * Oversample factor for SPI.
455 * * For the master mode, the data rate is the SCB clock / oversample
456 * (the valid range is 4 to 16, when MISO is used; if MISO is not used
457 * then the valid range is 2 to 16).
458 * * For the slave mode, the oversample value is ignored. The data rate is
459 * determined by the SCB clock frequency.
460 */
461 uint32_t oversample;
462
463 /**
464 * The width of RX data (valid range 4-32). It must be the same as
465 * \ref txDataWidth except in National sub-mode.
466 */
467 uint32_t rxDataWidth;
468
469 /**
470 * The width of TX data (valid range 4-32). It must be the same as
471 * \ref rxDataWidth except in National sub-mode.
472 */
473 uint32_t txDataWidth;
474
475 /**
476 * Enables the hardware to shift out the data element MSB first, otherwise,
477 * LSB first
478 */
479 bool enableMsbFirst;
480
481 /**
482 * Enables the master to generate a continuous SCLK regardless of whether
483 * there is data to send
484 */
485 bool enableFreeRunSclk;
486
487 /**
488 * Enables a digital 3-tap median filter to be applied to the input
489 * of the RX FIFO to filter glitches on the line.
490 */
491 bool enableInputFilter;
492
493 /**
494 * Enables the master to sample MISO line one half clock later to allow
495 * better timings.
496 */
497 bool enableMisoLateSample;
498
499 /**
500 * Enables the master to transmit each data element separated by a
501 * de-assertion of the slave select line (only applicable for the master
502 * mode)
503 */
504 bool enableTransferSeperation;
505
506 /**
507 * Sets active polarity of each SS line.
508 * This is a bit mask: bit 0 corresponds to SS0 and so on to SS3.
509 * 1 means active high, a 0 means active low.
510 */
511 uint32_t ssPolarity;
512
513 #if(((defined (CY_IP_MXSCB_VERSION) && (CY_IP_MXSCB_VERSION>=2)) || defined (CY_IP_MXS22SCB)) || defined (CY_DOXYGEN))
514 /**
515 * Indicates the SPI SELECT setup delay (between SELECT activation and SPI clock).
516 * '0': With this setting the same timing is generated as in SCB v1 block.
517 * 0.75 SPI clock cycles
518 * '1': With this setting an additional delay of 1 SPI clock cycle is generated.
519 * 1.75 SPI clock cycles
520 * Only applies in SPI MOTOROLA submode, when SCLK_CONTINUOUS=0 and oversampling factor>2.
521 */
522 /**
523 * \note
524 * This parameter is available for CAT1B, CAT1C and CAT1D devices.
525 **/
526 bool ssSetupDelay;
527
528 /**
529 * Indicates the SPI SELECT hold delay (between SPI clock and SELECT deactivation).
530 * '0': With this setting the same timing is generated as in CAT1A devices.
531 * 0.75 SPI clock cycles
532 * '1': With this setting an additional delay of 1 SPI clock cycle is generated.
533 * 1.75 SPI clock cycles
534 * Only applies in SPI MOTOROLA submode, when SCLK_CONTINUOUS=0 and oversampling factor>2.
535 */
536 /**
537 * \note
538 * This parameter is available for CAT1B, CAT1C and CAT1D devices.
539 **/
540 bool ssHoldDelay;
541
542 /**
543 * Indicates the SPI SELECT inter-dataframe delay (between SELECT deactivation and SELECT activation).
544 * '0': With this setting the same timing is generated as in CAT1A devices.
545 * 1.5 SPI clock cycles
546 * '1': With this setting an additional delay of 1 SPI clock cycle is generated.
547 * 2.5 SPI clock cycles
548 * Only applies in SPI MOTOROLA submode, when SCLK_CONTINUOUS=0 and oversampling factor>2.
549 */
550 /**
551 * \note
552 * This parameter is available for CAT1B, CAT1C and CAT1D devices.
553 **/
554 bool ssInterFrameDelay;
555 #endif /* CY_IP_MXSCB_VERSION */
556
557 /**
558 * When set, the slave will wake the device when the slave select line
559 * becomes active.
560 * Note that not all SCBs support this mode. Consult the device
561 * datasheet to determine which SCBs support wake from Deep Sleep.
562 */
563 bool enableWakeFromSleep;
564
565 /**
566 * When there are more entries in the RX FIFO then this level
567 * the RX trigger output goes high. This output can be connected
568 * to a DMA channel through a trigger mux.
569 * Also, it controls the \ref CY_SCB_SPI_RX_TRIGGER interrupt source.
570 */
571 uint32_t rxFifoTriggerLevel;
572
573 /**
574 * Bits set in this mask will allow events to cause an interrupt
575 * (See \ref group_scb_spi_macros_rx_fifo_status for the set of constant)
576 */
577 uint32_t rxFifoIntEnableMask;
578
579 /**
580 * When there are fewer entries in the TX FIFO then this level
581 * the TX trigger output goes high. This output can be connected
582 * to a DMA channel through a trigger mux.
583 * Also, it controls the \ref CY_SCB_SPI_TX_TRIGGER interrupt source.
584 */
585 uint32_t txFifoTriggerLevel;
586
587 /**
588 * Bits set in this mask allow events to cause an interrupt
589 * (See \ref group_scb_spi_macros_tx_fifo_status for the set of constants)
590 */
591 uint32_t txFifoIntEnableMask;
592
593 /**
594 * Bits set in this mask allow events to cause an interrupt
595 * (See \ref group_scb_spi_macros_master_slave_status for the set of
596 * constants)
597 */
598 uint32_t masterSlaveIntEnableMask;
599
600 }cy_stc_scb_spi_config_t;
601
602 /** SPI context structure.
603 * All fields for the context structure are internal. Firmware never reads or
604 * writes these values. Firmware allocates the structure and provides the
605 * address of the structure to the driver in function calls. Firmware must
606 * ensure that the defined instance of this structure remains in scope
607 * while the drive is in use.
608 */
609 typedef struct cy_stc_scb_spi_context
610 {
611 /** \cond INTERNAL */
612 uint32_t volatile status; /**< The receive status */
613
614 void *rxBuf; /**< The pointer to the receive buffer */
615 uint32_t rxBufSize; /**< The receive buffer size */
616 uint32_t volatile rxBufIdx; /**< The current location in the receive buffer */
617
618 void *txBuf; /**< The pointer to the transmit buffer */
619 uint32_t txBufSize; /**< The transmit buffer size */
620 uint32_t volatile txBufIdx; /**< The current location in the transmit buffer */
621
622 uint32_t volatile WriteFillSize; /**< If rxSize is greater than txSize, the difference of receive and transmit buffer size */
623 uint32_t volatile DiscardRxSize; /**< If txSize is greater than rxSize, the difference of transmit and receive buffer size */
624 uint32_t writeFill; /**< If rxSize is greater than txSize, value to be used for filling end of tx data array. */
625
626 /**
627 * The pointer to an event callback that is called when any of
628 * \ref group_scb_spi_macros_callback_events occurs
629 */
630 cy_cb_scb_spi_handle_events_t cbEvents;
631
632 #if !defined(NDEBUG)
633 uint32_t initKey; /**< Tracks the context initialization */
634 #endif /* !(NDEBUG) */
635 /** \endcond */
636 } cy_stc_scb_spi_context_t;
637
638 /** \} group_scb_spi_data_structures */
639
640
641 /*******************************************************************************
642 * Function Prototypes
643 *******************************************************************************/
644
645 /**
646 * \addtogroup group_scb_spi_general_functions
647 * \{
648 */
649 cy_en_scb_spi_status_t Cy_SCB_SPI_Init(CySCB_Type *base, cy_stc_scb_spi_config_t const *config,
650 cy_stc_scb_spi_context_t *context);
651 void Cy_SCB_SPI_DeInit (CySCB_Type *base);
652 __STATIC_INLINE void Cy_SCB_SPI_Enable(CySCB_Type *base);
653 void Cy_SCB_SPI_Disable(CySCB_Type *base, cy_stc_scb_spi_context_t *context);
654
655 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelect(CySCB_Type *base,
656 cy_en_scb_spi_slave_select_t slaveSelect);
657 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelectPolarity(CySCB_Type *base,
658 cy_en_scb_spi_slave_select_t slaveSelect,
659 cy_en_scb_spi_polarity_t polarity);
660
661 __STATIC_INLINE bool Cy_SCB_SPI_IsBusBusy(CySCB_Type const *base);
662 /** \} group_scb_spi_general_functions */
663
664 /**
665 * \addtogroup group_scb_spi_high_level_functions
666 * \{
667 */
668 cy_en_scb_spi_status_t Cy_SCB_SPI_Transfer(CySCB_Type *base, void *txBuffer, void *rxBuffer, uint32_t size,
669 cy_stc_scb_spi_context_t *context);
670 cy_en_scb_spi_status_t Cy_SCB_SPI_Transfer_Buffer(CySCB_Type *base, void *txBuffer, void *rxBuffer,
671 uint32_t txSize, uint32_t rxSize, uint32_t writeFill,
672 cy_stc_scb_spi_context_t *context);
673 void Cy_SCB_SPI_AbortTransfer (CySCB_Type *base, cy_stc_scb_spi_context_t *context);
674 uint32_t Cy_SCB_SPI_GetTransferStatus(CySCB_Type const *base, cy_stc_scb_spi_context_t const *context);
675 uint32_t Cy_SCB_SPI_GetNumTransfered (CySCB_Type const *base, cy_stc_scb_spi_context_t const *context);
676 /** \} group_scb_spi_high_level_functions */
677
678 /**
679 * \addtogroup group_scb_spi_low_level_functions
680 * \{
681 */
682 __STATIC_INLINE uint32_t Cy_SCB_SPI_Read (CySCB_Type const *base);
683 __STATIC_INLINE uint32_t Cy_SCB_SPI_ReadArray(CySCB_Type const *base, void *buffer, uint32_t size);
684
685 __STATIC_INLINE uint32_t Cy_SCB_SPI_Write (CySCB_Type *base, uint32_t data);
686 __STATIC_INLINE uint32_t Cy_SCB_SPI_WriteArray(CySCB_Type *base, void *buffer, uint32_t size);
687 __STATIC_INLINE void Cy_SCB_SPI_WriteArrayBlocking(CySCB_Type *base, void *buffer, uint32_t size);
688
689 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetTxFifoStatus (CySCB_Type const *base);
690 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifoStatus(CySCB_Type *base, uint32_t clearMask);
691
692 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetRxFifoStatus (CySCB_Type const *base);
693 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifoStatus(CySCB_Type *base, uint32_t clearMask);
694
695 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetSlaveMasterStatus (CySCB_Type const *base);
696 __STATIC_INLINE void Cy_SCB_SPI_ClearSlaveMasterStatus(CySCB_Type *base, uint32_t clearMask);
697
698 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInTxFifo(CySCB_Type const *base);
699 __STATIC_INLINE bool Cy_SCB_SPI_IsTxComplete (CySCB_Type const *base);
700
701 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInRxFifo(CySCB_Type const *base);
702
703 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifo(CySCB_Type *base);
704 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifo(CySCB_Type *base);
705 /** \} group_scb_spi_low_level_functions */
706
707 /**
708 * \addtogroup group_scb_spi_interrupt_functions
709 * \{
710 */
711 void Cy_SCB_SPI_Interrupt(CySCB_Type *base, cy_stc_scb_spi_context_t *context);
712
713 __STATIC_INLINE void Cy_SCB_SPI_RegisterCallback(CySCB_Type const *base, cy_cb_scb_spi_handle_events_t callback,
714 cy_stc_scb_spi_context_t *context);
715 /** \} group_scb_spi_interrupt_functions */
716
717 /**
718 * \addtogroup group_scb_spi_low_power_functions
719 * \{
720 */
721 cy_en_syspm_status_t Cy_SCB_SPI_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode);
722 cy_en_syspm_status_t Cy_SCB_SPI_HibernateCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode);
723 /** \} group_scb_spi_low_power_functions */
724
725
726 /*******************************************************************************
727 * API Constants
728 *******************************************************************************/
729
730 /**
731 * \addtogroup group_scb_spi_macros
732 * \{
733 */
734
735 /**
736 * \defgroup group_scb_spi_macros_tx_fifo_status SPI TX FIFO Statuses
737 * Macros to check SPI TX FIFO status returned by \ref Cy_SCB_SPI_GetTxFifoStatus
738 * function or assign mask for \ref Cy_SCB_SPI_ClearTxFifoStatus function.
739 * Each SPI TX FIFO status is encoded in a separate bit, therefore multiple bits
740 * may be set to indicate the current status.
741 * \{
742 */
743 /** The number of entries in the TX FIFO is less than the TX FIFO trigger level
744 * value
745 */
746 #define CY_SCB_SPI_TX_TRIGGER (SCB_INTR_TX_TRIGGER_Msk)
747
748 /** The TX FIFO is not full, there is a space for more data */
749 #define CY_SCB_SPI_TX_NOT_FULL (SCB_INTR_TX_NOT_FULL_Msk)
750
751 /**
752 * The TX FIFO is empty, note that there may still be data in the shift register.
753 */
754 #define CY_SCB_SPI_TX_EMPTY (SCB_INTR_TX_EMPTY_Msk)
755
756 /** An attempt to write to the full TX FIFO */
757 #define CY_SCB_SPI_TX_OVERFLOW (SCB_INTR_TX_OVERFLOW_Msk)
758
759 /**
760 * Applicable only for the slave mode. The master tried to read more
761 * data elements than available.
762 */
763 #define CY_SCB_SPI_TX_UNDERFLOW (SCB_INTR_TX_UNDERFLOW_Msk)
764 /** \} group_scb_spi_macros_tx_fifo_status */
765
766 /**
767 * \defgroup group_scb_spi_macros_rx_fifo_status SPI RX FIFO Statuses
768 * \{
769 * Macros to check SPI RX FIFO status returned by \ref Cy_SCB_SPI_GetRxFifoStatus
770 * function or assign mask for \ref Cy_SCB_SPI_ClearRxFifoStatus function.
771 * Each SPI RX FIFO status is encoded in a separate bit, therefore multiple
772 * bits may be set to indicate the current status.
773 */
774 /** The number of entries in the RX FIFO is more than the RX FIFO trigger
775 * level value.
776 */
777 #define CY_SCB_SPI_RX_TRIGGER (SCB_INTR_RX_TRIGGER_Msk)
778
779 /** The RX FIFO is not empty, there is data to read */
780 #define CY_SCB_SPI_RX_NOT_EMPTY (SCB_INTR_RX_NOT_EMPTY_Msk)
781
782 /**
783 * The RX FIFO is full. There is no more space for additional data.
784 * Any additional data will be dropped.
785 */
786 #define CY_SCB_SPI_RX_FULL (SCB_INTR_RX_FULL_Msk)
787
788 /**
789 * The RX FIFO was full and there was an attempt to write to it.
790 * This additional data was dropped.
791 */
792 #define CY_SCB_SPI_RX_OVERFLOW (SCB_INTR_RX_OVERFLOW_Msk)
793
794 /** An attempt to read from an empty RX FIFO */
795 #define CY_SCB_SPI_RX_UNDERFLOW (SCB_INTR_RX_UNDERFLOW_Msk)
796 /** \} group_scb_spi_macros_rx_fifo_status */
797
798 /**
799 * \defgroup group_scb_spi_macros_master_slave_status SPI Master and Slave Statuses
800 * \{
801 * Macros to check SPI Mater and Slave status returned by \ref Cy_SCB_SPI_GetSlaveMasterStatus
802 * function or assign mask for \ref Cy_SCB_SPI_ClearSlaveMasterStatus function.
803 */
804 /** The slave was deselected at the wrong time */
805 #define CY_SCB_SPI_SLAVE_ERR (SCB_INTR_S_SPI_BUS_ERROR_Msk)
806
807 /** The master has transmitted all data elements from FIFO and shifter */
808 #define CY_SCB_SPI_MASTER_DONE (SCB_INTR_M_SPI_DONE_Msk)
809 /** \} group_scb_spi_macros_master_slave_status */
810
811 /**
812 * \defgroup group_scb_spi_macros_xfer_status SPI Transfer Status
813 * \{
814 * Macros to check current SPI transfer status returned by
815 * \ref Cy_SCB_SPI_GetTransferStatus function.
816 * Each SPI transfer status is encoded in a separate bit, therefore multiple bits
817 * may be set to indicate the current status.
818 */
819 /**
820 * Transfer operation started by \ref Cy_SCB_SPI_Transfer is in progress
821 */
822 #define CY_SCB_SPI_TRANSFER_ACTIVE (0x01UL)
823
824 /**
825 * All data elements specified by \ref Cy_SCB_SPI_Transfer for transmission
826 * have been loaded into the TX FIFO
827 */
828 #define CY_SCB_SPI_TRANSFER_IN_FIFO (0x02UL)
829
830 /** The slave was deselected at the wrong time. */
831 #define CY_SCB_SPI_SLAVE_TRANSFER_ERR (SCB_INTR_S_SPI_BUS_ERROR_Msk)
832
833 /**
834 * RX FIFO was full and there was an attempt to write to it.
835 * This additional data was dropped.
836 */
837 #define CY_SCB_SPI_TRANSFER_OVERFLOW (SCB_INTR_RX_OVERFLOW_Msk)
838
839 /**
840 * Applicable only for the slave mode. The master tried to read more
841 * data elements than available in the TX FIFO.
842 */
843 #define CY_SCB_SPI_TRANSFER_UNDERFLOW (SCB_INTR_TX_UNDERFLOW_Msk)
844 /** \} group_scb_spi_macros_xfer_status */
845
846 /**
847 * \defgroup group_scb_spi_macros_callback_events SPI Callback Events
848 * \{
849 * Macros to check SPI events passed by \ref cy_cb_scb_spi_handle_events_t callback.
850 * Note that only single event is notified by the callback when it is called.
851 */
852 /**
853 * All data elements specified by \ref Cy_SCB_SPI_Transfer for transmission
854 * have been loaded into the TX FIFO
855 */
856 #define CY_SCB_SPI_TRANSFER_IN_FIFO_EVENT (0x01U)
857
858 /** The transfer operation started by \ref Cy_SCB_SPI_Transfer is complete */
859 #define CY_SCB_SPI_TRANSFER_CMPLT_EVENT (0x02U)
860
861 /**
862 * An error occurred during the transfer. This includes overflow, underflow
863 * and a transfer error. Check \ref Cy_SCB_SPI_GetTransferStatus.
864 */
865 #define CY_SCB_SPI_TRANSFER_ERR_EVENT (0x04U)
866 /** \} group_scb_spi_macros_callback_events */
867
868
869 /** Default TX value when no TX buffer is defined */
870 #define CY_SCB_SPI_DEFAULT_TX (0x0000FFFFUL)
871
872 /** Data returned by the hardware when an empty RX FIFO is read */
873 #define CY_SCB_SPI_RX_NO_DATA (0xFFFFFFFFUL)
874
875
876 /*******************************************************************************
877 * Internal Constants
878 *******************************************************************************/
879
880 /** \cond INTERNAL */
881 #define CY_SCB_SPI_RX_INTR_MASK (CY_SCB_SPI_RX_TRIGGER | CY_SCB_SPI_RX_NOT_EMPTY | CY_SCB_SPI_RX_FULL | \
882 CY_SCB_SPI_RX_OVERFLOW | CY_SCB_SPI_RX_UNDERFLOW)
883
884 #define CY_SCB_SPI_TX_INTR_MASK (CY_SCB_SPI_TX_TRIGGER | CY_SCB_SPI_TX_NOT_FULL | CY_SCB_SPI_TX_EMPTY | \
885 CY_SCB_SPI_TX_OVERFLOW | CY_SCB_SPI_TX_UNDERFLOW)
886
887 #define CY_SCB_SPI_MASTER_SLAVE_INTR_MASK (CY_SCB_SPI_MASTER_DONE | CY_SCB_SPI_SLAVE_ERR)
888
889 #define CY_SCB_SPI_TRANSFER_ERR (CY_SCB_SPI_SLAVE_TRANSFER_ERR | CY_SCB_SPI_TRANSFER_OVERFLOW | \
890 CY_SCB_SPI_TRANSFER_UNDERFLOW)
891
892 #define CY_SCB_SPI_INIT_KEY (0x00ABCDEFUL)
893
894 #define CY_SCB_SPI_IS_MODE_VALID(mode) ( (CY_SCB_SPI_SLAVE == (mode)) || \
895 (CY_SCB_SPI_MASTER == (mode)) )
896
897 #define CY_SCB_SPI_IS_SUB_MODE_VALID(subMode) ( (CY_SCB_SPI_MOTOROLA == (subMode)) || \
898 (CY_SCB_SPI_TI_COINCIDES == (subMode)) || \
899 (CY_SCB_SPI_TI_PRECEDES == (subMode)) || \
900 (CY_SCB_SPI_NATIONAL == (subMode)) )
901
902 #define CY_SCB_SPI_IS_SCLK_MODE_VALID(clkMode) ( (CY_SCB_SPI_CPHA0_CPOL0 == (clkMode)) || \
903 (CY_SCB_SPI_CPHA0_CPOL1 == (clkMode)) || \
904 (CY_SCB_SPI_CPHA1_CPOL0 == (clkMode)) || \
905 (CY_SCB_SPI_CPHA1_CPOL1 == (clkMode)) )
906
907 #define CY_SCB_SPI_IS_POLARITY_VALID(polarity) ( (CY_SCB_SPI_ACTIVE_LOW == (polarity)) || \
908 (CY_SCB_SPI_ACTIVE_HIGH == (polarity)) )
909
910 #define CY_SCB_SPI_IS_SLAVE_SEL_VALID(ss) ( (CY_SCB_SPI_SLAVE_SELECT0 == (ss)) || \
911 (CY_SCB_SPI_SLAVE_SELECT1 == (ss)) || \
912 (CY_SCB_SPI_SLAVE_SELECT2 == (ss)) || \
913 (CY_SCB_SPI_SLAVE_SELECT3 == (ss)) )
914
915 #define CY_SCB_SPI_IS_OVERSAMPLE_VALID(ovs, mode) ( (CY_SCB_SPI_MASTER == (mode)) ? (((ovs) >= 2UL) && ((ovs) <= 16UL)) : true )
916 #define CY_SCB_SPI_IS_DATA_WIDTH_VALID(width) ( ((width) >= 4UL) && ((width) <= 32UL) )
917 #define CY_SCB_SPI_IS_SS_POLARITY_VALID(polarity) ( (0UL == ((polarity) & (~0x0FUL))) )
918 #define CY_SCB_SPI_IS_BUFFER_VALID(txBuffer, rxBuffer, size) ( ((size) > 0UL) && \
919 (false == ((NULL == (txBuffer)) && (NULL == (rxBuffer)))) )
920 #define CY_SCB_SPI_IS_TX_RX_BUFFER_VALID(txBuffer, rxBuffer, txSize, rxSize) ( (false == (((txSize) <= 0UL) && ((rxSize) <= 0UL))) && \
921 (false == ((NULL == (txBuffer)) && (NULL == (rxBuffer)))) )
922
923 #define CY_SCB_SPI_IS_BOTH_DATA_WIDTH_VALID(subMode, rxWidth, txWidth) ( (CY_SCB_SPI_NATIONAL != (subMode)) ? \
924 ((rxWidth) == (txWidth)) : true )
925
926 /** \endcond */
927
928 /** \} group_scb_spi_macros */
929
930
931 /*******************************************************************************
932 * In-line Function Implementation
933 *******************************************************************************/
934
935 /**
936 * \addtogroup group_scb_spi_general_functions
937 * \{
938 */
939
940 /*******************************************************************************
941 * Function Name: Cy_SCB_SPI_Enable
942 ****************************************************************************//**
943 *
944 * Enables the SCB block for the SPI operation.
945 *
946 * \param base
947 * The pointer to the SPI SCB instance.
948 *
949 *******************************************************************************/
Cy_SCB_SPI_Enable(CySCB_Type * base)950 __STATIC_INLINE void Cy_SCB_SPI_Enable(CySCB_Type *base)
951 {
952 SCB_CTRL(base) |= SCB_CTRL_ENABLED_Msk;
953 }
954
955
956 /*******************************************************************************
957 * Function Name: Cy_SCB_SPI_IsBusBusy
958 ****************************************************************************//**
959 *
960 * Returns whether the SPI bus is busy or not. The bus busy is determined using
961 * the slave select signal.
962 * * Motorola and National Semiconductor sub-modes: the bus is busy after the
963 * slave select line is activated and lasts until the slave select line is
964 * deactivated.
965 * * Texas Instrument sub-modes: The bus is busy the moment of the initial
966 * pulse on the slave select line and lasts until the transfer is complete
967 * (all bytes from the TX FIFO area shifted-out on the bus).
968 *
969 * \param base
970 * The pointer to the SPI SCB instance.
971 *
972 * \return
973 * True - the bus is busy; false - the bus is idle.
974 *
975 * \note
976 * * The SPI master does not assign the slave select line immediately after
977 * the first data element is written into the TX FIFO. It takes up to two SCLK
978 * clocks to assign the slave select line. Before this happens, the bus
979 * is considered idle.
980 * * If the SPI master is configured to transmit each data element separated by
981 * a de-assertion of the slave select line, the bus is busy during each element
982 * transfer and is free between them.
983 *
984 *******************************************************************************/
Cy_SCB_SPI_IsBusBusy(CySCB_Type const * base)985 __STATIC_INLINE bool Cy_SCB_SPI_IsBusBusy(CySCB_Type const *base)
986 {
987 return _FLD2BOOL(SCB_SPI_STATUS_BUS_BUSY, SCB_SPI_STATUS(base));
988 }
989
990
991 /*******************************************************************************
992 * Function Name: Cy_SCB_SPI_SetActiveSlaveSelect
993 ****************************************************************************//**
994 *
995 * Selects an active slave select line from one of four available.
996 * This function is applicable for the master and slave.
997 *
998 * \param base
999 * The pointer to the SPI SCB instance.
1000 *
1001 * \param slaveSelect
1002 * The slave select line number.
1003 * See \ref cy_en_scb_spi_slave_select_t for the set of constants.
1004 *
1005 * \note
1006 * The SCB must be idle or disabled before calling this function.
1007 *
1008 *******************************************************************************/
Cy_SCB_SPI_SetActiveSlaveSelect(CySCB_Type * base,cy_en_scb_spi_slave_select_t slaveSelect)1009 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelect(CySCB_Type *base, cy_en_scb_spi_slave_select_t slaveSelect)
1010 {
1011 CY_ASSERT_L3(CY_SCB_SPI_IS_SLAVE_SEL_VALID(slaveSelect));
1012
1013 CY_REG32_CLR_SET(SCB_SPI_CTRL(base), SCB_SPI_CTRL_SSEL, (uint32_t) slaveSelect);
1014 }
1015
1016
1017 /*******************************************************************************
1018 * Function Name: Cy_SCB_SPI_SetActiveSlaveSelectPolarity
1019 ****************************************************************************//**
1020 *
1021 * Sets the active polarity for the slave select line.
1022 *
1023 * \param base
1024 * The pointer to the SPI SCB instance.
1025 *
1026 * \param slaveSelect
1027 * The slave select line number.
1028 * See \ref cy_en_scb_spi_slave_select_t for the set of constants.
1029 *
1030 * \param polarity
1031 * The polarity of the slave select line.
1032 * See \ref cy_en_scb_spi_polarity_t for the set of constants.
1033 *
1034 * \note
1035 * The SCB must be idle or disabled before calling this function.
1036 *
1037 *******************************************************************************/
Cy_SCB_SPI_SetActiveSlaveSelectPolarity(CySCB_Type * base,cy_en_scb_spi_slave_select_t slaveSelect,cy_en_scb_spi_polarity_t polarity)1038 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelectPolarity(CySCB_Type *base,
1039 cy_en_scb_spi_slave_select_t slaveSelect,
1040 cy_en_scb_spi_polarity_t polarity)
1041 {
1042 uint32_t mask = _VAL2FLD(CY_SCB_SPI_CTRL_SSEL_POLARITY, (0x01UL << ((uint32_t)slaveSelect)));
1043
1044 CY_ASSERT_L3(CY_SCB_SPI_IS_SLAVE_SEL_VALID(slaveSelect));
1045 CY_ASSERT_L3(CY_SCB_SPI_IS_POLARITY_VALID (polarity));
1046
1047 if (CY_SCB_SPI_ACTIVE_HIGH == polarity)
1048 {
1049 SCB_SPI_CTRL(base) |= (uint32_t) mask;
1050 }
1051 else
1052 {
1053 SCB_SPI_CTRL(base) &= (uint32_t) ~mask;
1054 }
1055 }
1056 /** \} group_scb_spi_general_functions */
1057
1058
1059 /**
1060 * \addtogroup group_scb_spi_low_level_functions
1061 * \{
1062 */
1063 /*******************************************************************************
1064 * Function Name: Cy_SCB_SPI_GetRxFifoStatus
1065 ****************************************************************************//**
1066 *
1067 * Returns the current status of the RX FIFO.
1068 *
1069 * \param base
1070 * The pointer to the SPI SCB instance.
1071 *
1072 * \return
1073 * \ref group_scb_spi_macros_rx_fifo_status
1074 *
1075 *******************************************************************************/
Cy_SCB_SPI_GetRxFifoStatus(CySCB_Type const * base)1076 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetRxFifoStatus(CySCB_Type const *base)
1077 {
1078 return (Cy_SCB_GetRxInterruptStatus(base) & CY_SCB_SPI_RX_INTR_MASK);
1079 }
1080
1081
1082 /*******************************************************************************
1083 * Function Name: Cy_SCB_SPI_ClearRxFifoStatus
1084 ****************************************************************************//**
1085 *
1086 * Clears the selected statuses of the RX FIFO.
1087 *
1088 * \param base
1089 * The pointer to the SPI SCB instance.
1090 *
1091 * \param clearMask
1092 * The mask of which statuses to clear.
1093 * See \ref group_scb_spi_macros_rx_fifo_status for the set of constants.
1094 *
1095 * \note
1096 * * This status is also used for interrupt generation, so clearing it also
1097 * clears the interrupt sources.
1098 * * Level sensitive statuses such as \ref CY_SCB_SPI_RX_TRIGGER,
1099 * \ref CY_SCB_SPI_RX_NOT_EMPTY and \ref CY_SCB_SPI_RX_FULL set high again after
1100 * being cleared if the condition remains true.
1101 *
1102 *******************************************************************************/
Cy_SCB_SPI_ClearRxFifoStatus(CySCB_Type * base,uint32_t clearMask)1103 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifoStatus(CySCB_Type *base, uint32_t clearMask)
1104 {
1105 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_SPI_RX_INTR_MASK));
1106
1107 Cy_SCB_ClearRxInterrupt(base, clearMask);
1108 }
1109
1110
1111 /*******************************************************************************
1112 * Function Name: Cy_SCB_SPI_GetNumInRxFifo
1113 ****************************************************************************//**
1114 *
1115 * Returns the number of data elements in the SPI RX FIFO.
1116 *
1117 * \param base
1118 * The pointer to the SPI SCB instance.
1119 *
1120 * \return
1121 * The number of data elements in the RX FIFO.
1122 * The size of a data element defined by the configured RX data width.
1123 *
1124 * \note
1125 * This number does not include any data currently in the RX shifter.
1126 *
1127 *******************************************************************************/
Cy_SCB_SPI_GetNumInRxFifo(CySCB_Type const * base)1128 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInRxFifo(CySCB_Type const *base)
1129 {
1130 return Cy_SCB_GetNumInRxFifo(base);
1131 }
1132
1133
1134 /*******************************************************************************
1135 * Function Name: Cy_SCB_SPI_ClearRxFifo
1136 ****************************************************************************//**
1137 *
1138 * Clears all data out of the SPI RX FIFO.
1139 *
1140 * \param base
1141 * The pointer to the SPI SCB instance.
1142 *
1143 * \sideeffect
1144 * Any data currently in the shifter is cleared and lost.
1145 *
1146 *******************************************************************************/
Cy_SCB_SPI_ClearRxFifo(CySCB_Type * base)1147 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifo(CySCB_Type *base)
1148 {
1149 Cy_SCB_ClearRxFifo(base);
1150 }
1151
1152
1153 /*******************************************************************************
1154 * Function Name: Cy_SCB_SPI_GetTxFifoStatus
1155 ****************************************************************************//**
1156 *
1157 * Returns the current status of the TX FIFO.
1158 *
1159 * \param base
1160 * The pointer to the SPI SCB instance.
1161 *
1162 * \return
1163 * \ref group_scb_spi_macros_tx_fifo_status
1164 *
1165 *******************************************************************************/
Cy_SCB_SPI_GetTxFifoStatus(CySCB_Type const * base)1166 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetTxFifoStatus(CySCB_Type const *base)
1167 {
1168 return (Cy_SCB_GetTxInterruptStatus(base) & CY_SCB_SPI_TX_INTR_MASK);
1169 }
1170
1171
1172 /*******************************************************************************
1173 * Function Name: Cy_SCB_SPI_ClearTxFifoStatus
1174 ****************************************************************************//**
1175 *
1176 * Clears the selected statuses of the TX FIFO.
1177 *
1178 * \param base
1179 * The pointer to the SPI SCB instance.
1180 *
1181 * \param clearMask
1182 * The mask of which statuses to clear.
1183 * See \ref group_scb_spi_macros_tx_fifo_status for the set of constants.
1184 *
1185 * \note
1186 * * The status is also used for interrupt generation, so clearing it also
1187 * clears the interrupt sources.
1188 * * Level sensitive statuses such as \ref CY_SCB_SPI_TX_TRIGGER,
1189 * \ref CY_SCB_SPI_TX_EMPTY and \ref CY_SCB_SPI_TX_NOT_FULL set high again after
1190 * being cleared if the condition remains true.
1191 *
1192 *******************************************************************************/
Cy_SCB_SPI_ClearTxFifoStatus(CySCB_Type * base,uint32_t clearMask)1193 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifoStatus(CySCB_Type *base, uint32_t clearMask)
1194 {
1195 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_SPI_TX_INTR_MASK));
1196
1197 Cy_SCB_ClearTxInterrupt(base, clearMask);
1198 }
1199
1200
1201 /*******************************************************************************
1202 * Function Name: Cy_SCB_SPI_GetNumInTxFifo
1203 ****************************************************************************//**
1204 *
1205 * Returns the number of data elements in the SPI TX FIFO.
1206 *
1207 * \param base
1208 * The pointer to the SPI SCB instance.
1209 *
1210 * \return
1211 * The number of data elements in the TX FIFO.
1212 * The size of a data element defined by the configured TX data width.
1213 *
1214 * \note
1215 * This number does not include any data currently in the TX shifter.
1216 *
1217 *******************************************************************************/
Cy_SCB_SPI_GetNumInTxFifo(CySCB_Type const * base)1218 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInTxFifo(CySCB_Type const *base)
1219 {
1220 return Cy_SCB_GetNumInTxFifo(base);
1221 }
1222
1223
1224 /*******************************************************************************
1225 * Function Name: Cy_SCB_SPI_IsTxComplete
1226 ****************************************************************************//**
1227 *
1228 * Checks whether the TX FIFO and Shifter are empty and there is no more data to send
1229 *
1230 * \param base
1231 * Pointer to the SPI SCB instance.
1232 *
1233 * \return
1234 * If true, transmission complete. If false, transmission is not complete.
1235 *
1236 *******************************************************************************/
Cy_SCB_SPI_IsTxComplete(CySCB_Type const * base)1237 __STATIC_INLINE bool Cy_SCB_SPI_IsTxComplete(CySCB_Type const *base)
1238 {
1239 return Cy_SCB_IsTxComplete(base);
1240 }
1241
1242
1243 /*******************************************************************************
1244 * Function Name: Cy_SCB_SPI_ClearTxFifo
1245 ****************************************************************************//**
1246 *
1247 * Clears all data out of the SPI TX FIFO.
1248 *
1249 * \param base
1250 * The pointer to the SPI SCB instance.
1251 *
1252 * \sideeffect
1253 * The TX FIFO clear operation also clears the shift register;
1254 * the shifter can be cleared in the middle of a data element transfer,
1255 * corrupting it. The data element corruption means that all bits that have
1256 * not been transmitted are transmitted as 1s on the bus.
1257 *
1258 *******************************************************************************/
Cy_SCB_SPI_ClearTxFifo(CySCB_Type * base)1259 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifo(CySCB_Type *base)
1260 {
1261 Cy_SCB_ClearTxFifo(base);
1262 }
1263
1264
1265 /*******************************************************************************
1266 * Function Name: Cy_SCB_SPI_GetSlaveMasterStatus
1267 ****************************************************************************//**
1268 *
1269 * Returns the current status of either the slave or the master, depending
1270 * on the configured SPI mode.
1271 *
1272 * \param base
1273 * The pointer to the SPI SCB instance.
1274 *
1275 * \return
1276 * \ref group_scb_spi_macros_master_slave_status
1277 *
1278 *******************************************************************************/
Cy_SCB_SPI_GetSlaveMasterStatus(CySCB_Type const * base)1279 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetSlaveMasterStatus(CySCB_Type const *base)
1280 {
1281 uint32_t retStatus;
1282
1283 if (_FLD2BOOL(SCB_SPI_CTRL_MASTER_MODE, SCB_SPI_CTRL(base)))
1284 {
1285 retStatus = (Cy_SCB_GetMasterInterruptStatus(base) & CY_SCB_MASTER_INTR_SPI_DONE);
1286 }
1287 else
1288 {
1289 retStatus = (Cy_SCB_GetSlaveInterruptStatus(base) & CY_SCB_SLAVE_INTR_SPI_BUS_ERROR);
1290 }
1291
1292 return (retStatus);
1293 }
1294
1295
1296 /*******************************************************************************
1297 * Function Name: Cy_SCB_SPI_ClearSlaveMasterStatus
1298 ****************************************************************************//**
1299 *
1300 * Clears the selected statuses of either the slave or the master.
1301 *
1302 * \param base
1303 * The pointer to the SPI SCB instance.
1304 *
1305 * \param clearMask
1306 * The mask of which statuses to clear.
1307 * See \ref group_scb_spi_macros_master_slave_status for the set of constants.
1308 *
1309 *******************************************************************************/
Cy_SCB_SPI_ClearSlaveMasterStatus(CySCB_Type * base,uint32_t clearMask)1310 __STATIC_INLINE void Cy_SCB_SPI_ClearSlaveMasterStatus(CySCB_Type *base, uint32_t clearMask)
1311 {
1312 if (_FLD2BOOL(SCB_SPI_CTRL_MASTER_MODE, SCB_SPI_CTRL(base)))
1313 {
1314 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_MASTER_INTR_SPI_DONE));
1315
1316 Cy_SCB_ClearMasterInterrupt(base, clearMask);
1317 }
1318 else
1319 {
1320 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_SLAVE_INTR_SPI_BUS_ERROR));
1321
1322 Cy_SCB_ClearSlaveInterrupt(base, clearMask);
1323 }
1324 }
1325
1326
1327 /*******************************************************************************
1328 * Function Name: Cy_SCB_SPI_Read
1329 ****************************************************************************//**
1330 *
1331 * Reads a single data element from the SPI RX FIFO.
1332 * This function does not check whether the RX FIFO has data before reading it.
1333 * If the RX FIFO is empty, the function returns \ref CY_SCB_SPI_RX_NO_DATA.
1334 *
1335 * \param base
1336 * The pointer to the SPI SCB instance.
1337 *
1338 * \return
1339 * Data from the RX FIFO.
1340 * The data element size is defined by the configured RX data width.
1341 *
1342 * \note
1343 * * This function only reads data available in the RX FIFO. It does not
1344 * initiate an SPI transfer.
1345 *
1346 *******************************************************************************/
Cy_SCB_SPI_Read(CySCB_Type const * base)1347 __STATIC_INLINE uint32_t Cy_SCB_SPI_Read(CySCB_Type const *base)
1348 {
1349 return Cy_SCB_ReadRxFifo(base);
1350 }
1351
1352
1353 /*******************************************************************************
1354 * Function Name: Cy_SCB_SPI_ReadArray
1355 ****************************************************************************//**
1356 *
1357 * Reads an array of data out of the SPI RX FIFO.
1358 * This function does not block. It returns how many data elements were read
1359 * from the RX FIFO.
1360 *
1361 * \param base
1362 * The pointer to the SPI SCB instance.
1363 *
1364 * \param buffer
1365 * The pointer to the location to place data read from the RX FIFO.
1366 * The element size is defined by the data type, which depends on the configured
1367 * RX data width.
1368 *
1369 * \param size
1370 * The number of data elements to read from the RX FIFO.
1371 *
1372 * \return
1373 * The number of data elements read from the RX FIFO.
1374 *
1375 * \note
1376 * * This function only reads data available in the RX FIFO. It does not
1377 * initiate an SPI transfer.
1378 *
1379 *******************************************************************************/
Cy_SCB_SPI_ReadArray(CySCB_Type const * base,void * buffer,uint32_t size)1380 __STATIC_INLINE uint32_t Cy_SCB_SPI_ReadArray(CySCB_Type const *base, void *buffer, uint32_t size)
1381 {
1382 CY_ASSERT_L1(CY_SCB_IS_BUFFER_VALID(buffer, size));
1383
1384 return Cy_SCB_ReadArray(base, buffer, size);
1385 }
1386
1387
1388 /*******************************************************************************
1389 * Function Name: Cy_SCB_SPI_Write
1390 ****************************************************************************//**
1391 *
1392 * Places a single data element in the SPI TX FIFO.
1393 * This function does not block. It returns how many data elements were placed
1394 * in the TX FIFO.
1395 *
1396 * \param base
1397 * The pointer to the SPI SCB instance.
1398 *
1399 * \param data
1400 * Data to put in the TX FIFO.
1401 * The element size is defined by the data type, which depends on the configured
1402 * TX data width.
1403 *
1404 * \return
1405 * The number of data elements placed in the TX FIFO: 0 or 1.
1406 *
1407 * \note
1408 * * When in the master mode, writing data into the TX FIFO starts an SPI
1409 * transfer.
1410 * * When in the slave mode, writing data into the TX FIFO does not start
1411 * an SPI transfer. The data is loaded in the TX FIFO and will be sent
1412 * to the master on its request.
1413 * * The SPI interface is full-duplex, therefore reads and writes occur
1414 * at the same time. Thus, for every data element transferred out of the
1415 * TX FIFO, one is transferred into the RX FIFO.
1416 *
1417 *******************************************************************************/
Cy_SCB_SPI_Write(CySCB_Type * base,uint32_t data)1418 __STATIC_INLINE uint32_t Cy_SCB_SPI_Write(CySCB_Type *base, uint32_t data)
1419 {
1420 return Cy_SCB_Write(base, data);
1421 }
1422
1423
1424 /*******************************************************************************
1425 * Function Name: Cy_SCB_SPI_WriteArray
1426 ****************************************************************************//**
1427 *
1428 * Places an array of data in the SPI TX FIFO. This function does not
1429 * block. It returns how many data elements were placed in the TX FIFO.
1430 *
1431 * \param base
1432 * The pointer to the SPI SCB instance.
1433 *
1434 * \param buffer
1435 * The pointer to the data to place in the TX FIFO.
1436 * The element size is defined by the data type, which depends on the configured
1437 * TX data width.
1438 *
1439 * \param size
1440 * The number of data elements to transmit.
1441 *
1442 * \return
1443 * The number of data elements placed in the TX FIFO.
1444 *
1445 * \note
1446 * * When in the master mode, writing data into the TX FIFO starts an SPI
1447 * transfer.
1448 * * When in the slave mode, writing data into the TX FIFO does not start
1449 * an SPI transfer. The data is loaded in the TX FIFO and will be sent to
1450 * the master on its request.
1451 * * The SPI interface is full-duplex, therefore reads and writes occur
1452 * at the same time. Thus, for every data element transferred out of the
1453 * TX FIFO, one is transferred into the RX FIFO.
1454 *
1455 *******************************************************************************/
Cy_SCB_SPI_WriteArray(CySCB_Type * base,void * buffer,uint32_t size)1456 __STATIC_INLINE uint32_t Cy_SCB_SPI_WriteArray(CySCB_Type *base, void *buffer, uint32_t size)
1457 {
1458 CY_ASSERT_L1(CY_SCB_IS_BUFFER_VALID(buffer, size));
1459
1460 return Cy_SCB_WriteArray(base, buffer, size);
1461 }
1462
1463
1464 /*******************************************************************************
1465 * Function Name: Cy_SCB_SPI_WriteArrayBlocking
1466 ****************************************************************************//**
1467 *
1468 * Places an array of data in the SPI TX FIFO. This function blocks
1469 * until the number of data elements specified by size is placed in the SPI
1470 * TX FIFO.
1471 *
1472 * \param base
1473 * The pointer to the SPI SCB instance.
1474 *
1475 * \param buffer
1476 * The pointer to data to place in the TX FIFO.
1477 * The element size is defined by the data type, which depends on the configured
1478 * TX data width.
1479 *
1480 * \param size
1481 * The number of data elements to write into the TX FIFO.
1482 *
1483 * \note
1484 * * When in the master mode, writing data into the TX FIFO starts an SPI
1485 * transfer.
1486 * * When in the slave mode, writing data into the TX FIFO does not start
1487 * an SPI transfer. The data is loaded in the TX FIFO and will be sent to
1488 * the master on its request.
1489 * * The SPI interface is full-duplex, therefore reads and writes occur
1490 * at the same time. Thus, for every data element transferred out of the
1491 * TX FIFO, one is transferred into the RX FIFO.
1492 *
1493 *******************************************************************************/
Cy_SCB_SPI_WriteArrayBlocking(CySCB_Type * base,void * buffer,uint32_t size)1494 __STATIC_INLINE void Cy_SCB_SPI_WriteArrayBlocking(CySCB_Type *base, void *buffer, uint32_t size)
1495 {
1496 CY_ASSERT_L1(CY_SCB_IS_BUFFER_VALID(buffer, size));
1497
1498 Cy_SCB_WriteArrayBlocking(base, buffer, size);
1499 }
1500 /** \} group_scb_spi_low_level_functions */
1501
1502
1503 /**
1504 * \addtogroup group_scb_spi_interrupt_functions
1505 * \{
1506 */
1507 /*******************************************************************************
1508 * Function Name: Cy_SCB_SPI_RegisterCallback
1509 ****************************************************************************//**
1510 *
1511 * Registers a callback function, which notifies that
1512 * \ref group_scb_spi_macros_callback_events occurred in the
1513 * \ref Cy_SCB_SPI_Interrupt.
1514 *
1515 * \param base
1516 * The pointer to the SPI SCB instance.
1517 *
1518 * \param callback
1519 * The pointer to the callback function.
1520 * See \ref cy_cb_scb_spi_handle_events_t for the function prototype.
1521 *
1522 * \param context
1523 * The pointer to the context structure \ref cy_stc_scb_spi_context_t allocated
1524 * by the user. The structure is used during the SPI operation for internal
1525 * configuration and data retention. The user should not modify anything
1526 * in this structure.
1527 *
1528 * \note
1529 * * To remove the callback, pass NULL as the pointer to the callback function.
1530 * * To get the event notification, the corresponding interrupt mask needs to be
1531 * set in the interrupt mask register so that interrupt request register can
1532 * trigger an interrupt event in \ref Cy_SCB_SPI_Interrupt.
1533 *
1534 *******************************************************************************/
Cy_SCB_SPI_RegisterCallback(CySCB_Type const * base,cy_cb_scb_spi_handle_events_t callback,cy_stc_scb_spi_context_t * context)1535 __STATIC_INLINE void Cy_SCB_SPI_RegisterCallback(CySCB_Type const *base,
1536 cy_cb_scb_spi_handle_events_t callback, cy_stc_scb_spi_context_t *context)
1537 {
1538 /* Suppress a compiler warning about unused variables */
1539 (void) base;
1540
1541 context->cbEvents = callback;
1542 }
1543
1544 /** \cond INTERNAL */
1545 /*******************************************************************************
1546 * Function Name: CY_SCB_SPI_GetSclkMode
1547 ****************************************************************************//**
1548 *
1549 * Return correct SCLK mode depends on selected sub mode.
1550 *
1551 * \param subMode
1552 * \ref cy_en_scb_spi_sub_mode_t
1553 *
1554 * \param sclkMode
1555 * \ref cy_en_scb_spi_sclk_mode_t
1556 *
1557 * \return
1558 * \ref cy_en_scb_spi_sclk_mode_t
1559 *
1560 *******************************************************************************/
CY_SCB_SPI_GetSclkMode(cy_en_scb_spi_sub_mode_t subMode,cy_en_scb_spi_sclk_mode_t sclkMode)1561 __STATIC_INLINE uint32_t CY_SCB_SPI_GetSclkMode(cy_en_scb_spi_sub_mode_t subMode , cy_en_scb_spi_sclk_mode_t sclkMode)
1562 {
1563 uint32_t retVal;
1564 switch (subMode)
1565 {
1566 case CY_SCB_SPI_TI_PRECEDES:
1567 case CY_SCB_SPI_TI_COINCIDES:
1568 retVal = (uint32_t) CY_SCB_SPI_CPHA1_CPOL0;
1569 break;
1570 case CY_SCB_SPI_NATIONAL:
1571 retVal = (uint32_t) CY_SCB_SPI_CPHA0_CPOL0;
1572 break;
1573 case CY_SCB_SPI_MOTOROLA:
1574 retVal = (uint32_t) sclkMode;
1575 break;
1576 default:
1577 retVal = (uint32_t) sclkMode;
1578 break;
1579 }
1580 return retVal;
1581 }
1582 /** \endcond */
1583
1584 /** \} group_scb_spi_interrupt_functions */
1585
1586 #if defined(__cplusplus)
1587 }
1588 #endif
1589
1590 /** \} group_scb_spi */
1591
1592 #endif /* (defined (CY_IP_MXSCB) || defined (CY_IP_MXS22SCB)) */
1593
1594 #endif /* (CY_SCB_SPI_H) */
1595
1596 /* [] END OF FILE */
1597