![](/media/cache/profiles/cff02a74da64d145a4aed3a577a106ab.jpg.50x50_q85.jpg)
test
main.cpp@10:b72f2fa277af, 2015-03-04 (annotated)
- Committer:
- fblanc
- Date:
- Wed Mar 04 14:52:06 2015 +0000
- Revision:
- 10:b72f2fa277af
- Parent:
- 9:4a67a60a9f8e
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 0:92dd9611bf80 | 1 | #include "mbed.h" |
fblanc | 4:75077e05de3e | 2 | #include "USBSerial.h" |
fblanc | 9:4a67a60a9f8e | 3 | #define VERSION "27_06_2014" |
fblanc | 7:de5b1bc07eb5 | 4 | #define SPEED 5000//1000 //µs |
fblanc | 6:fa9df9f03bfa | 5 | #define BLOCUSB 64 //64 |
fblanc | 6:fa9df9f03bfa | 6 | #define NBCHAN 2 |
fblanc | 6:fa9df9f03bfa | 7 | #define NBSAMPLE BLOCUSB/NBCHAN // 2 samples 16bit |
fblanc | 10:b72f2fa277af | 8 | #define __KL25 |
fblanc | 8:213824bed8d5 | 9 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 10 | #define CIBLE "LPC1768_SDA" |
fblanc | 8:213824bed8d5 | 11 | char str[256]; |
fblanc | 8:213824bed8d5 | 12 | uint8_t start_str=0; |
fblanc | 8:213824bed8d5 | 13 | Serial pc(USBTX, USBRX); |
fblanc | 8:213824bed8d5 | 14 | DigitalOut led2(LED2); |
fblanc | 8:213824bed8d5 | 15 | DigitalOut led3(LED3); |
fblanc | 8:213824bed8d5 | 16 | DigitalOut led4(LED4); |
fblanc | 8:213824bed8d5 | 17 | AnalogIn adc_1(p16); |
fblanc | 8:213824bed8d5 | 18 | AnalogIn adc_2(p17); |
fblanc | 8:213824bed8d5 | 19 | #endif |
fblanc | 4:75077e05de3e | 20 | #if defined( __LPC1768) |
fblanc | 8:213824bed8d5 | 21 | #define CIBLE "LPC1768" |
fblanc | 4:75077e05de3e | 22 | USBSerial pc; |
fblanc | 0:92dd9611bf80 | 23 | DigitalOut led2(LED2); |
fblanc | 0:92dd9611bf80 | 24 | DigitalOut led3(LED3); |
fblanc | 0:92dd9611bf80 | 25 | DigitalOut led4(LED4); |
fblanc | 3:7acf6d1a6b1e | 26 | AnalogIn adc_1(p16); |
fblanc | 3:7acf6d1a6b1e | 27 | AnalogIn adc_2(p17); |
fblanc | 4:75077e05de3e | 28 | #endif |
fblanc | 4:75077e05de3e | 29 | #if defined( __KL25) |
fblanc | 8:213824bed8d5 | 30 | #define CIBLE "KL25" |
fblanc | 4:75077e05de3e | 31 | USBSerial pc; |
fblanc | 9:4a67a60a9f8e | 32 | DigitalOut led2(LED_GREEN); |
fblanc | 9:4a67a60a9f8e | 33 | DigitalOut led3(LED_RED); |
fblanc | 9:4a67a60a9f8e | 34 | DigitalOut led4(LED_BLUE); |
fblanc | 6:fa9df9f03bfa | 35 | AnalogIn adc_1(PTB0); |
fblanc | 6:fa9df9f03bfa | 36 | AnalogIn adc_2(PTB1); |
fblanc | 4:75077e05de3e | 37 | #endif |
fblanc | 9:4a67a60a9f8e | 38 | #if defined( __K64F) |
fblanc | 9:4a67a60a9f8e | 39 | #define CIBLE "K64F" |
fblanc | 9:4a67a60a9f8e | 40 | USBSerial pc; |
fblanc | 9:4a67a60a9f8e | 41 | DigitalOut led2(LED2); |
fblanc | 9:4a67a60a9f8e | 42 | DigitalOut led3(LED1); |
fblanc | 9:4a67a60a9f8e | 43 | DigitalOut led4(LED3); |
fblanc | 9:4a67a60a9f8e | 44 | AnalogIn adc_1(PTB2); |
fblanc | 9:4a67a60a9f8e | 45 | AnalogIn adc_2(PTB3); |
fblanc | 9:4a67a60a9f8e | 46 | #endif |
fblanc | 6:fa9df9f03bfa | 47 | uint16_t T_hma_u16[2][BLOCUSB]; |
fblanc | 0:92dd9611bf80 | 48 | uint16_t index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 49 | |
fblanc | 0:92dd9611bf80 | 50 | uint16_t index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 51 | bool t_write=0; |
fblanc | 9:4a67a60a9f8e | 52 | bool highspeed=0; |
fblanc | 8:213824bed8d5 | 53 | int speed; // int for scanf(%d) |
fblanc | 7:de5b1bc07eb5 | 54 | bool start_mesure; |
fblanc | 0:92dd9611bf80 | 55 | bool flag_write_USB=0; |
fblanc | 0:92dd9611bf80 | 56 | Ticker flipperADC; |
fblanc | 0:92dd9611bf80 | 57 | |
fblanc | 0:92dd9611bf80 | 58 | void flipADC() |
fblanc | 0:92dd9611bf80 | 59 | { |
fblanc | 9:4a67a60a9f8e | 60 | led2=1; |
fblanc | 3:7acf6d1a6b1e | 61 | T_hma_u16[t_write][index_hma_write++]=adc_1.read_u16(); //read ADC |
fblanc | 3:7acf6d1a6b1e | 62 | T_hma_u16[t_write][index_hma_write++]=adc_2.read_u16(); //read ADC |
fblanc | 5:832b59a73dec | 63 | |
fblanc | 5:832b59a73dec | 64 | |
fblanc | 7:de5b1bc07eb5 | 65 | if(index_hma_write>=NBSAMPLE) { |
fblanc | 0:92dd9611bf80 | 66 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 67 | t_write=!t_write; //toggel buffer |
fblanc | 7:de5b1bc07eb5 | 68 | index_hma_read=index_hma_write; |
fblanc | 0:92dd9611bf80 | 69 | index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 70 | } |
fblanc | 9:4a67a60a9f8e | 71 | led2=0; |
fblanc | 0:92dd9611bf80 | 72 | } |
fblanc | 0:92dd9611bf80 | 73 | |
fblanc | 0:92dd9611bf80 | 74 | Ticker flipperUSB; |
fblanc | 0:92dd9611bf80 | 75 | void flipUSB() |
fblanc | 0:92dd9611bf80 | 76 | { |
fblanc | 0:92dd9611bf80 | 77 | |
fblanc | 0:92dd9611bf80 | 78 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 79 | |
fblanc | 0:92dd9611bf80 | 80 | } |
fblanc | 0:92dd9611bf80 | 81 | |
fblanc | 0:92dd9611bf80 | 82 | void pc_rx(void) |
fblanc | 0:92dd9611bf80 | 83 | { |
fblanc | 8:213824bed8d5 | 84 | |
fblanc | 8:213824bed8d5 | 85 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 86 | for(; pc.readable()==true; ++start_str) { |
fblanc | 8:213824bed8d5 | 87 | str[start_str]=pc.getc(); |
fblanc | 8:213824bed8d5 | 88 | } |
fblanc | 8:213824bed8d5 | 89 | |
fblanc | 8:213824bed8d5 | 90 | if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) {//ABORT |
fblanc | 8:213824bed8d5 | 91 | start_mesure=0; |
fblanc | 8:213824bed8d5 | 92 | flipperADC.detach(); |
fblanc | 8:213824bed8d5 | 93 | t_write=0; |
fblanc | 8:213824bed8d5 | 94 | index_hma_read=0; |
fblanc | 8:213824bed8d5 | 95 | index_hma_write=0; |
fblanc | 8:213824bed8d5 | 96 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 97 | str[i]='\0'; |
fblanc | 8:213824bed8d5 | 98 | |
fblanc | 8:213824bed8d5 | 99 | } |
fblanc | 8:213824bed8d5 | 100 | |
fblanc | 8:213824bed8d5 | 101 | } |
fblanc | 8:213824bed8d5 | 102 | if(start_mesure==0) { |
fblanc | 8:213824bed8d5 | 103 | if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) {//*IDN? |
fblanc | 8:213824bed8d5 | 104 | pc.printf("LAAS-CNRS ,HMA ,%s ,%s\r",CIBLE,VERSION); |
fblanc | 8:213824bed8d5 | 105 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 106 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 107 | |
fblanc | 8:213824bed8d5 | 108 | } |
fblanc | 8:213824bed8d5 | 109 | } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) {//MEASure |
fblanc | 8:213824bed8d5 | 110 | start_mesure=1; |
fblanc | 8:213824bed8d5 | 111 | flipperADC.attach_us(&flipADC, speed); |
fblanc | 8:213824bed8d5 | 112 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 113 | str[i]='\0'; |
fblanc | 5:832b59a73dec | 114 | |
fblanc | 8:213824bed8d5 | 115 | } |
fblanc | 8:213824bed8d5 | 116 | } else if( strncmp(str,"TIME",strlen("TIME"))==0 ) {//TIME |
fblanc | 8:213824bed8d5 | 117 | sscanf(&str[strlen("TIME")],"%d",&speed); |
fblanc | 8:213824bed8d5 | 118 | if(speed<1) |
fblanc | 8:213824bed8d5 | 119 | speed=1; |
fblanc | 8:213824bed8d5 | 120 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 121 | str[i]='\0'; |
fblanc | 8:213824bed8d5 | 122 | |
fblanc | 8:213824bed8d5 | 123 | } |
fblanc | 9:4a67a60a9f8e | 124 | }else if( strncmp(str,"HS",strlen("HS"))==0 ) {//HS |
fblanc | 9:4a67a60a9f8e | 125 | highspeed=1; |
fblanc | 9:4a67a60a9f8e | 126 | for(int i=0; i<256; ++i) { |
fblanc | 9:4a67a60a9f8e | 127 | str[i]='\0'; |
fblanc | 9:4a67a60a9f8e | 128 | |
fblanc | 9:4a67a60a9f8e | 129 | } |
fblanc | 9:4a67a60a9f8e | 130 | }else if( strncmp(str,"LS",strlen("LS"))==0 ) {//Ls |
fblanc | 9:4a67a60a9f8e | 131 | highspeed=0; |
fblanc | 9:4a67a60a9f8e | 132 | for(int i=0; i<256; ++i) { |
fblanc | 9:4a67a60a9f8e | 133 | str[i]='\0'; |
fblanc | 9:4a67a60a9f8e | 134 | |
fblanc | 9:4a67a60a9f8e | 135 | } |
fblanc | 8:213824bed8d5 | 136 | } |
fblanc | 8:213824bed8d5 | 137 | } |
fblanc | 8:213824bed8d5 | 138 | for(int i=0; i<start_str; ++i) { |
fblanc | 8:213824bed8d5 | 139 | if(str[i]=='\r') { |
fblanc | 8:213824bed8d5 | 140 | for(int j=0; j<256; ++j) { |
fblanc | 8:213824bed8d5 | 141 | str[j]='\0'; |
fblanc | 8:213824bed8d5 | 142 | |
fblanc | 8:213824bed8d5 | 143 | } |
fblanc | 8:213824bed8d5 | 144 | start_str=0; |
fblanc | 8:213824bed8d5 | 145 | |
fblanc | 8:213824bed8d5 | 146 | } |
fblanc | 8:213824bed8d5 | 147 | |
fblanc | 8:213824bed8d5 | 148 | } |
fblanc | 8:213824bed8d5 | 149 | |
fblanc | 8:213824bed8d5 | 150 | #else |
fblanc | 8:213824bed8d5 | 151 | char str[32]; |
fblanc | 5:832b59a73dec | 152 | for(int start_str=0; pc.readable()!=0; ++start_str) { |
fblanc | 0:92dd9611bf80 | 153 | str[start_str]=pc.getc(); |
fblanc | 0:92dd9611bf80 | 154 | } |
fblanc | 5:832b59a73dec | 155 | if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) { |
fblanc | 7:de5b1bc07eb5 | 156 | start_mesure=0; |
fblanc | 0:92dd9611bf80 | 157 | flipperADC.detach(); |
fblanc | 0:92dd9611bf80 | 158 | t_write=0; |
fblanc | 0:92dd9611bf80 | 159 | index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 160 | index_hma_write=0; |
fblanc | 4:75077e05de3e | 161 | |
fblanc | 7:de5b1bc07eb5 | 162 | } |
fblanc | 7:de5b1bc07eb5 | 163 | if(start_mesure==0) { |
fblanc | 7:de5b1bc07eb5 | 164 | if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) { |
fblanc | 8:213824bed8d5 | 165 | pc.printf("LAAS-CNRS ,HMA ,%s ,%s\r",CIBLE,VERSION); |
fblanc | 7:de5b1bc07eb5 | 166 | } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) { |
fblanc | 7:de5b1bc07eb5 | 167 | start_mesure=1; |
fblanc | 7:de5b1bc07eb5 | 168 | flipperADC.attach_us(&flipADC, speed); |
fblanc | 7:de5b1bc07eb5 | 169 | } else if( strncmp(str,"TIME",strlen("TIME"))==0 ) { |
fblanc | 7:de5b1bc07eb5 | 170 | sscanf(&str[strlen("TIME")],"%d",&speed); |
fblanc | 8:213824bed8d5 | 171 | if(speed<1) |
fblanc | 8:213824bed8d5 | 172 | speed=1; |
fblanc | 9:4a67a60a9f8e | 173 | }else if( strncmp(str,"HS",strlen("HS"))==0 ) { |
fblanc | 9:4a67a60a9f8e | 174 | highspeed=1; |
fblanc | 9:4a67a60a9f8e | 175 | }else if( strncmp(str,"LS",strlen("LS"))==0 ) { |
fblanc | 9:4a67a60a9f8e | 176 | highspeed=0; |
fblanc | 7:de5b1bc07eb5 | 177 | } |
fblanc | 0:92dd9611bf80 | 178 | } |
fblanc | 8:213824bed8d5 | 179 | |
fblanc | 8:213824bed8d5 | 180 | #endif |
fblanc | 0:92dd9611bf80 | 181 | } |
fblanc | 0:92dd9611bf80 | 182 | |
fblanc | 0:92dd9611bf80 | 183 | int main() |
fblanc | 0:92dd9611bf80 | 184 | { |
fblanc | 8:213824bed8d5 | 185 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 186 | pc.baud(460800); |
fblanc | 8:213824bed8d5 | 187 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 188 | str[i]='\0'; |
fblanc | 8:213824bed8d5 | 189 | } |
fblanc | 7:de5b1bc07eb5 | 190 | #endif |
fblanc | 7:de5b1bc07eb5 | 191 | speed=SPEED; |
fblanc | 7:de5b1bc07eb5 | 192 | start_mesure=0; |
fblanc | 5:832b59a73dec | 193 | while(1) { |
fblanc | 0:92dd9611bf80 | 194 | |
fblanc | 0:92dd9611bf80 | 195 | if (pc.readable()) { |
fblanc | 0:92dd9611bf80 | 196 | pc_rx(); |
fblanc | 0:92dd9611bf80 | 197 | } |
fblanc | 5:832b59a73dec | 198 | |
fblanc | 0:92dd9611bf80 | 199 | if(flag_write_USB==1) { |
fblanc | 9:4a67a60a9f8e | 200 | led2=1; |
fblanc | 9:4a67a60a9f8e | 201 | led3=1; |
fblanc | 9:4a67a60a9f8e | 202 | led4=1; |
fblanc | 0:92dd9611bf80 | 203 | flag_write_USB=0; |
fblanc | 9:4a67a60a9f8e | 204 | if(highspeed==0) |
fblanc | 9:4a67a60a9f8e | 205 | { |
fblanc | 9:4a67a60a9f8e | 206 | pc.printf("%04d%c\r",index_hma_read,'\0'); //'\0'fin de chaine ASCII '\r' fin de transmission |
fblanc | 9:4a67a60a9f8e | 207 | } |
fblanc | 6:fa9df9f03bfa | 208 | for(unsigned int i=0; i<index_hma_read*NBCHAN; i+=BLOCUSB ) { // 2 cannaux de 16bits |
fblanc | 8:213824bed8d5 | 209 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 210 | char* ptr=(char*)&T_hma_u16[!t_write][0]; |
fblanc | 8:213824bed8d5 | 211 | |
fblanc | 8:213824bed8d5 | 212 | for(uint16_t i=0; i<index_hma_read; ++i) { |
fblanc | 8:213824bed8d5 | 213 | pc.putc(*ptr++); |
fblanc | 8:213824bed8d5 | 214 | pc.putc(*ptr++); |
fblanc | 8:213824bed8d5 | 215 | } |
fblanc | 8:213824bed8d5 | 216 | #else |
fblanc | 6:fa9df9f03bfa | 217 | 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. |
fblanc | 8:213824bed8d5 | 218 | #endif |
fblanc | 0:92dd9611bf80 | 219 | } |
fblanc | 9:4a67a60a9f8e | 220 | led2=0; |
fblanc | 0:92dd9611bf80 | 221 | } |
fblanc | 0:92dd9611bf80 | 222 | } |
fblanc | 0:92dd9611bf80 | 223 | } |