1 /**
2  * \file
3  *
4  * \brief Common SPI related functionality declaration.
5  *
6  * Copyright (C) 2015 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 
44 #ifndef _HPL_SPI_ASYNC_H_INCLUDED
45 #define _HPL_SPI_ASYNC_H_INCLUDED
46 
47 #include <hpl_spi.h>
48 #include <hpl_irq.h>
49 
50 /**
51  * \addtogroup hpl_spi HPL SPI
52  *
53  *@{
54  */
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /**
61  *  \brief Callbacks the SPI driver must offer in async mode
62  */
63 //@{
64 /** The callback types */
65 enum _spi_async_dev_cb_type {
66 	/** Callback type for transmit, see \ref _spi_async_dev_cb_xfer_t. */
67 	SPI_DEV_CB_TX,
68 	/** Callback type for receive, see \ref _spi_async_dev_cb_xfer_t. */
69 	SPI_DEV_CB_RX,
70 	/** Callback type for \ref _spi_async_dev_cb_complete_t. */
71 	SPI_DEV_CB_COMPLETE,
72 	/** Number of callbacks. */
73 	SPI_DEV_CB_N
74 };
75 
76 struct _spi_async_dev;
77 
78 /** \brief The prototype for callback on SPI transfer complete.
79  *  If status code is zero, it indicates the normal completion, that is,
80  *  SS deactivation.
81  *  If status code belows zero, it indicates complete.
82  */
83 typedef void (*_spi_async_dev_cb_complete_t)(struct _spi_async_dev *dev, int32_t status);
84 
85 /** \brief The prototype for callback on SPI transmit/receive event
86  *  For TX, the callback is invoked when transmit is done or ready to start
87  *  transmit.
88  *  For RX, the callback is invoked when receive is done or ready to read data,
89  *  see \ref _spi_async_dev_read_one_t on data reading.
90  *  Without DMA enabled, the callback is invoked on each character event.
91  *  With DMA enabled, the callback is invoked on DMA buffer done.
92  */
93 typedef void (*_spi_async_dev_cb_xfer_t)(struct _spi_async_dev *dev);
94 
95 /**
96  *  \brief The callbacks offered by SPI driver
97  */
98 struct _spi_async_dev_callbacks {
99 	/** TX callback, see \ref _spi_async_dev_cb_xfer_t. */
100 	_spi_async_dev_cb_xfer_t tx;
101 	/** RX callback, see \ref _spi_async_dev_cb_xfer_t. */
102 	_spi_async_dev_cb_xfer_t rx;
103 	/** Complete or complete callback, see \ref _spi_async_dev_cb_complete_t. */
104 	_spi_async_dev_cb_complete_t complete;
105 };
106 //@}
107 
108 /**
109  *  \brief SPI async driver
110  */
111 //@{
112 
113 /** SPI driver to support async HAL */
114 struct _spi_async_dev {
115 	/** Pointer to the hardware base or private data for special device. */
116 	void *prvt;
117 	/** Data size, number of bytes for each character */
118 	uint8_t char_size;
119 	/** Dummy byte used in master mode when reading the slave */
120 	uint16_t dummy_byte;
121 
122 	/** \brief Pointer to callback functions, ignored for polling mode
123 	 *  Pointer to the callback functions so that initialize the driver to
124 	 *  handle interrupts.
125 	 */
126 	struct _spi_async_dev_callbacks callbacks;
127 	/** IRQ instance for SPI device. */
128 	struct _irq_descriptor irq;
129 };
130 //@}
131 
132 #ifdef __cplusplus
133 }
134 #endif
135 
136 /**@}*/
137 #endif /* ifndef _HPL_SPI_ASYNC_H_INCLUDED */
138