gossamer 0.0.1
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
tcc.h
1
2/*
3 * MIT License
4 *
5 * Copyright (c) 2022 Joey Castillo
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#pragma once
27
28#include <stdint.h>
29#include <stdbool.h>
30#include "sam.h"
31
42typedef enum {
43 TCC_PRESCALER_DIV1 = 0,
44 TCC_PRESCALER_DIV2 = 1,
45 TCC_PRESCALER_DIV4 = 2,
46 TCC_PRESCALER_DIV8 = 3,
47 TCC_PRESCALER_DIV16 = 4,
48 TCC_PRESCALER_DIV64 = 5,
49 TCC_PRESCALER_DIV256 = 6,
50 TCC_PRESCALER_DIV1024 = 7
51} tcc_prescaler_value_t;
52
53typedef enum {
54 TCC_WAVEGEN_NORMAL_FREQUENCY = 0,
55 TCC_WAVEGEN_MATCH_FREQUENCY = 1,
56 TCC_WAVEGEN_NORMAL_PWM = 2,
57// These modes may be supported in the future, but I don't have a use for them right now.
58 // TCC_WAVEGEN_DUAL_SLOPE_TOP = 7,
59 // TCC_WAVEGEN_DUAL_SLOPE_BOTTOM = 5,
60 // TCC_WAVEGEN_DUAL_SLOPE_BOTH = 6,
61 // TCC_WAVEGEN_DUAL_SLOPE_CRITICAL = 4,
62} tcc_wavegen_t;
63
64typedef enum {
65 TCC_OUTPUT_MATRIX_MODULO_4 = 0,
66 TCC_OUTPUT_MATRIX_MODULO_2 = 1,
67 TCC_OUTPUT_MATRIX_ALL_CC0 = 2,
68 TCC_OUTPUT_MATRIX_WO0_CC0_OTHERS_CC1 = 3,
69} tcc_output_matrix_t;
70
71typedef enum {
72 TCC_CHANNEL_POLARITY_NORMAL = 0,
73 TCC_CHANNEL_POLARITY_INVERTED = 1,
74} tcc_channel_polarity_t;
75
76typedef enum {
77 TCC_EVENT_ACTION_OFF = 0,
78 TCC_EVENT_ACTION_RETRIGGER = 1,
79 TCC_EVENT_ACTION_COUNT_EVENT = 2,
80 TCC_EVENT_ACTION_START = 3,
81 TCC_EVENT_ACTION_INC = 4,
82 TCC_EVENT_ACTION_COUNT = 5,
83 TCC_EVENT_ACTION_FAULT = 7,
84} tcc_event_action_t;
85
86typedef enum {
87 TCC_INTERRUPT_OVF = (1 << 0),
88 TCC_INTERRUPT_MC0 = (1 << 16),
89 TCC_INTERRUPT_MC1 = (1 << 17),
90 TCC_INTERRUPT_MC2 = (1 << 18),
91 TCC_INTERRUPT_MC3 = (1 << 19),
92} tcc_interrupt_t;
93
98typedef void (*tcc_cb_t)(tcc_interrupt_t intflag);
99
120bool tcc_init(uint8_t instance, generic_clock_generator_t clocksource, tcc_prescaler_value_t prescaler);
121
127void tcc_set_run_in_standby(uint8_t instance, bool runStandby);
128
147void tcc_set_wavegen(uint8_t instance, tcc_wavegen_t mode);
148
164void tcc_set_output_matrix(uint8_t instance, tcc_output_matrix_t mode);
165
177void tcc_set_channel_polarity(uint8_t instance, uint8_t channel, tcc_channel_polarity_t polarity);
178
182void tcc_enable(uint8_t instance);
183
188bool tcc_is_enabled(uint8_t instance);
189
201void tcc_set_period(uint8_t instance, uint32_t period, bool buffered);
202
208uint32_t tcc_get_period(uint8_t instance);
209
221void tcc_set_cc(uint8_t instance, uint8_t channel, uint32_t value, bool buffered);
222
228void tcc_set_count(uint8_t instance, uint32_t value);
229
235uint32_t tcc_get_count(uint8_t instance);
236
249void tcc_stop(uint8_t instance);
250
255void tcc_retrigger(uint8_t instance);
256
261void tcc_update(uint8_t instance);
262
266void tcc_disable(uint8_t instance);
267
274void tcc_enable_overflow_event_output(uint8_t instance, bool enabled);
275
284void tcc_set_event_action(uint8_t instance, uint8_t event_index, tcc_event_action_t action);
285
293void tcc_enable_match_event_input(uint8_t instance, uint8_t channel, bool enabled);
294
302void tcc_enable_match_event_output(uint8_t instance, uint8_t channel, bool enabled);
303
310void tcc_configure_callback(uint8_t instance, tcc_cb_t callback);
311
323void tcc_enable_interrupt(uint8_t instance, tcc_interrupt_t mask);
324
330void tcc_disable_interrupt(uint8_t instance, tcc_interrupt_t mask);
331
336void tcc_irq_handler(uint8_t instance);
337
void tcc_set_period(uint8_t instance, uint32_t period, bool buffered)
Sets the period of the TCC.
Definition tcc.c:101
void tcc_enable_interrupt(uint8_t instance, tcc_interrupt_t mask)
Enables TCC interrupts for the specified sources.
Definition tcc.c:178
void tcc_configure_callback(uint8_t instance, tcc_cb_t callback)
Registers a callback for TCC interrupts.
Definition tcc.c:174
void tcc_disable(uint8_t instance)
Disables the TCC, but retains all its settings.
Definition tcc.c:224
void tcc_set_run_in_standby(uint8_t instance, bool runStandby)
Sets whether the TCC should run in standby mode.
Definition tcc.c:72
void tcc_set_count(uint8_t instance, uint32_t value)
Sets the value of the counter.
Definition tcc.c:131
uint32_t tcc_get_period(uint8_t instance)
Gets the period of the TCC.
Definition tcc.c:114
void tcc_enable_overflow_event_output(uint8_t instance, bool enabled)
Enables or disables overflow event output for the TCC.
Definition tcc.c:146
uint32_t tcc_get_count(uint8_t instance)
Gets the current value of the counter.
Definition tcc.c:136
bool tcc_init(uint8_t instance, generic_clock_generator_t clocksource, tcc_prescaler_value_t prescaler)
Enables the peripheral clock for the TCC and clocks it with the selected clock source....
Definition tcc.c:36
void tcc_set_output_matrix(uint8_t instance, tcc_output_matrix_t mode)
Sets the output matrix for the TCC.
Definition tcc.c:80
bool tcc_is_enabled(uint8_t instance)
Checks whether the TCC is enabled.
Definition tcc.c:97
void tcc_enable(uint8_t instance)
Enables the TCC. Make sure to call tcc_init first to set it up.
Definition tcc.c:92
void tcc_disable_interrupt(uint8_t instance, tcc_interrupt_t mask)
Disables TCC interrupts for the specified sources.
Definition tcc.c:186
void tcc_irq_handler(uint8_t instance)
TCC interrupt handler. Call this from your irq_handler_tccN() override.
Definition tcc.c:196
void tcc_set_wavegen(uint8_t instance, tcc_wavegen_t mode)
Sets the waveform generation mode for the TCC.
Definition tcc.c:76
void tcc_set_event_action(uint8_t instance, uint8_t event_index, tcc_event_action_t action)
Sets the action taken when the TCC receives an event.
Definition tcc.c:150
void tcc_enable_match_event_output(uint8_t instance, uint8_t channel, bool enabled)
Enables or disables match/capture event output for a TCC channel.
Definition tcc.c:167
void tcc_stop(uint8_t instance)
Issues a STOP command to the TCC.
Definition tcc.c:206
void tcc_update(uint8_t instance)
Issues an UPDATE command to the TCC.
Definition tcc.c:218
void tcc_enable_match_event_input(uint8_t instance, uint8_t channel, bool enabled)
Enables or disables match/capture event input for a TCC channel.
Definition tcc.c:160
void tcc_set_channel_polarity(uint8_t instance, uint8_t channel, tcc_channel_polarity_t polarity)
Sets the polarity of a channel.
Definition tcc.c:84
void tcc_set_cc(uint8_t instance, uint8_t channel, uint32_t value, bool buffered)
Sets the value of a compare channel.
Definition tcc.c:118
void(* tcc_cb_t)(tcc_interrupt_t intflag)
Callback type for TCC interrupts. The callback receives a bitmask of the interrupt flags that trigger...
Definition tcc.h:98
void tcc_retrigger(uint8_t instance)
Issues a RETRIGGER command to the TCC.
Definition tcc.c:212