2#include "../../hardware_api.h"
7#if defined(_STM32_DEF_) || defined(TARGET_STM32H7)
10#pragma message("SimpleFOC: compiling for STM32")
28TIM_HandleTypeDef* timersUsed[SIMPLEFOC_STM32_MAX_TIMERSUSED];
31int numTimersReserved = 0;
32TIM_TypeDef* reservedTimers[SIMPLEFOC_STM32_MAX_TIMERSRESERVED];
36STM32DriverParams* motorsUsed[SIMPLEFOC_STM32_MAX_MOTORSUSED];
39int stm32_getNumTimersUsed() {
42int stm32_getNumMotorsUsed() {
45int stm32_getNumTimersReserved() {
46 return numTimersReserved;
48bool stm32_isTimerReserved(TIM_TypeDef* timer) {
49 for (
int i=0; i<numTimersReserved; i++) {
50 if (reservedTimers[i] == timer)
55bool stm32_isTimerUsed(TIM_HandleTypeDef* timer) {
56 for (
int i=0; i<numTimersUsed; i++) {
57 if (timersUsed[i] == timer)
62STM32DriverParams* stm32_getMotorUsed(
int index) {
63 return motorsUsed[index];
65bool stm32_isChannelUsed(PinMap* pin) {
66 if (stm32_isTimerReserved((TIM_TypeDef*)pin->peripheral)) {
69 for (
int i=0; i<numMotorsUsed; i++) {
70 for (
int j=0; j<6; j++) {
71 if (motorsUsed[i]->timers_handle[j] == NULL)
break;
72 if (motorsUsed[i]->channels[j] == STM_PIN_CHANNEL(pin->function) && ((TIM_TypeDef*)pin->peripheral) == motorsUsed[i]->timers_handle[j]->Instance)
78TIM_HandleTypeDef* stm32_getTimer(PinMap* timer) {
79 for (
int i=0; i<numTimersUsed; i++) {
80 if (timersUsed[i]->Instance == (TIM_TypeDef*)timer->peripheral)
85bool stm32_reserveTimer(TIM_TypeDef* timer) {
86 if (numTimersReserved >= SIMPLEFOC_STM32_MAX_TIMERSRESERVED) {
90 reservedTimers[numTimersReserved++] = timer;
95TIM_HandleTypeDef* stm32_useTimer(PinMap* timer) {
96 TIM_HandleTypeDef* handle = stm32_getTimer(timer);
97 if (handle != NULL)
return handle;
98 if (numTimersUsed >= SIMPLEFOC_STM32_MAX_TIMERSUSED) {
102 if (stm32_isTimerReserved((TIM_TypeDef*)timer->peripheral)) {
106 handle =
new TIM_HandleTypeDef();
107 handle->Instance = (TIM_TypeDef*)timer->peripheral;
108 handle->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
109 handle->Lock = HAL_UNLOCKED;
110 handle->State = HAL_TIM_STATE_RESET;
111 handle->hdma[0] = NULL;
112 handle->hdma[1] = NULL;
113 handle->hdma[2] = NULL;
114 handle->hdma[3] = NULL;
115 handle->hdma[4] = NULL;
116 handle->hdma[5] = NULL;
117 handle->hdma[6] = NULL;
118 handle->Init.Prescaler = 0;
119 handle->Init.Period = ((1 << 16) - 1);
120 handle->Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED2;
121 handle->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
122 handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
123 #if defined(TIM_RCR_REP)
124 handle->Init.RepetitionCounter = 1;
126 enableTimerClock(handle);
127 HAL_TIM_Base_Init(handle);
128 stm32_pauseTimer(handle);
129 timersUsed[numTimersUsed++] = handle;
136bool _getPwmState(
void*
params) {
138 bool dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(((STM32DriverParams*)
params)->timers_handle[0]);
145void stm32_pause(STM32DriverParams*
params) {
146 if (
params->master_timer != NULL) {
147 stm32_pauseTimer(
params->master_timer);
150 for (
int i=0; i<
params->num_timers; i++) {
151 stm32_pauseTimer(
params->timers_handle[i]);
158void stm32_resume(STM32DriverParams*
params) {
159 if (
params->master_timer != NULL) {
160 stm32_resumeTimer(
params->master_timer);
163 for (
int i=0; i<
params->num_timers; i++) {
164 stm32_resumeTimer(
params->timers_handle[i]);
172TIM_HandleTypeDef* stm32_initPinPWM(uint32_t PWM_freq, PinMap* timer, uint32_t mode = TIM_OCMODE_PWM1, uint32_t polarity = TIM_OCPOLARITY_HIGH, uint32_t Npolarity = TIM_OCNPOLARITY_HIGH) {
176 TIM_HandleTypeDef* handle = stm32_getTimer(timer);
177 uint32_t channel = STM_PIN_CHANNEL(timer->function);
179 handle = stm32_useTimer(timer);
180 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
181 SIMPLEFOC_DEBUG(
"STM32-DRV: Initializing TIM", (
int)stm32_getTimerNumber(handle->Instance));
183 uint32_t arr = stm32_setClockAndARR(handle, PWM_freq);
184 if (arr<SIMPLEFOC_STM32_MIN_RESOLUTION) {
185 SIMPLEFOC_DEBUG(
"STM32-DRV: WARN timer resolution too low (<8bit): ", (
int)arr+1);
188 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
193 TIM_OC_InitTypeDef channelOC;
194 channelOC.OCMode = mode;
196 channelOC.OCPolarity = polarity;
197 channelOC.OCFastMode = TIM_OCFAST_DISABLE;
198#if defined(TIM_CR2_OIS1)
199 channelOC.OCIdleState = TIM_OCIDLESTATE_RESET;
201#if defined(TIM_CCER_CC1NE)
202 channelOC.OCNPolarity = Npolarity;
203#if defined(TIM_CR2_OIS1)
204 channelOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
207 HAL_TIM_PWM_ConfigChannel(handle, &channelOC, stm32_getHALChannel(channel));
208 pinmap_pinout(timer->pin, PinMap_TIM);
209 LL_TIM_CC_EnableChannel(handle->Instance, stm32_getLLChannel(timer));
210 if (IS_TIM_BREAK_INSTANCE(handle->Instance)) {
211 __HAL_TIM_MOE_ENABLE(handle);
213 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
235TIM_HandleTypeDef* _stm32_initPinPWMHigh(uint32_t PWM_freq, PinMap* timer) {
237 TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, timer, TIM_OCMODE_PWM1, polarity);
238 LL_TIM_OC_EnablePreload(handle->Instance, stm32_getLLChannel(timer));
243TIM_HandleTypeDef* _stm32_initPinPWMLow(uint32_t PWM_freq, PinMap* timer) {
245 TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, timer, TIM_OCMODE_PWM2, polarity);
246 LL_TIM_OC_EnablePreload(handle->Instance, stm32_getLLChannel(timer));
279int stm32_checkTimerFrequency(
long pwm_frequency, TIM_HandleTypeDef *timers[], uint8_t num_timers){
280 TIM_HandleTypeDef* timers_distinct[6];
281 uint8_t timer_num = stm32_distinctTimers(timers, num_timers, timers_distinct);
282 float common_pwm_freq = 0.0f;
283 for (
int i=0; i<timer_num; i++) {
284 uint32_t freq = stm32_getTimerClockFreq(timers_distinct[i]);
285 uint32_t arr = timers_distinct[i]->Instance->ARR;
286 uint32_t prescaler = timers_distinct[i]->Instance->PSC;
287 float pwm_freq = (float)freq/(prescaler+1.0f)/(arr+1.0f)/2.0f;
289 common_pwm_freq = pwm_freq;
291 if (pwm_freq != common_pwm_freq) {
292 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
305 if ( (common_pwm_freq - (
float)pwm_frequency) > 1.0f) {
306 #if defined(SIMPLEFOC_STM32_DEBUG) && !defined(SIMPLEFOC_DISABLE_DEBUG)
307 SIMPLEFOC_DEBUG(
"STM32-DRV: ERR: Common timer frequency unexpected: ", common_pwm_freq);
316STM32DriverParams* _stm32_initHardware6PWMPair(
long PWM_freq,
float dead_zone, PinMap* pinH, PinMap* pinL, STM32DriverParams*
params,
int paramsPos) {
318 if (pinH==NULL || pinL==NULL)
320#if defined(STM32L0xx)
324 uint32_t channel1 = STM_PIN_CHANNEL(pinH->function);
325 uint32_t channel2 = STM_PIN_CHANNEL(pinL->function);
328 if (channel1!=channel2 || pinH->peripheral!=pinL->peripheral)
333 TIM_HandleTypeDef* handle = stm32_initPinPWM(PWM_freq, pinH, TIM_OCMODE_PWM1, polarity, Npolarity);
334 pinmap_pinout(pinL->pin, PinMap_TIM);
337 uint32_t dead_time_ns = (float)(1e9f/PWM_freq)*
dead_zone;
338 uint32_t dead_time = __LL_TIM_CALC_DEADTIME(stm32_getTimerClockFreq(handle), LL_TIM_GetClockDivision(handle->Instance), dead_time_ns);
339 if (dead_time>255) dead_time = 255;
342 SIMPLEFOC_DEBUG(
"STM32-DRV: WARN: dead time too large, setting to max");
348 LL_TIM_SetOffStates(handle->Instance, LL_TIM_OSSI_DISABLE, LL_TIM_OSSR_ENABLE);
349 LL_TIM_OC_SetDeadTime(handle->Instance, dead_time);
350 LL_TIM_CC_EnableChannel(handle->Instance, stm32_getLLChannel(pinH) | stm32_getLLChannel(pinL));
351 params->timers_handle[paramsPos] = handle;
352 params->timers_handle[paramsPos+1] = handle;
353 params->channels[paramsPos] = channel1;
354 params->channels[paramsPos+1] = channel2;
355 params->llchannels[paramsPos] = stm32_getLLChannel(pinH);
356 params->llchannels[paramsPos+1] = stm32_getLLChannel(pinL);
364 STM32DriverParams*
params =
new STM32DriverParams {
365 .timers_handle = { NULL, NULL, NULL, NULL, NULL, NULL },
366 .channels = { 0, 0, 0, 0, 0, 0 },
367 .llchannels = { 0, 0, 0, 0, 0, 0 },
368 .pwm_frequency = PWM_freq,
370 .master_timer = NULL,
372 .interface_type = _HARDWARE_6PWM
380 params->num_timers = stm32_countTimers(
params->timers_handle, 6);
390 if (numMotorsUsed+1 > SIMPLEFOC_STM32_MAX_MOTORSUSED) {
395 if( !pwm_frequency || !
_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY;
397 int pins[1] = {
pinA };
398 PinMap* pinTimers[1] = { NULL };
399 if (stm32_findBestTimerCombination(1, pins, pinTimers)<0)
402 TIM_HandleTypeDef* HT1 = stm32_initPinPWM(pwm_frequency, pinTimers[0], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
403 uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
405 STM32DriverParams*
params =
new STM32DriverParams {
406 .timers_handle = { HT1 },
407 .channels = { channel1 },
408 .llchannels = { stm32_getLLChannel(pinTimers[0]) },
409 .pwm_frequency = pwm_frequency,
414 params->master_timer = stm32_alignTimers(
params->timers_handle, 1);
415 motorsUsed[numMotorsUsed++] =
params;
427 if (numMotorsUsed+1 > SIMPLEFOC_STM32_MAX_MOTORSUSED) {
432 if( !pwm_frequency || !
_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY;
435 PinMap* pinTimers[2] = { NULL, NULL };
436 if (stm32_findBestTimerCombination(2, pins, pinTimers)<0)
439 TIM_HandleTypeDef* HT1 = stm32_initPinPWM(pwm_frequency, pinTimers[0], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
440 TIM_HandleTypeDef* HT2 = stm32_initPinPWM(pwm_frequency, pinTimers[1], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
441 TIM_HandleTypeDef *timers[2] = {HT1, HT2};
442 stm32_checkTimerFrequency(pwm_frequency, timers, 2);
444 uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
445 uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
447 STM32DriverParams*
params =
new STM32DriverParams {
448 .timers_handle = { HT1, HT2 },
449 .channels = { channel1, channel2 },
450 .llchannels = { stm32_getLLChannel(pinTimers[0]), stm32_getLLChannel(pinTimers[1]) },
451 .pwm_frequency = pwm_frequency,
452 .num_timers = stm32_countTimers(timers, 2),
456 params->master_timer = stm32_alignTimers(timers, 2);
457 motorsUsed[numMotorsUsed++] =
params;
468 if (numMotorsUsed+1 > SIMPLEFOC_STM32_MAX_MOTORSUSED) {
472 if( !pwm_frequency || !
_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY;
475 PinMap* pinTimers[3] = { NULL, NULL, NULL };
476 if (stm32_findBestTimerCombination(3, pins, pinTimers)<0)
479 TIM_HandleTypeDef* HT1 = stm32_initPinPWM(pwm_frequency, pinTimers[0], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
480 TIM_HandleTypeDef* HT2 = stm32_initPinPWM(pwm_frequency, pinTimers[1], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
481 TIM_HandleTypeDef* HT3 = stm32_initPinPWM(pwm_frequency, pinTimers[2], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
483 TIM_HandleTypeDef *timers[3] = {HT1, HT2, HT3};
484 stm32_checkTimerFrequency(pwm_frequency, timers, 3);
486 uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
487 uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
488 uint32_t channel3 = STM_PIN_CHANNEL(pinTimers[2]->function);
490 STM32DriverParams*
params =
new STM32DriverParams {
491 .timers_handle = { HT1, HT2, HT3 },
492 .channels = { channel1, channel2, channel3 },
493 .llchannels = { stm32_getLLChannel(pinTimers[0]), stm32_getLLChannel(pinTimers[1]), stm32_getLLChannel(pinTimers[2]) },
494 .pwm_frequency = pwm_frequency,
495 .num_timers = stm32_countTimers(timers, 3),
498 params->master_timer = stm32_alignTimers(timers, 3);
499 motorsUsed[numMotorsUsed++] =
params;
509 if (numMotorsUsed+1 > SIMPLEFOC_STM32_MAX_MOTORSUSED) {
513 if( !pwm_frequency || !
_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY;
516 PinMap* pinTimers[4] = { NULL, NULL, NULL, NULL };
517 if (stm32_findBestTimerCombination(4, pins, pinTimers)<0)
520 TIM_HandleTypeDef* HT1 = stm32_initPinPWM(pwm_frequency, pinTimers[0], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
521 TIM_HandleTypeDef* HT2 = stm32_initPinPWM(pwm_frequency, pinTimers[1], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
522 TIM_HandleTypeDef* HT3 = stm32_initPinPWM(pwm_frequency, pinTimers[2], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
523 TIM_HandleTypeDef* HT4 = stm32_initPinPWM(pwm_frequency, pinTimers[3], TIM_OCMODE_PWM1, (
SIMPLEFOC_PWM_ACTIVE_HIGH)?TIM_OCPOLARITY_HIGH:TIM_OCPOLARITY_LOW);
524 TIM_HandleTypeDef *timers[4] = {HT1, HT2, HT3, HT4};
525 stm32_checkTimerFrequency(pwm_frequency, timers, 4);
527 uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
528 uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
529 uint32_t channel3 = STM_PIN_CHANNEL(pinTimers[2]->function);
530 uint32_t channel4 = STM_PIN_CHANNEL(pinTimers[3]->function);
532 STM32DriverParams*
params =
new STM32DriverParams {
533 .timers_handle = { HT1, HT2, HT3, HT4 },
534 .channels = { channel1, channel2, channel3, channel4 },
535 .llchannels = { stm32_getLLChannel(pinTimers[0]), stm32_getLLChannel(pinTimers[1]), stm32_getLLChannel(pinTimers[2]), stm32_getLLChannel(pinTimers[3]) },
536 .pwm_frequency = pwm_frequency,
537 .num_timers = stm32_countTimers(timers, 4),
540 params->master_timer = stm32_alignTimers(timers, 4);
541 motorsUsed[numMotorsUsed++] =
params;
551 uint32_t duty = dc_a*(((STM32DriverParams*)
params)->timers_handle[0]->Instance->ARR+1);
552 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], duty);
561 uint32_t duty1 = dc_a*(((STM32DriverParams*)
params)->timers_handle[0]->Instance->ARR+1);
562 uint32_t duty2 =
dc_b*(((STM32DriverParams*)
params)->timers_handle[1]->Instance->ARR+1);
563 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
564 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], duty1);
565 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[1], ((STM32DriverParams*)
params)->channels[1], duty2);
566 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
573 uint32_t duty1 = dc_a*(((STM32DriverParams*)
params)->timers_handle[0]->Instance->ARR+1);
574 uint32_t duty2 =
dc_b*(((STM32DriverParams*)
params)->timers_handle[1]->Instance->ARR+1);
575 uint32_t duty3 =
dc_c*(((STM32DriverParams*)
params)->timers_handle[2]->Instance->ARR+1);
576 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
577 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], duty1);
578 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[1], ((STM32DriverParams*)
params)->channels[1], duty2);
579 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[2], ((STM32DriverParams*)
params)->channels[2], duty3);
580 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
588 uint32_t duty1 = dc_1a*(((STM32DriverParams*)
params)->timers_handle[0]->Instance->ARR+1);
589 uint32_t duty2 =
dc_1b*(((STM32DriverParams*)
params)->timers_handle[1]->Instance->ARR+1);
590 uint32_t duty3 =
dc_2a*(((STM32DriverParams*)
params)->timers_handle[2]->Instance->ARR+1);
591 uint32_t duty4 =
dc_2b*(((STM32DriverParams*)
params)->timers_handle[3]->Instance->ARR+1);
592 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
593 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], duty1);
594 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[1], ((STM32DriverParams*)
params)->channels[1], duty2);
595 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[2], ((STM32DriverParams*)
params)->channels[2], duty3);
596 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[3], ((STM32DriverParams*)
params)->channels[3], duty4);
597 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
607 if (numMotorsUsed+1 > SIMPLEFOC_STM32_MAX_MOTORSUSED) {
611 if( !pwm_frequency || !
_isset(pwm_frequency) ) pwm_frequency = SIMPLEFOC_STM32_PWM_FREQUENCY;
615 PinMap* pinTimers[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
616 int score = stm32_findBestTimerCombination(6, pins, pinTimers);
618 STM32DriverParams*
params;
623 params = _stm32_initHardware6PWMInterface(pwm_frequency,
dead_zone, pinTimers[0], pinTimers[1], pinTimers[2], pinTimers[3], pinTimers[4], pinTimers[5]);
625 TIM_HandleTypeDef* HT1 = _stm32_initPinPWMHigh(pwm_frequency, pinTimers[0]);
626 TIM_HandleTypeDef* HT2 = _stm32_initPinPWMLow(pwm_frequency, pinTimers[1]);
627 TIM_HandleTypeDef* HT3 = _stm32_initPinPWMHigh(pwm_frequency, pinTimers[2]);
628 TIM_HandleTypeDef* HT4 = _stm32_initPinPWMLow(pwm_frequency, pinTimers[3]);
629 TIM_HandleTypeDef* HT5 = _stm32_initPinPWMHigh(pwm_frequency, pinTimers[4]);
630 TIM_HandleTypeDef* HT6 = _stm32_initPinPWMLow(pwm_frequency, pinTimers[5]);
631 TIM_HandleTypeDef *timers[6] = {HT1, HT2, HT3, HT4, HT5, HT6};
632 stm32_checkTimerFrequency(pwm_frequency, timers, 6);
633 uint32_t channel1 = STM_PIN_CHANNEL(pinTimers[0]->function);
634 uint32_t channel2 = STM_PIN_CHANNEL(pinTimers[1]->function);
635 uint32_t channel3 = STM_PIN_CHANNEL(pinTimers[2]->function);
636 uint32_t channel4 = STM_PIN_CHANNEL(pinTimers[3]->function);
637 uint32_t channel5 = STM_PIN_CHANNEL(pinTimers[4]->function);
638 uint32_t channel6 = STM_PIN_CHANNEL(pinTimers[5]->function);
639 params =
new STM32DriverParams {
640 .timers_handle = { HT1, HT2, HT3, HT4, HT5, HT6 },
641 .channels = { channel1, channel2, channel3, channel4, channel5, channel6 },
642 .llchannels = { stm32_getLLChannel(pinTimers[0]), stm32_getLLChannel(pinTimers[1]), stm32_getLLChannel(pinTimers[2]), stm32_getLLChannel(pinTimers[3]), stm32_getLLChannel(pinTimers[4]), stm32_getLLChannel(pinTimers[5]) },
643 .pwm_frequency = pwm_frequency,
644 .num_timers = stm32_countTimers(timers, 6),
645 .master_timer = NULL,
647 .interface_type = _SOFTWARE_6PWM
651 params->master_timer = stm32_alignTimers(
params->timers_handle, 6);
652 motorsUsed[numMotorsUsed++] =
params;
659void _setSinglePhaseState(
PhaseState state, TIM_HandleTypeDef *HT,
int llchannel_hi,
int llchannel_lo) {
662 stm32_pauseChannel(HT, llchannel_hi | llchannel_lo);
665 stm32_pauseChannel(HT, llchannel_lo);
666 stm32_resumeChannel(HT, llchannel_hi);
669 stm32_pauseChannel(HT, llchannel_hi);
670 stm32_resumeChannel(HT, llchannel_lo);
674 stm32_resumeChannel(HT, llchannel_hi | llchannel_lo);
687 switch(((STM32DriverParams*)
params)->interface_type){
689 duty1 = dc_a*(((STM32DriverParams*)
params)->timers_handle[0]->Instance->ARR+1);
690 duty2 =
dc_b*(((STM32DriverParams*)
params)->timers_handle[2]->Instance->ARR+1);
691 duty3 =
dc_c*(((STM32DriverParams*)
params)->timers_handle[4]->Instance->ARR+1);
695 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
696 _setSinglePhaseState(
phase_state[0], ((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->llchannels[0], ((STM32DriverParams*)
params)->llchannels[1]);
697 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], duty1);
698 _setSinglePhaseState(
phase_state[1], ((STM32DriverParams*)
params)->timers_handle[2], ((STM32DriverParams*)
params)->llchannels[2], ((STM32DriverParams*)
params)->llchannels[3]);
699 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[2], ((STM32DriverParams*)
params)->channels[2], duty2);
700 _setSinglePhaseState(
phase_state[2], ((STM32DriverParams*)
params)->timers_handle[4], ((STM32DriverParams*)
params)->llchannels[4], ((STM32DriverParams*)
params)->llchannels[5]);
701 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[4], ((STM32DriverParams*)
params)->channels[4], duty3);
702 if (((STM32DriverParams*)
params)->num_timers == 1) LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
709 uint32_t duty1N =
_constrain(dc_a +
dead_zone, 0.0f, 1.0f)*(((STM32DriverParams*)
params)->timers_handle[1]->Instance->ARR+1);
713 LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
715 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], duty1);
717 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[0], ((STM32DriverParams*)
params)->channels[0], 0);
719 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[1], ((STM32DriverParams*)
params)->channels[1], duty1N);
721 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[1], ((STM32DriverParams*)
params)->channels[1], 0);
723 LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[2]->Instance);
725 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[2], ((STM32DriverParams*)
params)->channels[2], duty2);
727 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[2], ((STM32DriverParams*)
params)->channels[2], 0);
729 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[3], ((STM32DriverParams*)
params)->channels[3], duty2N);
731 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[3], ((STM32DriverParams*)
params)->channels[3], 0);
733 LL_TIM_DisableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[4]->Instance);
735 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[4], ((STM32DriverParams*)
params)->channels[4], duty3);
737 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[4], ((STM32DriverParams*)
params)->channels[4], 0);
739 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[5], ((STM32DriverParams*)
params)->channels[5], duty3N);
741 stm32_setPwm(((STM32DriverParams*)
params)->timers_handle[5], ((STM32DriverParams*)
params)->channels[5], 0);
743 LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[0]->Instance);
744 LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[2]->Instance);
745 LL_TIM_EnableUpdateEvent(((STM32DriverParams*)
params)->timers_handle[4]->Instance);
#define SIMPLEFOC_DEBUG(msg,...)
static void print(const char *msg)
const int const int const int pinC
GenericCurrentSenseParams * params
#define SIMPLEFOC_PWM_ACTIVE_HIGH
void * _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B)
void * _configure6PWM(long pwm_frequency, float dead_zone, const int pinA_h, const int pinA_l, const int pinB_h, const int pinB_l, const int pinC_h, const int pinC_l)
void * _configure2PWM(long pwm_frequency, const int pinA, const int pinB)
void _writeDutyCycle2PWM(float dc_a, float dc_b, void *params)
void _writeDutyCycle1PWM(float dc_a, void *params)
#define SIMPLEFOC_DRIVER_INIT_FAILED
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, void *params)
void * _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC)
#define SIMPLEFOC_PWM_LOWSIDE_ACTIVE_HIGH
void _writeDutyCycle6PWM(float dc_a, float dc_b, float dc_c, PhaseState *phase_state, void *params)
void * _configure1PWM(long pwm_frequency, const int pinA)
#define SIMPLEFOC_PWM_HIGHSIDE_ACTIVE_HIGH
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, void *params)
float const int const int const int pinB_h
float const int const int const int const int pinB_l
float const int const int const int const int const int pinC_h
float float PhaseState * phase_state
float const int const int const int const int const int const int pinC_l
float const int const int pinA_l
#define _constrain(amt, low, high)