Mozzi  version v2.0
sound synthesis library for Arduino
Mozzi on STM32-boards with libmaple based core.

port by Thomas Friedrichsmeier

Note
Be sure to understand the info given at Mozzi on STM32-based boards - disambiguation . This page is about using Mozzi with the STM32 "libmaple based" core.
This port may look similar to, but uses a different default GPIO pinout than @hardware_stm32duino !

Status and peculiarities of this port

Compiles for and runs on a STM32F103C8T6 blue pill board, with a bunch of caveats (see below), i.e. on a board without a real DAC. Should probably run on any other board supported by Roger Clark's libmaple-based core (although this theory is untested).

Note
that at the time of this writing, Stev Strong's slightliy more recent fork of this core does not work with Mozzi, apparently due to a bug in pwmWrite().
  • If you want to use MIDI, be sure to replace "MIDI_CREATE_DEFAULT_INSTANCE()" with "MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI)" (or Serial2)
  • MOZZI_AUDIO_INPUT_STANDARD is implemented in theory, but untested (feedback welcome)
  • getAudioInput() and mozziAnalogRead() return values in the STM32's full ADC resolution of 0-4095 rather than AVR's 0-1023.

twi_nonblock is not ported

Output modes

The following audio modes (see MOZZI_AUDIO_MODE) are currently supported on this hardware:

  • MOZZI_OUTPUT_EXTERNAL_TIMED
  • MOZZI_OUTPUT_EXTERNAL_CUSTOM
  • MOZZI_OUTPUT_PWM
  • MOZZI_OUTPUT_PWM_2PIN

The default mode is MOZZI_OUTPUT_PWM .

MOZZI_OUTPUT_PWM

Standard pulse width modulated output to one (mono) or two (stereo, see MOZZI_AUDIO_CHANNELS) GPIO pins. Default pinout: PB8 (mono/left), PB9 (right channel in stereo). This mode uses two hardware timers: One for the PWM (Timer 4 when using the default pin configuration), and a second for updating the output at audio rate. The default audio resolution is 10 bits, which results in a carrier frequency of ~70kHz on a 72MHz CPU. On slower boards you will have to descrease this. The following settings may be costumized, if desired:

#define MOZZI_AUDIO_PIN_1 ... // Left / mono output pin. Default: PB8
#define MOZZI_AUDIO_PWM_TIMER ... // Must be set ot the hardware timer connected to the above pin. Default: 4
#define MOZZI_AUDIO_UPDATE_TIMER ... // Second hardware timer to claim. Default 2
#define MOZZI_AUDIO_BITS ... // Output resolution in bits. Default is 10
// For stereo, only:
#define MOZZI_AUDIO_PIN_2 ... // Right channel output pin. This *must* be connected to the same hardware timer as MOZZI_AUDIO_PIN_1 ! Default: PB9

MOZZI_OUTPUT_2PIN_PWM

This mode is very similar to MOZZI_OUTPUT_PWM, but splitting output for a single channel across two GPIO pins for better resolution. For details on the required hardware setup, and configuration tradeoffs, see MOZZI_OUTPUT_2PIN_PWM . Stereo output is not available in this mode. Output is at 2*7 bits at up to 560kHz carrier frequency (but limited to 5 times audio rate).

Customizable configuration options:

#define MOZZI_AUDIO_PIN_1 ... // High byte of the output. Default: PB8
#define MOZZI_AUDIO_PIN_2 ... // Low byte of the output. Default: PB9
#define MOZZI_AUDIO_PWM_TIMER ... // Must be set to the number of the hardware timer connect to the above pins. Default: 4
#define MOZZI_AUDIO_UPDATE_TIMER ... // Second hardware timer to claim. Default TIM2
#define MOZZI_AUDIO_BITS_PER_CHANNEL ... // Bits per pin. Default is 7

MOZZI_OUTPUT_EXTERNAL_TIMED and MOZZI_OUTPUT_EXTERNAL_CUSTOM

See External audio output The (single) hardware timer claimed for MOZZI_OUTPUT_EXTERNAL_TIMED may be configured using "MOZZI_AUDIO_UPDATE_TIMER" (default: TIM2).