BEI ZHANG / Mbed 2 deprecated RM3100_SPI_SampleCode

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 * @file         main.cpp
00003 *
00004 * @brief        Sample interface for RM3100.
00005 *
00006 * @authors      Betty Zhang
00007 * @date         05/21/2018
00008 * @copyright    (C) 2018 PNI Corp, Protonex LLC
00009 *
00010 * @copyright    Disclosure to third parties or reproduction in any form
00011 *               whatsoever, without prior written consent, is strictly forbidden
00012 *
00013 */
00014 
00015 #include "mbed.h"
00016 #include "main.h"
00017 #include "RM3100.h"
00018 
00019 
00020 Serial comport(SERIAL_TX, SERIAL_RX);
00021 
00022 RM3100 rm3100;
00023 
00024 //Global variables
00025 int serial_inchar;
00026 float actual_rate;
00027 
00028 void OnSerial(void)
00029 {
00030     serial_inchar = comport.getc();
00031 }
00032 
00033 //=============================================================================
00034 //  Serial input Character (Key) Commands, courtesy of JM.
00035 //=============================================================================
00036 // This function is called upon every incomming serial character 
00037 void processSerialInchar(char key)
00038 {
00039     // The Simple Serial protocal mostly consists of single character commands
00040     // send '?' to list available commands
00041 
00042     serial_inchar = NULL;
00043     int tmrc_val, rate;
00044 
00045     switch (key) {
00046         case '+':
00047             rm3100.ChangeSampleRate(1);
00048             break;
00049 
00050         case '-':
00051             rm3100.ChangeSampleRate(-1);
00052             break;
00053 
00054         case '>':
00055             rm3100.ChangeCycleCount(1);
00056             break;
00057 
00058         case '<':
00059             rm3100.ChangeCycleCount(-1);
00060             break;
00061 
00062         case 'a':
00063             rm3100.DisplayREVIDReg();
00064             break;
00065         case 'b':
00066             rate = rm3100.GetSampleRate(&tmrc_val);
00067             comport.printf("Host process rate = %3.1f Hz, reqRate = %d, TMRC=0x%X\n\r",actual_rate, rate, tmrc_val);
00068             break;
00069         case 'c':
00070             rm3100.DisplayCycleCount();
00071             break;
00072 
00073         case 'q':
00074             rm3100.SelfTest();
00075             break;
00076         case 'r':
00077             rm3100.RunCMM(1); //Run CMM
00078             rm3100.ClearDrdyInt();
00079             break;
00080 
00081         case 's':
00082             rm3100.RunCMM(0); //Stop
00083             rate = rm3100.GetSampleRate(&tmrc_val);
00084             comport.printf("Host process rate = %3.1f Hz, reqRate = %d, TMRC=0x%X\n\r",actual_rate, rate, tmrc_val);
00085             break;
00086 
00087         case '?': {
00088             u8 bar[45];
00089             memset(bar, 205, sizeof(bar));
00090             bar[sizeof(bar)-1] = 0;
00091             comport.printf("\n\r");
00092             comport.printf("                  RM3100 Commands\n\r");
00093             comport.printf("                  Revision: %s\n\r",REVISION);
00094             comport.printf("  %c%s%c\n\r", 201, bar, 187);
00095             comport.printf("  %c        Commands  (case sensitive)          %c\n\r", 186, 186);
00096             // Status and configuration
00097             comport.printf("  %c%s%c\n\r", 204, bar, 185);
00098             comport.printf("  %c         Settings and Status                %c\n\r", 186, 186);
00099             comport.printf("  %c%s%c\n\r", 204, bar, 185);
00100             comport.printf("  %c a : Display REVID                          %c\n\r", 186, 186);
00101             comport.printf("  %c b : Display host rate, request rate & TMRC %c\n\r", 186, 186);
00102             comport.printf("  %c c : Display cycle count, gain & max rate   %c\n\r", 186, 186);
00103             comport.printf("  %c + : Increase Sample Rate                   %c\n\r", 186, 186);
00104             comport.printf("  %c - : Decrease Sample Rate                   %c\n\r", 186, 186);
00105             comport.printf("  %c > : Increase Cycle Count                   %c\n\r", 186, 186);
00106             comport.printf("  %c < : Decrease Cycle Count                   %c\n\r", 186, 186);
00107 
00108             // Tests
00109             comport.printf("  %c%s%c\n\r", 204, bar, 185);
00110             comport.printf("  %c                  Tests                     %c\n\r", 186, 186);
00111             comport.printf("  %c%s%c\n\r", 204, bar, 185);
00112             comport.printf("  %c q : Run sensor Self tests                  %c\n\r", 186, 186);
00113             comport.printf("  %c r : Run continous mode                     %c\n\r", 186, 186);
00114             comport.printf("  %c s : Stop continous mode                    %c\n\r", 186, 186);
00115 
00116             comport.printf("  %c%s%c\n\r", 200, bar, 188);
00117 
00118         }
00119         default:
00120             break;
00121     }
00122 }
00123         
00124 int main() {
00125     
00126     int counter = 0;
00127     Timer timer;
00128     
00129     
00130     // Init user serial interface
00131     comport.baud(921600);
00132     comport.printf("\n\rRM3100 Host SPI Sample Code Ver %s on mBed\n\r",REVISION);
00133         
00134     //Set sample rate
00135     rm3100.SetSampleRateReg(1); //in Hz
00136                 
00137     //Start to CMM run
00138     rm3100.RunCMM(1);
00139    
00140     // Setup interrupt callback functions
00141     comport.attach(&OnSerial);  // user input from serial term 
00142     
00143     // Callback function to the rising edge
00144     rm3100.DrdyCallBack();
00145     
00146     //Clear Interrupt first
00147     rm3100.ClearDrdyInt();
00148         
00149     while (1) 
00150     {
00151 
00152         if (serial_inchar) 
00153         {
00154             processSerialInchar(serial_inchar); // process user key commands
00155             serial_inchar = NULL;
00156         }
00157         
00158         if (counter <= 1)
00159             timer.start();
00160         else if (counter >= 10)
00161         {
00162             timer.stop();
00163             float time = timer.read();
00164             actual_rate = 10.0/time;
00165             if (actual_rate <= 150) {
00166                 __disable_irq();    // Disable Interrupts
00167                 comport.printf("Counter=%d, time=%3.3f s, Host Proc rate = %3.1f Hz\n\r",counter, time, actual_rate);
00168                 __enable_irq();    // Enable Interrupts
00169             }
00170             counter = 0;
00171             timer.reset();
00172         }
00173         
00174         //Process Drdy Interrupt
00175         if (rm3100.GetDrdyIntFlag())
00176         {
00177             rm3100.SetDrdyIntFlag(0);
00178             
00179             //If sample rate is higher than 150Hz, serial port cannot process in time
00180             //skip samples to make the host keep up, but to clear drdy interrupt
00181             if ((actual_rate <= 150) || (counter == 1))
00182                 rm3100.ReadRM3100();
00183             else
00184                 rm3100.ClearDrdyInt(); 
00185             
00186             counter++;
00187         }
00188     }
00189 }