lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Revision:
13:61131aac4031
Parent:
12:e44766b61346
Child:
14:18f159d48340
--- a/main.cpp	Sun Dec 29 06:59:45 2019 +0000
+++ b/main.cpp	Mon Dec 30 17:27:57 2019 +0000
@@ -6,7 +6,8 @@
   *          Parts are taken from example code from STMIcroelectronics
   ******************************************************************************
   * @attention
-  *          This code was specifically developed for 487 signal processing.
+  *          This code was specifically developed for BYU ECEn 487 course 
+  *          Introduction to Digital Signal Processing.
   *
   *
   ******************************************************************************
@@ -46,11 +47,13 @@
 
 uint32_t counter = 0;
 char buf[40];
-int first_half_time, second_half_time, total_time;
+int first_half_time = 0;
+int second_half_time = 0;
+int total_time = 0;
 
 int main()
 {
-    
+    /* Initialize the LCD Screen and display information */    
     BSP_LCD_Init();
     BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
     BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
@@ -64,70 +67,73 @@
     BSP_LCD_DisplayStringAt(0, L_CHANNEL_Y_POS, (uint8_t *)"L", LEFT_MODE);
     BSP_LCD_DisplayStringAt(0, R_CHANNEL_Y_POS, (uint8_t *)"R", LEFT_MODE);
 
+
+    /* Initialize the Audio Interface */
     BSP_AUDIO_IN_OUT_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, OUTPUT_DEVICE_HEADPHONE, DEFAULT_AUDIO_IN_FREQ, DEFAULT_AUDIO_IN_BIT_RESOLUTION, DEFAULT_AUDIO_IN_CHANNEL_NBR);
 
     /* Initialize SDRAM buffers */
     BSP_SDRAM_Init();
     memset((uint16_t *)AUDIO_BUFFER_IN, 0, AUDIO_BLOCK_SIZE * 2);
     memset((uint16_t *)AUDIO_BUFFER_OUT, 0, AUDIO_BLOCK_SIZE * 2);
-    printf("SDRAM init done\n");
 
-    audio_rec_buffer_state = BUFFER_OFFSET_NONE;
 
     /* Start Recording */
-    if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) {
-        printf("BSP_AUDIO_IN_Record error\n");
-    }
+    if (BSP_AUDIO_IN_Record((uint16_t *)AUDIO_BUFFER_IN, AUDIO_BLOCK_SIZE) != AUDIO_OK) { printf("BSP_AUDIO_IN_Record error\n"); }
 
     /* Start Playback */
     BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02);
-    if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) {
-        printf("BSP_AUDIO_OUT_Play error\n");
-    }
+    if (BSP_AUDIO_OUT_Play((uint16_t *)AUDIO_BUFFER_OUT, AUDIO_BLOCK_SIZE * 2) != AUDIO_OK) { printf("BSP_AUDIO_OUT_Play error\n"); }
+
 
     timer.start();
     while (1) {
     /* First Half */
-        /* Wait end of half block recording */
+        /* Wait end of half block recording before going on in the first half cycle*/
         while (audio_rec_buffer_state != BUFFER_OFFSET_HALF) {}
 
+        /* This captures the time of an entire cycle */
         total_time = timer.read_us();
+        
+        /* Reset the timer counter to zero */
         timer.reset();
 
-        /* Copy recorded 1st half block */
-        memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
-
-    /* Plot trace of first half block recording */   
+        /* Plot traces of first half block recording */   
         Erase_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, HALF_AUDIO_BLOCK_SIZE);
         Draw_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN, HALF_AUDIO_BLOCK_SIZE);
 
+        /* Copy recorded 1st half block into the audio buffer that goes out */
+        memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
+
+        /* Capture the timing of the first half processing */
         first_half_time = timer.read_us();
+    /* End First Half */
 
     /* Second Half */
-
-
         /* Wait end of one block recording */
         while (audio_rec_buffer_state != BUFFER_OFFSET_FULL) {}
         
-        /* Copy recorded 2nd half block */
-        memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
-
-    /* Plot second half recording */
+        /* Plot traces of second half block recording */
         Erase_Trace(OSC_START_X_POS+HALF_AUDIO_BLOCK_SIZE, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, HALF_AUDIO_BLOCK_SIZE);
         Draw_Trace(OSC_START_X_POS+HALF_AUDIO_BLOCK_SIZE, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE), HALF_AUDIO_BLOCK_SIZE);
-        
-        
-        
+                
+        /* Compute important cycle information and display it*/
         counter++;
         sprintf(buf, "Cycles: %9d", counter);
         BSP_LCD_SetTextColor(LCD_COLOR_RED);
         BSP_LCD_DisplayStringAt(0, 46, (uint8_t *) buf, LEFT_MODE);                
         sprintf(buf, "1:%6d 2:%6d T:%6d", first_half_time, second_half_time, total_time);
         BSP_LCD_DisplayStringAt(0, 20, (uint8_t *) buf, LEFT_MODE);
+
+        /* Copy recorded 2nd half block into audio output buffer */
+        memcpy((uint16_t *)(AUDIO_BUFFER_OUT + (AUDIO_BLOCK_SIZE)), (uint16_t *)(AUDIO_BUFFER_IN + (AUDIO_BLOCK_SIZE)), AUDIO_BLOCK_SIZE);
+    
+        /* Change the recording buffer state to reflect the status of the buffer */   
+        audio_rec_buffer_state = BUFFER_OFFSET_NONE;
         
-        audio_rec_buffer_state = BUFFER_OFFSET_NONE;
+        /* Measures the amount of time to process the second half */    
         second_half_time = timer.read_us();
         
+    /* End Second Half */
     }
 }