Merge pull request #409 from adafruit/fix-pdm

fix pdm issue with #352
This commit is contained in:
Limor "Ladyada" Fried authored and GitHub committed 2020-01-09 22:06:57 -05:00
commit 85a9711740
4 files changed
+56 -27

No files matched your search

@@ -10,10 +10,6 @@
#include <PDM.h>
// The default Circuit Playground Bluefruit pins
// data pin, clock pin, power pin (-1 if not used)
PDMClass PDM(24, 25, -1);
// buffer to read samples into, each sample is 16-bits
short sampleBuffer[256];
@@ -41,8 +37,6 @@ void setup() {
void loop() {
// wait for samples to be read
PDM.IrqHandler();
if (samplesRead) {
// print samples to the serial monitor or plotter
for (int i = 0; i < samplesRead; i++) {
@@ -62,4 +56,5 @@ void onPDMdata() {
// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
}
+41 -17
View File
@@ -44,15 +44,30 @@ PDMClass::~PDMClass()
{
}
void PDMClass::setPins(int dataPin, int clkPin, int pwrPin)
{
_dinPin = dataPin;
_clkPin = clkPin;
_pwrPin = pwrPin;
}
int PDMClass::begin(int channels, long sampleRate)
{
_channels = channels;
/*
// Enable high frequency oscillator if not already enabled
if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
NRF_CLOCK->TASKS_HFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { }
}
// configure the sample rate and channels
switch (sampleRate) {
case 16000:
nrf_pdm_clock_set(NRF_PDM_FREQ_1032K);
#ifndef NRF52832_XXAA
NRF_PDM->RATIO = ((PDM_RATIO_RATIO_Ratio80 << PDM_RATIO_RATIO_Pos) & PDM_RATIO_RATIO_Msk);
#endif
nrf_pdm_clock_set(NRF_PDM_FREQ_1280K);
break;
case 41667:
nrf_pdm_clock_set(NRF_PDM_FREQ_2667K);
@@ -60,7 +75,6 @@ int PDMClass::begin(int channels, long sampleRate)
default:
return 0; // unsupported
}
*/
switch (channels) {
case 2:
@@ -74,6 +88,7 @@ int PDMClass::begin(int channels, long sampleRate)
default:
return 0; // unsupported
}
setGain(DEFAULT_PDM_GAIN);
// configure the I/O and mux
@@ -82,8 +97,6 @@ int PDMClass::begin(int channels, long sampleRate)
pinMode(_dinPin, INPUT);
Serial.printf("Clock P %d Data P %d\n", digitalPinToPinName(_clkPin), digitalPinToPinName(_dinPin));
nrf_pdm_psel_connect(digitalPinToPinName(_clkPin), digitalPinToPinName(_dinPin));
// clear events and enable PDM interrupts
@@ -98,14 +111,13 @@ int PDMClass::begin(int channels, long sampleRate)
digitalWrite(_pwrPin, HIGH);
}
// clear the buffer
_doubleBuffer.reset();
// set the PDM IRQ priority and enable
NVIC_SetPriority(PDM_IRQn, PDM_IRQ_PRIORITY);
NVIC_ClearPendingIRQ(PDM_IRQn);
NVIC_EnableIRQ(PDM_IRQn);
// set the buffer for transfer
// nrf_pdm_buffer_set((uint32_t*)_doubleBuffer.data(), _doubleBuffer.availableForWrite() / (sizeof(int16_t) * _channels));
@@ -116,7 +128,6 @@ int PDMClass::begin(int channels, long sampleRate)
nrf_pdm_event_clear(NRF_PDM_EVENT_STARTED);
nrf_pdm_task_trigger(NRF_PDM_TASK_START);
return 1;
}
@@ -133,6 +144,8 @@ void PDMClass::end()
pinMode(_pwrPin, INPUT);
}
// Don't disable high frequency oscillator since it could be in use by RADIO
// unconfigure the I/O and un-mux
nrf_pdm_psel_disconnect();
@@ -145,7 +158,7 @@ int PDMClass::available()
size_t avail = _doubleBuffer.available();
//NVIC_EnableIRQ(PDM_IRQn);
NVIC_EnableIRQ(PDM_IRQn);
return avail;
}
@@ -156,7 +169,7 @@ int PDMClass::read(void* buffer, size_t size)
int read = _doubleBuffer.read(buffer, size);
//NVIC_EnableIRQ(PDM_IRQn);
NVIC_EnableIRQ(PDM_IRQn);
return read;
}
@@ -203,13 +216,24 @@ void PDMClass::IrqHandler()
}
}
extern "C" {
void PDM_IRQHandler(void);
}
void PDM_IRQHandler(void)
extern "C"
{
PDM.IrqHandler();
void PDM_IRQHandler(void)
{
PDM.IrqHandler();
}
}
extern PDMClass PDM;
#ifndef PIN_PDM_DIN
#define PIN_PDM_DIN -1
#endif
#ifndef PIN_PDM_CLK
#define PIN_PDM_CLK -1
#endif
#ifndef PIN_PDM_PWR
#define PIN_PDM_PWR -1
#endif
PDMClass PDM(PIN_PDM_DIN, PIN_PDM_CLK, PIN_PDM_PWR);
+1
View File
@@ -33,6 +33,7 @@ public:
PDMClass(int dataPin, int clkPin, int pwrPin);
virtual ~PDMClass();
void setPins(int dataPin, int clkPin, int pwrPin);
int begin(int channels, long sampleRate);
void end();
+12 -3
View File
@@ -54,8 +54,8 @@ extern "C"
#define LED_STATE_ON 1 // State when LED is litted
// Buttons
#define PIN_BUTTON1 (4)
#define PIN_BUTTON2 (5)
#define PIN_BUTTON1 (4)
#define PIN_BUTTON2 (5)
// Microphone
#define PIN_PDM_DIN 24
@@ -120,7 +120,9 @@ static const uint8_t SCK = PIN_SPI_SCK ;
#define PIN_WIRE1_SDA (28)
#define PIN_WIRE1_SCL (26)
// QSPI Pins
/*
* QSPI Interfaces
*/
#define PIN_QSPI_SCK 29
#define PIN_QSPI_CS 30
#define PIN_QSPI_IO0 31
@@ -134,6 +136,13 @@ static const uint8_t SCK = PIN_SPI_SCK ;
#define USB_MSC_BLOCK_SIZE 512
#define USB_MSC_BLOCK_COUNT ((2*1024*1024) / USB_MSC_BLOCK_SIZE)
/*
* PDM Interfaces
*/
#define PIN_PDM_DIN 24
#define PIN_PDM_CLK 25
#define PIN_PDM_PWR -1 // not used
#ifdef __cplusplus
}
#endif