Teensy 4.1 pwm resolution over 10 bits reset teensy 4.1 at 1023 level

Hussain

Member
Hello,

I have an issue with my teensy 4.1. Every time i change the pwm from 0 to over 1023 the teensy 4.1 reset it self, when i choose a pwm resolution over 10 bits. However when i use 10 bits or 8 bits it works proberly.
Is Teensy 4.1 capable of pwm resolution of over 10 bits?
PJRC website states it is capable of over 10 bits pwm up to 15 bits.
How can i use the function analogWriteResolution() properly?
will analogWriteResolution() set the pwm resolution for all pwm pins or only the pwm pins that are in use or what does it even do?
the setup is as follows:

Teensy 4.1 is connected to my pc via micro usb cable and it drives 3 smd mosfet gates via pwm, bss mosfet with max Drain current of 200 mA. The mosfets are all in open loop (D and S are not connected to anything at all).
the following code is what i use to change the pwm resolution as well as the pwm duty cycle:

#include <Arduino.h>
#includes .........

void setup()
{
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
analogWriteFrequency(pin1, 4577);
analogWriteFrequency(pin2, 4577);
analogWriteFrequency(pin3, 4577);
analogWriteResolution(15);
}

void loop()
{
for (size_t i1=0; i<32768; i++){
analogWrite(pin1, i1);
}
for (size_t i2=0; i<32768; i++){
analogWrite(pin2, i2);
}
for (size_t i3=0; i<32768; i++){
analogWrite(pin3, i3);
}
}

After the first for loop reaches i1 = 1023 the teensy 4.1 reset why?
I tried to connect the vin pin to 5V with 2A current just in case, but i get the same result -> teensy reset.

Can anyone please help me?
It is very urgent :(

Kind regards
Hussain
 
Could you please post your actual code? This is not a complete program and does not compile. There are several errors, such as in each of the for() loops, you define i1, i2, i3, but the loop logic uses "i". Your problem may be something simple like this, not directly related to PWM.

Code:
  for (size_t i1=0; i<32768; i++){
    analogWrite(pin1, i1);
  }
 
Can anyone please help me?

I want to help you. I tried to investigate the problem. Here is the code I ran:

Code:
const int pin1 = 1;
const int pin2 = 2;
const int pin3 = 3;
unsigned int loopcount = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  analogWriteFrequency(pin1, 4577);
  analogWriteFrequency(pin2, 4577);
  analogWriteFrequency(pin3, 4577);
  analogWriteResolution(15);
}

void loop()
{
  Serial.printf("loopcount = %u\n", ++loopcount);
  for (size_t i1 = 0; i1 < 32768; i1++) {
    analogWrite(pin1, i1);
  }
  for (size_t i2 = 0; i2 < 32768; i2++) {
    analogWrite(pin2, i2);
  }
  for (size_t i3 = 0; i3 < 32768; i3++) {
    analogWrite(pin3, i3);
  }
}

This is basically the same (incomplete) program you showed with minor errors fixed, and I added printing of a loop counter variable.

When I run it here and watch in the Arduino Serial Monitor, it does not crash.

screenshot.png


