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
30 uint8_t adcPinToChannelNum(uint8_t pin) {
31 return pin - PNUM_ANALOG_BASE;
34 uint32_t adc_pins_initialized = 0;
35 int16_t previously_sampled_pin = -1;
36 bool conversion_running =
false;
37 ADC_HandleTypeDef AdcHandle = {};
40 #include "MozziGuts_impl_STM32duino_analog.hpp"
43 void adcStartConversion(int8_t pin) {
44 if (pin != previously_sampled_pin) {
45 if (conversion_running) {
46 HAL_ADC_Stop(&AdcHandle);
47 HAL_ADC_DeInit(&AdcHandle);
49 previously_sampled_pin = pin;
50 uint32_t mask = 1 << pin;
51 if (!(adc_pins_initialized & mask)) {
52 analogRead(pin+PNUM_ANALOG_BASE);
53 adc_pins_initialized += mask;
56 adc_setup_read(analogInputToPinName(pin+PNUM_ANALOG_BASE), 16);
57 conversion_running =
true;
60 void startSecondADCReadOnCurrentChannel() {
61 HAL_ADC_Start(&AdcHandle);
62 conversion_running =
true;
65 void setupMozziADC(int8_t ) {
68 #define AUDIO_HOOK_HOOK checkADCConversionComplete();
70 void checkADCConversionComplete() {
71 if (!conversion_running)
return;
72 if(HAL_ADC_PollForConversion(&AdcHandle, 0) == HAL_OK) {
73 conversion_running =
false;
80 void setupFastAnalogRead(int8_t ) {
89 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
90 HardwareTimer audio_update_timer(MOZZI_AUDIO_UPDATE_TIMER);
91 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM)
92 HardwareTimer audio_update_timer(MOZZI_AUDIO_UPDATE_TIMER);
93 HardwareTimer *pwm_timer_ht;
95 uint32_t pwm_timer_channel_1 = STM_PIN_CHANNEL(pinmap_function(output_pin_1, PinMap_TIM));
97 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
98 PinName output_pin_1_low = digitalPinToPinName(MOZZI_AUDIO_PIN_1_LOW);
99 uint32_t pwm_timer_channel_1_low = STM_PIN_CHANNEL(pinmap_function(output_pin_1_low, PinMap_TIM));
100 # elif (MOZZI_AUDIO_CHANNELS > 1)
101 PinName output_pin_2 = digitalPinToPinName(MOZZI_AUDIO_PIN_2);
102 uint32_t pwm_timer_channel_2 = STM_PIN_CHANNEL(pinmap_function(output_pin_2, PinMap_TIM));
106 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
110 pwm_timer_ht->setCaptureCompare(pwm_timer_channel_1, f.l()+MOZZI_AUDIO_BIAS);
111 # if (MOZZI_AUDIO_CHANNELS > 1)
112 pwm_timer_ht->setCaptureCompare(pwm_timer_channel_2, f.r()+MOZZI_AUDIO_BIAS);
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();
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)
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));
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();
178 void MozziRandPrivate::autoSeed() {
179 #warning Automatic random seeding is not implemented on this platform
#define MOZZI_AUDIO_PIN_1
Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM: The IO pin to use as (first) ...
#define MOZZI_AUDIO_BITS_PER_CHANNEL
Only for MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM.
#define MOZZI_AUDIO_RATE
Defines the audio rate, i.e.
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
This struct encapsulates one frame of mono audio output.