2020-02-15 22:09:55 +01:00
|
|
|
/* Reflow Oven Controller
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2020-02-21 21:22:01 +01:00
|
|
|
* The Reflow Oven Control Firmware is distributed in the hope that it will be useful,
|
2020-02-15 22:09:55 +01:00
|
|
|
* 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-02-16 17:35:35 +01:00
|
|
|
/**
|
|
|
|
* @file main.c
|
|
|
|
* @brief Main file for firmware
|
|
|
|
*/
|
|
|
|
|
2020-01-26 21:07:54 +01:00
|
|
|
#include <stdlib.h>
|
2020-05-04 21:21:49 +02:00
|
|
|
#include <stdio.h>
|
2020-02-02 00:01:08 +01:00
|
|
|
#include <string.h>
|
2020-05-05 00:01:09 +02:00
|
|
|
#include <inttypes.h>
|
2020-02-12 21:00:35 +01:00
|
|
|
/* #include <arm_math.h> */
|
|
|
|
#include <stm32/stm32f4xx.h>
|
|
|
|
#include <cmsis/core_cm4.h>
|
|
|
|
#include <setup/system_stm32f4xx.h>
|
|
|
|
#include <reflow-controller/systick.h>
|
|
|
|
#include <reflow-controller/adc-meas.h>
|
|
|
|
#include <reflow-controller/shell.h>
|
|
|
|
#include <reflow-controller/digio.h>
|
2020-04-26 21:23:25 +02:00
|
|
|
#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h"
|
2020-02-12 21:00:35 +01:00
|
|
|
#include <stm-periph/stm32-gpio-macros.h>
|
|
|
|
#include <stm-periph/clock-enable-manager.h>
|
2020-02-12 21:06:52 +01:00
|
|
|
#include <stm-periph/uart.h>
|
2020-02-24 18:50:09 +01:00
|
|
|
#include <reflow-controller/shell-uart-config.h>
|
2020-05-05 18:55:55 +02:00
|
|
|
#include <reflow-controller/oven-driver.h>
|
2020-05-16 21:00:55 +02:00
|
|
|
#include <reflow-controller/safety-adc.h>
|
2020-02-25 20:05:48 +01:00
|
|
|
#include <fatfs/ff.h>
|
2020-06-13 22:47:45 +02:00
|
|
|
#include <reflow-controller/reflow-menu.h>
|
2020-01-26 21:07:54 +01:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
bool global_error_state;
|
|
|
|
|
|
|
|
static void setup_nvic_priorities(void)
|
2020-01-26 21:07:54 +01:00
|
|
|
{
|
2020-02-02 20:24:44 +01:00
|
|
|
/* No sub priorities */
|
|
|
|
NVIC_SetPriorityGrouping(2);
|
2020-01-26 21:07:54 +01:00
|
|
|
|
2020-02-02 20:24:44 +01:00
|
|
|
/* Setup Priorities */
|
2020-02-05 23:09:23 +01:00
|
|
|
NVIC_SetPriority(ADC_IRQn, 2);
|
|
|
|
NVIC_SetPriority(DMA2_Stream0_IRQn, 1);
|
2020-02-12 21:00:35 +01:00
|
|
|
NVIC_SetPriority(DMA2_Stream7_IRQn, 3);
|
2020-02-02 00:01:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-26 21:23:25 +02:00
|
|
|
FATFS fs;
|
2020-06-14 14:50:27 +02:00
|
|
|
FATFS * const fs_ptr = &fs;
|
2020-04-26 21:23:25 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
static inline void uart_gpio_config(void)
|
2020-02-24 18:50:09 +01:00
|
|
|
{
|
2020-05-11 21:51:32 +02:00
|
|
|
/*
|
|
|
|
* In case the application is build in debug mode, use the TX/RX Pins on the debug header
|
|
|
|
* else the Pins on the DIGIO header are configured in the digio module
|
|
|
|
*/
|
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(SHELL_UART_PORT_RCC_MASK));
|
|
|
|
SHELL_UART_PORT->MODER &= MODER_DELETE(SHELL_UART_TX_PIN) & MODER_DELETE(SHELL_UART_RX_PIN);
|
|
|
|
SHELL_UART_PORT->MODER |= ALTFUNC(SHELL_UART_RX_PIN) | ALTFUNC(SHELL_UART_TX_PIN);
|
|
|
|
SETAF(SHELL_UART_PORT, SHELL_UART_RX_PIN, SHELL_UART_RX_PIN_ALTFUNC);
|
|
|
|
SETAF(SHELL_UART_PORT, SHELL_UART_TX_PIN, SHELL_UART_TX_PIN_ALTFUNC);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char shell_uart_tx_buff[128];
|
2020-05-05 18:55:55 +02:00
|
|
|
static char shell_uart_rx_buff[48];
|
2020-02-24 18:50:09 +01:00
|
|
|
struct stm_uart shell_uart;
|
|
|
|
|
|
|
|
static shellmatta_retCode_t write_shell_callback(const char *data, uint32_t len)
|
|
|
|
{
|
|
|
|
uart_send_array_with_dma(&shell_uart, data, len);
|
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-13 22:47:45 +02:00
|
|
|
static inline void setup_shell_uart(struct stm_uart *uart)
|
2020-02-24 18:50:09 +01:00
|
|
|
{
|
|
|
|
uart->rx = 1;
|
|
|
|
uart->tx = 1;
|
|
|
|
uart->brr_val = SHELL_UART_BRR_REG_VALUE;
|
|
|
|
uart->rcc_reg = &SHELL_UART_RCC_REG;
|
|
|
|
uart->rcc_bit_no = BITMASK_TO_BITNO(SHELL_UART_RCC_MASK);
|
|
|
|
uart->uart_dev = SHELL_UART_PERIPH;
|
|
|
|
uart->dma_rx_buff = shell_uart_rx_buff;
|
|
|
|
uart->dma_tx_buff = shell_uart_tx_buff;
|
|
|
|
uart->rx_buff_count = sizeof(shell_uart_rx_buff);
|
|
|
|
uart->tx_buff_count = sizeof(shell_uart_tx_buff);
|
|
|
|
uart->base_dma_num = 2;
|
|
|
|
uart->dma_rx_stream = SHELL_UART_RECEIVE_DMA_STREAM;
|
|
|
|
uart->dma_tx_stream = SHELL_UART_SEND_DMA_STREAM;
|
|
|
|
uart->dma_rx_trigger_channel = SHELL_UART_RX_DMA_TRIGGER;
|
|
|
|
uart->dma_tx_trigger_channel = SHELL_UART_TX_DMA_TRIGGER;
|
|
|
|
|
|
|
|
uart_init(uart);
|
|
|
|
NVIC_EnableIRQ(DMA2_Stream7_IRQn);
|
|
|
|
}
|
|
|
|
|
2020-04-26 21:23:25 +02:00
|
|
|
static bool mount_sd_card_if_avail(bool mounted)
|
|
|
|
{
|
|
|
|
FRESULT res;
|
|
|
|
|
|
|
|
if (sdio_check_inserted() && mounted) {
|
|
|
|
memset(fs_ptr, 0, sizeof(FATFS));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sdio_check_inserted() && !mounted) {
|
|
|
|
res = f_mount(fs_ptr, "0:/", 1);
|
2020-06-14 23:22:35 +02:00
|
|
|
if (res == FR_OK)
|
2020-04-26 21:23:25 +02:00
|
|
|
return true;
|
2020-06-14 23:22:35 +02:00
|
|
|
else
|
2020-04-26 21:23:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mounted;
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
static void setup_unused_pins(void)
|
2020-05-11 21:51:32 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_GPIOEEN));
|
|
|
|
GPIOE->MODER = 0UL;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
GPIOE->PUPDR |= PULLDOWN(i);
|
|
|
|
}
|
2020-05-05 18:55:55 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
static inline void setup_system(void)
|
2020-05-05 18:55:55 +02:00
|
|
|
{
|
2020-02-02 20:24:44 +01:00
|
|
|
setup_nvic_priorities();
|
2020-02-02 00:01:08 +01:00
|
|
|
systick_setup();
|
|
|
|
|
2020-02-02 01:49:37 +01:00
|
|
|
adc_pt1000_setup_meas();
|
2020-05-05 18:55:55 +02:00
|
|
|
oven_driver_init();
|
2020-02-10 22:38:24 +01:00
|
|
|
digio_setup_default_all();
|
|
|
|
led_setup();
|
|
|
|
loudspeaker_setup();
|
2020-06-12 01:35:37 +02:00
|
|
|
reflow_menu_init();
|
2020-05-16 21:00:55 +02:00
|
|
|
safety_adc_init();
|
2020-02-10 22:38:24 +01:00
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
uart_gpio_config();
|
2020-06-13 22:47:45 +02:00
|
|
|
setup_shell_uart(&shell_uart);
|
2020-05-11 21:51:32 +02:00
|
|
|
|
|
|
|
setup_unused_pins();
|
2020-05-05 18:55:55 +02:00
|
|
|
}
|
2020-02-09 19:13:37 +01:00
|
|
|
|
2020-05-11 21:51:32 +02:00
|
|
|
static void handle_shell_uart_input(shellmatta_handle_t shell_handle)
|
2020-05-05 18:55:55 +02:00
|
|
|
{
|
2020-05-11 21:51:32 +02:00
|
|
|
int uart_receive_status;
|
2020-05-05 18:55:55 +02:00
|
|
|
const char *uart_input;
|
|
|
|
size_t uart_input_len;
|
2020-05-11 21:51:32 +02:00
|
|
|
|
2020-06-13 22:47:45 +02:00
|
|
|
/* Handle UART input for shell */
|
2020-05-11 21:51:32 +02:00
|
|
|
uart_receive_status = uart_receive_data_with_dma(&shell_uart, &uart_input, &uart_input_len);
|
|
|
|
if (uart_receive_status >= 0)
|
|
|
|
shell_handle_input(shell_handle, uart_input, uart_input_len);
|
|
|
|
}
|
|
|
|
|
2020-06-13 22:47:45 +02:00
|
|
|
static void zero_ccm_ram(void)
|
|
|
|
{
|
2020-06-14 23:22:35 +02:00
|
|
|
/* These extern variables are placed in the linker script */
|
|
|
|
extern char _sccmram;
|
|
|
|
extern char _eccmram;
|
2020-06-13 22:47:45 +02:00
|
|
|
uint32_t len;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t *ptr = (uint32_t *)&_sccmram;
|
2020-05-16 21:00:55 +02:00
|
|
|
|
2020-06-13 22:47:45 +02:00
|
|
|
len = (uint32_t)&_eccmram - (uint32_t)&_sccmram;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
ptr[i] = 0UL;
|
|
|
|
}
|
2020-05-16 21:00:55 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
/**
|
|
|
|
* @brief This function sets the appropriate error flags in the oven PID controller
|
|
|
|
* depending on the Safety ADC measurements.
|
|
|
|
* The PID controller's error flags have to be cleared via the GUI by either starting a new RUN or explicitly
|
|
|
|
* ack'ing these errors.
|
|
|
|
*/
|
|
|
|
static void propagate_safety_adc_error_to_oven_pid(void)
|
|
|
|
{
|
|
|
|
enum safety_adc_check_result safety_adc_result;
|
|
|
|
|
|
|
|
safety_adc_result = safety_adc_get_errors();
|
2020-06-14 19:09:59 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
if (safety_adc_result & SAFETY_ADC_CHECK_TEMP_LOW ||
|
|
|
|
safety_adc_result & SAFETY_ADC_CHECK_TEMP_HIGH)
|
|
|
|
oven_pid_report_error(OVEN_PID_ERR_OVERTEMP);
|
|
|
|
|
|
|
|
if (safety_adc_result & SAFETY_ADC_CHECK_VREF_LOW ||
|
|
|
|
safety_adc_result & SAFETY_ADC_CHECK_VREF_HIGH)
|
|
|
|
oven_pid_report_error(OVEN_PID_ERR_VREF_TOL);
|
|
|
|
|
|
|
|
if (safety_adc_result & SAFETY_ADC_INTERNAL_ERROR)
|
|
|
|
oven_pid_report_error(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
2020-05-11 21:51:32 +02:00
|
|
|
{
|
|
|
|
bool sd_card_mounted = false;
|
2020-05-05 18:55:55 +02:00
|
|
|
shellmatta_handle_t shell_handle;
|
2020-06-12 01:35:37 +02:00
|
|
|
int menu_wait_request;
|
2020-06-13 22:47:45 +02:00
|
|
|
uint64_t quarter_sec_timestamp = 0ULL;
|
2020-06-14 19:09:59 +02:00
|
|
|
const struct oven_pid_status *pid_status;
|
|
|
|
enum adc_pt1000_error pt1000_status;
|
2020-05-05 18:55:55 +02:00
|
|
|
|
2020-06-13 22:47:45 +02:00
|
|
|
zero_ccm_ram();
|
2020-05-05 18:55:55 +02:00
|
|
|
setup_system();
|
2020-05-04 21:21:49 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
global_error_state = false;
|
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
shell_handle = shell_init(write_shell_callback);
|
2020-04-26 21:23:25 +02:00
|
|
|
shell_print_motd(shell_handle);
|
2020-02-09 19:13:37 +01:00
|
|
|
|
2020-02-21 23:37:48 +01:00
|
|
|
while (1) {
|
2020-04-26 21:23:25 +02:00
|
|
|
sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted);
|
|
|
|
|
2020-06-14 19:09:59 +02:00
|
|
|
pid_status = oven_pid_get_status();
|
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
if (systick_ticks_have_passed(quarter_sec_timestamp, 250)) {
|
2020-06-13 22:47:45 +02:00
|
|
|
quarter_sec_timestamp = systick_get_global_tick();
|
2020-06-14 19:09:59 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
(void)handle_safety_adc();
|
|
|
|
propagate_safety_adc_error_to_oven_pid();
|
2020-06-14 19:09:59 +02:00
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
if (global_error_state)
|
2020-06-14 19:13:50 +02:00
|
|
|
led_set(0, !led_get(0));
|
2020-06-14 23:22:35 +02:00
|
|
|
else
|
2020-06-14 19:13:50 +02:00
|
|
|
led_set(0, 0);
|
2020-04-20 21:12:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 22:56:34 +02:00
|
|
|
pt1000_status = adc_pt1000_check_error();
|
2020-06-14 23:36:49 +02:00
|
|
|
global_error_state = pid_status->error_set || !!safety_adc_get_errors() || !!pt1000_status;
|
2020-04-27 21:08:53 +02:00
|
|
|
|
2020-06-12 01:35:37 +02:00
|
|
|
menu_wait_request = reflow_menu_handle();
|
2020-06-14 19:09:59 +02:00
|
|
|
|
|
|
|
/* Deactivate oven output in case of error! */
|
2020-06-14 23:36:49 +02:00
|
|
|
if (!pid_status->active || global_error_state)
|
2020-06-14 19:09:59 +02:00
|
|
|
oven_driver_set_power(0U);
|
|
|
|
|
|
|
|
handle_shell_uart_input(shell_handle);
|
|
|
|
|
2020-06-14 01:31:44 +02:00
|
|
|
if (menu_wait_request)
|
2020-05-09 19:56:47 +02:00
|
|
|
__WFI();
|
2020-06-12 01:35:37 +02:00
|
|
|
|
2020-01-26 21:07:54 +01:00
|
|
|
}
|
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
return 0;
|
2020-01-26 21:07:54 +01:00
|
|
|
}
|
2020-02-24 18:50:09 +01:00
|
|
|
|
2020-02-25 20:05:48 +01:00
|
|
|
void sdio_wait_ms(uint32_t ms)
|
|
|
|
{
|
|
|
|
systick_wait_ms(ms);
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
void DMA2_Stream7_IRQHandler(void)
|
2020-02-24 18:50:09 +01:00
|
|
|
{
|
|
|
|
uint32_t hisr = DMA2->HISR;
|
2020-06-14 23:22:35 +02:00
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
DMA2->HIFCR = hisr;
|
|
|
|
|
2020-06-14 23:22:35 +02:00
|
|
|
if (hisr & DMA_HISR_TCIF7)
|
2020-02-24 18:50:09 +01:00
|
|
|
uart_tx_dma_complete_int_callback(&shell_uart);
|
|
|
|
}
|