Hi, i need some help figuring how to determine pin number when i have already set what timer and what channel to use.
In my project i'm using one timer and 3 channels (one for interrupt, one for PWM2 and one for toggle) and i'm planning to use pins based on timer, not a timer based on pin. Main reason is because i use STM32F103C8, STM32F401CE, STM32F411CC and STM32F407VE boards for testing, and they have different timers on same pin name. Yes, i could use per board definitions, but i'm trying to avoid that.
Here is small sketch (original sketch is too large):
Code: Select all
uint32_t HSYNC_pin ; // TIM2 CH3 // TIM5 CH3
uint32_t channel_HSYNC = 3 ; // TIM2 CH3 // TIM5 CH3 // PA2
#ifdef TIM1
HardwareTimer *MyTim = new HardwareTimer(TIM1);
#else
HardwareTimer *MyTim = new HardwareTimer(TIM2);
#endif
TIM_TypeDef * Instance = NULL; // Instance can be used for direct writing to timer's registers
void changeTimer(TIM_TypeDef * NEW_timer) {
delete MyTim ; // destroy previous instance
HardwareTimer *MyTim = new HardwareTimer(NEW_timer); // set new instance
Instance = NEW_timer;
// HSYNC_pin = ?????
}
void setup() {
changeTimer(TIM5); // TIM2 for STM32F103 // TIM5 for STM32F401CC
uint32_t TIMER_MHZ = ( MyTim->getTimerClkFreq()) / 1000000;
MyTim->pause();
MyTim->setPrescaleFactor(1); // count timers clock ticks
MyTim->setMode(channel_HSYNC, TIMER_OUTPUT_COMPARE_PWM2, HSYNC_pin); // <--------- HSYNC pin?
MyTim->setOverflow( TIMER_MHZ * 64, TICK_FORMAT); // first value
MyTim->setCaptureCompare(channel_HSYNC, 4.70*TIMER_MHZ, TICK_COMPARE_FORMAT);
MyTim->resume();
}
void loop() {
}
by Bakisha » Wed Jul 15, 2020 5:31 pm
I think i got it right this time. Tested on STM32F10C8 and STM32F401CE
Code: Select all
#ifdef TIM1
HardwareTimer *MyTim = new HardwareTimer(TIM1);
#else
HardwareTimer *MyTim = new HardwareTimer(TIM2);
#endif
TIM_TypeDef * Instance = NULL;
uint32_t changeTimerGetPin(TIM_TypeDef * NEW_timer, uint32_t NEW_channel) {
bool finished = false;
uint32_t pin_match = 0xffff;
for (int k = 0; k < 256; k++) { // from PA_0 to PK_15
if (!finished) {
for (int i = 0; i < NUM_DIGITAL_PINS; i++) { // whatever order is in variant.h
if (k == digitalPinToPinName(i) ) { // there is only 1 match per i loop
if ((NEW_timer == (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(i), PinMap_PWM))) { // same as requested timer?
if (NEW_channel == STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(i ), PinMap_PWM))) { // same as requested channel
pin_match = i;
finished = true; // only first match from PinMap_PWM is valid
}
}
}
}
}
}
delete MyTim ; // destroy previous instance
HardwareTimer *MyTim = new HardwareTimer(NEW_timer); // set new instance
Instance = NEW_timer;
return (pin_match) ;
}
void setup() {
Serial.begin(9600);
delay(1000);
uint32_t pin = changeTimerGetPin(TIM2, 2); // change timer, get pin number based on timer and channel
if (pin < 256) { // only if there is available pin on requested timer / channel
Serial.print( "requested pin found: " );
Serial.print(pin);
Serial.print( " - P" );
Serial.write(((digitalPinToPinName(pin) >> 4) & 0x0f) + 65 );
Serial.print(digitalPinToPinName(pin) & 0x0f);
Serial.println("");
}
else {
Serial.println( "no pwm output on that timer/channel " );
}
}
void loop() {
}
Go to full post