Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

H7 timer/pwm output support #6905

Merged
merged 3 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/drivers/serial_softserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static bool isTimerPeriodTooLarge(uint32_t timerPeriod)

static void serialTimerConfigureTimebase(TCH_t * tch, uint32_t baud)
{
uint32_t baseClock = SystemCoreClock / timerClockDivisor(tch->timHw->tim);
uint32_t baseClock = timerClock(tch->timHw->tim);
uint32_t clock = baseClock;
uint32_t timerPeriod;

Expand Down
2 changes: 1 addition & 1 deletion src/main/drivers/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ uint32_t timerGetBaseClock(TCH_t * tch)

uint32_t timerGetBaseClockHW(const timerHardware_t * timHw)
{
return SystemCoreClock / timerClockDivisor(timHw->tim);
return timerClock(timHw->tim);
}

bool timerPWMConfigChannelDMA(TCH_t * tch, void * dmaBuffer, uint8_t dmaBufferElementSize, uint32_t dmaBufferElementCount)
Expand Down
2 changes: 1 addition & 1 deletion src/main/drivers/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ typedef enum {
TYPE_TIMER
} channelType_t;

uint8_t timerClockDivisor(TIM_TypeDef *tim);
uint32_t timerClock(TIM_TypeDef *tim);
uint32_t timerGetBaseClockHW(const timerHardware_t * timHw);

const timerHardware_t * timerGetByUsageFlag(timerUsageFlag_e flag);
Expand Down
4 changes: 2 additions & 2 deletions src/main/drivers/timer_stm32f30x.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ const timerDef_t timerDefinitions[HARDWARE_TIMER_DEFINITION_COUNT] = {
[16] = { .tim = TIM17, .rcc = RCC_APB2(TIM17), .irq = TIM1_TRG_COM_TIM17_IRQn },
};

uint8_t timerClockDivisor(TIM_TypeDef *tim)
uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
return 1;
return SystemCoreClock;
}

_TIM_IRQ_HANDLER(TIM1_CC_IRQHandler, 1);
Expand Down
8 changes: 4 additions & 4 deletions src/main/drivers/timer_stm32f4xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ const timerDef_t timerDefinitions[HARDWARE_TIMER_DEFINITION_COUNT] = {
#endif
};

