mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 17 09:15:10 2015 +0100
Revision:
592:a274ee790e56
Parent:
579:53297373a894
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6

Full URL: https://github.com/mbedmicro/mbed/commit/e7144f83a8d75df80c4877936b6ffe552b0be9e6/

More API implementation for SAMR21

Who changed what in which revision?

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