diff --git a/flight/PiOS/Boards/STM32F4xx_Revolution.h b/flight/PiOS/Boards/STM32F4xx_Revolution.h index 061b0fce0..f8cacf260 100644 --- a/flight/PiOS/Boards/STM32F4xx_Revolution.h +++ b/flight/PiOS/Boards/STM32F4xx_Revolution.h @@ -135,9 +135,38 @@ extern uint32_t pios_com_telem_usb_id; //------------------------- // System Settings +// +// See also System_stm32f4xx.c //------------------------- -#define PIOS_MASTER_CLOCK 168000000 -#define PIOS_PERIPHERAL_CLOCK (PIOS_MASTER_CLOCK / 2) +//These macros are deprecated +//please use PIOS_PERIPHERAL_APBx_CLOCK According to the table below +//#define PIOS_MASTER_CLOCK +//#define PIOS_PERIPHERAL_CLOCK +//#define PIOS_PERIPHERAL_CLOCK + +#define PIOS_SYSCLK 168000000 +// Peripherals that belongs to APB1 are: +// DAC |PWR |CAN1,2 +// I2C1,2,3 |UART4,5 |USART3,2 +// I2S3Ext |SPI3/I2S3 |SPI2/I2S2 +// I2S2Ext |IWDG |WWDG +// RTC/BKP reg +// TIM2,3,4,5,6,7,12,13,14 + +// Calculated as SYSCLK / APBPresc * (APBPre == 1 ? 1 : 2) +// Default APB1 Prescaler = 4 +#define PIOS_PERIPHERAL_APB1_CLOCK (PIOS_SYSCLK / 2) + +// Peripherals belonging to APB2 +// SDIO |EXTI |SYSCFG |SPI1 +// ADC1,2,3 +// USART1,6 +// TIM1,8,9,10,11 +// +// Default APB2 Prescaler = 2 +// +#define PIOS_PERIPHERAL_APB2_CLOCK PIOS_SYSCLK + //------------------------- // Interrupt Priorities diff --git a/flight/PiOS/STM32F4xx/pios_servo.c b/flight/PiOS/STM32F4xx/pios_servo.c index 876911126..cfcf5c1e5 100644 --- a/flight/PiOS/STM32F4xx/pios_servo.c +++ b/flight/PiOS/STM32F4xx/pios_servo.c @@ -96,7 +96,7 @@ void PIOS_Servo_SetHz(uint16_t * speeds, uint8_t banks) TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = servo_cfg->tim_base_init; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; - TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1; + // uint8_t set = 0; @@ -109,6 +109,14 @@ void PIOS_Servo_SetHz(uint16_t * speeds, uint8_t banks) new &= chan->timer != servo_cfg->channels[j].timer; if(new) { + // Choose the correct prescaler value for the APB the timer is attached + if (chan->timer==TIM1 || chan->timer==TIM8 || chan->timer==TIM9 || chan->timer==TIM10 || chan->timer==TIM11 ){ + TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_PERIPHERAL_APB2_CLOCK / 1000000) - 1; + } + else { + TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_PERIPHERAL_APB1_CLOCK / 1000000) - 1; + } + TIM_TimeBaseStructure.TIM_Period = ((1000000 / speeds[set]) - 1); TIM_TimeBaseInit(chan->timer, &TIM_TimeBaseStructure); set++; diff --git a/flight/board_hw_defs/revolution/board_hw_defs.c b/flight/board_hw_defs/revolution/board_hw_defs.c index 1cf70fc31..dff3bfc76 100644 --- a/flight/board_hw_defs/revolution/board_hw_defs.c +++ b/flight/board_hw_defs/revolution/board_hw_defs.c @@ -974,8 +974,15 @@ void PIOS_RTC_IRQ_Handler (void) #include "pios_tim_priv.h" -static const TIM_TimeBaseInitTypeDef tim_3_5_9_10_11_time_base = { - .TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1, +static const TIM_TimeBaseInitTypeDef tim_3_5_time_base = { + .TIM_Prescaler = (PIOS_PERIPHERAL_APB1_CLOCK / 1000000) - 1, + .TIM_ClockDivision = TIM_CKD_DIV1, + .TIM_CounterMode = TIM_CounterMode_Up, + .TIM_Period = ((1000000 / PIOS_SERVO_UPDATE_HZ) - 1), + .TIM_RepetitionCounter = 0x0000, +}; +static const TIM_TimeBaseInitTypeDef tim_9_10_11_time_base = { + .TIM_Prescaler = (PIOS_PERIPHERAL_APB2_CLOCK / 1000000) - 1, .TIM_ClockDivision = TIM_CKD_DIV1, .TIM_CounterMode = TIM_CounterMode_Up, .TIM_Period = ((1000000 / PIOS_SERVO_UPDATE_HZ) - 1), @@ -984,7 +991,7 @@ static const TIM_TimeBaseInitTypeDef tim_3_5_9_10_11_time_base = { static const struct pios_tim_clock_cfg tim_3_cfg = { .timer = TIM3, - .time_base_init = &tim_3_5_9_10_11_time_base, + .time_base_init = &tim_3_5_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM3_IRQn, @@ -997,7 +1004,7 @@ static const struct pios_tim_clock_cfg tim_3_cfg = { static const struct pios_tim_clock_cfg tim_5_cfg = { .timer = TIM5, - .time_base_init = &tim_3_5_9_10_11_time_base, + .time_base_init = &tim_3_5_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM5_IRQn, @@ -1010,7 +1017,7 @@ static const struct pios_tim_clock_cfg tim_5_cfg = { static const struct pios_tim_clock_cfg tim_9_cfg = { .timer = TIM9, - .time_base_init = &tim_3_5_9_10_11_time_base, + .time_base_init = &tim_9_10_11_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn, @@ -1023,7 +1030,7 @@ static const struct pios_tim_clock_cfg tim_9_cfg = { static const struct pios_tim_clock_cfg tim_10_cfg = { .timer = TIM10, - .time_base_init = &tim_3_5_9_10_11_time_base, + .time_base_init = &tim_9_10_11_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM1_UP_TIM10_IRQn, @@ -1036,7 +1043,7 @@ static const struct pios_tim_clock_cfg tim_10_cfg = { static const struct pios_tim_clock_cfg tim_11_cfg = { .timer = TIM11, - .time_base_init = &tim_3_5_9_10_11_time_base, + .time_base_init = &tim_9_10_11_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM1_TRG_COM_TIM11_IRQn, @@ -1047,9 +1054,17 @@ static const struct pios_tim_clock_cfg tim_11_cfg = { }, }; -// Set up timers that only have inputs -static const TIM_TimeBaseInitTypeDef tim_1_4_time_base = { - .TIM_Prescaler = (PIOS_MASTER_CLOCK / 1000000) - 1, +// Set up timers that only have inputs on APB2 +static const TIM_TimeBaseInitTypeDef tim_1_time_base = { + .TIM_Prescaler = (PIOS_PERIPHERAL_APB2_CLOCK / 1000000) - 1, + .TIM_ClockDivision = TIM_CKD_DIV1, + .TIM_CounterMode = TIM_CounterMode_Up, + .TIM_Period = 0xFFFF, + .TIM_RepetitionCounter = 0x0000, +}; +// Set up timers that only have inputs on APB2 +static const TIM_TimeBaseInitTypeDef tim_4_time_base = { + .TIM_Prescaler = (PIOS_PERIPHERAL_APB1_CLOCK / 1000000) - 1, .TIM_ClockDivision = TIM_CKD_DIV1, .TIM_CounterMode = TIM_CounterMode_Up, .TIM_Period = 0xFFFF, @@ -1058,7 +1073,7 @@ static const TIM_TimeBaseInitTypeDef tim_1_4_time_base = { static const struct pios_tim_clock_cfg tim_1_cfg = { .timer = TIM1, - .time_base_init = &tim_1_4_time_base, + .time_base_init = &tim_1_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM1_CC_IRQn, @@ -1071,7 +1086,7 @@ static const struct pios_tim_clock_cfg tim_1_cfg = { static const struct pios_tim_clock_cfg tim_4_cfg = { .timer = TIM4, - .time_base_init = &tim_1_4_time_base, + .time_base_init = &tim_4_time_base, .irq = { .init = { .NVIC_IRQChannel = TIM4_IRQn,