Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FastIO by
main.cpp
00001 #include "mbed.h" 00002 #include "FastIO.h" 00003 00004 DigitalInOut led1(LED1); 00005 FastInOut<LED2> led2; 00006 00007 Timer t; 00008 #define LOOPS 10000 00009 00010 void basic_test(); 00011 void fixed_write(); 00012 void variable_write(); 00013 void read(); 00014 void operator_toggle(); 00015 void input_output(); 00016 00017 void print_results(int digicount, int fastcount); 00018 00019 int main() { 00020 printf("\r\nStarting test bench\r\n"); 00021 00022 basic_test(); 00023 fixed_write(); 00024 variable_write(); 00025 read(); 00026 operator_toggle(); 00027 input_output(); 00028 00029 while(1); 00030 } 00031 00032 void print_results(int digicount, int fastcount) { 00033 float digicycles = (float)digicount / LOOPS * (float)SystemCoreClock / 1000000.0f; 00034 float fastcycles = (float)fastcount / LOOPS * (float)SystemCoreClock / 1000000.0f; 00035 printf("Standard took %.2f cycles, FastIO took %.2f cycles, which is %d%%\r\n", digicycles, fastcycles,(int)(100.0f*fastcycles/digicycles + 0.5)); 00036 printf("Standard took %.0fns, FastIO took %.0fns\r\n", (float)digicount/LOOPS * 1000, (float)fastcount/LOOPS * 1000); 00037 } 00038 00039 void basic_test() { 00040 printf("Verifying basic behavior\r\n"); 00041 int error = 0; 00042 led1.output(); 00043 led2.output(); 00044 led1 = 1; 00045 led2 = 1; 00046 error += (led1.read() != 1); 00047 error += (led2.read() != 1); 00048 led1 = 0; 00049 led2 = 0; 00050 error += (led1.read() != 0); 00051 error += (led2.read() != 0); 00052 00053 if (error == 0) 00054 printf("Basic behavior verified\r\n"); 00055 else { 00056 printf("Error in basic behavior\r\n"); 00057 while(1); 00058 } 00059 } 00060 00061 00062 void fixed_write() { 00063 int overhead; 00064 int digitalinout; 00065 int fastinout; 00066 int count; 00067 00068 printf("\nMeasuring fixed write pattern speed\r\n"); 00069 led1.output(); 00070 led2.output(); 00071 00072 //Calculate loop overhead 00073 count = LOOPS / 2; 00074 t.reset(); 00075 t.start(); 00076 while ( count -- ) { 00077 led1.write(1); 00078 led1.write(0); 00079 } 00080 t.stop(); 00081 overhead = t.read_us(); 00082 00083 count = LOOPS / 2; 00084 t.reset(); 00085 t.start(); 00086 while ( count -- ) 00087 { 00088 led1.write(1); 00089 led1.write(0); 00090 led1.write(1); 00091 led1.write(0); 00092 } 00093 t.stop(); 00094 digitalinout = t.read_us() - overhead; 00095 00096 //Calculate loop overhead 00097 count = LOOPS / 2; 00098 t.reset(); 00099 t.start(); 00100 while ( count -- ) { 00101 led2.write(1); 00102 led2.write(0); 00103 } 00104 t.stop(); 00105 overhead = t.read_us(); 00106 00107 count = LOOPS / 2; 00108 t.reset(); 00109 t.start(); 00110 while ( count -- ) 00111 { 00112 led2.write(1); 00113 led2.write(0); 00114 led2.write(1); 00115 led2.write(0); 00116 } 00117 t.stop(); 00118 fastinout = t.read_us() - overhead; 00119 00120 print_results(digitalinout, fastinout); 00121 } 00122 00123 void variable_write() { 00124 int overhead; 00125 int digitalinout; 00126 int fastinout; 00127 int count; 00128 00129 printf("\nMeasuring variable write pattern speed\r\n"); 00130 led1.output(); 00131 led2.output(); 00132 00133 //Calculate loop overhead 00134 int value = 1; 00135 count = LOOPS / 2; 00136 t.reset(); 00137 t.start(); 00138 while ( count -- ) { 00139 value = value - 1; led1.write(value); 00140 value = value - 1; 00141 value = value - 1; 00142 value = value - 1; led1.write(value); 00143 } 00144 00145 t.stop(); 00146 overhead = t.read_us(); 00147 00148 count = LOOPS / 2; 00149 t.reset(); 00150 t.start(); 00151 while ( count -- ) 00152 { 00153 value = value - 1; led1.write(value); 00154 value = value - 1; led1.write(value); 00155 value = value - 1; led1.write(value); 00156 value = value - 1; led1.write(value); 00157 } 00158 t.stop(); 00159 digitalinout = t.read_us() - overhead; 00160 00161 count = LOOPS / 2; 00162 t.reset(); 00163 t.start(); 00164 while ( count -- ) { 00165 value = value - 1; led2.write(value); 00166 value = value - 1; 00167 value = value - 1; 00168 value = value - 1; led2.write(value); 00169 } 00170 00171 t.stop(); 00172 overhead = t.read_us(); 00173 00174 count = LOOPS / 2; 00175 t.reset(); 00176 t.start(); 00177 while ( count -- ) 00178 { 00179 value = value - 1; led2.write(value); 00180 value = value - 1; led2.write(value); 00181 value = value - 1; led2.write(value); 00182 value = value - 1; led2.write(value); 00183 } 00184 t.stop(); 00185 fastinout = t.read_us() - overhead; 00186 print_results(digitalinout, fastinout); 00187 } 00188 00189 void read() { 00190 int overhead; 00191 int digitalinout; 00192 int fastinout; 00193 int count; 00194 00195 printf("\nMeasuring read speed\r\n"); 00196 led1.input(); led1.mode(PullUp); 00197 led2.input(); led2.mode(PullUp); 00198 00199 //Calculate loop overhead 00200 count = LOOPS; 00201 t.reset(); 00202 t.start(); 00203 while ( count -- ) { 00204 } 00205 t.stop(); 00206 overhead = t.read_us(); 00207 00208 count = LOOPS; 00209 t.reset(); 00210 t.start(); 00211 while ( count -- ) 00212 { 00213 if (led1.read() == 2) //This shouldn't happen due to pull-up, if you get weird results, it is happening 00214 break; 00215 } 00216 t.stop(); 00217 digitalinout = t.read_us() - overhead; 00218 00219 count = LOOPS; 00220 t.reset(); 00221 t.start(); 00222 while ( count -- ) 00223 { 00224 if (led2.read() == 2) //This shouldn't happen due to pull-up, if you get weird results, it is happening 00225 break; 00226 } 00227 t.stop(); 00228 fastinout = t.read_us() - overhead; 00229 00230 print_results(digitalinout, fastinout); 00231 } 00232 00233 void operator_toggle() { 00234 int overhead; 00235 int digitalinout; 00236 int fastinout; 00237 int count; 00238 00239 printf("\nMeasuring toggling using operators speed\r\n"); 00240 led1.output(); 00241 led2.output(); 00242 00243 //Calculate loop overhead 00244 count = LOOPS / 2; 00245 t.reset(); 00246 t.start(); 00247 while ( count -- ) { 00248 led1 = !led1; 00249 led1 = !led1; 00250 } 00251 t.stop(); 00252 overhead = t.read_us(); 00253 00254 count = LOOPS / 2; 00255 t.reset(); 00256 t.start(); 00257 while ( count -- ) { 00258 led1 = !led1; 00259 led1 = !led1; 00260 led1 = !led1; 00261 led1 = !led1; 00262 } 00263 t.stop(); 00264 digitalinout = t.read_us() - overhead; 00265 00266 count = LOOPS / 2; 00267 t.reset(); 00268 t.start(); 00269 while ( count -- ) { 00270 led2 = !led2; 00271 led2 = !led2; 00272 } 00273 t.stop(); 00274 overhead = t.read_us(); 00275 00276 count = LOOPS / 2; 00277 t.reset(); 00278 t.start(); 00279 while ( count -- ) 00280 { 00281 led2 = !led2; 00282 led2 = !led2; 00283 led2 = !led2; 00284 led2 = !led2; 00285 } 00286 t.stop(); 00287 fastinout = t.read_us() - overhead; 00288 print_results(digitalinout, fastinout); 00289 } 00290 00291 void input_output() { 00292 int overhead; 00293 int digitalinout; 00294 int fastinout; 00295 int count; 00296 00297 printf("\nMeasuring switching between input and output\r\n"); 00298 00299 //Calculate loop overhead 00300 count = LOOPS / 2; 00301 t.reset(); 00302 t.start(); 00303 while ( count -- ) { 00304 led1.input(); 00305 led1.output(); 00306 } 00307 t.stop(); 00308 overhead = t.read_us(); 00309 00310 count = LOOPS / 2; 00311 t.reset(); 00312 t.start(); 00313 while ( count -- ) { 00314 led1.input(); 00315 led1.output(); 00316 led1.input(); 00317 led1.output(); 00318 } 00319 t.stop(); 00320 digitalinout = t.read_us() - overhead; 00321 00322 count = LOOPS / 2; 00323 t.reset(); 00324 t.start(); 00325 while ( count -- ) { 00326 led2.input(); 00327 led2.output(); 00328 } 00329 t.stop(); 00330 overhead = t.read_us(); 00331 00332 count = LOOPS / 2; 00333 t.reset(); 00334 t.start(); 00335 while ( count -- ) 00336 { 00337 led2.input(); 00338 led2.output(); 00339 led2.input(); 00340 led2.output(); 00341 } 00342 t.stop(); 00343 fastinout = t.read_us() - overhead; 00344 print_results(digitalinout, fastinout); 00345 }
Generated on Tue Jul 19 2022 15:02:24 by
1.7.2
