reflow-oven-control-sw/stm-firmware/main.c

310 lines
8.2 KiB
C
Raw Permalink Normal View History

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.
*
* 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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2020-02-12 21:00:35 +01:00
#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>
#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/rcc-manager.h>
2020-02-12 21:06:52 +01:00
#include <stm-periph/uart.h>
#include <reflow-controller/periph-config/shell-uart-config.h>
#include <reflow-controller/oven-driver.h>
#include <fatfs/ff.h>
2020-11-29 19:04:10 +01:00
#include <reflow-controller/ui/gui.h>
2020-07-09 22:31:42 +02:00
#include <reflow-controller/safety/safety-controller.h>
#include <reflow-controller/settings/settings.h>
#include <reflow-controller/safety/safety-memory.h>
#include <reflow-controller/safety/fault.h>
#include <reflow-controller/updater/updater.h>
2021-05-22 16:40:08 +02:00
#include <reflow-controller/temp-profile/temp-profile-executer.h>
#include <reflow-controller/settings/spi-eeprom.h>
#include <reflow-controller/main-cycle-counter.h>
static void setup_nvic_priorities(void)
{
2020-02-02 20:24:44 +01:00
/* No sub priorities */
NVIC_SetPriorityGrouping(2);
2020-02-02 20:24:44 +01:00
/* Setup Priorities */
NVIC_SetPriority(ADC_IRQn, 2);
2020-09-27 23:23:44 +02:00
/* Measurement ADC DMA */
NVIC_SetPriority(DMA2_Stream0_IRQn, 1);
2020-09-27 23:23:44 +02:00
/* Shelmatta UART TX */
2020-02-12 21:00:35 +01:00
NVIC_SetPriority(DMA2_Stream7_IRQn, 3);
NVIC_SetPriority(DMA2_Stream4_IRQn, 2);
}
FATFS fs;
2020-08-22 13:33:53 +02:00
#define fs_ptr (&fs)
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
*/
#if defined(DEBUGBUILD) || defined(UART_ON_DEBUG_HEADER)
2020-02-24 18:50:09 +01:00
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);
2020-06-21 01:29:50 +02:00
/* Setup Pullup resistor at UART RX */
SHELL_UART_PORT->PUPDR |= PULLUP(SHELL_UART_RX_PIN);
2020-02-24 18:50:09 +01:00
#endif
}
2020-11-29 20:00:16 +01:00
static char shell_uart_tx_buff[256];
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;
}
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);
}
static bool mount_sd_card_if_avail(bool mounted)
{
FRESULT res;
static uint8_t IN_SECTION(.ccm.bss) inserted_counter = 0;
if (sdio_check_inserted() && mounted) {
memset(fs_ptr, 0, sizeof(FATFS));
2020-08-17 22:10:04 +02:00
sdio_stop_clk();
inserted_counter = 0;
return false;
}
2020-08-17 22:10:04 +02:00
if (!sdio_check_inserted() && inserted_counter < 255)
inserted_counter++;
if (!sdio_check_inserted() && !mounted && inserted_counter > 4) {
inserted_counter = 0;
res = f_mount(fs_ptr, "0:/", 1);
if (res == FR_OK) {
led_set(1, 1);
return true;
2021-10-10 20:45:59 +02:00
} else {
return false;
2021-10-10 20:45:59 +02:00
}
}
return mounted;
}
static inline void handle_boot_status(void)
{
struct safety_memory_boot_status status;
int res;
res = safety_memory_get_boot_status(&status);
if (res != 0)
panic_mode();
if (status.reset_from_panic) {
/* We've seen a panic */
gui_root_menu_message_set("!! PANIC !!", "Check error me- mory!");
}
if (status.reboot_to_bootloader) {
status.reboot_to_bootloader = 0UL;
safety_memory_set_boot_status(&status);
led_set(0, 1);
led_set(1, 1);
gui_lcd_write_direct_blocking(0, "Updating...");
2021-04-06 20:55:41 +02:00
start_updater_ram_code();
}
if (status.code_updated) {
status.code_updated = 0x0UL;
safety_memory_set_boot_status(&status);
/* Display notification on GUI */
gui_root_menu_message_set("Firmware updated", "[Press Key]");
}
}
static inline void setup_system(void)
{
float tmp;
2020-02-02 20:24:44 +01:00
setup_nvic_priorities();
/* Init safety controller and safety memory */
safety_controller_init();
systick_setup();
oven_driver_init();
digio_setup_default_all();
led_setup();
loudspeaker_setup();
2020-11-29 19:04:10 +01:00
gui_init();
2020-02-24 18:50:09 +01:00
uart_gpio_config();
settings_setup();
/* Load the overtemperature limit from eeprom if available. Otherwise the default value will be used */
2021-10-10 20:45:59 +02:00
if (settings_load_overtemp_limit(&tmp) == SETT_LOAD_SUCCESS)
safety_controller_set_overtemp_limit(tmp);
handle_boot_status();
setup_shell_uart(&shell_uart);
adc_pt1000_setup_meas();
}
2020-05-11 21:51:32 +02:00
static void handle_shell_uart_input(shellmatta_handle_t shell_handle)
{
2020-05-11 21:51:32 +02:00
int uart_receive_status;
const char *uart_input;
size_t uart_input_len;
2020-05-11 21:51:32 +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);
}
int main(void)
2020-05-11 21:51:32 +02:00
{
bool cal_active;
float offset;
float sens;
int status;
2020-05-11 21:51:32 +02:00
bool sd_card_mounted = false;
bool sd_old;
shellmatta_handle_t shell_handle;
int menu_wait_request;
uint64_t quarter_sec_timestamp = 0ULL;
/* Setup all the peripherals and external componets like LCD, EEPROM etc. and the safety controller */
setup_system();
/* Try load the calibration. This will only succeed if there's an EEPROM */
status = settings_load_calibration(&sens, &offset);
2021-10-10 20:45:59 +02:00
if (!status)
adc_pt1000_set_resistance_calibration(offset, sens, true);
2020-02-24 18:50:09 +01:00
shell_handle = shell_init(write_shell_callback);
shell_print_motd(shell_handle);
main_cycle_counter_init();
2020-02-21 23:37:48 +01:00
while (1) {
if (systick_ticks_have_passed(quarter_sec_timestamp, 250)) {
led_set(1, 0);
sd_old = sd_card_mounted;
sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted);
if (sd_card_mounted && !sd_old) {
adc_pt1000_get_resistance_calibration(NULL, NULL, &cal_active);
if (!cal_active) {
status = settings_load_calibration(&sens, &offset);
2021-10-10 20:45:59 +02:00
if (!status)
adc_pt1000_set_resistance_calibration(offset, sens, true);
}
}
quarter_sec_timestamp = systick_get_global_tick();
}
2020-11-29 19:04:10 +01:00
menu_wait_request = gui_handle();
handle_shell_uart_input(shell_handle);
/* Execute current profile step, if a profile is active */
temp_profile_executer_handle();
safety_controller_handle();
2021-10-10 20:45:59 +02:00
if (oven_pid_get_status() == OVEN_PID_RUNNING)
oven_pid_handle();
2021-10-10 20:45:59 +02:00
2020-07-09 22:31:42 +02:00
oven_driver_apply_power_level();
safety_controller_report_timing(ERR_TIMING_MAIN_LOOP);
if (menu_wait_request)
2020-05-09 19:56:47 +02:00
__WFI();
2020-06-25 23:52:58 +02:00
else
__NOP();
main_cycle_counter_inc();
}
return 0;
}
2020-02-24 18:50:09 +01:00
void sdio_wait_ms(uint32_t ms)
{
systick_wait_ms(ms);
}
2020-09-27 23:23:44 +02:00
/**
* @brief Handles the TX of UART1 (Shellmatta)
*/
void DMA2_Stream7_IRQHandler(void)
2020-02-24 18:50:09 +01:00
{
uint32_t hisr = DMA2->HISR & (0x3F << 22);
2020-02-24 18:50:09 +01:00
DMA2->HIFCR = hisr;
if (hisr & DMA_HISR_TCIF7)
2020-02-24 18:50:09 +01:00
uart_tx_dma_complete_int_callback(&shell_uart);
}