Microchip® Advanced Software Framework

tcc.c File Reference

SAM TCC - Timer Counter for Control Applications Driver.

Copyright (c) 2013-2020 Microchip Technology Inc. and its subsidiaries.

#include "tcc.h"
#include "tcc_callback.h"
#include <system_interrupt.h>

Macros

#define _SIZE_MAX(size)
 
#define _SIZE_MAX_WITH_DITHER   0x03FFFFFF
 
#define _TCC_CHANNEL_FUNCTION_INIT(n, value)   config->compare.channel_function[n] = value;
 
#define _TCC_CHANNEL_MATCH_VALUE_INIT(n, value)   config->compare.match[n] = value;
 
#define _TCC_CHANNEL_OUT_PIN_INIT(n, dummy)
 
#define _TCC_CHANNEL_WAVE_POLARITY_INIT(n, value)   config->compare.wave_polarity[n] = value;
 
#define _TCC_DITHERING_B   16u
 
#define _TCC_DTI_B   2u
 
#define _TCC_FAULT_FUNCTION_INIT(n, dummy)
 
#define _TCC_INTERRUPT_VECT_NUM(n, unused)   SYSTEM_INTERRUPT_MODULE_TCC##n,
 
#define _TCC_NRF_FUNCTION_INIT(n, dummy)
 
#define _TCC_OTMX_B   1u
 
#define _TCC_OUT_INVERT_INIT(n, value)   config->wave_ext.invert[n] = value;
 
#define _TCC_PG_B   8u
 
#define _TCC_SWAP_B   4u
 

Functions

static enum status_code _tcc_build_ctrla (const uint8_t module_index, const struct tcc_config *const config, uint32_t *value_buffer)
 Build CTRLA register value from configuration. More...
 
static void _tcc_build_ctrlb (const uint8_t module_index, const struct tcc_config *const config, uint8_t *value_buffer)
 Build CTRLB register value from configuration. More...
 
static enum status_code _tcc_build_drvctrl (const uint8_t module_index, const struct tcc_config *const config, uint32_t *value_buffer)
 Build DRVCTRL register values from configuration. More...
 
static enum status_code _tcc_build_faults (const uint8_t module_index, const struct tcc_config *const config, uint32_t *value_buffer)
 Build FAULTs register values from configuration. More...
 
static enum status_code _tcc_build_waves (const uint8_t module_index, const struct tcc_config *const config, uint32_t *value_buffer)
 Build WAVE & WAVEB register values from configuration. More...
 
uint8_t _tcc_get_inst_index (Tcc *const hw)
 
static enum status_code _tcc_set_compare_value (const struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index, const uint32_t compare, const bool double_buffering_enabled)
 Sets a TCC module compare value/buffer. More...
 
static enum status_code _tcc_set_top_value (const struct tcc_module *const module_inst, const uint32_t top_value, const bool double_buffering_enabled)
 Set the timer TOP/PERIOD buffer/value. More...
 
void tcc_clear_status (struct tcc_module *const module_inst, const uint32_t status_flags)
 Clears a module status flag. More...
 
enum status_code tcc_disable_circular_buffer_compare (struct tcc_module *const module_inst, enum tcc_match_capture_channel channel_index)
 Disable circular option for double buffered compare values. More...
 
void tcc_disable_events (struct tcc_module *const module_inst, struct tcc_events *const events)
 Disables the event input or output of a TCC instance. More...
 
enum status_code tcc_enable_circular_buffer_compare (struct tcc_module *const module_inst, enum tcc_match_capture_channel channel_index)
 Enable circular option for double buffered compare values. More...
 
enum status_code tcc_enable_events (struct tcc_module *const module_inst, struct tcc_events *const events)
 Enables the TCC module event input or output. More...
 
uint32_t tcc_get_capture_value (const struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index)
 Gets the TCC module capture value. More...
 
void tcc_get_config_defaults (struct tcc_config *const config, Tcc *const hw)
 Initializes config with predefined default values. More...
 
uint32_t tcc_get_count_value (const struct tcc_module *const module_inst)
 Get count value of the given TCC module. More...
 
uint32_t tcc_get_status (struct tcc_module *const module_inst)
 Retrieves the current module status. More...
 
enum status_code tcc_init (struct tcc_module *const module_inst, Tcc *const hw, const struct tcc_config *const config)
 Initializes a hardware TCC module instance. More...
 
enum status_code tcc_set_compare_value (const struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index, const uint32_t compare)
 Sets a TCC module compare value. More...
 
