4444#define NUMBER_OF_BYTES_IN_SAMPLE 2
4545#define EXTERNAL_SRAM_SIZE_IN_SAMPLES (AM_EXTERNAL_SRAM_SIZE_IN_BYTES / NUMBER_OF_BYTES_IN_SAMPLE)
4646#define NUMBER_OF_SAMPLES_IN_BUFFER (EXTERNAL_SRAM_SIZE_IN_SAMPLES / NUMBER_OF_BUFFERS)
47- #define NUMBER_OF_SAMPLES_IN_DMA_TRANSFER 1024
47+
48+ /* DMA transfer constant */
49+
50+ #define MAXIMUM_SAMPLES_IN_DMA_TRANSFER 1024
4851
4952/* Microphone warm-up constant */
5053
@@ -541,6 +544,10 @@ static configSettings_t *configSettings = (configSettings_t*)(AM_BACKUP_DOMAIN_S
541544
542545static AM_filterType_t requestedFilterType ;
543546
547+ /* DMA transfer variable */
548+
549+ static uint32_t numberOfSamplesInDMATransfer ;
550+
544551/* SRAM buffer variables */
545552
546553static volatile uint32_t writeBuffer ;
@@ -567,17 +574,17 @@ static volatile bool switchPositionChanged;
567574
568575/* DMA buffers */
569576
570- static int16_t primaryBuffer [NUMBER_OF_SAMPLES_IN_DMA_TRANSFER ];
577+ static int16_t primaryBuffer [MAXIMUM_SAMPLES_IN_DMA_TRANSFER ];
571578
572- static int16_t secondaryBuffer [NUMBER_OF_SAMPLES_IN_DMA_TRANSFER ];
579+ static int16_t secondaryBuffer [MAXIMUM_SAMPLES_IN_DMA_TRANSFER ];
573580
574581/* Current recording file name */
575582
576583static char fileName [32 ];
577584
578585/* Firmware version and description */
579586
580- static uint8_t firmwareVersion [AM_FIRMWARE_VERSION_LENGTH ] = {1 , 4 , 2 };
587+ static uint8_t firmwareVersion [AM_FIRMWARE_VERSION_LENGTH ] = {1 , 4 , 4 };
581588
582589static uint8_t firmwareDescription [AM_FIRMWARE_DESCRIPTION_LENGTH ] = "AudioMoth-Firmware-Basic" ;
583590
@@ -809,9 +816,9 @@ int main(void) {
809816
810817 }
811818
812- /* Use longer power-down period if an error has occurred (otherwise restart immediately) */
819+ /* Use longer power-down period if recording did not end normally or early due to the file size limit (otherwise restart immediately) */
813820
814- if (switchPosition == AM_SWITCH_DEFAULT && (recordingState == SDCARD_WRITE_ERROR || recordingState == SUPPLY_VOLTAGE_LOW )) {
821+ if (switchPosition == AM_SWITCH_DEFAULT && (recordingState == SUPPLY_VOLTAGE_LOW || recordingState == SWITCH_CHANGED || recordingState == SDCARD_WRITE_ERROR )) {
815822
816823 SAVE_SWITCH_POSITION_AND_POWER_DOWN (DEFAULT_WAIT_INTERVAL );
817824
@@ -870,13 +877,13 @@ inline void AudioMoth_handleDirectMemoryAccessInterrupt(bool isPrimaryBuffer, in
870877
871878 /* Update the current buffer index and write buffer */
872879
873- bool thresholdExceeded = DigitalFilter_filter (source , buffers [writeBuffer ] + writeBufferIndex , configSettings -> sampleRateDivider , NUMBER_OF_SAMPLES_IN_DMA_TRANSFER , configSettings -> amplitudeThreshold );
880+ bool thresholdExceeded = DigitalFilter_filter (source , buffers [writeBuffer ] + writeBufferIndex , configSettings -> sampleRateDivider , numberOfSamplesInDMATransfer , configSettings -> amplitudeThreshold );
874881
875882 if (dmaTransfersProcessed > dmaTransfersToSkip ) {
876883
877884 writeIndicator [writeBuffer ] |= thresholdExceeded ;
878885
879- writeBufferIndex += NUMBER_OF_SAMPLES_IN_DMA_TRANSFER / configSettings -> sampleRateDivider ;
886+ writeBufferIndex += numberOfSamplesInDMATransfer / configSettings -> sampleRateDivider ;
880887
881888 if (writeBufferIndex == NUMBER_OF_SAMPLES_IN_BUFFER ) {
882889
@@ -1030,11 +1037,23 @@ static AM_recordingState_t makeRecording(uint32_t currentTime, uint32_t recordDu
10301037
10311038 DigitalFilter_applyAdditionalGain (sampleMultiplier );
10321039
1040+ /* Calculate the number of samples in each DMA transfer */
1041+
1042+ numberOfSamplesInDMATransfer = MAXIMUM_SAMPLES_IN_DMA_TRANSFER / configSettings -> sampleRateDivider ;
1043+
1044+ while (numberOfSamplesInDMATransfer & (numberOfSamplesInDMATransfer - 1 )) {
1045+
1046+ numberOfSamplesInDMATransfer = numberOfSamplesInDMATransfer & (numberOfSamplesInDMATransfer - 1 );
1047+
1048+ }
1049+
1050+ numberOfSamplesInDMATransfer *= configSettings -> sampleRateDivider ;
1051+
10331052 /* Set up the DMA transfers to skip */
10341053
10351054 dmaTransfersProcessed = 0 ;
10361055
1037- dmaTransfersToSkip = configSettings -> sampleRate / FRACTION_OF_SECOND_FOR_WARMUP / NUMBER_OF_SAMPLES_IN_DMA_TRANSFER ;
1056+ dmaTransfersToSkip = configSettings -> sampleRate / FRACTION_OF_SECOND_FOR_WARMUP / numberOfSamplesInDMATransfer ;
10381057
10391058 /* Calculate recording parameters */
10401059
@@ -1052,7 +1071,7 @@ static AM_recordingState_t makeRecording(uint32_t currentTime, uint32_t recordDu
10521071
10531072 AudioMoth_enableMicrophone (configSettings -> gain , configSettings -> clockDivider , configSettings -> acquisitionCycles , configSettings -> oversampleRate );
10541073
1055- AudioMoth_initialiseDirectMemoryAccess (primaryBuffer , secondaryBuffer , NUMBER_OF_SAMPLES_IN_DMA_TRANSFER );
1074+ AudioMoth_initialiseDirectMemoryAccess (primaryBuffer , secondaryBuffer , numberOfSamplesInDMATransfer );
10561075
10571076 AudioMoth_startMicrophoneSamples (configSettings -> sampleRate );
10581077
0 commit comments