From fdc493fcef8b7bf985d76644628c065927a278da Mon Sep 17 00:00:00 2001 From: tkl Date: Tue, 23 Aug 2016 11:40:57 +0200 Subject: [PATCH] add trigger pwm for ultrasonic module --- .../include/stm32f4-discovery.h | 189 ++++++------------ .../arch/stm32f4xx/driver/stm32f4_pwm.c | 14 +- source/test/pwm/main.c | 3 + 3 files changed, 63 insertions(+), 143 deletions(-) diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h index 9860cb4..32fe8bb 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h @@ -308,6 +308,63 @@ const struct driver pwm_1 = { &pwm_ch1, }; +// PWM5 CHANNEL 2 +/* apb1 clock = 84MHz */ +/* period_reg = src_clk / presc / cnt_clk */ +/* 1679 = 84MHZ / 1000 / 50Hz - 1 */ +static TIM_HandleTypeDef tim5_handle = { + .Instance = TIM5, + .Init.Prescaler = 1000, + .Init.CounterMode = TIM_COUNTERMODE_UP, + .Init.Period = 1679, + .Init.ClockDivision = TIM_CLOCKDIVISION_DIV1, + .Init.RepetitionCounter = 0, +}; + +static TIM_OC_InitTypeDef t5_output_compare_cfg = { + .OCMode = TIM_OCMODE_PWM1, + .Pulse = 840, + .OCPolarity = TIM_OCPOLARITY_HIGH, + .OCNPolarity = TIM_OCNPOLARITY_HIGH, + .OCFastMode = TIM_OCFAST_DISABLE, + .OCIdleState = TIM_OCIDLESTATE_SET, + .OCNIdleState = TIM_OCNIDLESTATE_SET +}; + +static const GPIO_InitTypeDef port_cfg_A1 = { + .Pin = GPIO_PIN_1, + .Mode = GPIO_MODE_AF_PP, + .Speed = GPIO_SPEED_FREQ_HIGH, + .Pull = GPIO_PULLUP, + .Alternate = GPIO_AF2_TIM5, +}; + +static const struct stm32f4_gpio t5c2_gpio = { + .port = GPIOA, + .pin = &port_cfg_A1, +}; + +static struct stm32f4_pwm str32f4_pwm5_c2 = { + .pwm_gpio = &t5c2_gpio, + .timer_handle = &tim5_handle, + .output_compare_cfg = &t5_output_compare_cfg, + .channel = TIM_CHANNEL_2, +}; + +static const struct pwm pwm5_ch2 = { + .arch_dep_device = &str32f4_pwm5_c2, + .fp = &stm32f4_pwm_fp, +}; + +#ifdef TEST_APP +static const struct driver pwm5_c2 = { +#else +const struct driver pwm5_c2 = { +#endif + DRIVER_TYPE_PWM, + &pwm5_ch2, +}; + // UART 1 static const GPIO_InitTypeDef port_cfg_uart1 = { .Pin = GPIO_PIN_6 | GPIO_PIN_7, @@ -373,138 +430,6 @@ const struct driver uart_1 = { &__uart_1, }; -#if 0 -// GPIOC0 -static const GPIO_InitTypeDef port_cfg_C0 = { - GPIO_Pin_0, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_gpio_C0 = { - GPIOC, - &port_cfg_C0, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __gpio_c0 = { - (void*)&stm32_f4_gpio_C0, - &gpio_fp -}; - -#ifdef TEST_APP -static const struct driver gpio_c0 = { -#else -const struct driver gpio_c0 = { -#endif - DRIVER_TYPE_GPIO, - &__gpio_c0, -}; -#endif -#if 0 -// GPIO_C1 -static const GPIO_InitTypeDef port_cfg_C1 = { - GPIO_Pin_1, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_gpio_C1 = { - GPIOC, - &port_cfg_C1, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __gpio_c1 = { - (void*)&stm32_f4_gpio_C1, - &gpio_fp -}; - -#ifdef TEST_APP -static const struct driver gpio_c1 = { -#else -const struct driver gpio_c1 = { -#endif - DRIVER_TYPE_GPIO, - &__gpio_c1, -}; -#endif -#if 0 -// GPIO_C2 -static const GPIO_InitTypeDef port_cfg_C2 = { - GPIO_Pin_2, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_gpio_C2 = { - GPIOC, - &port_cfg_C2, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __gpio_c2 = { - (void*)&stm32_f4_gpio_C2, - &gpio_fp -}; - -#ifdef TEST_APP -static const struct driver gpio_c2 = { -#else -const struct driver gpio_c2 = { -#endif - DRIVER_TYPE_GPIO, - &__gpio_c2, -}; -#endif -#if 0 -// GPIO_C3 -static const GPIO_InitTypeDef port_cfg_C3 = { - GPIO_Pin_3, - GPIO_Mode_OUT, - GPIO_Speed_100MHz, - GPIO_OType_PP, - GPIO_PuPd_NOPULL -}; - -static const struct stm32f4_gpio stm32_f4_gpio_C3 = { - GPIOC, - &port_cfg_C3, - NULL, - NULL, - NULL, - NULL -}; - -static const struct gpio __gpio_c3 = { - (void*)&stm32_f4_gpio_C3, - &gpio_fp -}; - -#ifdef TEST_APP -static const struct driver gpio_c3 = { -#else -const struct driver gpio_c3 = { -#endif - DRIVER_TYPE_GPIO, - &__gpio_c3, -}; -#endif //! \brief Setup the hardware of the stm32f4-discovery board. void board_init(void); diff --git a/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c b/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c index bca8c13..580c5cb 100644 --- a/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c +++ b/source/firmware/arch/stm32f4xx/driver/stm32f4_pwm.c @@ -15,17 +15,6 @@ #include "pwm.h" #include "stm32f4_pwm.h" -#pragma pack(push) -#pragma pack(1) -struct stm32f4_pwm_object { - uint8_t used_channels; - uint32_t channel_1_max_period; - uint32_t channel_2_max_period; - uint32_t channel_3_max_period; - uint32_t channel_4_max_period; -}; -#pragma pack(pop) - int stm32f4_pwm_open(const void *pwm) { if(NULL == pwm) @@ -35,6 +24,9 @@ int stm32f4_pwm_open(const void *pwm) if(this->timer_handle->Instance == TIM4) { __HAL_RCC_TIM4_CLK_ENABLE(); } + if(this->timer_handle->Instance == TIM5) { + __HAL_RCC_TIM5_CLK_ENABLE(); + } HAL_TIM_PWM_Init(this->timer_handle); HAL_TIM_PWM_ConfigChannel(this->timer_handle, this->output_compare_cfg, this->channel); diff --git a/source/test/pwm/main.c b/source/test/pwm/main.c index 01371b0..5dc1f09 100644 --- a/source/test/pwm/main.c +++ b/source/test/pwm/main.c @@ -22,6 +22,9 @@ int main(void) drv_open(&pwm_2); drv_open(&pwm_3); drv_open(&pwm_4); + + drv_open(&pwm5_c2); + while(1) { for(unsigned int duty = 0; duty < 100; duty++) { drv_ioctl(&pwm_1, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);