12 #include "HardwareTimer.h"
17 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
24 uint8_t stm32_current_adc_pin;
25 #define getADCReading() adc.getData()
26 #define channelNumToIndex(channel) STM32PinMap(channel)
27 uint8_t adcPinToChannelNum(uint8_t pin) {
31 void adcStartConversion(uint8_t channel) {
32 stm32_current_adc_pin = channel;
33 adc.setPins(&stm32_current_adc_pin, 1);
34 adc.startConversion();
37 static void startSecondADCReadOnCurrentChannel() {
38 adc.setPins(&stm32_current_adc_pin, 1);
39 adc.startConversion();
42 void stm32_adc_eoc_handler() {
46 void setupMozziADC(int8_t speed) {
47 adc.attachInterrupt(stm32_adc_eoc_handler);
51 inline uint8_t STM32PinMap(uint8_t pin)
53 if (pin > 15)
return pin-8;
57 void setupFastAnalogRead(int8_t speed) {
59 if (speed == FASTEST_ADC) adc.setSampleRate(ADC_SMPR_1_5);
60 else if (speed == FASTER_ADC) adc.setSampleRate(ADC_SMPR_13_5);
61 else (adc.setSampleRate(ADC_SMPR_41_5));
70 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
71 HardwareTimer audio_update_timer(MOZZI_AUDIO_UPDATE_TIMER);
72 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM)
73 HardwareTimer audio_update_timer(MOZZI_AUDIO_UPDATE_TIMER);
74 HardwareTimer audio_pwm_timer(MOZZI_AUDIO_PWM_TIMER);
77 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM)
82 # if (MOZZI_AUDIO_CHANNELS > 1)
83 pwmWrite(MOZZI_AUDIO_PIN_2, f.r()+MOZZI_AUDIO_BIAS);
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();
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)
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(
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();
147 void MozziRandPrivate::autoSeed() {
151 adc.enableInternalReading();
156 conv.cf = adc.readTemp();
159 conv.cf = adc.readTemp();
162 conv.cf = adc.readTemp();
#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.