Analog Devices / Mbed OS EVAL-CN0540-ARDZ

Dependencies:   platform_drivers LTC26X6 AD77681

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002  *Copyright (c)2020 Analog Devices, Inc.  
00003  *
00004  * Licensed under the 2020-04-27-CN0540EC License(the "License");
00005  * you may not use this file except in compliance with the License.
00006  *
00007  ****************************************************************************/
00008 #include <mbed.h>
00009 #include "platform_drivers.h"
00010 #include <assert.h>
00011 #include "main.h"
00012 #include "cn0540_app_config.h"
00013 #include "platform_drivers.h"
00014 #include "cn0540_init_params.h"
00015 
00016 extern "C"{
00017 #include "ad77681.h"
00018 #include "cn0540_adi_fft.h" 
00019 #include "ltc26x6.h"
00020 }
00021 // Descriptor of the main device - the ADC AD7768-1
00022 ad77681_dev *device_adc;
00023 // Structure carying measured data, sampled by the ADC
00024 adc_data measured_data;
00025 // AD7768-1's status register structure, carying all the error flags
00026 ad77681_status_registers *current_status;
00027 // Initialize the interrupt event variable
00028 volatile bool int_event= false;
00029 
00030 // Descriptor of the DAC LTC2606
00031 ltc26x6_dev *device_dac;
00032 
00033 // Structure carying data, the FFT module works with                
00034 fft_entry *FFT_data;    
00035 // Structure carying measuremtns from the FFT module                                
00036 fft_measurements *FFT_meas; 
00037 
00038 // Initialize the serial object with TX and RX pins
00039 Serial pc(USBTX, USBRX);
00040 
00041 // Initialize the drdy pin as interrupt input
00042 InterruptIn drdy(DRDY_PIN, PullNone);
00043 // Initialize the adc_rst_pin pin as digital output
00044 DigitalOut adc_reset(ADC_RST_PIN);
00045 // Initialize the buffer enable control pin as digital output
00046 DigitalOut buffer_en(ARD_BUF_EN_PIN);
00047 // Initialize the red LED control pin as digital output
00048 DigitalOut led_red(ARD_RED_LED_PIN);
00049 // Initialize the blue LED pin as digital output
00050 DigitalOut led_blue(ARD_BLUE_LED_PIN);
00051 
00052 /**
00053  * ADC data recteption interrupt from DRDY
00054  * 
00055  * Data reception from the ADC using interrupt generated by the ADC's DRDY (Data Ready) pin
00056  * Interrupt triggers falling edge of the active-high DRDY pulse
00057  * DRDY pulse is generated by the ADC and frequency of the DRDY pulse depends on the ADC settings:
00058  * 
00059  * DRDY frequency = MCLK / ( MCLK_DIV * FILTER_OSR )
00060  */
00061 void drdy_interrupt()
00062 {
00063     int_event = true;
00064 
00065     if (measured_data.count == measured_data.samples) { // Desired numer of samples has been taken, set everything back
00066         drdy.disable_irq();                             // Disable interrupt on DRDY pin
00067         measured_data.finish = true;                    // Desired number of samples has been taken
00068         measured_data.count = 0;                        // Set measured data counter to 0
00069     }
00070 }
00071 /**
00072  *=================================================================================================================================
00073  *
00074  * //////////////////////////////////////////////////    MAIN function      \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
00075  *
00076  *==================================================================================================================================
00077  */
00078 int main() {
00079 
00080     int32_t connected = FAILURE;
00081     uint32_t menu;
00082 
00083     sdpk1_gpio_setup();                                                     // Setup SDP-K1 GPIOs
00084     adc_hard_reset();                                                       // Perform ADC hard reset
00085     connected = ad77681_setup(&device_adc, init_params, &current_status);   // SETUP and check connection
00086     if(connected == FAILURE)
00087         go_to_error();      
00088     ltc26x6_init(&device_dac, init_params_dac);                             // Initialize DAC
00089     
00090     #ifdef CN0540_ADI_FFT_H_                                                // if the adi_fft.h is included , initialize it
00091     FFT_init_params(&FFT_data, &FFT_meas);                                  // Initialize FFT structure and allocate space
00092     update_FFT_enviroment(device_adc->vref, device_adc->mclk, device_adc->sample_rate, FFT_data);   // Update the Vref, Mclk, Sampling rate
00093     measured_data.samples = 4096;                                           // Initialize FFT with 4096 samples
00094     FFT_init(measured_data.samples, FFT_data);                              // Update sample count for FFT
00095     #endif // CN0540_ADI_FFT_H_                                                    // FFT module is initializedd with 4096 samples and 7-term BH window
00096 
00097     print_title();
00098     print_prompt();
00099 
00100 //============ MAIN WHILE ====================//    
00101     while(1)
00102     {
00103         
00104         if (pc.readable()) { // Settings menu SWITCH
00105             getUserInput(&menu);
00106             
00107             switch (menu) {     
00108             case 1:
00109                 menu_1_set_adc_powermode();     // Set ADC power mode
00110                 break;
00111             case 2:
00112                 menu_2_set_adc_clock_divider(); // Set ADC clock divider
00113                 break;
00114             case 3:
00115                 menu_3_set_adc_filter_type();   // Set ad7768-1 filter type
00116                 break;
00117             case 4:
00118                 menu_4_adc_buffers_controll();  // Set ADC AIN & Reference buffers
00119                 break;
00120             case 5:
00121                 menu_5_set_default_settings();  // Set ADC default value
00122                 break;
00123             case 6:
00124                 menu_6_set_adc_vcm();           // Set ADC VCM
00125                 break;  
00126             case 7:
00127                 menu_7_adc_read_register();     // Read ADC register
00128                 break;
00129             case 8:
00130                 menu_8_adc_cont_read_data();    // Perform continuous ADC read
00131                 break;
00132             case 9:
00133                 menu_9_reset_ADC();             // Reset ADC
00134                 break;
00135             case 10:
00136                 menu_10_power_down();           // ADC Wake up and sleep 
00137                 break;
00138             case 11:
00139                 menu_11_ADC_GPIO();             // Set ADC GPIOs
00140                 break;
00141             case 12:
00142                 menu_12_read_master_status();   // Read ADC master status
00143                 break;
00144             case 13:
00145                 menu_13_mclk_vref();            // Set ADC MCLK / Vref
00146                 break;
00147             case 14:
00148                 menu_14_print_measured_data();  // Print continouos ADC read data
00149                 break;
00150             case 15:
00151                 menu_15_set_adc_data_output_mode(); // Set ADC data output mode 
00152                 break;
00153             case 16:
00154                 menu_16_set_adc_diagnostic_mode();  // Set ADC to diagnostic mode
00155                 break;
00156             case 17:
00157                 menu_17_do_the_fft();               // Perform FFT
00158                 break;
00159             case 18:
00160                 menu_18_fft_settings();             // Change FFT settins
00161                 break;
00162             case 19:
00163                 menu_19_gains_offsets();            // Set ADC gain and offset
00164                 break;
00165             case 20:
00166                 menu_20_check_scratchpad();         // Perform scratchpad check
00167                 break;
00168             case 21:
00169                 menu_21_piezo_offset();             // Compensate piezo offset
00170                 break;
00171             case 22:
00172                 menu_22_set_DAC_output();           // Set DAC output mode
00173                 break;
00174             default:
00175                 pc.printf("Invalid option");
00176                 print_prompt();
00177                 break;
00178             }
00179         }
00180     }   
00181 }
00182 
00183 /**
00184  * Error warning, in case of unsuccessfull SPI connection
00185  *
00186  */
00187 void static go_to_error()
00188 {
00189     int32_t connected = FAILURE;
00190     uint8_t scratchpad_sequence = 0xAD;
00191     while (1) {
00192         pc.printf("ERROR: NOT CONNECTED\nCHECK YOUR PHYSICAL CONNECTION\n\n");  // When not connected, keep showing error message
00193         wait(5);
00194         connected = ad77681_setup(&device_adc, init_params, &current_status);   // Keep trying to connect
00195         if (connected == SUCCESS) {
00196             pc.printf("SUCCESSFULLY RECONNECTED\n\n");                          // If successfull reading from scratchpad, init the ADC and go back
00197             break;
00198         }
00199     }
00200 }
00201 
00202 /**
00203  * Print title
00204  *
00205  */
00206 void static print_title() {
00207     pc.printf("\n\r");
00208     pc.printf("****************************************************************\n");
00209     pc.printf("*      EVAL-CN0540-PMDZ Demonstration Program -- (mbed)        *\n");
00210     pc.printf("*                                                              *\n");
00211     pc.printf("*   This program demonstrates IEPE / ICP piezo accelerometer   *\n");
00212     pc.printf("*        interfacing and FFT measurements using AD7768-1       *\n");
00213     pc.printf("*           Precision 24-bit sigma-delta AD converter          *\n");
00214     pc.printf("*                                                              *\n");
00215     pc.printf("* Set the baud rate to 115200 select the newline terminator.   *\n");
00216     pc.printf("****************************************************************\n");
00217 }
00218 
00219 /**
00220  * Print main menu to console
00221  *
00222  */
00223 void static print_prompt() {
00224     pc.printf("\n\nCommand Summary:\n\n");
00225     pc.printf("  1  - Set ADC power mode\n");
00226     pc.printf("  2  - Set ADC MCLK divider\n");
00227     pc.printf("  3  - Set ADC filter type\n");
00228     pc.printf("  4  - Set ADC AIN and REF buffers\n");
00229     pc.printf("  5  - Set ADC to default config\n");
00230     pc.printf("  6  - Set ADC VCM output\n");
00231     pc.printf("  7  - Read desired ADC register\n");
00232     pc.printf("  8  - Read continuous ADC data\n");
00233     pc.printf("  9  - Reset ADC\n");
00234     pc.printf("  10 - ADC Power-down\n");
00235     pc.printf("  11 - Set ADC GPIOs\n");
00236     pc.printf("  12 - Read ADC master status\n");
00237     pc.printf("  13 - Set ADC Vref and MCLK\n");
00238     pc.printf("  14 - Print ADC measured data\n");
00239     pc.printf("  15 - Set ADC data output mode\n");
00240     pc.printf("  16 - Set ADC diagnostic mode\n");
00241     pc.printf("  17 - Do the FFT\n");
00242     pc.printf("  18 - FFT settings\n");
00243     pc.printf("  19 - Set ADC Gains, Offsets\n");
00244     pc.printf("  20 - ADC Scratchpad Check\n");
00245     pc.printf("  21 - Compenzate Piezo sensor offset\n");
00246     pc.printf("  22 - Set DAC output\n");
00247     pc.printf("\n\r");
00248 }
00249 
00250 /**
00251  * Read user input from uart
00252  * *UserInput = 0 if failure
00253  *
00254  */
00255 int32_t static getUserInput(uint32_t *UserInput)
00256 {
00257     long uart_val;
00258     int32_t ret;
00259 
00260     ret = pc.scanf("%ld", &uart_val);       // Return 1 = OK, Return 0 = Fail
00261 
00262     if((ret == 0) || (uart_val < 0)) {      // Failure if uart_val is negative, or non-digit
00263         *UserInput = 0;
00264         return FAILURE;
00265     }
00266     *UserInput = (uint32_t)(uart_val);
00267     return SUCCESS;
00268 }
00269 
00270 /**
00271  * Set power mode
00272  *
00273  */
00274  void static menu_1_set_adc_powermode(void)
00275  {  
00276     uint32_t new_pwr_mode;
00277     
00278     pc.printf(" Avaliable power modes: \n");
00279     pc.printf("  1 - Low power mode\n");
00280     pc.printf("  2 - Median power mode\n");
00281     pc.printf("  3 - Fast power mode\n");
00282     pc.printf(" Select an option: \n");
00283 
00284     getUserInput(&new_pwr_mode);    
00285     pc.putc('\n');
00286     
00287     switch (new_pwr_mode) { 
00288     case 1:
00289         ad77681_set_power_mode(device_adc, AD77681_ECO);
00290         pc.printf(" Low power mode selected\n");
00291         break;
00292     case 2:
00293         ad77681_set_power_mode(device_adc, AD77681_MEDIAN);
00294         pc.printf(" Median power mode selected\n");
00295         break;
00296     case 3:
00297         ad77681_set_power_mode(device_adc, AD77681_FAST);
00298         pc.printf(" Fast power mode selected\n");
00299         break;
00300     default:
00301         pc.printf(" Invalid option\n");
00302         break;
00303     }
00304     print_prompt();
00305 }
00306 
00307 /**
00308  * Set clock divider
00309  *
00310  */
00311  void static menu_2_set_adc_clock_divider(void)
00312  {
00313     uint32_t new_mclk_div;
00314     
00315     pc.printf(" Avaliable MCLK divide options: \n");
00316     pc.printf("  1 - MCLK/16\n");
00317     pc.printf("  2 - MCLK/8\n");
00318     pc.printf("  3 - MCLK/4\n");
00319     pc.printf("  4 - MCLK/2\n");
00320     pc.printf(" Select an option: \n");
00321         
00322     getUserInput(&new_mclk_div);    
00323     pc.putc('\n');
00324     
00325     switch (new_mclk_div) { 
00326     case 1:
00327         ad77681_set_mclk_div(device_adc, AD77681_MCLK_DIV_16);
00328         pc.printf(" MCLK/16 selected\n");
00329         break;
00330     case 2:
00331         ad77681_set_mclk_div(device_adc, AD77681_MCLK_DIV_8);
00332         pc.printf(" MCLK/8 selected\n");
00333         break;
00334     case 3:
00335         ad77681_set_mclk_div(device_adc, AD77681_MCLK_DIV_4);
00336         pc.printf(" MCLK/4 selected\n");
00337         break;
00338     case 4:
00339         ad77681_set_mclk_div(device_adc, AD77681_MCLK_DIV_2);
00340         pc.printf(" MCLK/2 selected\n");
00341         break;
00342     default:
00343         pc.printf(" Invalid option\n");
00344         break;
00345     }
00346     // Update the sample rate after changing the MCLK divider
00347     ad77681_update_sample_rate(device_adc);         
00348     print_prompt();
00349 }
00350 
00351 /**
00352  * Set filter type
00353  *
00354  */
00355  void static menu_3_set_adc_filter_type(void) 
00356 {
00357     uint32_t new_filter = 0;
00358     int32_t ret;
00359     
00360     pc.printf(" Avaliable clock divide options: \n");
00361     pc.printf(" 1 - SINC3 Fileter\n");
00362     pc.printf(" 2 - SINC5 Filter\n");
00363     pc.printf(" 3 - Low ripple FIR Filter\n");
00364     pc.printf(" 4 - SINC3 50/60Hz rejection\n");
00365     pc.printf(" 5 - User-defined FIR filter\n");
00366     pc.printf(" Select an option: \n");
00367     
00368     getUserInput(&new_filter);  
00369     pc.putc('\n');
00370     
00371     switch (new_filter) {   
00372     case 1:
00373         set_adc_SINC3_filter();
00374         break;      
00375     case 2:
00376         set_adc_SINC5_filter();
00377         break;      
00378     case 3:
00379         set_adc_FIR_filter();
00380         break;  
00381     case 4:
00382         set_adc_50HZ_rej();
00383         break;  
00384     case 5:
00385         set_adc_user_defined_FIR();
00386         break;      
00387     default:
00388         pc.printf(" Invalid option\n");
00389         break;
00390     }
00391     // Update the sample rate after changing the Filter type
00392     ad77681_update_sample_rate(device_adc); 
00393     print_prompt();
00394 }
00395 
00396 /**
00397  * Set SINC3 filter
00398  *
00399  */
00400 void static set_adc_SINC3_filter(void)
00401 {   
00402     uint32_t new_sinc3 = 0, new_sinc5 = 0;
00403     int32_t ret;
00404     
00405     pc.printf(" SINC3 filter Oversampling ratios: \n");
00406     pc.printf(" OSR is calculated as (x + 1)*32\n");
00407     pc.printf(" x is SINC3 OSR register value\n");
00408     pc.printf(" Please input a value from 0 to 8192 = 2^13\n  :");
00409         
00410     ret = getUserInput(&new_sinc3);
00411 
00412     if ((new_sinc3 >= 0) && (new_sinc3 <= 8192) && (ret == SUCCESS)) {
00413         pc.printf("%d\n", new_sinc3);
00414         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_SINC3, new_sinc3);
00415         pc.printf(" SINC3 OSR is set to %d\n", (new_sinc3 + 1) * 32);
00416     } else {
00417         pc.printf("%d\n", new_sinc3);
00418         pc.printf(" Invalid option - too large number\n");
00419     }    
00420 }
00421 
00422 /**
00423  * Set SINC5 filter
00424  *
00425  */
00426 void static set_adc_SINC5_filter(void)
00427 {
00428     uint32_t new_sinc5;
00429     
00430     pc.printf(" SINC5 filter Oversampling ratios: \n");
00431     pc.printf("  1 - Oversampled by 8\n");
00432     pc.printf("  2 - Oversampled by 16\n");
00433     pc.printf("  3 - Oversampled by 32\n");
00434     pc.printf("  4 - Oversampled by 64\n");
00435     pc.printf("  5 - Oversampled by 128\n");
00436     pc.printf("  6 - Oversampled by 256\n");
00437     pc.printf("  7 - Oversampled by 512\n");
00438     pc.printf("  8 - Oversampled by 1024\n");
00439     pc.printf(" Select an option: \n");
00440         
00441     getUserInput(&new_sinc5);
00442         
00443     pc.putc('\n');
00444         
00445     switch (new_sinc5) {
00446     case 1:
00447         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_SINC5_DECx8, 0);
00448         pc.printf(" SINC5 with OSRx8 set\n");
00449         break;
00450     case 2:
00451         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_SINC5_DECx16, 0);
00452         pc.printf(" SINC5 with OSRx16 set\n");
00453         break;
00454     case 3:
00455         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_SINC5, 0);
00456         pc.printf(" SINC5 with OSRx32 set\n");
00457         break;
00458     case 4:
00459         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx64, AD77681_SINC5, 0);
00460         pc.printf(" SINC5 with OSRx64 set\n");
00461         break;
00462     case 5:
00463         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx128, AD77681_SINC5, 0);
00464         pc.printf(" SINC5 with OSRx128 set\n");
00465         break;
00466     case 6:
00467         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx256, AD77681_SINC5, 0);
00468         pc.printf(" SINC5 with OSRx256 set\n");
00469         break;
00470     case 7:
00471         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx512, AD77681_SINC5, 0);
00472         pc.printf(" SINC5 with OSRx512 set\n");
00473         break;
00474     case 8:
00475         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx1024, AD77681_SINC5, 0);
00476         pc.printf(" SINC5 with OSRx1024 set\n");
00477         break;
00478     default:
00479         pc.printf(" Invalid option\n");
00480         break;
00481     }   
00482 }
00483 
00484 /**
00485  * Set FIR filter
00486  *
00487  */
00488 void static set_adc_FIR_filter(void)
00489 {
00490     uint32_t new_fir;
00491     
00492     pc.printf(" FIR filter Oversampling ratios: \n");
00493     pc.printf("  1 - Oversampled by 32\n");
00494     pc.printf("  2 - Oversampled by 64\n");
00495     pc.printf("  3 - Oversampled by 128\n");
00496     pc.printf("  4 - Oversampled by 256\n");
00497     pc.printf("  5 - Oversampled by 512\n");
00498     pc.printf("  6 - Oversampled by 1024\n");
00499     pc.printf(" Select an option: \n");
00500         
00501     getUserInput(&new_fir);
00502         
00503     pc.putc('\n');
00504         
00505     switch (new_fir) {
00506     case 1:
00507         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_FIR, 0);
00508         pc.printf(" FIR with OSRx32 set\n");
00509         break;
00510     case 2:
00511         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx64, AD77681_FIR, 0);
00512         pc.printf(" FIR with OSRx64 set\n");
00513         break;
00514     case 3:
00515         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx128, AD77681_FIR, 0);
00516         pc.printf(" FIR with OSRx128 set\n");
00517         break;
00518     case 4:
00519         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx256, AD77681_FIR, 0);
00520         pc.printf(" FIR with OSRx256 set\n");
00521         break;
00522     case 5:
00523         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx512, AD77681_FIR, 0);
00524         pc.printf(" FIR with OSRx512 set\n");
00525         break;
00526     case 6:
00527         ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx1024, AD77681_FIR, 0);
00528         pc.printf(" FIR with OSRx1024 set\n");
00529         break;
00530     default:
00531         pc.printf(" Invalid option\n");
00532         break;
00533     }           
00534 }
00535 
00536 /**
00537  * Set 50HZ rejection bit when SINC3 is being used
00538  *
00539  */
00540 void static set_adc_50HZ_rej(void)
00541 {
00542     uint32_t new_50Hz;
00543     
00544     pc.printf(" SINC3 50/60Hz rejection: \n");
00545     pc.printf("  1 - 50/60Hz rejection enable \n");
00546     pc.printf("  2 - 50/60Hz rejection disable \n");
00547     pc.printf(" Select an option: \n");
00548         
00549     getUserInput(&new_50Hz);
00550         
00551     pc.putc('\n');
00552         
00553     switch (new_50Hz)
00554     {
00555     case 1:
00556         ad77681_set_50HZ_rejection(device_adc, ENABLE);
00557         pc.printf(" SINC3 50/60Hz rejection enabled\n");
00558         break;
00559     case 2:
00560         ad77681_set_50HZ_rejection(device_adc, DISABLE);
00561         pc.printf(" SINC3 50/60Hz rejection disabled\n");
00562         break;
00563     default:
00564         pc.printf(" Invalid option\n");
00565         break;
00566     }   
00567 }
00568 
00569 /**
00570  * Insert user-defined FIR filter coeffs
00571  *
00572  */
00573 void static set_adc_user_defined_FIR(void) 
00574 {
00575     const uint8_t coeff_reg_length = 56; // Maximum allowed number of coefficients in the coeff register                                        
00576 
00577     pc.printf(" User Defined FIR filter\n");
00578     
00579     if ((ARRAY_SIZE(programmable_FIR) <= coeff_reg_length) && (count_of_active_coeffs <= coeff_reg_length)) {
00580         pc.printf("  Aplying user-defined FIR filter coefficients from 'FIR_user_coeffs.h'\n");
00581         ad77681_programmable_filter(device_adc, programmable_FIR, count_of_active_coeffs);
00582         pc.printf(" Coeffs inserted successfully\n");
00583     } else
00584         pc.printf("  Incorrect count of coefficients in 'FIR_user_coeffs.h'\n");    
00585 }
00586 
00587 /**
00588  * AIN and REF buffers controll
00589  *
00590  */
00591  void static menu_4_adc_buffers_controll(void)
00592  {
00593     uint32_t new_AIN_buffer = 0, new_REF_buffer = 0, new_buffer = 0;
00594     
00595     pc.printf(" Buffers settings: \n");
00596     pc.printf("  1 - Set AIN precharge buffers\n");
00597     pc.printf("  2 - Set REF buffers\n");
00598     pc.printf(" Select an option: \n");
00599         
00600     getUserInput(&new_buffer);
00601     pc.putc('\n');
00602     
00603     switch (new_buffer) {
00604     case 1:
00605         pc.printf(" Analog IN precharge buffers settings: \n");
00606         pc.printf("  1 - Turn ON  both precharge buffers\n");
00607         pc.printf("  2 - Turn OFF both precharge buffers\n");
00608         pc.printf("  3 - Turn ON  AIN- precharge buffer\n");
00609         pc.printf("  4 - Turn OFF AIN- precharge buffer\n");
00610         pc.printf("  5 - Turn ON  AIN+ precharge buffer\n");
00611         pc.printf("  6 - Turn OFF AIN+ precharge buffer\n");
00612         pc.printf(" Select an option: \n");
00613                 
00614         getUserInput(&new_AIN_buffer);
00615         pc.putc('\n');
00616         
00617         switch (new_AIN_buffer) {
00618         case 1:
00619             ad77681_set_AINn_buffer(device_adc, AD77681_AINn_ENABLED);
00620             ad77681_set_AINp_buffer(device_adc, AD77681_AINp_ENABLED);
00621             pc.printf(" AIN+ and AIN- enabled\n");
00622             break;
00623         case 2:
00624             ad77681_set_AINn_buffer(device_adc, AD77681_AINn_DISABLED);
00625             ad77681_set_AINp_buffer(device_adc, AD77681_AINp_DISABLED);
00626             pc.printf(" AIN+ and AIN- disabled\n");
00627             break;
00628         case 3:
00629             ad77681_set_AINn_buffer(device_adc, AD77681_AINn_ENABLED);
00630             pc.printf(" AIN- Enabled\n");
00631             break;
00632         case 4:
00633             ad77681_set_AINn_buffer(device_adc, AD77681_AINn_DISABLED);
00634             pc.printf(" AIN- Disabled\n");
00635             break;
00636         case 5:
00637             ad77681_set_AINp_buffer(device_adc, AD77681_AINp_ENABLED);
00638             pc.printf(" AIN+ Enabled\n");
00639             break;
00640         case 6:
00641             ad77681_set_AINp_buffer(device_adc, AD77681_AINp_DISABLED);
00642             pc.printf(" AIN+ Disabled\n");
00643             break;
00644         default:
00645             pc.printf(" Invalid option\n");
00646             break;
00647         }       
00648         break;
00649     case 2:
00650         pc.printf(" REF buffers settings: \n");
00651         pc.printf("  1 - Full REF- reference buffer\n");
00652         pc.printf("  2 - Full REF+ reference buffer\n");
00653         pc.printf("  3 - Unbuffered REF- reference buffer\n");
00654         pc.printf("  4 - Unbuffered REF+ reference buffer\n");
00655         pc.printf("  5 - Precharge  REF- reference buffer\n");
00656         pc.printf("  6 - Precharge  REF+ reference buffer\n");
00657         pc.printf(" Select an option: \n");
00658         
00659         getUserInput(&new_REF_buffer);
00660         pc.putc('\n');
00661         
00662         switch (new_REF_buffer) {
00663         case 1:
00664             ad77681_set_REFn_buffer(device_adc, AD77681_BUFn_FULL_BUFFER_ON);
00665             pc.printf(" Fully buffered REF-\n");
00666             break;
00667         case 2:
00668             ad77681_set_REFp_buffer(device_adc, AD77681_BUFp_FULL_BUFFER_ON);
00669             pc.printf(" Fully buffered REF+\n");
00670             break;
00671         case 3:
00672             ad77681_set_REFn_buffer(device_adc, AD77681_BUFn_DISABLED);
00673             pc.printf(" Unbuffered REF-\n");
00674             break;
00675         case 4:
00676             ad77681_set_REFp_buffer(device_adc, AD77681_BUFp_DISABLED);
00677             pc.printf(" Unbuffered REF+\n");
00678             break;
00679         case 5:
00680             ad77681_set_REFn_buffer(device_adc, AD77681_BUFn_ENABLED);
00681             pc.printf(" Precharge buffer on REF-\n");
00682             break;
00683         case 6:
00684             ad77681_set_REFp_buffer(device_adc, AD77681_BUFp_ENABLED);
00685             pc.printf(" Precharge buffer on REF+\n");
00686             break;
00687         default:
00688             pc.printf(" Invalid option\n");
00689             break;
00690         }       
00691         break;      
00692     default:
00693         pc.printf(" Invalid option\n");
00694         break;
00695     }
00696     print_prompt();
00697 }
00698 
00699 /**
00700  * Default ADC Settings
00701  *
00702  */
00703  void static menu_5_set_default_settings(void)
00704  {  
00705     int32_t default_settings_flag  = ad77681_setup(&device_adc, init_params, &current_status);
00706     
00707     if (default_settings_flag == SUCCESS)
00708         pc.printf("\n Default ADC settings successfull\n");
00709     else
00710         pc.printf("\n Error in settings, please reset the ADC\n");
00711     print_prompt();
00712 }
00713 
00714 /**
00715  * VCM output controll
00716  *
00717  */
00718  void static menu_6_set_adc_vcm(void)
00719  {
00720     uint32_t new_vcm = 0;
00721     
00722     pc.printf(" Avaliable VCM output voltage levels: \n");
00723     pc.printf("  1 - VCM = (AVDD1-AVSS)/2\n");
00724     pc.printf("  2 - VCM = 2.5V\n");
00725     pc.printf("  3 - VCM = 2.05V\n");
00726     pc.printf("  4 - VCM = 1.9V\n");
00727     pc.printf("  5 - VCM = 1.65V\n");
00728     pc.printf("  6 - VCM = 1.1V\n");
00729     pc.printf("  7 - VCM = 0.9V\n");
00730     pc.printf("  8 - VCM off\n");
00731     pc.printf(" Select an option: \n");
00732     
00733     getUserInput(&new_vcm);
00734     pc.putc('\n');
00735     
00736     switch (new_vcm) {
00737     
00738     case 1:
00739         ad77681_set_VCM_output(device_adc, AD77681_VCM_HALF_VCC);
00740         pc.printf(" VCM set to half of the Vcc\n");
00741         break;
00742     case 2:
00743         ad77681_set_VCM_output(device_adc, AD77681_VCM_2_5V);
00744         pc.printf(" VCM set to 2.5V\n");
00745         break;
00746     case 3:
00747         ad77681_set_VCM_output(device_adc, AD77681_VCM_2_05V);
00748         pc.printf(" VCM set to 2.05V\n");
00749         break;
00750     case 4:
00751         ad77681_set_VCM_output(device_adc, AD77681_VCM_1_9V);
00752         pc.printf(" VCM set to 1.9V\n");
00753         break;
00754     case 5:
00755         ad77681_set_VCM_output(device_adc, AD77681_VCM_1_65V);
00756         pc.printf(" VCM set to 1.65V\n");
00757         break;
00758     case 6:
00759         ad77681_set_VCM_output(device_adc, AD77681_VCM_1_1V);
00760         pc.printf(" VCM set to 1.1V\n");
00761         break;
00762     case 7:
00763         ad77681_set_VCM_output(device_adc, AD77681_VCM_0_9V);
00764         pc.printf(" VCM set to 0.9V\n");
00765         break;      
00766     case 8:
00767         ad77681_set_VCM_output(device_adc, AD77681_VCM_OFF);
00768         pc.printf(" VCM OFF\n");
00769         break;
00770     default:
00771         pc.printf(" Invalid option\n");
00772         break;
00773     }
00774     print_prompt();
00775 }
00776 
00777 /**
00778  * Register read
00779  *
00780  */
00781  void static menu_7_adc_read_register(void)
00782  {
00783     uint32_t new_reg_to_read = 0;
00784     uint8_t reg_read_buf[3], read_adc_data[6], hex_number = 0;
00785     uint8_t HI = 0, MID = 0, LO = 0;
00786     char binary_number[8], other_register[2] = "";
00787     double voltage;
00788     
00789     pc.printf(" Read desired register: \n");
00790     pc.printf("  1 - 0x03        - Chip type\n");
00791     pc.printf("  2 - 0x14        - Interface format\n");
00792     pc.printf("  3 - 0x15        - Power clock\n");
00793     pc.printf("  4 - 0x16        - Analog\n");
00794     pc.printf("  5 - 0x17        - Analog2\n");
00795     pc.printf("  6 - 0x18        - Conversion\n");
00796     pc.printf("  7 - 0x19        - Digital filter\n");
00797     pc.printf("  8 - 0x1A        - SINC3 Dec. rate MSB\n");
00798     pc.printf("  9 - 0x1B        - SINC3 Dec. rate LSB\n");
00799     pc.printf(" 10 - 0x1C        - Duty cycle ratio\n");
00800     pc.printf(" 11 - 0x1D        - Sync, Reset\n");
00801     pc.printf(" 12 - 0x1E        - GPIO Controll\n");
00802     pc.printf(" 13 - 0x1F        - GPIO Write\n");
00803     pc.printf(" 14 - 0x20        - GPIO Read\n");
00804     pc.printf(" 15 - 0x21 - 0x23 - Offset register\n");
00805     pc.printf(" 16 - 0x24 - 0x26 - Gain register\n");
00806     pc.printf(" 17 - 0x2C        - ADC Data\n");
00807     pc.printf(" Select an option: \n");
00808     
00809     getUserInput(&new_reg_to_read);
00810     pc.putc('\n');
00811     
00812     switch (new_reg_to_read) {  
00813     case 1:
00814         ad77681_spi_reg_read(device_adc, AD77681_REG_CHIP_TYPE, reg_read_buf);
00815         print_binary(reg_read_buf[1], binary_number);
00816         pc.printf(" Value of 0x03 - Chip type register is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00817         break;
00818     case 2:
00819         ad77681_spi_reg_read(device_adc, AD77681_REG_INTERFACE_FORMAT, reg_read_buf);
00820         print_binary(reg_read_buf[1], binary_number);
00821         pc.printf(" Value of 0x14 - Interface format register is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00822         break;
00823     case 3:
00824         ad77681_spi_reg_read(device_adc, AD77681_REG_POWER_CLOCK, reg_read_buf);
00825         print_binary(reg_read_buf[1], binary_number);
00826         pc.printf(" Value of 0x15 - Power clock register is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00827         break;
00828     case 4:
00829         ad77681_spi_reg_read(device_adc, AD77681_REG_ANALOG, reg_read_buf);
00830         print_binary(reg_read_buf[1], binary_number);
00831         pc.printf(" Value of 0x16 - Anlaog register is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00832         break;
00833     case 5:
00834         ad77681_spi_reg_read(device_adc, AD77681_REG_ANALOG2, reg_read_buf);
00835         print_binary(reg_read_buf[1], binary_number);
00836         pc.printf(" Value of 0x17 - Analog2 regster is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00837         break;
00838     case 6:
00839         ad77681_spi_reg_read(device_adc, AD77681_REG_CONVERSION, reg_read_buf);
00840         print_binary(reg_read_buf[1], binary_number);
00841         pc.printf(" Value of 0x18 - Conversion register is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00842         break;
00843     case 7:
00844         ad77681_spi_reg_read(device_adc, AD77681_REG_DIGITAL_FILTER, reg_read_buf);
00845         print_binary(reg_read_buf[1], binary_number);
00846         pc.printf(" Value of 0x19 - Digital filter register is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00847         break;
00848     case 8:
00849         ad77681_spi_reg_read(device_adc, AD77681_REG_SINC3_DEC_RATE_MSB, reg_read_buf);
00850         print_binary(reg_read_buf[1], binary_number);
00851         pc.printf(" Value of 0x1A - SINC3 Dec. rate MSB is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00852         break;
00853     case 9:
00854         ad77681_spi_reg_read(device_adc, AD77681_REG_SINC3_DEC_RATE_LSB, reg_read_buf);
00855         print_binary(reg_read_buf[1], binary_number);
00856         pc.printf(" Value of 0x1B - SINC3 Dec. rate LSB is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00857         break;
00858     case 10:
00859         ad77681_spi_reg_read(device_adc, AD77681_REG_DUTY_CYCLE_RATIO, reg_read_buf);
00860         print_binary(reg_read_buf[1], binary_number);
00861         pc.printf(" Value of 0x1C - Duty cycle ratio 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00862         break;
00863     case 11:
00864         ad77681_spi_reg_read(device_adc, AD77681_REG_SYNC_RESET, reg_read_buf);
00865         print_binary(reg_read_buf[1], binary_number);
00866         pc.printf(" Value of 0x1D - Sync, Reset 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00867         break;
00868     case 12:
00869         ad77681_spi_reg_read(device_adc, AD77681_REG_GPIO_CONTROL, reg_read_buf);
00870         print_binary(reg_read_buf[1], binary_number);
00871         pc.printf(" Value of 0x1E - GPIO Controll is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00872         break;
00873     case 13:
00874         ad77681_spi_reg_read(device_adc, AD77681_REG_GPIO_WRITE, reg_read_buf);
00875         print_binary(reg_read_buf[1], binary_number);
00876         pc.printf(" Value of 0x1F - GPIO Write is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00877         break;
00878     case 14:
00879         ad77681_spi_reg_read(device_adc, AD77681_REG_GPIO_READ, reg_read_buf);
00880         print_binary(reg_read_buf[1], binary_number);
00881         pc.printf(" Value of 0x20 - GPIO Read is: 0x%x  0b%s\n", reg_read_buf[1], binary_number);
00882         break;
00883     case 15:
00884         ad77681_spi_reg_read(device_adc, AD77681_REG_OFFSET_HI, reg_read_buf);
00885         HI = reg_read_buf[1];
00886         
00887         ad77681_spi_reg_read(device_adc, AD77681_REG_OFFSET_MID, reg_read_buf);
00888         MID = reg_read_buf[1];
00889         
00890         ad77681_spi_reg_read(device_adc, AD77681_REG_OFFSET_LO, reg_read_buf);
00891         LO = reg_read_buf[1];
00892         
00893         pc.printf(" Value of 0x21 - 0x23 - Offset register is: 0x%x %x %x\n", HI, MID, LO);
00894         break;
00895         
00896     case 16:
00897         ad77681_spi_reg_read(device_adc, AD77681_REG_GAIN_HI, reg_read_buf);
00898         HI = reg_read_buf[1];
00899         
00900         ad77681_spi_reg_read(device_adc, AD77681_REG_GAIN_MID, reg_read_buf);
00901         MID = reg_read_buf[1];
00902         
00903         ad77681_spi_reg_read(device_adc, AD77681_REG_GAIN_LO, reg_read_buf);
00904         LO = reg_read_buf[1];
00905         
00906         pc.printf(" Value of 0x24 - 0x26 - Gain register is: 0x%x %x %x\n", HI, MID, LO);
00907         break;  
00908     case 17:
00909         ad77681_spi_read_adc_data(device_adc, read_adc_data, AD77681_REGISTER_DATA_READ);
00910         pc.printf(" Value of 0x2C - ADC data is: 0x%x 0x%x 0x%x\n", read_adc_data[1], read_adc_data[2], read_adc_data[3]);
00911         break;  
00912 
00913     default :
00914         pc.printf(" Invalid option\n");
00915         break;
00916     }
00917     print_prompt();
00918 }
00919 
00920 /**
00921  * Read ADC data
00922  *
00923  */
00924 void static menu_8_adc_cont_read_data(void)
00925 {
00926     uint32_t new_sample_count = 0;
00927     int32_t ret;
00928 
00929     pc.printf(" Read Continuous ADC Data");
00930     pc.printf("  Input number of samples (1 to 4096): \n");
00931     ret = getUserInput(&new_sample_count);                  // Get user input
00932 
00933     if ((new_sample_count <= 4096) && (ret == SUCCESS) ) {
00934         pc.printf("\n%d of samples\n", new_sample_count);   // Print Desired Measurement Count
00935         measured_data.samples = (uint16_t)(new_sample_count);
00936         measured_data.finish = false;
00937         measured_data.count = 0;
00938         pc.printf("Sampling....\n");
00939         cont_sampling();
00940         pc.printf("Done Sampling....\n");
00941     } else {
00942         pc.printf(" Invalid option\n");
00943     }
00944     print_prompt();
00945 }
00946 
00947 /**
00948  * ADC Continuous read
00949  *
00950  */
00951 void static cont_sampling()
00952 {      
00953     uint8_t buf[6];
00954      
00955     ad77681_set_continuos_read(device_adc, AD77681_CONTINUOUS_READ_ENABLE);
00956     __enable_irq();                                             // Enable all interupts
00957     drdy.enable_irq();                                          // Enable interrupt on DRDY pin
00958     drdy.fall(&drdy_interrupt);                                 // Interrupt on falling edne of DRDY
00959 
00960     while (!measured_data.finish) { // While loop. Waiting for the measurements to be completed
00961         if (int_event==true) {      // Checks if Interrupt Occurred
00962             ad77681_spi_read_adc_data(device_adc, buf, AD77681_CONTINUOUS_DATA_READ);    // Read the continuous read data
00963             if (device_adc->conv_len == AD77681_CONV_24BIT)                                                 // 24bit format
00964                 measured_data.raw_data[measured_data.count] = (buf[0] << 16 | buf[1] << 8 | buf[2]<< 0);    // Combining the SPI buffers
00965             else                                                                                            // 16bit format
00966                 measured_data.raw_data[measured_data.count] = (buf[0] << 8 | buf[1]<< 0);                   // Combining the SPI buffers
00967             measured_data.count++;  // Increment Measured Data Counter
00968             int_event=false;        // Set int event flag after reading the Data
00969         }
00970     }
00971     ad77681_set_continuos_read(device_adc, AD77681_CONTINUOUS_READ_DISABLE);    // Disable continuous ADC read
00972 }
00973 
00974 /**
00975  * Reset ADC
00976  *
00977  */
00978  void static menu_9_reset_ADC(void)
00979  {
00980     uint32_t new_reset_option = 0;
00981     
00982     pc.printf(" ADC reset opportunities: \n");
00983     pc.printf("  1 - Soft reset - over SPI\n");
00984     pc.printf("  2 - Hard reset - uing RESET pin\n");
00985     pc.printf(" Select an option: \n");
00986         
00987     getUserInput(&new_reset_option);
00988     pc.putc('\n');
00989     
00990     switch (new_reset_option) {
00991     case 1:
00992         ad77681_soft_reset(device_adc);             // Perform soft reset thru SPI write
00993         pc.printf(" ADC after soft reset\n");
00994         break;
00995     case 2:
00996         adc_hard_reset();
00997         pc.printf(" ADC after hard reset\n");
00998         break;
00999     default:
01000         pc.printf(" Invalid option\n");
01001         break;
01002     }
01003     print_prompt();
01004 }
01005 
01006 /**
01007  * Reset ADC thru SDP-K1 GPIO
01008  * 
01009  * 
01010  */
01011  void static adc_hard_reset()
01012 {
01013     adc_reset = 0;  // Set ADC reset pin to Low
01014     mdelay(100);    // Delay 100ms
01015     adc_reset = 1;  // Set ADC reset pin to High
01016     mdelay(100);    // Delay 100ms
01017 }
01018 
01019 /**
01020  * Sleep mode / Wake up ADC
01021  *
01022  */
01023  void static menu_10_power_down(void)
01024  {
01025     uint32_t new_sleep = 0;
01026     
01027     pc.printf(" Controll sleep mode of the ADC: \n");
01028     pc.printf("  1 - Put ADC to sleep mode\n");
01029     pc.printf("  2 - Wake up ADC\n");
01030     pc.printf(" Select an option: \n");
01031         
01032     getUserInput(&new_sleep);
01033     pc.putc('\n');
01034     
01035     switch (new_sleep) {
01036     case 1:
01037         ad77681_power_down(device_adc, AD77681_SLEEP);
01038         pc.printf(" ADC put to sleep mode\n");
01039         break;
01040     case 2:
01041         ad77681_power_down(device_adc, AD77681_WAKE);
01042         pc.printf(" ADC powered\n");
01043         break;
01044     default:
01045         pc.printf("Invalid option\n");
01046         break;          
01047     }
01048     print_prompt();
01049 }
01050 
01051 /**
01052  * ADC's GPIO Controll
01053  *
01054  */
01055  void static menu_11_ADC_GPIO(void) 
01056  {
01057     uint8_t GPIO_state;
01058     uint32_t new_gpio_sel = 0;
01059     char binary_number[8];
01060     int32_t ret_val = FAILURE, ret;
01061     
01062     pc.printf(" ADC GPIO Controll: \n");
01063     pc.printf("  1 - Read from GPIO\n");
01064     pc.printf("  2 - Write to  GPIO\n");
01065     pc.printf("  3 - Set GPIO as input / output\n");
01066     pc.printf("  4 - Change GPIO settings\n");
01067     pc.printf(" Select an option: \n");
01068     
01069     getUserInput(&new_gpio_sel);
01070     pc.putc('\n');
01071     
01072     switch (new_gpio_sel) {     
01073     case 1:
01074         ad77681_gpio_read(device_adc, &GPIO_state, AD77681_ALL_GPIOS);
01075         print_binary(GPIO_state, binary_number);
01076         pc.printf(" Current GPIO Values:\n GPIO0: %c\n GPIO1: %c\n GPIO2: %c\n GPIO3: %c\n", binary_number[7], binary_number[6], binary_number[5], binary_number[4]);
01077         break;      
01078     case 2:
01079         adc_GPIO_write();
01080         break;  
01081     case 3:
01082         adc_GPIO_inout();       
01083         break;          
01084     case 4:
01085         adc_GPIO_settings();        
01086         break;      
01087     default:
01088         pc.printf(" Invalid option\n");
01089         break;  
01090     }
01091     print_prompt();
01092 }
01093 
01094 /**
01095  * Write to GPIOs, part of the ADC_GPIO function
01096  *
01097  */
01098 void static adc_GPIO_write(void)
01099 {
01100     uint32_t new_gpio_write = 0, new_value = 0;
01101     int32_t ret, ret_val;
01102     
01103     pc.printf(" Write to GPIO: \n");
01104     pc.printf("  1 - Write to all GPIOs\n");
01105     pc.printf("  2 - Write to GPIO0\n");
01106     pc.printf("  3 - Write to GPIO1\n");
01107     pc.printf("  4 - Write to GPIO2\n");
01108     pc.printf("  5 - Write to GPIO3\n");
01109     pc.printf(" Select an option: \n");
01110         
01111     getUserInput(&new_gpio_write);      
01112     pc.putc('\n');
01113         
01114     switch (new_gpio_write)
01115     {
01116     case 1:
01117         pc.printf("Insert value to be writen into all GPIOs, same value for all GPIOs: ");
01118         ret = getUserInput(&new_value);
01119             
01120         if (((new_value == GPIO_HIGH) || (new_value == GPIO_LOW)) && (ret == SUCCESS)) {
01121             new_value *= 0xF;
01122             ret_val = ad77681_gpio_write(device_adc, new_value, AD77681_ALL_GPIOS);
01123             pc.printf("\n Value %d successully written to all GPOIs\n", new_value);
01124         } else
01125             pc.printf("\nInvalid value\n");             
01126         break;          
01127     case 2:
01128         pc.printf("Insert value to be written into GPIO0: ");
01129         ret = getUserInput(&new_value); 
01130             
01131         if (((new_value == GPIO_HIGH) || (new_value == GPIO_LOW)) && (ret == SUCCESS)) {
01132             ret_val = ad77681_gpio_write(device_adc, new_value, AD77681_GPIO0);
01133             pc.printf("\n Value %d successully written to GPIO0\n", new_value);
01134         } else
01135             pc.printf("\nInvalid value\n");         
01136         break;          
01137     case 3:
01138         pc.printf("Insert value to be written into GPIO1: ");
01139         ret = getUserInput(&new_value);         
01140             
01141         if (((new_value == GPIO_HIGH) || (new_value == GPIO_LOW)) && (ret == SUCCESS)) {
01142             ret_val = ad77681_gpio_write(device_adc, new_value, AD77681_GPIO1);
01143             pc.printf("\n Value %d successully written to GPIO1\n", new_value);
01144         } else
01145             pc.printf("\nInvalid value\n");         
01146         break;          
01147     case 4:
01148         pc.printf("Insert value to be written into GPIO2: ");
01149         ret = getUserInput(&new_value); 
01150             
01151         if (((new_value == GPIO_HIGH) || (new_value == GPIO_LOW)) && (ret == SUCCESS)) {
01152             ret_val = ad77681_gpio_write(device_adc, new_value, AD77681_GPIO2);
01153             pc.printf("\n Value %d successully written to GPIO2\n", new_value);
01154         } else
01155             pc.printf("\nInvalid value\n");         
01156         break;          
01157     case 5:
01158         pc.printf("Insert value to be written into GPIO3: ");
01159         ret = getUserInput(&new_value); 
01160             
01161         if (((new_value == GPIO_HIGH) || (new_value == GPIO_LOW)) && (ret == SUCCESS)) {
01162             ret_val = ad77681_gpio_write(device_adc, new_value, AD77681_GPIO3);
01163             pc.printf("\n Value %d successully written to GPIO3\n", new_value);
01164         } else
01165             pc.printf("\nInvalid value\n");         
01166         break;          
01167     default:
01168         pc.printf(" Invalid option\n");
01169         break;
01170     }   
01171 }
01172 
01173 /**
01174  * GPIO direction, part of the ADC_GPIO function
01175  *
01176  */
01177 void static adc_GPIO_inout(void)
01178 {
01179     uint32_t new_gpio_inout = 0, new_gpio_inout_set = 0;
01180     int32_t ret_val;
01181     
01182     pc.printf(" Set GPIOs as input or output: \n");
01183     pc.printf("  1 - Set all GPIOs\n");
01184     pc.printf("  2 - Set GPIO0\n");
01185     pc.printf("  3 - Set GPIO1\n");
01186     pc.printf("  4 - Set GIPO2\n");
01187     pc.printf("  5 - Set GPIO3\n");
01188     pc.printf(" Select an option: \n");
01189         
01190     getUserInput(&new_gpio_inout);  
01191     pc.putc('\n');
01192         
01193     switch (new_gpio_inout) {
01194     case 1:
01195         pc.printf("   1 - Set all GPIOS as inputs\n");
01196         pc.printf("   2 - Set all GPIOS as outputs\n");
01197             
01198         getUserInput(&new_gpio_inout_set);  
01199         pc.putc('\n');          
01200             
01201         if ((new_gpio_inout_set == 1) || (new_gpio_inout_set == 2)) {
01202             new_gpio_inout_set -= 1;
01203             new_gpio_inout_set *= 0xF;
01204             ret_val = ad77681_gpio_inout(device_adc, new_gpio_inout_set, AD77681_ALL_GPIOS);
01205             pc.printf("All GPIOs successfully set");
01206         } else
01207             pc.printf("\nInvalid value\n");         
01208         break;
01209     case 2:
01210         pc.printf("   1 - Set GPIO0 as input\n");
01211         pc.printf("   2 - Set GPIO0 as output\n");
01212             
01213         getUserInput(&new_gpio_inout_set);  
01214         pc.putc('\n');  
01215             
01216         if ((new_gpio_inout_set == 1) || (new_gpio_inout_set == 2)) {
01217             new_gpio_inout_set -= 1;
01218             ret_val = ad77681_gpio_inout(device_adc, new_gpio_inout_set, AD77681_GPIO0);
01219             pc.printf("GPIO0 successfully set");
01220         } else
01221             pc.printf("\nInvalid value\n");         
01222         break;          
01223     case 3:
01224         pc.printf("   1 - Set GPIO1 as input\n");
01225         pc.printf("   2 - Set GPIO1 as output\n");
01226             
01227         getUserInput(&new_gpio_inout_set);  
01228         pc.putc('\n');  
01229             
01230         if ((new_gpio_inout_set == 1) || (new_gpio_inout_set == 2)) {
01231             new_gpio_inout_set -= 1;
01232             ret_val = ad77681_gpio_inout(device_adc, new_gpio_inout_set, AD77681_GPIO1);
01233             pc.printf("GPIO1 successfully set");
01234         } else
01235             pc.printf("\nInvalid value\n");         
01236         break;          
01237     case 4:
01238         pc.printf("   1 - Set GPIO2 as input\n");
01239         pc.printf("   2 - Set GPIO2 as output\n");
01240             
01241         getUserInput(&new_gpio_inout_set);  
01242         pc.putc('\n');      
01243             
01244         if ((new_gpio_inout_set == 1) || (new_gpio_inout_set == 2)) {
01245             new_gpio_inout_set -= 1;
01246             ret_val = ad77681_gpio_inout(device_adc, new_gpio_inout_set, AD77681_GPIO2);
01247             pc.printf("GPIO2 successfully set");
01248         } else
01249             pc.printf("\nInvalid value\n");
01250         break;      
01251     case 5:
01252         pc.printf("   1 - Set GPIO3 as input\n");
01253         pc.printf("   2 - Set GPIO3 as output\n");
01254             
01255         getUserInput(&new_gpio_inout_set);  
01256         pc.putc('\n');  
01257             
01258         if ((new_gpio_inout_set == 1) || (new_gpio_inout_set == 2)) {
01259             new_gpio_inout_set -= 1;
01260             ret_val = ad77681_gpio_inout(device_adc, new_gpio_inout_set, AD77681_GPIO3);
01261             pc.printf("GPIO3 successfully set");
01262         } else
01263             pc.printf("\nInvalid value\n");
01264         break;              
01265     default:
01266         pc.printf(" Invalid option\n");
01267         break;  
01268     }
01269 }
01270 
01271 /**
01272  * Additional GPIO settings, part of the ADC_GPIO function
01273  *
01274  */
01275 void static adc_GPIO_settings(void)
01276 {
01277     uint32_t new_gpio_settings = 0;
01278     
01279     pc.printf(" GPIO Settings: \n");
01280     pc.printf("  1 - Enable  all GPIOs (Global enble)\n"); 
01281     pc.printf("  2 - Disable all GPIOs (Global disable)\n");
01282     pc.printf("  3 - Set GPIO0 - GPIO2 as open drain\n");
01283     pc.printf("  4 - Set GPIO0 - GPIO2 as strong driver\n");
01284     pc.printf(" Select an option: \n");
01285         
01286     getUserInput(&new_gpio_settings);   
01287     pc.putc('\n');
01288         
01289     switch (new_gpio_settings) {
01290     case 1:
01291         ad77681_global_gpio(device_adc, AD77681_GLOBAL_GPIO_ENABLE);
01292         pc.printf(" Global GPIO enalbe bit enabled");
01293         break;
01294     case 2:
01295         ad77681_global_gpio(device_adc, AD77681_GLOBAL_GPIO_DISABLE);
01296         pc.printf(" Global GPIO enalbe bit disabled");
01297         break;
01298     default:
01299         pc.printf(" Invalid option\n");
01300         break;  
01301     }
01302 }
01303 
01304 /**
01305  * Read ADC status from status registers
01306  *
01307  */
01308  void static menu_12_read_master_status(void)
01309  {  
01310     uint8_t reg_read_buf[3];
01311     char binary_number[8];
01312     char ok[3] = { 'O', 'K' }, fault[6] = { 'F', 'A', 'U', 'L', 'T' };
01313     
01314     ad77681_status(device_adc, current_status);
01315     pc.putc('\n');
01316     pc.printf("== MASTER STATUS REGISER\n");
01317     pc.printf("Master error:          %s\n", ((current_status->master_error == 0) ? (ok) : (fault)));
01318     pc.printf("ADC error:             %s\n", ((current_status->adc_error == 0) ? (ok) : (fault)));
01319     pc.printf("Dig error:             %s\n", ((current_status->dig_error == 0) ? (ok) : (fault)));
01320     pc.printf("Ext. clock:            %s\n", ((current_status->adc_err_ext_clk_qual == 0) ? (ok) : (fault)));
01321     pc.printf("Filter saturated:      %s\n", ((current_status->adc_filt_saturated == 0) ? (ok) : (fault)));
01322     pc.printf("Filter not settled:    %s\n", ((current_status->adc_filt_not_settled == 0) ? (ok) : (fault)));
01323     pc.printf("SPI error:             %s\n", ((current_status->spi_error == 0) ? (ok) : (fault)));
01324     pc.printf("POR Flag:              %s\n", ((current_status->por_flag == 0) ? (ok) : (fault)));
01325     
01326     if (current_status->spi_error == 1) {
01327         pc.printf("\n== SPI DIAG STATUS REGISER\n");
01328         pc.printf("SPI ignore error:      %s\n", ((current_status->spi_ignore == 0) ? (ok) : (fault)));
01329         pc.printf("SPI clock count error: %s\n", ((current_status->spi_clock_count == 0) ? (ok) : (fault)));
01330         pc.printf("SPI read error:        %s\n", ((current_status->spi_read_error == 0) ? (ok) : (fault)));
01331         pc.printf("SPI write error:       %s\n", ((current_status->spi_write_error == 0) ? (ok) : (fault)));
01332         pc.printf("SPI CRC error:         %s\n", ((current_status->spi_crc_error == 0) ? (ok) : (fault)));
01333     }
01334     if (current_status->adc_error == 1) {
01335         pc.printf("\n== ADC DIAG STATUS REGISER\n");
01336         pc.printf("DLDO PSM error:        %s\n", ((current_status->dldo_psm_error == 0) ? (ok) : (fault)));
01337         pc.printf("ALDO PSM error:        %s\n", ((current_status->aldo_psm_error == 0) ? (ok) : (fault)));
01338         pc.printf("REF DET error:         %s\n", ((current_status->ref_det_error == 0) ? (ok) : (fault)));
01339         pc.printf("FILT SAT error:        %s\n", ((current_status->filt_sat_error == 0) ? (ok) : (fault)));
01340         pc.printf("FILT NOT SET error:    %s\n", ((current_status->filt_not_set_error == 0) ? (ok) : (fault)));
01341         pc.printf("EXT CLK QUAL error:    %s\n", ((current_status->ext_clk_qual_error == 0) ? (ok) : (fault)));
01342     }   
01343     if (current_status->dig_error == 1) {
01344         pc.printf("\n== DIGITAL DIAG STATUS REGISER\n");
01345         pc.printf("Memory map CRC error:  %s\n", ((current_status->memoy_map_crc_error == 0) ? (ok) : (fault)));
01346         pc.printf("RAM CRC error:         %s\n", ((current_status->ram_crc_error == 0) ? (ok) : (fault)));
01347         pc.printf("FUSE CRC error:        %s\n", ((current_status->fuse_crc_error == 0) ? (ok) : (fault)));
01348     }   
01349     pc.putc('\n');
01350     print_prompt();
01351 }
01352 
01353 /**
01354  * Set Vref anc MCLK as "exteranl" values, depending on you setup
01355  *
01356  */
01357  void static menu_13_mclk_vref(void)
01358  {
01359     uint32_t input = 0, new_settings = 0;
01360     int32_t ret;
01361         
01362     pc.printf(" Set Vref and Mclk: \n");                                
01363     pc.printf("  1 - Change Vref\n"); 
01364     pc.printf("  2 - Change MCLK\n");
01365     pc.printf(" Select an option: \n");
01366     
01367     getUserInput(&new_settings);    
01368     pc.putc('\n');
01369     
01370     switch (new_settings) {
01371     case 1:
01372         pc.printf(" Change Vref from %d mV to [mV]: ", device_adc->vref);       // Vref change
01373         ret = getUserInput(&input); 
01374                 
01375         if ((input >= 1000) && (input <= 5000) && (ret == SUCCESS)) {
01376             pc.printf("\n New Vref value is %d mV", input);
01377             device_adc->vref = input;
01378             
01379             #ifdef CN0540_ADI_FFT_H_
01380             // Update the Vref, Mclk and sampling rate
01381             update_FFT_enviroment(device_adc->vref, device_adc->mclk, device_adc->sample_rate, FFT_data); 
01382             #endif //CN0540_ADI_FFT_H_         
01383         } else
01384             pc.printf(" Invalid option\n"); 
01385         pc.putc('\n');
01386         break;      
01387     case 2:
01388         pc.printf(" Change MCLK from %d kHz to [kHz]: ", device_adc->mclk);     // MCLK change
01389         ret = getUserInput(&input);             
01390         
01391         if ((input >= 10000) && (input <= 50000) && (ret == SUCCESS)){
01392             pc.printf("\n New MCLK value is %d kHz\n", input);
01393             device_adc->vref = input;
01394             ad77681_update_sample_rate(device_adc);                             // Update the sample rate after changinig the MCLK
01395             
01396             #ifdef CN0540_ADI_FFT_H_
01397             // Update the Vref, Mclk and sampling rate
01398             update_FFT_enviroment(device_adc->vref, device_adc->mclk, device_adc->sample_rate, FFT_data); 
01399             #endif //CN0540_ADI_FFT_H_
01400         } else
01401             pc.printf(" Invalid option\n"); 
01402         pc.putc('\n');
01403         break;
01404     default:
01405         pc.printf(" Invalid option\n");
01406         break;
01407     }
01408     print_prompt();
01409 }
01410 
01411 /**
01412  * Print measured data and transfered to voltage
01413  *
01414  */
01415  void static menu_14_print_measured_data(void)
01416  {
01417     double voltage;
01418     int32_t shifted_data;
01419     uint16_t i;
01420     char buf[15];
01421 
01422     if (measured_data.finish) {
01423         // Printing Voltage
01424         pc.printf("\n\nVoltage\n");
01425         for ( i = 0; i < measured_data.samples; i++) {
01426             ad77681_data_to_voltage(device_adc, &measured_data.raw_data[i], &voltage);
01427             pc.printf("%.9f \n",voltage);
01428         }
01429         // Printing Codes
01430         pc.printf("\n\nCodes\n");
01431         for(i = 0 ; i < measured_data.samples ; i++) {
01432             if (measured_data.raw_data[i] & 0x800000)
01433                 shifted_data = (int32_t)((0xFF << 24) | measured_data.raw_data[i]);
01434             else
01435                 shifted_data = (int32_t)((0x00 << 24) | measured_data.raw_data[i]);
01436             pc.printf("%d\n", shifted_data + AD7768_HALF_SCALE);
01437         }
01438         // Printing Raw Date
01439         pc.printf("\n\nRaw data\n");
01440         for (i = 0; i < measured_data.samples; i++)
01441             pc.printf("%d\n", measured_data.raw_data[i]);
01442         // Set  measured_data.finish to false after Printing
01443         measured_data.finish = false;
01444     } else
01445         pc.printf("Data not prepared\n");
01446     print_prompt();
01447 }
01448 
01449 /**
01450  * Set data output mode
01451  *
01452  */
01453 void static menu_15_set_adc_data_output_mode(void) 
01454 {
01455     uint32_t new_data_mode = 0, new_length = 0, new_status = 0, new_crc = 0, ret;
01456     
01457     pc.printf(" ADC data outpup modes: \n");
01458     pc.printf("  1 - Continuous: waiting for DRDY\n");
01459     pc.printf("  2 - Continuous one shot: waiting for SYNC_IN\n");
01460     pc.printf("  3 - Single-conversion standby\n");
01461     pc.printf("  4 - Periodic standby\n");
01462     pc.printf("  5 - Standby mode\n");
01463     pc.printf("  6 - 16bit or 24bit data format\n");
01464     pc.printf("  7 - Status bit output\n");
01465     pc.printf("  8 - Switch form diag mode to measure\n");
01466     pc.printf("  9 - Switch form measure to diag mode\n");
01467     pc.printf(" 10 - Set CRC type\n");
01468     pc.printf(" Select an option: \n");
01469     
01470     getUserInput(&new_data_mode);
01471     pc.putc('\n');
01472     
01473     switch (new_data_mode) {
01474     case 1:
01475         ad77681_set_conv_mode(device_adc, AD77681_CONV_CONTINUOUS, device_adc->diag_mux_sel, device_adc->conv_diag_sel);// DIAG MUX NOT SELECTED
01476         pc.printf(" Continuous mode set\n");
01477         break;
01478     case 2:
01479         ad77681_set_conv_mode(device_adc, AD77681_CONV_ONE_SHOT, device_adc->diag_mux_sel, device_adc->conv_diag_sel);
01480         pc.printf(" Continuous one shot conversion set\n");
01481         break;
01482     case 3:
01483         ad77681_set_conv_mode(device_adc, AD77681_CONV_SINGLE, device_adc->diag_mux_sel, device_adc->conv_diag_sel);
01484         pc.printf(" Single conversion standby mode set\n");
01485         break;  
01486     case 4:
01487         ad77681_set_conv_mode(device_adc, AD77681_CONV_PERIODIC, device_adc->diag_mux_sel, device_adc->conv_diag_sel);
01488         pc.printf(" Periodiec standby mode set\n");
01489         break;  
01490     case 5:
01491         ad77681_set_conv_mode(device_adc, AD77681_CONV_STANDBY, device_adc->diag_mux_sel, device_adc->conv_diag_sel);
01492         pc.printf(" Standby mode set\n");
01493         break;
01494     case 6:
01495         pc.printf(" Conversion length select: \n");
01496         pc.printf("  1 - 24bit length\n");
01497         pc.printf("  2 - 16bit length\n");
01498         
01499         getUserInput(&new_length);
01500         pc.putc('\n');
01501         
01502         switch (new_length) {
01503         case 1:
01504             ad77681_set_convlen(device_adc, AD77681_CONV_24BIT);
01505             pc.printf(" 24bit data output format selected\n");
01506             break;
01507         case 2:
01508             ad77681_set_convlen(device_adc, AD77681_CONV_16BIT);
01509             pc.printf(" 16bit data output format selected\n");
01510             break;
01511         default:
01512             pc.printf(" Invalid option\n");
01513             break;
01514         }
01515         break;
01516     case 7:
01517         pc.printf(" Status bit output: \n");
01518         pc.printf("  1 - Enable status bit after each ADC conversion\n");
01519         pc.printf("  2 - Disable status bit after each ADC conversion\n");
01520         
01521         getUserInput(&new_status);
01522         pc.putc('\n');
01523         
01524         switch (new_status) {
01525         case 1:
01526             ad77681_set_status_bit(device_adc, true);
01527             pc.printf(" Status bit enabled\n");
01528             break;
01529         case 2:
01530             ad77681_set_status_bit(device_adc, false);
01531             pc.printf(" Status bit disabled\n");
01532             break;
01533         default:
01534             pc.printf(" Invalid option\n");
01535             break;
01536         }       
01537         break;
01538     case 8:
01539         ad77681_set_conv_mode(device_adc, device_adc->conv_mode, device_adc->diag_mux_sel, false);// DIAG MUX NOT SELECTED
01540         pc.printf(" Measure mode selected\n");
01541         break;
01542     case 9:
01543         ad77681_set_conv_mode(device_adc, device_adc->conv_mode, device_adc->diag_mux_sel, true); // DIAG MUX SELECTED
01544         pc.printf(" Diagnostic mode selected\n");
01545         break;
01546     case 10:
01547         pc.printf(" CRC settings \n");
01548         pc.printf("  1 - Disable CRC\n");
01549         pc.printf("  2 - 8-bit polynomial CRC\n");
01550         pc.printf("  3 - XOR based CRC\n");
01551         
01552         getUserInput(&new_crc);
01553         pc.putc('\n');
01554         
01555         switch (new_crc) {
01556         case 1:
01557             ad77681_set_crc_sel(device_adc, AD77681_NO_CRC);
01558             pc.printf(" CRC disabled\n");
01559             break;
01560         case 2:
01561             ad77681_set_crc_sel(device_adc, AD77681_CRC);
01562             pc.printf("  8-bit polynomial CRC method selected\n");
01563             break;
01564         case 3:
01565             ad77681_set_crc_sel(device_adc, AD77681_XOR);
01566             pc.printf("  XOR based CRC method selected\n");
01567             break;
01568         default:
01569             pc.printf(" Invalid option\n");
01570             break;
01571         }
01572         break;      
01573     default:
01574         pc.printf(" Invalid option\n");
01575         break;
01576     }
01577     print_prompt();
01578 }
01579 
01580 /**
01581  * Set diagnostic mode
01582  *
01583  */
01584 void static menu_16_set_adc_diagnostic_mode(void)
01585 {
01586     uint32_t new_diag_mode = 0; 
01587         
01588     pc.printf(" ADC diagnostic modes: \n");
01589     pc.printf("  1 - Internal temperature sensor\n");
01590     pc.printf("  2 - AIN shorted\n");
01591     pc.printf("  3 - Positive full-scale\n");
01592     pc.printf("  4 - Negative full-scale\n");
01593     pc.printf(" Select an option: \n");
01594     
01595     getUserInput(&new_diag_mode);
01596     pc.putc('\n');
01597     
01598     switch (new_diag_mode) {
01599     case 1:
01600         ad77681_set_conv_mode(device_adc, device_adc->conv_mode, AD77681_TEMP_SENSOR, true);          
01601         pc.printf(" Diagnostic mode: Internal temperature sensor selected\n");
01602         break;
01603     case 2:
01604         ad77681_set_conv_mode(device_adc, device_adc->conv_mode, AD77681_AIN_SHORT, true);
01605         pc.printf(" Diagnostic mode: AIN shorted selected\n");
01606         break;
01607     case 3:
01608         ad77681_set_conv_mode(device_adc, device_adc->conv_mode, AD77681_POSITIVE_FS, true);
01609         pc.printf(" Diagnostic mode: Positive full-scale selected\n");
01610         break;  
01611     case 4:
01612         ad77681_set_conv_mode(device_adc, device_adc->conv_mode, AD77681_NEGATIVE_FS, true);
01613         pc.printf(" Diagnostic mode: Negative full-scale selected\n");
01614         break;  
01615     default:
01616         pc.printf(" Invalid option\n");
01617         break;
01618     }
01619     print_prompt();
01620 }
01621 
01622 /**
01623  * Do the FFT
01624  *
01625  */
01626 void static menu_17_do_the_fft(void)
01627 {
01628     pc.printf(" FFT in progress...\n");
01629     measured_data.samples = FFT_data->fft_length * 2;
01630     measured_data.samples = 4096;
01631     measured_data.finish = false;
01632     measured_data.count = 0;
01633     pc.printf("Sampling....\n");
01634     cont_sampling();
01635     perform_FFT(measured_data.raw_data, FFT_data, FFT_meas, device_adc->sample_rate);
01636     pc.printf(" FFT Done!\n");
01637     measured_data.finish = false;   
01638     
01639     pc.printf("\n THD:\t\t%.3f dB", FFT_meas->THD);
01640     pc.printf("\n SNR:\t\t%.3f dB", FFT_meas->SNR);
01641     pc.printf("\n DR:\t\t%.3f dB", FFT_meas->DR);
01642     pc.printf("\n Fundamental:\t%.3f dBFS", FFT_meas->harmonics_mag_dbfs[0]);
01643     pc.printf("\n Fundamental:\t%.3f Hz", FFT_meas->harmonics_freq[0]*FFT_data->bin_width);
01644     pc.printf("\n RMS noise:\t%.6f uV", FFT_meas->RMS_noise * 1000000.0);
01645     pc.printf("\n LSB noise:\t%.3f", FFT_meas->transition_noise_LSB);
01646     
01647     print_prompt(); 
01648 }
01649 
01650 /**
01651  * Setting of the FFT module
01652  *
01653  */
01654 void static menu_18_fft_settings(void)
01655 {
01656     uint32_t new_menu_select, new_window, new_sample_count;
01657     
01658     pc.printf(" FFT settings: \n");
01659     pc.printf("  1 - Set window type\n");
01660     pc.printf("  2 - Set sample count\n");
01661     pc.printf("  3 - Print FFT plot\n");
01662     pc.printf(" Select an option: \n\n");
01663     
01664     getUserInput(&new_menu_select);
01665     
01666     switch (new_menu_select) {
01667     case 1:
01668         pc.printf(" Choose window type:\n");
01669         pc.printf("  1 - 7-term Blackman-Harris\n");
01670         pc.printf("  2 - Rectangular\n");
01671         
01672         getUserInput(&new_window);
01673         
01674         switch (new_window) {
01675         case 1:
01676             pc.printf("  7-7-term Blackman-Harris window selected\n");
01677             FFT_data->window = BLACKMAN_HARRIS_7TERM;
01678             break;          
01679         case 2:
01680             pc.printf("  Rectalngular window selected\n");
01681             FFT_data->window = RECTANGULAR;
01682             break;          
01683         default:
01684             pc.printf(" Invalid option\n");
01685             break;
01686         }       
01687         break;  
01688     case 2:
01689         pc.printf(" Set sample count:\n");
01690         pc.printf("  1 - 4096 samples\n");
01691         pc.printf("  2 - 1024 samples\n");
01692         pc.printf("  3 - 256  samples\n");
01693         pc.printf("  4 - 64   samples\n");
01694         pc.printf("  5 - 16   samples\n");
01695         
01696         getUserInput(&new_sample_count);
01697         
01698         switch (new_sample_count) {
01699         case 1:
01700             pc.printf(" 4096 samples selected\n");
01701             FFT_init(4096, FFT_data);   // Update the FFT module with a new sample count
01702             break;
01703         case 2:
01704             pc.printf(" 1024 samples selected\n");
01705             FFT_init(1024, FFT_data);
01706             break;
01707         case 3:
01708             pc.printf(" 256 samples selected\n");
01709             FFT_init(256, FFT_data);
01710             break;
01711         case 4:
01712             pc.printf(" 64 samples selected\n");
01713             FFT_init(64, FFT_data);
01714             break;
01715         case 5:
01716             pc.printf(" 16 samples selected\n");
01717             FFT_init(16, FFT_data);
01718             break;
01719         default:
01720             pc.printf(" Invalid option\n");
01721             break;  
01722         }       
01723         break;      
01724     case 3:
01725         if (FFT_data->fft_done == true) {
01726             pc.printf(" Printing FFT plot in dB:\n");
01727             
01728             for (uint16_t i = 0; i < FFT_data->fft_length; i++)
01729                 pc.printf("%.4f\n", FFT_data->fft_dB[i]);
01730         }
01731         else
01732             pc.printf(" Data not prepared\n");
01733         break;      
01734     default:
01735         pc.printf(" Invalid option\n");
01736         break;  
01737     }
01738     print_prompt(); 
01739 }
01740 
01741 /**
01742  * Set Gains and Offsets
01743  *
01744  */
01745 void static menu_19_gains_offsets(void)
01746 {
01747     uint32_t gain_offset, new_menu_select;
01748     int32_t ret;
01749     
01750     pc.printf(" Gains and Offsets settings: \n");
01751     pc.printf("  1 - Set gain\n");
01752     pc.printf("  2 - Set offset\n");
01753     pc.printf(" Select an option: \n");
01754     
01755     getUserInput(&new_menu_select);
01756     
01757     switch (new_menu_select) {
01758     case 1:
01759         pc.printf(" Insert new Gain value in decimal form\n");
01760         ret = getUserInput(&gain_offset);
01761         
01762         if ((gain_offset <= 0xFFFFFF) && (ret == SUCCESS)) {
01763             ad77681_apply_gain(device_adc, gain_offset);
01764             pc.printf(" Value %d has been successfully inserted to the Gain register\n", gain_offset);
01765         } else
01766             pc.printf(" Invalid value\n");      
01767         break;  
01768     case 2:
01769         pc.printf(" Insert new Offset value in decimal form\n");
01770         ret = getUserInput(&gain_offset);       
01771         if ((gain_offset <= 0xFFFFFF) && (ret == SUCCESS)) {
01772             ad77681_apply_offset(device_adc, gain_offset);
01773             pc.printf(" Value %d has been successfully inserted to the Offset register\n", gain_offset);
01774         } else
01775             pc.printf(" Invalid value\n");      
01776         break;      
01777     default:
01778         pc.printf(" Invalid option\n");
01779         break;  
01780     }   
01781     print_prompt();     
01782 }
01783 
01784 /**
01785  * Chceck read and write functionaity by writing to and reading from scratchpad register
01786  *
01787  */
01788 void static menu_20_check_scratchpad(void)
01789 {
01790     int32_t ret;
01791     uint32_t ret_val;
01792     uint32_t new_menu_select;
01793     uint8_t chceck_sequence;
01794     
01795     pc.printf(" Scratchpad check\n");
01796     pc.printf("  Insert 8bit number for scratchpad check: \n");
01797     
01798     ret = getUserInput(&new_menu_select);
01799     
01800     if ((new_menu_select <= 0xFF) && (new_menu_select >= 0) && (ret == SUCCESS)) {
01801         chceck_sequence = (uint8_t)(new_menu_select);
01802         ret_val = ad77681_scratchpad(device_adc, &chceck_sequence);
01803         pc.printf("  Insered sequence:  %d\n  Returned sequence: %d\n", new_menu_select, chceck_sequence);
01804         if (ret_val == SUCCESS)
01805             pc.printf("  SUCCESS!\n");
01806         else
01807             pc.printf("  FAILURE!\n");
01808     } else
01809         pc.printf("  Invalid value\n"); 
01810     print_prompt(); 
01811 }
01812 
01813 /**
01814  * Start with the piezo accelerometer offset compensation
01815  * The offset compenzation process uses a successive approximation model
01816  * There is lot of averaging going on, because of quite noisy piezo accelerometer
01817  * It will take some time
01818  */
01819 void static menu_21_piezo_offset(void)
01820 {
01821     uint8_t ltc2606_res = 16;
01822     uint32_t dac_code = 0;
01823     uint32_t dac_code_arr[16];
01824     double mean_voltage = 0.0, min_voltage; 
01825     double mean_voltage_arr[16];
01826     int8_t sar_loop, min_find, min_index;
01827     uint16_t SINC3_odr;    
01828 
01829     // Low power mode and MCLK/16
01830     ad77681_set_power_mode(device_adc, AD77681_ECO);
01831     ad77681_set_mclk_div(device_adc, AD77681_MCLK_DIV_16); 
01832     
01833     // 4SPS = 7999 SINC3, 10SPS = 3199 SINC3, 50SPS = 639 SINC3
01834     ad77681_SINC3_ODR(device_adc, &SINC3_odr, 4);  
01835     // Set the oversamplig ratio to high value, to extract DC                                                                       
01836     ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_SINC3, SINC3_odr);
01837  
01838     // successive approximation algorithm
01839     pc.printf("\nInitialize SAR loop (DAC MSB set to high)\n");
01840     // Set DAC code to half scale  
01841     dac_code = (1 << (ltc2606_res - 1 ));
01842     // Update output of the DAC
01843     ltc26x6_write_code(device_dac, write_update_command, dac_code);
01844     // Wait for DAC output to settle                                    
01845     wait_ms(500);
01846     // Set desired number of samples for every iteration           
01847     measured_data.samples = 100;
01848     measured_data.finish = false;
01849     measured_data.count = 0;
01850     // Take X number of samples
01851     cont_sampling();
01852     // Get the mean voltage of taken samples stroed in the measured_data strucutre                                                                 
01853     get_mean_voltage(&measured_data, &mean_voltage);
01854     // Print the mean ADC read voltage for a given DAC code 
01855     pc.printf("DAC code:%x\t\tMean Voltage: %.6f\n", dac_code, mean_voltage);
01856     // Store the initial DAC code in the array
01857     dac_code_arr[ltc2606_res - 1] = dac_code;
01858     // Store the initial mean voltage in the array
01859     mean_voltage_arr[ltc2606_res - 1] = mean_voltage;
01860     
01861     for ( sar_loop = ltc2606_res - 1; sar_loop > 0; sar_loop--) {
01862         // Check if the mean voltage is positive or negative
01863         if (mean_voltage > 0) {
01864             dac_code = dac_code + (1 << (sar_loop - 1));
01865             pc.printf("UP\n\n");           
01866         } else {
01867             dac_code = dac_code - (1 << (sar_loop)) + (1 << (sar_loop-1));
01868             pc.printf("DOWN\n\n");        
01869         }  
01870         // Print loop coard
01871         pc.printf("SAR loop #: %d\n",sar_loop);
01872         // Update output of the DAC
01873         ltc26x6_write_code(device_dac, write_update_command, dac_code);
01874         // Wait for DAC output to settle                                    
01875         wait_ms(500);
01876         // Clear data finish flag
01877         measured_data.finish = false;
01878         measured_data.count = 0;
01879         // Take X number of samples
01880         cont_sampling();
01881         // Get the mean voltage of taken samples stroed in the measured_data strucutre                                                                 
01882         get_mean_voltage(&measured_data, &mean_voltage); 
01883         pc.printf("DAC code:%x\t\tMean Voltage: %.6f\n", dac_code, mean_voltage); 
01884         dac_code_arr[sar_loop - 1] = dac_code;
01885         mean_voltage_arr[sar_loop - 1] = mean_voltage;
01886     }
01887     min_voltage = abs(mean_voltage_arr[0]); 
01888     for (min_find = 0;  min_find < 16; min_find++) {
01889         if (min_voltage > abs(mean_voltage_arr[min_find])) {
01890             min_voltage = abs(mean_voltage_arr[min_find]);
01891             min_index = min_find;
01892         } 
01893     }
01894     ltc26x6_write_code(device_dac, write_update_command, dac_code_arr[min_index]);
01895     // Wait for DAC output to settle                                    
01896     wait_ms(500);
01897     // Print the final DAC code               
01898     pc.printf("\nFinal DAC code set to:%x\t\tFinal Mean Voltage: %.6f\n", dac_code_arr[min_index], mean_voltage_arr[min_index]);                
01899     // Set to original filter
01900     ad77681_set_filter_type(device_adc, AD77681_SINC5_FIR_DECx32, AD77681_FIR, 0);
01901     ad77681_update_sample_rate(device_adc);      
01902     pc.printf("\nOffset compenzation done!\n"); 
01903     print_prompt(); 
01904 }
01905 
01906 /**
01907  * Get mean from sampled data
01908  * @param mean_voltage      Mean Voltage
01909  * @param measured_data     The structure carying measured data
01910  */
01911 void static get_mean_voltage(struct adc_data *measured_data, double *mean_voltage)
01912 {
01913     int32_t shifted_data;
01914     double sum = 0, voltage = 0;
01915     uint16_t i;
01916     
01917     for ( i = 0; i < measured_data->samples; i++) {
01918         ad77681_data_to_voltage(device_adc, &measured_data->raw_data[i], &voltage);
01919         sum += voltage; 
01920     }
01921     *mean_voltage = (double)(sum / (double)(measured_data->samples));    
01922 }
01923 
01924 /**
01925  * Set output of the on-board DAC in codes or in voltage
01926  *
01927  */
01928 void static menu_22_set_DAC_output(void)
01929 {
01930     int16_t dac_status;
01931     uint16_t  code ;
01932     uint32_t new_menu_select, new_dac;
01933     float dac_voltage;
01934     // Gain factor of the on-board DAC buffer, to have full 5V range(ADA4807-1ARJZ)
01935     // Non-inverting op-amp resistor ratio => 1 + (2.7 k ohm / 2.7 k ohm)
01936     float buffer_gain =  2;  
01937     
01938     pc.printf(" Set DAC output: \n");
01939     pc.printf("  1 - Voltage\n");
01940     pc.printf("  2 - Codes\n");
01941     pc.printf(" Select an option: \n");
01942     
01943     getUserInput(&new_menu_select);
01944     
01945     switch (new_menu_select) {
01946     case 1:
01947         pc.printf(" Set DAC output in mV: ");
01948         getUserInput(&new_dac);
01949         
01950         dac_voltage = ((float)(new_dac) / 1000.0) / buffer_gain;
01951         ltc26x6_voltage_to_code(device_dac, dac_voltage, &code);    
01952         ltc26x6_write_code(device_dac, write_update_command, code); 
01953         if (dac_status == SUCCESS)    
01954             pc.printf("%.3f V at Shift output\n\n", dac_voltage * buffer_gain); 
01955         else if (dac_status == LTC26X6_CODE_OVERFLOW)
01956             pc.printf("%.3f V at Shift output, OVERFLOW!\n\n", dac_voltage * buffer_gain);  
01957         else if (dac_status == LTC26X6_CODE_UNDERFLOW)
01958             pc.printf("%.3f V at Shift output, UNDERFLOW!\n\n", dac_voltage * buffer_gain);     
01959         break;      
01960     case 2:
01961         pc.printf(" Set DAC codes in decimal form: ");
01962         getUserInput(&new_dac);             
01963         ltc26x6_write_code(device_dac, write_update_command, new_dac);
01964         pc.printf("%x at DAC output\n\n", new_dac);     
01965         break;
01966     default:
01967         pc.printf(" Invalid option\n");
01968         break;          
01969     }
01970     print_prompt();
01971 }
01972 
01973 /**
01974  * Prints out an array in binary form
01975  *
01976  */
01977 void static print_binary(uint8_t number, char *binary_number)
01978 {   
01979     for (int8_t i = 7; i >= 0; i--) {
01980         if (number & 1)
01981             binary_number[i] = '1';
01982         else
01983             binary_number[i] = '0';
01984         number >>= 1;
01985     }
01986 }
01987 
01988 /**
01989  * Setup SDP-K1 GPIOs
01990  * 
01991  * 
01992  */
01993  void static sdpk1_gpio_setup(void)
01994 {
01995     // Enable DAC buffer & other buffer
01996     buffer_en = GPIO_HIGH;
01997     // Turn on onboard red LED
01998     led_red = GPIO_HIGH;
01999     // Turn on onboard blue LED
02000     led_blue = GPIO_HIGH;
02001 }