Skip to content

Commit b02b9c4

Browse files
Add PDM library for Arduino Nano RP2040 Connect (#213)
* Add PDM library for Arduino Nano RP2040 Connect * No PDM test in CI, only works on Arduino Nano RP2040
1 parent 6afcf58 commit b02b9c4

File tree

13 files changed

+1100
-0
lines changed

13 files changed

+1100
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
This example reads audio data from the on-board PDM microphones, and prints
3+
out the samples to the Serial console. The Serial Plotter built into the
4+
Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
5+
6+
Circuit:
7+
- Arduino Nano 33 BLE board, or
8+
- Arduino Nano RP2040 Connect, or
9+
- Arduino Portenta H7 board plus Portenta Vision Shield
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <PDM.h>
15+
16+
// default number of output channels
17+
static const char channels = 1;
18+
19+
// default PCM output frequency
20+
static const int frequency = 16000;
21+
22+
// Buffer to read samples into, each sample is 16-bits
23+
short sampleBuffer[512];
24+
25+
// Number of audio samples read
26+
volatile int samplesRead;
27+
28+
void setup() {
29+
Serial.begin(9600);
30+
while (!Serial);
31+
32+
// Configure the data receive callback
33+
PDM.onReceive(onPDMdata);
34+
35+
// Optionally set the gain
36+
// Defaults to 20 on the BLE Sense and -10 on the Portenta Vision Shield
37+
// PDM.setGain(30);
38+
39+
// Initialize PDM with:
40+
// - one channel (mono mode)
41+
// - a 16 kHz sample rate for the Arduino Nano 33 BLE Sense
42+
// - a 32 kHz or 64 kHz sample rate for the Arduino Portenta Vision Shield
43+
if (!PDM.begin(channels, frequency)) {
44+
Serial.println("Failed to start PDM!");
45+
while (1);
46+
}
47+
}
48+
49+
void loop() {
50+
// Wait for samples to be read
51+
if (samplesRead) {
52+
53+
// Print samples to the serial monitor or plotter
54+
for (int i = 0; i < samplesRead; i++) {
55+
if(channels == 2) {
56+
Serial.print("L:");
57+
Serial.print(sampleBuffer[i]);
58+
Serial.print(" R:");
59+
i++;
60+
}
61+
Serial.println(sampleBuffer[i]);
62+
}
63+
64+
// Clear the read count
65+
samplesRead = 0;
66+
}
67+
}
68+
69+
/**
70+
* Callback function to process the data from the PDM microphone.
71+
* NOTE: This callback is executed as part of an ISR.
72+
* Therefore using `Serial` to print messages inside this function isn't supported.
73+
* */
74+
void onPDMdata() {
75+
// Query the number of available bytes
76+
int bytesAvailable = PDM.available();
77+
78+
// Read into the sample buffer
79+
PDM.read(sampleBuffer, bytesAvailable);
80+
81+
// 16-bit, 2 bytes per sample
82+
samplesRead = bytesAvailable / 2;
83+
}

libraries/PDM/keywords.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#######################################
2+
# Syntax Coloring Map PDM
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
PDM KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
end KEYWORD2
16+
17+
available KEYWORD2
18+
read KEYWORD2
19+
20+
onReceive KEYWORD2
21+
22+
setGain KEYWORD2
23+
setBufferSize KEYWORD2
24+
25+
#######################################
26+
# Constants (LITERAL1)
27+
#######################################

libraries/PDM/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=PDM
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Enables the communication with devices that use the PDM Bus. Specific implementation for nRF52.
6+
paragraph=
7+
category=Communication
8+
url=
9+
architectures=rp2040

libraries/PDM/src/PDM.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright (c) 2019 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _PDM_H_INCLUDED
20+
#define _PDM_H_INCLUDED
21+
22+
#include <Arduino.h>
23+
//#include <pinDefinitions.h>
24+
25+
#include "utility/PDMDoubleBuffer.h"
26+
27+
class PDMClass
28+
{
29+
public:
30+
PDMClass(int dinPin, int clkPin, int pwrPin);
31+
virtual ~PDMClass();
32+
33+
int begin(int channels, int sampleRate);
34+
void end();
35+
36+
virtual int available();
37+
virtual int read(void* buffer, size_t size);
38+
39+
void onReceive(void(*)(void));
40+
41+
//PORTENTA_H7 min -12 max 51
42+
//NANO 33 BLE SENSe min 0 max 80
43+
void setGain(int gain);
44+
void setBufferSize(int bufferSize);
45+
size_t getBufferSize();
46+
47+
// private:
48+
void IrqHandler(bool halftranfer);
49+
50+
private:
51+
int _dinPin;
52+
int _clkPin;
53+
int _pwrPin;
54+
55+
int _channels;
56+
int _samplerate;
57+
58+
int _gain;
59+
int _init;
60+
61+
PDMDoubleBuffer _doubleBuffer;
62+
63+
void (*_onReceive)(void);
64+
};
65+
#ifdef PIN_PDM_DIN
66+
extern PDMClass PDM;
67+
#endif
68+
#endif

0 commit comments

Comments
 (0)