![](/media/cache/profiles/cff02a74da64d145a4aed3a577a106ab.jpg.50x50_q85.jpg)
test
main.cpp@8:213824bed8d5, 2014-06-25 (annotated)
- Committer:
- fblanc
- Date:
- Wed Jun 25 15:10:42 2014 +0000
- Revision:
- 8:213824bed8d5
- Parent:
- 7:de5b1bc07eb5
- Child:
- 9:4a67a60a9f8e
multi cible LPC1768_SDA LPC1768 KL25
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 | 8:213824bed8d5 | 3 | #define VERSION "24_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 | 8:213824bed8d5 | 8 | #define __LPC1768_SDA |
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 | 7:de5b1bc07eb5 | 32 | PwmOut led2(LED_GREEN); |
fblanc | 7:de5b1bc07eb5 | 33 | PwmOut led3(LED_RED); |
fblanc | 7:de5b1bc07eb5 | 34 | PwmOut 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 | 4:75077e05de3e | 38 | |
fblanc | 6:fa9df9f03bfa | 39 | uint16_t T_hma_u16[2][BLOCUSB]; |
fblanc | 0:92dd9611bf80 | 40 | uint16_t index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 41 | |
fblanc | 0:92dd9611bf80 | 42 | uint16_t index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 43 | bool t_write=0; |
fblanc | 8:213824bed8d5 | 44 | int speed; // int for scanf(%d) |
fblanc | 7:de5b1bc07eb5 | 45 | bool start_mesure; |
fblanc | 0:92dd9611bf80 | 46 | bool flag_write_USB=0; |
fblanc | 0:92dd9611bf80 | 47 | Ticker flipperADC; |
fblanc | 0:92dd9611bf80 | 48 | |
fblanc | 0:92dd9611bf80 | 49 | void flipADC() |
fblanc | 0:92dd9611bf80 | 50 | { |
fblanc | 4:75077e05de3e | 51 | |
fblanc | 3:7acf6d1a6b1e | 52 | T_hma_u16[t_write][index_hma_write++]=adc_1.read_u16(); //read ADC |
fblanc | 3:7acf6d1a6b1e | 53 | T_hma_u16[t_write][index_hma_write++]=adc_2.read_u16(); //read ADC |
fblanc | 5:832b59a73dec | 54 | |
fblanc | 5:832b59a73dec | 55 | |
fblanc | 7:de5b1bc07eb5 | 56 | if(index_hma_write>=NBSAMPLE) { |
fblanc | 0:92dd9611bf80 | 57 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 58 | t_write=!t_write; //toggel buffer |
fblanc | 7:de5b1bc07eb5 | 59 | index_hma_read=index_hma_write; |
fblanc | 0:92dd9611bf80 | 60 | index_hma_write=0; |
fblanc | 0:92dd9611bf80 | 61 | } |
fblanc | 4:75077e05de3e | 62 | |
fblanc | 0:92dd9611bf80 | 63 | } |
fblanc | 0:92dd9611bf80 | 64 | |
fblanc | 0:92dd9611bf80 | 65 | Ticker flipperUSB; |
fblanc | 0:92dd9611bf80 | 66 | void flipUSB() |
fblanc | 0:92dd9611bf80 | 67 | { |
fblanc | 0:92dd9611bf80 | 68 | |
fblanc | 0:92dd9611bf80 | 69 | flag_write_USB=1; |
fblanc | 0:92dd9611bf80 | 70 | |
fblanc | 0:92dd9611bf80 | 71 | } |
fblanc | 0:92dd9611bf80 | 72 | |
fblanc | 0:92dd9611bf80 | 73 | void pc_rx(void) |
fblanc | 0:92dd9611bf80 | 74 | { |
fblanc | 8:213824bed8d5 | 75 | |
fblanc | 8:213824bed8d5 | 76 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 77 | for(; pc.readable()==true; ++start_str) { |
fblanc | 8:213824bed8d5 | 78 | str[start_str]=pc.getc(); |
fblanc | 8:213824bed8d5 | 79 | } |
fblanc | 8:213824bed8d5 | 80 | |
fblanc | 8:213824bed8d5 | 81 | if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) {//ABORT |
fblanc | 8:213824bed8d5 | 82 | start_mesure=0; |
fblanc | 8:213824bed8d5 | 83 | flipperADC.detach(); |
fblanc | 8:213824bed8d5 | 84 | t_write=0; |
fblanc | 8:213824bed8d5 | 85 | index_hma_read=0; |
fblanc | 8:213824bed8d5 | 86 | index_hma_write=0; |
fblanc | 8:213824bed8d5 | 87 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 88 | str[i]='\0'; |
fblanc | 8:213824bed8d5 | 89 | |
fblanc | 8:213824bed8d5 | 90 | } |
fblanc | 8:213824bed8d5 | 91 | |
fblanc | 8:213824bed8d5 | 92 | } |
fblanc | 8:213824bed8d5 | 93 | if(start_mesure==0) { |
fblanc | 8:213824bed8d5 | 94 | if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) {//*IDN? |
fblanc | 8:213824bed8d5 | 95 | pc.printf("LAAS-CNRS ,HMA ,%s ,%s\r",CIBLE,VERSION); |
fblanc | 8:213824bed8d5 | 96 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 97 | str[i]='\0'; |
fblanc | 0:92dd9611bf80 | 98 | |
fblanc | 8:213824bed8d5 | 99 | } |
fblanc | 8:213824bed8d5 | 100 | } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) {//MEASure |
fblanc | 8:213824bed8d5 | 101 | start_mesure=1; |
fblanc | 8:213824bed8d5 | 102 | flipperADC.attach_us(&flipADC, speed); |
fblanc | 8:213824bed8d5 | 103 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 104 | str[i]='\0'; |
fblanc | 5:832b59a73dec | 105 | |
fblanc | 8:213824bed8d5 | 106 | } |
fblanc | 8:213824bed8d5 | 107 | } else if( strncmp(str,"TIME",strlen("TIME"))==0 ) {//TIME |
fblanc | 8:213824bed8d5 | 108 | sscanf(&str[strlen("TIME")],"%d",&speed); |
fblanc | 8:213824bed8d5 | 109 | if(speed<1) |
fblanc | 8:213824bed8d5 | 110 | speed=1; |
fblanc | 8:213824bed8d5 | 111 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 112 | str[i]='\0'; |
fblanc | 8:213824bed8d5 | 113 | |
fblanc | 8:213824bed8d5 | 114 | } |
fblanc | 8:213824bed8d5 | 115 | } |
fblanc | 8:213824bed8d5 | 116 | } |
fblanc | 8:213824bed8d5 | 117 | for(int i=0; i<start_str; ++i) { |
fblanc | 8:213824bed8d5 | 118 | if(str[i]=='\r') { |
fblanc | 8:213824bed8d5 | 119 | for(int j=0; j<256; ++j) { |
fblanc | 8:213824bed8d5 | 120 | str[j]='\0'; |
fblanc | 8:213824bed8d5 | 121 | |
fblanc | 8:213824bed8d5 | 122 | } |
fblanc | 8:213824bed8d5 | 123 | start_str=0; |
fblanc | 8:213824bed8d5 | 124 | |
fblanc | 8:213824bed8d5 | 125 | } |
fblanc | 8:213824bed8d5 | 126 | |
fblanc | 8:213824bed8d5 | 127 | } |
fblanc | 8:213824bed8d5 | 128 | |
fblanc | 8:213824bed8d5 | 129 | #else |
fblanc | 8:213824bed8d5 | 130 | char str[32]; |
fblanc | 5:832b59a73dec | 131 | for(int start_str=0; pc.readable()!=0; ++start_str) { |
fblanc | 0:92dd9611bf80 | 132 | str[start_str]=pc.getc(); |
fblanc | 0:92dd9611bf80 | 133 | } |
fblanc | 5:832b59a73dec | 134 | if( strncmp(str,"ABORt",strlen("ABORt"))==0 ) { |
fblanc | 7:de5b1bc07eb5 | 135 | start_mesure=0; |
fblanc | 0:92dd9611bf80 | 136 | flipperADC.detach(); |
fblanc | 0:92dd9611bf80 | 137 | t_write=0; |
fblanc | 0:92dd9611bf80 | 138 | index_hma_read=0; |
fblanc | 0:92dd9611bf80 | 139 | index_hma_write=0; |
fblanc | 4:75077e05de3e | 140 | |
fblanc | 7:de5b1bc07eb5 | 141 | } |
fblanc | 7:de5b1bc07eb5 | 142 | if(start_mesure==0) { |
fblanc | 7:de5b1bc07eb5 | 143 | if( strncmp(str,"*IDN?",strlen("*IDN?"))==0 ) { |
fblanc | 8:213824bed8d5 | 144 | pc.printf("LAAS-CNRS ,HMA ,%s ,%s\r",CIBLE,VERSION); |
fblanc | 7:de5b1bc07eb5 | 145 | } else if( strncmp(str,"MEASure",strlen("MEASure"))==0 ) { |
fblanc | 7:de5b1bc07eb5 | 146 | start_mesure=1; |
fblanc | 7:de5b1bc07eb5 | 147 | flipperADC.attach_us(&flipADC, speed); |
fblanc | 7:de5b1bc07eb5 | 148 | } else if( strncmp(str,"TIME",strlen("TIME"))==0 ) { |
fblanc | 7:de5b1bc07eb5 | 149 | sscanf(&str[strlen("TIME")],"%d",&speed); |
fblanc | 8:213824bed8d5 | 150 | if(speed<1) |
fblanc | 8:213824bed8d5 | 151 | speed=1; |
fblanc | 0:92dd9611bf80 | 152 | |
fblanc | 7:de5b1bc07eb5 | 153 | } |
fblanc | 0:92dd9611bf80 | 154 | } |
fblanc | 8:213824bed8d5 | 155 | |
fblanc | 8:213824bed8d5 | 156 | #endif |
fblanc | 0:92dd9611bf80 | 157 | } |
fblanc | 0:92dd9611bf80 | 158 | |
fblanc | 0:92dd9611bf80 | 159 | int main() |
fblanc | 0:92dd9611bf80 | 160 | { |
fblanc | 8:213824bed8d5 | 161 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 162 | |
fblanc | 8:213824bed8d5 | 163 | pc.baud(460800); |
fblanc | 8:213824bed8d5 | 164 | for(int i=0; i<256; ++i) { |
fblanc | 8:213824bed8d5 | 165 | str[i]='\0'; |
fblanc | 8:213824bed8d5 | 166 | |
fblanc | 8:213824bed8d5 | 167 | } |
fblanc | 8:213824bed8d5 | 168 | led2=0; |
fblanc | 8:213824bed8d5 | 169 | led3=0; |
fblanc | 8:213824bed8d5 | 170 | led4=0; |
fblanc | 8:213824bed8d5 | 171 | #endif |
fblanc | 7:de5b1bc07eb5 | 172 | #if defined( __LPC1768) |
fblanc | 5:832b59a73dec | 173 | led2=0; |
fblanc | 5:832b59a73dec | 174 | led3=0; |
fblanc | 5:832b59a73dec | 175 | led4=0; |
fblanc | 7:de5b1bc07eb5 | 176 | #endif |
fblanc | 7:de5b1bc07eb5 | 177 | #if defined( __KL25) |
fblanc | 7:de5b1bc07eb5 | 178 | led2=1.0; |
fblanc | 7:de5b1bc07eb5 | 179 | led3=0.0; |
fblanc | 7:de5b1bc07eb5 | 180 | led4=0.0; |
fblanc | 7:de5b1bc07eb5 | 181 | #endif |
fblanc | 7:de5b1bc07eb5 | 182 | speed=SPEED; |
fblanc | 7:de5b1bc07eb5 | 183 | start_mesure=0; |
fblanc | 5:832b59a73dec | 184 | while(1) { |
fblanc | 0:92dd9611bf80 | 185 | |
fblanc | 0:92dd9611bf80 | 186 | if (pc.readable()) { |
fblanc | 0:92dd9611bf80 | 187 | pc_rx(); |
fblanc | 0:92dd9611bf80 | 188 | } |
fblanc | 5:832b59a73dec | 189 | |
fblanc | 0:92dd9611bf80 | 190 | if(flag_write_USB==1) { |
fblanc | 0:92dd9611bf80 | 191 | flag_write_USB=0; |
fblanc | 2:88f866289381 | 192 | pc.printf("%04d%c\r",index_hma_read,'\0'); //'\0'fin de chaine ASCII '\r' fin de transmission |
fblanc | 6:fa9df9f03bfa | 193 | for(unsigned int i=0; i<index_hma_read*NBCHAN; i+=BLOCUSB ) { // 2 cannaux de 16bits |
fblanc | 8:213824bed8d5 | 194 | #if defined( __LPC1768_SDA) |
fblanc | 8:213824bed8d5 | 195 | char* ptr=(char*)&T_hma_u16[!t_write][0]; |
fblanc | 8:213824bed8d5 | 196 | |
fblanc | 8:213824bed8d5 | 197 | for(uint16_t i=0; i<index_hma_read; ++i) { |
fblanc | 8:213824bed8d5 | 198 | pc.putc(*ptr++); |
fblanc | 8:213824bed8d5 | 199 | pc.putc(*ptr++); |
fblanc | 8:213824bed8d5 | 200 | } |
fblanc | 8:213824bed8d5 | 201 | #else |
fblanc | 6:fa9df9f03bfa | 202 | 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 | 203 | #endif |
fblanc | 0:92dd9611bf80 | 204 | } |
fblanc | 0:92dd9611bf80 | 205 | |
fblanc | 0:92dd9611bf80 | 206 | } |
fblanc | 0:92dd9611bf80 | 207 | } |
fblanc | 0:92dd9611bf80 | 208 | } |