11#include " Arduino.h"
22static XMC_CCU4_SLICE_t *active_pwm4_slice = nullptr ; // Active CCU4 slice
33static XMC_CCU8_SLICE_t *active_pwm8_slice = nullptr ; // Active CCU8 slice
4- static XMC_CCU4_MODULE_t *active_pwm4_ccu = nullptr ; // Active CCU4 module
5- static XMC_CCU8_MODULE_t *active_pwm8_ccu = nullptr ; // Active CCU8 module
4+ static XMC_CCU4_MODULE_t *active_pwm4_ccu = nullptr ; // Active CCU4 module
5+ static XMC_CCU8_MODULE_t *active_pwm8_ccu = nullptr ; // Active CCU8 module
66volatile uint8_t slice_number_ccu4;
77volatile uint8_t slice_number_ccu8;
88volatile uint32_t software_counter = 0 ;
99volatile uint32_t required_duration_ticks = 0 ;
1010
11-
12-
1311#ifdef __cplusplus
1412extern " C" {
1513#endif
16- void CCU43_0_IRQHandler (void ) {
14+ void CCU43_2_IRQHandler (void ) {
1715 software_counter++;
18- if (software_counter >= required_duration_ticks){
19- if (active_pwm4_slice != nullptr && active_pwm4_ccu != nullptr ) {
16+ if (software_counter >= required_duration_ticks) {
17+ if (active_pwm4_slice != nullptr && active_pwm4_ccu != nullptr ) {
2018 XMC_CCU4_SLICE_StopTimer (active_pwm4_slice);
2119 XMC_CCU4_DisableClock (active_pwm4_ccu, slice_number_ccu4); // Disable the clock
2220
2321 active_pwm4_slice = nullptr ; // Clear the active CCU4 slice
24- active_pwm4_ccu = nullptr ; // Clear the active CCU4 module
22+ active_pwm4_ccu = nullptr ; // Clear the active CCU4 module
2523 }
26- if (active_pwm8_slice != nullptr && active_pwm8_ccu != nullptr ) {
24+ if (active_pwm8_slice != nullptr && active_pwm8_ccu != nullptr ) {
2725 XMC_CCU8_SLICE_StopTimer (active_pwm8_slice);
2826 XMC_CCU8_DisableClock (active_pwm8_ccu, slice_number_ccu8);
2927 active_pwm8_slice = nullptr ; // Clear the active CCU8 slice
30- active_pwm8_ccu = nullptr ; // Clear the active CCU8 module
28+ active_pwm8_ccu = nullptr ; // Clear the active CCU8 module
3129 }
32- XMC_CCU4_SLICE_StopTimer (CCU43_CC40 );
33- XMC_CCU4_SLICE_ClearEvent (CCU43_CC40 , XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
30+ XMC_CCU4_SLICE_StopTimer (CCU43_CC42 );
31+ XMC_CCU4_SLICE_ClearEvent (CCU43_CC42 , XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
3432 software_counter = 0 ;
35- NVIC_DisableIRQ (CCU43_0_IRQn );
33+ NVIC_DisableIRQ (CCU43_2_IRQn );
3634 }
37- XMC_CCU4_SLICE_ClearEvent (CCU43_CC40 , XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
35+ XMC_CCU4_SLICE_ClearEvent (CCU43_CC42 , XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
3836}
3937
4038#ifdef __cplusplus
@@ -54,34 +52,33 @@ class Tone {
5452 stop (pin);
5553 if ((pin_index = scanMapTable (mapping_pin_PWM4, pin)) >= 0 ) {
5654 XMC_PWM4_t *pwm4 = &mapping_pwm4[pin_index];
57- active_pwm4_slice = pwm4->slice ; // Store the active CCU4 slice
58- active_pwm4_ccu = pwm4->ccu ; // Store the active CCU4 module
55+ active_pwm4_slice = pwm4->slice ; // Store the active CCU4 slice
56+ active_pwm4_ccu = pwm4->ccu ; // Store the active CCU4 module
5957 slice_number_ccu4 = pwm4->slice_num ; // Store the slice number
6058 configureTone (pin, frequency);
6159 XMC_GPIO_SetMode (pwm4->port_pin .port , pwm4->port_pin .pin ,
6260 (XMC_GPIO_MODE_t)(XMC_GPIO_MODE_OUTPUT_PUSH_PULL | pwm4->port_mode ));
6361 XMC_CCU4_SLICE_StartTimer (pwm4->slice );
64- if (duration >0 ) {
62+ if (duration > 0 ) {
6563 configureTimerInterrupt (duration);
6664 }
6765 }
6866#if defined(CCU8V2) || defined(CCU8V1)
6967 else if ((pin_index = scanMapTable (mapping_pin_PWM8, pin)) >= 0 ) {
7068 XMC_PWM8_t *pwm8 = &mapping_pwm8[pin_index];
7169 active_pwm8_slice = pwm8->slice ; // Store the active CCU8 slice
72- active_pwm8_ccu = pwm8->ccu ; // Store the active CCU8 module
70+ active_pwm8_ccu = pwm8->ccu ; // Store the active CCU8 module
7371 slice_number_ccu8 = pwm8->slice_num ;
7472 configureTone (pin, frequency);
7573 XMC_GPIO_SetMode (pwm8->port_pin .port , pwm8->port_pin .pin ,
7674 (XMC_GPIO_MODE_t)(XMC_GPIO_MODE_OUTPUT_PUSH_PULL | pwm8->port_mode ));
7775 XMC_CCU8_SLICE_StartTimer (pwm8->slice );
7876 // calculate pulses
79- if (duration >0 ) {
77+ if (duration > 0 ) {
8078 configureTimerInterrupt (duration);
8179 }
8280 }
8381#endif
84-
8582 }
8683
8784 void stop (pin_size_t pin) {
@@ -150,7 +147,6 @@ class Tone {
150147 XMC_CCU8_EnableShadowTransfer (
151148 pwm8->ccu , XMC_CCU8_SHADOW_TRANSFER_SLICE_0 | XMC_CCU8_SHADOW_TRANSFER_SLICE_2 |
152149 XMC_CCU8_SHADOW_TRANSFER_SLICE_3 | XMC_CCU8_SHADOW_TRANSFER_SLICE_1);
153-
154150 }
155151#endif
156152 }
@@ -159,33 +155,35 @@ class Tone {
159155 software_counter = 0 ;
160156 XMC_CCU4_Init (CCU43, XMC_CCU4_SLICE_MCMS_ACTION_TRANSFER_PR_CR);
161157 XMC_CCU4_SLICE_COMPARE_CONFIG_t timer_config;
162- timer_config.timer_mode = XMC_CCU4_SLICE_TIMER_COUNT_MODE_EA;
163- timer_config.monoshot = XMC_CCU4_SLICE_TIMER_REPEAT_MODE_REPEAT;
164- timer_config.prescaler_mode = XMC_CCU4_SLICE_PRESCALER_MODE_NORMAL;
165- timer_config.prescaler_initval = XMC_CCU4_SLICE_PRESCALER_64;
166- timer_config.float_limit = XMC_CCU4_SLICE_PRESCALER_32768;
167- timer_config.passive_level = XMC_CCU4_SLICE_OUTPUT_PASSIVE_LEVEL_LOW;
168- XMC_CCU4_SLICE_CompareInit (CCU43_CC40 , &timer_config);
158+ timer_config.timer_mode = XMC_CCU4_SLICE_TIMER_COUNT_MODE_EA;
159+ timer_config.monoshot = XMC_CCU4_SLICE_TIMER_REPEAT_MODE_REPEAT;
160+ timer_config.prescaler_mode = XMC_CCU4_SLICE_PRESCALER_MODE_NORMAL;
161+ timer_config.prescaler_initval = XMC_CCU4_SLICE_PRESCALER_64;
162+ timer_config.float_limit = XMC_CCU4_SLICE_PRESCALER_32768;
163+ timer_config.passive_level = XMC_CCU4_SLICE_OUTPUT_PASSIVE_LEVEL_LOW;
164+ XMC_CCU4_SLICE_CompareInit (CCU43_CC42 , &timer_config);
169165
170166 // Calculate period for the timer based on the duration
171167 uint32_t timer_freq = PCLK / 64 ;
172- uint32_t timer_ticks_ms = timer_freq /1000 ;
168+ uint32_t timer_ticks_ms = timer_freq / 1000 ;
173169 required_duration_ticks = duration_ms;
174- XMC_CCU4_SLICE_SetTimerPeriodMatch (CCU43_CC40, timer_ticks_ms);
175- XMC_CCU4_SetMultiChannelShadowTransferMode (CCU43, XMC_CCU4_MULTI_CHANNEL_SHADOW_TRANSFER_SW_SLICE0);
176- XMC_CCU4_EnableShadowTransfer (CCU43,XMC_CCU4_SHADOW_TRANSFER_SLICE_0);
177- XMC_CCU4_SLICE_SetInterruptNode (CCU43_CC40, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH, XMC_CCU4_SLICE_SR_ID_0);
178- XMC_CCU4_SLICE_EnableEvent (CCU43_CC40, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
179- XMC_CCU4_EnableClock (CCU43, 0 );
170+ XMC_CCU4_SLICE_SetTimerPeriodMatch (CCU43_CC42, timer_ticks_ms);
171+ XMC_CCU4_SetMultiChannelShadowTransferMode (
172+ CCU43, XMC_CCU4_MULTI_CHANNEL_SHADOW_TRANSFER_SW_SLICE2);
173+ XMC_CCU4_EnableShadowTransfer (CCU43, XMC_CCU4_SHADOW_TRANSFER_SLICE_2);
174+ XMC_CCU4_SLICE_SetInterruptNode (CCU43_CC42, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH,
175+ XMC_CCU4_SLICE_SR_ID_2);
176+ XMC_CCU4_SLICE_EnableEvent (CCU43_CC42, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
177+ XMC_CCU4_EnableClock (CCU43, 2 );
180178
181179 // Enable the NVIC interrupt
182- XMC_CCU4_SLICE_SetTimerValue (CCU43_CC40 , 0U );
180+ XMC_CCU4_SLICE_SetTimerValue (CCU43_CC42 , 0U );
183181
184182 // Start the timer
185- XMC_CCU4_SLICE_StartTimer (CCU43_CC40 );
186- NVIC_SetPriority (CCU43_0_IRQn , NVIC_EncodePriority (NVIC_GetPriorityGrouping (), 63 , 0 ));
183+ XMC_CCU4_SLICE_StartTimer (CCU43_CC42 );
184+ NVIC_SetPriority (CCU43_2_IRQn , NVIC_EncodePriority (NVIC_GetPriorityGrouping (), 63 , 0 ));
187185
188- NVIC_EnableIRQ (CCU43_0_IRQn );
186+ NVIC_EnableIRQ (CCU43_2_IRQn );
189187 }
190188
191189 // Scan the pwm pin mapping table
@@ -210,4 +208,3 @@ void tone(pin_size_t pin, unsigned int frequency, unsigned long duration) {
210208}
211209
212210void noTone (pin_size_t pin) { inst_Tone.stop (pin); }
213-
0 commit comments