From 1d81fa8a0fdc4e496f63ceb32da907e5833f3b29 Mon Sep 17 00:00:00 2001 From: Vincenzo Gibiino Date: Mon, 10 Sep 2018 12:13:04 +0200 Subject: [PATCH] added mega versio --- MEGA.ino | 202 ++++++++++++++++++++ STM32.ino | 82 +++++++++ UNO.ino | 125 +++++++++++++ arduino_test.ino | 20 ++ processing.pde | 469 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 898 insertions(+) create mode 100644 MEGA.ino create mode 100644 STM32.ino create mode 100644 UNO.ino create mode 100644 arduino_test.ino create mode 100644 processing.pde diff --git a/MEGA.ino b/MEGA.ino new file mode 100644 index 0000000..290b5dc --- /dev/null +++ b/MEGA.ino @@ -0,0 +1,202 @@ +/* + * LA.cpp + * + * Created: 11/12/2016 19.35.51 + * Author : Vincenzo + * Modificaciones agregadas para funcionar con ArduinoMega2560 por Enmanuel Sancho Quintanilla + * La unidad minima en tiempo para este sistema es de 8 micro segundos lo que idealmente permitiria + * observar clocks con periodos de 62 kHz sin embargo para poder apreciar las seƱales logicas con suficiente + * resolucion se recomienda no superar los 30 kHz en el clock del sistema. + */ + +#define baudrate 115200 //check if it is the same in processing +#define samples 10 +#define pin_used +#define timezerooffset 125 //microsegundos +#define PULLUP true //Si queremos entradas con PULLUP lo dejamos activado(true), si queremos dejarlas al "aire" (false), en caso de desactivarlo deberemos aterrizar todos los pines que no utilizemos. +#define F_CPU 16000000UL +#include +#include +#include +#define prescaler 0x02 +volatile uint16_t timer1_overflow_count; + +uint8_t initial1, initial2, initial3, state1, state2, state3, old_state1, old_state2, old_state3; +uint8_t pinChanged1[samples]; +uint8_t pinChanged2[samples]; +uint8_t pinChanged3[samples]; +uint32_t timer[samples]; +uint32_t timefix; +uint16_t event = 0; +uint8_t cambio = 0; +void init_board() +{ + + // PORTC = (0 << 0); DDRC |= (1 << 0); // led A0 + + DDRB = 0x00; + DDRC = 0x00; + DDRL = 0x00; + + if (PULLUP) + { + PORTA = B11111111; // pull-up + PORTC = B11111111; // Activamos el pull-up para que de no conectarse nada a puerto lea un uno siempre + PORTL = B11111111; + } + else + { + PORTA = B00000000; + PORTC = B00000000; + PORTL = B00000000; + } +} + +void init_timer() +{ + //clear + TCCR1A = 0b00000000; + TCCR1B = 0b00000000; + TIMSK1 = 0b00000000; + + //settings + TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0); //normal port operation + TCCR1A |= (0 << WGM11) | (0 << WGM10); //normal operation + TCCR1B |= (0 << WGM13) | (0 << WGM12); //normal operation + TCCR1B |= prescaler; //(0 << CS12) | (0 << CS11) | (1 << CS10); //clock prescaler + sei(); //enable interrupts + TIMSK1 |= (1 << TOIE1); // enable overflow interrupt +} + +ISR(TIMER1_OVF_vect) +{ + timer1_overflow_count++; +} + +void reset_timer1() +{ + TCNT1 = 0; + timer1_overflow_count = 0; +} + +uint32_t myMicros() +{ + cli(); + + if (TIFR1 & (1 << TOV1)) + { + TIFR1 = (0 << TOV1); + timer1_overflow_count++; + } + + uint32_t total_time = (65536 * timer1_overflow_count + TCNT1) / 2; + sei(); + return total_time; +} + +void start() +{ + _delay_ms(1000); + // Serial.print("hi"); + reset_timer1(); + event = 0; + + //PORTC = (1 << 0); + initial1 = PINA; + initial2 = PINL; + initial3 = PINC; + state1 = initial1; + state2 = initial2; + state3 = initial3; + for (int i = 0; i < samples; i++) + { + pinChanged1[i] = 0; + pinChanged2[i] = 0; + pinChanged3[i] = 0; + //Serial.print(pinChanged1[i]); Serial.print(','); Serial.print(pinChanged2[i]); Serial.print(','); Serial.println(pinChanged3[i]); //debug + } +} + +void sendData() +{ + //PORTC = (0 << 0); //turn off led + //initial data + Serial.println("S"); + Serial.print(initial1); + Serial.print(','); + Serial.print(initial2); + Serial.print(','); + Serial.print(initial3); + Serial.print(":"); + Serial.println(samples); + + timefix = -timer[0] + timezerooffset; + + for (int i = 0; i < samples; i++) + { + timer[i] = timer[i] + timefix; + } + //data + for (int i = 0; i < samples; i++) + { + Serial.print(pinChanged1[i]); + Serial.print(','); + Serial.print(pinChanged2[i]); + Serial.print(','); + Serial.print(pinChanged3[i]); + Serial.print(":"); + Serial.println(timer[i]); + } +} + +int main(void) +{ + Serial.begin(baudrate); + init_board(); + init_timer(); + + start(); + + while (1) + { + cambio = 0; + old_state1 = state1; + old_state2 = state2; + old_state3 = state3; + state1 = PINA; + state2 = PINL; + state3 = PINC; + //Serial.print(state1);Serial.print(" , ");Serial.print(state2);Serial.print(" , ");Serial.print(state3);Serial.print(" : ");Serial.println(event); + + if (old_state1 != state1) + { + pinChanged1[event] = state1 ^ old_state1; + cambio = 1; + } + if (old_state2 != state2) + { + pinChanged2[event] = state2 ^ old_state2; + cambio = 1; + } + if (old_state3 != state3) + { + pinChanged3[event] = state3 ^ old_state3; + cambio = 1; + } + + if (cambio == 1) + { + timer[event] = myMicros(); + event++; + } + if (event == samples) + { + sendData(); + while (Serial.read() != 'G') + { + ; + } //wait for the "go" + start(); + } + } +} diff --git a/STM32.ino b/STM32.ino new file mode 100644 index 0000000..d4631db --- /dev/null +++ b/STM32.ino @@ -0,0 +1,82 @@ +/* + Author : Vincenzo G + https://www.hackster.io/vincenzo-g/diy-logic-analyzer-f61ee5 + +*/ + +#define samples 300 +#define boardLed PB1 +uint8_t initial, state, old_state; +uint8_t pinChanged[samples]; +uint32_t timer[samples]; +uint16_t event = 0; + +//uncomment it if you want to use the USART1 instead of DFU serial +//#define Serial Serial1 + +void setup() { + + Serial.begin(115200); + + pinMode (boardLed, OUTPUT); + digitalWrite(boardLed, LOW); + + pinMode(PB12, INPUT_PULLUP); + pinMode(PB13, INPUT_PULLUP); + pinMode(PB14, INPUT_PULLUP); + pinMode(PB15, INPUT_PULLUP); + + startLA(); + +} + + +void startLA() { + //delay(1000); + + event = 0; + digitalWrite(boardLed, HIGH); + + reset_timer(); + initial = GPIOB->regs->IDR >> 12; + state = initial; + +} + +void loop() { + + old_state = state; + state = GPIOB->regs->IDR >> 12; + + if (old_state != state) { + timer[event] = micros(); + pinChanged[event] = state ^ old_state; + event++; + + if (event == samples) { + sendData(); + while (Serial.read() != 'G') ; //wait for the "go" + startLA(); + } + } +} + +void sendData() { + digitalWrite(boardLed, LOW); + + //initial data + Serial.println("S"); + Serial.print(initial); Serial.print(":"); + Serial.println(samples); + + //data + for (int i = 0; i < samples; i++) { + Serial.print(pinChanged[i]); Serial.print(":"); + Serial.println(timer[i]); + } +} + +void reset_timer() { + systick_uptime_millis = -1; + SYSTICK_BASE->CNT = 0; +} diff --git a/UNO.ino b/UNO.ino new file mode 100644 index 0000000..6e62f7d --- /dev/null +++ b/UNO.ino @@ -0,0 +1,125 @@ +/* + * LA.cpp + * + * Created: 11/12/2016 19.35.51 + * Author : Vincenzo + */ + +#define baudrate 115200 //check if it is the same in processing +#define samples 200 + +#define F_CPU 16000000UL +#include +#include +#include +#define prescaler 0x02 +volatile uint16_t timer1_overflow_count; + +uint8_t initial, state, old_state; +uint8_t pinChanged[samples]; +uint32_t timer[samples]; +uint16_t event = 0; + +void init_board() { + + PORTC = (0 << 0); DDRC |= (1 << 0); // led A0 + DDRB |= 0x00; // pin 8-13 input + PORTB |= 0x3F; // pull-up + +} + +void init_timer() { + + //clear + TCCR1A = 0b00000000; + TCCR1B = 0b00000000; + TIMSK1 = 0b00000000; + + //settings + TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0); //normal port operation + TCCR1A |= (0 << WGM11) | (0 << WGM10); //normal operation + TCCR1B |= (0 << WGM13) | (0 << WGM12); //normal operation + TCCR1B |= prescaler; //(0 << CS12) | (0 << CS11) | (1 << CS10); //clock prescaler + + sei(); //enable interrupts + TIMSK1 |= (1 << TOIE1); // enable overflow interrupt + +} + +ISR(TIMER1_OVF_vect) { + timer1_overflow_count++; +} + +void reset_timer1 () { + TCNT1 = 0; + timer1_overflow_count = 0; +} + +uint32_t myMicros () { + cli(); + + if (TIFR1 & (1 << TOV1)) { + TIFR1 = (0 << TOV1); + timer1_overflow_count++; + } + + uint32_t total_time = (65536 * timer1_overflow_count + TCNT1) / 2; + sei(); + return total_time; +} + +void start() { + _delay_ms(1000); + + reset_timer1(); + event = 0; + + PORTC = (1 << 0); + initial = PINB; + state = initial; + +} + + +void sendData() { + PORTC = (0 << 0); //turn off led + + //initial data + Serial.println("S"); + Serial.print(initial); Serial.print(":"); + Serial.println(samples); + + //data + for (int i = 0; i < samples; i++) { + Serial.print(pinChanged[i]); Serial.print(":"); + Serial.println(timer[i]); + } +} + + +int main(void) { + Serial.begin(baudrate); + + init_board(); + init_timer(); + + start(); + + while (1) { + + old_state = state; + state = PINB; + + if (old_state != state) { + timer[event] = myMicros(); + pinChanged[event] = state ^ old_state; + event++; + + if (event == samples) { + sendData(); + while (Serial.read() != 'G') ; //wait for the "go" + start(); + } + } + } +} diff --git a/arduino_test.ino b/arduino_test.ino new file mode 100644 index 0000000..cf280b9 --- /dev/null +++ b/arduino_test.ino @@ -0,0 +1,20 @@ +#define led A5 +#define led2 13 + +void setup() { + pinMode(led, OUTPUT); + pinMode(led2, OUTPUT); +} + + +void loop() { + + digitalWrite(led, HIGH); + delayMicroseconds(random(200)); + digitalWrite(led2, HIGH); + delayMicroseconds(random(200)); + digitalWrite(led2, LOW); + delayMicroseconds(random(200)); + digitalWrite(led, LOW); + delayMicroseconds(random(200)); +} diff --git a/processing.pde b/processing.pde new file mode 100644 index 0000000..81105df --- /dev/null +++ b/processing.pde @@ -0,0 +1,469 @@ +import processing.serial.*; +import java.util.Arrays; +Serial p; + +//////////////////////////////////////////////// +/*--------------------SETUP-------------------*/ + +//uncomment the line where your arduino/STM32 is connected +//String LA_port = "/dev/ttyACM0"; //linux DFU +//String LA_port = "/dev/ttyUSB0"; //linux Serial +String LA_port = "COM10"; //windows + +final int baudrate = 115200; //check if it is the same in arduino + +//change it to true if you are using a STM32 instead of arduino +final boolean STM32 = false; + +/*------------------END SETUP-----------------*/ +//////////////////////////////////////////////// + + +//colors: +int white = 255; +int black = 0; +int green = #00FF00; +int grey = 150; + + +// shift, reducer and millisecond view +float reducer = 1.0; +boolean milliseconds = false; +float xShift; + + +// start point in the processing window +int xEdge = 60; +int yEdge = 30; +int xEnd; +float[] xPos = {0, 0, 0, 0, 0, 0}; +int yBottom; +int yDiff; +int yPos = yEdge; +int ySave = yEdge; +boolean textCovered; +boolean drawTimes = true; + + +//Serial from mcu +//initial data +int samples; +int event; +int initialState; +boolean first = false; +boolean dataComplete = false; +//following data +boolean [][] state; +boolean [] isLow = new boolean[6]; +float[] usTime; +float[] xTime; +int[] pinChanged; + + +//buttons and others +int button1X = 8; +int button2X = 8; +int button3X = 80; +int button4X = 200; +int button5X = 270; +int buttonY; +int buttonH = 20; +int smallButtonW = 50; +int bigButtonW = 100; +int graphBoxH; +int textBoxH; +int immage = 1; +int corner = 10; + + +// bar scroll +int handleFill = grey; +float handleX; +float handleY; +float handleW = 20; +float handleH = 15; +boolean isDraggable = false; + + + +void setup () { + //p = new Serial(this, Serial.list()[0], 115200); + p = new Serial(this, LA_port, baudrate); + p.bufferUntil('\n'); + + size(1000, 460); + background(black); + smooth(4); + + graphBoxH = height -50; + textBoxH = height - 35; + yBottom = graphBoxH-20; + buttonY = textBoxH +8; + handleX = xEdge; + handleY = graphBoxH; +} + + +void cleanGraph() { + noStroke(); //no borders + fill(black); + rect(xEdge, 0, width, graphBoxH); //cancel the graph + stroke(green); //green lines + Arrays.fill(xPos, 0); //reset start point of the graph + textCovered = false; +} + + +void draw () { + + if (dataComplete==true) { + cleanGraph(); + pushMatrix(); //move the coordinate reference + translate(xEdge, 0); + for (int i=0; iwidth-handleW) handleX = width-handleW; + + getData(); + xShift = -map(handleX, xEdge, width-handleW, 0, xEnd-900); + } +} + + +void mousePressed() { + if (mouseX>handleX && mouseXhandleY && mouseYyBottom-15 && mouseY button1X && mouseX buttonY && mouseY button2X && mouseX buttonY && mouseY button3X && mouseX buttonY && mouseY button5X && mouseX buttonY && mouseY button4X && mouseX buttonY && mouseY button2X && mouseX buttonY && mouseY button3X && mouseX buttonY && mouseY button5X && mouseX yBottom-15 && mouseY button1X && mouseX handleX && mouseXhandleY && mouseY 0) + { + int i; + boolean drawLine = true; // alternate between dashes and gaps + + /* + Figure out x and y distances for each of the spacing values + I decided to trade memory for time; I'd rather allocate + a few dozen bytes than have to do a calculation every time + I draw. + */ + + for (i = 0; i < spacing.length; i++) + { + xSpacing[i] = lerp(0, (x1 - x0), spacing[i] / distance); + ySpacing[i] = lerp(0, (y1 - y0), spacing[i] / distance); + } + + i = 0; + while (drawn < distance) + { + if (drawLine) + { + line(x0, y0, x0 + xSpacing[i], y0 + ySpacing[i]); + } + x0 += xSpacing[i]; + y0 += ySpacing[i]; + /* Add distance "drawn" by this line or gap */ + drawn = drawn + mag(xSpacing[i], ySpacing[i]); + i = (i + 1) % spacing.length; // cycle through array + drawLine = !drawLine; // switch between dash and gap + } + } +}