From 8d6c81441d00a3846b7833bbbc8aedd323e7e1e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 10 May 2020 23:13:03 +0200 Subject: [PATCH] Add oven PWM controller --- .../include/reflow-controller/oven-driver.h | 2 ++ .../periph-config/oven-driver-hwcfg.h | 33 +++++++++++++++++++ .../include/stm-periph/stm32-gpio-macros.h | 2 +- stm-firmware/main.c | 3 ++ stm-firmware/oven-driver.c | 28 ++++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 stm-firmware/include/reflow-controller/periph-config/oven-driver-hwcfg.h diff --git a/stm-firmware/include/reflow-controller/oven-driver.h b/stm-firmware/include/reflow-controller/oven-driver.h index 4f6f317..4690419 100644 --- a/stm-firmware/include/reflow-controller/oven-driver.h +++ b/stm-firmware/include/reflow-controller/oven-driver.h @@ -27,4 +27,6 @@ void oven_driver_init(void); void oven_driver_set_power(uint8_t power); +void oven_driver_disable(void); + #endif /* __OVEN_DRIVER_H__ */ diff --git a/stm-firmware/include/reflow-controller/periph-config/oven-driver-hwcfg.h b/stm-firmware/include/reflow-controller/periph-config/oven-driver-hwcfg.h new file mode 100644 index 0000000..9fab85c --- /dev/null +++ b/stm-firmware/include/reflow-controller/periph-config/oven-driver-hwcfg.h @@ -0,0 +1,33 @@ +/* Reflow Oven Controller +* +* Copyright (C) 2020 Mario Hüttel +* +* This file is part of the Reflow Oven Controller Project. +* +* The reflow oven controller is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* The Reflow Oven Control Firmware is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with the reflow oven controller project. +* If not, see . +*/ + +#ifndef __OVEN_DRIVER_HWCFG_H__ +#define __OVEN_DRIVER_HWCFG_H__ + +#include + +#define OVEN_CONTROLLER_PWM_TIMER TIM3 +#define OVEN_CONTROLLER_PORT GPIOB +#define OVEN_CONTROLLER_PIN (0) +#define OVEN_CONTROLLER_PORT_RCC_MASK RCC_AHB1ENR_GPIOBEN +#define OVEN_CONTROLLER_TIM_RCC_MASK RCC_APB1ENR_TIM3EN +#define OVEN_CONTROLLER_PIN_ALTFUNC (2) + +#endif /* __OVEN_DRIVER_HWCFG_H__ */ diff --git a/stm-firmware/include/stm-periph/stm32-gpio-macros.h b/stm-firmware/include/stm-periph/stm32-gpio-macros.h index e71c1ad..f94e4da 100644 --- a/stm-firmware/include/stm-periph/stm32-gpio-macros.h +++ b/stm-firmware/include/stm-periph/stm32-gpio-macros.h @@ -27,7 +27,7 @@ #define PULLUP(pin) (0x1U << ((pin)* 2)) #define ALTFUNC(pin) ((0x2) << ((pin) * 2)) #define PINMASK(pin) ((0x3) << ((pin) * 2)) -#define SETAF(PORT,PIN,AF) PORT->AFR[((PIN) < 8 ? 0 : 1)] |= AF << (((PIN) < 8 ? (PIN) : ((PIN) - 8)) * 4) +#define SETAF(PORT,PIN,AF) PORT->AFR[((PIN) < 8 ? 0 : 1)] |= (AF) << (((PIN) < 8 ? (PIN) : ((PIN) - 8)) * 4) #define ANALOG(pin) (0x03 << ((pin) * 2)) #define OTYP_OPENDRAIN(pin) (0x1U << (pin)) diff --git a/stm-firmware/main.c b/stm-firmware/main.c index 97b29ab..0038bc9 100644 --- a/stm-firmware/main.c +++ b/stm-firmware/main.c @@ -236,6 +236,9 @@ int main() button = button_read_event(); rot = rotary_encoder_get_abs_val(); + oven_driver_set_power(rot > 100U ? 100U : rot); + + /* TODO: handle gui */ snprintf(&disp[1][0], 17, "Rotary: %u", (unsigned int)rot); snprintf(&disp[2][0], 17, "Button: %s", (button == BUTTON_SHORT ? "SHORT" : (button == BUTTON_LONG ? "LONG" : ""))); diff --git a/stm-firmware/oven-driver.c b/stm-firmware/oven-driver.c index beb356b..7a01a9f 100644 --- a/stm-firmware/oven-driver.c +++ b/stm-firmware/oven-driver.c @@ -19,13 +19,41 @@ */ #include +#include +#include void oven_driver_init() { + rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(OVEN_CONTROLLER_PORT_RCC_MASK)); + rcc_manager_enable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(OVEN_CONTROLLER_TIM_RCC_MASK)); + OVEN_CONTROLLER_PORT->MODER &= MODER_DELETE(OVEN_CONTROLLER_PIN); + OVEN_CONTROLLER_PORT->MODER |= ALTFUNC(OVEN_CONTROLLER_PIN); + SETAF(OVEN_CONTROLLER_PORT, OVEN_CONTROLLER_PIN, OVEN_CONTROLLER_PIN_ALTFUNC); + + OVEN_CONTROLLER_PWM_TIMER->CR2 = 0UL; + + OVEN_CONTROLLER_PWM_TIMER->CCMR2 = TIM_CCMR2_OC3M; + OVEN_CONTROLLER_PWM_TIMER->CCER = TIM_CCER_CC3E | TIM_CCER_CC3P; + + OVEN_CONTROLLER_PWM_TIMER->ARR = 1000U; + OVEN_CONTROLLER_PWM_TIMER->PSC = 42000U - 1U; + + OVEN_CONTROLLER_PWM_TIMER->CR1 = TIM_CR1_CMS | TIM_CR1_CEN; } void oven_driver_set_power(uint8_t power) { + if (power > 100U) + power = 100U; + OVEN_CONTROLLER_PWM_TIMER->CCR3 = power * 10; +} + +void oven_driver_disable() +{ + OVEN_CONTROLLER_PWM_TIMER->CR1 = 0UL; + OVEN_CONTROLLER_PWM_TIMER->CR2 = 0UL; + rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(OVEN_CONTROLLER_PORT_RCC_MASK)); + rcc_manager_disable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(OVEN_CONTROLLER_TIM_RCC_MASK)); }