1 /**
2  * \file
3  *
4  * \brief SAM Serial Peripheral Interface Driver
5  *
6  * Copyright (C) 2012-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 #include "sercom_interrupt.h"
47 
48 void *_sercom_instances[SERCOM_INST_NUM];
49 
50 /** Save status of initialized handlers */
51 static bool _handler_table_initialized = false;
52 
53 /** Void pointers for saving device instance structures */
54 static void (*_sercom_interrupt_handlers[SERCOM_INST_NUM])(const uint8_t instance);
55 
56 /**
57  * \internal
58  * Default interrupt handler.
59  *
60  * \param[in] instance SERCOM instance used.
61  */
_sercom_default_handler(const uint8_t instance)62 static void _sercom_default_handler(
63 		const uint8_t instance)
64 {
65 	Assert(false);
66 }
67 
68 /**
69  * \internal
70  * Saves the given callback handler.
71  *
72  * \param[in]  instance           Instance index.
73  * \param[in]  interrupt_handler  Pointer to instance callback handler.
74  */
_sercom_set_handler(const uint8_t instance,const sercom_handler_t interrupt_handler)75 void _sercom_set_handler(
76 		const uint8_t instance,
77 		const sercom_handler_t interrupt_handler)
78 {
79 	/* Initialize handlers with default handler and device instances with 0 */
80 	if (_handler_table_initialized == false) {
81 		for (uint32_t i = 0; i < SERCOM_INST_NUM; i++) {
82 			_sercom_interrupt_handlers[i] = &_sercom_default_handler;
83 			_sercom_instances[i] = NULL;
84 		}
85 
86 		_handler_table_initialized = true;
87 	}
88 
89 	/* Save interrupt handler */
90 	_sercom_interrupt_handlers[instance] = interrupt_handler;
91 }
92 
93 
94 /** \internal
95  * Converts a given SERCOM index to its interrupt vector index.
96  */
97 #define _SERCOM_INTERRUPT_VECT_NUM(n, unused) \
98 		SYSTEM_INTERRUPT_MODULE_SERCOM##n,
99 
100 /** \internal
101  * Generates a SERCOM interrupt handler function for a given SERCOM index.
102  */
103 #define _SERCOM_INTERRUPT_HANDLER(n, unused) \
104 		void SERCOM##n##_Handler(void) \
105 		{ \
106 			_sercom_interrupt_handlers[n](n); \
107 		}
108 
109 /**
110  * \internal
111  * Returns the system interrupt vector.
112  *
113  * \param[in]  sercom_instance  Instance pointer
114  *
115  * \return Enum of system interrupt vector
116  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM0
117  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM1
118  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM2
119  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM3
120  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM4
121  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM5
122  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM6
123  * \retval SYSTEM_INTERRUPT_MODULE_SERCOM7
124  */
_sercom_get_interrupt_vector(Sercom * const sercom_instance)125 enum system_interrupt_vector _sercom_get_interrupt_vector(
126 		Sercom *const sercom_instance)
127 {
128 	const uint8_t sercom_int_vectors[SERCOM_INST_NUM] =
129 		{
130 			MREPEAT(SERCOM_INST_NUM, _SERCOM_INTERRUPT_VECT_NUM, ~)
131 		};
132 
133 	/* Retrieve the index of the SERCOM being requested */
134 	uint8_t instance_index = _sercom_get_sercom_inst_index(sercom_instance);
135 
136 	/* Get the vector number from the lookup table for the requested SERCOM */
137 	return (enum system_interrupt_vector)sercom_int_vectors[instance_index];
138 }
139 
140 /** Auto-generate a set of interrupt handlers for each SERCOM in the device */
141 MREPEAT(SERCOM_INST_NUM, _SERCOM_INTERRUPT_HANDLER, ~)
142