frederic blanc / Mbed 2 deprecated HMA_HelloWord

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBSerial.h"
00003 #define VERSION "27_06_2014"
00004 #define SPEED 5000//1000 //µs
00005 #define BLOCUSB 64 //64
00006 #define NBCHAN 2
00007 #define NBSAMPLE BLOCUSB/NBCHAN //  2 samples 16bit
00008 #define __KL25
00009 #if defined( __LPC1768_SDA)
00010 #define CIBLE "LPC1768_SDA"
00011 char str[256];
00012 uint8_t start_str=0;
00013 Serial pc(USBTX, USBRX);
00014 DigitalOut led2(LED2);
00015 DigitalOut led3(LED3);
00016 DigitalOut led4(LED4);
00017 AnalogIn adc_1(p16);
00018 AnalogIn adc_2(p17);
00019 #endif
00020 #if defined( __LPC1768)
00021 #define CIBLE "LPC1768"
00022 USBSerial pc;
00023 DigitalOut led2(LED2);
00024 DigitalOut led3(LED3);
00025 DigitalOut led4(LED4);
00026 AnalogIn adc_1(p16);
00027 AnalogIn adc_2(p17);
00028 #endif
00029 #if defined( __KL25)
00030 #define CIBLE "KL25"
00031 USBSerial pc;
00032 DigitalOut  led2(LED_GREEN);
00033 DigitalOut  led3(LED_RED);
00034 DigitalOut led4(LED_BLUE);
00035 AnalogIn adc_1(PTB0);
00036 AnalogIn adc_2(PTB1);
00037 #endif
00038 #if defined( __K64F)
00039 #define CIBLE "K64F"
00040 USBSerial pc;
00041 DigitalOut led2(LED2);
00042 DigitalOut led3(LED1);
00043 DigitalOut led4(LED3);
00044 AnalogIn adc_1(PTB2);
00045 AnalogIn adc_2(PTB3);
00046 #endif
00047 uint16_t  T_hma_u16[2][BLOCUSB];
00048 uint16_t index_hma_write=0;
00049 
00050 uint16_t index_hma_read=0;
00051 bool t_write=0;
00052 bool highspeed=0;
00053 int speed; // int for scanf(%d)
00054 bool start_mesure;
00055 bool flag_write_USB=0;
00056 Ticker flipperADC;
00057 
00058 void flipADC()
00059 {
00060 led2=1;
00061     T_hma_u16[t_write][index_hma_write++]=adc_1.read_u16(); //read ADC
00062     T_hma_u16[t_write][index_hma_write++]=adc_2.read_u16(); //read ADC
00063 
00064 
00065     if(index_hma_write>=NBSAMPLE) {
00066         flag_write_USB=1;
00067         t_write=!t_write; //toggel buffer
00068         index_hma_read=index_hma_write;
00069         index_hma_write=0;
00070     }
00071 led2=0;
00072 }
00073 
00074 Ticker flipperUSB;
00075 void flipUSB()
00076 {
00077 
00078     flag_write_USB=1;
00079 
00080 }
00081 
00082 void pc_rx(void)
00083 {
00084 
00085 #if defined( __LPC1768_SDA)
00086     for(; pc.readable()==true; ++start_str) {
00087         str[start_str]=pc.getc();
00088     }
00089 
00090     if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) {//ABORT
00091         start_mesure=0;
00092         flipperADC.detach();
00093         t_write=0;
00094         index_hma_read=0;
00095         index_hma_write=0;
00096         for(int i=0; i<256; ++i) {
00097             str[i]='\0';
00098 
00099         }
00100 
00101     }
00102     if(start_mesure==0) {
00103         if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) {//*IDN?
00104             pc.printf("LAAS-CNRS ,HMA ,%s ,%s\r",CIBLE,VERSION);
00105             for(int i=0; i<256; ++i) {
00106                 str[i]='\0';
00107 
00108             }
00109         } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) {//MEASure
00110             start_mesure=1;
00111             flipperADC.attach_us(&flipADC, speed);
00112             for(int i=0; i<256; ++i) {
00113                 str[i]='\0';
00114 
00115             }
00116         } else if( strncmp(str,"TIME",strlen("TIME"))==0 ) {//TIME
00117             sscanf(&str[strlen("TIME")],"%d",&speed);
00118             if(speed<1)
00119                 speed=1;
00120             for(int i=0; i<256; ++i) {
00121                 str[i]='\0';
00122 
00123             }
00124         }else if( strncmp(str,"HS",strlen("HS"))==0 ) {//HS
00125             highspeed=1;
00126             for(int i=0; i<256; ++i) {
00127                 str[i]='\0';
00128 
00129             }
00130         }else if( strncmp(str,"LS",strlen("LS"))==0 ) {//Ls
00131             highspeed=0;
00132             for(int i=0; i<256; ++i) {
00133                 str[i]='\0';
00134 
00135             }
00136         }
00137     } 
00138     for(int i=0; i<start_str; ++i) {
00139         if(str[i]=='\r') {
00140             for(int j=0; j<256; ++j) {
00141                 str[j]='\0';
00142 
00143             }
00144             start_str=0;
00145 
00146         }
00147 
00148     }
00149 
00150 #else
00151     char str[32];
00152     for(int start_str=0; pc.readable()!=0; ++start_str) {
00153         str[start_str]=pc.getc();
00154     }
00155     if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) {
00156         start_mesure=0;
00157         flipperADC.detach();
00158         t_write=0;
00159         index_hma_read=0;
00160         index_hma_write=0;
00161 
00162     }
00163     if(start_mesure==0) {
00164         if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) {
00165             pc.printf("LAAS-CNRS ,HMA ,%s ,%s\r",CIBLE,VERSION);
00166         } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) {
00167             start_mesure=1;
00168             flipperADC.attach_us(&flipADC, speed);
00169         } else if( strncmp(str,"TIME",strlen("TIME"))==0 ) {
00170             sscanf(&str[strlen("TIME")],"%d",&speed);
00171             if(speed<1)
00172                 speed=1;
00173         }else if( strncmp(str,"HS",strlen("HS"))==0 ) {
00174             highspeed=1;
00175         }else if( strncmp(str,"LS",strlen("LS"))==0 ) {
00176             highspeed=0;
00177         }
00178     }
00179 
00180 #endif
00181 }
00182 
00183 int main()
00184 {
00185 #if defined( __LPC1768_SDA)
00186     pc.baud(460800);
00187     for(int i=0; i<256; ++i) {
00188         str[i]='\0';
00189     }
00190 #endif
00191     speed=SPEED;
00192     start_mesure=0;
00193     while(1) {
00194 
00195         if (pc.readable()) {
00196             pc_rx();
00197         }
00198 
00199         if(flag_write_USB==1) {
00200             led2=1;
00201                 led3=1;
00202     led4=1;
00203             flag_write_USB=0;
00204             if(highspeed==0)
00205             {
00206                 pc.printf("%04d%c\r",index_hma_read,'\0'); //'\0'fin de chaine ASCII '\r' fin de transmission
00207             }
00208             for(unsigned int i=0; i<index_hma_read*NBCHAN; i+=BLOCUSB ) { // 2 cannaux de 16bits
00209 #if defined( __LPC1768_SDA)
00210                 char* ptr=(char*)&T_hma_u16[!t_write][0];
00211 
00212                 for(uint16_t i=0; i<index_hma_read; ++i) {
00213                     pc.putc(*ptr++);
00214                     pc.putc(*ptr++);
00215                 }
00216 #else
00217                 pc.writeBlock((uint8_t*)&T_hma_u16[!t_write][i/NBCHAN],BLOCUSB );//For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.
00218 #endif
00219             }
00220 led2=0;
00221         }
00222     }
00223 }