1 /***************************************************************************//**
2 * \file cy_scb_spi.h
3 * \version 3.10
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 and CAT1C 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 and CAT1C 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 and CAT1C 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 and CAT1C 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 and CAT1C 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 and CAT1C 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 /**
623 * The pointer to an event callback that is called when any of
624 * \ref group_scb_spi_macros_callback_events occurs
625 */
626 cy_cb_scb_spi_handle_events_t cbEvents;
627
628 #if !defined(NDEBUG)
629 uint32_t initKey; /**< Tracks the context initialization */
630 #endif /* !(NDEBUG) */
631 /** \endcond */
632 } cy_stc_scb_spi_context_t;
633
634 /** \} group_scb_spi_data_structures */
635
636
637 /*******************************************************************************
638 * Function Prototypes
639 *******************************************************************************/
640
641 /**
642 * \addtogroup group_scb_spi_general_functions
643 * \{
644 */
645 cy_en_scb_spi_status_t Cy_SCB_SPI_Init(CySCB_Type *base, cy_stc_scb_spi_config_t const *config,
646 cy_stc_scb_spi_context_t *context);
647 void Cy_SCB_SPI_DeInit (CySCB_Type *base);
648 __STATIC_INLINE void Cy_SCB_SPI_Enable(CySCB_Type *base);
649 void Cy_SCB_SPI_Disable(CySCB_Type *base, cy_stc_scb_spi_context_t *context);
650
651 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelect(CySCB_Type *base,
652 cy_en_scb_spi_slave_select_t slaveSelect);
653 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelectPolarity(CySCB_Type *base,
654 cy_en_scb_spi_slave_select_t slaveSelect,
655 cy_en_scb_spi_polarity_t polarity);
656
657 __STATIC_INLINE bool Cy_SCB_SPI_IsBusBusy(CySCB_Type const *base);
658 /** \} group_scb_spi_general_functions */
659
660 /**
661 * \addtogroup group_scb_spi_high_level_functions
662 * \{
663 */
664 cy_en_scb_spi_status_t Cy_SCB_SPI_Transfer(CySCB_Type *base, void *txBuffer, void *rxBuffer, uint32_t size,
665 cy_stc_scb_spi_context_t *context);
666 void Cy_SCB_SPI_AbortTransfer (CySCB_Type *base, cy_stc_scb_spi_context_t *context);
667 uint32_t Cy_SCB_SPI_GetTransferStatus(CySCB_Type const *base, cy_stc_scb_spi_context_t const *context);
668 uint32_t Cy_SCB_SPI_GetNumTransfered (CySCB_Type const *base, cy_stc_scb_spi_context_t const *context);
669 /** \} group_scb_spi_high_level_functions */
670
671 /**
672 * \addtogroup group_scb_spi_low_level_functions
673 * \{
674 */
675 __STATIC_INLINE uint32_t Cy_SCB_SPI_Read (CySCB_Type const *base);
676 __STATIC_INLINE uint32_t Cy_SCB_SPI_ReadArray(CySCB_Type const *base, void *buffer, uint32_t size);
677
678 __STATIC_INLINE uint32_t Cy_SCB_SPI_Write (CySCB_Type *base, uint32_t data);
679 __STATIC_INLINE uint32_t Cy_SCB_SPI_WriteArray(CySCB_Type *base, void *buffer, uint32_t size);
680 __STATIC_INLINE void Cy_SCB_SPI_WriteArrayBlocking(CySCB_Type *base, void *buffer, uint32_t size);
681
682 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetTxFifoStatus (CySCB_Type const *base);
683 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifoStatus(CySCB_Type *base, uint32_t clearMask);
684
685 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetRxFifoStatus (CySCB_Type const *base);
686 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifoStatus(CySCB_Type *base, uint32_t clearMask);
687
688 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetSlaveMasterStatus (CySCB_Type const *base);
689 __STATIC_INLINE void Cy_SCB_SPI_ClearSlaveMasterStatus(CySCB_Type *base, uint32_t clearMask);
690
691 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInTxFifo(CySCB_Type const *base);
692 __STATIC_INLINE bool Cy_SCB_SPI_IsTxComplete (CySCB_Type const *base);
693
694 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInRxFifo(CySCB_Type const *base);
695
696 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifo(CySCB_Type *base);
697 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifo(CySCB_Type *base);
698 /** \} group_scb_spi_low_level_functions */
699
700 /**
701 * \addtogroup group_scb_spi_interrupt_functions
702 * \{
703 */
704 void Cy_SCB_SPI_Interrupt(CySCB_Type *base, cy_stc_scb_spi_context_t *context);
705
706 __STATIC_INLINE void Cy_SCB_SPI_RegisterCallback(CySCB_Type const *base, cy_cb_scb_spi_handle_events_t callback,
707 cy_stc_scb_spi_context_t *context);
708 /** \} group_scb_spi_interrupt_functions */
709
710 /**
711 * \addtogroup group_scb_spi_low_power_functions
712 * \{
713 */
714 cy_en_syspm_status_t Cy_SCB_SPI_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode);
715 cy_en_syspm_status_t Cy_SCB_SPI_HibernateCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode);
716 /** \} group_scb_spi_low_power_functions */
717
718
719 /*******************************************************************************
720 * API Constants
721 *******************************************************************************/
722
723 /**
724 * \addtogroup group_scb_spi_macros
725 * \{
726 */
727
728 /**
729 * \defgroup group_scb_spi_macros_tx_fifo_status SPI TX FIFO Statuses
730 * Macros to check SPI TX FIFO status returned by \ref Cy_SCB_SPI_GetTxFifoStatus
731 * function or assign mask for \ref Cy_SCB_SPI_ClearTxFifoStatus function.
732 * Each SPI TX FIFO status is encoded in a separate bit, therefore multiple bits
733 * may be set to indicate the current status.
734 * \{
735 */
736 /** The number of entries in the TX FIFO is less than the TX FIFO trigger level
737 * value
738 */
739 #define CY_SCB_SPI_TX_TRIGGER (SCB_INTR_TX_TRIGGER_Msk)
740
741 /** The TX FIFO is not full, there is a space for more data */
742 #define CY_SCB_SPI_TX_NOT_FULL (SCB_INTR_TX_NOT_FULL_Msk)
743
744 /**
745 * The TX FIFO is empty, note that there may still be data in the shift register.
746 */
747 #define CY_SCB_SPI_TX_EMPTY (SCB_INTR_TX_EMPTY_Msk)
748
749 /** An attempt to write to the full TX FIFO */
750 #define CY_SCB_SPI_TX_OVERFLOW (SCB_INTR_TX_OVERFLOW_Msk)
751
752 /**
753 * Applicable only for the slave mode. The master tried to read more
754 * data elements than available.
755 */
756 #define CY_SCB_SPI_TX_UNDERFLOW (SCB_INTR_TX_UNDERFLOW_Msk)
757 /** \} group_scb_spi_macros_tx_fifo_status */
758
759 /**
760 * \defgroup group_scb_spi_macros_rx_fifo_status SPI RX FIFO Statuses
761 * \{
762 * Macros to check SPI RX FIFO status returned by \ref Cy_SCB_SPI_GetRxFifoStatus
763 * function or assign mask for \ref Cy_SCB_SPI_ClearRxFifoStatus function.
764 * Each SPI RX FIFO status is encoded in a separate bit, therefore multiple
765 * bits may be set to indicate the current status.
766 */
767 /** The number of entries in the RX FIFO is more than the RX FIFO trigger
768 * level value.
769 */
770 #define CY_SCB_SPI_RX_TRIGGER (SCB_INTR_RX_TRIGGER_Msk)
771
772 /** The RX FIFO is not empty, there is data to read */
773 #define CY_SCB_SPI_RX_NOT_EMPTY (SCB_INTR_RX_NOT_EMPTY_Msk)
774
775 /**
776 * The RX FIFO is full. There is no more space for additional data.
777 * Any additional data will be dropped.
778 */
779 #define CY_SCB_SPI_RX_FULL (SCB_INTR_RX_FULL_Msk)
780
781 /**
782 * The RX FIFO was full and there was an attempt to write to it.
783 * This additional data was dropped.
784 */
785 #define CY_SCB_SPI_RX_OVERFLOW (SCB_INTR_RX_OVERFLOW_Msk)
786
787 /** An attempt to read from an empty RX FIFO */
788 #define CY_SCB_SPI_RX_UNDERFLOW (SCB_INTR_RX_UNDERFLOW_Msk)
789 /** \} group_scb_spi_macros_rx_fifo_status */
790
791 /**
792 * \defgroup group_scb_spi_macros_master_slave_status SPI Master and Slave Statuses
793 * \{
794 * Macros to check SPI Mater and Slave status returned by \ref Cy_SCB_SPI_GetSlaveMasterStatus
795 * function or assign mask for \ref Cy_SCB_SPI_ClearSlaveMasterStatus function.
796 */
797 /** The slave was deselected at the wrong time */
798 #define CY_SCB_SPI_SLAVE_ERR (SCB_INTR_S_SPI_BUS_ERROR_Msk)
799
800 /** The master has transmitted all data elements from FIFO and shifter */
801 #define CY_SCB_SPI_MASTER_DONE (SCB_INTR_M_SPI_DONE_Msk)
802 /** \} group_scb_spi_macros_master_slave_status */
803
804 /**
805 * \defgroup group_scb_spi_macros_xfer_status SPI Transfer Status
806 * \{
807 * Macros to check current SPI transfer status returned by
808 * \ref Cy_SCB_SPI_GetTransferStatus function.
809 * Each SPI transfer status is encoded in a separate bit, therefore multiple bits
810 * may be set to indicate the current status.
811 */
812 /**
813 * Transfer operation started by \ref Cy_SCB_SPI_Transfer is in progress
814 */
815 #define CY_SCB_SPI_TRANSFER_ACTIVE (0x01UL)
816
817 /**
818 * All data elements specified by \ref Cy_SCB_SPI_Transfer for transmission
819 * have been loaded into the TX FIFO
820 */
821 #define CY_SCB_SPI_TRANSFER_IN_FIFO (0x02UL)
822
823 /** The slave was deselected at the wrong time. */
824 #define CY_SCB_SPI_SLAVE_TRANSFER_ERR (SCB_INTR_S_SPI_BUS_ERROR_Msk)
825
826 /**
827 * RX FIFO was full and there was an attempt to write to it.
828 * This additional data was dropped.
829 */
830 #define CY_SCB_SPI_TRANSFER_OVERFLOW (SCB_INTR_RX_OVERFLOW_Msk)
831
832 /**
833 * Applicable only for the slave mode. The master tried to read more
834 * data elements than available in the TX FIFO.
835 */
836 #define CY_SCB_SPI_TRANSFER_UNDERFLOW (SCB_INTR_TX_UNDERFLOW_Msk)
837 /** \} group_scb_spi_macros_xfer_status */
838
839 /**
840 * \defgroup group_scb_spi_macros_callback_events SPI Callback Events
841 * \{
842 * Macros to check SPI events passed by \ref cy_cb_scb_spi_handle_events_t callback.
843 * Note that only single event is notified by the callback when it is called.
844 */
845 /**
846 * All data elements specified by \ref Cy_SCB_SPI_Transfer for transmission
847 * have been loaded into the TX FIFO
848 */
849 #define CY_SCB_SPI_TRANSFER_IN_FIFO_EVENT (0x01U)
850
851 /** The transfer operation started by \ref Cy_SCB_SPI_Transfer is complete */
852 #define CY_SCB_SPI_TRANSFER_CMPLT_EVENT (0x02U)
853
854 /**
855 * An error occurred during the transfer. This includes overflow, underflow
856 * and a transfer error. Check \ref Cy_SCB_SPI_GetTransferStatus.
857 */
858 #define CY_SCB_SPI_TRANSFER_ERR_EVENT (0x04U)
859 /** \} group_scb_spi_macros_callback_events */
860
861
862 /** Default TX value when no TX buffer is defined */
863 #define CY_SCB_SPI_DEFAULT_TX (0x0000FFFFUL)
864
865 /** Data returned by the hardware when an empty RX FIFO is read */
866 #define CY_SCB_SPI_RX_NO_DATA (0xFFFFFFFFUL)
867
868
869 /*******************************************************************************
870 * Internal Constants
871 *******************************************************************************/
872
873 /** \cond INTERNAL */
874 #define CY_SCB_SPI_RX_INTR_MASK (CY_SCB_SPI_RX_TRIGGER | CY_SCB_SPI_RX_NOT_EMPTY | CY_SCB_SPI_RX_FULL | \
875 CY_SCB_SPI_RX_OVERFLOW | CY_SCB_SPI_RX_UNDERFLOW)
876
877 #define CY_SCB_SPI_TX_INTR_MASK (CY_SCB_SPI_TX_TRIGGER | CY_SCB_SPI_TX_NOT_FULL | CY_SCB_SPI_TX_EMPTY | \
878 CY_SCB_SPI_TX_OVERFLOW | CY_SCB_SPI_TX_UNDERFLOW)
879
880 #define CY_SCB_SPI_MASTER_SLAVE_INTR_MASK (CY_SCB_SPI_MASTER_DONE | CY_SCB_SPI_SLAVE_ERR)
881
882 #define CY_SCB_SPI_TRANSFER_ERR (CY_SCB_SPI_SLAVE_TRANSFER_ERR | CY_SCB_SPI_TRANSFER_OVERFLOW | \
883 CY_SCB_SPI_TRANSFER_UNDERFLOW)
884
885 #define CY_SCB_SPI_INIT_KEY (0x00ABCDEFUL)
886
887 #define CY_SCB_SPI_IS_MODE_VALID(mode) ( (CY_SCB_SPI_SLAVE == (mode)) || \
888 (CY_SCB_SPI_MASTER == (mode)) )
889
890 #define CY_SCB_SPI_IS_SUB_MODE_VALID(subMode) ( (CY_SCB_SPI_MOTOROLA == (subMode)) || \
891 (CY_SCB_SPI_TI_COINCIDES == (subMode)) || \
892 (CY_SCB_SPI_TI_PRECEDES == (subMode)) || \
893 (CY_SCB_SPI_NATIONAL == (subMode)) )
894
895 #define CY_SCB_SPI_IS_SCLK_MODE_VALID(clkMode) ( (CY_SCB_SPI_CPHA0_CPOL0 == (clkMode)) || \
896 (CY_SCB_SPI_CPHA0_CPOL1 == (clkMode)) || \
897 (CY_SCB_SPI_CPHA1_CPOL0 == (clkMode)) || \
898 (CY_SCB_SPI_CPHA1_CPOL1 == (clkMode)) )
899
900 #define CY_SCB_SPI_IS_POLARITY_VALID(polarity) ( (CY_SCB_SPI_ACTIVE_LOW == (polarity)) || \
901 (CY_SCB_SPI_ACTIVE_HIGH == (polarity)) )
902
903 #define CY_SCB_SPI_IS_SLAVE_SEL_VALID(ss) ( (CY_SCB_SPI_SLAVE_SELECT0 == (ss)) || \
904 (CY_SCB_SPI_SLAVE_SELECT1 == (ss)) || \
905 (CY_SCB_SPI_SLAVE_SELECT2 == (ss)) || \
906 (CY_SCB_SPI_SLAVE_SELECT3 == (ss)) )
907
908 #define CY_SCB_SPI_IS_OVERSAMPLE_VALID(ovs, mode) ( (CY_SCB_SPI_MASTER == (mode)) ? (((ovs) >= 2UL) && ((ovs) <= 16UL)) : true )
909 #define CY_SCB_SPI_IS_DATA_WIDTH_VALID(width) ( ((width) >= 4UL) && ((width) <= 32UL) )
910 #define CY_SCB_SPI_IS_SS_POLARITY_VALID(polarity) ( (0UL == ((polarity) & (~0x0FUL))) )
911 #define CY_SCB_SPI_IS_BUFFER_VALID(txBuffer, rxBuffer, size) ( ((size) > 0UL) && \
912 (false == ((NULL == (txBuffer)) && (NULL == (rxBuffer)))) )
913
914 #define CY_SCB_SPI_IS_BOTH_DATA_WIDTH_VALID(subMode, rxWidth, txWidth) ( (CY_SCB_SPI_NATIONAL != (subMode)) ? \
915 ((rxWidth) == (txWidth)) : true )
916
917 /** \endcond */
918
919 /** \} group_scb_spi_macros */
920
921
922 /*******************************************************************************
923 * In-line Function Implementation
924 *******************************************************************************/
925
926 /**
927 * \addtogroup group_scb_spi_general_functions
928 * \{
929 */
930
931 /*******************************************************************************
932 * Function Name: Cy_SCB_SPI_Enable
933 ****************************************************************************//**
934 *
935 * Enables the SCB block for the SPI operation.
936 *
937 * \param base
938 * The pointer to the SPI SCB instance.
939 *
940 *******************************************************************************/
Cy_SCB_SPI_Enable(CySCB_Type * base)941 __STATIC_INLINE void Cy_SCB_SPI_Enable(CySCB_Type *base)
942 {
943 SCB_CTRL(base) |= SCB_CTRL_ENABLED_Msk;
944 }
945
946
947 /*******************************************************************************
948 * Function Name: Cy_SCB_SPI_IsBusBusy
949 ****************************************************************************//**
950 *
951 * Returns whether the SPI bus is busy or not. The bus busy is determined using
952 * the slave select signal.
953 * * Motorola and National Semiconductor sub-modes: the bus is busy after the
954 * slave select line is activated and lasts until the slave select line is
955 * deactivated.
956 * * Texas Instrument sub-modes: The bus is busy the moment of the initial
957 * pulse on the slave select line and lasts until the transfer is complete
958 * (all bytes from the TX FIFO area shifted-out on the bus).
959 *
960 * \param base
961 * The pointer to the SPI SCB instance.
962 *
963 * \return
964 * True - the bus is busy; false - the bus is idle.
965 *
966 * \note
967 * * The SPI master does not assign the slave select line immediately after
968 * the first data element is written into the TX FIFO. It takes up to two SCLK
969 * clocks to assign the slave select line. Before this happens, the bus
970 * is considered idle.
971 * * If the SPI master is configured to transmit each data element separated by
972 * a de-assertion of the slave select line, the bus is busy during each element
973 * transfer and is free between them.
974 *
975 *******************************************************************************/
Cy_SCB_SPI_IsBusBusy(CySCB_Type const * base)976 __STATIC_INLINE bool Cy_SCB_SPI_IsBusBusy(CySCB_Type const *base)
977 {
978 return _FLD2BOOL(SCB_SPI_STATUS_BUS_BUSY, SCB_SPI_STATUS(base));
979 }
980
981
982 /*******************************************************************************
983 * Function Name: Cy_SCB_SPI_SetActiveSlaveSelect
984 ****************************************************************************//**
985 *
986 * Selects an active slave select line from one of four available.
987 * This function is applicable for the master and slave.
988 *
989 * \param base
990 * The pointer to the SPI SCB instance.
991 *
992 * \param slaveSelect
993 * The slave select line number.
994 * See \ref cy_en_scb_spi_slave_select_t for the set of constants.
995 *
996 * \note
997 * The SCB must be idle or disabled before calling this function.
998 *
999 *******************************************************************************/
Cy_SCB_SPI_SetActiveSlaveSelect(CySCB_Type * base,cy_en_scb_spi_slave_select_t slaveSelect)1000 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelect(CySCB_Type *base, cy_en_scb_spi_slave_select_t slaveSelect)
1001 {
1002 CY_ASSERT_L3(CY_SCB_SPI_IS_SLAVE_SEL_VALID(slaveSelect));
1003
1004 CY_REG32_CLR_SET(SCB_SPI_CTRL(base), SCB_SPI_CTRL_SSEL, (uint32_t) slaveSelect);
1005 }
1006
1007
1008 /*******************************************************************************
1009 * Function Name: Cy_SCB_SPI_SetActiveSlaveSelectPolarity
1010 ****************************************************************************//**
1011 *
1012 * Sets the active polarity for the slave select line.
1013 *
1014 * \param base
1015 * The pointer to the SPI SCB instance.
1016 *
1017 * \param slaveSelect
1018 * The slave select line number.
1019 * See \ref cy_en_scb_spi_slave_select_t for the set of constants.
1020 *
1021 * \param polarity
1022 * The polarity of the slave select line.
1023 * See \ref cy_en_scb_spi_polarity_t for the set of constants.
1024 *
1025 * \note
1026 * The SCB must be idle or disabled before calling this function.
1027 *
1028 *******************************************************************************/
Cy_SCB_SPI_SetActiveSlaveSelectPolarity(CySCB_Type * base,cy_en_scb_spi_slave_select_t slaveSelect,cy_en_scb_spi_polarity_t polarity)1029 __STATIC_INLINE void Cy_SCB_SPI_SetActiveSlaveSelectPolarity(CySCB_Type *base,
1030 cy_en_scb_spi_slave_select_t slaveSelect,
1031 cy_en_scb_spi_polarity_t polarity)
1032 {
1033 uint32_t mask = _VAL2FLD(CY_SCB_SPI_CTRL_SSEL_POLARITY, (0x01UL << ((uint32_t)slaveSelect)));
1034
1035 CY_ASSERT_L3(CY_SCB_SPI_IS_SLAVE_SEL_VALID(slaveSelect));
1036 CY_ASSERT_L3(CY_SCB_SPI_IS_POLARITY_VALID (polarity));
1037
1038 if (CY_SCB_SPI_ACTIVE_HIGH == polarity)
1039 {
1040 SCB_SPI_CTRL(base) |= (uint32_t) mask;
1041 }
1042 else
1043 {
1044 SCB_SPI_CTRL(base) &= (uint32_t) ~mask;
1045 }
1046 }
1047 /** \} group_scb_spi_general_functions */
1048
1049
1050 /**
1051 * \addtogroup group_scb_spi_low_level_functions
1052 * \{
1053 */
1054 /*******************************************************************************
1055 * Function Name: Cy_SCB_SPI_GetRxFifoStatus
1056 ****************************************************************************//**
1057 *
1058 * Returns the current status of the RX FIFO.
1059 *
1060 * \param base
1061 * The pointer to the SPI SCB instance.
1062 *
1063 * \return
1064 * \ref group_scb_spi_macros_rx_fifo_status
1065 *
1066 *******************************************************************************/
Cy_SCB_SPI_GetRxFifoStatus(CySCB_Type const * base)1067 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetRxFifoStatus(CySCB_Type const *base)
1068 {
1069 return (Cy_SCB_GetRxInterruptStatus(base) & CY_SCB_SPI_RX_INTR_MASK);
1070 }
1071
1072
1073 /*******************************************************************************
1074 * Function Name: Cy_SCB_SPI_ClearRxFifoStatus
1075 ****************************************************************************//**
1076 *
1077 * Clears the selected statuses of the RX FIFO.
1078 *
1079 * \param base
1080 * The pointer to the SPI SCB instance.
1081 *
1082 * \param clearMask
1083 * The mask of which statuses to clear.
1084 * See \ref group_scb_spi_macros_rx_fifo_status for the set of constants.
1085 *
1086 * \note
1087 * * This status is also used for interrupt generation, so clearing it also
1088 * clears the interrupt sources.
1089 * * Level sensitive statuses such as \ref CY_SCB_SPI_RX_TRIGGER,
1090 * \ref CY_SCB_SPI_RX_NOT_EMPTY and \ref CY_SCB_SPI_RX_FULL set high again after
1091 * being cleared if the condition remains true.
1092 *
1093 *******************************************************************************/
Cy_SCB_SPI_ClearRxFifoStatus(CySCB_Type * base,uint32_t clearMask)1094 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifoStatus(CySCB_Type *base, uint32_t clearMask)
1095 {
1096 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_SPI_RX_INTR_MASK));
1097
1098 Cy_SCB_ClearRxInterrupt(base, clearMask);
1099 }
1100
1101
1102 /*******************************************************************************
1103 * Function Name: Cy_SCB_SPI_GetNumInRxFifo
1104 ****************************************************************************//**
1105 *
1106 * Returns the number of data elements in the SPI RX FIFO.
1107 *
1108 * \param base
1109 * The pointer to the SPI SCB instance.
1110 *
1111 * \return
1112 * The number of data elements in the RX FIFO.
1113 * The size of a data element defined by the configured RX data width.
1114 *
1115 * \note
1116 * This number does not include any data currently in the RX shifter.
1117 *
1118 *******************************************************************************/
Cy_SCB_SPI_GetNumInRxFifo(CySCB_Type const * base)1119 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInRxFifo(CySCB_Type const *base)
1120 {
1121 return Cy_SCB_GetNumInRxFifo(base);
1122 }
1123
1124
1125 /*******************************************************************************
1126 * Function Name: Cy_SCB_SPI_ClearRxFifo
1127 ****************************************************************************//**
1128 *
1129 * Clears all data out of the SPI RX FIFO.
1130 *
1131 * \param base
1132 * The pointer to the SPI SCB instance.
1133 *
1134 * \sideeffect
1135 * Any data currently in the shifter is cleared and lost.
1136 *
1137 *******************************************************************************/
Cy_SCB_SPI_ClearRxFifo(CySCB_Type * base)1138 __STATIC_INLINE void Cy_SCB_SPI_ClearRxFifo(CySCB_Type *base)
1139 {
1140 Cy_SCB_ClearRxFifo(base);
1141 }
1142
1143
1144 /*******************************************************************************
1145 * Function Name: Cy_SCB_SPI_GetTxFifoStatus
1146 ****************************************************************************//**
1147 *
1148 * Returns the current status of the TX FIFO.
1149 *
1150 * \param base
1151 * The pointer to the SPI SCB instance.
1152 *
1153 * \return
1154 * \ref group_scb_spi_macros_tx_fifo_status
1155 *
1156 *******************************************************************************/
Cy_SCB_SPI_GetTxFifoStatus(CySCB_Type const * base)1157 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetTxFifoStatus(CySCB_Type const *base)
1158 {
1159 return (Cy_SCB_GetTxInterruptStatus(base) & CY_SCB_SPI_TX_INTR_MASK);
1160 }
1161
1162
1163 /*******************************************************************************
1164 * Function Name: Cy_SCB_SPI_ClearTxFifoStatus
1165 ****************************************************************************//**
1166 *
1167 * Clears the selected statuses of the TX FIFO.
1168 *
1169 * \param base
1170 * The pointer to the SPI SCB instance.
1171 *
1172 * \param clearMask
1173 * The mask of which statuses to clear.
1174 * See \ref group_scb_spi_macros_tx_fifo_status for the set of constants.
1175 *
1176 * \note
1177 * * The status is also used for interrupt generation, so clearing it also
1178 * clears the interrupt sources.
1179 * * Level sensitive statuses such as \ref CY_SCB_SPI_TX_TRIGGER,
1180 * \ref CY_SCB_SPI_TX_EMPTY and \ref CY_SCB_SPI_TX_NOT_FULL set high again after
1181 * being cleared if the condition remains true.
1182 *
1183 *******************************************************************************/
Cy_SCB_SPI_ClearTxFifoStatus(CySCB_Type * base,uint32_t clearMask)1184 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifoStatus(CySCB_Type *base, uint32_t clearMask)
1185 {
1186 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_SPI_TX_INTR_MASK));
1187
1188 Cy_SCB_ClearTxInterrupt(base, clearMask);
1189 }
1190
1191
1192 /*******************************************************************************
1193 * Function Name: Cy_SCB_SPI_GetNumInTxFifo
1194 ****************************************************************************//**
1195 *
1196 * Returns the number of data elements in the SPI TX FIFO.
1197 *
1198 * \param base
1199 * The pointer to the SPI SCB instance.
1200 *
1201 * \return
1202 * The number of data elements in the TX FIFO.
1203 * The size of a data element defined by the configured TX data width.
1204 *
1205 * \note
1206 * This number does not include any data currently in the TX shifter.
1207 *
1208 *******************************************************************************/
Cy_SCB_SPI_GetNumInTxFifo(CySCB_Type const * base)1209 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetNumInTxFifo(CySCB_Type const *base)
1210 {
1211 return Cy_SCB_GetNumInTxFifo(base);
1212 }
1213
1214
1215 /*******************************************************************************
1216 * Function Name: Cy_SCB_SPI_IsTxComplete
1217 ****************************************************************************//**
1218 *
1219 * Checks whether the TX FIFO and Shifter are empty and there is no more data to send
1220 *
1221 * \param base
1222 * Pointer to the SPI SCB instance.
1223 *
1224 * \return
1225 * If true, transmission complete. If false, transmission is not complete.
1226 *
1227 *******************************************************************************/
Cy_SCB_SPI_IsTxComplete(CySCB_Type const * base)1228 __STATIC_INLINE bool Cy_SCB_SPI_IsTxComplete(CySCB_Type const *base)
1229 {
1230 return Cy_SCB_IsTxComplete(base);
1231 }
1232
1233
1234 /*******************************************************************************
1235 * Function Name: Cy_SCB_SPI_ClearTxFifo
1236 ****************************************************************************//**
1237 *
1238 * Clears all data out of the SPI TX FIFO.
1239 *
1240 * \param base
1241 * The pointer to the SPI SCB instance.
1242 *
1243 * \sideeffect
1244 * The TX FIFO clear operation also clears the shift register;
1245 * the shifter can be cleared in the middle of a data element transfer,
1246 * corrupting it. The data element corruption means that all bits that have
1247 * not been transmitted are transmitted as 1s on the bus.
1248 *
1249 *******************************************************************************/
Cy_SCB_SPI_ClearTxFifo(CySCB_Type * base)1250 __STATIC_INLINE void Cy_SCB_SPI_ClearTxFifo(CySCB_Type *base)
1251 {
1252 Cy_SCB_ClearTxFifo(base);
1253 }
1254
1255
1256 /*******************************************************************************
1257 * Function Name: Cy_SCB_SPI_GetSlaveMasterStatus
1258 ****************************************************************************//**
1259 *
1260 * Returns the current status of either the slave or the master, depending
1261 * on the configured SPI mode.
1262 *
1263 * \param base
1264 * The pointer to the SPI SCB instance.
1265 *
1266 * \return
1267 * \ref group_scb_spi_macros_master_slave_status
1268 *
1269 *******************************************************************************/
Cy_SCB_SPI_GetSlaveMasterStatus(CySCB_Type const * base)1270 __STATIC_INLINE uint32_t Cy_SCB_SPI_GetSlaveMasterStatus(CySCB_Type const *base)
1271 {
1272 uint32_t retStatus;
1273
1274 if (_FLD2BOOL(SCB_SPI_CTRL_MASTER_MODE, SCB_SPI_CTRL(base)))
1275 {
1276 retStatus = (Cy_SCB_GetMasterInterruptStatus(base) & CY_SCB_MASTER_INTR_SPI_DONE);
1277 }
1278 else
1279 {
1280 retStatus = (Cy_SCB_GetSlaveInterruptStatus(base) & CY_SCB_SLAVE_INTR_SPI_BUS_ERROR);
1281 }
1282
1283 return (retStatus);
1284 }
1285
1286
1287 /*******************************************************************************
1288 * Function Name: Cy_SCB_SPI_ClearSlaveMasterStatus
1289 ****************************************************************************//**
1290 *
1291 * Clears the selected statuses of either the slave or the master.
1292 *
1293 * \param base
1294 * The pointer to the SPI SCB instance.
1295 *
1296 * \param clearMask
1297 * The mask of which statuses to clear.
1298 * See \ref group_scb_spi_macros_master_slave_status for the set of constants.
1299 *
1300 *******************************************************************************/
Cy_SCB_SPI_ClearSlaveMasterStatus(CySCB_Type * base,uint32_t clearMask)1301 __STATIC_INLINE void Cy_SCB_SPI_ClearSlaveMasterStatus(CySCB_Type *base, uint32_t clearMask)
1302 {
1303 if (_FLD2BOOL(SCB_SPI_CTRL_MASTER_MODE, SCB_SPI_CTRL(base)))
1304 {
1305 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_MASTER_INTR_SPI_DONE));
1306
1307 Cy_SCB_ClearMasterInterrupt(base, clearMask);
1308 }
1309 else
1310 {
1311 CY_ASSERT_L2(CY_SCB_IS_INTR_VALID(clearMask, CY_SCB_SLAVE_INTR_SPI_BUS_ERROR));
1312
1313 Cy_SCB_ClearSlaveInterrupt(base, clearMask);
1314 }
1315 }
1316
1317
1318 /*******************************************************************************
1319 * Function Name: Cy_SCB_SPI_Read
1320 ****************************************************************************//**
1321 *
1322 * Reads a single data element from the SPI RX FIFO.
1323 * This function does not check whether the RX FIFO has data before reading it.
1324 * If the RX FIFO is empty, the function returns \ref CY_SCB_SPI_RX_NO_DATA.
1325 *
1326 * \param base
1327 * The pointer to the SPI SCB instance.
1328 *
1329 * \return
1330 * Data from the RX FIFO.
1331 * The data element size is defined by the configured RX data width.
1332 *
1333 * \note
1334 * * This function only reads data available in the RX FIFO. It does not
1335 * initiate an SPI transfer.
1336 * * When in the master mode, this function writes data into the TX FIFO and
1337 * waits until the transfer is completed before reading data from the RX FIFO.
1338 *
1339 *******************************************************************************/
Cy_SCB_SPI_Read(CySCB_Type const * base)1340 __STATIC_INLINE uint32_t Cy_SCB_SPI_Read(CySCB_Type const *base)
1341 {
1342 return Cy_SCB_ReadRxFifo(base);
1343 }
1344
1345
1346 /*******************************************************************************
1347 * Function Name: Cy_SCB_SPI_ReadArray
1348 ****************************************************************************//**
1349 *
1350 * Reads an array of data out of the SPI RX FIFO.
1351 * This function does not block. It returns how many data elements were read
1352 * from the RX FIFO.
1353 *
1354 * \param base
1355 * The pointer to the SPI SCB instance.
1356 *
1357 * \param buffer
1358 * The pointer to the location to place data read from the RX FIFO.
1359 * The element size is defined by the data type, which depends on the configured
1360 * RX data width.
1361 *
1362 * \param size
1363 * The number of data elements to read from the RX FIFO.
1364 *
1365 * \return
1366 * The number of data elements read from the RX FIFO.
1367 *
1368 * \note
1369 * * This function only reads data available in the RX FIFO. It does not
1370 * initiate an SPI transfer.
1371 * * When in the master mode, this function writes data into the TX FIFO and
1372 * waits until the transfer is completed before reading data from the RX FIFO.
1373 *
1374 *******************************************************************************/
Cy_SCB_SPI_ReadArray(CySCB_Type const * base,void * buffer,uint32_t size)1375 __STATIC_INLINE uint32_t Cy_SCB_SPI_ReadArray(CySCB_Type const *base, void *buffer, uint32_t size)
1376 {
1377 CY_ASSERT_L1(CY_SCB_IS_BUFFER_VALID(buffer, size));
1378
1379 return Cy_SCB_ReadArray(base, buffer, size);
1380 }
1381
1382
1383 /*******************************************************************************
1384 * Function Name: Cy_SCB_SPI_Write
1385 ****************************************************************************//**
1386 *
1387 * Places a single data element in the SPI TX FIFO.
1388 * This function does not block. It returns how many data elements were placed
1389 * in the TX FIFO.
1390 *
1391 * \param base
1392 * The pointer to the SPI SCB instance.
1393 *
1394 * \param data
1395 * Data to put in the TX FIFO.
1396 * The element size is defined by the data type, which depends on the configured
1397 * TX data width.
1398 *
1399 * \return
1400 * The number of data elements placed in the TX FIFO: 0 or 1.
1401 *
1402 * \note
1403 * * When in the master mode, writing data into the TX FIFO starts an SPI
1404 * transfer.
1405 * * When in the slave mode, writing data into the TX FIFO does not start
1406 * an SPI transfer. The data is loaded in the TX FIFO and will be sent
1407 * to the master on its request.
1408 * * The SPI interface is full-duplex, therefore reads and writes occur
1409 * at the same time. Thus, for every data element transferred out of the
1410 * TX FIFO, one is transferred into the RX FIFO.
1411 *
1412 *******************************************************************************/
Cy_SCB_SPI_Write(CySCB_Type * base,uint32_t data)1413 __STATIC_INLINE uint32_t Cy_SCB_SPI_Write(CySCB_Type *base, uint32_t data)
1414 {
1415 return Cy_SCB_Write(base, data);
1416 }
1417
1418
1419 /*******************************************************************************
1420 * Function Name: Cy_SCB_SPI_WriteArray
1421 ****************************************************************************//**
1422 *
1423 * Places an array of data in the SPI TX FIFO. This function does not
1424 * block. It returns how many data elements were placed in the TX FIFO.
1425 *
1426 * \param base
1427 * The pointer to the SPI SCB instance.
1428 *
1429 * \param buffer
1430 * The pointer to the data to place in the TX FIFO.
1431 * The element size is defined by the data type, which depends on the configured
1432 * TX data width.
1433 *
1434 * \param size
1435 * The number of data elements to transmit.
1436 *
1437 * \return
1438 * The number of data elements placed in the TX FIFO.
1439 *
1440 * \note
1441 * * When in the master mode, writing data into the TX FIFO starts an SPI
1442 * transfer.
1443 * * When in the slave mode, writing data into the TX FIFO does not start
1444 * an SPI transfer. The data is loaded in the TX FIFO and will be sent to
1445 * the master on its request.
1446 * * The SPI interface is full-duplex, therefore reads and writes occur
1447 * at the same time. Thus, for every data element transferred out of the
1448 * TX FIFO, one is transferred into the RX FIFO.
1449 *
1450 *******************************************************************************/
Cy_SCB_SPI_WriteArray(CySCB_Type * base,void * buffer,uint32_t size)1451 __STATIC_INLINE uint32_t Cy_SCB_SPI_WriteArray(CySCB_Type *base, void *buffer, uint32_t size)
1452 {
1453 CY_ASSERT_L1(CY_SCB_IS_BUFFER_VALID(buffer, size));
1454
1455 return Cy_SCB_WriteArray(base, buffer, size);
1456 }
1457
1458
1459 /*******************************************************************************
1460 * Function Name: Cy_SCB_SPI_WriteArrayBlocking
1461 ****************************************************************************//**
1462 *
1463 * Places an array of data in the SPI TX FIFO. This function blocks
1464 * until the number of data elements specified by size is placed in the SPI
1465 * TX FIFO.
1466 *
1467 * \param base
1468 * The pointer to the SPI SCB instance.
1469 *
1470 * \param buffer
1471 * The pointer to data to place in the TX FIFO.
1472 * The element size is defined by the data type, which depends on the configured
1473 * TX data width.
1474 *
1475 * \param size
1476 * The number of data elements to write into the TX FIFO.
1477 *
1478 * \note
1479 * * When in the master mode, writing data into the TX FIFO starts an SPI
1480 * transfer.
1481 * * When in the slave mode, writing data into the TX FIFO does not start
1482 * an SPI transfer. The data is loaded in the TX FIFO and will be sent to
1483 * the master on its request.
1484 * * The SPI interface is full-duplex, therefore reads and writes occur
1485 * at the same time. Thus, for every data element transferred out of the
1486 * TX FIFO, one is transferred into the RX FIFO.
1487 *
1488 *******************************************************************************/
Cy_SCB_SPI_WriteArrayBlocking(CySCB_Type * base,void * buffer,uint32_t size)1489 __STATIC_INLINE void Cy_SCB_SPI_WriteArrayBlocking(CySCB_Type *base, void *buffer, uint32_t size)
1490 {
1491 CY_ASSERT_L1(CY_SCB_IS_BUFFER_VALID(buffer, size));
1492
1493 Cy_SCB_WriteArrayBlocking(base, buffer, size);
1494 }
1495 /** \} group_scb_spi_low_level_functions */
1496
1497
1498 /**
1499 * \addtogroup group_scb_spi_interrupt_functions
1500 * \{
1501 */
1502 /*******************************************************************************
1503 * Function Name: Cy_SCB_SPI_RegisterCallback
1504 ****************************************************************************//**
1505 *
1506 * Registers a callback function, which notifies that
1507 * \ref group_scb_spi_macros_callback_events occurred in the
1508 * \ref Cy_SCB_SPI_Interrupt.
1509 *
1510 * \param base
1511 * The pointer to the SPI SCB instance.
1512 *
1513 * \param callback
1514 * The pointer to the callback function.
1515 * See \ref cy_cb_scb_spi_handle_events_t for the function prototype.
1516 *
1517 * \param context
1518 * The pointer to the context structure \ref cy_stc_scb_spi_context_t allocated
1519 * by the user. The structure is used during the SPI operation for internal
1520 * configuration and data retention. The user should not modify anything
1521 * in this structure.
1522 *
1523 * \note
1524 * * To remove the callback, pass NULL as the pointer to the callback function.
1525 * * To get the event notification, the corresponding interrupt mask needs to be
1526 * set in the interrupt mask register so that interrupt request register can
1527 * trigger an interrupt event in \ref Cy_SCB_SPI_Interrupt.
1528 *
1529 *******************************************************************************/
Cy_SCB_SPI_RegisterCallback(CySCB_Type const * base,cy_cb_scb_spi_handle_events_t callback,cy_stc_scb_spi_context_t * context)1530 __STATIC_INLINE void Cy_SCB_SPI_RegisterCallback(CySCB_Type const *base,
1531 cy_cb_scb_spi_handle_events_t callback, cy_stc_scb_spi_context_t *context)
1532 {
1533 /* Suppress a compiler warning about unused variables */
1534 (void) base;
1535
1536 context->cbEvents = callback;
1537 }
1538
1539 /** \cond INTERNAL */
1540 /*******************************************************************************
1541 * Function Name: CY_SCB_SPI_GetSclkMode
1542 ****************************************************************************//**
1543 *
1544 * Return correct SCLK mode depends on selected sub mode.
1545 *
1546 * \param subMode
1547 * \ref cy_en_scb_spi_sub_mode_t
1548 *
1549 * \param sclkMode
1550 * \ref cy_en_scb_spi_sclk_mode_t
1551 *
1552 * \return
1553 * \ref cy_en_scb_spi_sclk_mode_t
1554 *
1555 *******************************************************************************/
CY_SCB_SPI_GetSclkMode(cy_en_scb_spi_sub_mode_t subMode,cy_en_scb_spi_sclk_mode_t sclkMode)1556 __STATIC_INLINE uint32_t CY_SCB_SPI_GetSclkMode(cy_en_scb_spi_sub_mode_t subMode , cy_en_scb_spi_sclk_mode_t sclkMode)
1557 {
1558 uint32_t retVal;
1559 switch (subMode)
1560 {
1561 case CY_SCB_SPI_TI_PRECEDES:
1562 case CY_SCB_SPI_TI_COINCIDES:
1563 retVal = (uint32_t) CY_SCB_SPI_CPHA1_CPOL0;
1564 break;
1565 case CY_SCB_SPI_NATIONAL:
1566 retVal = (uint32_t) CY_SCB_SPI_CPHA0_CPOL0;
1567 break;
1568 case CY_SCB_SPI_MOTOROLA:
1569 retVal = (uint32_t) sclkMode;
1570 break;
1571 default:
1572 retVal = (uint32_t) sclkMode;
1573 break;
1574 }
1575 return retVal;
1576 }
1577 /** \endcond */
1578
1579 /** \} group_scb_spi_interrupt_functions */
1580
1581 #if defined(__cplusplus)
1582 }
1583 #endif
1584
1585 /** \} group_scb_spi */
1586
1587 #endif /* (defined (CY_IP_MXSCB) || defined (CY_IP_MXS22SCB)) */
1588
1589 #endif /* (CY_SCB_SPI_H) */
1590
1591 /* [] END OF FILE */
1592