uint8_t timerClockDivisor(TIM_TypeDef *tim)
uint32_t timerClock(TIM_TypeDef *tim)
{
#if defined (STM32F411xE)
UNUSED(tim);
return 1;
return SystemCoreClock;
#elif defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F446xx)
if (tim == TIM1 || tim == TIM8 || tim == TIM9 || tim == TIM10 || tim == TIM11) {
return 1;
return SystemCoreClock;
} else {
return 2;
return SystemCoreClock / 2;
}
#else
#error "No timer clock defined correctly for the MCU"
Expand Down
6 changes: 0 additions & 6 deletions src/main/drivers/timer_stm32f7xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ const timerDef_t timerDefinitions[HARDWARE_TIMER_DEFINITION_COUNT] = {
[13] = { .tim = TIM14, .rcc = RCC_APB1(TIM14), .irq = TIM8_TRG_COM_TIM14_IRQn},
};

uint8_t timerClockDivisor(TIM_TypeDef *tim)
{
UNUSED(tim);
return 1;
}

uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
Expand Down
50 changes: 40 additions & 10 deletions src/main/drivers/timer_stm32h7xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,48 @@ const timerDef_t timerDefinitions[HARDWARE_TIMER_DEFINITION_COUNT] = {
[13] = { .tim = TIM17, .rcc = RCC_APB2(TIM17), .irq = TIM17_IRQn},
};

uint8_t timerClockDivisor(TIM_TypeDef *tim)
{
UNUSED(tim);
return 1;
}

uint32_t timerClock(TIM_TypeDef *tim)
{
UNUSED(tim);
return SystemCoreClock;
int timpre;
uint32_t pclk;
uint32_t ppre;

// Implement the table:
// RM0433 (Rev 6) Table 52.
// RM0455 (Rev 3) Table 55.
// "Ratio between clock timer and pclk"
// (Tables are the same, just D2 or CD difference)

#if defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx)
#define PERIPH_PRESCALER(bus) ((RCC->D2CFGR & RCC_D2CFGR_D2PPRE ## bus) >> RCC_D2CFGR_D2PPRE ## bus ## _Pos)
#elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
#define PERIPH_PRESCALER(bus) ((RCC->CDCFGR2 & RCC_CDCFGR2_CDPPRE ## bus) >> RCC_CDCFGR2_CDPPRE ## bus ## _Pos)
#else
#error Unknown MCU type
#endif

if (tim == TIM1 || tim == TIM8 || tim == TIM15 || tim == TIM16 || tim == TIM17) {
// Timers on APB2
pclk = HAL_RCC_GetPCLK2Freq();
ppre = PERIPH_PRESCALER(2);
} else {
// Timers on APB1
pclk = HAL_RCC_GetPCLK1Freq();
ppre = PERIPH_PRESCALER(1);
}

timpre = (RCC->CFGR & RCC_CFGR_TIMPRE) ? 1 : 0;

int index = (timpre << 3) | ppre;

static uint8_t periphToKernel[16] = { // The mutiplier table
1, 1, 1, 1, 2, 2, 2, 2, // TIMPRE = 0
1, 1, 1, 1, 2, 4, 4, 4 // TIMPRE = 1
};

return pclk * periphToKernel[index];

#undef PERIPH_PRESCALER
}

_TIM_IRQ_HANDLER(TIM1_CC_IRQHandler, 1);
Expand All @@ -66,8 +98,6 @@ _TIM_IRQ_HANDLER(TIM3_IRQHandler, 3);
_TIM_IRQ_HANDLER(TIM4_IRQHandler, 4);
_TIM_IRQ_HANDLER(TIM5_IRQHandler, 5);
_TIM_IRQ_HANDLER(TIM8_CC_IRQHandler, 8);
//_TIM_IRQ_HANDLER(TIM1_BRK_TIM9_IRQHandler, 9);
//_TIM_IRQ_HANDLER(TIM1_TRG_COM_TIM11_IRQHandler, 11);
_TIM_IRQ_HANDLER(TIM8_BRK_TIM12_IRQHandler, 12);
_TIM_IRQ_HANDLER(TIM15_IRQHandler, 15);
_TIM_IRQ_HANDLER(TIM16_IRQHandler, 16);
Expand Down
26 changes: 24 additions & 2 deletions src/main/target/MATEKH743/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,30 @@ BUSDEV_REGISTER_SPI_TAG(busdev_imu2_6500, DEVHW_MPU6500, IMU2_SPI_BUS, IMU2
//BUSDEV_REGISTER_SPI_TAG(busdev_imu2_6000, DEVHW_MPU6000, IMU2_SPI_BUS, IMU2_CS_PIN, IMU2_EXTI_PIN, 1, DEVFLAGS_NONE, IMU2_ALIGN);

const timerHardware_t timerHardware[] = {
DEF_TIM(TIM2, CH1, PA0, TIM_USE_MC_MOTOR | TIM_USE_FW_MOTOR, 0, 0), // S1
DEF_TIM(TIM2, CH2, PA1, TIM_USE_MC_MOTOR | TIM_USE_FW_MOTOR, 0, 0), // S2
DEF_TIM(TIM3, CH3, PB0, TIM_USE_MC_MOTOR | TIM_USE_FW_MOTOR, 0, 2), // S1
DEF_TIM(TIM3, CH4, PB1, TIM_USE_MC_MOTOR | TIM_USE_FW_MOTOR, 0, 2), // S2

DEF_TIM(TIM5, CH1, PA0, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 0), // S3
DEF_TIM(TIM5, CH2, PA1, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 0), // S4
DEF_TIM(TIM5, CH3, PA2, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 0), // S5
DEF_TIM(TIM5, CH4, PA3, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 0), // S6

DEF_TIM(TIM4, CH1, PD12, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 1), // S7
DEF_TIM(TIM4, CH2, PD13, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 1), // S8
DEF_TIM(TIM4, CH3, PD14, TIM_USE_MC_MOTOR | TIM_USE_FW_SERVO, 0, 1), // S9

DEF_TIM(TIM4, CH4, PD15, TIM_USE_MC_SERVO | TIM_USE_FW_SERVO, 0, 0), // S10 DMA_NONE

DEF_TIM(TIM15, CH1, PE5, TIM_USE_MC_SERVO | TIM_USE_FW_SERVO, 0, 0), // S11
DEF_TIM(TIM15, CH2, PE6, TIM_USE_MC_SERVO | TIM_USE_FW_SERVO, 0, 0), // S12

DEF_TIM(TIM1, CH1, PA8, TIM_USE_LED, 0, 0), // LED_2812
DEF_TIM(TIM2, CH1, PA15, TIM_USE_BEEPER, 0, 0), // BEEPER PWM

DEF_TIM(TIM8, CH2, PC7, TIM_USE_PPM, 0, 0), // RX6 PPM
DEF_TIM(TIM8, CH1, PC6, TIM_USE_ANY, 0, 0), // TX6
DEF_TIM(TIM16, CH1, PB8, TIM_USE_ANY, 0, 0), // RX4
DEF_TIM(TIM17, CH1, PB9, TIM_USE_ANY, 0, 0), // TX4
};

const int timerHardwareCount = sizeof(timerHardware) / sizeof(timerHardware[0]);