enum status_code tcc_set_count_value (const struct tcc_module *const module_inst, const uint32_t count)
 Sets count value for the given TCC module. More...
 
enum status_code tcc_set_double_buffer_compare_values (struct tcc_module *const module_inst, const enum tcc_match_capture_channel channel_index, const uint32_t compare, const uint32_t compare_buffer)
 Sets a TCC module compare value and buffer value. More...
 
enum status_code tcc_set_double_buffer_top_values (const struct tcc_module *const module_inst, const uint32_t top_value, const uint32_t top_buffer_value)
 Set the timer TOP/PERIOD value and buffer value. More...
 
enum status_code tcc_set_pattern (const struct tcc_module *const module_inst, const uint32_t line_index, const enum tcc_output_pattern pattern)
 Sets the TCC module waveform output pattern. More...
 
enum status_code tcc_set_top_value (const struct tcc_module *const module_inst, const uint32_t top_value)
 Set the timer TOP/PERIOD value. More...
 

Variables

const uint32_t _tcc_apbcmasks [TCC_INST_NUM] = TCC_APBCMASKS
 
const uint8_t _tcc_cc_nums [TCC_INST_NUM] = TCC_CC_NUMS
 
const uint8_t _tcc_exts [TCC_INST_NUM] = TCC_EXTS
 
const uint8_t _tcc_gclk_ids [TCC_INST_NUM] = TCC_GCLK_IDS
 
const uint32_t _tcc_maxs [TCC_INST_NUM] = TCC_MAXS
 
const uint8_t _tcc_ow_nums [TCC_INST_NUM] = TCC_OW_NUMS
 
const uint8_t _tcc_sizes [TCC_INST_NUM] = TCC_SIZES
 
const Tcc *const tcc_modules [TCC_INST_NUM] = TCC_INSTS
 

#define _SIZE_MAX (   size)
Value:
((size==32u) ? 0xFFFFFFFF : ( \
(1u << size) - 1))
#define _SIZE_MAX_WITH_DITHER   0x03FFFFFF
#define _TCC_CHANNEL_FUNCTION_INIT (   n,
  value 
)    config->compare.channel_function[n] = value;

Referenced by tcc_get_config_defaults().

#define _TCC_CHANNEL_MATCH_VALUE_INIT (   n,
  value 
)    config->compare.match[n] = value;

Referenced by tcc_get_config_defaults().

#define _TCC_CHANNEL_OUT_PIN_INIT (   n,
  dummy 
)
Value:
config->pins.enable_wave_out_pin[n] = false;\
config->pins.wave_out_pin[TCC_WAVE_OUTPUT_##n] = 0; \
config->pins.wave_out_pin_mux[TCC_WAVE_OUTPUT_##n] = 0;

Referenced by tcc_get_config_defaults().

#define _TCC_CHANNEL_WAVE_POLARITY_INIT (   n,
  value 
)    config->compare.wave_polarity[n] = value;

Referenced by tcc_get_config_defaults().

#define _TCC_DITHERING_B   16u
#define _TCC_DTI_B   2u
#define _TCC_FAULT_FUNCTION_INIT (   n,
  dummy 
)
Value:
config->wave_ext.recoverable_fault[n].filter_value = 0; \
config->wave_ext.recoverable_fault[n].blanking_cycles = 0; \
config->wave_ext.recoverable_fault[n].restart = false; \
config->wave_ext.recoverable_fault[n].keep = false; \
config->wave_ext.recoverable_fault[n].qualification = false; \
config->wave_ext.recoverable_fault[n].source = TCC_FAULT_SOURCE_DISABLE; \
config->wave_ext.recoverable_fault[n].blanking = TCC_FAULT_BLANKING_DISABLE; \
config->wave_ext.recoverable_fault[n].halt_action = TCC_FAULT_HALT_ACTION_DISABLE; \
config->wave_ext.recoverable_fault[n].capture_action = TCC_FAULT_CAPTURE_DISABLE; \
config->wave_ext.recoverable_fault[n].capture_channel = TCC_FAULT_CAPTURE_CHANNEL_0;
Capture disabled.
Definition: tcc.h:1279
Recoverable fault triggers channel 0 capture operation.
Definition: tcc.h:1303
No blanking.
Definition: tcc.h:1331
Fault input is disabled.
Definition: tcc.h:1317
Halt action disabled.
Definition: tcc.h:1265

Referenced by tcc_get_config_defaults().

