source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/Adafruit_ZeroPDM.h @ 31

Last change on this file since 31 was 3, checked in by markus, 7 years ago

Setup project structure

File size: 7.5 KB
Line 
1// Adafruit Arduino Zero / Feather M0 PDM mic library.
2// Author: Tony DiCola & Limor "Ladyada" Fried
3//
4// The MIT License (MIT)
5//
6// Copyright (c) 2016 Adafruit Industries
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in all
16// copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24// SOFTWARE.
25#ifndef ADAFRUIT_ZEROPDM_H
26#define ADAFRUIT_ZEROPDM_H
27
28#include <Arduino.h>
29#include "wiring_private.h"
30
31#if defined(ARDUINO_ARCH_SAMD)
32
33// Uncomment to enable debug message output.
34#define DEBUG
35
36// Define where debug output is printed (the native USB port on the Zero).
37#define DEBUG_PRINTER Serial
38
39
40class Adafruit_ZeroPDM {
41public:
42  // Create a new instance of an I2S audio transmitter.
43  // Can specify the pins to use and the generic clock ID to use for driving the I2S
44  // hardware (default is GCLK 3).
45  Adafruit_ZeroPDM(int clockpin, int datapin, uint8_t gclk = 3); 
46
47  // Initialize the I2S audio  receiver.
48  bool begin();
49  void end();
50
51  // Configure the transmitter with the sample rate (in hertz
52  bool configure(uint32_t sampleRateHz, boolean stereo);
53
54  // Read a single sample from the I2S subsystem.  Will wait until the I2S
55  // hardware is ready to receive the sample.
56  uint32_t read(void);
57
58  uint8_t getSerializer(void) { return _i2sserializer; };
59  bool read(uint32_t *buffer, int bufsiz);
60
61private:
62  int _clk, _data;
63  uint32_t _clk_pin, _clk_mux, _data_pin, _data_mux;
64  uint8_t _i2sserializer;
65  uint8_t _i2sclock;
66  uint8_t _gclk;
67
68  I2s *_hw;
69};
70
71
72#ifndef I2S_H_INCLUDED
73
74/**
75 * Master Clock (MCK) source selection
76 */
77enum i2s_master_clock_source {
78        /** Master Clock (MCK) is from general clock */
79        I2S_MASTER_CLOCK_SOURCE_GCLK,
80        /** Master Clock (MCK) is from MCK input pin */
81        I2S_MASTER_CLOCK_SOURCE_MCKPIN
82};
83
84/**
85 * Serial Clock (SCK) source selection
86 */
87enum i2s_serial_clock_source {
88        /** Serial Clock (SCK) is divided from Master Clock */
89        I2S_SERIAL_CLOCK_SOURCE_MCKDIV,
90        /** Serial Clock (SCK) is input from SCK input pin */
91        I2S_SERIAL_CLOCK_SOURCE_SCKPIN
92};
93
94/**
95 * Data delay from Frame Sync (FS)
96 */
97enum i2s_data_delay {
98        /** Left Justified (no delay) */
99        I2S_DATA_DELAY_0,
100        /** I2S data delay (1-bit delay) */
101        I2S_DATA_DELAY_1,
102        /** Left Justified (no delay) */
103        I2S_DATA_DELAY_LEFT_JUSTIFIED = I2S_DATA_DELAY_0,
104        /** I2S data delay (1-bit delay) */
105        I2S_DATA_DELAY_I2S = I2S_DATA_DELAY_1
106};
107
108/**
109 * Frame Sync (FS) source
110 */
111enum i2s_frame_sync_source {
112        /** Frame Sync (FS) is divided from I2S Serial Clock */
113        I2S_FRAME_SYNC_SOURCE_SCKDIV,
114        /** Frame Sync (FS) is input from FS input pin */
115        I2S_FRAME_SYNC_SOURCE_FSPIN
116};
117
118/**
119 * Frame Sync (FS) output pulse width
120 */
121enum i2s_frame_sync_width {
122        /** Frame Sync (FS) Pulse is 1 Slot width */
123        I2S_FRAME_SYNC_WIDTH_SLOT,
124        /** Frame Sync (FS) Pulse is half a Frame width */
125        I2S_FRAME_SYNC_WIDTH_HALF_FRAME,
126        /** Frame Sync (FS) Pulse is 1 Bit width */
127        I2S_FRAME_SYNC_WIDTH_BIT,
128        /** 1-bit wide Frame Sync (FS) per Data sample, only used when Data transfer
129         *  is requested */
130        I2S_FRAME_SYNC_WIDTH_BURST
131};
132
133/**
134 * Time Slot Size in number of I2S serial clocks (bits)
135 */
136enum i2s_slot_size {
137        /** 8-bit slot */
138        I2S_SLOT_SIZE_8_BIT,
139        /** 16-bit slot */
140        I2S_SLOT_SIZE_16_BIT,
141        /** 24-bit slot */
142        I2S_SLOT_SIZE_24_BIT,
143        /** 32-bit slot */
144        I2S_SLOT_SIZE_32_BIT
145};
146
147/**
148 * DMA channels usage for I2S
149 */
150enum i2s_dma_usage {
151        /** Single DMA channel for all I2S channels */
152        I2S_DMA_USE_SINGLE_CHANNEL_FOR_ALL,
153        /** One DMA channel per data channel */
154        I2S_DMA_USE_ONE_CHANNEL_PER_DATA_CHANNEL
155};
156
157/**
158 * I2S data format, to extend mono data to 2 channels
159 */
160enum i2s_data_format {
161        /** Normal mode, keep data to its right channel */
162        I2S_DATA_FORMAT_STEREO,
163        /** Assume input is mono data for left channel, the data is duplicated to
164         *  right channel */
165        I2S_DATA_FORMAT_MONO
166};
167
168/**
169 * I2S data bit order
170 */
171enum i2s_bit_order {
172        /** Transfer Data Most Significant Bit first (Default for I2S protocol) */
173        I2S_BIT_ORDER_MSB_FIRST,
174        /** Transfer Data Least Significant Bit first */
175        I2S_BIT_ORDER_LSB_FIRST
176};
177
178/**
179 * I2S data bit padding
180 */
181enum i2s_bit_padding {
182        /** Padding with 0 */
183        I2S_BIT_PADDING_0,
184        /** Padding with 1 */
185        I2S_BIT_PADDING_1,
186        /** Padding with MSBit */
187        I2S_BIT_PADDING_MSB,
188        /** Padding with LSBit */
189        I2S_BIT_PADDING_LSB,
190};
191
192/**
193 * I2S data word adjust
194 */
195enum i2s_data_adjust {
196        /** Data is right adjusted in word */
197        I2S_DATA_ADJUST_RIGHT,
198        /** Data is left adjusted in word */
199        I2S_DATA_ADJUST_LEFT
200};
201
202/**
203 * I2S data word size
204 */
205enum i2s_data_size {
206        /** 32-bit */
207        I2S_DATA_SIZE_32BIT,
208        /** 24-bit */
209        I2S_DATA_SIZE_24BIT,
210        /** 20-bit */
211        I2S_DATA_SIZE_20BIT,
212        /** 18-bit */
213        I2S_DATA_SIZE_18BIT,
214        /** 16-bit */
215        I2S_DATA_SIZE_16BIT,
216        /** 16-bit compact stereo */
217        I2S_DATA_SIZE_16BIT_COMPACT,
218        /** 8-bit */
219        I2S_DATA_SIZE_8BIT,
220        /** 8-bit compact stereo */
221        I2S_DATA_SIZE_8BIT_COMPACT
222};
223
224/**
225 * I2S data slot adjust
226 */
227enum i2s_slot_adjust {
228        /** Data is right adjusted in slot */
229        I2S_SLOT_ADJUST_RIGHT,
230        /** Data is left adjusted in slot */
231        I2S_SLOT_ADJUST_LEFT
232};
233
234/**
235 * I2S data padding
236 */
237enum i2s_data_padding {
238        /** Padding 0 in case of under-run */
239        I2S_DATA_PADDING_0,
240        /** Padding last data in case of under-run */
241        I2S_DATA_PADDING_SAME_AS_LAST,
242        /** Padding last data in case of under-run
243         *  (abbr. \c I2S_DATA_PADDING_SAME_AS_LAST) */
244        I2S_DATA_PADDING_LAST = I2S_DATA_PADDING_SAME_AS_LAST,
245        /** Padding last data in case of under-run
246         *  (abbr. \c I2S_DATA_PADDING_SAME_AS_LAST) */
247        I2S_DATA_PADDING_SAME = I2S_DATA_PADDING_SAME_AS_LAST
248};
249
250/**
251 * I2S line default value when slot disabled
252 */
253enum i2s_line_default_state {
254        /** Output default value is 0 */
255        I2S_LINE_DEFAULT_0,
256        /** Output default value is 1 */
257        I2S_LINE_DEFAULT_1,
258        /** Output default value is high impedance */
259        I2S_LINE_DEFAULT_HIGH_IMPEDANCE = 3,
260        /** Output default value is high impedance
261         *  (abbr. \c I2S_LINE_DEFAULT_HIGH_IMPEDANCE) */
262        I2S_LINE_DEFAULT_HIZ = I2S_LINE_DEFAULT_HIGH_IMPEDANCE
263};
264
265/**
266 * I2S Serializer mode
267 */
268enum i2s_serializer_mode {
269        /** Serializer is used to receive data */
270        I2S_SERIALIZER_RECEIVE,
271        /** Serializer is used to transmit data */
272        I2S_SERIALIZER_TRANSMIT,
273        /** Serializer is used to receive PDM data on each clock edge */
274        I2S_SERIALIZER_PDM2
275};
276
277/**
278 * I2S clock unit selection
279 */
280enum i2s_clock_unit {
281        /** Clock Unit channel 0 */
282        I2S_CLOCK_UNIT_0,
283        /** Clock Unit channel 1 */
284        I2S_CLOCK_UNIT_1,
285        /** Number of Clock Unit channels */
286        I2S_CLOCK_UNIT_N
287};
288
289/**
290 * I2S Serializer selection
291 */
292enum i2s_serializer {
293        /** Serializer channel 0 */
294        I2S_SERIALIZER_0,
295        /** Serializer channel 1 */
296        I2S_SERIALIZER_1,
297        /** Number of Serializer channels */
298        I2S_SERIALIZER_N
299};
300
301
302#endif
303
304
305#endif
306
307#endif
Note: See TracBrowser for help on using the repository browser.