Clock generator @ 145 MHz and 7 mhz
Fork of Check_Si5351A_Clock_generator by
si5351a_check.cpp@4:5981cf106502, 2017-01-07 (annotated)
- Committer:
- kenjiArai
- Date:
- Sat Jan 07 05:57:55 2017 +0000
- Revision:
- 4:5981cf106502
- Parent:
- 3:92f31b23fb0b
- Child:
- 5:e32ef4b85317
support RANGE_EXTENDED mode, Freq range=1.627KHz to 300MHz, You need to modify si5351a.h definition (CAUTION : Out of specification)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:d68f1e7f7f49 | 1 | /* |
kenjiArai | 0:d68f1e7f7f49 | 2 | * mbed Application program / Si5351A sample program |
kenjiArai | 0:d68f1e7f7f49 | 3 | * tested on: |
kenjiArai | 0:d68f1e7f7f49 | 4 | * ST Nucleo-F411RE & F401RE |
kenjiArai | 0:d68f1e7f7f49 | 5 | * LPC1114FN28 |
kenjiArai | 0:d68f1e7f7f49 | 6 | * |
kenjiArai | 0:d68f1e7f7f49 | 7 | * Copyright (c) 2016,'17 Kenji Arai / JH1PJL |
kenjiArai | 0:d68f1e7f7f49 | 8 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:d68f1e7f7f49 | 9 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 0:d68f1e7f7f49 | 10 | * Created: December 28th, 2016 |
kenjiArai | 4:5981cf106502 | 11 | * Revised: January 7th, 2017 |
kenjiArai | 0:d68f1e7f7f49 | 12 | * |
kenjiArai | 0:d68f1e7f7f49 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
kenjiArai | 0:d68f1e7f7f49 | 14 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
kenjiArai | 0:d68f1e7f7f49 | 15 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
kenjiArai | 0:d68f1e7f7f49 | 16 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 0:d68f1e7f7f49 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
kenjiArai | 0:d68f1e7f7f49 | 18 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR |
kenjiArai | 0:d68f1e7f7f49 | 19 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 0:d68f1e7f7f49 | 20 | */ |
kenjiArai | 0:d68f1e7f7f49 | 21 | |
kenjiArai | 0:d68f1e7f7f49 | 22 | // Include -------------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 23 | #include "mbed.h" |
kenjiArai | 0:d68f1e7f7f49 | 24 | #include "si5351a.h" |
kenjiArai | 0:d68f1e7f7f49 | 25 | #include "si5351a_check.h" |
kenjiArai | 0:d68f1e7f7f49 | 26 | |
kenjiArai | 0:d68f1e7f7f49 | 27 | // Definition ----------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 28 | |
kenjiArai | 0:d68f1e7f7f49 | 29 | // Object --------------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 30 | Serial pc(USBTX, USBRX); |
kenjiArai | 0:d68f1e7f7f49 | 31 | //I2C i2c(dp5, dp27); // communication with Si5351A |
kenjiArai | 0:d68f1e7f7f49 | 32 | I2C i2c(I2C_SDA, I2C_SCL); // communication with Si5351A |
kenjiArai | 0:d68f1e7f7f49 | 33 | SI5351A clk(i2c, 25000000UL); // base clock = 25.0MHz |
kenjiArai | 0:d68f1e7f7f49 | 34 | |
kenjiArai | 3:92f31b23fb0b | 35 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 3:92f31b23fb0b | 36 | bool one_char = false; |
kenjiArai | 3:92f31b23fb0b | 37 | |
kenjiArai | 4:5981cf106502 | 38 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 4:5981cf106502 | 39 | #if defined(RANGE_EXTENDED) |
kenjiArai | 4:5981cf106502 | 40 | static const uint32_t freq_tbl[] = |
kenjiArai | 4:5981cf106502 | 41 | { |
kenjiArai | 4:5981cf106502 | 42 | 1628, 1800, 1900, 2000, 2500, 2604, 2605, 2700, 3000, // 1.627KHz-3KHz |
kenjiArai | 4:5981cf106502 | 43 | // Above includes out of range freq. |
kenjiArai | 4:5981cf106502 | 44 | 4000, 5000, 8000, // 4KHz-8KHz |
kenjiArai | 4:5981cf106502 | 45 | 10000, 20000, 50000, 80000, // 10KHz-80KHz |
kenjiArai | 4:5981cf106502 | 46 | 100000, 200000, 500000, 800000, // 100KHz-800KHz |
kenjiArai | 4:5981cf106502 | 47 | 1000000, 2000000, 5000000, 8000000, // 1MHz-8MHz |
kenjiArai | 4:5981cf106502 | 48 | 10000000, 20000000, 50000000, 80000000, // 10MHz-80MHz |
kenjiArai | 4:5981cf106502 | 49 | 100000000, 150000000, 200000000, // 100MHz-200MHz |
kenjiArai | 4:5981cf106502 | 50 | // Belows are out of Si5351A specification !!!! |
kenjiArai | 4:5981cf106502 | 51 | 220000000, 250000000, 280000000, 300000000,// 200MHz-300MHz |
kenjiArai | 4:5981cf106502 | 52 | 0 // Terminate data!! |
kenjiArai | 4:5981cf106502 | 53 | }; |
kenjiArai | 4:5981cf106502 | 54 | #else |
kenjiArai | 4:5981cf106502 | 55 | static const uint32_t freq_tbl[] = |
kenjiArai | 4:5981cf106502 | 56 | { |
kenjiArai | 4:5981cf106502 | 57 | 2605, 4000, 5000, 8000, // 2.6KHz-8KHz |
kenjiArai | 4:5981cf106502 | 58 | 10000, 20000, 50000, 80000, // 10KHz-80KHz |
kenjiArai | 4:5981cf106502 | 59 | 100000, 200000, 500000, 800000, // 100KHz-800KHz |
kenjiArai | 4:5981cf106502 | 60 | 1000000, 2000000, 5000000, 8000000, // 1MHz-8MHz |
kenjiArai | 4:5981cf106502 | 61 | 10000000, 20000000, 50000000, 80000000, // 10MHz-80MHz |
kenjiArai | 4:5981cf106502 | 62 | 100000000, 150000000, 200000000, // 100MHz-200MHz |
kenjiArai | 4:5981cf106502 | 63 | 0 // Terminate data!! |
kenjiArai | 4:5981cf106502 | 64 | }; |
kenjiArai | 4:5981cf106502 | 65 | #endif |
kenjiArai | 4:5981cf106502 | 66 | |
kenjiArai | 0:d68f1e7f7f49 | 67 | //------------------------------------------------------------------------------ |
kenjiArai | 0:d68f1e7f7f49 | 68 | // Control Program |
kenjiArai | 0:d68f1e7f7f49 | 69 | //------------------------------------------------------------------------------ |
kenjiArai | 0:d68f1e7f7f49 | 70 | int main() |
kenjiArai | 0:d68f1e7f7f49 | 71 | { |
kenjiArai | 0:d68f1e7f7f49 | 72 | while (true){ test_si5351();} |
kenjiArai | 0:d68f1e7f7f49 | 73 | } |
kenjiArai | 0:d68f1e7f7f49 | 74 | |
kenjiArai | 0:d68f1e7f7f49 | 75 | // Help Massage |
kenjiArai | 0:d68f1e7f7f49 | 76 | void msg_hlp (void) |
kenjiArai | 0:d68f1e7f7f49 | 77 | { |
kenjiArai | 0:d68f1e7f7f49 | 78 | PRINTF(opening_msg); |
kenjiArai | 0:d68f1e7f7f49 | 79 | put_rn(); |
kenjiArai | 4:5981cf106502 | 80 | #if defined(RANGE_EXTENDED) |
kenjiArai | 4:5981cf106502 | 81 | PRINTF("0 - Check CLK0 output between 1.627KHz to 300MHz"); |
kenjiArai | 4:5981cf106502 | 82 | put_rn(); |
kenjiArai | 4:5981cf106502 | 83 | PRINTF("-> You are using RANGE_EXTENDED (defined in si5351a.h)"); |
kenjiArai | 4:5981cf106502 | 84 | put_rn(); |
kenjiArai | 4:5981cf106502 | 85 | PRINTF("-> In RANGE_EXTENDED mode, PLL sets 250MHz to 1.3GHz"); |
kenjiArai | 4:5981cf106502 | 86 | put_rn(); |
kenjiArai | 4:5981cf106502 | 87 | PRINTF("-> This causes high possibility to make a PARMANENT DAMAGE"); |
kenjiArai | 4:5981cf106502 | 88 | PRINTF(" for the chip!"); |
kenjiArai | 4:5981cf106502 | 89 | put_rn(); |
kenjiArai | 4:5981cf106502 | 90 | PRINTF("-> Don't use this mode!!!"); |
kenjiArai | 4:5981cf106502 | 91 | #else |
kenjiArai | 4:5981cf106502 | 92 | PRINTF("0 - Check CLK0 output between 2.605KHz to 200MHz"); |
kenjiArai | 4:5981cf106502 | 93 | #endif |
kenjiArai | 0:d68f1e7f7f49 | 94 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 95 | PRINTF("1 - Check CLK1 (same as #1)"); |
kenjiArai | 0:d68f1e7f7f49 | 96 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 97 | PRINTF("2 - Check CLK2 (same as #1)"); |
kenjiArai | 0:d68f1e7f7f49 | 98 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 99 | PRINTF("3 - Example CLK0:120.00MHz, CLK1:12.00MHz, CLK2:13.56MHz"); |
kenjiArai | 4:5981cf106502 | 100 | PRINTF("-> No wat to apply a frequency compensation"); |
kenjiArai | 0:d68f1e7f7f49 | 101 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 102 | PRINTF("4 - Set desired freq. into CLK0"); |
kenjiArai | 0:d68f1e7f7f49 | 103 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 104 | PRINTF("5 - Set desired freq. into CLK0 & CLK1"); |
kenjiArai | 0:d68f1e7f7f49 | 105 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 106 | PRINTF("6 - Set desired freq. into CLK0, CLK1 & CLK2"); |
kenjiArai | 0:d68f1e7f7f49 | 107 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 108 | PRINTF("7 - CLK0: set desired center freq. then shift +/- range"); |
kenjiArai | 0:d68f1e7f7f49 | 109 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 110 | PRINTF("a - All clear registers"); |
kenjiArai | 0:d68f1e7f7f49 | 111 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 112 | PRINTF("c - Show current configration"); |
kenjiArai | 0:d68f1e7f7f49 | 113 | put_rn(); |
kenjiArai | 3:92f31b23fb0b | 114 | PRINTF("p - Compensation"); |
kenjiArai | 3:92f31b23fb0b | 115 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 116 | PRINTF("r - Show current resisters"); |
kenjiArai | 0:d68f1e7f7f49 | 117 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 118 | PRINTF("x - Go to special command"); |
kenjiArai | 0:d68f1e7f7f49 | 119 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 120 | PRINTF("Hit any key then return to prompt screen"); |
kenjiArai | 0:d68f1e7f7f49 | 121 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 122 | PRINTF("Reset mbed board -> please hit key <Alt>+<B>"); |
kenjiArai | 0:d68f1e7f7f49 | 123 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 124 | } |
kenjiArai | 0:d68f1e7f7f49 | 125 | |
kenjiArai | 0:d68f1e7f7f49 | 126 | // ---------- Program starts here! --------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 127 | void test_si5351(void) |
kenjiArai | 0:d68f1e7f7f49 | 128 | { |
kenjiArai | 0:d68f1e7f7f49 | 129 | char *ptr; |
kenjiArai | 0:d68f1e7f7f49 | 130 | int32_t f; |
kenjiArai | 0:d68f1e7f7f49 | 131 | |
kenjiArai | 0:d68f1e7f7f49 | 132 | BAUD(BAUD_RATE); |
kenjiArai | 0:d68f1e7f7f49 | 133 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 134 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 135 | PRINTF("%s [Help:'?' key]", opening_msg); |
kenjiArai | 0:d68f1e7f7f49 | 136 | put_rn(); |
kenjiArai | 4:5981cf106502 | 137 | #if defined(RANGE_EXTENDED) |
kenjiArai | 4:5981cf106502 | 138 | PRINTF("-> You are using RANGE_EXTENDED (defined in si5351a.h)"); |
kenjiArai | 4:5981cf106502 | 139 | put_rn(); |
kenjiArai | 4:5981cf106502 | 140 | PRINTF("-> In RANGE_EXTENDED mode, PLL sets 375MHz to 1.3GHz"); |
kenjiArai | 4:5981cf106502 | 141 | put_rn(); |
kenjiArai | 4:5981cf106502 | 142 | PRINTF("-> This causes high possibility to make a PARMANENT DAMAGE"); |
kenjiArai | 4:5981cf106502 | 143 | PRINTF(" for the chip!"); |
kenjiArai | 4:5981cf106502 | 144 | put_rn(); |
kenjiArai | 4:5981cf106502 | 145 | #endif |
kenjiArai | 0:d68f1e7f7f49 | 146 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 147 | for (;;) { |
kenjiArai | 0:d68f1e7f7f49 | 148 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 149 | PUTC('>'); |
kenjiArai | 0:d68f1e7f7f49 | 150 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 151 | get_line(ptr, sizeof(linebuf)); |
kenjiArai | 0:d68f1e7f7f49 | 152 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 153 | switch (*ptr++) { |
kenjiArai | 0:d68f1e7f7f49 | 154 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 155 | // test0 |
kenjiArai | 0:d68f1e7f7f49 | 156 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 157 | case '0' : |
kenjiArai | 0:d68f1e7f7f49 | 158 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 159 | test(0); |
kenjiArai | 0:d68f1e7f7f49 | 160 | //put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 161 | break; |
kenjiArai | 0:d68f1e7f7f49 | 162 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 163 | // test1 |
kenjiArai | 0:d68f1e7f7f49 | 164 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 165 | case '1' : |
kenjiArai | 0:d68f1e7f7f49 | 166 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 167 | test(1); |
kenjiArai | 0:d68f1e7f7f49 | 168 | //put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 169 | break; |
kenjiArai | 0:d68f1e7f7f49 | 170 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 171 | // test2 |
kenjiArai | 0:d68f1e7f7f49 | 172 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 173 | case '2' : |
kenjiArai | 0:d68f1e7f7f49 | 174 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 175 | test(2); |
kenjiArai | 0:d68f1e7f7f49 | 176 | //put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 177 | break; |
kenjiArai | 0:d68f1e7f7f49 | 178 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 179 | // test3 |
kenjiArai | 0:d68f1e7f7f49 | 180 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 181 | case '3' : |
kenjiArai | 0:d68f1e7f7f49 | 182 | clk.debug_example_clock(); |
kenjiArai | 0:d68f1e7f7f49 | 183 | //clk.debug_reg_print(); |
kenjiArai | 0:d68f1e7f7f49 | 184 | //put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 185 | break; |
kenjiArai | 0:d68f1e7f7f49 | 186 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 187 | // test4 |
kenjiArai | 0:d68f1e7f7f49 | 188 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 189 | case '4' : |
kenjiArai | 0:d68f1e7f7f49 | 190 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 191 | PRINTF("CLK0 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 192 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 193 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 194 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 195 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 196 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 197 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 198 | PRINTF("CLK0:%u [Hz]", clk.set_frequency(0, f)); |
kenjiArai | 0:d68f1e7f7f49 | 199 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 200 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 201 | } |
kenjiArai | 0:d68f1e7f7f49 | 202 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 203 | break; |
kenjiArai | 0:d68f1e7f7f49 | 204 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 205 | // test5 |
kenjiArai | 0:d68f1e7f7f49 | 206 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 207 | case '5' : |
kenjiArai | 0:d68f1e7f7f49 | 208 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 209 | PRINTF("CLK0 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 210 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 211 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 212 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 213 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 214 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 215 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 216 | PRINTF("CLK0:%u [Hz]", clk.set_frequency(0, f)); |
kenjiArai | 0:d68f1e7f7f49 | 217 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 218 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 219 | } |
kenjiArai | 0:d68f1e7f7f49 | 220 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 221 | PRINTF("CLK1 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 222 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 223 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 224 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 225 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 226 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 227 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 228 | PRINTF("CLK1:%u [Hz]", clk.set_frequency(1, f)); |
kenjiArai | 0:d68f1e7f7f49 | 229 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 230 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 231 | } |
kenjiArai | 0:d68f1e7f7f49 | 232 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 233 | break; |
kenjiArai | 0:d68f1e7f7f49 | 234 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 235 | // test6 |
kenjiArai | 0:d68f1e7f7f49 | 236 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 237 | case '6' : |
kenjiArai | 0:d68f1e7f7f49 | 238 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 239 | PRINTF("CLK0 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 240 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 241 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 242 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 243 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 244 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 245 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 246 | PRINTF("CLK0:%u [Hz]", clk.set_frequency(0, f)); |
kenjiArai | 0:d68f1e7f7f49 | 247 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 248 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 249 | } |
kenjiArai | 0:d68f1e7f7f49 | 250 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 251 | PRINTF("CLK1 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 252 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 253 | PRINTF( |
kenjiArai | 0:d68f1e7f7f49 | 254 | "If you set CLK1 over 100MHz, you cannt set CLK2 anymore"); |
kenjiArai | 0:d68f1e7f7f49 | 255 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 256 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 257 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 258 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 259 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 260 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 261 | PRINTF("CLK1:%u [Hz]", clk.set_frequency(1, f)); |
kenjiArai | 0:d68f1e7f7f49 | 262 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 263 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 264 | } |
kenjiArai | 0:d68f1e7f7f49 | 265 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 266 | PRINTF("CLK2 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 267 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 268 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 269 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 270 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 271 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 272 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 273 | PRINTF("CLK2:%u [Hz]", clk.set_frequency(2, f)); |
kenjiArai | 0:d68f1e7f7f49 | 274 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 275 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 276 | } |
kenjiArai | 0:d68f1e7f7f49 | 277 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 278 | break; |
kenjiArai | 0:d68f1e7f7f49 | 279 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 280 | // test7 CLK0: set desired center freq. then shift +/- range |
kenjiArai | 0:d68f1e7f7f49 | 281 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 282 | case '7' : |
kenjiArai | 3:92f31b23fb0b | 283 | one_char = true; |
kenjiArai | 0:d68f1e7f7f49 | 284 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 285 | PRINTF("CLK0 %s", msg0); |
kenjiArai | 0:d68f1e7f7f49 | 286 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 287 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 288 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 289 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 290 | if (xatoi(&ptr,&f)){ |
kenjiArai | 0:d68f1e7f7f49 | 291 | if (f < 0){ break;} |
kenjiArai | 0:d68f1e7f7f49 | 292 | PRINTF("CLK0:%u [Hz]", clk.set_frequency(0, f)); |
kenjiArai | 0:d68f1e7f7f49 | 293 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 294 | PUTC('?'); |
kenjiArai | 4:5981cf106502 | 295 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 296 | } |
kenjiArai | 0:d68f1e7f7f49 | 297 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 298 | PRINTF("Please enter sift command [Help: ?]"); |
kenjiArai | 0:d68f1e7f7f49 | 299 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 300 | while (true){ |
kenjiArai | 0:d68f1e7f7f49 | 301 | PRINTF("[S]>"); |
kenjiArai | 0:d68f1e7f7f49 | 302 | ptr = linebuf; |
kenjiArai | 0:d68f1e7f7f49 | 303 | get_line(ptr, buf_size); |
kenjiArai | 0:d68f1e7f7f49 | 304 | put_r(); |
kenjiArai | 0:d68f1e7f7f49 | 305 | switch(*ptr) { |
kenjiArai | 4:5981cf106502 | 306 | case 'P' : |
kenjiArai | 4:5981cf106502 | 307 | PRINTF("Shift to %u [Hz]", clk.shift_freq(0, 100)); |
kenjiArai | 4:5981cf106502 | 308 | put_rn(); |
kenjiArai | 4:5981cf106502 | 309 | break; |
kenjiArai | 0:d68f1e7f7f49 | 310 | case 'p' : |
kenjiArai | 0:d68f1e7f7f49 | 311 | PRINTF("Shift to %u [Hz]", clk.shift_freq(0, 10)); |
kenjiArai | 0:d68f1e7f7f49 | 312 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 313 | break; |
kenjiArai | 4:5981cf106502 | 314 | case 'M' : |
kenjiArai | 4:5981cf106502 | 315 | PRINTF("Shift to %u [Hz]", clk.shift_freq(0, -100)); |
kenjiArai | 4:5981cf106502 | 316 | put_rn(); |
kenjiArai | 4:5981cf106502 | 317 | break; |
kenjiArai | 0:d68f1e7f7f49 | 318 | case 'm' : |
kenjiArai | 0:d68f1e7f7f49 | 319 | PRINTF("Shift to %u [Hz]", clk.shift_freq(0, -10)); |
kenjiArai | 0:d68f1e7f7f49 | 320 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 321 | break; |
kenjiArai | 0:d68f1e7f7f49 | 322 | case 's' : |
kenjiArai | 0:d68f1e7f7f49 | 323 | ptr++; |
kenjiArai | 0:d68f1e7f7f49 | 324 | xatoi(&ptr,&f); |
kenjiArai | 0:d68f1e7f7f49 | 325 | PRINTF("offset=%d\r\n", f); |
kenjiArai | 0:d68f1e7f7f49 | 326 | PRINTF("Shift to %u [Hz]", clk.shift_freq(0, f)); |
kenjiArai | 0:d68f1e7f7f49 | 327 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 328 | break; |
kenjiArai | 0:d68f1e7f7f49 | 329 | case '?' : |
kenjiArai | 4:5981cf106502 | 330 | PRINTF("p -> +10Hz, P -> +100Hz"); |
kenjiArai | 0:d68f1e7f7f49 | 331 | put_rn(); |
kenjiArai | 4:5981cf106502 | 332 | PRINTF("m -> -10Hz, M -> -100Hz"); |
kenjiArai | 0:d68f1e7f7f49 | 333 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 334 | PRINTF("s -> Shift +/-x Hz (example -100)"); |
kenjiArai | 4:5981cf106502 | 335 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 336 | PRINTF("? -> You know this"); |
kenjiArai | 0:d68f1e7f7f49 | 337 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 338 | PRINTF("e -> Exit"); |
kenjiArai | 0:d68f1e7f7f49 | 339 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 340 | break; |
kenjiArai | 0:d68f1e7f7f49 | 341 | case 'e' : |
kenjiArai | 0:d68f1e7f7f49 | 342 | break; |
kenjiArai | 0:d68f1e7f7f49 | 343 | default: |
kenjiArai | 0:d68f1e7f7f49 | 344 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 345 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 346 | } |
kenjiArai | 0:d68f1e7f7f49 | 347 | if (*ptr == 'e'){ |
kenjiArai | 0:d68f1e7f7f49 | 348 | break; |
kenjiArai | 0:d68f1e7f7f49 | 349 | } |
kenjiArai | 0:d68f1e7f7f49 | 350 | } |
kenjiArai | 3:92f31b23fb0b | 351 | one_char = false; |
kenjiArai | 0:d68f1e7f7f49 | 352 | break; |
kenjiArai | 0:d68f1e7f7f49 | 353 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 354 | // All clear alll registers |
kenjiArai | 0:d68f1e7f7f49 | 355 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 356 | case 'a' : |
kenjiArai | 0:d68f1e7f7f49 | 357 | clk.all_reset(); |
kenjiArai | 0:d68f1e7f7f49 | 358 | PRINTF("Cleared all of registers!\r\n"); |
kenjiArai | 0:d68f1e7f7f49 | 359 | break; |
kenjiArai | 0:d68f1e7f7f49 | 360 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 361 | // Show configration |
kenjiArai | 0:d68f1e7f7f49 | 362 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 363 | case 'c' : |
kenjiArai | 0:d68f1e7f7f49 | 364 | clk.debug_current_config(); |
kenjiArai | 0:d68f1e7f7f49 | 365 | //put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 366 | break; |
kenjiArai | 0:d68f1e7f7f49 | 367 | //-------------------------------------------------------------- |
kenjiArai | 3:92f31b23fb0b | 368 | // Compensation |
kenjiArai | 3:92f31b23fb0b | 369 | //-------------------------------------------------------------- |
kenjiArai | 3:92f31b23fb0b | 370 | case 'p' : |
kenjiArai | 3:92f31b23fb0b | 371 | clk.set_frequency(0, 25000000); // Set CLK0=10MHz |
kenjiArai | 3:92f31b23fb0b | 372 | PRINTF( |
kenjiArai | 3:92f31b23fb0b | 373 | "Please measure Clock0 output frequency (aound 25MHz)\r\n"); |
kenjiArai | 3:92f31b23fb0b | 374 | PRINTF("Enter your mesured data = "); |
kenjiArai | 3:92f31b23fb0b | 375 | ptr = linebuf; |
kenjiArai | 3:92f31b23fb0b | 376 | get_line(ptr, buf_size); |
kenjiArai | 3:92f31b23fb0b | 377 | if (xatoi(&ptr,&f)){ |
kenjiArai | 3:92f31b23fb0b | 378 | if (f < 0){ |
kenjiArai | 3:92f31b23fb0b | 379 | PUTC('?'); |
kenjiArai | 3:92f31b23fb0b | 380 | break; |
kenjiArai | 3:92f31b23fb0b | 381 | } |
kenjiArai | 3:92f31b23fb0b | 382 | PRINTF("\r%u [Hz]\r\n", f); |
kenjiArai | 3:92f31b23fb0b | 383 | clk.f_compensation(25000000, f); |
kenjiArai | 3:92f31b23fb0b | 384 | clk.debug_current_config(); |
kenjiArai | 3:92f31b23fb0b | 385 | } else { |
kenjiArai | 3:92f31b23fb0b | 386 | PUTC('?'); |
kenjiArai | 4:5981cf106502 | 387 | } |
kenjiArai | 3:92f31b23fb0b | 388 | break; |
kenjiArai | 3:92f31b23fb0b | 389 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 390 | // Show resisters |
kenjiArai | 0:d68f1e7f7f49 | 391 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 392 | case 'r' : |
kenjiArai | 0:d68f1e7f7f49 | 393 | clk.debug_reg_print(); |
kenjiArai | 0:d68f1e7f7f49 | 394 | //put_rn(); |
kenjiArai | 4:5981cf106502 | 395 | break; |
kenjiArai | 0:d68f1e7f7f49 | 396 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 397 | // Go to special command |
kenjiArai | 0:d68f1e7f7f49 | 398 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 399 | case 'x' : |
kenjiArai | 0:d68f1e7f7f49 | 400 | special_command(); |
kenjiArai | 0:d68f1e7f7f49 | 401 | break; |
kenjiArai | 0:d68f1e7f7f49 | 402 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 403 | // help |
kenjiArai | 0:d68f1e7f7f49 | 404 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 405 | case '?' : |
kenjiArai | 0:d68f1e7f7f49 | 406 | msg_hlp(); |
kenjiArai | 0:d68f1e7f7f49 | 407 | break; |
kenjiArai | 0:d68f1e7f7f49 | 408 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 409 | // no support |
kenjiArai | 0:d68f1e7f7f49 | 410 | //-------------------------------------------------------------- |
kenjiArai | 0:d68f1e7f7f49 | 411 | default: |
kenjiArai | 0:d68f1e7f7f49 | 412 | PUTC('?'); |
kenjiArai | 0:d68f1e7f7f49 | 413 | put_rn(); |
kenjiArai | 0:d68f1e7f7f49 | 414 | break; |
kenjiArai | 0:d68f1e7f7f49 | 415 | } |
kenjiArai | 0:d68f1e7f7f49 | 416 | } |
kenjiArai | 0:d68f1e7f7f49 | 417 | } |
kenjiArai | 0:d68f1e7f7f49 | 418 | |
kenjiArai | 1:2b29f68043f7 | 419 | #if defined(INCREMENT) |
kenjiArai | 1:2b29f68043f7 | 420 | void test(uint8_t channel) |
kenjiArai | 1:2b29f68043f7 | 421 | { |
kenjiArai | 1:2b29f68043f7 | 422 | uint32_t f_set = 4000; // 4KHz |
kenjiArai | 1:2b29f68043f7 | 423 | double f = 0; |
kenjiArai | 1:2b29f68043f7 | 424 | uint32_t flg = 0; |
kenjiArai | 2:73d79424daa5 | 425 | char *ptr; |
kenjiArai | 2:73d79424daa5 | 426 | int32_t seconds = 1; |
kenjiArai | 2:73d79424daa5 | 427 | |
kenjiArai | 1:2b29f68043f7 | 428 | clk.all_reset(); |
kenjiArai | 1:2b29f68043f7 | 429 | f = clk.set_frequency(channel, f_set); |
kenjiArai | 1:2b29f68043f7 | 430 | clk.debug_reg_print(); |
kenjiArai | 1:2b29f68043f7 | 431 | PRINTF("CLK%u = %10.6f MHz\r\n", channel, f / 1000000.0f); |
kenjiArai | 2:73d79424daa5 | 432 | PRINTF("Please enter interval time[sec] -> "); |
kenjiArai | 2:73d79424daa5 | 433 | ptr = linebuf; |
kenjiArai | 2:73d79424daa5 | 434 | get_line(ptr, buf_size); |
kenjiArai | 2:73d79424daa5 | 435 | put_r(); |
kenjiArai | 2:73d79424daa5 | 436 | xatoi(&ptr,&seconds); |
kenjiArai | 2:73d79424daa5 | 437 | if (seconds <= 0){ seconds = 1;} |
kenjiArai | 2:73d79424daa5 | 438 | if (seconds > 1000){ seconds = 1000;} |
kenjiArai | 2:73d79424daa5 | 439 | PRINTF("%u[sec]\r\n", seconds); |
kenjiArai | 1:2b29f68043f7 | 440 | while(true){ |
kenjiArai | 1:2b29f68043f7 | 441 | f = clk.set_frequency(channel, f_set); |
kenjiArai | 1:2b29f68043f7 | 442 | PRINTF("CLK%u = %10.6f MHz\r\n", channel, f / 1000000.0f); |
kenjiArai | 1:2b29f68043f7 | 443 | if (flg){ |
kenjiArai | 1:2b29f68043f7 | 444 | f_set = f * 0.95f; |
kenjiArai | 1:2b29f68043f7 | 445 | if (f_set < 5000){ |
kenjiArai | 1:2b29f68043f7 | 446 | flg = 0; |
kenjiArai | 1:2b29f68043f7 | 447 | } |
kenjiArai | 1:2b29f68043f7 | 448 | } else { |
kenjiArai | 1:2b29f68043f7 | 449 | f_set = f * 1.05f; |
kenjiArai | 1:2b29f68043f7 | 450 | if (f_set > 190000000){ |
kenjiArai | 1:2b29f68043f7 | 451 | flg = 1; |
kenjiArai | 1:2b29f68043f7 | 452 | } |
kenjiArai | 1:2b29f68043f7 | 453 | } |
kenjiArai | 1:2b29f68043f7 | 454 | if (READABLE()) { |
kenjiArai | 1:2b29f68043f7 | 455 | GETC(); |
kenjiArai | 1:2b29f68043f7 | 456 | return; |
kenjiArai | 1:2b29f68043f7 | 457 | } |
kenjiArai | 2:73d79424daa5 | 458 | wait((float)seconds); |
kenjiArai | 1:2b29f68043f7 | 459 | } |
kenjiArai | 1:2b29f68043f7 | 460 | } |
kenjiArai | 1:2b29f68043f7 | 461 | #endif |
kenjiArai | 1:2b29f68043f7 | 462 | |
kenjiArai | 1:2b29f68043f7 | 463 | #if defined(TABLE_LOOK) |
kenjiArai | 1:2b29f68043f7 | 464 | void test(uint8_t channel) |
kenjiArai | 1:2b29f68043f7 | 465 | { |
kenjiArai | 1:2b29f68043f7 | 466 | uint32_t f_set = 0; |
kenjiArai | 1:2b29f68043f7 | 467 | uint32_t i,j; |
kenjiArai | 1:2b29f68043f7 | 468 | double f = 0; |
kenjiArai | 2:73d79424daa5 | 469 | char *ptr; |
kenjiArai | 2:73d79424daa5 | 470 | int32_t seconds = 1; |
kenjiArai | 2:73d79424daa5 | 471 | |
kenjiArai | 1:2b29f68043f7 | 472 | clk.all_reset(); |
kenjiArai | 2:73d79424daa5 | 473 | PRINTF("Please enter interval time[sec] -> "); |
kenjiArai | 2:73d79424daa5 | 474 | ptr = linebuf; |
kenjiArai | 2:73d79424daa5 | 475 | get_line(ptr, buf_size); |
kenjiArai | 2:73d79424daa5 | 476 | put_r(); |
kenjiArai | 2:73d79424daa5 | 477 | xatoi(&ptr,&seconds); |
kenjiArai | 2:73d79424daa5 | 478 | if (seconds <= 0){ seconds = 1;} |
kenjiArai | 2:73d79424daa5 | 479 | if (seconds > 1000){ seconds = 1000;} |
kenjiArai | 2:73d79424daa5 | 480 | PRINTF("%u[sec]\r\n", seconds); |
kenjiArai | 1:2b29f68043f7 | 481 | while(true){ |
kenjiArai | 1:2b29f68043f7 | 482 | for (i = 0; ; i++){ |
kenjiArai | 1:2b29f68043f7 | 483 | f_set = freq_tbl[i]; |
kenjiArai | 1:2b29f68043f7 | 484 | if (f_set == 0){ |
kenjiArai | 1:2b29f68043f7 | 485 | break; |
kenjiArai | 1:2b29f68043f7 | 486 | } |
kenjiArai | 1:2b29f68043f7 | 487 | f = clk.set_frequency(channel, f_set); |
kenjiArai | 1:2b29f68043f7 | 488 | PRINTF("CLK%u = %10.6f MHz\r\n", channel, f / 1000000.0f); |
kenjiArai | 1:2b29f68043f7 | 489 | if (READABLE()) { |
kenjiArai | 1:2b29f68043f7 | 490 | GETC(); |
kenjiArai | 1:2b29f68043f7 | 491 | return; |
kenjiArai | 1:2b29f68043f7 | 492 | } |
kenjiArai | 2:73d79424daa5 | 493 | wait((float)seconds); |
kenjiArai | 1:2b29f68043f7 | 494 | } |
kenjiArai | 1:2b29f68043f7 | 495 | for (j = i - 2; j > 0 ; j--){ |
kenjiArai | 1:2b29f68043f7 | 496 | f_set = freq_tbl[j]; |
kenjiArai | 1:2b29f68043f7 | 497 | f = clk.set_frequency(channel, f_set); |
kenjiArai | 1:2b29f68043f7 | 498 | PRINTF("CLK%u = %10.6f MHz\r\n", channel, f / 1000000.0f); |
kenjiArai | 1:2b29f68043f7 | 499 | if (READABLE()) { |
kenjiArai | 1:2b29f68043f7 | 500 | GETC(); |
kenjiArai | 1:2b29f68043f7 | 501 | return; |
kenjiArai | 1:2b29f68043f7 | 502 | } |
kenjiArai | 2:73d79424daa5 | 503 | wait((float)seconds); |
kenjiArai | 1:2b29f68043f7 | 504 | } |
kenjiArai | 1:2b29f68043f7 | 505 | } |
kenjiArai | 1:2b29f68043f7 | 506 | } |
kenjiArai | 1:2b29f68043f7 | 507 | #endif |
kenjiArai | 4:5981cf106502 | 508 | |
kenjiArai | 1:2b29f68043f7 | 509 | void special_command(void) |
kenjiArai | 1:2b29f68043f7 | 510 | { |
kenjiArai | 1:2b29f68043f7 | 511 | PRINTF("Not impliment yet"); |
kenjiArai | 1:2b29f68043f7 | 512 | put_rn(); |
kenjiArai | 1:2b29f68043f7 | 513 | } |
kenjiArai | 1:2b29f68043f7 | 514 | |
kenjiArai | 0:d68f1e7f7f49 | 515 | // Put \r\n |
kenjiArai | 0:d68f1e7f7f49 | 516 | void put_rn ( void ) |
kenjiArai | 0:d68f1e7f7f49 | 517 | { |
kenjiArai | 0:d68f1e7f7f49 | 518 | PUTC('\r'); |
kenjiArai | 0:d68f1e7f7f49 | 519 | PUTC('\n'); |
kenjiArai | 0:d68f1e7f7f49 | 520 | } |
kenjiArai | 0:d68f1e7f7f49 | 521 | |
kenjiArai | 0:d68f1e7f7f49 | 522 | // Put \r |
kenjiArai | 0:d68f1e7f7f49 | 523 | void put_r ( void ) |
kenjiArai | 0:d68f1e7f7f49 | 524 | { |
kenjiArai | 0:d68f1e7f7f49 | 525 | PUTC('\r'); |
kenjiArai | 0:d68f1e7f7f49 | 526 | } |
kenjiArai | 0:d68f1e7f7f49 | 527 | |
kenjiArai | 0:d68f1e7f7f49 | 528 | // Change string -> integer |
kenjiArai | 0:d68f1e7f7f49 | 529 | int xatoi (char **str, int32_t *res) |
kenjiArai | 0:d68f1e7f7f49 | 530 | { |
kenjiArai | 0:d68f1e7f7f49 | 531 | uint32_t val; |
kenjiArai | 0:d68f1e7f7f49 | 532 | int8_t c, radix, s = 0; |
kenjiArai | 0:d68f1e7f7f49 | 533 | |
kenjiArai | 0:d68f1e7f7f49 | 534 | while ((c = **str) == ' ') (*str)++; |
kenjiArai | 0:d68f1e7f7f49 | 535 | if (c == '-') { |
kenjiArai | 0:d68f1e7f7f49 | 536 | s = 1; |
kenjiArai | 0:d68f1e7f7f49 | 537 | c = *(++(*str)); |
kenjiArai | 0:d68f1e7f7f49 | 538 | } |
kenjiArai | 0:d68f1e7f7f49 | 539 | if (c == '+') { |
kenjiArai | 0:d68f1e7f7f49 | 540 | c = *(++(*str)); |
kenjiArai | 0:d68f1e7f7f49 | 541 | } |
kenjiArai | 0:d68f1e7f7f49 | 542 | if (c == '0') { |
kenjiArai | 0:d68f1e7f7f49 | 543 | c = *(++(*str)); |
kenjiArai | 0:d68f1e7f7f49 | 544 | if (c <= ' ') { |
kenjiArai | 0:d68f1e7f7f49 | 545 | *res = 0; |
kenjiArai | 0:d68f1e7f7f49 | 546 | return 1; |
kenjiArai | 0:d68f1e7f7f49 | 547 | } |
kenjiArai | 0:d68f1e7f7f49 | 548 | if (c == 'x') { |
kenjiArai | 0:d68f1e7f7f49 | 549 | radix = 16; |
kenjiArai | 0:d68f1e7f7f49 | 550 | c = *(++(*str)); |
kenjiArai | 0:d68f1e7f7f49 | 551 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 552 | if (c == 'b') { |
kenjiArai | 0:d68f1e7f7f49 | 553 | radix = 2; |
kenjiArai | 0:d68f1e7f7f49 | 554 | c = *(++(*str)); |
kenjiArai | 0:d68f1e7f7f49 | 555 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 556 | if ((c >= '0')&&(c <= '9')) { |
kenjiArai | 0:d68f1e7f7f49 | 557 | radix = 8; |
kenjiArai | 0:d68f1e7f7f49 | 558 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 559 | return 0; |
kenjiArai | 0:d68f1e7f7f49 | 560 | } |
kenjiArai | 0:d68f1e7f7f49 | 561 | } |
kenjiArai | 0:d68f1e7f7f49 | 562 | } |
kenjiArai | 0:d68f1e7f7f49 | 563 | } else { |
kenjiArai | 0:d68f1e7f7f49 | 564 | if ((c < '1')||(c > '9')) { |
kenjiArai | 0:d68f1e7f7f49 | 565 | return 0; |
kenjiArai | 0:d68f1e7f7f49 | 566 | } |
kenjiArai | 0:d68f1e7f7f49 | 567 | radix = 10; |
kenjiArai | 0:d68f1e7f7f49 | 568 | } |
kenjiArai | 0:d68f1e7f7f49 | 569 | val = 0; |
kenjiArai | 0:d68f1e7f7f49 | 570 | while (c > ' ') { |
kenjiArai | 0:d68f1e7f7f49 | 571 | if (c >= 'a') c -= 0x20; |
kenjiArai | 0:d68f1e7f7f49 | 572 | c -= '0'; |
kenjiArai | 0:d68f1e7f7f49 | 573 | if (c >= 17) { |
kenjiArai | 0:d68f1e7f7f49 | 574 | c -= 7; |
kenjiArai | 0:d68f1e7f7f49 | 575 | if (c <= 9) return 0; |
kenjiArai | 0:d68f1e7f7f49 | 576 | } |
kenjiArai | 0:d68f1e7f7f49 | 577 | if (c >= radix) return 0; |
kenjiArai | 0:d68f1e7f7f49 | 578 | val = val * radix + c; |
kenjiArai | 0:d68f1e7f7f49 | 579 | c = *(++(*str)); |
kenjiArai | 0:d68f1e7f7f49 | 580 | } |
kenjiArai | 0:d68f1e7f7f49 | 581 | if (s) val = -val; |
kenjiArai | 0:d68f1e7f7f49 | 582 | *res = val; |
kenjiArai | 0:d68f1e7f7f49 | 583 | return 1; |
kenjiArai | 0:d68f1e7f7f49 | 584 | } |
kenjiArai | 0:d68f1e7f7f49 | 585 | |
kenjiArai | 0:d68f1e7f7f49 | 586 | // Get key input data |
kenjiArai | 0:d68f1e7f7f49 | 587 | void get_line (char *buff, int len) |
kenjiArai | 0:d68f1e7f7f49 | 588 | { |
kenjiArai | 0:d68f1e7f7f49 | 589 | char c; |
kenjiArai | 0:d68f1e7f7f49 | 590 | int idx = 0; |
kenjiArai | 0:d68f1e7f7f49 | 591 | |
kenjiArai | 0:d68f1e7f7f49 | 592 | for (;;) { |
kenjiArai | 0:d68f1e7f7f49 | 593 | c = GETC(); |
kenjiArai | 0:d68f1e7f7f49 | 594 | if (c == '\r') { |
kenjiArai | 0:d68f1e7f7f49 | 595 | buff[idx++] = c; |
kenjiArai | 0:d68f1e7f7f49 | 596 | break; |
kenjiArai | 0:d68f1e7f7f49 | 597 | } |
kenjiArai | 0:d68f1e7f7f49 | 598 | if ((c == '\b') && idx) { |
kenjiArai | 0:d68f1e7f7f49 | 599 | idx--; |
kenjiArai | 0:d68f1e7f7f49 | 600 | PUTC(c); |
kenjiArai | 0:d68f1e7f7f49 | 601 | PUTC(' '); |
kenjiArai | 0:d68f1e7f7f49 | 602 | PUTC(c); |
kenjiArai | 0:d68f1e7f7f49 | 603 | } |
kenjiArai | 0:d68f1e7f7f49 | 604 | if (((uint8_t)c >= ' ') && (idx < len - 1)) { |
kenjiArai | 0:d68f1e7f7f49 | 605 | buff[idx++] = c; |
kenjiArai | 0:d68f1e7f7f49 | 606 | PUTC(c); |
kenjiArai | 3:92f31b23fb0b | 607 | // Special control |
kenjiArai | 3:92f31b23fb0b | 608 | if (one_char == true){ |
kenjiArai | 4:5981cf106502 | 609 | if ((c == 'p') || (c == 'm') || (c == 'P') || (c == 'M')){ |
kenjiArai | 4:5981cf106502 | 610 | break; |
kenjiArai | 4:5981cf106502 | 611 | } |
kenjiArai | 3:92f31b23fb0b | 612 | } |
kenjiArai | 0:d68f1e7f7f49 | 613 | } |
kenjiArai | 0:d68f1e7f7f49 | 614 | } |
kenjiArai | 0:d68f1e7f7f49 | 615 | buff[idx] = 0; |
kenjiArai | 0:d68f1e7f7f49 | 616 | PUTC('\n'); |
kenjiArai | 0:d68f1e7f7f49 | 617 | } |