1 /**
2  * \file
3  *
4  * \brief SAM Serial Peripheral Interface Driver (Callback Mode)
5  *
6  * Copyright (C) 2013-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  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 
47 #ifndef SPI_INTERRUPT_H_INCLUDED
48 #define SPI_INTERRUPT_H_INCLUDED
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /**
55  * \addtogroup asfdoc_sam0_sercom_spi_group
56  *
57  * @{
58  */
59 
60 #include "spi.h"
61 
62 /**
63  * \name Callback Management
64  * @{
65  */
66 
67 void spi_register_callback(
68 		struct spi_module *const module,
69 		spi_callback_t callback_func,
70 		enum spi_callback callback_type);
71 
72 void spi_unregister_callback(
73 		struct spi_module *module,
74 		enum spi_callback callback_type);
75 
76 /**
77  * \brief Enables an SPI callback of a given type
78  *
79  * Enables the callback function registered by the \ref spi_register_callback.
80  * The callback function will be called from the interrupt handler when the
81  * conditions for the callback type are met.
82  *
83  * \param[in] module         Pointer to SPI software instance struct
84  * \param[in] callback_type  Callback type given by an enum
85  */
spi_enable_callback(struct spi_module * const module,enum spi_callback callback_type)86 static inline void spi_enable_callback(
87 		struct spi_module *const module,
88 		enum spi_callback callback_type)
89 {
90 	/* Sanity check arguments */
91 	Assert(module);
92 
93 	/* Enable callback */
94 	module->enabled_callback |= (1 << callback_type);
95 }
96 
97 /**
98  * \brief Disables callback
99  *
100  * Disables the callback function registered by the \ref spi_register_callback,
101  * and the callback will not be called from the interrupt routine.
102  *
103  * \param[in] module         Pointer to SPI software instance struct
104  * \param[in] callback_type  Callback type given by an enum
105  */
spi_disable_callback(struct spi_module * const module,enum spi_callback callback_type)106 static inline void spi_disable_callback(
107 		struct spi_module *const module,
108 		enum spi_callback callback_type)
109 {
110 	/* Sanity check arguments */
111 	Assert(module);
112 
113 	/* Disable callback */
114 	module->enabled_callback &= ~(1 << callback_type);
115 }
116 
117 /** @} */
118 
119 
120 /**
121  * \name Writing and Reading
122  * @{
123  */
124 enum status_code spi_write_buffer_job(
125 		struct spi_module *const module,
126 		uint8_t *tx_data,
127 		uint16_t length);
128 
129 enum status_code spi_read_buffer_job(
130 		struct spi_module *const module,
131 		uint8_t *rx_data,
132 		uint16_t length,
133 		uint16_t dummy);
134 
135 enum status_code spi_transceive_buffer_job(
136 		struct spi_module *const module,
137 		uint8_t *tx_data,
138 		uint8_t *rx_data,
139 		uint16_t length);
140 
141 void spi_abort_job(
142 		struct spi_module *const module);
143 
144 /**
145  * \brief Retrieves the current status of a job.
146  *
147  * Retrieves the current status of a job that was previously issued.
148  *
149  * \param[in]  module    Pointer to SPI software instance struct
150  *
151  * \return Current job status.
152  */
spi_get_job_status(const struct spi_module * const module)153 static inline enum status_code spi_get_job_status(
154 		const struct spi_module *const module)
155 {
156 	return module->status;
157 }
158 
159 /**
160  * \brief Retrieves the status of job once it ends.
161  *
162  * Waits for current job status to become non-busy, then returns its value.
163  *
164  * \param[in]  module    Pointer to SPI software instance struct
165  *
166  * \return Current non-busy job status.
167  */
spi_get_job_status_wait(const struct spi_module * const module)168 static inline enum status_code spi_get_job_status_wait(
169 		const struct spi_module *const module)
170 {
171 	enum status_code status;
172 
173 	do {
174 		status = spi_get_job_status(module);
175 	} while (status == STATUS_BUSY);
176 
177 	return status;
178 }
179 
180 /** @} */
181 
182 /**
183  * @}
184  */
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif /* SPI_INTERRUPT_H_INCLUDED */
191