what is the pwm frequency of the arduino giga wifi
I believe this depends on the pin you are using, but normally it should be in the 500-1000hz range.
However it would be possible, and I plan to write a library to allow you to easily change this, over 1mhz should be possible while keeping a 255 step resolution.
use code like this to go as fast as you want:
void sendOutputToESCs() {
//SET ESCs HIGH
GPIOG->ODR |= 1UL << 13; //ESC 1, Pin 23
GPIOJ->ODR |= 1UL << 0; //ESC 2, Pin 25
GPIOJ->ODR |= 1UL << 1; //ESC 3, Pin 27
GPIOJ->ODR |= 1UL << 2; //ESC 4, Pin 29
//calculate how long to wait
esc_1_timer = loop_timer + esc_1_power;
esc_2_timer = loop_timer + esc_2_power;
esc_3_timer = loop_timer + esc_3_power;
esc_4_timer = loop_timer + esc_4_power;
//wait until time completes, then set to low
while (GPIOG->IDR & (1UL << 13) | GPIOJ->IDR & (1UL << 0) | GPIOJ->IDR & (1UL << 1) | GPIOJ->IDR & (1UL << 2)) {
esc_loop_timer = getCurrentTimeInMicroseconds(false);
if (esc_1_timer <= esc_loop_timer) {
GPIOG->ODR &= ~(1UL << 13); //ESC 1, Pin 23
}
if (esc_2_timer <= esc_loop_timer) {
GPIOJ->ODR &= ~(1UL << 0); //ESC 2, Pin 25
}
if (esc_3_timer <= esc_loop_timer) {
GPIOJ->ODR &= ~(1UL << 1); //ESC 3, Pin 27
}
if (esc_4_timer <= esc_loop_timer) {
GPIOJ->ODR &= ~(1UL << 2); //ESC 4, Pin 29
}
}
}
how do I use this? and is it in micropython?
This is arduino code, you’d have to include it in your code. Though this is not going to work for high speed pwm really. I will try to look into writing code soon ish
The code works to sub-microsecond precision, and can toggle the pin 1 million times per second if combined with working micros() code. I literally pulled out the STM32 data sheet and am directly changing internal registers, which takes 1-3 clock cycles according to the STM32 documentation. Nothing is faster than that! Also when combined with a hardware timer such as “Timer2” you can toggle the pins for PWM over 100 million times per second on the M7 core as confined by my logic analyzer.
The thing that’s “slow” about this code compared to the maximum possible speed is doing pwm manually, you can define the duty cycle in registers, which would especially save up processing time, and allow you to do other stuff simultaneously.
If you read the STM32 hardware PWM documentation it states it uses hardware interrupts that cost you the same 1-3 clock cycles. Also hardware PWM is not fully customizable, if I wanted PWM of half a microsecond or 10 mins it’s not possible. When you have 480 million clock cycles burning a few cycles here and there is not significant, the L1 cache on the M7 costs you more cycles when it randomly misses (ARM M series has some of the worst branch prediction I’ve ever seen). This isn’t like AVR where everything is atomic and not cache hierarchy based. You are right that the code could be modified to not waste time, but that is easily done with level 2 timer interrupts.
My code I posted is from ESC code I wrote for a drone and I was hoping people would adapt it to their own needs.
Did you create a library for this? Can you help me how to increase PWM frequency in Giga board?
Thanks
Would you please share path to your code for PWM frequency modification in giga ?
Thanks
Any chance your library could create a simple hardware clock output with a software controlled frequency in the 1,000 to 100,000 Hz range?
The interrupt itself yes, but entering to your custom function, run it and leave will cost you hundreds times more. Generate a PWM by interrupts and software timers just is not a good approach. The STM32 core has a very rich set of PWM modes and settings, that can be run by hardware, without manually managed micros timer.
You should read the datasheet more carefully.