It is very urgent :(

You can get better help much faster if you follow the "Forum Rule" at the top of every page. When you have a program which does not work, please trim it to reasonably small code and test to be certain the small program still reproduces the problem. Then show the complete program here, so anyone can copy it into Arduino without any guesswork or modifications. If you had done this, I would have been able to reproduce the problem and probably help solve it. But instead, I wasted precious time fixing minor errors like "i1" vs "i" and missing devices for "pin1", etc... and ultimately I was unsuccessful. So the time I could have spent solving this problem instead was wasted adapting your incomplete code and them writing this message to explain how to better ask for help.

Please follow the Forum Rule. Is saves everyone's valuable time and lets us help you better.
 
Hello joepasquariello,
i will work on it the next days since the project is huge and sensitve and is my Bachlorthesis i already finished.
I need more time to ask if i am allowed to publish my BA yet.
 
Hello PaulStoffregen,

the code i am using is oop based and huge to post on forum and i need to ask first if i am allowed to publish it yet.
But thank you anyway
 
Hello PaulStoffregen,

the code i am using is oop based and huge to post on forum and i need to ask first if i am allowed to publish it yet.
But thank you anyway

Can you run Paul's p#3 code on the Teensy there and see it work? It demonstrates proper usage and functions as expected.
> If the p#3 code works there, then the problem is elsewhere in the code/hardware not shown here.

Can you introduce changes reflecting usage there that reproduce the behavior noted in p#1?
 
Hello defragster,
i am working on it.
It is time consumingsince i am a student.
I will reproduce the usage changes tomorrow
Thank you very much

Kind regards
Hussain
 
Please do not post a huge program! Reduce your code to only the minimum amount which demonstrates the problem with analogWrite using more than 1023.

Yes, this requires some effort on your part to edit your large program to the minimum size which reproduces the problem. It is time consuming to edit your code. But you are asking everyone else to spend effort to help you solve a problem. Reading and diagnosing your program is also time consuming for everyone else!

Or perhaps before you edit your program, you could spend just a minute to copy the program I showed in msg #3 into Arduino and run it on your Teensy 4.1. Clearly writing larger than 1023 to analogWrite(), at least in that simple program which is very close to the (erroneous) code you showed in your first message. While it is theoretically possible you have found some previously unknown defect in analogWrite() which has eluded notice by the many thousands of people who have used Teensy 4.x since 2019, since that small program does not crash, I believe the much more likely scenario is analogWrite() works properly and does not crash, the issue is probably some other error in your code. And we have a very good history on this forum at identifying problems with code, but only when you show us the real code which reproduce the problem.

Please, consider the larger a program you show here, the more time you are asking everyone to spend reading it. Please reduce your program to only the minimum size which reproduce the crash.
 
Hello paulStoffregen,

I tried your code and it worked fine.
However when i try the following code the teensy 4.1 resets it self after i reaches 8191.

Code:
Code:
#include <Arduino.h>
#include <TimeLib.h>
#include <vector>
#include <cmath>
#include <TimeLib.h>

std::vector<String> current_time;
std::vector<float> UGS_1;
std::vector<float> UGS_2;
std::vector<float> UGS_3;

void function_2(float Ugs, int pwm_resolution, int pin1, int pin2, int pin3, std::vector<float> *UGS1, std::vector<float> *UGS2, std::vector<float> *UGS3, std::vector<String> *vector_address)
{
  float UGS = Ugs;
  String time_stamp = (String(hour()) + ":" + String(minute()) + ":" + String(second()));
  size_t a = pow(2, pwm_resolution);
  float D_in_V = ((3.3 / a) - 1);
  analogWrite(pin1, (int)((UGS) / (D_in_V)));
  analogWrite(pin2, (int)((UGS) / (D_in_V)));
  analogWrite(pin3, (int)((UGS) / (D_in_V)));

  for (size_t i = 0; i < a; i++)
  {
    UGS -= D_in_V;
    analogWrite(pin1, (int)((UGS) / (D_in_V)));
    analogWrite(pin2, (int)((UGS) / (D_in_V)));
    analogWrite(pin3, (int)((UGS) / (D_in_V)));
    time_stamp = (String(hour()) + ":" + String(minute()) + ":" + String(second()));
    vector_address->push_back(time_stamp);
    UGS1->push_back(UGS);
    UGS2->push_back(UGS);
    UGS3->push_back(UGS);
    Serial.println(i);
    delay(5);
  }
}

void setup()
{
  Serial.begin(9600);
  pinMode(24, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(8, OUTPUT);
  analogWriteFrequency(24, 1000000);
  analogWriteFrequency(11, 1000000);
  analogWriteFrequency(8, 1000000);
  analogWriteResolution(15);
}

void loop()
{
  function_2(3.3, 15, 24,11,8,&UGS_1, &UGS_2, &UGS_3, &current_time);
}

as proof the monitor stoped at 8191 as you can see in screenshot:
Screenshot.jpg

Kind regards
Hussain
 
Yes it does fault then:

I changed your program to print out crash report:
Code:
void setup()
{
  Serial.begin(9600);
  while(!Serial && millis() < 4000) ;
  if (CrashReport) Serial.print(CrashReport);
  pinMode(24, OUTPUT);

Code:
190
8191
CrashReport:
  A problem occurred at (system time) 6:37:31
  Code was executing from address 0xB70
  CFSR: 400
	(IMPRECISERR) Data bus error but address not related to instruction
  Temperature inside the chip was 39.87 °C
  Startup CPU clock speed is 600MHz
  Reboot was caused by auto reboot after fault or bad interrupt detected
0
1
2
3
4

Code:
C:\Users\kurte\AppData\Local\Temp>cd arduino-sketch-9A403FF5CBA93AA8B20F2A41C15E521A

C:\Users\kurte\AppData\Local\Temp\arduino-sketch-9A403FF5CBA93AA8B20F2A41C15E521A>addr2line -e bar.ino.elf 0xB70
C:\Users\kurte\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.57.0\cores\teensy4/WString.cpp:156

C:\Users\kurte\AppData\Local\Temp\arduino-sketch-9A403FF5CBA93AA8B20F2A41C15E521A>

Which is at the start of the function:
Code:
String & String::copy(const char *cstr, unsigned int length)
{
	if (length == 0) {
		if (buffer) buffer[0] = 0;
		len = 0;
		return *this;
	}
	if (!reserve(length)) {
		if (buffer) {
			free(buffer);
			buffer = NULL;
		}
		len = capacity = 0;
		return *this;
	}
	len = length;
	strcpy(buffer, cstr);
	return *this;
}
 
Hello KurtE,
Thank you for the help.
the following is the crashreport i get from my huge oop code:
CrashReport:
A problem occurred at (system time) 16:13:25
Code was executing from address 0x8F84
CFSR: 82
(DACCVIOL) Data Access Violation
(MMARVALID) Accessed Address: 0x10 (nullptr)
Check code at 0x8F84 - very likely a bug!
Run "addr2line -e mysketch.ino.elf 0x8F84" for filename & line number.
Temperature inside the chip was 45.44 °C
Startup CPU clock speed is 600MHz
Reboot was caused by auto reboot after fault or bad interrupt detected

Do you know how to solve it or is it just bad programming problem?

Kind regards
Hussain
 
I only have a matter of minutes available right now. Will look at this in more detail later today, if it's still unsolved.

But my first impression is you're infinitely adding data to 4 std::vector instances (looks like 200 times per second), but never removing it. So they will (probably) keep growing without bound until all of Teensy's limited memory is consumed. We don't support C++ exceptions, so when you've filled up all the memory, you'll get undefined behavior. While that could be nearly anything, usually the result will be your program keeps running (no exception support) and tries to write to places that aren't really memory. Sooner or later you'll get a memory fault. On Teensy 4 the default fault handler reboots after 8 seconds. It also logs basic info for CrashReport before rebooting...
 
Hello paulStoffregen,

Thank you for your fast response.
I think the problem is like you said in #12, since my huge code uses over 24 std::vector instances and empties them after while, but when i use 15 bits the vectors sizes gets too big so that undefined behavior occurs, which leads to rebooting the teensy 4.1.

Thank you very much for every one who helped me.
I will keep you updated when i solve the problem

Kind regards
Hussain
 
Yes I also am pretty sure ti is you are running out of heap space.

I added the defines at the start
Code:
extern "C" {
  extern void * _sbrk(int incr);
extern unsigned long _heap_start;
extern unsigned long _heap_end;

}
IN setup I added after crashreport:
Code:
  Serial.printf("Heap Start: %x End:%x\n", (uint32_t)&_heap_start, (uint32_t)&_heap_end);

In your loop I put:
Code:
    Serial.print((uint32_t)_sbrk(0), HEX);
    Serial.print(" ");
    Serial.println(i);


Output when crash:
Code:
20260000 8188
20260000 8189
20260000 8190
20260000 8191
CrashReport:
  A problem occurred at (system time) 7:29:28
  Code was executing from address 0xBD0
  CFSR: 400
	(IMPRECISERR) Data bus error but address not related to instruction
  Temperature inside the chip was 42.35 °C
  Startup CPU clock speed is 600MHz
  Reboot was caused by auto reboot after fault or bad interrupt detected
Heap Start: 20203060 End:20280000
20204000 0
20204000 1
20204000 2

Looked like the memory code calling _sbrk to allocate chunks was calling maybe with: 2E000 or at least the last increase was
by that amount, and if you add that on to the last value you see, it will exceed the END of the heap space
 
Hello KurtE,

Thank you very much for your help.
I solved the problem by dumping the data before exceeding the heap end.
Since i am using an sd card, i was able to dump all std::vector<float> data into the sd card after the size of the vector reaches 100 and it solved the problem proberly.

Kind regards
Hussain
 
Back
Top