mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
screamer
Date:
Tue Aug 02 14:07:36 2016 +0000
Revision:
144:423e1876dc07
Parent:
18:da299f395b9e
Added targets.json file for the supported targets in the release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 18:da299f395b9e 1 /**
mbed_official 18:da299f395b9e 2 * \file
mbed_official 18:da299f395b9e 3 *
mbed_official 18:da299f395b9e 4 * \brief SAM TC - Timer Counter Driver
mbed_official 18:da299f395b9e 5 *
mbed_official 18:da299f395b9e 6 * Copyright (C) 2014-2015 Atmel Corporation. All rights reserved.
mbed_official 18:da299f395b9e 7 *
mbed_official 18:da299f395b9e 8 * \asf_license_start
mbed_official 18:da299f395b9e 9 *
mbed_official 18:da299f395b9e 10 * \page License
mbed_official 18:da299f395b9e 11 *
mbed_official 18:da299f395b9e 12 * Redistribution and use in source and binary forms, with or without
mbed_official 18:da299f395b9e 13 * modification, are permitted provided that the following conditions are met:
mbed_official 18:da299f395b9e 14 *
mbed_official 18:da299f395b9e 15 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 18:da299f395b9e 16 * this list of conditions and the following disclaimer.
mbed_official 18:da299f395b9e 17 *
mbed_official 18:da299f395b9e 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 18:da299f395b9e 19 * this list of conditions and the following disclaimer in the documentation
mbed_official 18:da299f395b9e 20 * and/or other materials provided with the distribution.
mbed_official 18:da299f395b9e 21 *
mbed_official 18:da299f395b9e 22 * 3. The name of Atmel may not be used to endorse or promote products derived
mbed_official 18:da299f395b9e 23 * from this software without specific prior written permission.
mbed_official 18:da299f395b9e 24 *
mbed_official 18:da299f395b9e 25 * 4. This software may only be redistributed and used in connection with an
mbed_official 18:da299f395b9e 26 * Atmel microcontroller product.
mbed_official 18:da299f395b9e 27 *
mbed_official 18:da299f395b9e 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
mbed_official 18:da299f395b9e 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
mbed_official 18:da299f395b9e 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
mbed_official 18:da299f395b9e 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
mbed_official 18:da299f395b9e 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 18:da299f395b9e 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
mbed_official 18:da299f395b9e 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
mbed_official 18:da299f395b9e 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
mbed_official 18:da299f395b9e 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
mbed_official 18:da299f395b9e 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 18:da299f395b9e 38 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 18:da299f395b9e 39 *
mbed_official 18:da299f395b9e 40 * \asf_license_stop
mbed_official 18:da299f395b9e 41 *
mbed_official 18:da299f395b9e 42 */
mbed_official 18:da299f395b9e 43 /*
mbed_official 18:da299f395b9e 44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
mbed_official 18:da299f395b9e 45 */
mbed_official 18:da299f395b9e 46
mbed_official 18:da299f395b9e 47 #include "tc.h"
mbed_official 18:da299f395b9e 48
mbed_official 18:da299f395b9e 49 #if TC_ASYNC == true
mbed_official 18:da299f395b9e 50 # include "tc_interrupt.h"
mbed_official 18:da299f395b9e 51 # include <system_interrupt.h>
mbed_official 18:da299f395b9e 52 #endif
mbed_official 18:da299f395b9e 53
mbed_official 18:da299f395b9e 54 /**
mbed_official 18:da299f395b9e 55 * \internal Find the index of given TC module instance.
mbed_official 18:da299f395b9e 56 *
mbed_official 18:da299f395b9e 57 * \param[in] TC module instance pointer.
mbed_official 18:da299f395b9e 58 *
mbed_official 18:da299f395b9e 59 * \return Index of the given TC module instance.
mbed_official 18:da299f395b9e 60 */
mbed_official 18:da299f395b9e 61 uint8_t _tc_get_inst_index(
mbed_official 18:da299f395b9e 62 Tc *const hw)
mbed_official 18:da299f395b9e 63 {
mbed_official 18:da299f395b9e 64 /* List of available TC modules. */
mbed_official 18:da299f395b9e 65 Tc *const tc_modules[TC_INST_NUM] = TC_INSTS;
mbed_official 18:da299f395b9e 66
mbed_official 18:da299f395b9e 67 /* Find index for TC instance. */
mbed_official 18:da299f395b9e 68 for (uint32_t i = 0; i < TC_INST_NUM; i++) {
mbed_official 18:da299f395b9e 69 if (hw == tc_modules[i]) {
mbed_official 18:da299f395b9e 70 return i;
mbed_official 18:da299f395b9e 71 }
mbed_official 18:da299f395b9e 72 }
mbed_official 18:da299f395b9e 73
mbed_official 18:da299f395b9e 74 /* Invalid data given. */
mbed_official 18:da299f395b9e 75 Assert(false);
mbed_official 18:da299f395b9e 76 return 0;
mbed_official 18:da299f395b9e 77 }
mbed_official 18:da299f395b9e 78
mbed_official 18:da299f395b9e 79
mbed_official 18:da299f395b9e 80 /**
mbed_official 18:da299f395b9e 81 * \brief Initializes a hardware TC module instance.
mbed_official 18:da299f395b9e 82 *
mbed_official 18:da299f395b9e 83 * Enables the clock and initializes the TC module, based on the given
mbed_official 18:da299f395b9e 84 * configuration values.
mbed_official 18:da299f395b9e 85 *
mbed_official 18:da299f395b9e 86 * \param[in,out] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 87 * \param[in] hw Pointer to the TC hardware module
mbed_official 18:da299f395b9e 88 * \param[in] config Pointer to the TC configuration options struct
mbed_official 18:da299f395b9e 89 *
mbed_official 18:da299f395b9e 90 * \return Status of the initialization procedure.
mbed_official 18:da299f395b9e 91 *
mbed_official 18:da299f395b9e 92 * \retval STATUS_OK The module was initialized successfully
mbed_official 18:da299f395b9e 93 * \retval STATUS_BUSY Hardware module was busy when the
mbed_official 18:da299f395b9e 94 * initialization procedure was attempted
mbed_official 18:da299f395b9e 95 * \retval STATUS_INVALID_ARG An invalid configuration option or argument
mbed_official 18:da299f395b9e 96 * was supplied
mbed_official 18:da299f395b9e 97 * \retval STATUS_ERR_DENIED Hardware module was already enabled, or the
mbed_official 18:da299f395b9e 98 * hardware module is configured in 32-bit
mbed_official 18:da299f395b9e 99 * slave mode
mbed_official 18:da299f395b9e 100 */
mbed_official 18:da299f395b9e 101 enum status_code tc_init(
mbed_official 18:da299f395b9e 102 struct tc_module *const module_inst,
mbed_official 18:da299f395b9e 103 Tc *const hw,
mbed_official 18:da299f395b9e 104 const struct tc_config *const config)
mbed_official 18:da299f395b9e 105 {
mbed_official 18:da299f395b9e 106 /* Sanity check arguments */
mbed_official 18:da299f395b9e 107 Assert(hw);
mbed_official 18:da299f395b9e 108 Assert(module_inst);
mbed_official 18:da299f395b9e 109 Assert(config);
mbed_official 18:da299f395b9e 110
mbed_official 18:da299f395b9e 111 /* Temporary variable to hold all updates to the CTRLA
mbed_official 18:da299f395b9e 112 * register before they are written to it */
mbed_official 18:da299f395b9e 113 uint32_t ctrla_tmp = 0;
mbed_official 18:da299f395b9e 114 /* Temporary variable to hold all updates to the CTRLBSET
mbed_official 18:da299f395b9e 115 * register before they are written to it */
mbed_official 18:da299f395b9e 116 uint8_t ctrlbset_tmp = 0;
mbed_official 18:da299f395b9e 117 /* Temporary variable to hold TC instance number */
mbed_official 18:da299f395b9e 118 uint8_t instance = _tc_get_inst_index(hw);
mbed_official 18:da299f395b9e 119
mbed_official 18:da299f395b9e 120 #if (SAMC20) || (SAMC21)
mbed_official 18:da299f395b9e 121 /* Array of GLCK ID for different TC instances */
mbed_official 18:da299f395b9e 122 uint8_t inst_gclk_id[] = {TC0_GCLK_ID, TC1_GCLK_ID, TC2_GCLK_ID, TC3_GCLK_ID, TC4_GCLK_ID};
mbed_official 18:da299f395b9e 123 /* Array of MCLK APB mask bit position for different TC instances */
mbed_official 18:da299f395b9e 124 uint32_t inst_mclk_apbmask[] = {SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC0,
mbed_official 18:da299f395b9e 125 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC1,
mbed_official 18:da299f395b9e 126 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC2,
mbed_official 18:da299f395b9e 127 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC3,
mbed_official 18:da299f395b9e 128 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC4
mbed_official 18:da299f395b9e 129 };
mbed_official 18:da299f395b9e 130 #elif (SAML21J)
mbed_official 18:da299f395b9e 131 /* Array of GLCK ID for different TC instances */
mbed_official 18:da299f395b9e 132 uint8_t inst_gclk_id[] = {TC0_GCLK_ID, TC1_GCLK_ID, TC2_GCLK_ID, TC3_GCLK_ID, TC4_GCLK_ID};
mbed_official 18:da299f395b9e 133 /* Array of MCLK APB mask bit position for different TC instances */
mbed_official 18:da299f395b9e 134 uint32_t inst_mclk_apbmask[] = {SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC0,
mbed_official 18:da299f395b9e 135 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC1,
mbed_official 18:da299f395b9e 136 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC2,
mbed_official 18:da299f395b9e 137 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC3,
mbed_official 18:da299f395b9e 138 SYSTEM_CLOCK_APB_APBD, MCLK_APBDMASK_TC4
mbed_official 18:da299f395b9e 139 };
mbed_official 18:da299f395b9e 140 #else
mbed_official 18:da299f395b9e 141 /* Array of GLCK ID for different TC instances */
mbed_official 18:da299f395b9e 142 uint8_t inst_gclk_id[] = {TC0_GCLK_ID, TC1_GCLK_ID, TC4_GCLK_ID};
mbed_official 18:da299f395b9e 143 /* Array of PM APB mask bit position for different TC instances */
mbed_official 18:da299f395b9e 144 uint32_t inst_mclk_apbmask[] = {SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC0,
mbed_official 18:da299f395b9e 145 SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_TC1,
mbed_official 18:da299f395b9e 146 SYSTEM_CLOCK_APB_APBD, MCLK_APBDMASK_TC4
mbed_official 18:da299f395b9e 147 };
mbed_official 18:da299f395b9e 148 #endif
mbed_official 18:da299f395b9e 149
mbed_official 18:da299f395b9e 150 struct system_pinmux_config pin_config;
mbed_official 18:da299f395b9e 151 struct system_gclk_chan_config gclk_chan_config;
mbed_official 18:da299f395b9e 152
mbed_official 18:da299f395b9e 153 #if TC_ASYNC == true
mbed_official 18:da299f395b9e 154 /* Initialize parameters */
mbed_official 18:da299f395b9e 155 for (uint8_t i = 0; i < TC_CALLBACK_N; i++) {
mbed_official 18:da299f395b9e 156 module_inst->callback[i] = NULL;
mbed_official 18:da299f395b9e 157 }
mbed_official 18:da299f395b9e 158 module_inst->register_callback_mask = 0x00;
mbed_official 18:da299f395b9e 159 module_inst->enable_callback_mask = 0x00;
mbed_official 18:da299f395b9e 160
mbed_official 18:da299f395b9e 161 /* Register this instance for callbacks*/
mbed_official 18:da299f395b9e 162 _tc_instances[instance] = module_inst;
mbed_official 18:da299f395b9e 163 #endif
mbed_official 18:da299f395b9e 164
mbed_official 18:da299f395b9e 165 /* Associate the given device instance with the hardware module */
mbed_official 18:da299f395b9e 166 module_inst->hw = hw;
mbed_official 18:da299f395b9e 167
mbed_official 18:da299f395b9e 168 module_inst->double_buffering_enabled = config->double_buffering_enabled;
mbed_official 18:da299f395b9e 169
mbed_official 18:da299f395b9e 170 /* Check if odd numbered TC modules are being configured in 32-bit
mbed_official 18:da299f395b9e 171 * counter size. Only even numbered counters are allowed to be
mbed_official 18:da299f395b9e 172 * configured in 32-bit counter size.
mbed_official 18:da299f395b9e 173 */
mbed_official 18:da299f395b9e 174 if ((config->counter_size == TC_COUNTER_SIZE_32BIT) &&
mbed_official 18:da299f395b9e 175 ((instance + TC_INSTANCE_OFFSET) & 0x01)) {
mbed_official 18:da299f395b9e 176 Assert(false);
mbed_official 18:da299f395b9e 177 return STATUS_ERR_INVALID_ARG;
mbed_official 18:da299f395b9e 178 }
mbed_official 18:da299f395b9e 179
mbed_official 18:da299f395b9e 180 /* Make the counter size variable in the module_inst struct reflect
mbed_official 18:da299f395b9e 181 * the counter size in the module
mbed_official 18:da299f395b9e 182 */
mbed_official 18:da299f395b9e 183 module_inst->counter_size = config->counter_size;
mbed_official 18:da299f395b9e 184
mbed_official 18:da299f395b9e 185 if (hw->COUNT8.CTRLA.reg & TC_CTRLA_SWRST) {
mbed_official 18:da299f395b9e 186 /* We are in the middle of a reset. Abort. */
mbed_official 18:da299f395b9e 187 return STATUS_BUSY;
mbed_official 18:da299f395b9e 188 }
mbed_official 18:da299f395b9e 189
mbed_official 18:da299f395b9e 190 if (hw->COUNT8.STATUS.reg & TC_STATUS_SLAVE) {
mbed_official 18:da299f395b9e 191 /* Module is used as a slave */
mbed_official 18:da299f395b9e 192 return STATUS_ERR_DENIED;
mbed_official 18:da299f395b9e 193 }
mbed_official 18:da299f395b9e 194
mbed_official 18:da299f395b9e 195 if (hw->COUNT8.CTRLA.reg & TC_CTRLA_ENABLE) {
mbed_official 18:da299f395b9e 196 /* Module must be disabled before initialization. Abort. */
mbed_official 18:da299f395b9e 197 return STATUS_ERR_DENIED;
mbed_official 18:da299f395b9e 198 }
mbed_official 18:da299f395b9e 199
mbed_official 18:da299f395b9e 200 /* Set up the TC PWM out pin for channel 0 */
mbed_official 18:da299f395b9e 201 if (config->pwm_channel[0].enabled) {
mbed_official 18:da299f395b9e 202 system_pinmux_get_config_defaults(&pin_config);
mbed_official 18:da299f395b9e 203 pin_config.mux_position = config->pwm_channel[0].pin_mux;
mbed_official 18:da299f395b9e 204 pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT;
mbed_official 18:da299f395b9e 205 system_pinmux_pin_set_config(
mbed_official 18:da299f395b9e 206 config->pwm_channel[0].pin_out, &pin_config);
mbed_official 18:da299f395b9e 207 }
mbed_official 18:da299f395b9e 208
mbed_official 18:da299f395b9e 209 /* Set up the TC PWM out pin for channel 1 */
mbed_official 18:da299f395b9e 210 if (config->pwm_channel[1].enabled) {
mbed_official 18:da299f395b9e 211 system_pinmux_get_config_defaults(&pin_config);
mbed_official 18:da299f395b9e 212 pin_config.mux_position = config->pwm_channel[1].pin_mux;
mbed_official 18:da299f395b9e 213 pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT;
mbed_official 18:da299f395b9e 214 system_pinmux_pin_set_config(
mbed_official 18:da299f395b9e 215 config->pwm_channel[1].pin_out, &pin_config);
mbed_official 18:da299f395b9e 216 }
mbed_official 18:da299f395b9e 217
mbed_official 18:da299f395b9e 218 /* Enable the user interface clock in the MCLK */
mbed_official 18:da299f395b9e 219 system_apb_clock_set_mask((enum system_clock_apb_bus)inst_mclk_apbmask[instance*2],
mbed_official 18:da299f395b9e 220 inst_mclk_apbmask[2*instance+1]);
mbed_official 18:da299f395b9e 221
mbed_official 18:da299f395b9e 222 /* Enable the slave counter if counter_size is 32-bit */
mbed_official 18:da299f395b9e 223 if ((config->counter_size == TC_COUNTER_SIZE_32BIT) && (instance+1 < TC_INST_NUM)) {
mbed_official 18:da299f395b9e 224 /* Enable the user interface clock in the MCLK */
mbed_official 18:da299f395b9e 225 system_apb_clock_set_mask((enum system_clock_apb_bus)inst_mclk_apbmask[instance*2+1],
mbed_official 18:da299f395b9e 226 inst_mclk_apbmask[2*instance+2]);
mbed_official 18:da299f395b9e 227 }
mbed_official 18:da299f395b9e 228
mbed_official 18:da299f395b9e 229
mbed_official 18:da299f395b9e 230 /* Setup clock for module */
mbed_official 18:da299f395b9e 231 system_gclk_chan_get_config_defaults(&gclk_chan_config);
mbed_official 18:da299f395b9e 232 gclk_chan_config.source_generator = config->clock_source;
mbed_official 18:da299f395b9e 233 system_gclk_chan_set_config(inst_gclk_id[instance], &gclk_chan_config);
mbed_official 18:da299f395b9e 234 system_gclk_chan_enable(inst_gclk_id[instance]);
mbed_official 18:da299f395b9e 235
mbed_official 18:da299f395b9e 236 /* Set ctrla register */
mbed_official 18:da299f395b9e 237 ctrla_tmp =
mbed_official 18:da299f395b9e 238 (uint32_t)config->counter_size |
mbed_official 18:da299f395b9e 239 (uint32_t)config->reload_action |
mbed_official 18:da299f395b9e 240 (uint32_t)config->clock_prescaler;
mbed_official 18:da299f395b9e 241
mbed_official 18:da299f395b9e 242 for (uint8_t i = 0; i < NUMBER_OF_COMPARE_CAPTURE_CHANNELS; i++) {
mbed_official 18:da299f395b9e 243 if (config->enable_capture_on_channel[i] == true) {
mbed_official 18:da299f395b9e 244 ctrla_tmp |= (TC_CTRLA_CAPTEN(1) << i);
mbed_official 18:da299f395b9e 245 }
mbed_official 18:da299f395b9e 246 }
mbed_official 18:da299f395b9e 247
mbed_official 18:da299f395b9e 248 for (uint8_t i = 0; i < NUMBER_OF_COMPARE_CAPTURE_CHANNELS; i++) {
mbed_official 18:da299f395b9e 249 if (config->enable_capture_on_IO[i] == true) {
mbed_official 18:da299f395b9e 250 ctrla_tmp |= (TC_CTRLA_COPEN(1) << i);
mbed_official 18:da299f395b9e 251 }
mbed_official 18:da299f395b9e 252 }
mbed_official 18:da299f395b9e 253
mbed_official 18:da299f395b9e 254 ctrla_tmp |= (config->run_in_standby << TC_CTRLA_RUNSTDBY_Pos)
mbed_official 18:da299f395b9e 255 |(config->on_demand << TC_CTRLA_ONDEMAND_Pos);
mbed_official 18:da299f395b9e 256
mbed_official 18:da299f395b9e 257 /* Write configuration to register */
mbed_official 18:da299f395b9e 258 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 259 /* Wait for sync */
mbed_official 18:da299f395b9e 260 }
mbed_official 18:da299f395b9e 261 hw->COUNT8.CTRLA.reg = ctrla_tmp;
mbed_official 18:da299f395b9e 262
mbed_official 18:da299f395b9e 263 /* Write configuration to register */
mbed_official 18:da299f395b9e 264 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 265 /* Wait for sync */
mbed_official 18:da299f395b9e 266 }
mbed_official 18:da299f395b9e 267 hw->COUNT8.WAVE.reg = config->wave_generation;
mbed_official 18:da299f395b9e 268
mbed_official 18:da299f395b9e 269 /* Set ctrlb register */
mbed_official 18:da299f395b9e 270 if (config->oneshot) {
mbed_official 18:da299f395b9e 271 ctrlbset_tmp = TC_CTRLBSET_ONESHOT;
mbed_official 18:da299f395b9e 272 }
mbed_official 18:da299f395b9e 273
mbed_official 18:da299f395b9e 274 if (config->count_direction) {
mbed_official 18:da299f395b9e 275 ctrlbset_tmp |= TC_CTRLBSET_DIR;
mbed_official 18:da299f395b9e 276 }
mbed_official 18:da299f395b9e 277
mbed_official 18:da299f395b9e 278 /* Clear old ctrlb configuration */
mbed_official 18:da299f395b9e 279 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 280 /* Wait for sync */
mbed_official 18:da299f395b9e 281 }
mbed_official 18:da299f395b9e 282 hw->COUNT8.CTRLBCLR.reg = 0xFF;
mbed_official 18:da299f395b9e 283
mbed_official 18:da299f395b9e 284 /* Check if we actually need to go into a wait state. */
mbed_official 18:da299f395b9e 285 if (ctrlbset_tmp) {
mbed_official 18:da299f395b9e 286 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 287 /* Wait for sync */
mbed_official 18:da299f395b9e 288 }
mbed_official 18:da299f395b9e 289 /* Write configuration to register */
mbed_official 18:da299f395b9e 290 hw->COUNT8.CTRLBSET.reg = ctrlbset_tmp;
mbed_official 18:da299f395b9e 291 }
mbed_official 18:da299f395b9e 292
mbed_official 18:da299f395b9e 293 /* Set drvvtrl register*/
mbed_official 18:da299f395b9e 294 hw->COUNT8.DRVCTRL.reg = config->waveform_invert_output;
mbed_official 18:da299f395b9e 295
mbed_official 18:da299f395b9e 296 /* Write configuration to register */
mbed_official 18:da299f395b9e 297 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 298 /* Wait for sync */
mbed_official 18:da299f395b9e 299 }
mbed_official 18:da299f395b9e 300
mbed_official 18:da299f395b9e 301 /* Switch for TC counter size */
mbed_official 18:da299f395b9e 302 switch (module_inst->counter_size) {
mbed_official 18:da299f395b9e 303 case TC_COUNTER_SIZE_8BIT:
mbed_official 18:da299f395b9e 304 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 305 /* Wait for sync */
mbed_official 18:da299f395b9e 306 }
mbed_official 18:da299f395b9e 307
mbed_official 18:da299f395b9e 308 hw->COUNT8.COUNT.reg =
mbed_official 18:da299f395b9e 309 config->counter_8_bit.value;
mbed_official 18:da299f395b9e 310
mbed_official 18:da299f395b9e 311
mbed_official 18:da299f395b9e 312 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 313 /* Wait for sync */
mbed_official 18:da299f395b9e 314 }
mbed_official 18:da299f395b9e 315
mbed_official 18:da299f395b9e 316 hw->COUNT8.PER.reg =
mbed_official 18:da299f395b9e 317 config->counter_8_bit.period;
mbed_official 18:da299f395b9e 318
mbed_official 18:da299f395b9e 319 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 320 /* Wait for sync */
mbed_official 18:da299f395b9e 321 }
mbed_official 18:da299f395b9e 322
mbed_official 18:da299f395b9e 323 hw->COUNT8.CC[0].reg =
mbed_official 18:da299f395b9e 324 config->counter_8_bit.compare_capture_channel[0];
mbed_official 18:da299f395b9e 325
mbed_official 18:da299f395b9e 326 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 327 /* Wait for sync */
mbed_official 18:da299f395b9e 328 }
mbed_official 18:da299f395b9e 329
mbed_official 18:da299f395b9e 330 hw->COUNT8.CC[1].reg =
mbed_official 18:da299f395b9e 331 config->counter_8_bit.compare_capture_channel[1];
mbed_official 18:da299f395b9e 332
mbed_official 18:da299f395b9e 333 return STATUS_OK;
mbed_official 18:da299f395b9e 334
mbed_official 18:da299f395b9e 335 case TC_COUNTER_SIZE_16BIT:
mbed_official 18:da299f395b9e 336 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 337 /* Wait for sync */
mbed_official 18:da299f395b9e 338 }
mbed_official 18:da299f395b9e 339
mbed_official 18:da299f395b9e 340 hw->COUNT16.COUNT.reg
mbed_official 18:da299f395b9e 341 = config->counter_16_bit.value;
mbed_official 18:da299f395b9e 342
mbed_official 18:da299f395b9e 343 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 344 /* Wait for sync */
mbed_official 18:da299f395b9e 345 }
mbed_official 18:da299f395b9e 346
mbed_official 18:da299f395b9e 347 hw->COUNT16.CC[0].reg =
mbed_official 18:da299f395b9e 348 config->counter_16_bit.compare_capture_channel[0];
mbed_official 18:da299f395b9e 349
mbed_official 18:da299f395b9e 350 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 351 /* Wait for sync */
mbed_official 18:da299f395b9e 352 }
mbed_official 18:da299f395b9e 353
mbed_official 18:da299f395b9e 354 hw->COUNT16.CC[1].reg =
mbed_official 18:da299f395b9e 355 config->counter_16_bit.compare_capture_channel[1];
mbed_official 18:da299f395b9e 356
mbed_official 18:da299f395b9e 357 return STATUS_OK;
mbed_official 18:da299f395b9e 358
mbed_official 18:da299f395b9e 359 case TC_COUNTER_SIZE_32BIT:
mbed_official 18:da299f395b9e 360 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 361 /* Wait for sync */
mbed_official 18:da299f395b9e 362 }
mbed_official 18:da299f395b9e 363
mbed_official 18:da299f395b9e 364 hw->COUNT32.COUNT.reg
mbed_official 18:da299f395b9e 365 = config->counter_32_bit.value;
mbed_official 18:da299f395b9e 366
mbed_official 18:da299f395b9e 367 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 368 /* Wait for sync */
mbed_official 18:da299f395b9e 369 }
mbed_official 18:da299f395b9e 370
mbed_official 18:da299f395b9e 371 hw->COUNT32.CC[0].reg =
mbed_official 18:da299f395b9e 372 config->counter_32_bit.compare_capture_channel[0];
mbed_official 18:da299f395b9e 373
mbed_official 18:da299f395b9e 374 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 375 /* Wait for sync */
mbed_official 18:da299f395b9e 376 }
mbed_official 18:da299f395b9e 377
mbed_official 18:da299f395b9e 378 hw->COUNT32.CC[1].reg =
mbed_official 18:da299f395b9e 379 config->counter_32_bit.compare_capture_channel[1];
mbed_official 18:da299f395b9e 380
mbed_official 18:da299f395b9e 381 return STATUS_OK;
mbed_official 18:da299f395b9e 382 }
mbed_official 18:da299f395b9e 383
mbed_official 18:da299f395b9e 384 Assert(false);
mbed_official 18:da299f395b9e 385 return STATUS_ERR_INVALID_ARG;
mbed_official 18:da299f395b9e 386 }
mbed_official 18:da299f395b9e 387
mbed_official 18:da299f395b9e 388 /**
mbed_official 18:da299f395b9e 389 * \brief Sets TC module count value.
mbed_official 18:da299f395b9e 390 *
mbed_official 18:da299f395b9e 391 * Sets the current timer count value of a initialized TC module. The
mbed_official 18:da299f395b9e 392 * specified TC module may be started or stopped.
mbed_official 18:da299f395b9e 393 *
mbed_official 18:da299f395b9e 394 * \param[in] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 395 * \param[in] count New timer count value to set
mbed_official 18:da299f395b9e 396 *
mbed_official 18:da299f395b9e 397 * \return Status of the count update procedure.
mbed_official 18:da299f395b9e 398 *
mbed_official 18:da299f395b9e 399 * \retval STATUS_OK The timer count was updated successfully
mbed_official 18:da299f395b9e 400 * \retval STATUS_ERR_INVALID_ARG An invalid timer counter size was specified
mbed_official 18:da299f395b9e 401 */
mbed_official 18:da299f395b9e 402 enum status_code tc_set_count_value(
mbed_official 18:da299f395b9e 403 const struct tc_module *const module_inst,
mbed_official 18:da299f395b9e 404 const uint32_t count)
mbed_official 18:da299f395b9e 405 {
mbed_official 18:da299f395b9e 406 /* Sanity check arguments */
mbed_official 18:da299f395b9e 407 Assert(module_inst);
mbed_official 18:da299f395b9e 408 Assert(module_inst->hw);
mbed_official 18:da299f395b9e 409
mbed_official 18:da299f395b9e 410 /* Get a pointer to the module's hardware instance*/
mbed_official 18:da299f395b9e 411 Tc *const tc_module = module_inst->hw;
mbed_official 18:da299f395b9e 412
mbed_official 18:da299f395b9e 413 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 414 /* Wait for sync */
mbed_official 18:da299f395b9e 415 }
mbed_official 18:da299f395b9e 416
mbed_official 18:da299f395b9e 417 /* Write to based on the TC counter_size */
mbed_official 18:da299f395b9e 418 switch (module_inst->counter_size) {
mbed_official 18:da299f395b9e 419 case TC_COUNTER_SIZE_8BIT:
mbed_official 18:da299f395b9e 420 tc_module->COUNT8.COUNT.reg = (uint8_t)count;
mbed_official 18:da299f395b9e 421 return STATUS_OK;
mbed_official 18:da299f395b9e 422
mbed_official 18:da299f395b9e 423 case TC_COUNTER_SIZE_16BIT:
mbed_official 18:da299f395b9e 424 tc_module->COUNT16.COUNT.reg = (uint16_t)count;
mbed_official 18:da299f395b9e 425 return STATUS_OK;
mbed_official 18:da299f395b9e 426
mbed_official 18:da299f395b9e 427 case TC_COUNTER_SIZE_32BIT:
mbed_official 18:da299f395b9e 428 tc_module->COUNT32.COUNT.reg = (uint32_t)count;
mbed_official 18:da299f395b9e 429 return STATUS_OK;
mbed_official 18:da299f395b9e 430
mbed_official 18:da299f395b9e 431 default:
mbed_official 18:da299f395b9e 432 return STATUS_ERR_INVALID_ARG;
mbed_official 18:da299f395b9e 433 }
mbed_official 18:da299f395b9e 434 }
mbed_official 18:da299f395b9e 435
mbed_official 18:da299f395b9e 436 /**
mbed_official 18:da299f395b9e 437 * \brief Get TC module count value.
mbed_official 18:da299f395b9e 438 *
mbed_official 18:da299f395b9e 439 * Retrieves the current count value of a TC module. The specified TC module
mbed_official 18:da299f395b9e 440 * may be started or stopped.
mbed_official 18:da299f395b9e 441 *
mbed_official 18:da299f395b9e 442 * \param[in] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 443 *
mbed_official 18:da299f395b9e 444 * \return Count value of the specified TC module.
mbed_official 18:da299f395b9e 445 */
mbed_official 18:da299f395b9e 446 uint32_t tc_get_count_value(
mbed_official 18:da299f395b9e 447 const struct tc_module *const module_inst)
mbed_official 18:da299f395b9e 448 {
mbed_official 18:da299f395b9e 449 /* Sanity check arguments */
mbed_official 18:da299f395b9e 450 Assert(module_inst);
mbed_official 18:da299f395b9e 451 Assert(module_inst->hw);
mbed_official 18:da299f395b9e 452
mbed_official 18:da299f395b9e 453 /* Read synchronization */
mbed_official 18:da299f395b9e 454 tc_sync_read_count(module_inst);
mbed_official 18:da299f395b9e 455
mbed_official 18:da299f395b9e 456 /* Get a pointer to the module's hardware instance */
mbed_official 18:da299f395b9e 457 Tc *const tc_module = module_inst->hw;
mbed_official 18:da299f395b9e 458
mbed_official 18:da299f395b9e 459 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 460 /* Wait for sync */
mbed_official 18:da299f395b9e 461 }
mbed_official 18:da299f395b9e 462
mbed_official 18:da299f395b9e 463 /* Read from based on the TC counter size */
mbed_official 18:da299f395b9e 464 switch (module_inst->counter_size) {
mbed_official 18:da299f395b9e 465 case TC_COUNTER_SIZE_8BIT:
mbed_official 18:da299f395b9e 466 return (uint32_t)tc_module->COUNT8.COUNT.reg;
mbed_official 18:da299f395b9e 467
mbed_official 18:da299f395b9e 468 case TC_COUNTER_SIZE_16BIT:
mbed_official 18:da299f395b9e 469 return (uint32_t)tc_module->COUNT16.COUNT.reg;
mbed_official 18:da299f395b9e 470
mbed_official 18:da299f395b9e 471 case TC_COUNTER_SIZE_32BIT:
mbed_official 18:da299f395b9e 472 return tc_module->COUNT32.COUNT.reg;
mbed_official 18:da299f395b9e 473 }
mbed_official 18:da299f395b9e 474
mbed_official 18:da299f395b9e 475 Assert(false);
mbed_official 18:da299f395b9e 476 return 0;
mbed_official 18:da299f395b9e 477 }
mbed_official 18:da299f395b9e 478
mbed_official 18:da299f395b9e 479 /**
mbed_official 18:da299f395b9e 480 * \brief Gets the TC module capture value.
mbed_official 18:da299f395b9e 481 *
mbed_official 18:da299f395b9e 482 * Retrieves the capture value in the indicated TC module capture channel.
mbed_official 18:da299f395b9e 483 *
mbed_official 18:da299f395b9e 484 * \param[in] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 485 * \param[in] channel_index Index of the Compare Capture channel to read
mbed_official 18:da299f395b9e 486 *
mbed_official 18:da299f395b9e 487 * \return Capture value stored in the specified timer channel.
mbed_official 18:da299f395b9e 488 */
mbed_official 18:da299f395b9e 489 uint32_t tc_get_capture_value(
mbed_official 18:da299f395b9e 490 const struct tc_module *const module_inst,
mbed_official 18:da299f395b9e 491 const enum tc_compare_capture_channel channel_index)
mbed_official 18:da299f395b9e 492 {
mbed_official 18:da299f395b9e 493 /* Sanity check arguments */
mbed_official 18:da299f395b9e 494 Assert(module_inst);
mbed_official 18:da299f395b9e 495 Assert(module_inst->hw);
mbed_official 18:da299f395b9e 496
mbed_official 18:da299f395b9e 497 /* Get a pointer to the module's hardware instance */
mbed_official 18:da299f395b9e 498 Tc *const tc_module = module_inst->hw;
mbed_official 18:da299f395b9e 499
mbed_official 18:da299f395b9e 500 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 501 /* Wait for sync */
mbed_official 18:da299f395b9e 502 }
mbed_official 18:da299f395b9e 503
mbed_official 18:da299f395b9e 504 /* Read out based on the TC counter size */
mbed_official 18:da299f395b9e 505 switch (module_inst->counter_size) {
mbed_official 18:da299f395b9e 506 case TC_COUNTER_SIZE_8BIT:
mbed_official 18:da299f395b9e 507 if (channel_index <
mbed_official 18:da299f395b9e 508 NUMBER_OF_COMPARE_CAPTURE_CHANNELS) {
mbed_official 18:da299f395b9e 509 return tc_module->COUNT8.CC[channel_index].reg;
mbed_official 18:da299f395b9e 510 }
mbed_official 18:da299f395b9e 511
mbed_official 18:da299f395b9e 512 case TC_COUNTER_SIZE_16BIT:
mbed_official 18:da299f395b9e 513 if (channel_index <
mbed_official 18:da299f395b9e 514 NUMBER_OF_COMPARE_CAPTURE_CHANNELS) {
mbed_official 18:da299f395b9e 515 return tc_module->COUNT16.CC[channel_index].reg;
mbed_official 18:da299f395b9e 516 }
mbed_official 18:da299f395b9e 517
mbed_official 18:da299f395b9e 518 case TC_COUNTER_SIZE_32BIT:
mbed_official 18:da299f395b9e 519 if (channel_index <
mbed_official 18:da299f395b9e 520 NUMBER_OF_COMPARE_CAPTURE_CHANNELS) {
mbed_official 18:da299f395b9e 521 return tc_module->COUNT32.CC[channel_index].reg;
mbed_official 18:da299f395b9e 522 }
mbed_official 18:da299f395b9e 523 }
mbed_official 18:da299f395b9e 524
mbed_official 18:da299f395b9e 525 Assert(false);
mbed_official 18:da299f395b9e 526 return 0;
mbed_official 18:da299f395b9e 527 }
mbed_official 18:da299f395b9e 528
mbed_official 18:da299f395b9e 529 /**
mbed_official 18:da299f395b9e 530 * \brief Sets a TC module compare value.
mbed_official 18:da299f395b9e 531 *
mbed_official 18:da299f395b9e 532 * Writes a compare value to the given TC module compare/capture channel.
mbed_official 18:da299f395b9e 533 *
mbed_official 18:da299f395b9e 534 * \param[in] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 535 * \param[in] channel_index Index of the compare channel to write to
mbed_official 18:da299f395b9e 536 * \param[in] compare New compare value to set
mbed_official 18:da299f395b9e 537 *
mbed_official 18:da299f395b9e 538 * \return Status of the compare update procedure.
mbed_official 18:da299f395b9e 539 *
mbed_official 18:da299f395b9e 540 * \retval STATUS_OK The compare value was updated successfully
mbed_official 18:da299f395b9e 541 * \retval STATUS_ERR_INVALID_ARG An invalid channel index was supplied
mbed_official 18:da299f395b9e 542 */
mbed_official 18:da299f395b9e 543 enum status_code tc_set_compare_value(
mbed_official 18:da299f395b9e 544 const struct tc_module *const module_inst,
mbed_official 18:da299f395b9e 545 const enum tc_compare_capture_channel channel_index,
mbed_official 18:da299f395b9e 546 const uint32_t compare)
mbed_official 18:da299f395b9e 547 {
mbed_official 18:da299f395b9e 548 /* Sanity check arguments */
mbed_official 18:da299f395b9e 549 Assert(module_inst);
mbed_official 18:da299f395b9e 550 Assert(module_inst->hw);
mbed_official 18:da299f395b9e 551 Assert(compare);
mbed_official 18:da299f395b9e 552
mbed_official 18:da299f395b9e 553 /* Get a pointer to the module's hardware instance */
mbed_official 18:da299f395b9e 554 Tc *const tc_module = module_inst->hw;
mbed_official 18:da299f395b9e 555
mbed_official 18:da299f395b9e 556 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 557 /* Wait for sync */
mbed_official 18:da299f395b9e 558 }
mbed_official 18:da299f395b9e 559
mbed_official 18:da299f395b9e 560 /* Read out based on the TC counter size */
mbed_official 18:da299f395b9e 561 switch (module_inst->counter_size) {
mbed_official 18:da299f395b9e 562 case TC_COUNTER_SIZE_8BIT:
mbed_official 18:da299f395b9e 563 if (channel_index <
mbed_official 18:da299f395b9e 564 NUMBER_OF_COMPARE_CAPTURE_CHANNELS) {
mbed_official 18:da299f395b9e 565 if (module_inst->double_buffering_enabled) {
mbed_official 18:da299f395b9e 566 tc_module->COUNT8.CCBUF[channel_index].reg =
mbed_official 18:da299f395b9e 567 (uint8_t)compare;
mbed_official 18:da299f395b9e 568 } else {
mbed_official 18:da299f395b9e 569 tc_module->COUNT8.CC[channel_index].reg =
mbed_official 18:da299f395b9e 570 (uint8_t)compare;
mbed_official 18:da299f395b9e 571 }
mbed_official 18:da299f395b9e 572 return STATUS_OK;
mbed_official 18:da299f395b9e 573 }
mbed_official 18:da299f395b9e 574 case TC_COUNTER_SIZE_16BIT:
mbed_official 18:da299f395b9e 575 if (channel_index <
mbed_official 18:da299f395b9e 576 NUMBER_OF_COMPARE_CAPTURE_CHANNELS) {
mbed_official 18:da299f395b9e 577 if (module_inst->double_buffering_enabled) {
mbed_official 18:da299f395b9e 578 tc_module->COUNT16.CCBUF[channel_index].reg =
mbed_official 18:da299f395b9e 579 (uint16_t)compare;
mbed_official 18:da299f395b9e 580 } else {
mbed_official 18:da299f395b9e 581 tc_module->COUNT16.CC[channel_index].reg =
mbed_official 18:da299f395b9e 582 (uint16_t)compare;
mbed_official 18:da299f395b9e 583 }
mbed_official 18:da299f395b9e 584 return STATUS_OK;
mbed_official 18:da299f395b9e 585 }
mbed_official 18:da299f395b9e 586
mbed_official 18:da299f395b9e 587 case TC_COUNTER_SIZE_32BIT:
mbed_official 18:da299f395b9e 588 if (channel_index <
mbed_official 18:da299f395b9e 589 NUMBER_OF_COMPARE_CAPTURE_CHANNELS) {
mbed_official 18:da299f395b9e 590 if (module_inst->double_buffering_enabled) {
mbed_official 18:da299f395b9e 591 tc_module->COUNT32.CCBUF[channel_index].reg =
mbed_official 18:da299f395b9e 592 (uint32_t)compare;
mbed_official 18:da299f395b9e 593 } else {
mbed_official 18:da299f395b9e 594 tc_module->COUNT32.CC[channel_index].reg =
mbed_official 18:da299f395b9e 595 (uint32_t)compare;
mbed_official 18:da299f395b9e 596 }
mbed_official 18:da299f395b9e 597 return STATUS_OK;
mbed_official 18:da299f395b9e 598 }
mbed_official 18:da299f395b9e 599 }
mbed_official 18:da299f395b9e 600
mbed_official 18:da299f395b9e 601 return STATUS_ERR_INVALID_ARG;
mbed_official 18:da299f395b9e 602 }
mbed_official 18:da299f395b9e 603
mbed_official 18:da299f395b9e 604 /**
mbed_official 18:da299f395b9e 605 * \brief Resets the TC module.
mbed_official 18:da299f395b9e 606 *
mbed_official 18:da299f395b9e 607 * Resets the TC module, restoring all hardware module registers to their
mbed_official 18:da299f395b9e 608 * default values and disabling the module. The TC module will not be
mbed_official 18:da299f395b9e 609 * accessible while the reset is being performed.
mbed_official 18:da299f395b9e 610 *
mbed_official 18:da299f395b9e 611 * \note When resetting a 32-bit counter only the master TC module's instance
mbed_official 18:da299f395b9e 612 * structure should be passed to the function.
mbed_official 18:da299f395b9e 613 *
mbed_official 18:da299f395b9e 614 * \param[in] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 615 *
mbed_official 18:da299f395b9e 616 * \return Status of the procedure.
mbed_official 18:da299f395b9e 617 * \retval STATUS_OK The module was reset successfully
mbed_official 18:da299f395b9e 618 * \retval STATUS_ERR_UNSUPPORTED_DEV A 32-bit slave TC module was passed to
mbed_official 18:da299f395b9e 619 * the function. Only use reset on master
mbed_official 18:da299f395b9e 620 * TC
mbed_official 18:da299f395b9e 621 */
mbed_official 18:da299f395b9e 622 enum status_code tc_reset(
mbed_official 18:da299f395b9e 623 const struct tc_module *const module_inst)
mbed_official 18:da299f395b9e 624 {
mbed_official 18:da299f395b9e 625 /* Sanity check arguments */
mbed_official 18:da299f395b9e 626 Assert(module_inst);
mbed_official 18:da299f395b9e 627 Assert(module_inst->hw);
mbed_official 18:da299f395b9e 628
mbed_official 18:da299f395b9e 629 /* Get a pointer to the module hardware instance */
mbed_official 18:da299f395b9e 630 TcCount8 *const tc_module = &(module_inst->hw->COUNT8);
mbed_official 18:da299f395b9e 631
mbed_official 18:da299f395b9e 632 if (tc_module->STATUS.reg & TC_STATUS_SLAVE) {
mbed_official 18:da299f395b9e 633 return STATUS_ERR_UNSUPPORTED_DEV;
mbed_official 18:da299f395b9e 634 }
mbed_official 18:da299f395b9e 635
mbed_official 18:da299f395b9e 636 /* Disable this module if it is running */
mbed_official 18:da299f395b9e 637 if (tc_module->CTRLA.reg & TC_CTRLA_ENABLE) {
mbed_official 18:da299f395b9e 638 tc_disable(module_inst);
mbed_official 18:da299f395b9e 639 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 640 /* wait while module is disabling */
mbed_official 18:da299f395b9e 641 }
mbed_official 18:da299f395b9e 642 }
mbed_official 18:da299f395b9e 643
mbed_official 18:da299f395b9e 644 /* Reset this TC module */
mbed_official 18:da299f395b9e 645 tc_module->CTRLA.reg |= TC_CTRLA_SWRST;
mbed_official 18:da299f395b9e 646
mbed_official 18:da299f395b9e 647 return STATUS_OK;
mbed_official 18:da299f395b9e 648 }
mbed_official 18:da299f395b9e 649
mbed_official 18:da299f395b9e 650 /**
mbed_official 18:da299f395b9e 651 * \brief Set the timer TOP/period value.
mbed_official 18:da299f395b9e 652 *
mbed_official 18:da299f395b9e 653 * For 8-bit counter size this function writes the top value to the period
mbed_official 18:da299f395b9e 654 * register.
mbed_official 18:da299f395b9e 655 *
mbed_official 18:da299f395b9e 656 * For 16- and 32-bit counter size this function writes the top value to
mbed_official 18:da299f395b9e 657 * Capture Compare register 0. The value in this register can not be used for
mbed_official 18:da299f395b9e 658 * any other purpose.
mbed_official 18:da299f395b9e 659 *
mbed_official 18:da299f395b9e 660 * \note This function is designed to be used in PWM or frequency
mbed_official 18:da299f395b9e 661 * match modes only. When the counter is set to 16- or 32-bit counter
mbed_official 18:da299f395b9e 662 * size. In 8-bit counter size it will always be possible to change the
mbed_official 18:da299f395b9e 663 * top value even in normal mode.
mbed_official 18:da299f395b9e 664 *
mbed_official 18:da299f395b9e 665 * \param[in] module_inst Pointer to the software module instance struct
mbed_official 18:da299f395b9e 666 * \param[in] top_value New timer TOP value to set
mbed_official 18:da299f395b9e 667 *
mbed_official 18:da299f395b9e 668 * \return Status of the TOP set procedure.
mbed_official 18:da299f395b9e 669 *
mbed_official 18:da299f395b9e 670 * \retval STATUS_OK The timer TOP value was updated successfully
mbed_official 18:da299f395b9e 671 * \retval STATUS_ERR_INVALID_ARG The configured TC module counter size in the
mbed_official 18:da299f395b9e 672 * module instance is invalid
mbed_official 18:da299f395b9e 673 */
mbed_official 18:da299f395b9e 674 enum status_code tc_set_top_value (
mbed_official 18:da299f395b9e 675 const struct tc_module *const module_inst,
mbed_official 18:da299f395b9e 676 const uint32_t top_value)
mbed_official 18:da299f395b9e 677 {
mbed_official 18:da299f395b9e 678 Assert(module_inst);
mbed_official 18:da299f395b9e 679 Assert(module_inst->hw);
mbed_official 18:da299f395b9e 680 Assert(top_value);
mbed_official 18:da299f395b9e 681
mbed_official 18:da299f395b9e 682 Tc *const tc_module = module_inst->hw;
mbed_official 18:da299f395b9e 683
mbed_official 18:da299f395b9e 684 while (tc_is_syncing(module_inst)) {
mbed_official 18:da299f395b9e 685 /* Wait for sync */
mbed_official 18:da299f395b9e 686 }
mbed_official 18:da299f395b9e 687
mbed_official 18:da299f395b9e 688 switch (module_inst->counter_size) {
mbed_official 18:da299f395b9e 689 case TC_COUNTER_SIZE_8BIT:
mbed_official 18:da299f395b9e 690 if (module_inst->double_buffering_enabled) {
mbed_official 18:da299f395b9e 691 tc_module->COUNT8.PERBUF.reg = (uint8_t)top_value;
mbed_official 18:da299f395b9e 692 } else {
mbed_official 18:da299f395b9e 693 tc_module->COUNT8.PER.reg = (uint8_t)top_value;
mbed_official 18:da299f395b9e 694 }
mbed_official 18:da299f395b9e 695 return STATUS_OK;
mbed_official 18:da299f395b9e 696
mbed_official 18:da299f395b9e 697 case TC_COUNTER_SIZE_16BIT:
mbed_official 18:da299f395b9e 698 if (module_inst->double_buffering_enabled) {
mbed_official 18:da299f395b9e 699 tc_module->COUNT16.CCBUF[0].reg = (uint16_t)top_value;
mbed_official 18:da299f395b9e 700 } else {
mbed_official 18:da299f395b9e 701 tc_module->COUNT16.CC[0].reg = (uint16_t)top_value;
mbed_official 18:da299f395b9e 702 }
mbed_official 18:da299f395b9e 703 return STATUS_OK;
mbed_official 18:da299f395b9e 704
mbed_official 18:da299f395b9e 705 case TC_COUNTER_SIZE_32BIT:
mbed_official 18:da299f395b9e 706 if (module_inst->double_buffering_enabled) {
mbed_official 18:da299f395b9e 707 tc_module->COUNT32.CCBUF[0].reg = (uint32_t)top_value;
mbed_official 18:da299f395b9e 708 } else {
mbed_official 18:da299f395b9e 709 tc_module->COUNT32.CC[0].reg = (uint32_t)top_value;
mbed_official 18:da299f395b9e 710 }
mbed_official 18:da299f395b9e 711 return STATUS_OK;
mbed_official 18:da299f395b9e 712
mbed_official 18:da299f395b9e 713 default:
mbed_official 18:da299f395b9e 714 Assert(false);
mbed_official 18:da299f395b9e 715 return STATUS_ERR_INVALID_ARG;
mbed_official 18:da299f395b9e 716 }
mbed_official 18:da299f395b9e 717 }