Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/tc/tc_interrupt.c@15:a81a8d6c1dfe, 2015-11-04 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Nov 04 16:30:11 2015 +0000
- Revision:
- 15:a81a8d6c1dfe
Synchronized with git revision 46af745ef4405614c3fa49abbd9a706a362ea514
Full URL: https://github.com/mbedmicro/mbed/commit/46af745ef4405614c3fa49abbd9a706a362ea514/
Renamed TARGET_SAM_CortexM0+ to TARGET_SAM_CortexM0P for compatiblity with online compiler
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 15:a81a8d6c1dfe | 1 | /** |
| mbed_official | 15:a81a8d6c1dfe | 2 | * \file |
| mbed_official | 15:a81a8d6c1dfe | 3 | * |
| mbed_official | 15:a81a8d6c1dfe | 4 | * \brief SAM TC - Timer Counter Callback Driver |
| mbed_official | 15:a81a8d6c1dfe | 5 | * |
| mbed_official | 15:a81a8d6c1dfe | 6 | * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved. |
| mbed_official | 15:a81a8d6c1dfe | 7 | * |
| mbed_official | 15:a81a8d6c1dfe | 8 | * \asf_license_start |
| mbed_official | 15:a81a8d6c1dfe | 9 | * |
| mbed_official | 15:a81a8d6c1dfe | 10 | * \page License |
| mbed_official | 15:a81a8d6c1dfe | 11 | * |
| mbed_official | 15:a81a8d6c1dfe | 12 | * Redistribution and use in source and binary forms, with or without |
| mbed_official | 15:a81a8d6c1dfe | 13 | * modification, are permitted provided that the following conditions are met: |
| mbed_official | 15:a81a8d6c1dfe | 14 | * |
| mbed_official | 15:a81a8d6c1dfe | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
| mbed_official | 15:a81a8d6c1dfe | 16 | * this list of conditions and the following disclaimer. |
| mbed_official | 15:a81a8d6c1dfe | 17 | * |
| mbed_official | 15:a81a8d6c1dfe | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| mbed_official | 15:a81a8d6c1dfe | 19 | * this list of conditions and the following disclaimer in the documentation |
| mbed_official | 15:a81a8d6c1dfe | 20 | * and/or other materials provided with the distribution. |
| mbed_official | 15:a81a8d6c1dfe | 21 | * |
| mbed_official | 15:a81a8d6c1dfe | 22 | * 3. The name of Atmel may not be used to endorse or promote products derived |
| mbed_official | 15:a81a8d6c1dfe | 23 | * from this software without specific prior written permission. |
| mbed_official | 15:a81a8d6c1dfe | 24 | * |
| mbed_official | 15:a81a8d6c1dfe | 25 | * 4. This software may only be redistributed and used in connection with an |
| mbed_official | 15:a81a8d6c1dfe | 26 | * Atmel microcontroller product. |
| mbed_official | 15:a81a8d6c1dfe | 27 | * |
| mbed_official | 15:a81a8d6c1dfe | 28 | * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED |
| mbed_official | 15:a81a8d6c1dfe | 29 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| mbed_official | 15:a81a8d6c1dfe | 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| mbed_official | 15:a81a8d6c1dfe | 31 | * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR |
| mbed_official | 15:a81a8d6c1dfe | 32 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| mbed_official | 15:a81a8d6c1dfe | 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| mbed_official | 15:a81a8d6c1dfe | 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| mbed_official | 15:a81a8d6c1dfe | 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| mbed_official | 15:a81a8d6c1dfe | 36 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| mbed_official | 15:a81a8d6c1dfe | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| mbed_official | 15:a81a8d6c1dfe | 38 | * POSSIBILITY OF SUCH DAMAGE. |
| mbed_official | 15:a81a8d6c1dfe | 39 | * |
| mbed_official | 15:a81a8d6c1dfe | 40 | * \asf_license_stop |
| mbed_official | 15:a81a8d6c1dfe | 41 | * |
| mbed_official | 15:a81a8d6c1dfe | 42 | */ |
| mbed_official | 15:a81a8d6c1dfe | 43 | |
| mbed_official | 15:a81a8d6c1dfe | 44 | /* |
| mbed_official | 15:a81a8d6c1dfe | 45 | * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> |
| mbed_official | 15:a81a8d6c1dfe | 46 | */ |
| mbed_official | 15:a81a8d6c1dfe | 47 | |
| mbed_official | 15:a81a8d6c1dfe | 48 | #include "tc_interrupt.h" |
| mbed_official | 15:a81a8d6c1dfe | 49 | |
| mbed_official | 15:a81a8d6c1dfe | 50 | void *_tc_instances[TC_INST_NUM]; |
| mbed_official | 15:a81a8d6c1dfe | 51 | |
| mbed_official | 15:a81a8d6c1dfe | 52 | void _tc_interrupt_handler(uint8_t instance); |
| mbed_official | 15:a81a8d6c1dfe | 53 | |
| mbed_official | 15:a81a8d6c1dfe | 54 | /** |
| mbed_official | 15:a81a8d6c1dfe | 55 | * \brief Registers a callback. |
| mbed_official | 15:a81a8d6c1dfe | 56 | * |
| mbed_official | 15:a81a8d6c1dfe | 57 | * Registers a callback function which is implemented by the user. |
| mbed_official | 15:a81a8d6c1dfe | 58 | * |
| mbed_official | 15:a81a8d6c1dfe | 59 | * \note The callback must be enabled by \ref tc_enable_callback, |
| mbed_official | 15:a81a8d6c1dfe | 60 | * in order for the interrupt handler to call it when the conditions for the |
| mbed_official | 15:a81a8d6c1dfe | 61 | * callback type is met. |
| mbed_official | 15:a81a8d6c1dfe | 62 | * |
| mbed_official | 15:a81a8d6c1dfe | 63 | * \param[in] module Pointer to TC software instance struct |
| mbed_official | 15:a81a8d6c1dfe | 64 | * \param[in] callback_func Pointer to callback function |
| mbed_official | 15:a81a8d6c1dfe | 65 | * \param[in] callback_type Callback type given by an enum |
| mbed_official | 15:a81a8d6c1dfe | 66 | */ |
| mbed_official | 15:a81a8d6c1dfe | 67 | enum status_code tc_register_callback( |
| mbed_official | 15:a81a8d6c1dfe | 68 | struct tc_module *const module, |
| mbed_official | 15:a81a8d6c1dfe | 69 | tc_callback_t callback_func, |
| mbed_official | 15:a81a8d6c1dfe | 70 | const enum tc_callback callback_type) |
| mbed_official | 15:a81a8d6c1dfe | 71 | { |
| mbed_official | 15:a81a8d6c1dfe | 72 | /* Sanity check arguments */ |
| mbed_official | 15:a81a8d6c1dfe | 73 | Assert(module); |
| mbed_official | 15:a81a8d6c1dfe | 74 | Assert(callback_func); |
| mbed_official | 15:a81a8d6c1dfe | 75 | |
| mbed_official | 15:a81a8d6c1dfe | 76 | /* Register callback function */ |
| mbed_official | 15:a81a8d6c1dfe | 77 | module->callback[callback_type] = callback_func; |
| mbed_official | 15:a81a8d6c1dfe | 78 | |
| mbed_official | 15:a81a8d6c1dfe | 79 | /* Set the bit corresponding to the callback_type */ |
| mbed_official | 15:a81a8d6c1dfe | 80 | if (callback_type == TC_CALLBACK_CC_CHANNEL0) { |
| mbed_official | 15:a81a8d6c1dfe | 81 | module->register_callback_mask |= TC_INTFLAG_MC(1); |
| mbed_official | 15:a81a8d6c1dfe | 82 | } else if (callback_type == TC_CALLBACK_CC_CHANNEL1) { |
| mbed_official | 15:a81a8d6c1dfe | 83 | module->register_callback_mask |= TC_INTFLAG_MC(2); |
| mbed_official | 15:a81a8d6c1dfe | 84 | } else { |
| mbed_official | 15:a81a8d6c1dfe | 85 | module->register_callback_mask |= (1 << callback_type); |
| mbed_official | 15:a81a8d6c1dfe | 86 | } |
| mbed_official | 15:a81a8d6c1dfe | 87 | return STATUS_OK; |
| mbed_official | 15:a81a8d6c1dfe | 88 | } |
| mbed_official | 15:a81a8d6c1dfe | 89 | |
| mbed_official | 15:a81a8d6c1dfe | 90 | /** |
| mbed_official | 15:a81a8d6c1dfe | 91 | * \brief Unregisters a callback. |
| mbed_official | 15:a81a8d6c1dfe | 92 | * |
| mbed_official | 15:a81a8d6c1dfe | 93 | * Unregisters a callback function implemented by the user. The callback should be |
| mbed_official | 15:a81a8d6c1dfe | 94 | * disabled before it is unregistered. |
| mbed_official | 15:a81a8d6c1dfe | 95 | * |
| mbed_official | 15:a81a8d6c1dfe | 96 | * \param[in] module Pointer to TC software instance struct |
| mbed_official | 15:a81a8d6c1dfe | 97 | * \param[in] callback_type Callback type given by an enum |
| mbed_official | 15:a81a8d6c1dfe | 98 | */ |
| mbed_official | 15:a81a8d6c1dfe | 99 | enum status_code tc_unregister_callback( |
| mbed_official | 15:a81a8d6c1dfe | 100 | struct tc_module *const module, |
| mbed_official | 15:a81a8d6c1dfe | 101 | const enum tc_callback callback_type) |
| mbed_official | 15:a81a8d6c1dfe | 102 | { |
| mbed_official | 15:a81a8d6c1dfe | 103 | /* Sanity check arguments */ |
| mbed_official | 15:a81a8d6c1dfe | 104 | Assert(module); |
| mbed_official | 15:a81a8d6c1dfe | 105 | |
| mbed_official | 15:a81a8d6c1dfe | 106 | /* Unregister callback function */ |
| mbed_official | 15:a81a8d6c1dfe | 107 | module->callback[callback_type] = NULL; |
| mbed_official | 15:a81a8d6c1dfe | 108 | |
| mbed_official | 15:a81a8d6c1dfe | 109 | /* Clear the bit corresponding to the callback_type */ |
| mbed_official | 15:a81a8d6c1dfe | 110 | if (callback_type == TC_CALLBACK_CC_CHANNEL0) { |
| mbed_official | 15:a81a8d6c1dfe | 111 | module->register_callback_mask &= ~TC_INTFLAG_MC(1); |
| mbed_official | 15:a81a8d6c1dfe | 112 | } else if (callback_type == TC_CALLBACK_CC_CHANNEL1) { |
| mbed_official | 15:a81a8d6c1dfe | 113 | module->register_callback_mask &= ~TC_INTFLAG_MC(2); |
| mbed_official | 15:a81a8d6c1dfe | 114 | } else { |
| mbed_official | 15:a81a8d6c1dfe | 115 | module->register_callback_mask &= ~(1 << callback_type); |
| mbed_official | 15:a81a8d6c1dfe | 116 | } |
| mbed_official | 15:a81a8d6c1dfe | 117 | return STATUS_OK; |
| mbed_official | 15:a81a8d6c1dfe | 118 | } |
| mbed_official | 15:a81a8d6c1dfe | 119 | |
| mbed_official | 15:a81a8d6c1dfe | 120 | /** |
| mbed_official | 15:a81a8d6c1dfe | 121 | * \internal ISR handler for TC |
| mbed_official | 15:a81a8d6c1dfe | 122 | * |
| mbed_official | 15:a81a8d6c1dfe | 123 | * Auto-generate a set of interrupt handlers for each TC in the device. |
| mbed_official | 15:a81a8d6c1dfe | 124 | */ |
| mbed_official | 15:a81a8d6c1dfe | 125 | #define _TC_INTERRUPT_HANDLER(n, m) \ |
| mbed_official | 15:a81a8d6c1dfe | 126 | void TC##n##_Handler(void) \ |
| mbed_official | 15:a81a8d6c1dfe | 127 | { \ |
| mbed_official | 15:a81a8d6c1dfe | 128 | _tc_interrupt_handler(m); \ |
| mbed_official | 15:a81a8d6c1dfe | 129 | } |
| mbed_official | 15:a81a8d6c1dfe | 130 | |
| mbed_official | 15:a81a8d6c1dfe | 131 | #if (SAML21E) || (SAML21G) |
| mbed_official | 15:a81a8d6c1dfe | 132 | _TC_INTERRUPT_HANDLER(0,0) |
| mbed_official | 15:a81a8d6c1dfe | 133 | _TC_INTERRUPT_HANDLER(1,1) |
| mbed_official | 15:a81a8d6c1dfe | 134 | _TC_INTERRUPT_HANDLER(4,2) |
| mbed_official | 15:a81a8d6c1dfe | 135 | #else |
| mbed_official | 15:a81a8d6c1dfe | 136 | MRECURSION(TC_INST_NUM, _TC_INTERRUPT_HANDLER, TC_INST_MAX_ID) |
| mbed_official | 15:a81a8d6c1dfe | 137 | #endif |
| mbed_official | 15:a81a8d6c1dfe | 138 | |
| mbed_official | 15:a81a8d6c1dfe | 139 | |
| mbed_official | 15:a81a8d6c1dfe | 140 | /** |
| mbed_official | 15:a81a8d6c1dfe | 141 | * \internal Interrupt Handler for TC module |
| mbed_official | 15:a81a8d6c1dfe | 142 | * |
| mbed_official | 15:a81a8d6c1dfe | 143 | * Handles interrupts as they occur, it will run the callback functions |
| mbed_official | 15:a81a8d6c1dfe | 144 | * that are registered and enabled. |
| mbed_official | 15:a81a8d6c1dfe | 145 | * |
| mbed_official | 15:a81a8d6c1dfe | 146 | * \param[in] instance ID of the TC instance calling the interrupt |
| mbed_official | 15:a81a8d6c1dfe | 147 | * handler. |
| mbed_official | 15:a81a8d6c1dfe | 148 | */ |
| mbed_official | 15:a81a8d6c1dfe | 149 | void _tc_interrupt_handler( |
| mbed_official | 15:a81a8d6c1dfe | 150 | uint8_t instance) |
| mbed_official | 15:a81a8d6c1dfe | 151 | { |
| mbed_official | 15:a81a8d6c1dfe | 152 | /* Temporary variable */ |
| mbed_official | 15:a81a8d6c1dfe | 153 | uint8_t interrupt_and_callback_status_mask; |
| mbed_official | 15:a81a8d6c1dfe | 154 | |
| mbed_official | 15:a81a8d6c1dfe | 155 | /* Get device instance from the look-up table */ |
| mbed_official | 15:a81a8d6c1dfe | 156 | struct tc_module *module |
| mbed_official | 15:a81a8d6c1dfe | 157 | = (struct tc_module *)_tc_instances[instance]; |
| mbed_official | 15:a81a8d6c1dfe | 158 | |
| mbed_official | 15:a81a8d6c1dfe | 159 | /* Read and mask interrupt flag register */ |
| mbed_official | 15:a81a8d6c1dfe | 160 | interrupt_and_callback_status_mask = module->hw->COUNT8.INTFLAG.reg & |
| mbed_official | 15:a81a8d6c1dfe | 161 | module->register_callback_mask & |
| mbed_official | 15:a81a8d6c1dfe | 162 | module->enable_callback_mask; |
| mbed_official | 15:a81a8d6c1dfe | 163 | |
| mbed_official | 15:a81a8d6c1dfe | 164 | /* Check if an Overflow interrupt has occurred */ |
| mbed_official | 15:a81a8d6c1dfe | 165 | if (interrupt_and_callback_status_mask & TC_INTFLAG_OVF) { |
| mbed_official | 15:a81a8d6c1dfe | 166 | /* Invoke registered and enabled callback function */ |
| mbed_official | 15:a81a8d6c1dfe | 167 | (module->callback[TC_CALLBACK_OVERFLOW])(module); |
| mbed_official | 15:a81a8d6c1dfe | 168 | /* Clear interrupt flag */ |
| mbed_official | 15:a81a8d6c1dfe | 169 | module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_OVF; |
| mbed_official | 15:a81a8d6c1dfe | 170 | } |
| mbed_official | 15:a81a8d6c1dfe | 171 | |
| mbed_official | 15:a81a8d6c1dfe | 172 | /* Check if an Error interrupt has occurred */ |
| mbed_official | 15:a81a8d6c1dfe | 173 | if (interrupt_and_callback_status_mask & TC_INTFLAG_ERR) { |
| mbed_official | 15:a81a8d6c1dfe | 174 | /* Invoke registered and enabled callback function */ |
| mbed_official | 15:a81a8d6c1dfe | 175 | (module->callback[TC_CALLBACK_ERROR])(module); |
| mbed_official | 15:a81a8d6c1dfe | 176 | /* Clear interrupt flag */ |
| mbed_official | 15:a81a8d6c1dfe | 177 | module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_ERR; |
| mbed_official | 15:a81a8d6c1dfe | 178 | } |
| mbed_official | 15:a81a8d6c1dfe | 179 | |
| mbed_official | 15:a81a8d6c1dfe | 180 | /* Check if an Match/Capture Channel 0 interrupt has occurred */ |
| mbed_official | 15:a81a8d6c1dfe | 181 | if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(1)) { |
| mbed_official | 15:a81a8d6c1dfe | 182 | /* Invoke registered and enabled callback function */ |
| mbed_official | 15:a81a8d6c1dfe | 183 | (module->callback[TC_CALLBACK_CC_CHANNEL0])(module); |
| mbed_official | 15:a81a8d6c1dfe | 184 | /* Clear interrupt flag */ |
| mbed_official | 15:a81a8d6c1dfe | 185 | module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(1); |
| mbed_official | 15:a81a8d6c1dfe | 186 | } |
| mbed_official | 15:a81a8d6c1dfe | 187 | |
| mbed_official | 15:a81a8d6c1dfe | 188 | /* Check if an Match/Capture Channel 1 interrupt has occurred */ |
| mbed_official | 15:a81a8d6c1dfe | 189 | if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(2)) { |
| mbed_official | 15:a81a8d6c1dfe | 190 | /* Invoke registered and enabled callback function */ |
| mbed_official | 15:a81a8d6c1dfe | 191 | (module->callback[TC_CALLBACK_CC_CHANNEL1])(module); |
| mbed_official | 15:a81a8d6c1dfe | 192 | /* Clear interrupt flag */ |
| mbed_official | 15:a81a8d6c1dfe | 193 | module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(2); |
| mbed_official | 15:a81a8d6c1dfe | 194 | } |
| mbed_official | 15:a81a8d6c1dfe | 195 | } |
