5#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
17int _stm32_scoreCombination(
int numPins, PinMap* pinTimers[]) {
19 for (
int i=0; i<numPins; i++) {
20 if (stm32_isChannelUsed(pinTimers[i]))
30 for (
int i=0; i<numPins; i++) {
31 if (STM_PIN_INVERTED(pinTimers[i]->function))
36 for (
int i=0; i<numPins-1; i++) {
37 for (
int j=i+1; j<numPins; j++) {
38 if (pinTimers[i]->peripheral == pinTimers[j]->peripheral
39 && STM_PIN_CHANNEL(pinTimers[i]->function) == STM_PIN_CHANNEL(pinTimers[j]->function)
40 && STM_PIN_INVERTED(pinTimers[i]->function) == STM_PIN_INVERTED(pinTimers[j]->function))
45 for (
int i=0; i<numPins; i++) {
48 for (
int j=i+1; j<numPins; j++) {
49 if (pinTimers[i]->peripheral == pinTimers[j]->peripheral)
60 if (STM_PIN_INVERTED(pinTimers[0]->function) || STM_PIN_INVERTED(pinTimers[2]->function) || STM_PIN_INVERTED(pinTimers[4]->function))
62 if (pinTimers[0]->peripheral == pinTimers[1]->peripheral
63 && pinTimers[2]->peripheral == pinTimers[3]->peripheral
64 && pinTimers[4]->peripheral == pinTimers[5]->peripheral
65 && STM_PIN_CHANNEL(pinTimers[0]->function) == STM_PIN_CHANNEL(pinTimers[1]->function)
66 && STM_PIN_CHANNEL(pinTimers[2]->function) == STM_PIN_CHANNEL(pinTimers[3]->function)
67 && STM_PIN_CHANNEL(pinTimers[4]->function) == STM_PIN_CHANNEL(pinTimers[5]->function)
68 && STM_PIN_INVERTED(pinTimers[1]->function) && STM_PIN_INVERTED(pinTimers[3]->function) && STM_PIN_INVERTED(pinTimers[5]->function)) {
77 #if defined(STM32F4xx_HAL_TIM_H) || defined(STM32F3xx_HAL_TIM_H) || defined(STM32F2xx_HAL_TIM_H) || defined(STM32F1xx_HAL_TIM_H) || defined(STM32F100_HAL_TIM_H) || defined(STM32FG0x1_HAL_TIM_H) || defined(STM32G0x0_HAL_TIM_H)
78 if (STM_PIN_CHANNEL(pinTimers[0]->function)>3 || STM_PIN_CHANNEL(pinTimers[2]->function)>3 || STM_PIN_CHANNEL(pinTimers[4]->function)>3 )
81 #ifdef STM32G4xx_HAL_TIM_H
82 if (STM_PIN_CHANNEL(pinTimers[0]->function)>4 || STM_PIN_CHANNEL(pinTimers[2]->function)>4 || STM_PIN_CHANNEL(pinTimers[4]->function)>4 )
88 if (STM_PIN_INVERTED(pinTimers[1]->function) || STM_PIN_INVERTED(pinTimers[3]->function) || STM_PIN_INVERTED(pinTimers[5]->function))
90 if (pinTimers[0]->peripheral != pinTimers[1]->peripheral
91 || pinTimers[2]->peripheral != pinTimers[3]->peripheral
92 || pinTimers[4]->peripheral != pinTimers[5]->peripheral)
103int _stm32_findIndexOfFirstPinMapEntry(
int pin) {
104 PinName pinName = digitalPinToPinName(pin);
106 while (PinMap_TIM[i].pin!=NC) {
107 if (pinName == PinMap_TIM[i].pin)
115int _stm32_findIndexOfLastPinMapEntry(
int pin) {
116 PinName pinName = digitalPinToPinName(pin);
118 while (PinMap_TIM[i].pin!=NC) {
119 if ( pinName == (PinMap_TIM[i].pin & ~ALTX_MASK)
120 && pinName != (PinMap_TIM[i+1].pin & ~ALTX_MASK))
132#define NOT_FOUND 1000
134int _stm32_findBestTimerCombination(
int numPins,
int index,
int pins[], PinMap* pinTimers[]) {
135 PinMap* searchArray[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
136 for (
int j=0;j<numPins;j++)
137 searchArray[j] = pinTimers[j];
138 int bestScore = NOT_FOUND;
139 int startIndex = _stm32_findIndexOfFirstPinMapEntry(pins[index]);
140 int endIndex = _stm32_findIndexOfLastPinMapEntry(pins[index]);
141 if (startIndex == -1 || endIndex == -1) {
145 for (
int i=startIndex;i<=endIndex;i++) {
146 searchArray[index] = (PinMap*)&PinMap_TIM[i];
147 int score = NOT_FOUND;
149 score = _stm32_findBestTimerCombination(numPins, index+1, pins, searchArray);
151 score = _stm32_scoreCombination(numPins, searchArray);
152 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
153 stm32_printTimerCombination(numPins, searchArray, score);
158 if (score>=0 && score<bestScore) {
160 for (
int j=index;j<numPins;j++)
161 pinTimers[j] = searchArray[j];
171int stm32_findBestTimerCombination(
int numPins,
int pins[], PinMap* pinTimers[]) {
172 int bestScore = _stm32_findBestTimerCombination(numPins, 0, pins, pinTimers);
173 if (bestScore == NOT_FOUND) {
174 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
179 else if (bestScore >= 0) {
180 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
182 stm32_printTimerCombination(numPins, pinTimers, bestScore);
#define SIMPLEFOC_DEBUG(msg,...)
static void print(const char *msg)