@@ -58,6 +58,26 @@ uint8_t tcc_channels[3]; // Set by pwmout_reset() to {0xf0, 0xfc, 0xfc} initia
5858uint8_t tcc_channels [5 ]; // Set by pwmout_reset() to {0xc0, 0xf0, 0xf8, 0xfc, 0xfc} initially.
5959#endif
6060
61+ static uint8_t never_reset_tc_or_tcc [TC_INST_NUM + TCC_INST_NUM ];
62+
63+ void common_hal_pulseio_pwmout_never_reset (pulseio_pwmout_obj_t * self ) {
64+ if (self -> timer -> is_tc ) {
65+ never_reset_tc_or_tcc [self -> timer -> index ] += 1 ;
66+ } else {
67+ never_reset_tc_or_tcc [TC_INST_NUM + self -> timer -> index ] += 1 ;
68+ }
69+
70+ never_reset_pin_number (self -> pin -> number );
71+ }
72+
73+ void common_hal_pulseio_pwmout_reset_ok (pulseio_pwmout_obj_t * self ) {
74+ if (self -> timer -> is_tc ) {
75+ never_reset_tc_or_tcc [self -> timer -> index ] -= 1 ;
76+ } else {
77+ never_reset_tc_or_tcc [TC_INST_NUM + self -> timer -> index ] -= 1 ;
78+ }
79+ }
80+
6181void pwmout_reset (void ) {
6282 // Reset all timers
6383 for (int i = 0 ; i < TCC_INST_NUM ; i ++ ) {
@@ -66,6 +86,9 @@ void pwmout_reset(void) {
6686 }
6787 Tcc * tccs [TCC_INST_NUM ] = TCC_INSTS ;
6888 for (int i = 0 ; i < TCC_INST_NUM ; i ++ ) {
89+ if (never_reset_tc_or_tcc [TC_INST_NUM + i ] > 0 ) {
90+ continue ;
91+ }
6992 // Disable the module before resetting it.
7093 if (tccs [i ]-> CTRLA .bit .ENABLE == 1 ) {
7194 tccs [i ]-> CTRLA .bit .ENABLE = 0 ;
@@ -81,6 +104,9 @@ void pwmout_reset(void) {
81104 }
82105 Tc * tcs [TC_INST_NUM ] = TC_INSTS ;
83106 for (int i = 0 ; i < TC_INST_NUM ; i ++ ) {
107+ if (never_reset_tc_or_tcc [i ] > 0 ) {
108+ continue ;
109+ }
84110 tcs [i ]-> COUNT16 .CTRLA .bit .SWRST = 1 ;
85111 while (tcs [i ]-> COUNT16 .CTRLA .bit .SWRST == 1 ) {
86112 }
@@ -99,11 +125,11 @@ bool channel_ok(const pin_timer_t* t) {
99125 t -> is_tc ;
100126}
101127
102- void common_hal_pulseio_pwmout_construct (pulseio_pwmout_obj_t * self ,
103- const mcu_pin_obj_t * pin ,
104- uint16_t duty ,
105- uint32_t frequency ,
106- bool variable_frequency ) {
128+ pwmout_result_t common_hal_pulseio_pwmout_construct (pulseio_pwmout_obj_t * self ,
129+ const mcu_pin_obj_t * pin ,
130+ uint16_t duty ,
131+ uint32_t frequency ,
132+ bool variable_frequency ) {
107133 self -> pin = pin ;
108134 self -> variable_frequency = variable_frequency ;
109135
@@ -113,11 +139,11 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
113139 && pin -> timer [2 ].index >= TCC_INST_NUM
114140#endif
115141 ) {
116- mp_raise_ValueError ( translate ( "Invalid pin" )) ;
142+ return PWMOUT_INVALID_PIN ;
117143 }
118144
119145 if (frequency == 0 || frequency > 6000000 ) {
120- mp_raise_ValueError ( translate ( "Invalid PWM frequency" )) ;
146+ return PWMOUT_INVALID_FREQUENCY ;
121147 }
122148
123149 // Figure out which timer we are using.
@@ -184,11 +210,9 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
184210
185211 if (timer == NULL ) {
186212 if (found ) {
187- mp_raise_ValueError (translate ("All timers for this pin are in use" ));
188- } else {
189- mp_raise_RuntimeError (translate ("All timers in use" ));
213+ return PWMOUT_ALL_TIMERS_ON_PIN_IN_USE ;
190214 }
191- return ;
215+ return PWMOUT_ALL_TIMERS_IN_USE ;
192216 }
193217
194218 uint8_t resolution = 0 ;
@@ -259,6 +283,7 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
259283 gpio_set_pin_function (pin -> number , GPIO_PIN_FUNCTION_E + mux_position );
260284
261285 common_hal_pulseio_pwmout_set_duty_cycle (self , duty );
286+ return PWMOUT_OK ;
262287}
263288
264289bool common_hal_pulseio_pwmout_deinited (pulseio_pwmout_obj_t * self ) {
0 commit comments