add digitalPinToPinName(P) (arduino/mbed has it) and the adapted PDM library
This commit is contained in:
1 parent
8628008fb5
commit
d3f3b34319
9 files changed
+575
-1
No files matched your search
@@ -114,11 +114,15 @@ void suspendLoop(void);
|
||||
#endif
|
||||
|
||||
#define digitalPinToBitMask(P) ( 1UL << ( g_ADigitalPinMap[P] < 32 ? g_ADigitalPinMap[P] : (g_ADigitalPinMap[P]-32) ) )
|
||||
|
||||
#define digitalPinToPinName(P) g_ADigitalPinMap[P]
|
||||
|
||||
//#define analogInPinToBit(P) ( )
|
||||
#define portOutputRegister(port) ( &(port->OUT) )
|
||||
#define portInputRegister(port) ( (volatile uint32_t*) &(port->IN) )
|
||||
#define portModeRegister(port) ( &(port->DIR) )
|
||||
#define digitalPinHasPWM(P) ( g_ADigitalPinMap[P] > 1 )
|
||||
|
||||
/*
|
||||
* digitalPinToTimer(..) is AVR-specific and is not defined for nRF52
|
||||
* architecture. If you need to check if a pin supports PWM you must
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
This example reads audio data from the on-board PDM microphones, and prints
|
||||
out the samples to the Serial console. The Serial Plotter built into the
|
||||
Arduino IDE can be used to plot the audio data (Tools -> Serial Plotter)
|
||||
|
||||
Please try with the Circuit Playground Bluefruit
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
#include <PDM.h>
|
||||
|
||||
// The default Circuit Playground Bluefruit pins
|
||||
// data pin, clock pin, power pin (-1 if not used)
|
||||
PDMClass PDM(33, 34, -1);
|
||||
|
||||
// buffer to read samples into, each sample is 16-bits
|
||||
short sampleBuffer[256];
|
||||
|
||||
// number of samples read
|
||||
volatile int samplesRead;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
// configure the data receive callback
|
||||
PDM.onReceive(onPDMdata);
|
||||
|
||||
// optionally set the gain, defaults to 20
|
||||
// PDM.setGain(30);
|
||||
|
||||
// initialize PDM with:
|
||||
// - one channel (mono mode)
|
||||
// - a 16 kHz sample rate
|
||||
if (!PDM.begin(1, 16000)) {
|
||||
Serial.println("Failed to start PDM!");
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// wait for samples to be read
|
||||
if (samplesRead) {
|
||||
|
||||
// print samples to the serial monitor or plotter
|
||||
for (int i = 0; i < samplesRead; i++) {
|
||||
Serial.println(sampleBuffer[i]);
|
||||
}
|
||||
|
||||
// clear the read count
|
||||
samplesRead = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void onPDMdata() {
|
||||
// query the number of bytes available
|
||||
int bytesAvailable = PDM.available();
|
||||
|
||||
// read into the sample buffer
|
||||
PDM.read(sampleBuffer, bytesAvailable);
|
||||
|
||||
// 16-bit, 2 bytes per sample
|
||||
samplesRead = bytesAvailable / 2;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map PDM
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
PDM KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
begin KEYWORD2
|
||||
end KEYWORD2
|
||||
|
||||
available KEYWORD2
|
||||
read KEYWORD2
|
||||
|
||||
onReceive KEYWORD2
|
||||
|
||||
setGain KEYWORD2
|
||||
setBufferSize KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
@@ -0,0 +1,9 @@
|
||||
name=nRF52840 PDM - Adafruit Fork
|
||||
version=1.0
|
||||
author=Arduino
|
||||
maintainer=Adafruit <support@adafruit.com>
|
||||
sentence=Enables the communication with devices that use the PDM Bus. Specific implementation for nRF52.
|
||||
paragraph=
|
||||
category=Communication
|
||||
url=
|
||||
architectures=*
|
||||
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
PDM.cpp - library to interface with nRF52840 PDM peripheral
|
||||
Part of Arduino - http://www.arduino.cc/
|
||||
|
||||
Copyright (c) 2019 Arduino SA
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General
|
||||
Public License along with this library; if not, write to the
|
||||
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "PDM.h"
|
||||
#include <hal/nrf_pdm.h>
|
||||
|
||||
#define DEFAULT_PDM_GAIN 20
|
||||
#define PDM_IRQ_PRIORITY 7
|
||||
|
||||
#define NRF_PDM_FREQ_1280K (nrf_pdm_freq_t)(0x0A000000UL) ///< PDM_CLK= 1.280 MHz (32 MHz / 25) => Fs= 20000 Hz
|
||||
#define NRF_PDM_FREQ_2000K (nrf_pdm_freq_t)(0x10000000UL) ///< PDM_CLK= 2.000 MHz (32 MHz / 16) => Fs= 31250 Hz
|
||||
#define NRF_PDM_FREQ_2667K (nrf_pdm_freq_t)(0x15000000UL) ///< PDM_CLK= 2.667 MHz (32 MHz / 12) => Fs= 41667 Hz
|
||||
#define NRF_PDM_FREQ_3200K (nrf_pdm_freq_t)(0x19000000UL) ///< PDM_CLK= 3.200 MHz (32 MHz / 10) => Fs= 50000 Hz
|
||||
#define NRF_PDM_FREQ_4000K (nrf_pdm_freq_t)(0x20000000UL) ///< PDM_CLK= 4.000 MHz (32 MHz / 8) => Fs= 62500 Hz
|
||||
|
||||
PDMClass::PDMClass(int dinPin, int clkPin, int pwrPin) :
|
||||
_dinPin(dinPin),
|
||||
_clkPin(clkPin),
|
||||
_pwrPin(pwrPin),
|
||||
_onReceive(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
PDMClass::~PDMClass()
|
||||
{
|
||||
}
|
||||
|
||||
int PDMClass::begin(int channels, long sampleRate)
|
||||
{
|
||||
_channels = channels;
|
||||
|
||||
/*
|
||||
// configure the sample rate and channels
|
||||
switch (sampleRate) {
|
||||
case 16000:
|
||||
nrf_pdm_clock_set(NRF_PDM_FREQ_1032K);
|
||||
break;
|
||||
case 41667:
|
||||
nrf_pdm_clock_set(NRF_PDM_FREQ_2667K);
|
||||
break;
|
||||
default:
|
||||
return 0; // unsupported
|
||||
}
|
||||
*/
|
||||
|
||||
switch (channels) {
|
||||
case 2:
|
||||
nrf_pdm_mode_set(NRF_PDM_MODE_STEREO, NRF_PDM_EDGE_LEFTFALLING);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
nrf_pdm_mode_set(NRF_PDM_MODE_MONO, NRF_PDM_EDGE_LEFTFALLING);
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0; // unsupported
|
||||
}
|
||||
setGain(DEFAULT_PDM_GAIN);
|
||||
|
||||
// configure the I/O and mux
|
||||
pinMode(_clkPin, OUTPUT);
|
||||
digitalWrite(_clkPin, LOW);
|
||||
|
||||
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
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_STARTED);
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_END);
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_STOPPED);
|
||||
nrf_pdm_int_enable(NRF_PDM_INT_STARTED | NRF_PDM_INT_STOPPED);
|
||||
|
||||
if (_pwrPin > -1) {
|
||||
// power the mic on
|
||||
pinMode(_pwrPin, OUTPUT);
|
||||
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));
|
||||
// _doubleBuffer.swap();
|
||||
|
||||
// enable and trigger start task
|
||||
nrf_pdm_enable();
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_STARTED);
|
||||
nrf_pdm_task_trigger(NRF_PDM_TASK_START);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void PDMClass::end()
|
||||
{
|
||||
// disable PDM and IRQ
|
||||
nrf_pdm_disable();
|
||||
|
||||
NVIC_DisableIRQ(PDM_IRQn);
|
||||
|
||||
if (_pwrPin > -1) {
|
||||
// power the mic off
|
||||
digitalWrite(_pwrPin, LOW);
|
||||
pinMode(_pwrPin, INPUT);
|
||||
}
|
||||
|
||||
// unconfigure the I/O and un-mux
|
||||
nrf_pdm_psel_disconnect();
|
||||
|
||||
pinMode(_clkPin, INPUT);
|
||||
}
|
||||
|
||||
int PDMClass::available()
|
||||
{
|
||||
NVIC_DisableIRQ(PDM_IRQn);
|
||||
|
||||
size_t avail = _doubleBuffer.available();
|
||||
|
||||
//NVIC_EnableIRQ(PDM_IRQn);
|
||||
|
||||
return avail;
|
||||
}
|
||||
|
||||
int PDMClass::read(void* buffer, size_t size)
|
||||
{
|
||||
NVIC_DisableIRQ(PDM_IRQn);
|
||||
|
||||
int read = _doubleBuffer.read(buffer, size);
|
||||
|
||||
//NVIC_EnableIRQ(PDM_IRQn);
|
||||
|
||||
return read;
|
||||
}
|
||||
|
||||
void PDMClass::onReceive(void(*function)(void))
|
||||
{
|
||||
_onReceive = function;
|
||||
}
|
||||
|
||||
void PDMClass::setGain(int gain)
|
||||
{
|
||||
nrf_pdm_gain_set(gain, gain);
|
||||
}
|
||||
|
||||
void PDMClass::setBufferSize(int bufferSize)
|
||||
{
|
||||
_doubleBuffer.setSize(bufferSize);
|
||||
}
|
||||
|
||||
void PDMClass::IrqHandler()
|
||||
{
|
||||
if (nrf_pdm_event_check(NRF_PDM_EVENT_STARTED)) {
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_STARTED);
|
||||
|
||||
if (_doubleBuffer.available() == 0) {
|
||||
// switch to the next buffer
|
||||
nrf_pdm_buffer_set((uint32_t*)_doubleBuffer.data(), _doubleBuffer.availableForWrite() / (sizeof(int16_t) * _channels));
|
||||
|
||||
// make the current one available for reading
|
||||
_doubleBuffer.swap(_doubleBuffer.availableForWrite());
|
||||
|
||||
// call receive callback if provided
|
||||
if (_onReceive) {
|
||||
_onReceive();
|
||||
}
|
||||
} else {
|
||||
// buffer overflow, stop
|
||||
nrf_pdm_disable();
|
||||
}
|
||||
} else if (nrf_pdm_event_check(NRF_PDM_EVENT_STOPPED)) {
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_STOPPED);
|
||||
} else if (nrf_pdm_event_check(NRF_PDM_EVENT_END)) {
|
||||
nrf_pdm_event_clear(NRF_PDM_EVENT_END);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void PDM_IRQHandler(void);
|
||||
}
|
||||
|
||||
void PDM_IRQHandler(void)
|
||||
{
|
||||
PDM.IrqHandler();
|
||||
}
|
||||
|
||||
extern PDMClass PDM;
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
Copyright (c) 2019 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef _PDM_H_INCLUDED
|
||||
#define _PDM_H_INCLUDED
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if !defined(ARDUINO_ARCH_ADAFRUIT_NRF52_ARDUINO)
|
||||
#error "This library targets only Adafruit NRF52840 boards"
|
||||
#endif
|
||||
|
||||
#include "utility/PDMDoubleBuffer.h"
|
||||
|
||||
class PDMClass
|
||||
{
|
||||
public:
|
||||
PDMClass(int dataPin, int clkPin, int pwrPin);
|
||||
virtual ~PDMClass();
|
||||
|
||||
int begin(int channels, long sampleRate);
|
||||
void end();
|
||||
|
||||
virtual int available();
|
||||
virtual int read(void* buffer, size_t size);
|
||||
|
||||
void onReceive(void(*)(void));
|
||||
|
||||
void setGain(int gain);
|
||||
void setBufferSize(int bufferSize);
|
||||
|
||||
// private:
|
||||
void IrqHandler();
|
||||
|
||||
private:
|
||||
int _dinPin;
|
||||
int _clkPin;
|
||||
int _pwrPin;
|
||||
|
||||
int _channels;
|
||||
|
||||
PDMDoubleBuffer _doubleBuffer;
|
||||
|
||||
void (*_onReceive)(void);
|
||||
};
|
||||
|
||||
extern PDMClass PDM;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "PDMDoubleBuffer.h"
|
||||
|
||||
PDMDoubleBuffer::PDMDoubleBuffer() :
|
||||
_size(DEFAULT_PDM_BUFFER_SIZE)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
PDMDoubleBuffer::~PDMDoubleBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
void PDMDoubleBuffer::setSize(int size)
|
||||
{
|
||||
_size = size;
|
||||
}
|
||||
|
||||
void PDMDoubleBuffer::reset()
|
||||
{
|
||||
_buffer[0] = (uint8_t*)realloc(_buffer[0], _size);
|
||||
_buffer[1] = (uint8_t*)realloc(_buffer[1], _size);
|
||||
|
||||
memset(_buffer[0], 0x00, _size);
|
||||
memset(_buffer[1], 0x00, _size);
|
||||
|
||||
_index = 0;
|
||||
_length[0] = 0;
|
||||
_length[1] = 0;
|
||||
_readOffset[0] = 0;
|
||||
_readOffset[1] = 0;
|
||||
}
|
||||
|
||||
size_t PDMDoubleBuffer::availableForWrite()
|
||||
{
|
||||
return (_size - (_length[_index] - _readOffset[_index]));
|
||||
}
|
||||
|
||||
size_t PDMDoubleBuffer::write(const void *buffer, size_t size)
|
||||
{
|
||||
size_t space = availableForWrite();
|
||||
|
||||
if (size > space) {
|
||||
size = space;
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(&_buffer[_index][_length[_index]], buffer, size);
|
||||
|
||||
_length[_index] += size;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t PDMDoubleBuffer::read(void *buffer, size_t size)
|
||||
{
|
||||
size_t avail = available();
|
||||
|
||||
if (size > avail) {
|
||||
size = avail;
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(buffer, &_buffer[_index][_readOffset[_index]], size);
|
||||
_readOffset[_index] += size;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t PDMDoubleBuffer::peek(void *buffer, size_t size)
|
||||
{
|
||||
size_t avail = available();
|
||||
|
||||
if (size > avail) {
|
||||
size = avail;
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(buffer, &_buffer[_index][_readOffset[_index]], size);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
void* PDMDoubleBuffer::data()
|
||||
{
|
||||
return (void*)_buffer[_index];
|
||||
}
|
||||
|
||||
size_t PDMDoubleBuffer::available()
|
||||
{
|
||||
return _length[_index] - _readOffset[_index];
|
||||
}
|
||||
|
||||
void PDMDoubleBuffer::swap(int length)
|
||||
{
|
||||
if (_index == 0) {
|
||||
_index = 1;
|
||||
} else {
|
||||
_index = 0;
|
||||
}
|
||||
|
||||
_length[_index] = length;
|
||||
_readOffset[_index] = 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Copyright (c) 2016 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef _PDM_DOUBLE_BUFFER_H_INCLUDED
|
||||
#define _PDM_DOUBLE_BUFFER_H_INCLUDED
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define DEFAULT_PDM_BUFFER_SIZE 512
|
||||
|
||||
class PDMDoubleBuffer
|
||||
{
|
||||
public:
|
||||
PDMDoubleBuffer();
|
||||
virtual ~PDMDoubleBuffer();
|
||||
|
||||
void setSize(int size);
|
||||
|
||||
void reset();
|
||||
|
||||
size_t availableForWrite();
|
||||
size_t write(const void *buffer, size_t size);
|
||||
size_t read(void *buffer, size_t size);
|
||||
size_t peek(void *buffer, size_t size);
|
||||
void* data();
|
||||
size_t available();
|
||||
void swap(int length = 0);
|
||||
|
||||
private:
|
||||
uint8_t* _buffer[2];
|
||||
int _size;
|
||||
volatile int _length[2];
|
||||
volatile int _readOffset[2];
|
||||
volatile int _index;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -54,10 +54,14 @@ extern "C"
|
||||
#define LED_STATE_ON 1 // State when LED is litted
|
||||
|
||||
// Buttons
|
||||
|
||||
#define PIN_BUTTON1 (4)
|
||||
#define PIN_BUTTON2 (5)
|
||||
|
||||
// Microphone
|
||||
#define PIN_PDM_DIN 33
|
||||
#define PIN_PDM_CLK 34
|
||||
#define PIN_PDM_PWR -1 // not used
|
||||
|
||||
/*
|
||||
* Analog pins
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user