35
35
36
36
#undef ENABLE
37
37
38
+ # define _TCC_SIZE (n ,unused ) TPASTE3(TCC,n,_SIZE),
39
+ # define TCC_SIZES { MREPEAT(TCC_INST_NUM, _TCC_SIZE, 0) }
40
+
38
41
uint32_t target_timer_frequencies [TC_INST_NUM + TCC_INST_NUM ];
39
42
uint8_t timer_refcount [TC_INST_NUM + TCC_INST_NUM ];
40
43
const uint16_t prescaler [8 ] = {1 , 2 , 4 , 8 , 16 , 64 , 256 , 1024 };
@@ -55,6 +58,12 @@ void pwmout_reset(void) {
55
58
}
56
59
Tcc * tccs [TCC_INST_NUM ] = TCC_INSTS ;
57
60
for (int i = 0 ; i < TCC_INST_NUM ; i ++ ) {
61
+ // Disable the module before resetting it.
62
+ if (tccs [i ]-> CTRLA .bit .ENABLE == 1 ) {
63
+ tccs [i ]-> CTRLA .bit .ENABLE = 0 ;
64
+ while (tccs [i ]-> SYNCBUSY .bit .ENABLE == 1 ) {
65
+ }
66
+ }
58
67
tccs [i ]-> CTRLA .bit .SWRST = 1 ;
59
68
}
60
69
Tc * tcs [TC_INST_NUM ] = TC_INSTS ;
@@ -63,6 +72,8 @@ void pwmout_reset(void) {
63
72
continue ;
64
73
}
65
74
tcs [i ]-> COUNT16 .CTRLA .bit .SWRST = 1 ;
75
+ while (tcs [i ]-> COUNT16 .CTRLA .bit .SWRST == 1 ) {
76
+ }
66
77
}
67
78
}
68
79
@@ -142,7 +153,9 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
142
153
if (t -> is_tc ) {
143
154
resolution = 16 ;
144
155
} else {
145
- resolution = 24 ;
156
+ // TCC resolution varies so look it up.
157
+ const uint8_t _tcc_sizes [TCC_INST_NUM ] = TCC_SIZES ;
158
+ resolution = _tcc_sizes [index ];
146
159
}
147
160
// First determine the divisor that gets us the highest resolution.
148
161
uint32_t system_clock = system_cpu_clock_get_hz ();
@@ -164,7 +177,10 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
164
177
config_tc .wave_generation = TC_WAVE_GENERATION_MATCH_PWM ;
165
178
config_tc .counter_16_bit .compare_capture_channel [0 ] = top ;
166
179
167
-
BE90
tc_init (& self -> tc_instance , t -> tc , & config_tc );
180
+ enum status_code status = tc_init (& self -> tc_instance , t -> tc , & config_tc );
181
+ if (status != STATUS_OK ) {
182
+ mp_raise_RuntimeError ("Failed to init timer" );
183
+ }
168
184
tc_enable (& self -> tc_instance );
169
185
} else {
170
186
struct tcc_config config_tcc ;
@@ -174,7 +190,10 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
174
190
config_tcc .counter .period = top ;
175
191
config_tcc .compare .wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM ;
176
192
177
- tcc_init (& self -> tcc_instance , t -> tcc , & config_tcc );
193
+ enum status_code status = tcc_init (& self -> tcc_instance , t -> tcc , & config_tcc );
194
+ if (status != STATUS_OK ) {
195
+ mp_raise_RuntimeError ("Failed to init timer" );
196
+ }
178
197
tcc_enable (& self -> tcc_instance );
179
198
}
180
199
0 commit comments