Move push button and rotary encoder files to UI folder
This commit is contained in:
		
							
								
								
									
										111
									
								
								stm-firmware/ui/button.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								stm-firmware/ui/button.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,111 @@
 | 
			
		||||
/* 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/ui/button.h>
 | 
			
		||||
#include <stm-periph/stm32-gpio-macros.h>
 | 
			
		||||
#include <stm-periph/rcc-manager.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <helper-macros/helper-macros.h>
 | 
			
		||||
#include <cmsis/core_cm4.h>
 | 
			
		||||
#include <reflow-controller/systick.h>
 | 
			
		||||
 | 
			
		||||
static volatile uint64_t IN_SECTION(.ccm.bss) to_active_timestamp;
 | 
			
		||||
static volatile enum button_state IN_SECTION(.ccm.bss) int_state;
 | 
			
		||||
static volatile enum button_state IN_SECTION(.ccm.bss) override_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;
 | 
			
		||||
	override_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 (override_state != BUTTON_IDLE) {
 | 
			
		||||
		temp_state = override_state;
 | 
			
		||||
		override_state = BUTTON_IDLE;
 | 
			
		||||
		return 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();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void button_override_event(enum button_state state)
 | 
			
		||||
{
 | 
			
		||||
	override_state = state;
 | 
			
		||||
}
 | 
			
		||||
@@ -22,7 +22,7 @@
 | 
			
		||||
#include <reflow-controller/ui/gui-config.h>
 | 
			
		||||
#include <reflow-controller/ui/menu.h>
 | 
			
		||||
#include <reflow-controller/ui/lcd.h>
 | 
			
		||||
#include <reflow-controller/rotary-encoder.h>
 | 
			
		||||
#include <reflow-controller/ui/rotary-encoder.h>
 | 
			
		||||
#include <reflow-controller/systick.h>
 | 
			
		||||
#include <reflow-controller/adc-meas.h>
 | 
			
		||||
#include <reflow-controller/safety/safety-controller.h>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										102
									
								
								stm-firmware/ui/rotary-encoder.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								stm-firmware/ui/rotary-encoder.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,102 @@
 | 
			
		||||
/* 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/rotary-encoder.h>
 | 
			
		||||
#include <stm-periph/rcc-manager.h>
 | 
			
		||||
#include <stm-periph/stm32-gpio-macros.h>
 | 
			
		||||
#include <helper-macros/helper-macros.h>
 | 
			
		||||
 | 
			
		||||
static int16_t IN_SECTION(.ccm.bss) override_delta;
 | 
			
		||||
 | 
			
		||||
static inline void rotary_encoder_setup_pins(void)
 | 
			
		||||
{
 | 
			
		||||
	rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_RCC_MASK));
 | 
			
		||||
	ROTARY_ENCODER_PORT->MODER &= MODER_DELETE(ROTARY_ENCODER_PIN1) & MODER_DELETE(ROTARY_ENCODER_PIN2);
 | 
			
		||||
	ROTARY_ENCODER_PORT->MODER |= ALTFUNC(ROTARY_ENCODER_PIN1) | ALTFUNC(ROTARY_ENCODER_PIN2);
 | 
			
		||||
	SETAF(ROTARY_ENCODER_PORT, ROTARY_ENCODER_PIN1, ROTARY_ENCODER_PORT_ALTFUNC);
 | 
			
		||||
	SETAF(ROTARY_ENCODER_PORT, ROTARY_ENCODER_PIN2, ROTARY_ENCODER_PORT_ALTFUNC);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rotary_encoder_setup(void)
 | 
			
		||||
{
 | 
			
		||||
	rcc_manager_enable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_TIMER_RCC_MASK));
 | 
			
		||||
	rotary_encoder_setup_pins();
 | 
			
		||||
 | 
			
		||||
	ROTARY_ENCODER_TIMER->ARR = 0xFFFF;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CNT = 0;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CR2 = 0;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->SMCR = TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CCER = TIM_CCER_CC1P | TIM_CCER_CC2P;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->PSC = 0;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CR1 = TIM_CR1_CEN;
 | 
			
		||||
 | 
			
		||||
	override_delta = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t rotary_encoder_get_abs_val(void)
 | 
			
		||||
{
 | 
			
		||||
	return (uint32_t)ROTARY_ENCODER_TIMER->CNT;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32_t rotary_encoder_get_change_val(void)
 | 
			
		||||
{
 | 
			
		||||
	static uint32_t last_val = 0;
 | 
			
		||||
	uint32_t val;
 | 
			
		||||
	int32_t diff;
 | 
			
		||||
 | 
			
		||||
	val = rotary_encoder_get_abs_val();
 | 
			
		||||
 | 
			
		||||
	diff = val - last_val;
 | 
			
		||||
 | 
			
		||||
	if (val > last_val) {
 | 
			
		||||
		if (diff > (0xFFFF/2))
 | 
			
		||||
			diff = -(last_val + 0x10000-val);
 | 
			
		||||
	} else {
 | 
			
		||||
		if (ABS(diff) > (0xFFFF/2))
 | 
			
		||||
			diff = 0x10000 - last_val + val;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	last_val = val;
 | 
			
		||||
 | 
			
		||||
	diff += override_delta;
 | 
			
		||||
	override_delta = 0;
 | 
			
		||||
 | 
			
		||||
	return diff;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rotary_encoder_stop(void)
 | 
			
		||||
{
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CR1 = 0;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CR2 = 0;
 | 
			
		||||
	ROTARY_ENCODER_TIMER->ARR = 0;
 | 
			
		||||
	rcc_manager_disable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_TIMER_RCC_MASK));
 | 
			
		||||
	rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_RCC_MASK));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rotary_encoder_zero(void)
 | 
			
		||||
{
 | 
			
		||||
	ROTARY_ENCODER_TIMER->CNT = 0UL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rotary_encoder_override_delta(int16_t delta)
 | 
			
		||||
{
 | 
			
		||||
	override_delta = delta;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user