12 #include "HardwareTimer.h"
17 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
25 #define getADCReading() HAL_ADC_GetValue(&AdcHandle)
29 #define channelNumToIndex(channel) channel
40 #include "MozziGuts_impl_STM32duino_analog.hpp"
68 #define AUDIO_HOOK_HOOK checkADCConversionComplete();
80 void setupFastAnalogRead(int8_t ) {
89 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
91 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM)
97 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
100 # elif (MOZZI_AUDIO_CHANNELS > 1
)
106 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
111 # if (MOZZI_AUDIO_CHANNELS > 1
)
118 static void startAudio() {
119 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
120 audio_update_timer.pause();
123 uint32_t period_cyc = F_CPU / MOZZI_AUDIO_RATE;
124 uint16_t prescaler = (uint16_t)(period_cyc / 65535 + 1);
125 uint16_t overflow = (uint16_t)((period_cyc + (prescaler / 2)) / prescaler);
126 audio_update_timer.setPrescaleFactor(prescaler);
127 audio_update_timer.setOverflow(overflow);
128 audio_update_timer.setMode( 1, TIMER_OUTPUT_COMPARE);
129 audio_update_timer.setCaptureCompare( 1, 1);
130 audio_update_timer.attachInterrupt( 1, defaultAudioOutput);
131 audio_update_timer.refresh();
134 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM)
136 pinMode(MOZZI_AUDIO_PIN_1, OUTPUT);
137 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
138 pinMode(MOZZI_AUDIO_PIN_1_LOW, OUTPUT);
139 # elif (MOZZI_AUDIO_CHANNELS > 1
)
140 pinMode(MOZZI_AUDIO_PIN_2, OUTPUT);
143 # define MAX_CARRIER_FREQ (F_CPU / (1
<< MOZZI_AUDIO_BITS_PER_CHANNEL))
145 TIM_TypeDef *pwm_timer_tim = (TIM_TypeDef *) pinmap_peripheral(output_pin_1, PinMap_TIM);
146 pwm_timer_ht =
new HardwareTimer(pwm_timer_tim);
147 pwm_timer_ht->setMode(pwm_timer_channel_1, TIMER_OUTPUT_COMPARE_PWM1, output_pin_1);
148 # if MAX_CARRIER_FREQ < (MOZZI_AUDIO_RATE * 5
)
150 pwm_timer_ht->setPrescaleFactor(1);
155 pwm_timer_ht->setPrescaleFactor((
int)MAX_CARRIER_FREQ / (MOZZI_AUDIO_RATE * 5));
158 pwm_timer_ht->setOverflow(1 << MOZZI_AUDIO_BITS_PER_CHANNEL);
159 pwm_timer_ht->setCaptureCompare(pwm_timer_channel_1, MOZZI_AUDIO_BIAS );
161 pwm_timer_ht->resume();
164 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
165 audio_update_timer.resume();
170 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
171 audio_update_timer.pause();
179 #warning Automatic random seeding is not implemented on this platform