Use ZeroPDM library, disable HeatSensor.cpp by default
This commit is contained in:
parent
b97ea1d4cf
commit
7fd0290198
3 changed files with 19 additions and 186 deletions
|
|
@ -1,6 +1,8 @@
|
|||
#if 0 // Change to 1 to enable this code (referenced by user_watch.cpp)
|
||||
// CORRESPONDING LINE IN user_watch.cpp MUST ALSO BE ENABLED!
|
||||
|
||||
/* Read the IR sensor and try to figure out where the heat is located.
|
||||
*/
|
||||
|
||||
#include "HeatSensor.h"
|
||||
|
||||
#include <Wire.h>
|
||||
|
|
@ -107,4 +109,5 @@ void loop() {
|
|||
|
||||
delay(200);
|
||||
}
|
||||
*/
|
||||
*/
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "globals.h"
|
||||
#include <SPI.h>
|
||||
#include <Adafruit_ZeroPDMSPI.h>
|
||||
|
||||
#define MIN_PITCH_HZ 65
|
||||
#define MAX_PITCH_HZ 1600
|
||||
|
|
@ -16,7 +17,6 @@ static float actualPlaybackRate;
|
|||
// PDM mic allows 1.0 to 3.25 MHz max clock (2.4 typical).
|
||||
// SPI native max is is 24 MHz, so available speeds are 12, 6, 3 MHz.
|
||||
#define SPI_BITRATE 3000000
|
||||
static SPISettings settings(SPI_BITRATE, LSBFIRST, SPI_MODE0);
|
||||
// 3 MHz / 32 bits = 93,750 Hz interrupt frequency
|
||||
// 2 interrupts/sample = 46,875 Hz audio sample rate
|
||||
const float sampleRate = (float)SPI_BITRATE / 64.0;
|
||||
|
|
@ -26,29 +26,10 @@ const float sampleRate = (float)SPI_BITRATE / 64.0;
|
|||
// Although SPI lib now has an option to get an SPI object's SERCOM number
|
||||
// at run time, the interrupt handler MUST be declared at compile time...
|
||||
// so it's necessary to know the SERCOM # ahead of time anyway, oh well.
|
||||
#define PDM_SERCOM SERCOM3 // PDM mic SPI SERCOM on MONSTER M4SK
|
||||
#define PDM_SPI SPI2 // PDM mic SPI peripheral
|
||||
#define PDM_SERCOM_HANDLER SERCOM3_0_Handler
|
||||
#define PDM_SERCOM_IRQn SERCOM3_0_IRQn // _0_IRQn is DRE interrupt
|
||||
|
||||
static Sercom *sercom;
|
||||
static volatile uint32_t *dataReg;
|
||||
|
||||
Sercom * const sercomList[] = {
|
||||
SERCOM0, SERCOM1, SERCOM2, SERCOM3,
|
||||
#if defined(SERCOM4)
|
||||
SERCOM4,
|
||||
#endif
|
||||
#if defined(SERCOM5)
|
||||
SERCOM5,
|
||||
#endif
|
||||
#if defined(SERCOM6)
|
||||
SERCOM6,
|
||||
#endif
|
||||
#if defined(SERCOM7)
|
||||
SERCOM7,
|
||||
#endif
|
||||
};
|
||||
Adafruit_ZeroPDMSPI pdmspi(&PDM_SPI);
|
||||
|
||||
static float playbackRate = sampleRate;
|
||||
static uint16_t *recBuf = NULL;
|
||||
|
|
@ -65,16 +46,6 @@ volatile uint16_t voiceLastReading = 32768;
|
|||
volatile uint16_t voiceMin = 32768;
|
||||
volatile uint16_t voiceMax = 32768;
|
||||
|
||||
#define DC_PERIOD 4096 // Recalculate DC offset this many samplings
|
||||
// DC_PERIOD does NOT need to be a power of 2, but might save a few cycles.
|
||||
// PDM rate is 46875, so 4096 = 11.44 times/sec
|
||||
static uint16_t dcCounter = 0; // Rolls over every DC_PERIOD samples
|
||||
static uint32_t dcSum = 0; // Accumulates DC_PERIOD samples
|
||||
static uint16_t dcOffsetPrior = 32768; // DC offset interpolates linearly
|
||||
static uint16_t dcOffsetNext = 32768; // between these two values
|
||||
|
||||
static uint16_t micGain = 256; // 1:1
|
||||
|
||||
#define MOD_MIN 20 // Lowest supported modulation frequency (lower = more RAM use)
|
||||
static uint8_t modWave = 0; // Modulation wave type (none, sine, square, tri, saw)
|
||||
static uint8_t *modBuf = NULL; // Modulation waveform buffer
|
||||
|
|
@ -119,37 +90,9 @@ bool voiceSetup(bool modEnable) {
|
|||
// If malloc fails, program will continue without modulation
|
||||
}
|
||||
|
||||
// Set up PDM microphone input -------------------------------------------
|
||||
|
||||
PDM_SPI.begin();
|
||||
PDM_SPI.beginTransaction(settings); // this SPI transaction is left open
|
||||
sercom = sercomList[PDM_SPI.getSercomIndex()];
|
||||
dataReg = PDM_SPI.getDataRegister();
|
||||
|
||||
// Enabling 32-bit SPI must be done AFTER SPI.begin() which
|
||||
// resets registers. But SPI.CTRLC (where 32-bit mode is set) is
|
||||
// enable-protected, so peripheral must be disabled temporarily...
|
||||
sercom->SPI.CTRLA.bit.ENABLE = 0; // Disable SPI
|
||||
while(sercom->SPI.SYNCBUSY.bit.ENABLE); // Wait for disable
|
||||
sercom->SPI.CTRLC.bit.DATA32B = 1; // Enable 32-bit mode
|
||||
sercom->SPI.CTRLA.bit.ENABLE = 1; // Re-enable SPI
|
||||
while(sercom->SPI.SYNCBUSY.bit.ENABLE); // Wait for enable
|
||||
// 4-byte word length is implicit in 32-bit mode,
|
||||
// no need to set up LENGTH register.
|
||||
|
||||
sercom->SPI.INTENSET.bit.DRE = 1; // Data-register-empty interrupt
|
||||
NVIC_DisableIRQ(PDM_SERCOM_IRQn);
|
||||
NVIC_ClearPendingIRQ(PDM_SERCOM_IRQn);
|
||||
NVIC_SetPriority(PDM_SERCOM_IRQn, 0); // Top priority
|
||||
NVIC_EnableIRQ(PDM_SERCOM_IRQn);
|
||||
|
||||
sercom->SPI.DATA.bit.DATA = 0; // Kick off SPI free-run
|
||||
|
||||
// Set up analog output & timer ------------------------------------------
|
||||
|
||||
analogWriteResolution(12);
|
||||
|
||||
voicePitch(1.0); // Set timer interval
|
||||
pdmspi.begin(sampleRate); // Set up PDM microphone
|
||||
analogWriteResolution(12); // Set up analog output
|
||||
voicePitch(1.0); // Set timer interval
|
||||
|
||||
return true; // Success
|
||||
}
|
||||
|
|
@ -180,9 +123,7 @@ float voicePitch(float p) {
|
|||
// SET GAIN ----------------------------------------------------------------
|
||||
|
||||
void voiceGain(float g) {
|
||||
if(g >= (65535.0/256.0)) micGain = 65535;
|
||||
else if(g < 0.0) micGain = 0;
|
||||
else micGain = (uint16_t)(g * 256.0 + 0.5);
|
||||
pdmspi.setMicGain(g); // Handles its own clipping
|
||||
}
|
||||
|
||||
// SET MODULATION ----------------------------------------------------------
|
||||
|
|
@ -226,119 +167,9 @@ void voiceMod(uint32_t freq, uint8_t waveform) {
|
|||
|
||||
// INTERRUPT HANDLERS ------------------------------------------------------
|
||||
|
||||
static uint16_t const sincfilter[64] = { 0, 2, 9, 21, 39, 63, 94, 132, 179, 236, 302, 379, 467, 565, 674, 792, 920, 1055, 1196, 1341, 1487, 1633, 1776, 1913, 2042, 2159, 2263, 2352, 2422, 2474, 2506, 2516, 2506, 2474, 2422, 2352, 2263, 2159, 2042, 1913, 1776, 1633, 1487, 1341, 1196, 1055, 920, 792, 674, 565, 467, 379, 302, 236, 179, 132, 94, 63, 39, 21, 9, 2, 0, 0 };
|
||||
|
||||
void PDM_SERCOM_HANDLER(void) {
|
||||
static bool evenWord = 1; // Alternates 0/1 with each interrupt call
|
||||
static uint32_t sumTemp = 0; // Temp. value used across 2 interrupt calls
|
||||
// Shenanigans: SPI data read/write are shadowed...even though it appears
|
||||
// the same register here, it's legit to write new MOSI value before
|
||||
// reading the received MISO value from the same location. This helps
|
||||
// avoid a gap between words...provides a steady stream of bits.
|
||||
*dataReg = 0; // Write clears DRE flag, starts next xfer
|
||||
uint32_t sample = *dataReg; // Read last-received word
|
||||
|
||||
uint32_t sum = 0; // local var = register = faster than sumTemp
|
||||
if(evenWord) { // Even-numbered 32-bit word...
|
||||
// At default speed and optimization settings (120 MHz -Os), the PDM-
|
||||
// servicing interrupt consumes about 12.5% of CPU time. Though this
|
||||
// code looks bulky, it's actually reasonably efficient (sincfilter[] is
|
||||
// const, so these compile down to constants, there is no array lookup,
|
||||
// any any zero-value element refs will be removed by the compiler).
|
||||
// Tested MANY methods and this was hard to beat. One managed just under
|
||||
// 10% load, but required 4KB of tables...not worth it for small boost.
|
||||
// Can get an easy boost with overclock and optimizer tweaks.
|
||||
if(sample & 0x00000001) sum += sincfilter[ 0];
|
||||
if(sample & 0x00000002) sum += sincfilter[ 1];
|
||||
if(sample & 0x00000004) sum += sincfilter[ 2];
|
||||
if(sample & 0x00000008) sum += sincfilter[ 3];
|
||||
if(sample & 0x00000010) sum += sincfilter[ 4];
|
||||
if(sample & 0x00000020) sum += sincfilter[ 5];
|
||||
if(sample & 0x00000040) sum += sincfilter[ 6];
|
||||
if(sample & 0x00000080) sum += sincfilter[ 7];
|
||||
if(sample & 0x00000100) sum += sincfilter[ 8];
|
||||
if(sample & 0x00000200) sum += sincfilter[ 9];
|
||||
if(sample & 0x00000400) sum += sincfilter[10];
|
||||
if(sample & 0x00000800) sum += sincfilter[11];
|
||||
if(sample & 0x00001000) sum += sincfilter[12];
|
||||
if(sample & 0x00002000) sum += sincfilter[13];
|
||||
if(sample & 0x00004000) sum += sincfilter[14];
|
||||
if(sample & 0x00008000) sum += sincfilter[15];
|
||||
if(sample & 0x00010000) sum += sincfilter[16];
|
||||
if(sample & 0x00020000) sum += sincfilter[17];
|
||||
if(sample & 0x00040000) sum += sincfilter[18];
|
||||
if(sample & 0x00080000) sum += sincfilter[19];
|
||||
if(sample & 0x00100000) sum += sincfilter[20];
|
||||
if(sample & 0x00200000) sum += sincfilter[21];
|
||||
if(sample & 0x00400000) sum += sincfilter[22];
|
||||
if(sample & 0x00800000) sum += sincfilter[23];
|
||||
if(sample & 0x01000000) sum += sincfilter[24];
|
||||
if(sample & 0x02000000) sum += sincfilter[25];
|
||||
if(sample & 0x04000000) sum += sincfilter[26];
|
||||
if(sample & 0x08000000) sum += sincfilter[27];
|
||||
if(sample & 0x10000000) sum += sincfilter[28];
|
||||
if(sample & 0x20000000) sum += sincfilter[29];
|
||||
if(sample & 0x40000000) sum += sincfilter[30];
|
||||
if(sample & 0x80000000) sum += sincfilter[31];
|
||||
sumTemp = sum; // Copy register to static var for next call
|
||||
} else {
|
||||
if(sample & 0x00000001) sum += sincfilter[32];
|
||||
if(sample & 0x00000002) sum += sincfilter[33];
|
||||
if(sample & 0x00000004) sum += sincfilter[34];
|
||||
if(sample & 0x00000008) sum += sincfilter[35];
|
||||
if(sample & 0x00000010) sum += sincfilter[36];
|
||||
if(sample & 0x00000020) sum += sincfilter[37];
|
||||
if(sample & 0x00000040) sum += sincfilter[38];
|
||||
if(sample & 0x00000080) sum += sincfilter[39];
|
||||
if(sample & 0x00000100) sum += sincfilter[40];
|
||||
if(sample & 0x00000200) sum += sincfilter[41];
|
||||
if(sample & 0x00000400) sum += sincfilter[42];
|
||||
if(sample & 0x00000800) sum += sincfilter[43];
|
||||
if(sample & 0x00001000) sum += sincfilter[44];
|
||||
if(sample & 0x00002000) sum += sincfilter[45];
|
||||
if(sample & 0x00004000) sum += sincfilter[46];
|
||||
if(sample & 0x00008000) sum += sincfilter[47];
|
||||
if(sample & 0x00010000) sum += sincfilter[48];
|
||||
if(sample & 0x00020000) sum += sincfilter[49];
|
||||
if(sample & 0x00040000) sum += sincfilter[50];
|
||||
if(sample & 0x00080000) sum += sincfilter[51];
|
||||
if(sample & 0x00100000) sum += sincfilter[52];
|
||||
if(sample & 0x00200000) sum += sincfilter[53];
|
||||
if(sample & 0x00400000) sum += sincfilter[54];
|
||||
if(sample & 0x00800000) sum += sincfilter[55];
|
||||
if(sample & 0x01000000) sum += sincfilter[56];
|
||||
if(sample & 0x02000000) sum += sincfilter[57];
|
||||
if(sample & 0x04000000) sum += sincfilter[58];
|
||||
if(sample & 0x08000000) sum += sincfilter[59];
|
||||
if(sample & 0x10000000) sum += sincfilter[60];
|
||||
if(sample & 0x20000000) sum += sincfilter[61];
|
||||
if(sample & 0x40000000) sum += sincfilter[62];
|
||||
if(sample & 0x80000000) sum += sincfilter[63];
|
||||
sum += sumTemp; // Add static var from last call
|
||||
|
||||
// 'sum' is new raw audio value -- process it --------------------------
|
||||
|
||||
uint16_t dcOffset;
|
||||
|
||||
dcSum += sum; // Accumulate long-term average for DC offset correction
|
||||
if(++dcCounter < DC_PERIOD) {
|
||||
// Interpolate between dcOffsetPrior and dcOffsetNext
|
||||
dcOffset = dcOffsetPrior + (dcOffsetNext - dcOffsetPrior) * dcCounter / DC_PERIOD;
|
||||
} else {
|
||||
// End of period reached, move 'next' to 'previous,' calc new 'next' from avg
|
||||
dcOffsetPrior = dcOffset = dcOffsetNext;
|
||||
dcOffsetNext = dcSum / DC_PERIOD;
|
||||
dcCounter = dcSum = 0;
|
||||
}
|
||||
|
||||
// Adjust raw reading by DC offset to center (ish) it, scale by mic gain
|
||||
int32_t adjusted = ((int32_t)sum - dcOffset) * micGain / 256;
|
||||
|
||||
// Go back to uint16_t space and clip to 16-bit range
|
||||
adjusted += 32768;
|
||||
if(adjusted > 65535) adjusted = 65535;
|
||||
else if(adjusted < 0) adjusted = 0;
|
||||
|
||||
uint16_t micReading = 0;
|
||||
if(pdmspi.decimateFilterWord(&micReading, true)) {
|
||||
// So, the theory is, in the future some basic pitch detection could be
|
||||
// added right about here, which could be used to improve the seam
|
||||
// transitions in the playback interrupt (and possibly other things,
|
||||
|
|
@ -352,7 +183,7 @@ void PDM_SERCOM_HANDLER(void) {
|
|||
// project" code here, but it's pulled out for now for the sake of
|
||||
// getting something not-broken in folks' hands in a sensible timeframe.
|
||||
if(++recIndex >= recBufSize) recIndex = 0;
|
||||
recBuf[recIndex] = adjusted;
|
||||
recBuf[recIndex] = micReading;
|
||||
|
||||
// Outside code can use the value of voiceLastReading if you want to
|
||||
// do an approximate live waveform display, or dynamic gain adjustment
|
||||
|
|
@ -360,15 +191,14 @@ void PDM_SERCOM_HANDLER(void) {
|
|||
// sample in the recording buffer one-by-one sequentially...it's just
|
||||
// the last thing that was stored prior to whatever time you polled it,
|
||||
// but may still have some uses.
|
||||
voiceLastReading = adjusted;
|
||||
voiceLastReading = micReading;
|
||||
|
||||
// Similarly, user code can extern these variables and monitor the
|
||||
// peak-to-peak range. They are never reset in the voice code itself,
|
||||
// it's the duty of the user code to reset both to 32768 periodically.
|
||||
if(adjusted < voiceMin) voiceMin = adjusted;
|
||||
else if(adjusted > voiceMax) voiceMax = adjusted;
|
||||
if(micReading < voiceMin) voiceMin = micReading;
|
||||
else if(micReading > voiceMax) voiceMax = micReading;
|
||||
}
|
||||
evenWord ^= 1;
|
||||
}
|
||||
|
||||
static void voiceOutCallback(void) {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
#if 0 // Change to 0 to disable this code (must enable ONE user*.cpp only!)
|
||||
#if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
|
||||
// CORRESPONDING LINE IN HeatSensor.cpp MUST ALSO BE ENABLED!
|
||||
|
||||
#include "globals.h"
|
||||
#include "heatSensor.h"
|
||||
|
||||
|
||||
// For heat sensing
|
||||
HeatSensor heatSensor;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue