yutong look here

Dependencies:   FastAnalogIn fastADC

Files at this revision

API Documentation at this revision

Comitter:
yutonggu
Date:
Sun Dec 08 05:27:35 2019 +0000
Parent:
113:233a2fac1911
Commit message:
Changed to use fastADC and sample at 20KHz

Changed in this revision

fastADC.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fastADC.lib	Sun Dec 08 05:27:35 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/SafeDetect/code/fastADC/#d415e0b7dc3e
--- a/main.cpp	Tue Dec 03 22:39:22 2019 +0000
+++ b/main.cpp	Sun Dec 08 05:27:35 2019 +0000
@@ -9,6 +9,7 @@
 #include <AnalogIn.h>
 #include <AnalogOut.h>
 #include "circ_buff.hpp"
+#include "fastADC.h"
 
 
 AnalogOut v_src(GPIO0);
@@ -28,35 +29,33 @@
 #define WINDOW_SIZE         DEF_WINLENGTH - DEF_FRAMESHIFT
 #define ARRAY_SIZE          800
  
-#define BUFFSIZE                    11100 // size of buffer = sampling_time * sampling_rate * size(float)
+#define BUFFSIZE                    1000 // size of buffer = sampling_time * sampling_rate * size(float)
 //DigitalOut led1(LED1);
 //DigitalOut led2(LED2);
 Thread thread_adc;
 Thread thread_mfcc;
+Ticker read_ADC_ticker;
 
 // PIN DEFINITIONS
 DigitalOut vcc(GPIO0);
 AnalogIn mic(PB_0);
 CircularBuff<uint16_t> buff(BUFFSIZE);
 
+Serial pc(USBTX, USBRX);
 
-int num_readings = 0;
+volatile int num_readings = 0;
 int z = 0;
-uint16_t raw_adc = 0; 
+volatile uint16_t raw_adc = 0; 
 uint16_t* pop_val;
 
 void adc_thread() {
-    while (true) {
-        for (;;) {                            /* Loop forever                       */
-            if (ADC1->SR & (1 << 1)) {          /* If conversion has finished         */
-              raw_adc = ADC1->DR & 0x0FFF;       /* Read AD converted value            */
-              ADC1->CR2 |= 1 << 22;             /* Start new conversion               */ 
-            }
-            buff.push(raw_adc);
-            num_readings++;
-        }
+//    while (true) {
+//        wait_us(42);
+        raw_adc = readADC();       /* Read AD converted value            */
+        num_readings++;
+    
         
-    }
+//    }
 }
 
 //To sleep, 'wait' should be replaced by 'ThisThread::sleep_for' (C++) or 'thread_sleep_for' (C). If you wish to wait (without sleeping), call 'wait_us'. 'wait_us' is safe to call from ISR context. [since mbed-os-5.14] [-Wdeprecated-declarations] in "main.cpp", Line: 59, Col: 9
@@ -67,12 +66,13 @@
 void mfcc_thread() {
     while (true) {
         //printf("Top of MFCC thread");
-        thread_sleep_for(2000);
-        printf("Readings / second: %i\n\n", num_readings/2);
+        thread_sleep_for(5000);
+        double rate = num_readings/5000.0;
+        pc.printf("Readings / second: %f Ksps\n\n", rate);
         num_readings = 0;
-        pop_val = buff.pop();
-        printf("last reading: %u\n", *pop_val);
-        buff.clear();
+        //pop_val = buff.pop();
+        //pc.printf("last reading: %u\n", *pop_val);
+        //buff.clear();
     }
 }
 
@@ -91,14 +91,15 @@
 DMA_CCRx register
 6. Activate the channel by setting the ENABLE bit in the DMA_CCRx register
 */
-
-void config_dma() {
-    /* 
-    1. Set the peripheral register address in the DMA_CPARx register. The data will be
-    moved from/ to this address to/ from the memory after the peripheral event.
-    */
-    //RCC->DMA_CPAR1 &= 0x00000000;
-}
+//
+//void config_dma() {
+//    /* 
+//    1. Set the peripheral register address in the DMA_CPARx register. The data will be
+//    moved from/ to this address to/ from the memory after the peripheral event.
+//    */
+//    //RCC->DMA_CPAR1 &= 0x00000000;
+//    
+//}
 
 
 
@@ -107,24 +108,21 @@
 
 
 int main() {
-    config_dma();
-    printf("configuring DMA\n");
+//    config_dma();
+    pc.printf("configuring DMA\n");
     
-    printf("Creating buff of size %d\n", BUFFSIZE);
+    pc.printf("Creating buff of size %d\n", BUFFSIZE);
     /* clear the buffer */
-    buff.clear();
-    printf("library read val: %u\n", mic.read_u16());
+//    buff.clear();
+    pc.printf("Setting up ADC\n");
     
-//    /* Setup and initialize ADC converter   https://www.davidkebo.com/microcontroller-interfacing */
-//    RCC->APB2RSTR |=  1 <<  9;             /* Enable ADC1 clock                  */
-//    GPIOA->ARL   &= 0xFFF0FFFF;           /* Configure PC4 as ADC.14 input      */
-//    ADC1->SQR1    = 0x00000000;           /* Regular channel 1 conversion       */
-//    ADC1->SQR2    = 0x00000000;           /* Clear register                     */
-//    ADC1->SQR3    = 14 <<  0;             /* SQ1 = channel 14                   */
-//    ADC1->SMPR1   =  5 << 12;             /* Channel 14 sample time is 55.5 cyc */
-//    ADC1->SMPR2   = 0x00000000;           /* Clear register                     */
-//    ADC1->CR1     =  1 <<  8;             /* Scan mode on                       */
-//    ADC1->CR2     = (1 << 20) |           /* Enable external trigger            */
+    /* Setup and initialize ADC converter   https://www.davidkebo.com/microcontroller-interfacing */
+//    RCC->APB2RSTR |=  1 <<  9;            /* Enable ADC1 clock                  */
+    initADC();
+//    ADC->SMPR1   =  5 << 12;             /* Channel 14 sample time is 55.5 cyc */
+//    ADC->SMPR2   = 0x00000000;           /* Clear register                     */
+//    ADC->CR1     =  1 <<  8;             /* Scan mode on                       */
+//    ADC->CR2     = (1 << 20) |           /* Enable external trigger            */
 //                  (7 << 17) |           /* EXTSEL = SWSTART                   */
 //                  (1 <<  1) |           /* Continuous conversion              */
 //                  (1 <<  0) ;           /* ADC enable                         */
@@ -134,18 +132,18 @@
 //    while (ADC1->CR2 & (1 << 2));         /* Wait for calibration to finish     */
 //    ADC1->CR2    |=  1 << 22;             /* Start first conversion  */
 //    
-//    for (;;) {                            /* Loop forever                       */
-//        if (ADC1->SR & (1 << 1)) {          /* If conversion has finished         */
-//          raw_adc = ADC1->DR & 0x0FFF;       /* Read AD converted value            */
-//          ADC1->CR2 |= 1 << 22;             /* Start new conversion               */ 
-//        }
-//        printf("register read val: %u\n", raw_adc);
-//        thread_sleep_for(2000);
+    
+    //for (;;) {                            /* Loop forever                       */
+//        raw_adc = readADC();
+//        pc.printf("register read val: %u\n", raw_adc);
 //    }
     
     
     
-    while(1);
-    thread_adc.start(adc_thread);
+    
+    read_ADC_ticker.attach_us(&adc_thread, 50);
     thread_mfcc.start(mfcc_thread);
+    while(1){
+        thread_sleep_for(10000);
+    }
 }
\ No newline at end of file