simplefoc / Arduino-FOC

Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library
https://docs.simplefoc.com
MIT License
1.95k stars 511 forks source link

[FEATURE] Support 6-channel PWM with only TC4 on ATmega32u4 #294

Open cinderblock opened 11 months ago

cinderblock commented 11 months ago

I'd like to use some existing ATmega32u4 based driver boards. Theses are wired up to the 6-channel PWM output that is unique to the ATmega32u4's 10-bit High Speed Timer/Counter4.

AVR Pin Output Compare Arduino Pin
PC7 OC4A D13
PC6 OC4A~ D5
PB6 OC4B D10
PB5 OC4B~ D9
PD7 OC4D D6
PD6 OC4D~ D12

Looking at the sources in atmega32u4_mcu.cpp, it's clear that the code is only set up for a couple dedicated pins that don't fully overlap with the Timer 4 hardware outputs.

I realize this is an older IC but is there any interest in supporting this mode?

My very old implementation: https://github.com/cinderblock/3-Phase-Controller/blob/master/src/ThreePhase/Driver.cpp


PS, great to meet you at OpenSauce!

askuric commented 11 months ago

Hey, this could be a nice addition, would you be interested to do a PR for this?

cinderblock commented 11 months ago

Yes, I'm interested. But I won't have time to look much at this for the next 2, maybe 6+ weeks! (Moving offices, Burning Man, traveling to a wedding) So if anyone else wants to get started, be my guest!


To be sure, would this (new) code live in this repo or in the drivers repo?

runger1101001 commented 11 months ago

From my point of view it would be one of the PWM drivers in the main repository, or a modification to one of the existing AVR drivers…

askuric commented 11 months ago

Yep, I agree. This would go the the main library inside the file atmega32u4_mcu.cpp. I am going to try to provide you with a starting point @cinderblock, maybe even implement it completely, if I manage to get the time. Otherwise, don't hesitate to contact us when you start implementing it if you need some assistance.