/* vim: noexpandtab fileencoding=utf-8 nomodified nowrap textwidth=200 foldmethod=marker foldmarker={{{,}}} foldcolumn=4 ruler showcmd lcs=tab\:|- list: */ #define VERZE "ChipTest v.0.4 (zif40)" /* ChipTest - Scans for chip, manipulates pins, 115.200 Bd, ANSI color HW: Arduino Mega + ShieldMega-001 TL;DR; H-Help, T-test (see help() for more) examples: 10:^ set pin D10 to weak pullup (INPUT_PULLUP) - if it is input on 74HCxx, then it counts as log. 1; if it is output, it will be driven to output value vvvvv (Space+5v) set pins 0..4 to weak pulldown (INPUT+5k Ohm) 13:1 set pin 13 to logical 1 (digitalWrite(HIGH);) - input is logical 1, if it is output and driven toward zero, then it cannot destroy anything, as there is 500 Ohm resistor anyway 2:0000^v set pins 2..5 to logical 0 (digitalWrite(LOW);), 6 to ^ and 7 to v // ShieldMega-001 have pulldown resistors for all zif40 pins */ // #define SERIAL_SPEED 9600 #define SERIAL_SPEED 115200 #include #include "b40.h" #define INPUTLINE_LENGTH 80 #include "InputLine.h" InputLine line; // {{{ zif40 {Dx for zif40}, alive {reacted on test}, pin {zif40 for chip} int zif40[] = {53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 5, 6, 7, 8, 9, 10, 11, 12 }; b40 alive; int pin[40], pins; // }}} // {{{ ANSI constants #define ANSI_L "\e[41mL\e[0m" #define ANSI_H "\e[41mH\e[0m" // }}} char dig[40]; #define P(A) Serial.print(F(A)); #define Pn(A) Serial.println(F(A)); #define Pnl() Serial.println(); #define Px(A) Serial.print(A); #define Pxn(A) Serial.println(A); void help() { // {{{ Pn(" send line"); Pn(" delete last char"); Pn("t, T run tests"); Pn("h, H, ? print help"); Pn("( /pin:)[ |.01v^]* manipulate pins (Space, Pipe are ignored, . means no change, v pulldown, ^ pullup; if line does not start wit Space, pin number is first manipulated pin"); } // }}} void setup() { // {{{ for (int i=0; i<40;++i){ pinMode(zif40[i],INPUT); // digitalWrite(i,LOW); dig[i]='v'; }; //Initialize serial and wait for port to open: Serial.begin(SERIAL_SPEED); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // prints title with ending line break delay(20); Pn("-= =-"); Pn(VERZE); } // }}} void mezery(int i) { // {{{ if (i % 10 == 0) P("|") else if (i % 5 == 0) P(" "); } // }}} bool match(uint8_t x, char y) { // {{{ status of pin - x actual value, y set value if (x==0) { if (y=='0' || y=='v' ) { return true; }; if (y=='1' || y=='^' ) { return false;}; }; if (x==1) { if (y=='1' || y=='^' ) { return true; }; if (y=='0' || y=='v' ) { return false;}; }; P("WTF match("); Px(x); P(", '"); Px(y); Pn("')"); return false; // Should be unreachable } // }}} void headers() { // {{{ Pnl(); for (int i=0; i<40;++i){ mezery(i); Px(i/10); }; Pnl(); for (int i=0; i<40;++i){ mezery(i); Px(i%10); }; Pnl(); } // }}} void status() { // {{{ uint8_t x; char y; for (int i=0; i<40;++i){ mezery(i); x=digitalRead(zif40[i]); y=dig[i]; if (match(x,y)) { Px(y) } else if (x) { P(ANSI_H); } else { P(ANSI_L); }; }; Pnl(); } // }}} void test() { // {{{ alive.zero(); for (int i=0; i<40; ++i) { dig[i]='v'; pinMode(zif40[i],INPUT); }; headers(); status(); bool b; // walking ^ for (int i=0; i<40; ++i) { dig[i]='^'; pinMode(zif40[i],INPUT_PULLUP); for (int j=0; j<40;++j){ // {{{ test pins b = match(digitalRead(zif40[j]), dig[j]); if (!b) alive.set(j); }; // }}} status(); dig[i]='v'; pinMode(zif40[i],INPUT); }; // walking v for (int i=0; i<40; ++i) { dig[i]='^'; pinMode(zif40[i],INPUT_PULLUP); }; for (int i=0; i<40; ++i) { dig[i]='v'; pinMode(zif40[i],INPUT); for (int j=0; j<40;++j){ // {{{ test pins b = match(digitalRead(zif40[j]), dig[j]); if (!b) alive.set(j); }; // }}} status(); dig[i]='^'; pinMode(zif40[i],INPUT_PULLUP); }; pins = 0; for (int i=0; i<40;++i){ mezery(i); if (alive.val(i)) { Serial.print("#"); pin[pins++]=i; } else { Serial.print("~"); }; }; Serial.print(" Pins found: "); Serial.println(pins); headers(); status(); } // }}} void err(int linepos, char c, const char *msg) { // {{{ if (linepos>1) { for (int i=1; i'9')) { err(line.atChar,c,"Space or : expected"); return;}; if (c!=' ') { if(! line.get_uint8_t(pin0)) { err(line.atChar,line.buff[line.atChar], "bad pin0 number"); return;}; if( pin0 >= 40 ) { err(line.atChar,line.buff[line.atChar], "too big pin0 number"); return;}; c=line.nextChar(); if ( c != ':' ) { err(line.atChar,c,": expected"); return;}; }; // from pin0 read 01v^ while ((c=line.nextChar())) { if (c == ' ' || c == '|') continue; if (pin0 >= 40) { err(line.atChar,c,"too many symbols"); break; }; uint8_t x=pin0; pin0++; if (c == '.' ) { continue; }; if (c=='0') {pinMode(zif40[x],OUTPUT);digitalWrite(zif40[x],LOW); } else if (c=='1') {pinMode(zif40[x],OUTPUT);digitalWrite(zif40[x],HIGH); } else if (c=='v') {pinMode(zif40[x],INPUT); } else if (c=='^') {pinMode(zif40[x],INPUT_PULLUP); } else { err(line.atChar,c,"unknown symbol"); break; }; dig[x]=c; }; Serial.println("OK"); } // }}} void loop() { line.Update(); if (line.isReady()){ if (line.charsReady ==0) { status(); } else { solveLine(); headers(); status(); }; }; }