Skip to content

Commit d688c1f

Browse files
committed
Updated Showinfo
1 parent b2fb879 commit d688c1f

File tree

7 files changed

+135
-147
lines changed

7 files changed

+135
-147
lines changed

README.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ Not yet available as Arduino library.
3333
<br/>
3434

3535
# Table of content
36-
* [SimpleEMAFilters.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#simpleemafiltershpp)
37-
* [ADCUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#adcutilscpp)
36+
* [SimpleEMAFilters](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#simpleemafilters)
37+
* [ADCUtils](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#adcutils)
3838
* [HCSR04](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#hcsr04)
39-
* [MeasureVoltageAndResistance.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#measurevoltageandresistancehpp)
40-
* [BlinkLed.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#blinkledcpp)
39+
* [MeasureVoltageAndResistance](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#measurevoltageandresistancehpp)
40+
* [BlinkLed](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#blinkled)
4141
* [ShowInfo](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#showinfo)
4242
* [HexDump](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#hexdump)
43-
* [AVRUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#avrutilscpp)
44-
* [MillisUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#millisutilscpp)
45-
* [DebugLevel.h](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#debuglevelh)
46-
* [ATtinyUtils.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#attinyutilscpp)
47-
* [AvrTracing.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#avrtracinghpp)
43+
* [AVRUtils](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#avrutils)
44+
* [MillisUtils](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#millisutils)
45+
* [DebugLevel](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#debuglevel)
46+
* [ATtinyUtils](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#attinyutils)
47+
* [AvrTracing](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#avrtracing)
4848
* [Utilities available as separate Arduino library](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#utilities-available-as-separate-arduino-library)
49-
* [ATtinySerialOut.cpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#attinyserialoutcpp)
50-
* [EasyButtonAtInt01.hpp](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#easybuttonatint01hpp)
49+
* [ATtinySerialOut](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#attinyserialout)
50+
* [EasyButtonAtInt01](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#easybuttonatint01)
5151
* [The very useful digitalWriteFast.h file based on the version from Watterott electronic.](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#the-very-useful-digitalwritefasth-file-based-on-the-version-from-watterott-electronic)
5252
* [Revision History](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#revision-history)
5353
* [CI](https://github.com/ArminJo/Arduino-Utils?tab=readme-ov-file#ci)
5454

55-
# SimpleEMAFilters.hpp
55+
# SimpleEMAFilters
5656
An EMA (**Exponential Moving Average**) filter behaves like an RC lowpass filter with RC = SamplePeriod((1-alpha)/alpha) see [here](https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter).<br/>
5757
An EMA filter is implemented by e.g. the following statement:
5858

@@ -170,7 +170,7 @@ Lowpass8_int32 += ((((int32_t) InputValue) << 16) - Lowpass8_int32) >> 8; // Fix
170170
- https://github.com/popcornell/Arduino-Multiplierless-EMA-filter
171171
- https://github.com/MakeMagazinDE/DigitaleFilter
172172

173-
# ADCUtils.cpp
173+
# ADCUtils
174174
Fast and flexible ADC conversions. **Intelligent handling of delays for reference and channel switching**.
175175
- Functions for easy **oversampling**.
176176
- Function for easy getting the maximum value of measurements.
@@ -192,7 +192,7 @@ Fast and flexible ADC conversions. **Intelligent handling of delays for referenc
192192
4. All modules:<br/>
193193
Connect Trigger and Echo by a resistor of 4.7 k&ohm;.
194194

195-
# MeasureVoltageAndResistance.hpp
195+
# MeasureVoltageAndResistance
196196
Measures voltage and resistance with **1 mV and 2 &ohm; resolution** at the lower end.<br/>
197197
First voltage is measured. If voltage is zero, then the unknown resistance to ground is measured using the 5 volt (VCC) supply with internal/series resistance of 10 k&ohm; or 100 k&ohm;.
198198

@@ -201,7 +201,7 @@ First voltage is measured. If voltage is zero, then the unknown resistance to gr
201201
### Fritzing schematics
202202
![Fritzing schematics](extras/VoltAndOhmmeter_Schaltplan.png)
203203

204-
# BlinkLed.cpp
204+
# BlinkLed
205205
- Class for blinking one ore more LED's in different fashions.
206206

207207
# ShowInfo
@@ -216,7 +216,7 @@ First voltage is measured. If voltage is zero, then the unknown resistance to gr
216216
0x0020: 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 UUUUUUUUUUUUUUUU
217217
```
218218

219-
# AVRUtils.cpp
219+
# AVRUtils
220220
- Sleep and sleep with watchdog functions.
221221
- Available Ram, Heap / Stack memory display.
222222

@@ -229,7 +229,7 @@ Stack used 20 of 7
229229
Currently available Heap=0
230230
```
231231

232-
# MillisUtils.cpp
232+
# MillisUtils
233233
Unifies millis() timer handling for Digispark, AttinyCore and Arduino cores.
234234
- Start, stop and modify milliseconds timer and value.
235235
- Functions to compensate `millis()` after long running tasks in `noIterrupt()` context like NeoPixel output, ADC buffer reading etc.
@@ -239,7 +239,7 @@ Unifies millis() timer handling for Digispark, AttinyCore and Arduino cores.
239239
- Propagating debug levels for development. Supports level `TRACE, DEBUG, INFO, WARN and ERROR`.
240240
- **Includes an explanation of semantics of these levels**.
241241

242-
# ATtinyUtils.cpp
242+
# ATtinyUtils
243243
- `toneWithTimer1PWM()`.
244244
- `noToneWithTimer1PWM()`.
245245
- `isBODSFlagExistent()` -> checking for ATtiny85 revision C.
@@ -248,18 +248,18 @@ Unifies millis() timer handling for Digispark, AttinyCore and Arduino cores.
248248
It changes Digispark Bootloader clock settings to get the right CPU frequency and resets Digispark OCCAL tweak.
249249
Consider to use new [optimized Digispark core](https://github.com/ArminJo/DigistumpArduino) instead.
250250

251-
# AvrTracing.hpp
251+
# AvrTracing
252252
**Tracing an Arduino program** by printing each program counter value after executing one instruction.
253253

254254
# Utilities available as separate Arduino library
255255

256-
## [ATtinySerialOut.cpp](https://github.com/ArminJo/ATtinySerialOut/src)
256+
## [ATtinySerialOut](https://github.com/ArminJo/ATtinySerialOut/src)
257257
Minimal bit-bang send serial
258258
- 115200 baud for 1/8/16 MHz ATtiny clock.
259259
The utility for serial output for ATtinies is contained in the [ATtinySerialOut library](https://github.com/ArminJo/ATtinySerialOut)
260260
available as an Arduino library.
261261

262-
## [EasyButtonAtInt01.hpp](https://github.com/ArminJo/EasyButtonAtInt01/src)
262+
## [EasyButtonAtInt01](https://github.com/ArminJo/EasyButtonAtInt01/src)
263263
- Arduino library for handling push buttons just connected between ground and INT0 and / or INT1 pin.
264264
- No external pullup, **no polling needed**.
265265
The utility for easy button handling for ATmegas or ATtinies is contained in the [EasyButtonAtInt01 library](https://github.com/ArminJo/EasyButtonAtInt01)
+51-82
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
22
* AVRUtilsDemo.cpp
33
*
4-
* Demo of using 4 seconds sleep on an ATtiny
4+
* Demo of 2 seconds sleep with watchdog
5+
* Demo of printRAMInfo() and printStackUnusedAndUsedBytes ()
56
*
6-
* Copyright (C) 2020 Armin Joachimsmeyer
7+
* Copyright (C) 2020-2024 Armin Joachimsmeyer
78
* armin.joachimsmeyer@gmail.com
89
*
910
* This file is part of Arduino-Utils https://github.com/ArminJo/Arduino-Utils.
@@ -39,16 +40,16 @@
3940

4041
#if defined(__AVR__)
4142

42-
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) \
43+
#include "AVRUtils.h"
44+
#include "ShowInfo.h"
45+
#include "HexDump.hpp"
46+
47+
# if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) \
4348
|| defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) \
4449
|| defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__) \
4550
|| defined(__AVR_ATtiny88__)
46-
#define CODE_FOR_ATTINY
51+
# define CODE_FOR_ATTINY
4752
#endif
48-
49-
#include "AVRUtils.h"
50-
#include "HexDump.h"
51-
5253
#if defined(CODE_FOR_ATTINY)
5354
#include "ATtinySerialOut.hpp" // Available as Arduino library "ATtinySerialOut"
5455
#else
@@ -57,59 +58,25 @@
5758
//#define SIZE_OF_DUMMY_ARRAY 1700 // Stack is OK, but no heap is available
5859
#define SIZE_OF_DUMMY_ARRAY 1600 // Stack is OK, and small heap is available
5960
uint8_t sDummyArray[SIZE_OF_DUMMY_ARRAY] __attribute__((section(".noinit"))); // Place it at end of BSS to be first overwritten by stack.
60-
61-
/*
62-
* !!! THIS WORKS ONLY WITH VERSION 8.0 OF THE OPTIBOOT BOOTLOADER !!!
63-
First, we need a variable to hold the reset cause that can be written before
64-
early sketch initialization (that might change r2), and won't be reset by the
65-
various initialization code.
66-
avr-gcc provides for this via the ".noinit" section.
67-
*/
68-
uint8_t sMCUSR __attribute__ ((section(".noinit")));
69-
70-
/*
71-
Next, we need to put some code to save reset cause from the bootloader (in r2)
72-
to the variable. Again, avr-gcc provides special code sections for this.
73-
If compiled with link time optimization (-flto), as done by the Arduino
74-
IDE version 1.6 and higher, we need the "used" attribute to prevent this
75-
from being omitted.
76-
*/
77-
void resetFlagsInit(void) __attribute__ ((naked))
78-
__attribute__ ((used))
79-
__attribute__ ((section (".init0")));
80-
void resetFlagsInit(void) {
81-
/*
82-
save the reset flags passed from the bootloader
83-
This is a "simple" matter of storing (STS) r2 in the special variable
84-
that we have created. We use assembler to access the right variable.
85-
*/
86-
__asm__ __volatile__ ("sts %0, r2\n" : "=m" (sMCUSR) :);
87-
}
88-
8961
#endif
9062

91-
uint16_t sStaticVariable = 1; // Resides in data segment (variables initialized with values != zero)
92-
bool sBootReasonWasPowerUp = false; // Resides in BSS segment, because it is initialized with 0;
93-
94-
//#include "ShowInfo.h"
95-
9663
#define VERSION_EXAMPLE "1.0"
9764

98-
#define LED_PIN PB1
9965
#define TONE_OUT_PIN 4
100-
#define BEEP_FREQUENCY 2100
101-
#define BEEP_START_DURATION_MILLIS 400
102-
#define BEEP_DEFAULT_DURATION_MILLIS 80
10366

10467
#if defined(CODE_FOR_ATTINY)
68+
#define LED_PIN PB1
10569
// Pin 1 has an LED connected on my Digispark board.
10670
# if (LED_PIN == TX_PIN)
10771
#error LED pin must not be equal TX pin (pin 2).
10872
# endif
10973

110-
uint8_t sMCUSRStored; // content of MCUSR register at startup
74+
#else
75+
#define LED_PIN LED_BUILTIN
11176
#endif
11277

78+
uint8_t sMCUSRStored; // content of MCUSR register at startup
79+
11380
// Helper macro for getting a macro definition as string
11481
#define STR_HELPER(x) #x
11582
#define STR(x) STR_HELPER(x)
@@ -128,50 +95,44 @@ void setup() {
12895
GPIOR0 = 0; // Clear it to detect a jmp 0
12996
}
13097

98+
initStackFreeMeasurement();
99+
131100
/*
132101
* Initialize the serial pin as an output for Serial.print like debugging
133102
*/
134103
initTXPin();
135-
writeString(F("START " __FILE__ "\nVersion " VERSION_EXAMPLE " from " __DATE__ "\nMCUSR="));
136-
writeUnsignedByteHexWithPrefix(sMCUSRStored);
137-
write1Start8Data1StopNoParity('\n');
138104
#else
139105

106+
sMCUSRStored = MCUSR; // content of MCUSR register at startup
107+
MCUSR = 0;
108+
wdt_disable();
109+
140110
for (int i = 0; i < SIZE_OF_DUMMY_ARRAY; ++i) {
141-
sDummyArray[i] = 0; // Mark array with 0 to detect overwriting by stack
111+
sDummyArray[i] = 0; // Mark array with 0 to detect overwriting by StackFreeMeasurement
142112
}
143113
initStackFreeMeasurement();
144114

145-
MCUSR = 0;
146-
if (sMCUSR & (1 << PORF)) {
147-
sBootReasonWasPowerUp = true; // always true for old Optiboot bootloader
148-
}
149-
150115
Serial.begin(115200);
151116

117+
#endif // defined(CODE_FOR_ATTINY)
152118
// Just to know which program is running on my Arduino
153-
Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__ ));
119+
Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__));
120+
Serial.println();
121+
154122
Serial.print(F("sMCUSR=0x"));
155-
Serial.print(sMCUSR, HEX);
156-
Serial.print(F(" BootReasonWasPowerUp="));
157-
Serial.println(sBootReasonWasPowerUp);
158-
#endif
123+
Serial.print(sMCUSRStored, HEX);
124+
Serial.print(F(" => Boot reason is"));
125+
printMCUSR(sMCUSRStored);
126+
127+
printBODLevel();
159128

160129
/*
161-
* Prints to avoid optimizing away the two variables
130+
* Prints to avoid optimizing away the sDummyArray, which is first overwritten by stack.
162131
*/
163-
sStaticVariable = analogRead(A0); // to avoid optimizing away sStaticVariable
164-
Serial.print(F("sStaticVariable filled by analogRead(A0)="));
165-
Serial.println(sStaticVariable);
166132
Serial.print(F("sDummyArray[1000]=0x"));
167133
Serial.println(sDummyArray[1000], HEX);
168134
Serial.println(F("DummyArray size=" STR(SIZE_OF_DUMMY_ARRAY)));
169135

170-
// This requires ShowInfo
171-
// printMCUSR(sMCUSRStored);
172-
// printBODLevel();
173-
// printFuses();
174-
175136
pinMode(LED_PIN, OUTPUT);
176137

177138
// activate LED
@@ -182,17 +143,22 @@ void setup() {
182143
digitalWrite(LED_PIN, 0);
183144
delay(200);
184145

185-
tone(TONE_OUT_PIN, BEEP_FREQUENCY, BEEP_START_DURATION_MILLIS);
186-
delay(BEEP_START_DURATION_MILLIS);
146+
tone(TONE_OUT_PIN, 2200, 400);
147+
delay(400);
187148

188149
Serial.println();
189150
printRAMInfo(&Serial);
190151
printStackUnusedAndUsedBytes(&Serial);
152+
191153
Serial.println();
192154

193-
printMemoryHexDump((uint8_t*) (RAMEND - 128) + 1, 128, true);
155+
Serial.println(F("Dump stack / end of RAM"));
156+
printMemoryHexDump((uint8_t*) (RAMEND - 256) + 1, 256);
194157

195158
Serial.println();
159+
printStackUsedBytes(&Serial);
160+
Serial.println();
161+
196162
//}
197163

198164
/*
@@ -215,16 +181,19 @@ void setup() {
215181

216182
void loop() {
217183

218-
// 4 second CPU idle
219-
for (int i = 0; i < 100; ++i) {
220-
delayMicroseconds(40000);
221-
}
222184
digitalWrite(LED_PIN, HIGH);
223-
delayMicroseconds(20000);
185+
delay(400);
186+
if (sNumberOfSleeps == 4) {
187+
Serial.println(F("Wait 5 seconds to force watchdog reset"));
188+
wdt_enable(WDTO_2S); // Resets after 2.5 seconds and then every 100 ms if not wdt_disable();
189+
delay(5000);
190+
}
224191
digitalWrite(LED_PIN, LOW);
225-
ADCSRA = 0; // disable ADC just before sleep -> saves 200 uA
226-
sleepWithWatchdog(WDTO_4S, true); // Sleep 4 seconds
192+
Serial.print(F("Sleep 2 seconds with watchdog reset sNumberOfSleeps="));
193+
Serial.println(sNumberOfSleeps);
194+
Serial.flush(); // Otherwise the USART interrupt will wake us up
195+
sleepWithWatchdog(WDTO_2S, true); // Sleep 2 seconds
227196
}
228197
#else
229-
#error This source is only for ATTinies
230-
#endif
198+
#error This source is only for AVR
199+
#endif //defined(__AVR__)

src/AVRUtils.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ uint16_t getStackUnusedAndUsedBytes(uint16_t *aStackUsedBytesPointer) {
8686
tHeapPtr++;
8787
tStackUnused++;
8888
}
89-
*aStackUsedBytesPointer = (RAMEND - (uint16_t) tHeapPtr) + 1;
89+
*aStackUsedBytesPointer = ((RAMEND + 1) - (uint16_t) tHeapPtr);
9090

9191
return tStackUnused;
9292
}
@@ -95,8 +95,9 @@ uint16_t getStackUnusedAndUsedBytes(uint16_t *aStackUsedBytesPointer) {
9595
* Returns the amount of stack/heap touched since the last call to initStackFreeMeasurement()
9696
* by check for first non touched pattern on the stack/heap, starting the DOWNWARD search at current stack pointer.
9797
*
98-
* This returns too big value, if the end of former malloced and written memory was higher than current stackpointer,
99-
* which nevertheless looks like a potential programming problem.
98+
* If the end of former malloced and written memory was higher than current stackpointer,
99+
* the memory area is taken as overwritten stack. Therefore we may return values, which are too big.
100+
* But nevertheless, this constellation is likely a potential programming problem!
100101
*/
101102
uint16_t getStackUsedBytes() {
102103
uint8_t tDummyVariableOnStack;
@@ -108,7 +109,7 @@ uint16_t getStackUsedBytes() {
108109
tSearchPtr--;
109110
}
110111

111-
return (RAMEND + 1) - (uint16_t)tSearchPtr;
112+
return (RAMEND + 1) - (uint16_t) tSearchPtr;
112113
}
113114

114115
/*
@@ -217,7 +218,9 @@ void printStackUnusedAndUsedBytesIfChanged(Print *aSerial) {
217218
*/
218219
void printRAMInfo(Print *aSerial) {
219220
uint16_t tHeapStart = (uint16_t) getHeapStart();
220-
aSerial->print(F("Size of Data + BSS, Heap start, Stack end="));
221+
aSerial->print(F("Size of Data + BSS, Heap start, Stack end=0x"));
222+
aSerial->print(tHeapStart - RAMSTART, HEX);
223+
aSerial->print(F(" | "));
221224
aSerial->println(tHeapStart - RAMSTART);
222225

223226
printStackUsedBytes(aSerial);

src/AVRUtils.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdint.h>
2828
#include <avr/sleep.h>
2929
#include <avr/wdt.h>
30+
#include "avr/boot.h"
3031

3132
/*
3233
* storage for millis value to enable compensation for interrupt disable at signal acquisition etc.

src/HexDump.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define HEX_DUMP_FORMAT_8_BIT_ADDRESS 0x04 // Bit 2: else 16 bit Address
4040
#define HEX_DUMP_FORMAT_ASCII_VALUES 0x08 // default
4141

42+
void printBufferHex(uint8_t *aBufferAddress, uint16_t aNumberOfBytesToPrint);
4243
void printBufferHexDump(uint8_t *aBufferAddress, uint16_t aNumberOfBytesToPrint);
4344
void printBufferHexAndASCIIDump(uint8_t *aBufferAddress, uint16_t aNumberOfBytesToPrint);
4445
void printMemoryHexDump(uint8_t *aMemory, uint16_t aSizeOfMemoryToPrint, uint8_t aBytesPerLine = _16_BYTES_PER_LINE,

0 commit comments

Comments
 (0)