logic-analyzer
Loading...
Searching...
No Matches
config_avr.h
1#pragma once
2#ifdef AVR
3#include "Arduino.h"
4#include "SoftwareSerial.h"
5
6#define MAX_CAPTURE_SIZE 500
7#define SERIAL_SPEED 9600
8#define SERIAL_TIMEOUT 500
9#define MAX_FREQ 100000
10#define MAX_FREQ_THRESHOLD 100000
11#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
12// Arduino Mega: pins 22-29 are mapped in order to PORTA
13#define START_PIN 22
14#define DESCRIPTION "Arduino-AVR-Mega"
15#else
16#define START_PIN 0
17#define DESCRIPTION "Arduino-AVR"
18#endif
19#define PIN_COUNT sizeof(PinBitArray)*8
20
21// Software Serial for logging
22#define LOG soft_serial
23#define RXD2 12
24#define TXD2 13
25
26
27namespace logic_analyzer {
28
30inline SoftwareSerial soft_serial(RXD2, TXD2); // RX, TX
31
32void setupLogger() {
33 LOG.begin(115200);
34}
35
37typedef uint8_t PinBitArray;
38
39
40#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
41
52class PinReader {
53 public:
54 PinReader(int startPin){
55 this->start_pin = startPin;
56 if (startPin >= 22 && startPin <= 29) {
57 reg = &PINA;
58 shift = startPin - 22;
59 } else if (startPin >= A0 && startPin <= A7) {
60 reg = &PINF;
61 shift = startPin - A0;
62 } else if (startPin >= A8 && startPin <= A15) {
63 reg = &PINK;
64 shift = startPin - A8;
65 }
66 }
67
69 inline PinBitArray readAll() {
70 if (reg != nullptr) {
71 return *reg >> shift;
72 }
73 // slow generic fallback
74 PinBitArray result = 0;
75 for (int j = 0; j < (int)sizeof(PinBitArray) * 8; j++) {
76 if (digitalRead(start_pin + j)) result |= (1 << j);
77 }
78 return result;
79 }
80
81 private:
82 int start_pin;
83 volatile uint8_t *reg = nullptr;
84 uint8_t shift = 0;
85};
86
87#else
88
95class PinReader {
96 public:
97 PinReader(int startPin){
98 this->start_pin = startPin;
99 }
100
102 inline PinBitArray readAll() {
103 // Use the PINx input registers (actual physical pin state), not the
104 // PORTx output-latch registers - PORTx does not reflect the real
105 // voltage on pins configured as INPUT.
106 uint16_t result = ((uint16_t)PINB & B00111111) << 8 | PIND;
107 return result >> start_pin;
108 }
109
110 private:
111 int start_pin;
112};
113
114#endif
115
116} // namespace
117
118#endif
119
Arduino Mega specific implementation Logic for the PinReader. The Mega GPIO numbers are not mapped to...
Definition config_avr.h:52
PinBitArray readAll()
reads all pins and provides the result as bitmask
Definition config_avr.h:69