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-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>
|
2020-04-20 21:12:40 +02:00
|
|
|
#include <reflow-controller/pid-controller.h>
|
2020-05-04 21:21:49 +02:00
|
|
|
#include <reflow-controller/ui/lcd.h>
|
2020-02-12 21:00:35 +01:00
|
|
|
#include <reflow-controller/digio.h>
|
2020-04-26 21:23:25 +02:00
|
|
|
#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h"
|
2020-04-20 21:12:40 +02:00
|
|
|
#include <reflow-controller/temp-converter.h>
|
2020-02-16 16:38:31 +01:00
|
|
|
#include <reflow-controller/rotary-encoder.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>
|
|
|
|
#include <helper-macros/helper-macros.h>
|
2020-04-27 20:18:45 +02:00
|
|
|
#include <reflow-controller/button.h>
|
2020-02-25 20:05:48 +01:00
|
|
|
#include <fatfs/ff.h>
|
2020-01-26 21:07:54 +01:00
|
|
|
|
2020-02-02 20:24:44 +01:00
|
|
|
static void setup_nvic_priorities()
|
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-02-02 20:24:44 +01:00
|
|
|
static float pt1000_value;
|
|
|
|
static volatile int pt1000_value_status;
|
2020-02-16 16:38:31 +01:00
|
|
|
static uint32_t rot;
|
2020-04-20 21:12:40 +02:00
|
|
|
static volatile float pid_out;
|
|
|
|
static volatile float current_temperature;
|
2020-02-24 18:50:09 +01:00
|
|
|
|
2020-04-26 21:23:25 +02:00
|
|
|
FATFS fs;
|
|
|
|
FATFS *fs_ptr = &fs;
|
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
static inline void uart_gpio_config()
|
|
|
|
{
|
|
|
|
#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];
|
|
|
|
static char shell_uart_rx_buff[32];
|
|
|
|
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_sell_uart(struct stm_uart *uart)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (res == FR_OK) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mounted;
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:05:48 +01:00
|
|
|
const char *oven_controller_hello_world = "Hello world :)\n";
|
2020-04-27 20:18:45 +02:00
|
|
|
static volatile enum button_state button;
|
2020-04-27 21:08:53 +02:00
|
|
|
static volatile uint32_t main_loops_per_ms;
|
2020-02-09 19:13:37 +01:00
|
|
|
int main()
|
|
|
|
{
|
2020-05-04 21:21:49 +02:00
|
|
|
char disp[4][21] = {0};
|
2020-04-26 21:23:25 +02:00
|
|
|
bool sd_card_mounted = false;
|
2020-02-25 20:05:48 +01:00
|
|
|
FIL test_file;
|
2020-02-09 19:13:37 +01:00
|
|
|
const char *uart_input;
|
|
|
|
size_t uart_input_len;
|
|
|
|
shellmatta_handle_t shell_handle;
|
2020-02-15 17:53:15 +01:00
|
|
|
int uart_receive_status;
|
2020-02-09 19:13:37 +01:00
|
|
|
|
2020-04-27 21:08:53 +02:00
|
|
|
uint32_t main_loop_cnt = 0UL;
|
|
|
|
uint64_t ms_stamp = 0ULL;
|
|
|
|
|
2020-04-20 21:12:40 +02:00
|
|
|
static struct pid_controller pid;
|
2020-05-04 21:21:49 +02:00
|
|
|
uint64_t pid_timestamp = 0ULL;
|
|
|
|
uint64_t display_timestamp = 0ULL;
|
2020-04-20 21:12:40 +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-01-26 21:07:54 +01:00
|
|
|
|
2020-02-10 22:38:24 +01:00
|
|
|
digio_setup_default_all();
|
|
|
|
led_setup();
|
|
|
|
loudspeaker_setup();
|
2020-02-16 16:38:31 +01:00
|
|
|
rotary_encoder_setup();
|
2020-04-27 20:18:45 +02:00
|
|
|
button_init();
|
2020-05-04 21:21:49 +02:00
|
|
|
lcd_init();
|
2020-02-10 22:38:24 +01:00
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
uart_gpio_config();
|
|
|
|
setup_sell_uart(&shell_uart);
|
2020-02-09 19:13:37 +01:00
|
|
|
|
2020-05-04 21:21:49 +02:00
|
|
|
lcd_home();
|
|
|
|
lcd_string("Reflow");
|
|
|
|
|
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-04-26 21:23:25 +02:00
|
|
|
if (f_mount(fs_ptr, "0:/", 1) == FR_OK) {
|
|
|
|
sd_card_mounted = true;
|
2020-02-25 20:05:48 +01:00
|
|
|
f_open(&test_file, "hello-world.txt", FA_OPEN_APPEND | FA_WRITE);
|
|
|
|
f_write(&test_file, oven_controller_hello_world, strlen(oven_controller_hello_world), NULL);
|
|
|
|
f_close(&test_file);
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:12:40 +02:00
|
|
|
pid_init(&pid, 0.1, 0.1, 4.0, 0.0, 100.0, 40.0);
|
|
|
|
pid_zero(&pid);
|
2020-02-25 20:05:48 +01:00
|
|
|
|
2020-02-21 23:37:48 +01:00
|
|
|
while (1) {
|
2020-04-27 21:08:53 +02:00
|
|
|
if (systick_get_global_tick() - ms_stamp >= 1) {
|
|
|
|
ms_stamp = systick_get_global_tick();
|
|
|
|
main_loops_per_ms = main_loop_cnt;
|
|
|
|
main_loop_cnt = 0UL;
|
|
|
|
}
|
2020-04-26 21:23:25 +02:00
|
|
|
sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted);
|
|
|
|
|
2020-02-02 20:24:44 +01:00
|
|
|
pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value);
|
2020-04-20 21:12:40 +02:00
|
|
|
|
|
|
|
if (pt1000_value_status >= 0) {
|
|
|
|
(void)temp_converter_convert_resistance_to_temp(pt1000_value, (float *)¤t_temperature);
|
2020-05-04 21:21:49 +02:00
|
|
|
if (systick_ticks_have_passed(pid_timestamp, 250)) {
|
|
|
|
|
2020-04-20 21:12:40 +02:00
|
|
|
pid_out = pid_sample(&pid, 100.0 - current_temperature);
|
|
|
|
pid_timestamp = systick_get_global_tick();
|
2020-05-04 21:21:49 +02:00
|
|
|
snprintf(&disp[2][0], 21, "Temp: %.1f C", current_temperature);
|
|
|
|
led_set(1, !led_get(1));
|
2020-04-20 21:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 21:21:49 +02:00
|
|
|
//pid_timestamp = systick_get_global_tick();
|
|
|
|
|
2020-04-27 20:18:45 +02:00
|
|
|
button = button_read_event();
|
2020-05-04 21:21:49 +02:00
|
|
|
|
2020-02-24 18:50:09 +01:00
|
|
|
uart_receive_status = uart_receive_data_with_dma(&shell_uart, &uart_input, &uart_input_len);
|
2020-02-15 17:53:15 +01:00
|
|
|
if (uart_receive_status >= 1)
|
2020-02-09 19:13:37 +01:00
|
|
|
shell_handle_input(shell_handle, uart_input, uart_input_len);
|
2020-04-27 21:08:53 +02:00
|
|
|
|
|
|
|
main_loop_cnt++;
|
2020-05-04 21:21:49 +02:00
|
|
|
|
2020-05-04 21:36:53 +02:00
|
|
|
|
|
|
|
rot = rotary_encoder_get_abs_val();
|
|
|
|
snprintf(&disp[0][0], 21U, "Rot-Enc: %u", rot);
|
2020-05-04 21:21:49 +02:00
|
|
|
strcpy(&disp[1][0], "Line 2");
|
|
|
|
strcpy(&disp[3][0], "Shimatta Reflow");
|
|
|
|
|
|
|
|
if (systick_ticks_have_passed(display_timestamp, 1)) {
|
|
|
|
display_timestamp = systick_get_global_tick();
|
|
|
|
lcd_fsm_write_buffer(disp);
|
|
|
|
}
|
|
|
|
__WFI();
|
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-02-24 18:50:09 +01:00
|
|
|
void DMA2_Stream7_IRQHandler()
|
|
|
|
{
|
|
|
|
uint32_t hisr = DMA2->HISR;
|
|
|
|
DMA2->HIFCR = hisr;
|
|
|
|
|
|
|
|
if (hisr & DMA_HISR_TCIF7) {
|
|
|
|
uart_tx_dma_complete_int_callback(&shell_uart);
|
|
|
|
}
|
|
|
|
}
|