#define _TCC_INTERRUPT_VECT_NUM (   n,
  unused 
)    SYSTEM_INTERRUPT_MODULE_TCC##n,

Converts a given TCC index to its interrupt vector index.

#define _TCC_NRF_FUNCTION_INIT (   n,
  dummy 
)
Value:
config->wave_ext.non_recoverable_fault[n].filter_value = 0; \
config->wave_ext.non_recoverable_fault[n].output = TCC_FAULT_STATE_OUTPUT_OFF;
Non-recoverable fault output is tri-stated.
Definition: tcc.h:1365

Referenced by tcc_get_config_defaults().

#define _TCC_OTMX_B   1u
#define _TCC_OUT_INVERT_INIT (   n,
  value 
)    config->wave_ext.invert[n] = value;

Referenced by tcc_get_config_defaults().

#define _TCC_PG_B   8u
#define _TCC_SWAP_B   4u

static enum status_code _tcc_build_ctrla ( const uint8_t  module_index,
const struct tcc_config *const  config,
uint32_t *  value_buffer 
)
inlinestatic

Build CTRLA register value from configuration.

Parameters
[in]module_indexThe software module instance index
[in]configPointer to the TCC configuration options struct
[out]value_bufferPointer to the buffer to fill with built value
Returns
Configuration validation status.
Return values
STATUS_OKConfiguration values are good and register value built and save to buffer
STATUS_ERR_INVALID_ARGInvalid parameter found: assigned dither mode is invalid for module; used capture channel is invalid for module

References _tcc_cc_nums, tcc_config::capture, tcc_capture_config::channel_function, tcc_counter_config::clock_prescaler, tcc_config::counter, i, tcc_counter_config::reload_action, tcc_config::run_in_standby, STATUS_ERR_INVALID_ARG, STATUS_OK, TCC_CHANNEL_FUNCTION_CAPTURE, and TCC_NUM_CHANNELS.

Referenced by tcc_init().

static void _tcc_build_ctrlb ( const uint8_t  module_index,
const struct tcc_config *const  config,
uint8_t *  value_buffer 
)
inlinestatic

Build CTRLB register value from configuration.

Parameters
[in]module_indexThe software module instance index
[in]configPointer to the TCC configuration options struct
[out]value_bufferPointer to the buffer to fill with built value

References tcc_config::counter, tcc_counter_config::direction, tcc_counter_config::oneshot, and TCC_COUNT_DIRECTION_DOWN.

Referenced by tcc_init().

static enum status_code _tcc_build_drvctrl ( const uint8_t  module_index,
const struct tcc_config *const  config,
uint32_t *  value_buffer 
)
inlinestatic

Build DRVCTRL register values from configuration.

Parameters
[in]module_indexThe software module instance index
[in]configPointer to the TCC configuration options struct
[out]value_bufferPointer to the buffer to fill with built value
Return values
STATUS_OKConfiguration values are good and register value built and save to buffer
STATUS_ERR_INVALID_ARGInvalid parameter found: assigned output line is invalid; filter value is invalid

References _tcc_ow_nums, i, tcc_wave_extension_config::invert, tcc_wave_extension_config::non_recoverable_fault, tcc_non_recoverable_fault_config::output, STATUS_ERR_INVALID_ARG, STATUS_OK, TCC_FAULT_STATE_OUTPUT_1, TCC_FAULT_STATE_OUTPUT_OFF, TCC_NUM_WAVE_OUTPUTS, and tcc_config::wave_ext.

Referenced by tcc_init().

static enum status_code _tcc_build_faults ( const uint8_t  module_index,
const struct tcc_config *const  config,
uint32_t *  value_buffer 
)
inlinestatic

Build FAULTs register values from configuration.

Parameters
[in]module_indexThe software module instance index
[in]configPointer to the TCC configuration options struct
[out]value_bufferPointer to the buffer to fill with built values
Return values
STATUS_OKConfiguration values are good and register value built and save to buffer
STATUS_ERR_INVALID_ARGInvalid parameter found: assigned fault capture channel is invalid; assigned filter value is invalid

References _tcc_cc_nums, tcc_recoverable_fault_config::blanking, tcc_recoverable_fault_config::blanking_cycles, tcc_recoverable_fault_config::capture_action, tcc_recoverable_fault_config::capture_channel, tcc_recoverable_fault_config::filter_value, tcc_recoverable_fault_config::halt_action, i, if(), tcc_recoverable_fault_config::keep, tcc_recoverable_fault_config::qualification, tcc_wave_extension_config::recoverable_fault, tcc_recoverable_fault_config::restart, tcc_recoverable_fault_config::source, STATUS_ERR_INVALID_ARG, STATUS_OK, TCC_NUM_FAULTS, and tcc_config::wave_ext.

