11#include " Arduino.h"
22#define CCU8V2 // for CCU8 PWM pins configure
3+ XMC_PWM4_t *pwm4;
34
45class Tone {
56public:
@@ -18,17 +19,27 @@ class Tone {
1819 XMC_GPIO_MODE_OUTPUT_PUSH_PULL | pwm4->port_mode );
1920 XMC_CCU4_SLICE_StartTimer (pwm4->slice );
2021 // count pulses for stop the timer once reached required pulses
22+ // if (duration > 0) {
23+ // uint32_t required_pulses = frequency * duration / 1000;
24+ // for (uint32_t i = 0; i < required_pulses; i++) {
25+ // while (
26+ // !XMC_CCU4_SLICE_GetEvent(pwm4->slice,
27+ // XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH)) {
28+ // }
29+ // XMC_CCU4_SLICE_ClearEvent(pwm4->slice, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
30+ // }
31+ // XMC_CCU4_SLICE_StopTimer(pwm4->slice);
32+ // }
33+ // if(duration >0) {
34+ // unsigned long start_time = millis();
35+ // while(millis() - start_time < duration) {
36+ // delay(1);
37+ // }
38+ // XMC_CCU4_SLICE_StopTimer(pwm4->slice);
39+ // }
2140 if (duration > 0 ) {
22- uint32_t required_pulses = frequency * duration / 1000 ;
23- for (uint32_t i = 0 ; i < required_pulses; i++) {
24- while (
25- !XMC_CCU4_SLICE_GetEvent (pwm4->slice , XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH)) {
26- }
27- XMC_CCU4_SLICE_ClearEvent (pwm4->slice , XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
28- }
29- XMC_CCU4_SLICE_StopTimer (pwm4->slice );
41+ configureTimerInterrupt (duration);
3042 }
31-
3243 }
3344#if defined(CCU8V2) || defined(CCU8V1)
3445 else if ((pin_index = scanMapTable (mapping_pin_PWM8, pin)) >= 0 ) {
@@ -38,15 +49,26 @@ class Tone {
3849 XMC_GPIO_MODE_OUTPUT_PUSH_PULL | pwm8->port_mode );
3950 XMC_CCU8_SLICE_StartTimer (pwm8->slice );
4051 // calculate pulses
52+ // if (duration > 0) {
53+ // uint32_t required_pulses = frequency * duration / 1000;
54+ // for (uint32_t i = 0; i < required_pulses; i++) {
55+ // while (
56+ // !XMC_CCU8_SLICE_GetEvent(pwm8->slice,
57+ // XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH)) {
58+ // }
59+ // XMC_CCU8_SLICE_ClearEvent(pwm8->slice, XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH);
60+ // }
61+ // XMC_CCU8_SLICE_StopTimer(pwm8->slice);
62+ // }
63+ // if(duration >0) {
64+ // unsigned long start_time = millis();
65+ // while(millis() - start_time < duration) {
66+ // delay(1);
67+ // }
68+ // XMC_CCU8_SLICE_StopTimer(pwm8->slice);
69+ // }
4170 if (duration > 0 ) {
42- uint32_t required_pulses = frequency * duration / 1000 ;
43- for (uint32_t i = 0 ; i < required_pulses; i++) {
44- while (
45- !XMC_CCU8_SLICE_GetEvent (pwm8->slice , XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH)) {
46- }
47- XMC_CCU8_SLICE_ClearEvent (pwm8->slice , XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH);
48- }
49- XMC_CCU8_SLICE_StopTimer (pwm8->slice );
71+ configureTimerInterrupt (duration);
5072 }
5173 }
5274#endif
@@ -129,6 +151,30 @@ class Tone {
129151 }
130152 return -1 ;
131153 }
154+
155+ void configureTimerInterrupt (unsigned long duration_ms) {
156+
157+ XMC_CCU4_SLICE_COMPARE_CONFIG_t timer_config;
158+ memset (&timer_config, 0 , sizeof (timer_config));
159+ timer_config.prescaler_initval = XMC_CCU4_SLICE_PRESCALER_64;
160+ timer_config.passive_level = XMC_CCU4_SLICE_OUTPUT_PASSIVE_LEVEL_LOW;
161+
162+ XMC_CCU4_SLICE_CompareInit (CCU40_CC40, &timer_config);
163+
164+ // Calculate period for the timer based on the duration
165+ uint32_t timer_ticks = (PCLK / 64 ) * duration_ms / 1000 ;
166+ XMC_CCU4_SLICE_SetTimerPeriodMatch (CCU40_CC40, timer_ticks);
167+
168+ XMC_CCU4_SLICE_EnableEvent (CCU40_CC40, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
169+ XMC_CCU4_SLICE_SetInterruptNode (CCU40_CC40, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH,
170+ XMC_CCU4_SLICE_SR_ID_0);
171+
172+ // Enable the NVIC interrupt
173+ NVIC_EnableIRQ (CCU40_0_IRQn);
174+
175+ // Start the timer
176+ XMC_CCU4_SLICE_StartTimer (CCU40_CC40);
177+ }
132178};
133179
134180static Tone inst_Tone;
@@ -140,3 +186,11 @@ void tone(pin_size_t pin, unsigned int frequency, unsigned long duration) {
140186}
141187
142188void noTone (pin_size_t pin) { inst_Tone.stop (pin); }
189+
190+ void CCU40_0_IRQHandler (void ) {
191+
192+ XMC_CCU4_SLICE_StopTimer (pwm4->slice );
193+
194+ XMC_CCU4_SLICE_StopTimer (CCU40_CC40);
195+ XMC_CCU4_SLICE_ClearEvent (CCU40_CC40, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
196+ }
0 commit comments