1 /**
2  * \file
3  *
4  * \brief SAM I2S - Inter-IC Sound Controller
5  *
6  * Copyright (C) 2014-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 I2S_CALLBACK_H_INCLUDED
48 #define I2S_CALLBACK_H_INCLUDED
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /**
55  * \addtogroup asfdoc_sam0_i2s_group
56  *
57  * @{
58  */
59 
60 #include <i2s.h>
61 
62 /**
63  * Enum for the possible types of I<SUP>2</SUP>S asynchronous jobs that may be issued to
64  * the driver.
65  */
66 enum i2s_job_type {
67 	/** Asynchronous I<SUP>2</SUP>S write from a user provided buffer */
68 	I2S_JOB_WRITE_BUFFER,
69 	/** Asynchronous I<SUP>2</SUP>S read into a user provided buffer */
70 	I2S_JOB_READ_BUFFER
71 };
72 
73 /**
74  * \name Callback Management
75  * @{
76  */
77 
78 /**
79  * \brief Registers a callback for serializer
80  *
81  * Registers a callback function which is implemented by the user.
82  *
83  * \note The callback must be enabled by for the interrupt handler to call it
84  * when the condition for the callback is met.
85  *
86  * \param[in] module         Pointer to ADC software instance struct
87  * \param[in] serializer     The serializer that generates callback
88  * \param[in] callback_func  Pointer to callback function
89  * \param[in] callback_type  Callback type given by an enum
90  *
91  */
i2s_serializer_register_callback(struct i2s_module * const module_inst,const enum i2s_serializer serializer,const i2s_serializer_callback_t callback_func,const enum i2s_serializer_callback callback_type)92 static inline void i2s_serializer_register_callback(
93 		struct i2s_module *const module_inst,
94 		const enum i2s_serializer serializer,
95 		const i2s_serializer_callback_t callback_func,
96 		const enum i2s_serializer_callback callback_type)
97 {
98 	/* Sanity check arguments */
99 	Assert(module_inst);
100 	Assert(serializer < I2S_SERIALIZER_N);
101 
102 	module_inst->serializer[serializer].callback[callback_type] = callback_func;
103 	module_inst->serializer[serializer].registered_callback_mask |=
104 			(1u << callback_type);
105 }
106 
107 /**
108  * \brief Unregisters a callback for serializer
109  *
110  * Unregisters a callback function which is implemented by the user.
111  *
112  * \param[in] module         Pointer to ADC software instance struct
113  * \param[in] serializer     The serializer that generates callback
114  * \param[in] callback_type  Callback type given by an enum
115  *
116  */
i2s_serializer_unregister_callback(struct i2s_module * const module_inst,const enum i2s_serializer serializer,const enum i2s_serializer_callback callback_type)117 static inline void i2s_serializer_unregister_callback(
118 		struct i2s_module *const module_inst,
119 		const enum i2s_serializer serializer,
120 		const enum i2s_serializer_callback callback_type)
121 {
122 	/* Sanity check arguments */
123 	Assert(module_inst);
124 	Assert(serializer < I2S_SERIALIZER_N);
125 
126 	module_inst->serializer[serializer].callback[callback_type] = NULL;
127 	module_inst->serializer[serializer].registered_callback_mask &=
128 			~(1u << callback_type);
129 }
130 
131 /**
132  * \brief Enables callback for serializer
133  *
134  * Enables the callback function registered by \ref
135  * i2s_serializer_register_callback. The callback function will be called from
136  * the interrupt handler when the conditions for the callback type are met.
137  *
138  * \param[in] module         Pointer to ADC software instance struct
139  * \param[in] serializer     The serializer that generates callback
140  * \param[in] callback_type  Callback type given by an enum
141  *
142  */
i2s_serializer_enable_callback(struct i2s_module * const module_inst,const enum i2s_serializer serializer,const enum i2s_serializer_callback callback_type)143 static inline void i2s_serializer_enable_callback(
144 		struct i2s_module *const module_inst,
145 		const enum i2s_serializer serializer,
146 		const enum i2s_serializer_callback callback_type)
147 {
148 	/* Sanity check arguments */
149 	Assert(module_inst);
150 	Assert(module_inst->hw);
151 	Assert(serializer < I2S_SERIALIZER_N);
152 
153 	module_inst->serializer[serializer].enabled_callback_mask |=
154 			(1u << callback_type);
155 	if (I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN != callback_type) {
156 		return;
157 	}
158 	module_inst->hw->INTENSET.reg =
159 		(module_inst->serializer[serializer].mode == I2S_SERIALIZER_TRANSMIT) ?
160 			(I2S_INTFLAG_TXUR0 << serializer) :
161 			(I2S_INTFLAG_RXOR0 << serializer);
162 }
163 
164 /**
165  * \brief Disables callback for Serializer
166  *
167  * Disables the callback function registered by the \ref
168  * i2s_serializer_register_callback.
169  *
170  * \param[in] module         Pointer to ADC software instance struct
171  * \param[in] serializer     The serializer that generates callback
172  * \param[in] callback_type  Callback type given by an enum
173  *
174  */
i2s_serializer_disable_callback(struct i2s_module * const module_inst,const enum i2s_serializer serializer,const enum i2s_serializer_callback callback_type)175 static inline void i2s_serializer_disable_callback(
176 		struct i2s_module *const module_inst,
177 		const enum i2s_serializer serializer,
178 		const enum i2s_serializer_callback callback_type)
179 {
180 	/* Sanity check arguments */
181 	Assert(module_inst);
182 	Assert(module_inst->hw);
183 	Assert(serializer < I2S_SERIALIZER_N);
184 
185 	module_inst->serializer[serializer].enabled_callback_mask &=
186 			~(1u << callback_type);
187 	if (I2S_SERIALIZER_CALLBACK_OVER_UNDER_RUN != callback_type) {
188 		return;
189 	}
190 	module_inst->hw->INTENCLR.reg =
191 		(module_inst->serializer[serializer].mode == I2S_SERIALIZER_TRANSMIT) ?
192 			(I2S_INTFLAG_TXUR0 << serializer) :
193 			(I2S_INTFLAG_RXOR0 << serializer);
194 }
195 
196 /** @} */
197 
198 /**
199  * \name Job Management
200  *
201  * @{
202  */
203 
204 enum status_code i2s_serializer_write_buffer_job(
205 		struct i2s_module *const module_inst,
206 		const enum i2s_serializer serializer,
207 		const void *buffer,
208 		const uint32_t size);
209 
210 enum status_code i2s_serializer_read_buffer_job(
211 		struct i2s_module *const module_inst,
212 		const enum i2s_serializer serializer,
213 		void *buffer,
214 		const uint32_t size);
215 
216 void i2s_serializer_abort_job(
217 		struct i2s_module *const module_inst,
218 		const enum i2s_serializer serializer,
219 		const enum i2s_job_type job_type);
220 
221 enum status_code i2s_serializer_get_job_status(
222 		const struct i2s_module *const module_inst,
223 		const enum i2s_serializer serializer,
224 		const enum i2s_job_type job_type);
225 
226 /** @} */
227 
228 /** @} */
229 
230 #ifdef __cplusplus
231 }
232 #endif
233 
234 #endif /* #ifndef I2S_CALLBACK_H_INCLUDED */
235