2020-04-20 21:16:39 +02:00
|
|
|
/* Reflow Oven Controller
|
2020-08-31 22:50:39 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-20 21:16:39 +02:00
|
|
|
|
|
|
|
#include <reflow-controller/oven-driver.h>
|
2020-05-10 23:13:03 +02:00
|
|
|
#include <reflow-controller/periph-config/oven-driver-hwcfg.h>
|
2020-12-01 21:00:23 +01:00
|
|
|
#include <stm-periph/rcc-manager.h>
|
2020-06-13 23:23:59 +02:00
|
|
|
#include <reflow-controller/systick.h>
|
2020-06-14 19:09:59 +02:00
|
|
|
#include <reflow-controller/adc-meas.h>
|
|
|
|
#include <reflow-controller/temp-converter.h>
|
2020-08-18 19:57:13 +02:00
|
|
|
#include <helper-macros/helper-macros.h>
|
2020-07-09 22:31:42 +02:00
|
|
|
#include <reflow-controller/safety/safety-controller.h>
|
2021-01-09 21:59:59 +01:00
|
|
|
#include <reflow-controller/hw-version-detect.h>
|
2020-06-13 23:23:59 +02:00
|
|
|
|
2020-08-21 23:25:03 +02:00
|
|
|
static struct pid_controller IN_SECTION(.ccm.bss) oven_pid;
|
2020-08-31 22:50:39 +02:00
|
|
|
static bool oven_pid_running;
|
|
|
|
static bool oven_pid_aborted;
|
|
|
|
static uint8_t IN_SECTION(.ccm.bss) oven_driver_power_level;
|
2020-11-30 21:43:38 +01:00
|
|
|
static float IN_SECTION(.ccm.bss) target_temp;
|
|
|
|
static uint64_t IN_SECTION(.ccm.bss) timestamp_last_run;
|
2020-05-05 18:55:55 +02:00
|
|
|
|
2021-01-09 21:59:59 +01:00
|
|
|
static void ssr_safety_en(bool enable)
|
|
|
|
{
|
|
|
|
if (get_pcb_hardware_version() >= HW_REV_V1_3) {
|
|
|
|
if (enable)
|
|
|
|
SSR_SAFETY_EN_PORT->ODR |= (1<<SSR_SAFETY_EN_PIN);
|
|
|
|
else
|
|
|
|
SSR_SAFETY_EN_PORT->ODR &= ~(1<<SSR_SAFETY_EN_PIN);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 22:50:39 +02:00
|
|
|
void oven_driver_init(void)
|
2020-05-05 18:55:55 +02:00
|
|
|
{
|
2020-05-10 23:13:03 +02:00
|
|
|
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));
|
2020-05-05 18:55:55 +02:00
|
|
|
|
2020-05-10 23:13:03 +02:00
|
|
|
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;
|
2020-08-31 22:50:39 +02:00
|
|
|
|
|
|
|
/* Explicitly init global variables */
|
|
|
|
oven_pid_aborted = false;
|
|
|
|
oven_pid_running = false;
|
|
|
|
|
2021-01-09 21:59:59 +01:00
|
|
|
if (get_pcb_hardware_version() >= HW_REV_V1_3) {
|
|
|
|
/* Init the safety SSR enable signal */
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(SSR_SAFETY_EN_PORT_RCC_MASK));
|
|
|
|
SSR_SAFETY_EN_PORT->MODER &= MODER_DELETE(SSR_SAFETY_EN_PIN);
|
|
|
|
SSR_SAFETY_EN_PORT->MODER |= OUTPUT(SSR_SAFETY_EN_PIN);
|
|
|
|
ssr_safety_en(false);
|
|
|
|
}
|
|
|
|
|
2020-08-31 22:50:39 +02:00
|
|
|
oven_driver_set_power(0U);
|
2020-11-30 21:43:38 +01:00
|
|
|
oven_driver_apply_power_level();
|
2020-05-05 18:55:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void oven_driver_set_power(uint8_t power)
|
|
|
|
{
|
2020-05-10 23:13:03 +02:00
|
|
|
if (power > 100U)
|
|
|
|
power = 100U;
|
2020-05-05 18:55:55 +02:00
|
|
|
|
2020-07-09 22:31:42 +02:00
|
|
|
oven_driver_power_level = power;
|
|
|
|
}
|
|
|
|
|
|
|
|
void oven_driver_apply_power_level(void)
|
|
|
|
{
|
|
|
|
OVEN_CONTROLLER_PWM_TIMER->CCR3 = oven_driver_power_level * 10;
|
2020-05-10 23:13:03 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 22:50:39 +02:00
|
|
|
void oven_driver_disable(void)
|
2020-05-10 23:13:03 +02:00
|
|
|
{
|
|
|
|
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));
|
2020-05-05 18:55:55 +02:00
|
|
|
}
|
2020-06-13 23:23:59 +02:00
|
|
|
|
2020-06-14 23:36:49 +02:00
|
|
|
void oven_pid_init(struct pid_controller *controller_to_copy)
|
|
|
|
{
|
|
|
|
pid_copy(&oven_pid, controller_to_copy);
|
|
|
|
oven_pid.output_sat_min = 0.0f;
|
|
|
|
oven_pid.output_sat_max = 100.0f;
|
2020-07-09 22:31:42 +02:00
|
|
|
|
|
|
|
oven_pid_running = true;
|
|
|
|
oven_pid_aborted = false;
|
|
|
|
safety_controller_report_timing(ERR_TIMING_PID);
|
2020-11-30 21:43:38 +01:00
|
|
|
timestamp_last_run = systick_get_global_tick();
|
2021-01-09 21:59:59 +01:00
|
|
|
ssr_safety_en(true);
|
2020-06-14 23:36:49 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 21:43:38 +01:00
|
|
|
void oven_pid_set_target_temperature(float temp)
|
|
|
|
{
|
|
|
|
target_temp = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void oven_pid_handle(void)
|
2020-06-13 23:23:59 +02:00
|
|
|
{
|
|
|
|
float pid_out;
|
2020-06-14 19:09:59 +02:00
|
|
|
float current_temp;
|
|
|
|
|
2020-07-09 22:31:42 +02:00
|
|
|
if (oven_pid_running && !oven_pid_aborted) {
|
|
|
|
if (systick_ticks_have_passed(timestamp_last_run, (uint64_t)(oven_pid.sample_period * 1000))) {
|
|
|
|
/* No need to check. Safety controller will monitor this */
|
|
|
|
(void)adc_pt1000_get_current_resistance(¤t_temp);
|
2020-06-14 19:09:59 +02:00
|
|
|
(void)temp_converter_convert_resistance_to_temp(current_temp, ¤t_temp);
|
|
|
|
|
2020-06-13 23:23:59 +02:00
|
|
|
pid_out = pid_sample(&oven_pid, target_temp - current_temp);
|
|
|
|
oven_driver_set_power((uint8_t)pid_out);
|
2020-07-09 22:31:42 +02:00
|
|
|
timestamp_last_run = systick_get_global_tick();
|
|
|
|
|
|
|
|
safety_controller_report_timing(ERR_TIMING_PID);
|
2020-06-13 23:23:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 22:50:39 +02:00
|
|
|
void oven_pid_stop(void)
|
2020-06-13 23:23:59 +02:00
|
|
|
{
|
2020-07-09 22:31:42 +02:00
|
|
|
oven_pid_running = false;
|
2020-08-31 22:50:39 +02:00
|
|
|
oven_driver_set_power(0U);
|
2020-07-09 22:31:42 +02:00
|
|
|
safety_controller_enable_timing_mon(ERR_TIMING_PID, false);
|
2021-01-09 21:59:59 +01:00
|
|
|
ssr_safety_en(false);
|
2020-06-13 23:23:59 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 22:54:06 +02:00
|
|
|
void oven_pid_abort(void)
|
|
|
|
{
|
|
|
|
oven_pid_aborted = true;
|
|
|
|
oven_pid_stop();
|
|
|
|
}
|
|
|
|
|
2020-07-09 22:31:42 +02:00
|
|
|
enum oven_pid_status oven_pid_get_status(void)
|
2020-06-13 23:23:59 +02:00
|
|
|
{
|
2020-07-09 22:31:42 +02:00
|
|
|
enum oven_pid_status ret = OVEN_PID_ABORTED;
|
2020-06-13 23:23:59 +02:00
|
|
|
|
2020-07-09 22:31:42 +02:00
|
|
|
if (oven_pid_running && !oven_pid_aborted)
|
|
|
|
ret = OVEN_PID_RUNNING;
|
|
|
|
else if (!oven_pid_running && !oven_pid_aborted)
|
|
|
|
ret = OVEN_PID_DEACTIVATED;
|
|
|
|
|
|
|
|
return ret;
|
2020-06-13 23:23:59 +02:00
|
|
|
}
|