t
Fork of mbed-dev by
targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/tc/TARGET_SAMR21/tc.c@18:da299f395b9e, 2015-11-09 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Nov 09 13:30:11 2015 +0000
- Revision:
- 18:da299f395b9e
- Parent:
- targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/tc/tc_sam_d_r/tc.c@15:a81a8d6c1dfe
Synchronized with git revision f605825f66bb2e462ff7dbc5fb4ed2dbe979d1c3
Full URL: https://github.com/mbedmicro/mbed/commit/f605825f66bb2e462ff7dbc5fb4ed2dbe979d1c3/
Added support for SAML21
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 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 | * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> |
mbed_official | 15:a81a8d6c1dfe | 45 | */ |
mbed_official | 15:a81a8d6c1dfe | 46 | |
mbed_official | 15:a81a8d6c1dfe | 47 | #include "tc.h" |
mbed_official | 15:a81a8d6c1dfe | 48 | |
mbed_official | 15:a81a8d6c1dfe | 49 | #if TC_ASYNC == true |
mbed_official | 15:a81a8d6c1dfe | 50 | # include "tc_interrupt.h" |
mbed_official | 15:a81a8d6c1dfe | 51 | # include <system_interrupt.h> |
mbed_official | 15:a81a8d6c1dfe | 52 | |
mbed_official | 15:a81a8d6c1dfe | 53 | /** \internal |
mbed_official | 15:a81a8d6c1dfe | 54 | * Converts a given TC index to its interrupt vector index. |
mbed_official | 15:a81a8d6c1dfe | 55 | */ |
mbed_official | 15:a81a8d6c1dfe | 56 | # define _TC_INTERRUPT_VECT_NUM(n, unused) \ |
mbed_official | 15:a81a8d6c1dfe | 57 | SYSTEM_INTERRUPT_MODULE_TC##n, |
mbed_official | 15:a81a8d6c1dfe | 58 | #endif |
mbed_official | 15:a81a8d6c1dfe | 59 | |
mbed_official | 15:a81a8d6c1dfe | 60 | #if !defined(__DOXYGEN__) |
mbed_official | 15:a81a8d6c1dfe | 61 | # define _TC_GCLK_ID(n,unused) TPASTE3(TC,n,_GCLK_ID) , |
mbed_official | 15:a81a8d6c1dfe | 62 | # define _TC_PM_APBCMASK(n,unused) TPASTE2(PM_APBCMASK_TC,n) , |
mbed_official | 15:a81a8d6c1dfe | 63 | |
mbed_official | 15:a81a8d6c1dfe | 64 | # define TC_INST_GCLK_ID { MRECURSION(TC_INST_NUM, _TC_GCLK_ID, TC_INST_MAX_ID) } |
mbed_official | 15:a81a8d6c1dfe | 65 | # define TC_INST_PM_APBCMASK { MRECURSION(TC_INST_NUM, _TC_PM_APBCMASK, TC_INST_MAX_ID) } |
mbed_official | 15:a81a8d6c1dfe | 66 | |
mbed_official | 15:a81a8d6c1dfe | 67 | #endif |
mbed_official | 15:a81a8d6c1dfe | 68 | |
mbed_official | 15:a81a8d6c1dfe | 69 | /** |
mbed_official | 15:a81a8d6c1dfe | 70 | * \internal Find the index of given TC module instance. |
mbed_official | 15:a81a8d6c1dfe | 71 | * |
mbed_official | 15:a81a8d6c1dfe | 72 | * \param[in] TC module instance pointer. |
mbed_official | 15:a81a8d6c1dfe | 73 | * |
mbed_official | 15:a81a8d6c1dfe | 74 | * \return Index of the given TC module instance. |
mbed_official | 15:a81a8d6c1dfe | 75 | */ |
mbed_official | 15:a81a8d6c1dfe | 76 | uint8_t _tc_get_inst_index( |
mbed_official | 15:a81a8d6c1dfe | 77 | Tc *const hw) |
mbed_official | 15:a81a8d6c1dfe | 78 | { |
mbed_official | 15:a81a8d6c1dfe | 79 | /* List of available TC modules. */ |
mbed_official | 15:a81a8d6c1dfe | 80 | Tc *const tc_modules[TC_INST_NUM] = TC_INSTS; |
mbed_official | 15:a81a8d6c1dfe | 81 | |
mbed_official | 15:a81a8d6c1dfe | 82 | /* Find index for TC instance. */ |
mbed_official | 15:a81a8d6c1dfe | 83 | for (uint32_t i = 0; i < TC_INST_NUM; i++) { |
mbed_official | 15:a81a8d6c1dfe | 84 | if (hw == tc_modules[i]) { |
mbed_official | 15:a81a8d6c1dfe | 85 | return i; |
mbed_official | 15:a81a8d6c1dfe | 86 | } |
mbed_official | 15:a81a8d6c1dfe | 87 | } |
mbed_official | 15:a81a8d6c1dfe | 88 | |
mbed_official | 15:a81a8d6c1dfe | 89 | /* Invalid data given. */ |
mbed_official | 15:a81a8d6c1dfe | 90 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 91 | return 0; |
mbed_official | 15:a81a8d6c1dfe | 92 | } |
mbed_official | 15:a81a8d6c1dfe | 93 | |
mbed_official | 15:a81a8d6c1dfe | 94 | |
mbed_official | 15:a81a8d6c1dfe | 95 | /** |
mbed_official | 15:a81a8d6c1dfe | 96 | * \brief Initializes a hardware TC module instance. |
mbed_official | 15:a81a8d6c1dfe | 97 | * |
mbed_official | 15:a81a8d6c1dfe | 98 | * Enables the clock and initializes the TC module, based on the given |
mbed_official | 15:a81a8d6c1dfe | 99 | * configuration values. |
mbed_official | 15:a81a8d6c1dfe | 100 | * |
mbed_official | 15:a81a8d6c1dfe | 101 | * \param[in,out] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 102 | * \param[in] hw Pointer to the TC hardware module |
mbed_official | 15:a81a8d6c1dfe | 103 | * \param[in] config Pointer to the TC configuration options struct |
mbed_official | 15:a81a8d6c1dfe | 104 | * |
mbed_official | 15:a81a8d6c1dfe | 105 | * \return Status of the initialization procedure. |
mbed_official | 15:a81a8d6c1dfe | 106 | * |
mbed_official | 15:a81a8d6c1dfe | 107 | * \retval STATUS_OK The module was initialized successfully |
mbed_official | 15:a81a8d6c1dfe | 108 | * \retval STATUS_BUSY Hardware module was busy when the |
mbed_official | 15:a81a8d6c1dfe | 109 | * initialization procedure was attempted |
mbed_official | 15:a81a8d6c1dfe | 110 | * \retval STATUS_INVALID_ARG An invalid configuration option or argument |
mbed_official | 15:a81a8d6c1dfe | 111 | * was supplied |
mbed_official | 15:a81a8d6c1dfe | 112 | * \retval STATUS_ERR_DENIED Hardware module was already enabled, or the |
mbed_official | 15:a81a8d6c1dfe | 113 | * hardware module is configured in 32-bit |
mbed_official | 15:a81a8d6c1dfe | 114 | * slave mode |
mbed_official | 15:a81a8d6c1dfe | 115 | */ |
mbed_official | 15:a81a8d6c1dfe | 116 | enum status_code tc_init( |
mbed_official | 15:a81a8d6c1dfe | 117 | struct tc_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 118 | Tc *const hw, |
mbed_official | 15:a81a8d6c1dfe | 119 | const struct tc_config *const config) |
mbed_official | 15:a81a8d6c1dfe | 120 | { |
mbed_official | 15:a81a8d6c1dfe | 121 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 122 | Assert(hw); |
mbed_official | 15:a81a8d6c1dfe | 123 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 124 | Assert(config); |
mbed_official | 15:a81a8d6c1dfe | 125 | |
mbed_official | 15:a81a8d6c1dfe | 126 | /* Temporary variable to hold all updates to the CTRLA |
mbed_official | 15:a81a8d6c1dfe | 127 | * register before they are written to it */ |
mbed_official | 15:a81a8d6c1dfe | 128 | uint16_t ctrla_tmp = 0; |
mbed_official | 15:a81a8d6c1dfe | 129 | /* Temporary variable to hold all updates to the CTRLBSET |
mbed_official | 15:a81a8d6c1dfe | 130 | * register before they are written to it */ |
mbed_official | 15:a81a8d6c1dfe | 131 | uint8_t ctrlbset_tmp = 0; |
mbed_official | 15:a81a8d6c1dfe | 132 | /* Temporary variable to hold all updates to the CTRLC |
mbed_official | 15:a81a8d6c1dfe | 133 | * register before they are written to it */ |
mbed_official | 15:a81a8d6c1dfe | 134 | uint8_t ctrlc_tmp = 0; |
mbed_official | 15:a81a8d6c1dfe | 135 | /* Temporary variable to hold TC instance number */ |
mbed_official | 15:a81a8d6c1dfe | 136 | uint8_t instance = _tc_get_inst_index(hw); |
mbed_official | 15:a81a8d6c1dfe | 137 | |
mbed_official | 15:a81a8d6c1dfe | 138 | /* Array of GLCK ID for different TC instances */ |
mbed_official | 15:a81a8d6c1dfe | 139 | uint8_t inst_gclk_id[] = TC_INST_GCLK_ID; |
mbed_official | 15:a81a8d6c1dfe | 140 | /* Array of PM APBC mask bit position for different TC instances */ |
mbed_official | 15:a81a8d6c1dfe | 141 | uint16_t inst_pm_apbmask[] = TC_INST_PM_APBCMASK; |
mbed_official | 15:a81a8d6c1dfe | 142 | |
mbed_official | 15:a81a8d6c1dfe | 143 | struct system_pinmux_config pin_config; |
mbed_official | 15:a81a8d6c1dfe | 144 | struct system_gclk_chan_config gclk_chan_config; |
mbed_official | 15:a81a8d6c1dfe | 145 | |
mbed_official | 15:a81a8d6c1dfe | 146 | #if TC_ASYNC == true |
mbed_official | 15:a81a8d6c1dfe | 147 | /* Initialize parameters */ |
mbed_official | 15:a81a8d6c1dfe | 148 | for (uint8_t i = 0; i < TC_CALLBACK_N; i++) { |
mbed_official | 15:a81a8d6c1dfe | 149 | module_inst->callback[i] = NULL; |
mbed_official | 15:a81a8d6c1dfe | 150 | } |
mbed_official | 15:a81a8d6c1dfe | 151 | module_inst->register_callback_mask = 0x00; |
mbed_official | 15:a81a8d6c1dfe | 152 | module_inst->enable_callback_mask = 0x00; |
mbed_official | 15:a81a8d6c1dfe | 153 | |
mbed_official | 15:a81a8d6c1dfe | 154 | /* Register this instance for callbacks*/ |
mbed_official | 15:a81a8d6c1dfe | 155 | _tc_instances[instance] = module_inst; |
mbed_official | 15:a81a8d6c1dfe | 156 | #endif |
mbed_official | 15:a81a8d6c1dfe | 157 | |
mbed_official | 15:a81a8d6c1dfe | 158 | /* Associate the given device instance with the hardware module */ |
mbed_official | 15:a81a8d6c1dfe | 159 | module_inst->hw = hw; |
mbed_official | 15:a81a8d6c1dfe | 160 | |
mbed_official | 15:a81a8d6c1dfe | 161 | #if SAMD10 || SAMD11 |
mbed_official | 15:a81a8d6c1dfe | 162 | /* Check if even numbered TC modules are being configured in 32-bit |
mbed_official | 15:a81a8d6c1dfe | 163 | * counter size. Only odd numbered counters are allowed to be |
mbed_official | 15:a81a8d6c1dfe | 164 | * configured in 32-bit counter size. |
mbed_official | 15:a81a8d6c1dfe | 165 | */ |
mbed_official | 15:a81a8d6c1dfe | 166 | if ((config->counter_size == TC_COUNTER_SIZE_32BIT) && |
mbed_official | 15:a81a8d6c1dfe | 167 | !((instance + TC_INSTANCE_OFFSET) & 0x01)) { |
mbed_official | 15:a81a8d6c1dfe | 168 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 169 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 170 | } |
mbed_official | 15:a81a8d6c1dfe | 171 | #else |
mbed_official | 15:a81a8d6c1dfe | 172 | /* Check if odd numbered TC modules are being configured in 32-bit |
mbed_official | 15:a81a8d6c1dfe | 173 | * counter size. Only even numbered counters are allowed to be |
mbed_official | 15:a81a8d6c1dfe | 174 | * configured in 32-bit counter size. |
mbed_official | 15:a81a8d6c1dfe | 175 | */ |
mbed_official | 15:a81a8d6c1dfe | 176 | if ((config->counter_size == TC_COUNTER_SIZE_32BIT) && |
mbed_official | 15:a81a8d6c1dfe | 177 | ((instance + TC_INSTANCE_OFFSET) & 0x01)) { |
mbed_official | 15:a81a8d6c1dfe | 178 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 179 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 180 | } |
mbed_official | 15:a81a8d6c1dfe | 181 | #endif |
mbed_official | 15:a81a8d6c1dfe | 182 | |
mbed_official | 15:a81a8d6c1dfe | 183 | /* Make the counter size variable in the module_inst struct reflect |
mbed_official | 15:a81a8d6c1dfe | 184 | * the counter size in the module |
mbed_official | 15:a81a8d6c1dfe | 185 | */ |
mbed_official | 15:a81a8d6c1dfe | 186 | module_inst->counter_size = config->counter_size; |
mbed_official | 15:a81a8d6c1dfe | 187 | |
mbed_official | 15:a81a8d6c1dfe | 188 | if (hw->COUNT8.CTRLA.reg & TC_CTRLA_SWRST) { |
mbed_official | 15:a81a8d6c1dfe | 189 | /* We are in the middle of a reset. Abort. */ |
mbed_official | 15:a81a8d6c1dfe | 190 | return STATUS_BUSY; |
mbed_official | 15:a81a8d6c1dfe | 191 | } |
mbed_official | 15:a81a8d6c1dfe | 192 | |
mbed_official | 15:a81a8d6c1dfe | 193 | if (hw->COUNT8.STATUS.reg & TC_STATUS_SLAVE) { |
mbed_official | 15:a81a8d6c1dfe | 194 | /* Module is used as a slave */ |
mbed_official | 15:a81a8d6c1dfe | 195 | return STATUS_ERR_DENIED; |
mbed_official | 15:a81a8d6c1dfe | 196 | } |
mbed_official | 15:a81a8d6c1dfe | 197 | |
mbed_official | 15:a81a8d6c1dfe | 198 | if (hw->COUNT8.CTRLA.reg & TC_CTRLA_ENABLE) { |
mbed_official | 15:a81a8d6c1dfe | 199 | /* Module must be disabled before initialization. Abort. */ |
mbed_official | 15:a81a8d6c1dfe | 200 | return STATUS_ERR_DENIED; |
mbed_official | 15:a81a8d6c1dfe | 201 | } |
mbed_official | 15:a81a8d6c1dfe | 202 | |
mbed_official | 15:a81a8d6c1dfe | 203 | /* Set up the TC PWM out pin for channel 0 */ |
mbed_official | 15:a81a8d6c1dfe | 204 | if (config->pwm_channel[0].enabled) { |
mbed_official | 15:a81a8d6c1dfe | 205 | system_pinmux_get_config_defaults(&pin_config); |
mbed_official | 15:a81a8d6c1dfe | 206 | pin_config.mux_position = config->pwm_channel[0].pin_mux; |
mbed_official | 15:a81a8d6c1dfe | 207 | pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT; |
mbed_official | 15:a81a8d6c1dfe | 208 | system_pinmux_pin_set_config( |
mbed_official | 15:a81a8d6c1dfe | 209 | config->pwm_channel[0].pin_out, &pin_config); |
mbed_official | 15:a81a8d6c1dfe | 210 | } |
mbed_official | 15:a81a8d6c1dfe | 211 | |
mbed_official | 15:a81a8d6c1dfe | 212 | /* Set up the TC PWM out pin for channel 1 */ |
mbed_official | 15:a81a8d6c1dfe | 213 | if (config->pwm_channel[1].enabled) { |
mbed_official | 15:a81a8d6c1dfe | 214 | system_pinmux_get_config_defaults(&pin_config); |
mbed_official | 15:a81a8d6c1dfe | 215 | pin_config.mux_position = config->pwm_channel[1].pin_mux; |
mbed_official | 15:a81a8d6c1dfe | 216 | pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT; |
mbed_official | 15:a81a8d6c1dfe | 217 | system_pinmux_pin_set_config( |
mbed_official | 15:a81a8d6c1dfe | 218 | config->pwm_channel[1].pin_out, &pin_config); |
mbed_official | 15:a81a8d6c1dfe | 219 | } |
mbed_official | 15:a81a8d6c1dfe | 220 | |
mbed_official | 15:a81a8d6c1dfe | 221 | /* Enable the user interface clock in the PM */ |
mbed_official | 15:a81a8d6c1dfe | 222 | system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, |
mbed_official | 15:a81a8d6c1dfe | 223 | inst_pm_apbmask[instance]); |
mbed_official | 15:a81a8d6c1dfe | 224 | |
mbed_official | 15:a81a8d6c1dfe | 225 | /* Enable the slave counter if counter_size is 32-bit */ |
mbed_official | 15:a81a8d6c1dfe | 226 | if ((config->counter_size == TC_COUNTER_SIZE_32BIT)) { |
mbed_official | 15:a81a8d6c1dfe | 227 | /* Enable the user interface clock in the PM */ |
mbed_official | 15:a81a8d6c1dfe | 228 | system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, |
mbed_official | 15:a81a8d6c1dfe | 229 | inst_pm_apbmask[instance + 1]); |
mbed_official | 15:a81a8d6c1dfe | 230 | } |
mbed_official | 15:a81a8d6c1dfe | 231 | |
mbed_official | 15:a81a8d6c1dfe | 232 | /* Setup clock for module */ |
mbed_official | 15:a81a8d6c1dfe | 233 | system_gclk_chan_get_config_defaults(&gclk_chan_config); |
mbed_official | 15:a81a8d6c1dfe | 234 | gclk_chan_config.source_generator = config->clock_source; |
mbed_official | 15:a81a8d6c1dfe | 235 | system_gclk_chan_set_config(inst_gclk_id[instance], &gclk_chan_config); |
mbed_official | 15:a81a8d6c1dfe | 236 | system_gclk_chan_enable(inst_gclk_id[instance]); |
mbed_official | 15:a81a8d6c1dfe | 237 | |
mbed_official | 15:a81a8d6c1dfe | 238 | /* Set ctrla register */ |
mbed_official | 15:a81a8d6c1dfe | 239 | ctrla_tmp = |
mbed_official | 15:a81a8d6c1dfe | 240 | (uint32_t)config->counter_size | |
mbed_official | 15:a81a8d6c1dfe | 241 | (uint32_t)config->wave_generation | |
mbed_official | 15:a81a8d6c1dfe | 242 | (uint32_t)config->reload_action | |
mbed_official | 15:a81a8d6c1dfe | 243 | (uint32_t)config->clock_prescaler; |
mbed_official | 15:a81a8d6c1dfe | 244 | |
mbed_official | 15:a81a8d6c1dfe | 245 | if (config->run_in_standby) { |
mbed_official | 15:a81a8d6c1dfe | 246 | ctrla_tmp |= TC_CTRLA_RUNSTDBY; |
mbed_official | 15:a81a8d6c1dfe | 247 | } |
mbed_official | 15:a81a8d6c1dfe | 248 | |
mbed_official | 15:a81a8d6c1dfe | 249 | /* Write configuration to register */ |
mbed_official | 15:a81a8d6c1dfe | 250 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 251 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 252 | } |
mbed_official | 15:a81a8d6c1dfe | 253 | hw->COUNT8.CTRLA.reg = ctrla_tmp; |
mbed_official | 15:a81a8d6c1dfe | 254 | |
mbed_official | 15:a81a8d6c1dfe | 255 | /* Set ctrlb register */ |
mbed_official | 15:a81a8d6c1dfe | 256 | if (config->oneshot) { |
mbed_official | 15:a81a8d6c1dfe | 257 | ctrlbset_tmp = TC_CTRLBSET_ONESHOT; |
mbed_official | 15:a81a8d6c1dfe | 258 | } |
mbed_official | 15:a81a8d6c1dfe | 259 | |
mbed_official | 15:a81a8d6c1dfe | 260 | if (config->count_direction) { |
mbed_official | 15:a81a8d6c1dfe | 261 | ctrlbset_tmp |= TC_CTRLBSET_DIR; |
mbed_official | 15:a81a8d6c1dfe | 262 | } |
mbed_official | 15:a81a8d6c1dfe | 263 | |
mbed_official | 15:a81a8d6c1dfe | 264 | /* Clear old ctrlb configuration */ |
mbed_official | 15:a81a8d6c1dfe | 265 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 266 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 267 | } |
mbed_official | 15:a81a8d6c1dfe | 268 | hw->COUNT8.CTRLBCLR.reg = 0xFF; |
mbed_official | 15:a81a8d6c1dfe | 269 | |
mbed_official | 15:a81a8d6c1dfe | 270 | /* Check if we actually need to go into a wait state. */ |
mbed_official | 15:a81a8d6c1dfe | 271 | if (ctrlbset_tmp) { |
mbed_official | 15:a81a8d6c1dfe | 272 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 273 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 274 | } |
mbed_official | 15:a81a8d6c1dfe | 275 | /* Write configuration to register */ |
mbed_official | 15:a81a8d6c1dfe | 276 | hw->COUNT8.CTRLBSET.reg = ctrlbset_tmp; |
mbed_official | 15:a81a8d6c1dfe | 277 | } |
mbed_official | 15:a81a8d6c1dfe | 278 | |
mbed_official | 15:a81a8d6c1dfe | 279 | /* Set ctrlc register*/ |
mbed_official | 15:a81a8d6c1dfe | 280 | ctrlc_tmp = config->waveform_invert_output; |
mbed_official | 15:a81a8d6c1dfe | 281 | for (uint8_t i = 0; i < NUMBER_OF_COMPARE_CAPTURE_CHANNELS; i++) { |
mbed_official | 15:a81a8d6c1dfe | 282 | if (config->enable_capture_on_channel[i] == true) { |
mbed_official | 15:a81a8d6c1dfe | 283 | ctrlc_tmp |= (TC_CTRLC_CPTEN(1) << i); |
mbed_official | 15:a81a8d6c1dfe | 284 | } |
mbed_official | 15:a81a8d6c1dfe | 285 | } |
mbed_official | 15:a81a8d6c1dfe | 286 | |
mbed_official | 15:a81a8d6c1dfe | 287 | /* Write configuration to register */ |
mbed_official | 15:a81a8d6c1dfe | 288 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 289 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 290 | } |
mbed_official | 15:a81a8d6c1dfe | 291 | hw->COUNT8.CTRLC.reg = ctrlc_tmp; |
mbed_official | 15:a81a8d6c1dfe | 292 | |
mbed_official | 15:a81a8d6c1dfe | 293 | /* Write configuration to register */ |
mbed_official | 15:a81a8d6c1dfe | 294 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 295 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 296 | } |
mbed_official | 15:a81a8d6c1dfe | 297 | |
mbed_official | 15:a81a8d6c1dfe | 298 | /* Switch for TC counter size */ |
mbed_official | 15:a81a8d6c1dfe | 299 | switch (module_inst->counter_size) { |
mbed_official | 15:a81a8d6c1dfe | 300 | case TC_COUNTER_SIZE_8BIT: |
mbed_official | 15:a81a8d6c1dfe | 301 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 302 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 303 | } |
mbed_official | 15:a81a8d6c1dfe | 304 | |
mbed_official | 15:a81a8d6c1dfe | 305 | hw->COUNT8.COUNT.reg = |
mbed_official | 15:a81a8d6c1dfe | 306 | config->counter_8_bit.value; |
mbed_official | 15:a81a8d6c1dfe | 307 | |
mbed_official | 15:a81a8d6c1dfe | 308 | |
mbed_official | 15:a81a8d6c1dfe | 309 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 310 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 311 | } |
mbed_official | 15:a81a8d6c1dfe | 312 | |
mbed_official | 15:a81a8d6c1dfe | 313 | hw->COUNT8.PER.reg = |
mbed_official | 15:a81a8d6c1dfe | 314 | config->counter_8_bit.period; |
mbed_official | 15:a81a8d6c1dfe | 315 | |
mbed_official | 15:a81a8d6c1dfe | 316 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 317 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 318 | } |
mbed_official | 15:a81a8d6c1dfe | 319 | |
mbed_official | 15:a81a8d6c1dfe | 320 | hw->COUNT8.CC[0].reg = |
mbed_official | 15:a81a8d6c1dfe | 321 | config->counter_8_bit.compare_capture_channel[0]; |
mbed_official | 15:a81a8d6c1dfe | 322 | |
mbed_official | 15:a81a8d6c1dfe | 323 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 324 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 325 | } |
mbed_official | 15:a81a8d6c1dfe | 326 | |
mbed_official | 15:a81a8d6c1dfe | 327 | hw->COUNT8.CC[1].reg = |
mbed_official | 15:a81a8d6c1dfe | 328 | config->counter_8_bit.compare_capture_channel[1]; |
mbed_official | 15:a81a8d6c1dfe | 329 | |
mbed_official | 15:a81a8d6c1dfe | 330 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 331 | |
mbed_official | 15:a81a8d6c1dfe | 332 | case TC_COUNTER_SIZE_16BIT: |
mbed_official | 15:a81a8d6c1dfe | 333 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 334 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 335 | } |
mbed_official | 15:a81a8d6c1dfe | 336 | |
mbed_official | 15:a81a8d6c1dfe | 337 | hw->COUNT16.COUNT.reg |
mbed_official | 15:a81a8d6c1dfe | 338 | = config->counter_16_bit.value; |
mbed_official | 15:a81a8d6c1dfe | 339 | |
mbed_official | 15:a81a8d6c1dfe | 340 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 341 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 342 | } |
mbed_official | 15:a81a8d6c1dfe | 343 | |
mbed_official | 15:a81a8d6c1dfe | 344 | hw->COUNT16.CC[0].reg = |
mbed_official | 15:a81a8d6c1dfe | 345 | config->counter_16_bit.compare_capture_channel[0]; |
mbed_official | 15:a81a8d6c1dfe | 346 | |
mbed_official | 15:a81a8d6c1dfe | 347 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 348 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 349 | } |
mbed_official | 15:a81a8d6c1dfe | 350 | |
mbed_official | 15:a81a8d6c1dfe | 351 | hw->COUNT16.CC[1].reg = |
mbed_official | 15:a81a8d6c1dfe | 352 | config->counter_16_bit.compare_capture_channel[1]; |
mbed_official | 15:a81a8d6c1dfe | 353 | |
mbed_official | 15:a81a8d6c1dfe | 354 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 355 | |
mbed_official | 15:a81a8d6c1dfe | 356 | case TC_COUNTER_SIZE_32BIT: |
mbed_official | 15:a81a8d6c1dfe | 357 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 358 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 359 | } |
mbed_official | 15:a81a8d6c1dfe | 360 | |
mbed_official | 15:a81a8d6c1dfe | 361 | hw->COUNT32.COUNT.reg |
mbed_official | 15:a81a8d6c1dfe | 362 | = config->counter_32_bit.value; |
mbed_official | 15:a81a8d6c1dfe | 363 | |
mbed_official | 15:a81a8d6c1dfe | 364 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 365 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 366 | } |
mbed_official | 15:a81a8d6c1dfe | 367 | |
mbed_official | 15:a81a8d6c1dfe | 368 | hw->COUNT32.CC[0].reg = |
mbed_official | 15:a81a8d6c1dfe | 369 | config->counter_32_bit.compare_capture_channel[0]; |
mbed_official | 15:a81a8d6c1dfe | 370 | |
mbed_official | 15:a81a8d6c1dfe | 371 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 372 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 373 | } |
mbed_official | 15:a81a8d6c1dfe | 374 | |
mbed_official | 15:a81a8d6c1dfe | 375 | hw->COUNT32.CC[1].reg = |
mbed_official | 15:a81a8d6c1dfe | 376 | config->counter_32_bit.compare_capture_channel[1]; |
mbed_official | 15:a81a8d6c1dfe | 377 | |
mbed_official | 15:a81a8d6c1dfe | 378 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 379 | } |
mbed_official | 15:a81a8d6c1dfe | 380 | |
mbed_official | 15:a81a8d6c1dfe | 381 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 382 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 383 | } |
mbed_official | 15:a81a8d6c1dfe | 384 | |
mbed_official | 15:a81a8d6c1dfe | 385 | /** |
mbed_official | 15:a81a8d6c1dfe | 386 | * \brief Sets TC module count value. |
mbed_official | 15:a81a8d6c1dfe | 387 | * |
mbed_official | 15:a81a8d6c1dfe | 388 | * Sets the current timer count value of a initialized TC module. The |
mbed_official | 15:a81a8d6c1dfe | 389 | * specified TC module may be started or stopped. |
mbed_official | 15:a81a8d6c1dfe | 390 | * |
mbed_official | 15:a81a8d6c1dfe | 391 | * \param[in] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 392 | * \param[in] count New timer count value to set |
mbed_official | 15:a81a8d6c1dfe | 393 | * |
mbed_official | 15:a81a8d6c1dfe | 394 | * \return Status of the count update procedure. |
mbed_official | 15:a81a8d6c1dfe | 395 | * |
mbed_official | 15:a81a8d6c1dfe | 396 | * \retval STATUS_OK The timer count was updated successfully |
mbed_official | 15:a81a8d6c1dfe | 397 | * \retval STATUS_ERR_INVALID_ARG An invalid timer counter size was specified |
mbed_official | 15:a81a8d6c1dfe | 398 | */ |
mbed_official | 15:a81a8d6c1dfe | 399 | enum status_code tc_set_count_value( |
mbed_official | 15:a81a8d6c1dfe | 400 | const struct tc_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 401 | const uint32_t count) |
mbed_official | 15:a81a8d6c1dfe | 402 | { |
mbed_official | 15:a81a8d6c1dfe | 403 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 404 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 405 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 406 | |
mbed_official | 15:a81a8d6c1dfe | 407 | /* Get a pointer to the module's hardware instance*/ |
mbed_official | 15:a81a8d6c1dfe | 408 | Tc *const tc_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 409 | |
mbed_official | 15:a81a8d6c1dfe | 410 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 411 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 412 | } |
mbed_official | 15:a81a8d6c1dfe | 413 | |
mbed_official | 15:a81a8d6c1dfe | 414 | /* Write to based on the TC counter_size */ |
mbed_official | 15:a81a8d6c1dfe | 415 | switch (module_inst->counter_size) { |
mbed_official | 15:a81a8d6c1dfe | 416 | case TC_COUNTER_SIZE_8BIT: |
mbed_official | 15:a81a8d6c1dfe | 417 | tc_module->COUNT8.COUNT.reg = (uint8_t)count; |
mbed_official | 15:a81a8d6c1dfe | 418 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 419 | |
mbed_official | 15:a81a8d6c1dfe | 420 | case TC_COUNTER_SIZE_16BIT: |
mbed_official | 15:a81a8d6c1dfe | 421 | tc_module->COUNT16.COUNT.reg = (uint16_t)count; |
mbed_official | 15:a81a8d6c1dfe | 422 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 423 | |
mbed_official | 15:a81a8d6c1dfe | 424 | case TC_COUNTER_SIZE_32BIT: |
mbed_official | 15:a81a8d6c1dfe | 425 | tc_module->COUNT32.COUNT.reg = (uint32_t)count; |
mbed_official | 15:a81a8d6c1dfe | 426 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 427 | |
mbed_official | 15:a81a8d6c1dfe | 428 | default: |
mbed_official | 15:a81a8d6c1dfe | 429 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 430 | } |
mbed_official | 15:a81a8d6c1dfe | 431 | } |
mbed_official | 15:a81a8d6c1dfe | 432 | |
mbed_official | 15:a81a8d6c1dfe | 433 | /** |
mbed_official | 15:a81a8d6c1dfe | 434 | * \brief Get TC module count value. |
mbed_official | 15:a81a8d6c1dfe | 435 | * |
mbed_official | 15:a81a8d6c1dfe | 436 | * Retrieves the current count value of a TC module. The specified TC module |
mbed_official | 15:a81a8d6c1dfe | 437 | * may be started or stopped. |
mbed_official | 15:a81a8d6c1dfe | 438 | * |
mbed_official | 15:a81a8d6c1dfe | 439 | * \param[in] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 440 | * |
mbed_official | 15:a81a8d6c1dfe | 441 | * \return Count value of the specified TC module. |
mbed_official | 15:a81a8d6c1dfe | 442 | */ |
mbed_official | 15:a81a8d6c1dfe | 443 | uint32_t tc_get_count_value( |
mbed_official | 15:a81a8d6c1dfe | 444 | const struct tc_module *const module_inst) |
mbed_official | 15:a81a8d6c1dfe | 445 | { |
mbed_official | 15:a81a8d6c1dfe | 446 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 447 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 448 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 449 | |
mbed_official | 15:a81a8d6c1dfe | 450 | /* Get a pointer to the module's hardware instance */ |
mbed_official | 15:a81a8d6c1dfe | 451 | Tc *const tc_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 452 | |
mbed_official | 15:a81a8d6c1dfe | 453 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 454 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 455 | } |
mbed_official | 15:a81a8d6c1dfe | 456 | |
mbed_official | 15:a81a8d6c1dfe | 457 | /* Read from based on the TC counter size */ |
mbed_official | 15:a81a8d6c1dfe | 458 | switch (module_inst->counter_size) { |
mbed_official | 15:a81a8d6c1dfe | 459 | case TC_COUNTER_SIZE_8BIT: |
mbed_official | 15:a81a8d6c1dfe | 460 | return (uint32_t)tc_module->COUNT8.COUNT.reg; |
mbed_official | 15:a81a8d6c1dfe | 461 | |
mbed_official | 15:a81a8d6c1dfe | 462 | case TC_COUNTER_SIZE_16BIT: |
mbed_official | 15:a81a8d6c1dfe | 463 | return (uint32_t)tc_module->COUNT16.COUNT.reg; |
mbed_official | 15:a81a8d6c1dfe | 464 | |
mbed_official | 15:a81a8d6c1dfe | 465 | case TC_COUNTER_SIZE_32BIT: |
mbed_official | 15:a81a8d6c1dfe | 466 | return tc_module->COUNT32.COUNT.reg; |
mbed_official | 15:a81a8d6c1dfe | 467 | } |
mbed_official | 15:a81a8d6c1dfe | 468 | |
mbed_official | 15:a81a8d6c1dfe | 469 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 470 | return 0; |
mbed_official | 15:a81a8d6c1dfe | 471 | } |
mbed_official | 15:a81a8d6c1dfe | 472 | |
mbed_official | 15:a81a8d6c1dfe | 473 | /** |
mbed_official | 15:a81a8d6c1dfe | 474 | * \brief Gets the TC module capture value. |
mbed_official | 15:a81a8d6c1dfe | 475 | * |
mbed_official | 15:a81a8d6c1dfe | 476 | * Retrieves the capture value in the indicated TC module capture channel. |
mbed_official | 15:a81a8d6c1dfe | 477 | * |
mbed_official | 15:a81a8d6c1dfe | 478 | * \param[in] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 479 | * \param[in] channel_index Index of the Compare Capture channel to read |
mbed_official | 15:a81a8d6c1dfe | 480 | * |
mbed_official | 15:a81a8d6c1dfe | 481 | * \return Capture value stored in the specified timer channel. |
mbed_official | 15:a81a8d6c1dfe | 482 | */ |
mbed_official | 15:a81a8d6c1dfe | 483 | uint32_t tc_get_capture_value( |
mbed_official | 15:a81a8d6c1dfe | 484 | const struct tc_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 485 | const enum tc_compare_capture_channel channel_index) |
mbed_official | 15:a81a8d6c1dfe | 486 | { |
mbed_official | 15:a81a8d6c1dfe | 487 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 488 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 489 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 490 | |
mbed_official | 15:a81a8d6c1dfe | 491 | /* Get a pointer to the module's hardware instance */ |
mbed_official | 15:a81a8d6c1dfe | 492 | Tc *const tc_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 493 | |
mbed_official | 15:a81a8d6c1dfe | 494 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 495 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 496 | } |
mbed_official | 15:a81a8d6c1dfe | 497 | |
mbed_official | 15:a81a8d6c1dfe | 498 | /* Read out based on the TC counter size */ |
mbed_official | 15:a81a8d6c1dfe | 499 | switch (module_inst->counter_size) { |
mbed_official | 15:a81a8d6c1dfe | 500 | case TC_COUNTER_SIZE_8BIT: |
mbed_official | 15:a81a8d6c1dfe | 501 | if (channel_index < |
mbed_official | 15:a81a8d6c1dfe | 502 | NUMBER_OF_COMPARE_CAPTURE_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 503 | return tc_module->COUNT8.CC[channel_index].reg; |
mbed_official | 15:a81a8d6c1dfe | 504 | } |
mbed_official | 15:a81a8d6c1dfe | 505 | |
mbed_official | 15:a81a8d6c1dfe | 506 | case TC_COUNTER_SIZE_16BIT: |
mbed_official | 15:a81a8d6c1dfe | 507 | if (channel_index < |
mbed_official | 15:a81a8d6c1dfe | 508 | NUMBER_OF_COMPARE_CAPTURE_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 509 | return tc_module->COUNT16.CC[channel_index].reg; |
mbed_official | 15:a81a8d6c1dfe | 510 | } |
mbed_official | 15:a81a8d6c1dfe | 511 | |
mbed_official | 15:a81a8d6c1dfe | 512 | case TC_COUNTER_SIZE_32BIT: |
mbed_official | 15:a81a8d6c1dfe | 513 | if (channel_index < |
mbed_official | 15:a81a8d6c1dfe | 514 | NUMBER_OF_COMPARE_CAPTURE_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 515 | return tc_module->COUNT32.CC[channel_index].reg; |
mbed_official | 15:a81a8d6c1dfe | 516 | } |
mbed_official | 15:a81a8d6c1dfe | 517 | } |
mbed_official | 15:a81a8d6c1dfe | 518 | |
mbed_official | 15:a81a8d6c1dfe | 519 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 520 | return 0; |
mbed_official | 15:a81a8d6c1dfe | 521 | } |
mbed_official | 15:a81a8d6c1dfe | 522 | |
mbed_official | 15:a81a8d6c1dfe | 523 | /** |
mbed_official | 15:a81a8d6c1dfe | 524 | * \brief Sets a TC module compare value. |
mbed_official | 15:a81a8d6c1dfe | 525 | * |
mbed_official | 15:a81a8d6c1dfe | 526 | * Writes a compare value to the given TC module compare/capture channel. |
mbed_official | 15:a81a8d6c1dfe | 527 | * |
mbed_official | 15:a81a8d6c1dfe | 528 | * \param[in] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 529 | * \param[in] channel_index Index of the compare channel to write to |
mbed_official | 15:a81a8d6c1dfe | 530 | * \param[in] compare New compare value to set |
mbed_official | 15:a81a8d6c1dfe | 531 | * |
mbed_official | 15:a81a8d6c1dfe | 532 | * \return Status of the compare update procedure. |
mbed_official | 15:a81a8d6c1dfe | 533 | * |
mbed_official | 15:a81a8d6c1dfe | 534 | * \retval STATUS_OK The compare value was updated successfully |
mbed_official | 15:a81a8d6c1dfe | 535 | * \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied |
mbed_official | 15:a81a8d6c1dfe | 536 | */ |
mbed_official | 15:a81a8d6c1dfe | 537 | enum status_code tc_set_compare_value( |
mbed_official | 15:a81a8d6c1dfe | 538 | const struct tc_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 539 | const enum tc_compare_capture_channel channel_index, |
mbed_official | 15:a81a8d6c1dfe | 540 | const uint32_t compare) |
mbed_official | 15:a81a8d6c1dfe | 541 | { |
mbed_official | 15:a81a8d6c1dfe | 542 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 543 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 544 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 545 | |
mbed_official | 15:a81a8d6c1dfe | 546 | /* Get a pointer to the module's hardware instance */ |
mbed_official | 15:a81a8d6c1dfe | 547 | Tc *const tc_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 548 | |
mbed_official | 15:a81a8d6c1dfe | 549 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 550 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 551 | } |
mbed_official | 15:a81a8d6c1dfe | 552 | |
mbed_official | 15:a81a8d6c1dfe | 553 | /* Read out based on the TC counter size */ |
mbed_official | 15:a81a8d6c1dfe | 554 | switch (module_inst->counter_size) { |
mbed_official | 15:a81a8d6c1dfe | 555 | case TC_COUNTER_SIZE_8BIT: |
mbed_official | 15:a81a8d6c1dfe | 556 | if (channel_index < |
mbed_official | 15:a81a8d6c1dfe | 557 | NUMBER_OF_COMPARE_CAPTURE_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 558 | tc_module->COUNT8.CC[channel_index].reg = |
mbed_official | 15:a81a8d6c1dfe | 559 | (uint8_t)compare; |
mbed_official | 15:a81a8d6c1dfe | 560 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 561 | } |
mbed_official | 15:a81a8d6c1dfe | 562 | |
mbed_official | 15:a81a8d6c1dfe | 563 | case TC_COUNTER_SIZE_16BIT: |
mbed_official | 15:a81a8d6c1dfe | 564 | if (channel_index < |
mbed_official | 15:a81a8d6c1dfe | 565 | NUMBER_OF_COMPARE_CAPTURE_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 566 | tc_module->COUNT16.CC[channel_index].reg = |
mbed_official | 15:a81a8d6c1dfe | 567 | (uint16_t)compare; |
mbed_official | 15:a81a8d6c1dfe | 568 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 569 | } |
mbed_official | 15:a81a8d6c1dfe | 570 | |
mbed_official | 15:a81a8d6c1dfe | 571 | case TC_COUNTER_SIZE_32BIT: |
mbed_official | 15:a81a8d6c1dfe | 572 | if (channel_index < |
mbed_official | 15:a81a8d6c1dfe | 573 | NUMBER_OF_COMPARE_CAPTURE_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 574 | tc_module->COUNT32.CC[channel_index].reg = |
mbed_official | 15:a81a8d6c1dfe | 575 | (uint32_t)compare; |
mbed_official | 15:a81a8d6c1dfe | 576 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 577 | } |
mbed_official | 15:a81a8d6c1dfe | 578 | } |
mbed_official | 15:a81a8d6c1dfe | 579 | |
mbed_official | 15:a81a8d6c1dfe | 580 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 581 | } |
mbed_official | 15:a81a8d6c1dfe | 582 | |
mbed_official | 15:a81a8d6c1dfe | 583 | /** |
mbed_official | 15:a81a8d6c1dfe | 584 | * \brief Resets the TC module. |
mbed_official | 15:a81a8d6c1dfe | 585 | * |
mbed_official | 15:a81a8d6c1dfe | 586 | * Resets the TC module, restoring all hardware module registers to their |
mbed_official | 15:a81a8d6c1dfe | 587 | * default values and disabling the module. The TC module will not be |
mbed_official | 15:a81a8d6c1dfe | 588 | * accessible while the reset is being performed. |
mbed_official | 15:a81a8d6c1dfe | 589 | * |
mbed_official | 15:a81a8d6c1dfe | 590 | * \note When resetting a 32-bit counter only the master TC module's instance |
mbed_official | 15:a81a8d6c1dfe | 591 | * structure should be passed to the function. |
mbed_official | 15:a81a8d6c1dfe | 592 | * |
mbed_official | 15:a81a8d6c1dfe | 593 | * \param[in] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 594 | * |
mbed_official | 15:a81a8d6c1dfe | 595 | * \return Status of the procedure. |
mbed_official | 15:a81a8d6c1dfe | 596 | * \retval STATUS_OK The module was reset successfully |
mbed_official | 15:a81a8d6c1dfe | 597 | * \retval STATUS_ERR_UNSUPPORTED_DEV A 32-bit slave TC module was passed to |
mbed_official | 15:a81a8d6c1dfe | 598 | * the function. Only use reset on master |
mbed_official | 15:a81a8d6c1dfe | 599 | * TC |
mbed_official | 15:a81a8d6c1dfe | 600 | */ |
mbed_official | 15:a81a8d6c1dfe | 601 | enum status_code tc_reset( |
mbed_official | 15:a81a8d6c1dfe | 602 | const struct tc_module *const module_inst) |
mbed_official | 15:a81a8d6c1dfe | 603 | { |
mbed_official | 15:a81a8d6c1dfe | 604 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 605 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 606 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 607 | |
mbed_official | 15:a81a8d6c1dfe | 608 | /* Get a pointer to the module hardware instance */ |
mbed_official | 15:a81a8d6c1dfe | 609 | TcCount8 *const tc_module = &(module_inst->hw->COUNT8); |
mbed_official | 15:a81a8d6c1dfe | 610 | |
mbed_official | 15:a81a8d6c1dfe | 611 | if (tc_module->STATUS.reg & TC_STATUS_SLAVE) { |
mbed_official | 15:a81a8d6c1dfe | 612 | return STATUS_ERR_UNSUPPORTED_DEV; |
mbed_official | 15:a81a8d6c1dfe | 613 | } |
mbed_official | 15:a81a8d6c1dfe | 614 | |
mbed_official | 15:a81a8d6c1dfe | 615 | /* Disable this module if it is running */ |
mbed_official | 15:a81a8d6c1dfe | 616 | if (tc_module->CTRLA.reg & TC_CTRLA_ENABLE) { |
mbed_official | 15:a81a8d6c1dfe | 617 | tc_disable(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 618 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 619 | /* wait while module is disabling */ |
mbed_official | 15:a81a8d6c1dfe | 620 | } |
mbed_official | 15:a81a8d6c1dfe | 621 | } |
mbed_official | 15:a81a8d6c1dfe | 622 | |
mbed_official | 15:a81a8d6c1dfe | 623 | /* Reset this TC module */ |
mbed_official | 15:a81a8d6c1dfe | 624 | tc_module->CTRLA.reg |= TC_CTRLA_SWRST; |
mbed_official | 15:a81a8d6c1dfe | 625 | |
mbed_official | 15:a81a8d6c1dfe | 626 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 627 | } |
mbed_official | 15:a81a8d6c1dfe | 628 | |
mbed_official | 15:a81a8d6c1dfe | 629 | /** |
mbed_official | 15:a81a8d6c1dfe | 630 | * \brief Set the timer TOP/period value. |
mbed_official | 15:a81a8d6c1dfe | 631 | * |
mbed_official | 15:a81a8d6c1dfe | 632 | * For 8-bit counter size this function writes the top value to the period |
mbed_official | 15:a81a8d6c1dfe | 633 | * register. |
mbed_official | 15:a81a8d6c1dfe | 634 | * |
mbed_official | 15:a81a8d6c1dfe | 635 | * For 16- and 32-bit counter size this function writes the top value to |
mbed_official | 15:a81a8d6c1dfe | 636 | * Capture Compare register 0. The value in this register can not be used for |
mbed_official | 15:a81a8d6c1dfe | 637 | * any other purpose. |
mbed_official | 15:a81a8d6c1dfe | 638 | * |
mbed_official | 15:a81a8d6c1dfe | 639 | * \note This function is designed to be used in PWM or frequency |
mbed_official | 15:a81a8d6c1dfe | 640 | * match modes only. When the counter is set to 16- or 32-bit counter |
mbed_official | 15:a81a8d6c1dfe | 641 | * size. In 8-bit counter size it will always be possible to change the |
mbed_official | 15:a81a8d6c1dfe | 642 | * top value even in normal mode. |
mbed_official | 15:a81a8d6c1dfe | 643 | * |
mbed_official | 15:a81a8d6c1dfe | 644 | * \param[in] module_inst Pointer to the software module instance struct |
mbed_official | 15:a81a8d6c1dfe | 645 | * \param[in] top_value New timer TOP value to set |
mbed_official | 15:a81a8d6c1dfe | 646 | * |
mbed_official | 15:a81a8d6c1dfe | 647 | * \return Status of the TOP set procedure. |
mbed_official | 15:a81a8d6c1dfe | 648 | * |
mbed_official | 15:a81a8d6c1dfe | 649 | * \retval STATUS_OK The timer TOP value was updated successfully |
mbed_official | 15:a81a8d6c1dfe | 650 | * \retval STATUS_ERR_INVALID_ARG The configured TC module counter size in the |
mbed_official | 15:a81a8d6c1dfe | 651 | * module instance is invalid |
mbed_official | 15:a81a8d6c1dfe | 652 | */ |
mbed_official | 15:a81a8d6c1dfe | 653 | enum status_code tc_set_top_value ( |
mbed_official | 15:a81a8d6c1dfe | 654 | const struct tc_module *const module_inst, |
mbed_official | 15:a81a8d6c1dfe | 655 | const uint32_t top_value) |
mbed_official | 15:a81a8d6c1dfe | 656 | { |
mbed_official | 15:a81a8d6c1dfe | 657 | Assert(module_inst); |
mbed_official | 15:a81a8d6c1dfe | 658 | Assert(module_inst->hw); |
mbed_official | 15:a81a8d6c1dfe | 659 | Assert(top_value); |
mbed_official | 15:a81a8d6c1dfe | 660 | |
mbed_official | 15:a81a8d6c1dfe | 661 | Tc *const tc_module = module_inst->hw; |
mbed_official | 15:a81a8d6c1dfe | 662 | |
mbed_official | 15:a81a8d6c1dfe | 663 | while (tc_is_syncing(module_inst)) { |
mbed_official | 15:a81a8d6c1dfe | 664 | /* Wait for sync */ |
mbed_official | 15:a81a8d6c1dfe | 665 | } |
mbed_official | 15:a81a8d6c1dfe | 666 | |
mbed_official | 15:a81a8d6c1dfe | 667 | switch (module_inst->counter_size) { |
mbed_official | 15:a81a8d6c1dfe | 668 | case TC_COUNTER_SIZE_8BIT: |
mbed_official | 15:a81a8d6c1dfe | 669 | tc_module->COUNT8.PER.reg = (uint8_t)top_value; |
mbed_official | 15:a81a8d6c1dfe | 670 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 671 | |
mbed_official | 15:a81a8d6c1dfe | 672 | case TC_COUNTER_SIZE_16BIT: |
mbed_official | 15:a81a8d6c1dfe | 673 | tc_module->COUNT16.CC[0].reg = (uint16_t)top_value; |
mbed_official | 15:a81a8d6c1dfe | 674 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 675 | |
mbed_official | 15:a81a8d6c1dfe | 676 | case TC_COUNTER_SIZE_32BIT: |
mbed_official | 15:a81a8d6c1dfe | 677 | tc_module->COUNT32.CC[0].reg = (uint32_t)top_value; |
mbed_official | 15:a81a8d6c1dfe | 678 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 679 | |
mbed_official | 15:a81a8d6c1dfe | 680 | default: |
mbed_official | 15:a81a8d6c1dfe | 681 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 682 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 683 | } |
mbed_official | 15:a81a8d6c1dfe | 684 | } |