Compare commits
35 Commits
cfaffc7d50
...
04389b1df6
Author | SHA1 | Date | |
---|---|---|---|
04389b1df6 | |||
90da880386 | |||
bd82f55966 | |||
3d48bfb6a6 | |||
ea3d0121cc | |||
e2c2be6e30 | |||
df82f14206 | |||
e237776810 | |||
88062dc8e4 | |||
67899c8f02 | |||
fe51c80248 | |||
119aa1b0ff | |||
415979e211 | |||
122a16ad4e | |||
3834bd404c | |||
eb3b0eb459 | |||
7fca0fc31d | |||
5017bf7003 | |||
455c3dae68 | |||
f119433814 | |||
18bb0ae1af | |||
dcf10d39c7 | |||
0ef58a7d0d | |||
1acce563c1 | |||
9311d0b515 | |||
0bc341c0aa | |||
4487f854cd | |||
49835d9213 | |||
5948ac2897 | |||
55b664b58f | |||
fcbd1ae05e | |||
6700b0ea81 | |||
6965882435 | |||
c35c80ce24 | |||
8ffc5c11e0 |
@ -39,8 +39,10 @@ CFILES += digio.c
|
||||
CFILES += stm-periph/unique-id.c
|
||||
CFILES += calibration.c
|
||||
CFILES += temp-converter.c
|
||||
CFILES += rotary-encoder.c
|
||||
CFILES += rotary-encoder.c button.c
|
||||
CFILES += stack-check.c
|
||||
CFILES += ui/lcd.c ui/menu.c
|
||||
#CFILES += onewire-temp-sensors.c
|
||||
CFILES += fatfs/diskio.c fatfs/ff.c fatfs/ffsystem.c fatfs/ffunicode.c fatfs/shimatta_sdio_driver/shimatta_sdio.c
|
||||
CFILES += pid-controller.c oven-driver.c
|
||||
CFILES += settings/settings.c settings/settings-sd-card.c
|
||||
@ -89,7 +91,7 @@ $(ASOBJ):
|
||||
@echo [AS] $@
|
||||
$(eval OUTPATH=$(dir $@))
|
||||
@mkdir -p $(OUTPATH)
|
||||
$(QUIET)$(CC) $(CFLAGS) -MMD -MT $@ $(INCLUDEPATH) $(DEFINES) -o $@ $(@:$(OBJDIR)/%.S.o=%.S)
|
||||
$(QUIET)$(CC) $(CFLAGS) -MMD -MT $@ $(INCLUDEPATH) $(DEFINES) -o $@ $(@:$(OBJDIR)/%.S.o=%.S)
|
||||
|
||||
|
||||
.PHONY: qtproject clean mrproper objcopy disassemble program
|
||||
@ -107,7 +109,9 @@ mrproper: clean
|
||||
|
||||
clean:
|
||||
@echo "Cleaning up derived files..."
|
||||
$(QUIET)rm -f $(target).elf $(target).bin $(target).hex $(OBJ) $(ASOBJ) $(mapfile).map $(CFILES:%.c=$(OBJDIR)/%.c.d) $(ASFILES:%.S=$(OBJDIR)/%.S.d)
|
||||
$(QUIET)rm -f $(target).elf $(target).bin $(target).hex $(OBJ) $(ASOBJ) $(mapfile).map $(CFILES:%.c=$(OBJDIR)/%.c.d) $(ASFILES:%.S=$(OBJDIR)/%.S.d)
|
||||
$(QUIET)rm -rf $(OBJDIR)/*
|
||||
|
||||
qtproject:
|
||||
echo -e "TEMPLATE = app\nCONFIG -= console app_bundle qt" > $(target).pro
|
||||
echo -e "SOURCES += $(CFILES) $(ASFILES)" >> $(target).pro
|
||||
|
97
stm-firmware/button.c
Normal file
97
stm-firmware/button.c
Normal file
@ -0,0 +1,97 @@
|
||||
/* 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,
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <stm32/stm32f4xx.h>
|
||||
#include <reflow-controller/button.h>
|
||||
#include <stm-periph/stm32-gpio-macros.h>
|
||||
#include <stm-periph/clock-enable-manager.h>
|
||||
#include <stdint.h>
|
||||
#include <cmsis/core_cm4.h>
|
||||
#include <reflow-controller/systick.h>
|
||||
|
||||
static volatile uint64_t to_active_timestamp;
|
||||
static volatile enum button_state int_state;
|
||||
|
||||
void button_init()
|
||||
{
|
||||
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(BUTTON_RCC_MASK));
|
||||
rcc_manager_enable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(RCC_APB2ENR_SYSCFGEN));
|
||||
BUTTON_PORT->MODER &= MODER_DELETE(BUTTON_PIN);
|
||||
BUTTON_PORT->PUPDR &= PUPDR_DELETE(BUTTON_PIN);
|
||||
BUTTON_PORT->PUPDR |= PULLUP(BUTTON_PIN);
|
||||
|
||||
to_active_timestamp = 0ULL;
|
||||
int_state = BUTTON_IDLE;
|
||||
|
||||
SYSCFG->EXTICR[1] |= 0x3;
|
||||
EXTI->IMR |= (1U<<4);
|
||||
EXTI->RTSR |= (1U<<4);
|
||||
EXTI->FTSR |= (1U<<4);
|
||||
NVIC_EnableIRQ(EXTI4_IRQn);
|
||||
}
|
||||
|
||||
enum button_state button_read_event()
|
||||
{
|
||||
uint64_t time_delta;
|
||||
enum button_state temp_state;
|
||||
|
||||
if (BUTTON_PORT->IDR & (1U<<BUTTON_PIN)) {
|
||||
temp_state = int_state;
|
||||
int_state = BUTTON_IDLE;
|
||||
return temp_state;
|
||||
} else {
|
||||
time_delta = systick_get_global_tick() - to_active_timestamp;
|
||||
if (time_delta >= BUTTON_LONG_ON_TIME_MS)
|
||||
return BUTTON_LONG;
|
||||
else if (time_delta >= BUTTON_SHORT_ON_TIME_MS)
|
||||
return BUTTON_SHORT;
|
||||
else
|
||||
return BUTTON_IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void button_deinit()
|
||||
{
|
||||
BUTTON_PORT->MODER &= MODER_DELETE(BUTTON_PIN);
|
||||
BUTTON_PORT->PUPDR &= PUPDR_DELETE(BUTTON_PIN);
|
||||
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(BUTTON_RCC_MASK));
|
||||
EXTI->IMR &= ~(1U<<4);
|
||||
rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(RCC_APB2ENR_SYSCFGEN));
|
||||
}
|
||||
|
||||
void EXTI4_IRQHandler(void)
|
||||
{
|
||||
uint64_t time_delta;
|
||||
|
||||
/* Clear interrupts */
|
||||
EXTI->PR = EXTI->PR;
|
||||
__DSB();
|
||||
|
||||
if (BUTTON_PORT->IDR & (1U<<BUTTON_PIN)) {
|
||||
time_delta = systick_get_global_tick() - to_active_timestamp;
|
||||
if (time_delta >= BUTTON_SHORT_ON_TIME_MS && time_delta < BUTTON_LONG_ON_TIME_MS) {
|
||||
int_state = BUTTON_SHORT_RELEASED;
|
||||
} else if (time_delta >= BUTTON_LONG_ON_TIME_MS) {
|
||||
int_state = BUTTON_LONG_RELEASED;
|
||||
}
|
||||
} else {
|
||||
to_active_timestamp = systick_get_global_tick();
|
||||
}
|
||||
}
|
40
stm-firmware/include/reflow-controller/button.h
Normal file
40
stm-firmware/include/reflow-controller/button.h
Normal file
@ -0,0 +1,40 @@
|
||||
/* 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,
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __BUTTON_H__
|
||||
#define __BUTTON_H__
|
||||
|
||||
#define BUTTON_PORT GPIOD
|
||||
#define BUTTON_RCC_MASK RCC_AHB1ENR_GPIODEN
|
||||
#define BUTTON_PIN 4
|
||||
|
||||
#define BUTTON_SHORT_ON_TIME_MS 50UL
|
||||
#define BUTTON_LONG_ON_TIME_MS 400UL
|
||||
|
||||
enum button_state {BUTTON_IDLE = 0, BUTTON_SHORT_RELEASED, BUTTON_LONG_RELEASED, BUTTON_SHORT, BUTTON_LONG};
|
||||
|
||||
void button_init();
|
||||
|
||||
enum button_state button_read_event();
|
||||
|
||||
void button_deinit();
|
||||
|
||||
|
||||
#endif /* __BUTTON_H__ */
|
@ -26,6 +26,7 @@
|
||||
#define __SYSTICK_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief Reload value for the systick timer.
|
||||
@ -65,4 +66,6 @@ void systick_wait_ms(uint32_t ms);
|
||||
|
||||
uint64_t systick_get_global_tick();
|
||||
|
||||
bool systick_ticks_have_passed(uint64_t start_timestamp, uint64_t ticks);
|
||||
|
||||
#endif /* __SYSTICK_H__ */
|
||||
|
44
stm-firmware/include/reflow-controller/ui/lcd.h
Normal file
44
stm-firmware/include/reflow-controller/ui/lcd.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* 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,
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __LCD_H__
|
||||
#define __LCD_H__
|
||||
|
||||
#define LCD_DPORT (GPIOD)
|
||||
#define LCD_RCC_MASK RCC_AHB1ENR_GPIODEN
|
||||
#define LCD_DATA_BIT_OFFSET (8)
|
||||
#define LCD_RS (6)
|
||||
#define LCD_E (7)
|
||||
|
||||
#define LCD_DATA_MASK (0xFU << LCD_DATA_BIT_OFFSET)
|
||||
#define LCD_RS_MASK (1U << LCD_RS)
|
||||
#define LCD_E_MASK (1U << LCD_E)
|
||||
|
||||
enum lcd_fsm_ret {LCD_FSM_NOP, LCD_FSM_CALL_AGAIN, LCD_FSM_WAIT_CALL};
|
||||
|
||||
void lcd_init(void);
|
||||
|
||||
void lcd_string(const char *data);
|
||||
|
||||
void lcd_home(void);
|
||||
|
||||
enum lcd_fsm_ret lcd_fsm_write_buffer(const char (*display_buffer)[21]);
|
||||
|
||||
#endif /* __LCD_H__ */
|
24
stm-firmware/include/reflow-controller/ui/menu.h
Normal file
24
stm-firmware/include/reflow-controller/ui/menu.h
Normal file
@ -0,0 +1,24 @@
|
||||
/* 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,
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __MENU_H__
|
||||
#define __MENU_H__
|
||||
|
||||
#endif /* __MENU_H__ */
|
@ -21,13 +21,14 @@
|
||||
#ifndef __STM32GPIOMACROS_H__
|
||||
#define __STM32GPIOMACROS_H__
|
||||
|
||||
#define MODER_DELETE(pin) ~(0x3U << (pin * 2))
|
||||
#define OUTPUT(pin) (0x01U << (pin * 2))
|
||||
#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 ANALOG(pin) (0x03 << (pin * 2))
|
||||
#define MODER_DELETE(pin) ~(0x3U << ((pin) * 2))
|
||||
#define PUPDR_DELETE(pin) ~(0x3U << ((pin) * 2))
|
||||
#define OUTPUT(pin) (0x01U << ((pin) * 2))
|
||||
#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 ANALOG(pin) (0x03 << ((pin) * 2))
|
||||
#define OTYP_OPENDRAIN(pin) (0x1U << (pin))
|
||||
|
||||
#define BITMASK_TO_BITNO(x) (x&0x1?0:x&0x2?1:x&0x4?2:x&0x8?3: \
|
||||
|
@ -24,7 +24,9 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
/* #include <arm_math.h> */
|
||||
#include <stm32/stm32f4xx.h>
|
||||
#include <cmsis/core_cm4.h>
|
||||
@ -33,6 +35,7 @@
|
||||
#include <reflow-controller/adc-meas.h>
|
||||
#include <reflow-controller/shell.h>
|
||||
#include <reflow-controller/pid-controller.h>
|
||||
#include <reflow-controller/ui/lcd.h>
|
||||
#include <reflow-controller/digio.h>
|
||||
#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h"
|
||||
#include <reflow-controller/temp-converter.h>
|
||||
@ -42,6 +45,7 @@
|
||||
#include <stm-periph/uart.h>
|
||||
#include <reflow-controller/shell-uart-config.h>
|
||||
#include <helper-macros/helper-macros.h>
|
||||
#include <reflow-controller/button.h>
|
||||
#include <fatfs/ff.h>
|
||||
|
||||
static void setup_nvic_priorities()
|
||||
@ -130,9 +134,11 @@ static bool mount_sd_card_if_avail(bool mounted)
|
||||
}
|
||||
|
||||
const char *oven_controller_hello_world = "Hello world :)\n";
|
||||
|
||||
static volatile enum button_state button;
|
||||
static volatile uint32_t main_loops_per_ms;
|
||||
int main()
|
||||
{
|
||||
char disp[4][21] = {0};
|
||||
bool sd_card_mounted = false;
|
||||
FIL test_file;
|
||||
const char *uart_input;
|
||||
@ -140,8 +146,12 @@ int main()
|
||||
shellmatta_handle_t shell_handle;
|
||||
int uart_receive_status;
|
||||
|
||||
uint32_t main_loop_cnt = 0UL;
|
||||
uint64_t ms_stamp = 0ULL;
|
||||
|
||||
static struct pid_controller pid;
|
||||
uint64_t pid_timestamp = 0;
|
||||
uint64_t pid_timestamp = 0ULL;
|
||||
uint64_t display_timestamp = 0ULL;
|
||||
|
||||
setup_nvic_priorities();
|
||||
systick_setup();
|
||||
@ -152,10 +162,15 @@ int main()
|
||||
led_setup();
|
||||
loudspeaker_setup();
|
||||
rotary_encoder_setup();
|
||||
button_init();
|
||||
lcd_init();
|
||||
|
||||
uart_gpio_config();
|
||||
setup_sell_uart(&shell_uart);
|
||||
|
||||
lcd_home();
|
||||
lcd_string("Reflow");
|
||||
|
||||
shell_handle = shell_init(write_shell_callback);
|
||||
shell_print_motd(shell_handle);
|
||||
|
||||
@ -170,23 +185,47 @@ int main()
|
||||
pid_zero(&pid);
|
||||
|
||||
while (1) {
|
||||
|
||||
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;
|
||||
}
|
||||
sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted);
|
||||
|
||||
pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value);
|
||||
|
||||
if (pt1000_value_status >= 0) {
|
||||
(void)temp_converter_convert_resistance_to_temp(pt1000_value, (float *)¤t_temperature);
|
||||
if ((systick_get_global_tick() - pid_timestamp) >= 250) {
|
||||
if (systick_ticks_have_passed(pid_timestamp, 250)) {
|
||||
|
||||
pid_out = pid_sample(&pid, 100.0 - current_temperature);
|
||||
pid_timestamp = systick_get_global_tick();
|
||||
snprintf(&disp[2][0], 21, "Temp: %.1f C", current_temperature);
|
||||
led_set(1, !led_get(1));
|
||||
}
|
||||
}
|
||||
|
||||
rot = rotary_encoder_get_abs_val();
|
||||
//pid_timestamp = systick_get_global_tick();
|
||||
|
||||
button = button_read_event();
|
||||
|
||||
uart_receive_status = uart_receive_data_with_dma(&shell_uart, &uart_input, &uart_input_len);
|
||||
if (uart_receive_status >= 1)
|
||||
shell_handle_input(shell_handle, uart_input, uart_input_len);
|
||||
|
||||
main_loop_cnt++;
|
||||
|
||||
|
||||
rot = rotary_encoder_get_abs_val();
|
||||
snprintf(&disp[0][0], 21U, "Rot-Enc: %" PRIu32, rot);
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -91,7 +91,8 @@ static shellmatta_retCode_t shell_cmd_digio_set(const shellmatta_handle_t hand
|
||||
char buff[64];
|
||||
char *curr_token;
|
||||
|
||||
strncpy(buff, arguments, sizeof(buff));
|
||||
strncpy(buff, arguments, sizeof(buff)-1);
|
||||
buff[63] = '\0';
|
||||
|
||||
curr_token = strtok(buff, " ");
|
||||
curr_token = strtok(NULL, " ");
|
||||
|
@ -46,6 +46,26 @@ uint64_t systick_get_global_tick()
|
||||
return global_tick_ms;
|
||||
}
|
||||
|
||||
bool __attribute__((optimize("O3"))) systick_ticks_have_passed(uint64_t start_timestamp, uint64_t ticks)
|
||||
{
|
||||
uint64_t end_timestamp = start_timestamp + ticks;
|
||||
uint64_t current_timestamp = systick_get_global_tick();
|
||||
|
||||
/* wrap around expected */
|
||||
if (end_timestamp < start_timestamp) {
|
||||
/* Wrap around occured */
|
||||
if (current_timestamp < start_timestamp) {
|
||||
if (current_timestamp >= end_timestamp)
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (current_timestamp >= end_timestamp)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Interrupt Handler for SysTick
|
||||
*
|
||||
@ -53,7 +73,7 @@ uint64_t systick_get_global_tick()
|
||||
*
|
||||
* @warning For calling cyclic functions use separate timers/flags and don't spoil this function
|
||||
*/
|
||||
void SysTick_Handler()
|
||||
void __attribute__((optimize("O3"))) SysTick_Handler()
|
||||
{
|
||||
/* Increase tick counters */
|
||||
wait_tick_ms++;
|
||||
|
454
stm-firmware/ui/lcd.c
Normal file
454
stm-firmware/ui/lcd.c
Normal file
@ -0,0 +1,454 @@
|
||||
/* 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,
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/* Thanks to
|
||||
* https://www.mikrocontroller.net/articles/AVR-GCC-Tutorial/LCD-Ansteuerung
|
||||
* for the basic code construct
|
||||
*/
|
||||
|
||||
#include <stm32/stm32f4xx.h>
|
||||
#include <reflow-controller/ui/lcd.h>
|
||||
#include <reflow-controller/systick.h>
|
||||
#include <stm-periph/clock-enable-manager.h>
|
||||
#include <stm-periph/stm32-gpio-macros.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
static void lcd_port_clear(void)
|
||||
{
|
||||
LCD_DPORT->ODR &= ~(LCD_E_MASK);
|
||||
LCD_DPORT->ODR &= ~(LCD_DATA_MASK | LCD_RS_MASK | LCD_E_MASK);
|
||||
}
|
||||
|
||||
static void lcd_enable(void)
|
||||
{
|
||||
LCD_DPORT->ODR |= LCD_E_MASK;
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
//systick_wait_ms(10);
|
||||
LCD_DPORT->ODR &= ~LCD_E_MASK;
|
||||
//systick_wait_ms(10);
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
}
|
||||
|
||||
static void lcd_out(uint8_t data)
|
||||
{
|
||||
LCD_DPORT->ODR &= ~(LCD_DATA_MASK);
|
||||
LCD_DPORT->ODR |= (data << LCD_DATA_BIT_OFFSET) & LCD_DATA_MASK;
|
||||
}
|
||||
|
||||
static void lcd_data(uint8_t data)
|
||||
{
|
||||
lcd_port_clear();
|
||||
LCD_DPORT->ODR |= LCD_RS_MASK;
|
||||
lcd_out((data>>4) & 0xFU);
|
||||
lcd_enable();
|
||||
lcd_out(data & 0xFU);
|
||||
lcd_enable();
|
||||
systick_wait_ms(1);
|
||||
}
|
||||
|
||||
static void lcd_command(uint8_t data)
|
||||
{
|
||||
lcd_port_clear();
|
||||
lcd_out((data>>4) & 0xFU);
|
||||
lcd_enable();
|
||||
lcd_out(data & 0xFU);
|
||||
lcd_enable();
|
||||
systick_wait_ms(1);
|
||||
}
|
||||
|
||||
#define LCD_DDADR_LINE1 0x00
|
||||
#define LCD_DDADR_LINE2 0x40
|
||||
#define LCD_DDADR_LINE3 0x10
|
||||
#define LCD_DDADR_LINE4 0x50
|
||||
|
||||
// Clear Display -------------- 0b00000001
|
||||
#define LCD_CLEAR_DISPLAY 0x01
|
||||
|
||||
// Cursor Home ---------------- 0b0000001x
|
||||
#define LCD_CURSOR_HOME 0x02
|
||||
|
||||
// Set Entry Mode ------------- 0b000001xx
|
||||
#define LCD_SET_ENTRY 0x04
|
||||
|
||||
#define LCD_ENTRY_DECREASE 0x00
|
||||
#define LCD_ENTRY_INCREASE 0x02
|
||||
#define LCD_ENTRY_NOSHIFT 0x00
|
||||
#define LCD_ENTRY_SHIFT 0x01
|
||||
|
||||
// Set Display ---------------- 0b00001xxx
|
||||
#define LCD_SET_DISPLAY 0x08
|
||||
|
||||
#define LCD_DISPLAY_OFF 0x00
|
||||
#define LCD_DISPLAY_ON 0x04
|
||||
#define LCD_CURSOR_OFF 0x00
|
||||
#define LCD_CURSOR_ON 0x02
|
||||
#define LCD_BLINKING_OFF 0x00
|
||||
#define LCD_BLINKING_ON 0x01
|
||||
|
||||
// Set Shift ------------------ 0b0001xxxx
|
||||
#define LCD_SET_SHIFT 0x10
|
||||
|
||||
#define LCD_CURSOR_MOVE 0x00
|
||||
#define LCD_DISPLAY_SHIFT 0x08
|
||||
#define LCD_SHIFT_LEFT 0x00
|
||||
#define LCD_SHIFT_RIGHT 0x04
|
||||
|
||||
// Set Function --------------- 0b001xxxxx
|
||||
#define LCD_SET_FUNCTION 0x20
|
||||
|
||||
#define LCD_FUNCTION_4BIT 0x00
|
||||
#define LCD_FUNCTION_8BIT 0x10
|
||||
#define LCD_FUNCTION_1LINE 0x00
|
||||
#define LCD_FUNCTION_2LINE 0x08
|
||||
#define LCD_FUNCTION_5X7 0x00
|
||||
#define LCD_FUNCTION_5X10 0x04
|
||||
|
||||
#define LCD_SOFT_RESET 0x30
|
||||
|
||||
// Set CG RAM Address --------- 0b01xxxxxx (Character Generator RAM)
|
||||
#define LCD_SET_CGADR 0x40
|
||||
|
||||
#define LCD_GC_CHAR0 0
|
||||
#define LCD_GC_CHAR1 1
|
||||
#define LCD_GC_CHAR2 2
|
||||
#define LCD_GC_CHAR3 3
|
||||
#define LCD_GC_CHAR4 4
|
||||
#define LCD_GC_CHAR5 5
|
||||
#define LCD_GC_CHAR6 6
|
||||
#define LCD_GC_CHAR7 7
|
||||
|
||||
// Set DD RAM Address --------- 0b1xxxxxxx (Display Data RAM)
|
||||
#define LCD_SET_DDADR 0x80
|
||||
|
||||
static char shadow_display[4][21];
|
||||
|
||||
void lcd_clear(void)
|
||||
{
|
||||
lcd_command(LCD_CLEAR_DISPLAY);
|
||||
systick_wait_ms(3);
|
||||
}
|
||||
|
||||
void lcd_home(void)
|
||||
{
|
||||
lcd_command(LCD_CURSOR_HOME);
|
||||
systick_wait_ms(3);
|
||||
}
|
||||
|
||||
static uint8_t lcd_get_set_cursor_cmd(uint8_t x, uint8_t y)
|
||||
{
|
||||
uint8_t data;
|
||||
|
||||
switch (y) {
|
||||
case 0:
|
||||
/* First line */
|
||||
data = LCD_SET_DDADR + LCD_DDADR_LINE1 + x;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
/* Second Line */
|
||||
data = LCD_SET_DDADR + LCD_DDADR_LINE2 + x;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
/* Third line */
|
||||
data = LCD_SET_DDADR + LCD_DDADR_LINE3 + x;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
/* Fourth line */
|
||||
data = LCD_SET_DDADR + LCD_DDADR_LINE4 + x;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* In case of wrong line, assume first line */
|
||||
data = LCD_SET_DDADR + LCD_DDADR_LINE1;
|
||||
break;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
void lcd_setcursor(uint8_t x, uint8_t y)
|
||||
{
|
||||
uint8_t data;
|
||||
|
||||
data = lcd_get_set_cursor_cmd(x, y);
|
||||
lcd_command(data);
|
||||
}
|
||||
|
||||
void lcd_string(const char *data)
|
||||
{
|
||||
while (*data != '\0')
|
||||
lcd_data((uint8_t)*data++);
|
||||
}
|
||||
|
||||
static void lcd_port_init()
|
||||
{
|
||||
LCD_DPORT->MODER &= MODER_DELETE(LCD_E) & MODER_DELETE(LCD_RS) & MODER_DELETE(LCD_DATA_BIT_OFFSET) &
|
||||
MODER_DELETE(LCD_DATA_BIT_OFFSET + 1) & MODER_DELETE(LCD_DATA_BIT_OFFSET + 2) &
|
||||
MODER_DELETE(LCD_DATA_BIT_OFFSET + 3);
|
||||
|
||||
LCD_DPORT->MODER |= OUTPUT(LCD_E) | OUTPUT(LCD_RS) | OUTPUT(LCD_DATA_BIT_OFFSET) |
|
||||
OUTPUT(LCD_DATA_BIT_OFFSET + 1) | OUTPUT(LCD_DATA_BIT_OFFSET + 2) |
|
||||
OUTPUT(LCD_DATA_BIT_OFFSET + 3);
|
||||
}
|
||||
|
||||
static void lcd_clear_shadow_buff()
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < 21; j++) {
|
||||
shadow_display[i][j] = 0x0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lcd_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LCD_RCC_MASK));
|
||||
lcd_port_init();
|
||||
lcd_port_clear();
|
||||
|
||||
lcd_clear_shadow_buff();
|
||||
|
||||
systick_wait_ms(100);
|
||||
LCD_DPORT->ODR |= (0x3 << LCD_DATA_BIT_OFFSET);
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
lcd_enable();
|
||||
systick_wait_ms(5);
|
||||
}
|
||||
|
||||
// Set 4 Bit mode
|
||||
lcd_port_clear();
|
||||
LCD_DPORT->ODR |= (0x2<<LCD_DATA_BIT_OFFSET);
|
||||
lcd_enable();
|
||||
systick_wait_ms(2);
|
||||
|
||||
/* 4 Bit mode 2 lines */
|
||||
lcd_command( LCD_SET_FUNCTION |
|
||||
LCD_FUNCTION_4BIT |
|
||||
LCD_FUNCTION_2LINE |
|
||||
LCD_FUNCTION_5X7 );
|
||||
|
||||
/* Display on without cursor */
|
||||
lcd_command( LCD_SET_DISPLAY |
|
||||
LCD_DISPLAY_ON |
|
||||
LCD_CURSOR_OFF |
|
||||
LCD_BLINKING_OFF);
|
||||
|
||||
/* Cursor increment, no scroll */
|
||||
lcd_command( LCD_SET_ENTRY |
|
||||
LCD_ENTRY_INCREASE |
|
||||
LCD_ENTRY_NOSHIFT );
|
||||
|
||||
lcd_clear();
|
||||
}
|
||||
|
||||
void lcd_deinit()
|
||||
{
|
||||
lcd_port_clear();
|
||||
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LCD_RCC_MASK));
|
||||
}
|
||||
|
||||
static uint8_t compare_input_to_shadow(const char (*display_buffer)[21])
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
int i, row;
|
||||
|
||||
for (row = 0; row < 4; row++) {
|
||||
for (i = 0; i < 20; i++) {
|
||||
if (display_buffer[row][i] != shadow_display[row][i]) {
|
||||
ret |= (1U<<row);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void lcd_fsm_enable(bool en)
|
||||
{
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
|
||||
if (en)
|
||||
LCD_DPORT->ODR |= LCD_E_MASK;
|
||||
else
|
||||
LCD_DPORT->ODR &= ~LCD_E_MASK;
|
||||
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
__ASM("nop");
|
||||
}
|
||||
|
||||
static void lcd_fsm_write_command(bool high, uint8_t cmd)
|
||||
{
|
||||
lcd_port_clear();
|
||||
|
||||
if (high)
|
||||
lcd_out((cmd >> 4) & 0x0F);
|
||||
else
|
||||
lcd_out((cmd) & 0x0F);
|
||||
}
|
||||
|
||||
static void lcd_fsm_write_data(bool high, uint8_t data)
|
||||
{
|
||||
LCD_DPORT->ODR |= (1<<LCD_RS);
|
||||
if (high)
|
||||
lcd_out((data >> 4) & 0x0F);
|
||||
else
|
||||
lcd_out((data) & 0x0F);
|
||||
}
|
||||
|
||||
enum lcd_fsm_ret lcd_fsm_write_buffer(const char (*display_buffer)[21])
|
||||
{
|
||||
static bool idle = true;
|
||||
static uint8_t rows_to_handle = 0;
|
||||
static uint32_t state_cnt;
|
||||
static uint8_t row_cnt = 0;
|
||||
static uint32_t char_cnt;
|
||||
static uint32_t line_len;
|
||||
static uint64_t timestamp = 0ULL;
|
||||
enum lcd_fsm_ret ret;
|
||||
|
||||
ret = LCD_FSM_NOP;
|
||||
|
||||
if (idle) {
|
||||
rows_to_handle = compare_input_to_shadow(display_buffer);
|
||||
memcpy(shadow_display, display_buffer, sizeof(shadow_display));
|
||||
shadow_display[0][20] = 0;
|
||||
shadow_display[1][20] = 0;
|
||||
shadow_display[2][20] = 0;
|
||||
shadow_display[3][20] = 0;
|
||||
state_cnt = 0;
|
||||
row_cnt = 0;
|
||||
idle = false;
|
||||
}
|
||||
|
||||
if (rows_to_handle == 0) {
|
||||
idle = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((rows_to_handle & (1<<row_cnt))) {
|
||||
switch (state_cnt) {
|
||||
case 0:
|
||||
lcd_fsm_write_command(true, lcd_get_set_cursor_cmd(0, row_cnt));
|
||||
line_len = strlen(&shadow_display[row_cnt][0]);
|
||||
char_cnt = 0;
|
||||
lcd_fsm_enable(true);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
break;
|
||||
case 1:
|
||||
lcd_fsm_enable(false);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
break;
|
||||
case 2:
|
||||
lcd_fsm_write_command(false, lcd_get_set_cursor_cmd(0, row_cnt));
|
||||
lcd_fsm_enable(true);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
break;
|
||||
case 3:
|
||||
lcd_fsm_enable(false);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
timestamp = systick_get_global_tick();
|
||||
state_cnt++;
|
||||
break;
|
||||
case 4:
|
||||
if (!systick_ticks_have_passed(timestamp, 5)) {
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
} else {
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
lcd_fsm_write_data(true, (char_cnt >= line_len) ? ' ' : shadow_display[row_cnt][char_cnt]);
|
||||
lcd_fsm_enable(true);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
break;
|
||||
case 6:
|
||||
lcd_fsm_enable(false);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
break;
|
||||
case 7:
|
||||
lcd_fsm_write_data(false, (char_cnt >= line_len) ? ' ' : shadow_display[row_cnt][char_cnt]);
|
||||
lcd_fsm_enable(true);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
state_cnt++;
|
||||
break;
|
||||
case 8:
|
||||
lcd_fsm_enable(false);
|
||||
ret = LCD_FSM_WAIT_CALL;
|
||||
char_cnt++;
|
||||
if (char_cnt < 16) {
|
||||
state_cnt = 5;
|
||||
} else {
|
||||
state_cnt = 0;
|
||||
rows_to_handle &= (uint8_t)~(1U<<row_cnt);
|
||||
if (row_cnt < 3) {
|
||||
row_cnt++;
|
||||
} else {
|
||||
idle = true;
|
||||
row_cnt = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ret = LCD_FSM_NOP;
|
||||
idle = true;
|
||||
rows_to_handle = 0U;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (row_cnt < 3) {
|
||||
row_cnt++;
|
||||
ret = LCD_FSM_CALL_AGAIN;
|
||||
} else {
|
||||
row_cnt = 0;
|
||||
ret = LCD_FSM_NOP;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
21
stm-firmware/ui/menu.c
Normal file
21
stm-firmware/ui/menu.c
Normal file
@ -0,0 +1,21 @@
|
||||
/* 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,
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <reflow-controller/ui/menu.h>
|
Loading…
x
Reference in New Issue
Block a user