| 1 | /* |
| 2 | * baudcheck.c |
| 3 | * Mar, 2013 by Bill Westfield ([email protected]) |
| 4 | * Exercises in executing arithmetic code on a system that we can't count |
| 5 | * on having the usual languages or tools installed. |
| 6 | * |
| 7 | * This little "C program" is run through the C preprocessor using the same |
| 8 | * arguments as our "real" target (which should assure that it gets the |
| 9 | * same values for clock speed and desired baud rate), and produces as |
| 10 | * output a shell script that can be run through bash, and THAT in turn |
| 11 | * writes the desired output... |
| 12 | * |
| 13 | * Note that the C-style comments are stripped by the C preprocessor. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * First strip any trailing "L" from the defined constants. To do this |
| 18 | * we need to make the constants into shell variables first. |
| 19 | */ |
| 20 | bpsx=BAUD_RATE |
| 21 | bps=${bpsx/L/} |
| 22 | fcpux=F_CPU |
| 23 | fcpu=${fcpux/L/} |
| 24 | |
| 25 | // echo f_cpu = $fcpu, baud = $bps |
| 26 | /* |
| 27 | * Compute the divisor |
| 28 | */ |
| 29 | BAUD_SETTING=$(( ( ($fcpu + $bps * 4) / (($bps * 8))) - 1 )) |
| 30 | // echo baud setting = $BAUD_SETTING |
| 31 | |
| 32 | /* |
| 33 | * Based on the computer divisor, calculate the actual bitrate, |
| 34 | * And the error. Since we're all integers, we have to calculate |
| 35 | * the tenths part of the error separately. |
| 36 | */ |
| 37 | BAUD_ACTUAL=$(( ($fcpu/(8 * (($BAUD_SETTING)+1))) )) |
| 38 | BAUD_ERROR=$(( (( 100*($bps - $BAUD_ACTUAL) ) / $bps) )) |
| 39 | ERR_TS=$(( ((( 1000*($bps - $BAUD_ACTUAL) ) / $bps) - $BAUD_ERROR * 10) )) |
| 40 | ERR_TENTHS=$(( ERR_TS > 0 ? ERR_TS: -ERR_TS )) |
| 41 | |
| 42 | /* |
| 43 | * Print a nice message containing the info we've calculated |
| 44 | */ |
| 45 | echo BAUD RATE CHECK: Desired: $bps, Real: $BAUD_ACTUAL, UBRRL = $BAUD_SETTING, Error=$BAUD_ERROR.$ERR_TENTHS\% |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 |