12 #include "HardwareTimer.h"
17 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
25 #define getADCReading() adc.getData()
26 #define channelNumToIndex(channel) STM32PinMap(channel)
53 if (
pin > 15)
return pin-8;
70 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
72 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM)
77 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
82 # if (MOZZI_AUDIO_CHANNELS > 1
)
89 static void startAudio() {
90 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
91 audio_update_timer.pause();
94 uint32_t period_cyc = F_CPU / MOZZI_AUDIO_RATE;
95 uint16_t prescaler = (uint16_t)(period_cyc / 65535 + 1);
96 uint16_t overflow = (uint16_t)((period_cyc + (prescaler / 2)) / prescaler);
97 audio_update_timer.setPrescaleFactor(prescaler);
98 audio_update_timer.setOverflow(overflow);
99 audio_update_timer.setMode(TIMER_CH1, TIMER_OUTPUT_COMPARE);
100 audio_update_timer.setCompare(TIMER_CH1,
102 audio_update_timer.attachInterrupt(TIMER_CH1, defaultAudioOutput);
103 audio_update_timer.refresh();
104 audio_update_timer.resume();
107 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM)
108 pinMode(MOZZI_AUDIO_PIN_1, PWM);
109 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
110 pinMode(MOZZI_AUDIO_PIN_1_LOW, PWM);
111 # elif (MOZZI_AUDIO_CHANNELS > 1
)
112 pinMode(MOZZI_AUDIO_PIN_2, PWM);
115 # define MAX_CARRIER_FREQ (F_CPU / (1
<< MOZZI_AUDIO_BITS_PER_CHANNEL))
116 # if MAX_CARRIER_FREQ < MOZZI_AUDIO_RATE
117 # error Configured audio resolution is definitely too high at the configured audio rate (and the given CPU speed)
118 # elif MAX_CARRIER_FREQ < (MOZZI_AUDIO_RATE * 3
)
119 # warning Configured audio resolution may be higher than optimal at the configured audio rate (and the given CPU speed)
122 # if MAX_CARRIER_FREQ < (MOZZI_AUDIO_RATE * 5
)
124 audio_pwm_timer.setPrescaleFactor(1);
129 audio_pwm_timer.setPrescaleFactor((
int)MAX_CARRIER_FREQ / (MOZZI_AUDIO_RATE * 5));
131 audio_pwm_timer.setOverflow(
132 1 << MOZZI_AUDIO_BITS_PER_CHANNEL);
134 # undef MAX_CARRIER_FREQ
139 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
140 audio_update_timer.pause();