Mozzi  version v2.0
sound synthesis library for Arduino
config_checks_rp2040.h
1 /*
2  * config_checks_rp2040.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2023-2024 Thomas Friedrichsmeier and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10 */
11 
12 #ifndef CONFIG_CHECK_RP2040_H
13 #define CONFIG_CHECK_RP2040_H
14 
15 /**
16  * @page hardware_rp2040 Mozzi on RP2040 (Raspberry Pi Pico)
17  *
18  * port by Thomas Friedrichsmeier
19  *
20  * @section rp2040_status Port status and notes
21  * Compiles and runs using [this core](https://github.com/earlephilhower/arduino-pico). Can probably be ported to the Mbed core for RP2040, relatively easily,
22  * as it relies mostly on the RP2040 SDK API. Tested on a Pi Pico.
23  *
24  * - This is a recent addition, implementation details may still change (currently just PWM driven by a timer; this may be worth changing to a DMA driven output)
25  * - Wavetables and samples are not kept in progmem on this platform. While apparently speed (of the external flash) is not much of an issue, the data always seems to be copied into RAM, anyway.
26  * - Note that getAudioInput() and mozziAnalogRead() return values in the RP2040's full ADC resolution of 0-4095 rather than AVR's 0-1023.
27  * - twi_nonblock is not ported
28  * - Code uses only one CPU core
29  *
30  * @section rp2040_output Output modes
31  * The following audio modes (see @ref MOZZI_AUDIO_MODE) are currently supported on this hardware:
32  * - MOZZI_OUTPUT_EXTERNAL_TIMED
33  * - MOZZI_OUTPUT_EXTERNAL_CUSTOM
34  * - MOZZI_OUTPUT_PWM
35  * - MOZZI_OUTPUT_I2S_DAC
36  *
37  * The default mode is @ref rp2040_pwm .
38  *
39  * @section rp2040_pwm MOZZI_OUTPUT_PWM
40  * Audio output is written to pin 0 (mono) or 0 and 1 (stereo), by default, with 11 bits of ouput resolution.
41  * One hardware timer interrupt and one DMA channel are claimed (number not hardcoded), a non-exclusive handler is installed on DMA_IRQ_0.
42  *
43  * Configuration options:
44  * @code
45  * #define MOZZI_AUDIO_PIN_1 ... // default is 0
46  * #define MOZZI_AUDIO_BITS ... // output resolution (bits); default is 11
47  * // additionally, for stereo:
48  * #define MOZZI_AUDIO_PIN_2 ... // default is 1; this must be on the same PWM slice as the first pin (i.e. neighboring)
49  * @endcode
50  *
51  * @section rp2040_i2s_dac MOZZI_OUTPUT_I2S_DAC
52  * Output to an external DAC, connected via I2S. This uses 16 bit (per audio channel), but can be changed to 8, 16, 24 (left aligned) and 32 resolution.
53  * Both plain I2S and LSBJ format (PT8211 needs this, for instance) are available. Plain format is used by default. The GPIO pins to use can be configured,
54  * - almost - freely (see below). Two DMA channels are claimed (numbers not hardcoded), non-exclusive handlers are installed on DMA_IRQ_0.
55  *
56  * Configuration options:
57  * @code
58  * #define MOZZI_AUDIO_BITS ... // available values are 8, 16 (default), 24 (LEFT ALIGN in 32 bits type!!) and 32 bits
59  * #define MOZZI_I2S_PIN_BCK ... // /BLCK) default is 20
60  * //#define MOZZI_I2S_PIN_WS (MOZZI_I2S_PIN_BCK+1) ... // CANNOT BE CHANGED, HAS TO BE NEXT TO pBCLK, i.e. default is 21
61  * #define MOZZI_I2S_PIN_DATA ... // (DOUT) default is 22
62  * #define MOZZI_I2S_FORMAT ... // may be MOZZI_I2S_FORMAT_LSBJ or MOZZI_I2S_FORMAT_PLAIN (default)
63  * @endcode
64  *
65  * @note
66  * The MOZZI_I2S_FORMAT_LSBJ option may require a relatively recent git-hub checkout of the arduino-pico core.
67  *
68  * @section rp2040_external MOZZI_OUTPUT_EXTERNAL_TIMED and MOZZI_OUTPUT_EXTERNAL_CUSTOM
69  * See @ref external_audio
70 */
71 
72 #if not IS_RP2040()
73 #error This header should be included for RP2040 architecture (Raspberry Pi Pico and others), only
74 #endif
75 
77 #if !defined(MOZZI_AUDIO_MODE)
78 # define MOZZI_AUDIO_MODE MOZZI_OUTPUT_PWM
79 #endif
80 MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_I2S_DAC)
81 
82 #if !defined(MOZZI_AUDIO_RATE)
83 #define MOZZI_AUDIO_RATE 32768
84 #endif
85 
86 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
87 # if !defined(MOZZI_AUDIO_BITS)
88 # define MOZZI_AUDIO_BITS 11
89 # endif
90 # if !defined(MOZZI_AUDIO_PIN_1)
91 # define MOZZI_AUDIO_PIN_1 0
92 # endif
93 # if !defined(MOZZI_AUDIO_PIN_2)
94 # define MOZZI_AUDIO_PIN_2 1
95 # endif
96 #endif
97 
98 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
99 # if !defined(MOZZI_AUDIO_BITS)
100 # define MOZZI_AUDIO_BITS 16
101 # endif
102 # if !defined(MOZZI_I2S_PIN_BCK)
103 # define MOZZI_I2S_PIN_BCK 20
104 # endif
105 //# define MOZZI_IS_PIN_WS(MOZZI_I2S_PIN_BCK + 1) // implicit
106 # if !defined(MOZZI_I2S_PIN_DATA)
107 # define MOZZI_I2S_PIN_DATA 22
108 # endif
109 # if !defined(MOZZI_I2S_FORMAT)
110 # define MOZZI_I2S_FORMAT MOZZI_I2S_FORMAT_PLAIN
111 # endif
112 MOZZI_CHECK_SUPPORTED(MOZZI_I2S_FORMAT, MOZZI_I2S_FORMAT_PLAIN, MOZZI_I2S_FORMAT_LSBJ)
113 # define BYPASS_MOZZI_OUTPUT_BUFFER true
114 # define MOZZI_RP2040_BUFFERS 8 // number of DMA buffers used
115 # define MOZZI_RP2040_BUFFER_SIZE 256 // total size of the buffer, in samples
116 #endif
117 
118 #if !defined(MOZZI_ANALOG_READ)
119 # define MOZZI_ANALOG_READ MOZZI_ANALOG_READ_STANDARD
120 #endif
121 
122 #define MOZZI__INTERNAL_ANALOG_READ_RESOLUTION 12
123 
124 MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE, MOZZI_ANALOG_READ_STANDARD)
125 MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE, MOZZI_ANALOG_READ_STANDARD)
126 
127 #endif // #ifndef CONFIG_CHECK_RP2040_H