|
| 1 | +/* This example was developed for Nucleo F401RE or L476RG only. If you want to use other Nucleo or Architecture |
| 2 | + * you have to implement the dedicated file to manage the low level PDM part |
| 3 | + */ |
| 4 | +#if !defined(ARDUINO_NUCLEO_L476RG) && !defined(ARDUINO_NUCLEO_F401RE) |
| 5 | +#error "This example is only for STM32 Nucleo-F401RE or Nucleo-L476RG!" |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "Arduino.h" |
| 9 | +#include "app_config.h" |
| 10 | +#include "PDM.h" |
| 11 | +#include "pdm2pcm.h" |
| 12 | +#include "WaveEncoder.h" |
| 13 | +#include "SD.h" |
| 14 | +#include <CMSIS_DSP.h> |
| 15 | + |
| 16 | +/* PDM */ |
| 17 | +uint16_t sampleBuffer[(((AUDIO_IN_FREQ / 8) * MAX_AUDIO_IN_CHANNEL_NBR_PER_IF * N_MS_PER_INTERRUPT)/2)] = {0}; |
| 18 | +int count = 1; |
| 19 | +String file_name = ""; |
| 20 | + |
| 21 | +/* Button */ |
| 22 | +bool is_start = false; /* true means start, false means stop */ |
| 23 | +volatile bool button = false; |
| 24 | + |
| 25 | +#ifdef USER_BTN |
| 26 | + const int buttonPin = USER_BTN; |
| 27 | +#else |
| 28 | + const int buttonPin = 5; |
| 29 | +#endif |
| 30 | + |
| 31 | +int PushButtonState = LOW; |
| 32 | + |
| 33 | +/* PDM2PCM */ |
| 34 | +#define PCM_BUFLEN (AUDIO_IN_FREQ/AUDIO_IN_DECIMATOR_FACTOR) /* AUDIO_IN_FREQ = 1280, AUDIO_IN_DECIMATOR_FACTOR = 80 so PCM_BUFLEN = 1280/80 = 16*/ |
| 35 | +int16_t pcmSamples[PCM_BUFLEN*N_MS_PER_INTERRUPT]; |
| 36 | + |
| 37 | +/* SD Card */ |
| 38 | +File myFile; |
| 39 | +uint8_t pHeader[44]; |
| 40 | +uint32_t byteswritten = 44; |
| 41 | + |
| 42 | +void setup() { |
| 43 | + /* Serial */ |
| 44 | + Serial.begin(115200); |
| 45 | + |
| 46 | + /* Button Event Setting */ |
| 47 | + pinMode(buttonPin,INPUT_PULLUP); |
| 48 | + attachInterrupt(digitalPinToInterrupt(buttonPin), play_stop, FALLING); |
| 49 | + |
| 50 | + /* Check what is the Push Button State when the button is not pressed. It can change across families */ |
| 51 | + PushButtonState = (digitalRead(buttonPin)) ? LOW : HIGH; |
| 52 | + |
| 53 | + /* SD Card Initialization */ |
| 54 | + if (!SD.begin(12000000, 10)) { |
| 55 | + Serial.println("Failed to inizialize SD!\n"); |
| 56 | + while(1); |
| 57 | + } |
| 58 | + |
| 59 | + /* Enable and Start */ |
| 60 | + if (PDM.Begin() != PDM_OK) |
| 61 | + { |
| 62 | + Serial.println("Failed to start PDM!\n"); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + Serial.println("PDM correctly initialized!\n"); |
| 67 | + } |
| 68 | + |
| 69 | + /* Function to do with data */ |
| 70 | + PDM.onReceive(foo); |
| 71 | + |
| 72 | + /* Initialize PDM2PCM */ |
| 73 | + pdm2pcm_init(BYTE_LEFT_MSB, PDM_ENDIANNESS_BE, SINC4); |
| 74 | + if(pdm2pcm_volume(5)) //set volume (0-6) |
| 75 | + Serial.println("Volume not correct!\n"); |
| 76 | +} |
| 77 | + |
| 78 | +void loop() { |
| 79 | + if(button){ |
| 80 | + /* Debouncing */ |
| 81 | + delay(50); |
| 82 | + |
| 83 | + /* Wait until the button is released */ |
| 84 | + while ((digitalRead(buttonPin) == PushButtonState)); |
| 85 | + |
| 86 | + /* Debouncing */ |
| 87 | + delay(50); |
| 88 | + |
| 89 | + if(!is_start){ |
| 90 | + |
| 91 | + Serial.println("Setting File"); |
| 92 | + |
| 93 | + /*Inizialization File */ |
| 94 | + file_name = "sample_" + String(count++) + ".wav"; |
| 95 | + byteswritten = 44; |
| 96 | + |
| 97 | + /*Inizialization File */ |
| 98 | + if (SD.exists(file_name)) { |
| 99 | + SD.remove(file_name); |
| 100 | + Serial.println("File Already Exists - File Removed"); |
| 101 | + } |
| 102 | + |
| 103 | + if (!(myFile = SD.open(file_name, FILE_WRITE))) { |
| 104 | + Serial.println("Failed to Inizialize File\n"); |
| 105 | + while(1); |
| 106 | + } |
| 107 | + |
| 108 | + WAV.header_init(pHeader, 16000, 16); |
| 109 | + |
| 110 | + myFile.write(pHeader, 44); |
| 111 | + myFile.close(); |
| 112 | + Serial.flush(); |
| 113 | + |
| 114 | + is_start = true; |
| 115 | + Serial.println("RECORDING"); |
| 116 | + PDM.Record(sampleBuffer); |
| 117 | + |
| 118 | + }else{ |
| 119 | + is_start = false; |
| 120 | + if (PDM.Stop() == PDM_ERROR) |
| 121 | + { |
| 122 | + Serial.println("PDM_ERROR"); |
| 123 | + } |
| 124 | + Serial.println("STOP"); |
| 125 | + |
| 126 | + /* Update and Writing header */ |
| 127 | + if (!(myFile = SD.open(file_name, O_WRITE))) { |
| 128 | + Serial.println("Failed to Open File\n"); |
| 129 | + while(1); |
| 130 | + } |
| 131 | + |
| 132 | + WAV.header_update(pHeader, &byteswritten); |
| 133 | + if (!myFile.seek(0)) { |
| 134 | + Serial.println("Failed to Update Header\n"); |
| 135 | + while(1); |
| 136 | + } |
| 137 | + myFile.write(pHeader, 44); |
| 138 | + myFile.close(); |
| 139 | + |
| 140 | + } |
| 141 | + button = false; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +extern "C" void foo() |
| 147 | +{ |
| 148 | + for (uint32_t i = 0; i < N_MS_PER_INTERRUPT; i++) |
| 149 | + { |
| 150 | + pdm2pcm((uint8_t *) & (sampleBuffer[i * ((BLOCK_SIZE/8)/2)]), &pcmSamples[i * PCM_BUFLEN], BLOCK_SIZE); |
| 151 | + } |
| 152 | + |
| 153 | + /* Writing on SD */ |
| 154 | + myFile = SD.open(file_name, O_WRITE); |
| 155 | + myFile.seek(byteswritten); |
| 156 | + for (uint32_t i = 0; i < PCM_BUFLEN * N_MS_PER_INTERRUPT; i++) |
| 157 | + { |
| 158 | + uint16_t data = (uint16_t)pcmSamples[i]; |
| 159 | + |
| 160 | + myFile.write((uint8_t)data); |
| 161 | + myFile.write((uint8_t)(data>>8)); |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + byteswritten += PCM_BUFLEN * N_MS_PER_INTERRUPT * sizeof(int16_t); |
| 166 | + myFile.flush(); |
| 167 | + myFile.close(); |
| 168 | +} |
| 169 | + |
| 170 | +void play_stop() |
| 171 | +{ |
| 172 | + button = true; |
| 173 | +} |
0 commit comments