gossamer 0.0.1
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
tc.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#include "system.h"
32
39typedef enum {
40 TC_PRESCALER_DIV1 = 0,
41 TC_PRESCALER_DIV2 = 1,
42 TC_PRESCALER_DIV4 = 2,
43 TC_PRESCALER_DIV8 = 3,
44 TC_PRESCALER_DIV16 = 4,
45 TC_PRESCALER_DIV64 = 5,
46 TC_PRESCALER_DIV256 = 6,
47 TC_PRESCALER_DIV1024 = 7
48} tc_prescaler_value_t;
49
50typedef enum {
51 TC_COUNTER_MODE_16BIT = 0,
52 TC_COUNTER_MODE_8BIT = 1,
53 TC_COUNTER_MODE_32BIT = 2,
54} tc_counter_mode_t;
55
56typedef enum {
57 TC_WAVEGEN_NORMAL_FREQUENCY = 0,
58 TC_WAVEGEN_MATCH_FREQUENCY = 1,
59 TC_WAVEGEN_NORMAL_PWM = 2,
60 TC_WAVEGEN_MATCH_PWM = 3,
61} tc_wavegen_t;
62
63typedef enum {
64 TC_CHANNEL_POLARITY_NORMAL = 0,
65 TC_CHANNEL_POLARITY_INVERTED = 1,
66} tc_channel_polarity_t;
67
68typedef enum {
69 TC_EVENT_ACTION_DISABLED = 0x0ul,
70 TC_EVENT_ACTION_RETRIGGER = 0x1ul,
71 TC_EVENT_ACTION_COUNT = 0x2ul,
72 TC_EVENT_ACTION_START = 0x3ul,
73 TC_EVENT_ACTION_STAMP = 0x4ul,
75 TC_EVENT_ACTION_PWP = 0x6ul,
76 TC_EVENT_ACTION_PW = 0x7ul,
78
79typedef enum {
80 TC_INTERRUPT_OVF = (1 << 0),
81 TC_INTERRUPT_MC0 = (1 << 4),
82 TC_INTERRUPT_MC1 = (1 << 5),
83} tc_interrupt_t;
84
89typedef void (*tc_cb_t)(tc_interrupt_t intflag);
90
112bool tc_init(uint8_t instance, generic_clock_generator_t clocksource, tc_prescaler_value_t prescaler);
113
133void tc_set_counter_mode(uint8_t instance, tc_counter_mode_t mode);
134
140void tc_set_run_in_standby(uint8_t instance, bool runStandby);
141
142#if !defined(_SAMD21_) && !defined(_SAMD11_)
149void tc_set_run_on_demand(uint8_t instance, bool onDemand);
150#endif
151
176void tc_set_wavegen(uint8_t instance, tc_wavegen_t mode);
177
190void tc_set_channel_polarity(uint8_t instance, uint8_t channel, tc_channel_polarity_t polarity);
191
195void tc_enable(uint8_t instance);
196
201bool tc_is_enabled(uint8_t instance);
202
209void tc_count8_set_period(uint8_t instance, uint8_t period);
210
216uint8_t tc_count8_get_period(uint8_t instance);
217
225void tc_count8_set_cc(uint8_t instance, uint8_t channel, uint8_t value);
226
234void tc_count16_set_cc(uint8_t instance, uint8_t channel, uint16_t value);
235
242void tc_count32_set_cc(uint8_t instance, uint8_t channel, uint32_t value);
243
249void tc_count8_set_count(uint8_t instance, uint8_t value);
250
258void tc_count16_set_count(uint8_t instance, uint16_t value);
259
265void tc_count32_set_count(uint8_t instance, uint32_t value);
266
272uint8_t tc_count8_get_count(uint8_t instance);
273
281uint16_t tc_count16_get_count(uint8_t instance);
282
288uint32_t tc_count32_get_count(uint8_t instance);
289
295void tc_set_event_action(uint8_t instance, tc_event_action_t action);
296
303void tc_enable_overflow_event_output(uint8_t instance, bool enabled);
304
312void tc_enable_match_event_output(uint8_t instance, uint8_t channel, bool enabled);
313
321void tc_stop(uint8_t instance);
322
330void tc_retrigger(uint8_t instance);
331
335void tc_disable(uint8_t instance);
336
343void tc_configure_callback(uint8_t instance, tc_cb_t callback);
344
356void tc_enable_interrupt(uint8_t instance, tc_interrupt_t mask);
357
363void tc_disable_interrupt(uint8_t instance, tc_interrupt_t mask);
364
369void tc_irq_handler(uint8_t instance);
370
uint8_t tc_count8_get_count(uint8_t instance)
Gets the TC's current counter value in 8-bit mode.
Definition tc.c:187
void tc_set_run_in_standby(uint8_t instance, bool runStandby)
Sets the TC's run-in-standby mode.
Definition tc.c:93
void tc_stop(uint8_t instance)
Issues a STOP command to the TC.
Definition tc.c:246
bool tc_is_enabled(uint8_t instance)
Checks whether the TC is enabled.
Definition tc.c:135
bool tc_init(uint8_t instance, generic_clock_generator_t clocksource, tc_prescaler_value_t prescaler)
Enables the peripheral clock for the TC and clocks it with the selected clock source....
Definition tc.c:39
void tc_enable(uint8_t instance)
Enables the TC. Make sure to call tc_init first to set it up.
Definition tc.c:130
void tc_count16_set_count(uint8_t instance, uint16_t value)
Sets the TC's counter value in 16-bit mode.
Definition tc.c:163
uint16_t tc_count16_get_count(uint8_t instance)
Gets the TC's current counter value in 16-bit mode.
Definition tc.c:192
void tc_count16_set_cc(uint8_t instance, uint8_t channel, uint16_t value)
Sets the TC's compare channel CC0 or CC1 register in 16-bit mode.
Definition tc.c:151
void tc_count8_set_count(uint8_t instance, uint8_t value)
Sets the TC's counter value in 8-bit mode.
Definition tc.c:159
void tc_count32_set_count(uint8_t instance, uint32_t value)
Sets the TC's counter value in 32-bit mode.
Definition tc.c:167
void tc_count32_set_cc(uint8_t instance, uint8_t channel, uint32_t value)
Sets the TC's compare channel CC0 or CC1 register in 32-bit mode.
Definition tc.c:155
void tc_set_event_action(uint8_t instance, tc_event_action_t action)
Sets the action to take when the TC receives an event.
Definition tc.c:202
void tc_enable_interrupt(uint8_t instance, tc_interrupt_t mask)
Enables TC interrupts for the specified sources.
Definition tc.c:222
void tc_disable_interrupt(uint8_t instance, tc_interrupt_t mask)
Disables TC interrupts for the specified sources.
Definition tc.c:228
void tc_enable_overflow_event_output(uint8_t instance, bool enabled)
Enables or disables overflow event output for the TC.
Definition tc.c:207
void tc_set_run_on_demand(uint8_t instance, bool onDemand)
Sets the TC's run-on-demand mode.
Definition tc.c:98
void tc_configure_callback(uint8_t instance, tc_cb_t callback)
Registers a callback for TC interrupts.
Definition tc.c:218
void tc_count8_set_cc(uint8_t instance, uint8_t channel, uint8_t value)
Sets the TC's compare channel CC0 or CC1 register in 8-bit mode.
Definition tc.c:147
void tc_enable_match_event_output(uint8_t instance, uint8_t channel, bool enabled)
Enables or disables match/capture event output for a TC channel.
Definition tc.c:211
uint8_t tc_count8_get_period(uint8_t instance)
Gets the TC's period or PER register in 8-bit mode.
Definition tc.c:143
void tc_irq_handler(uint8_t instance)
TC interrupt handler. Call this from your irq_handler_tcN() override.
Definition tc.c:236
void(* tc_cb_t)(tc_interrupt_t intflag)
Callback type for TC interrupts. The callback receives a bitmask of the interrupt flags that triggere...
Definition tc.h:89
uint32_t tc_count32_get_count(uint8_t instance)
Gets the TC's current counter value in 32-bit mode.
Definition tc.c:197
void tc_retrigger(uint8_t instance)
Issues a RETRIGGER command to the TC.
Definition tc.c:250
void tc_count8_set_period(uint8_t instance, uint8_t period)
Sets the TC's period or PER register in 8-bit mode.
Definition tc.c:139
void tc_set_counter_mode(uint8_t instance, tc_counter_mode_t mode)
Sets the TC's counting mode (an 8, 16 or 32-bit counter),.
Definition tc.c:89
tc_event_action_t
Definition tc.h:68
void tc_disable(uint8_t instance)
Disables the TC, but retains all its settings.
Definition tc.c:254
void tc_set_channel_polarity(uint8_t instance, uint8_t channel, tc_channel_polarity_t polarity)
Sets the polarity of one of the waveform output channels.
Definition tc.c:111
void tc_set_wavegen(uint8_t instance, tc_wavegen_t mode)
Sets the TC's waveform generation mode.
Definition tc.c:103
@ TC_EVENT_ACTION_PPW
NOTE: Not available on SAM D11 or D21.
Definition tc.h:74