Referenced by tcc_init().

static enum status_code _tcc_build_waves ( const uint8_t  module_index,
const struct tcc_config *const  config,
uint32_t *  value_buffer 
)
inlinestatic

Build WAVE & WAVEB register values from configuration.

Parameters
[in]module_indexThe software module instance index
[in]configPointer to the TCC configuration options struct
[out]value_bufferPointer to the buffer to fill with built value
Return values
STATUS_OKConfiguration values are good and register value built and save to buffer
STATUS_ERR_INVALID_ARGInvalid parameter found: assigned output line is invalid; circular and double buffering conflict; assigned function not supported by module

References _tcc_cc_nums, tcc_config::compare, STATUS_ERR_INVALID_ARG, STATUS_OK, TCC_NUM_CHANNELS, tcc_match_wave_config::wave_generation, tcc_match_wave_config::wave_polarity, and tcc_match_wave_config::wave_ramp.

Referenced by tcc_init().

uint8_t _tcc_get_inst_index ( Tcc *const  hw)

Find the index of the given TCC module instance.

Parameters
[in]TheTCC module instance pointer
Returns
Index of the given TCC module instance.

References Assert, i, and tcc_modules.

Referenced by _tcc_set_compare_value(), _tcc_set_top_value(), tcc_disable_callback(), tcc_disable_circular_buffer_compare(), tcc_enable_callback(), tcc_enable_circular_buffer_compare(), tcc_get_capture_value(), tcc_get_config_defaults(), tcc_init(), tcc_set_count_value(), and tcc_set_pattern().

static enum status_code _tcc_set_compare_value ( const struct tcc_module *const  module_inst,
const enum tcc_match_capture_channel  channel_index,
const uint32_t  compare,
const bool  double_buffering_enabled 
)
static

Sets a TCC module compare value/buffer.

Writes a compare value to the given TCC module compare/capture channel or buffer one.

Parameters
[in]module_instPointer to the software module instance struct
[in]channel_indexIndex of the compare channel to write to
[in]compareNew compare value/buffer value to set
[in]double_buffering_enabledSet to true to write to CCBx
Returns
Status of the compare update procedure.
Return values
STATUS_OKThe compare value was updated successfully
STATUS_ERR_INVALID_ARGAn invalid channel index was supplied or compare value exceed resolution

References _tcc_cc_nums, _tcc_get_inst_index(), _tcc_maxs, Assert, tcc_module::hw, max_count, STATUS_ERR_INVALID_ARG, and STATUS_OK.

Referenced by tcc_set_compare_value(), and tcc_set_double_buffer_compare_values().

static enum status_code _tcc_set_top_value ( const struct tcc_module *const  module_inst,
const uint32_t  top_value,
const bool  double_buffering_enabled 
)
static

Set the timer TOP/PERIOD buffer/value.

This function writes the given value to the PER/PERB register.

Parameters
[in]module_instPointer to the software module instance struct
[in]top_valueNew value to be loaded into the PER/PERB register
[in]double_buffering_enabledSet to true to write to PERB
Returns
Status of the TOP set procedure.
Return values
STATUS_OKThe timer TOP value was updated successfully
STATUS_ERR_INVALID_ARGAn invalid channel index was supplied or top/period value exceed resolution

References _tcc_get_inst_index(), _tcc_maxs, Assert, tcc_module::hw, max_count, STATUS_ERR_INVALID_ARG, and STATUS_OK.

Referenced by tcc_set_double_buffer_top_values(), and tcc_set_top_value().

const uint32_t _tcc_apbcmasks[TCC_INST_NUM] = TCC_APBCMASKS

Referenced by tcc_init().

const uint8_t _tcc_exts[TCC_INST_NUM] = TCC_EXTS
const uint8_t _tcc_gclk_ids[TCC_INST_NUM] = TCC_GCLK_IDS

Referenced by tcc_init().

const uint32_t _tcc_maxs[TCC_INST_NUM] = TCC_MAXS
const uint8_t _tcc_ow_nums[TCC_INST_NUM] = TCC_OW_NUMS
const uint8_t _tcc_sizes[TCC_INST_NUM] = TCC_SIZES
const Tcc* const tcc_modules[TCC_INST_NUM] = TCC_INSTS

Referenced by _tcc_get_inst_index().