1 /**
2  * \file
3  *
4  * \brief SAM SERCOM I2C Slave Interrupt Driver
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 I2C_SLAVE_INTERRUPT_H_INCLUDED
48 #define I2C_SLAVE_INTERRUPT_H_INCLUDED
49 
50 #include "i2c_slave.h"
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /**
57  * \addtogroup asfdoc_sam0_sercom_i2c_group
58  * @{
59  *
60  */
61 
62 /**
63  * \name Address Match Functionality
64  * @{
65  */
66 
67 void i2c_slave_enable_nack_on_address(
68 		struct i2c_slave_module *const module);
69 void i2c_slave_disable_nack_on_address(
70 		struct i2c_slave_module *const module);
71 
72 /** @} */
73 
74 /**
75  * \name Callbacks
76  * @{
77  */
78 #if !defined(__DOXYGEN__)
79 void _i2c_slave_interrupt_handler(uint8_t instance);
80 #endif
81 
82 void i2c_slave_register_callback(
83 		struct i2c_slave_module *const module,
84 		i2c_slave_callback_t callback,
85 		enum i2c_slave_callback callback_type);
86 
87 void i2c_slave_unregister_callback(
88 		struct i2c_slave_module *const module,
89 		enum i2c_slave_callback callback_type);
90 
91 /**
92  * \brief Enables callback
93  *
94  * Enables the callback specified by the callback_type.
95  *
96  * \param[in,out]  module         Pointer to the software module struct
97  * \param[in]      callback_type  Callback type to enable
98  */
i2c_slave_enable_callback(struct i2c_slave_module * const module,enum i2c_slave_callback callback_type)99 static inline void i2c_slave_enable_callback(
100 		struct i2c_slave_module *const module,
101 		enum i2c_slave_callback callback_type)
102 {
103 	/* Sanity check */
104 	Assert(module);
105 	Assert(module->hw);
106 
107 	/* Mark callback as enabled */
108 	module->enabled_callback |= (1 << callback_type);
109 
110 	/* Enable address callback */
111 	SercomI2cs *const i2c_hw = &(module->hw->I2CS);
112 	if (callback_type == I2C_SLAVE_CALLBACK_READ_REQUEST ||
113 			callback_type == I2C_SLAVE_CALLBACK_WRITE_REQUEST) {
114 		i2c_hw->INTENSET.reg = SERCOM_I2CS_INTFLAG_AMATCH;
115 	}
116 }
117 
118 /**
119  * \brief Disables callback
120  *
121  * Disables the callback specified by the callback_type.
122  *
123  * \param[in,out]  module         Pointer to the software module struct
124  * \param[in]      callback_type  Callback type to disable
125  */
i2c_slave_disable_callback(struct i2c_slave_module * const module,enum i2c_slave_callback callback_type)126 static inline void i2c_slave_disable_callback(
127 		struct i2c_slave_module *const module,
128 		enum i2c_slave_callback callback_type)
129 {
130 	/* Sanity check */
131 	Assert(module);
132 	Assert(module->hw);
133 
134 	/* Mark callback as disabled */
135 	module->enabled_callback &= ~(1 << callback_type);
136 	SercomI2cs *const i2c_hw = &(module->hw->I2CS);
137 	if (callback_type == I2C_SLAVE_CALLBACK_READ_REQUEST ||
138 			callback_type == I2C_SLAVE_CALLBACK_WRITE_REQUEST ||
139 			module->status != STATUS_BUSY) {
140 		i2c_hw->INTENCLR.reg = SERCOM_I2CS_INTFLAG_AMATCH;
141 	}
142 }
143 
144 /** @} */
145 
146 /**
147  * \name Read and Write, Interrupt-Driven
148  * @{
149  */
150 
151 
152 enum status_code i2c_slave_read_packet_job(
153 		struct i2c_slave_module *const module,
154 		struct i2c_slave_packet *const packet);
155 
156 enum status_code i2c_slave_write_packet_job(
157 		struct i2c_slave_module *const module,
158 		struct i2c_slave_packet *const packet);
159 
160 /**
161  * \brief Cancels any currently ongoing operation
162  *
163  * Terminates the running transfer operation.
164  *
165  * \param[in,out] module  Pointer to software module structure
166  */
i2c_slave_cancel_job(struct i2c_slave_module * const module)167 static inline void i2c_slave_cancel_job(
168 		struct i2c_slave_module *const module)
169 {
170 	/* Sanity check. */
171 	Assert(module);
172 	Assert(module->hw);
173 
174 	/* Set buffer to 0. */
175 	module->buffer_remaining = 0;
176 	module->buffer_length = 0;
177 }
178 
179 /**
180  * \brief Gets status of ongoing job
181  *
182  * Will return the status of the ongoing job, or the error that occurred
183  * in the last transfer operation.
184  * The status will be cleared when starting a new job.
185  *
186  * \param[in,out] module Pointer to software module structure
187  *
188  * \return                      Status of job.
189  * \retval STATUS_OK            No error has occurred
190  * \retval STATUS_BUSY          Transfer is in progress
191  * \retval STATUS_ERR_IO        A collision, timeout or bus error happened in
192  *                              the last transfer
193  * \retval STATUS_ERR_TIMEOUT   A timeout occurred
194  * \retval STATUS_ERR_OVERFLOW  Data from master overflows receive buffer
195  */
i2c_slave_get_job_status(struct i2c_slave_module * const module)196 static inline enum status_code i2c_slave_get_job_status(
197 		struct i2c_slave_module *const module)
198 {
199 	/* Check sanity. */
200 	Assert(module);
201 	Assert(module->hw);
202 
203 	/* Return current status code. */
204 	return module->status;
205 }
206 
207 /** @} */
208 
209 /** @} */
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif /* I2C_SLAVE_INTERRUPT_H_INCLUDED */
216