/* vim: noexpandtab fileencoding=utf-8 nomodified nowrap textwidth=200 foldmethod=marker foldmarker={{{,}}} foldcolumn=4 ruler showcmd lcs=tab\:|- list: */ /* ChipTest v.0.3 - 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 13:1 set pin 13 to logical 1 (digitalWrite(HIGH);) - input is logical 1, if it is output and driven toward zero, then it may destroy something 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; #define VERZE "ChipTest v.0.3 (zif40)" // {{{ 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='0' && c<='9') {pin0 = (c-'0'); c=line.nextChar(); // pin0 if (c>='0' && c<='9') {pin0 = 10*pin0 + (c-'0'); c=line.nextChar();}; }; if (pin0>=0 && pin0<40) { /* OK */ } else {err(line.atChar,c, "not valid pin0 number"); return;}; if (c==0) { pin1=pin0; } else { if (c=='-') {c=line.nextChar();} else if (c=='.') {c=line.nextChar(); if (c=='.') {c=line.nextChar();} else { err(line.atChar,c,".. expected"); return;}; } else { err(line.atChar,c,"-/.. expected"); return;}; if (c>='0' && c<='9') {pin1 = (c-'0'); c=line.nextChar(); // pin1 if (c>='0' && c<='9') {pin1 = 10*pin1 + (c-'0'); c=line.nextChar();}; }; if (pin1>=0 && pin1<40) { /* OK */ } else {err(line.atChar,c, "not valid pin1 number"); return;}; }; if (pin0>pin1) {err(line.atChar,c, "pin1 must be same or larger, than pin0");return;}; if (c!=0 && c!='\n') {err(line.atChar,c, "extra characters on line"); return;}; for (int i=pin0; i<=pin1; ++i) { int x=i; dig[i]=val; if (val=='0') {pinMode(zif40[x],OUTPUT);digitalWrite(zif40[x],LOW); } else if (val=='1') {pinMode(zif40[x],OUTPUT);digitalWrite(zif40[x],HIGH); } else if (val=='v') {pinMode(zif40[x],INPUT); } else if (val=='^') {pinMode(zif40[x],INPUT_PULLUP); } else { err(line.atChar,c, "WTF, unknown val"); return; }; Serial.print(val); Serial.print("->"); Serial.print(x); Serial.println(";"); }; Serial.println("OK"); } // }}} void loop() { line.Update(); if (line.isReady()){ if (line.charsReady ==0) { status(); } else { solveLine(); headers(); status(); }; }; }