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/>.
|
|
|
|
*/
|
|
|
|
|
2021-10-11 19:45:13 +02:00
|
|
|
/**
|
|
|
|
* @addtogroup digio
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2020-02-12 21:00:35 +01:00
|
|
|
#include <reflow-controller/digio.h>
|
|
|
|
#include <stm32/stm32f4xx.h>
|
2020-12-01 21:00:23 +01:00
|
|
|
#include <stm-periph/rcc-manager.h>
|
2020-02-12 21:00:35 +01:00
|
|
|
#include <stm-periph/stm32-gpio-macros.h>
|
2020-02-10 22:38:24 +01:00
|
|
|
#include <helper-macros/helper-macros.h>
|
|
|
|
|
|
|
|
static const uint8_t digio_pins[] = {DIGIO_PINS};
|
|
|
|
static const uint8_t digio_default_io[] = {DIGIO_INOUT_DEFAULT};
|
|
|
|
static const uint8_t digio_default_altfunc[] = {DIGIO_ALTFUNC_DEFAULT};
|
2020-05-24 23:35:17 +02:00
|
|
|
static uint16_t loudspeaker_val;
|
2020-02-10 22:38:24 +01:00
|
|
|
|
|
|
|
static void digio_setup_pin_int(uint8_t bit_no, uint8_t in_out, uint8_t alt_func)
|
|
|
|
{
|
|
|
|
DIGIO_PORT->MODER &= MODER_DELETE(bit_no);
|
|
|
|
switch (in_out) {
|
|
|
|
case 2:
|
|
|
|
DIGIO_PORT->MODER |= ALTFUNC(bit_no);
|
|
|
|
SETAF(DIGIO_PORT, bit_no, alt_func);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
DIGIO_PORT->MODER |= OUTPUT(bit_no);
|
|
|
|
break;
|
|
|
|
case 0: /* expected fallthrough */
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-26 20:36:49 +01:00
|
|
|
void digio_init(void)
|
2020-02-10 22:38:24 +01:00
|
|
|
{
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(DIGIO_RCC_MASK));
|
2021-12-26 20:36:49 +01:00
|
|
|
digio_set_default_values();
|
2020-02-10 22:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void digio_setup_pin(uint8_t num, uint8_t in_out, uint8_t alt_func)
|
|
|
|
{
|
|
|
|
if (num >= COUNT_OF(digio_pins))
|
|
|
|
return;
|
|
|
|
digio_setup_pin_int(digio_pins[num], in_out, alt_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void digio_set(uint8_t num, int val)
|
|
|
|
{
|
2021-12-26 20:40:28 +01:00
|
|
|
uint8_t pin;
|
|
|
|
|
2020-02-10 22:38:24 +01:00
|
|
|
if (num >= COUNT_OF(digio_pins))
|
|
|
|
return;
|
2021-12-26 20:40:28 +01:00
|
|
|
|
|
|
|
pin = digio_pins[num];
|
|
|
|
|
|
|
|
/* Check if port is an output. If not, do noting. */
|
|
|
|
if ((DIGIO_PORT->MODER & (0x3<<pin)) != OUTPUT(pin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-10 22:38:24 +01:00
|
|
|
if (val)
|
2021-12-26 20:40:28 +01:00
|
|
|
DIGIO_PORT->ODR |= (1<<pin);
|
2020-02-10 22:38:24 +01:00
|
|
|
else
|
2021-12-26 20:40:28 +01:00
|
|
|
DIGIO_PORT->ODR &= ~(1<<pin);
|
2020-02-10 22:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int digio_get(uint8_t num)
|
|
|
|
{
|
|
|
|
if (num >= COUNT_OF(digio_pins))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((DIGIO_PORT->MODER & (0x3<<digio_pins[num])) == OUTPUT(digio_pins[num]))
|
|
|
|
return (DIGIO_PORT->ODR & (1<<digio_pins[num]) ? 1 : 0);
|
|
|
|
else
|
|
|
|
return (DIGIO_PORT->IDR & (1<<digio_pins[num]) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t led_pins[] = {LED_PINS};
|
|
|
|
|
2020-08-31 22:58:00 +02:00
|
|
|
void led_setup(void)
|
2020-02-10 22:38:24 +01:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LED_RCC_MASK));
|
|
|
|
for (i = 0; i < COUNT_OF(led_pins); i++) {
|
|
|
|
LED_PORT->MODER &= MODER_DELETE(led_pins[i]);
|
|
|
|
LED_PORT->MODER |= OUTPUT(led_pins[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void led_set(uint8_t num, int val)
|
|
|
|
{
|
|
|
|
if (num >= COUNT_OF(led_pins))
|
|
|
|
return;
|
|
|
|
if (val)
|
|
|
|
LED_PORT->ODR |= (1<<led_pins[num]);
|
|
|
|
else
|
|
|
|
LED_PORT->ODR &= ~(1<<led_pins[num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int led_get(uint8_t num)
|
|
|
|
{
|
|
|
|
if (num >= COUNT_OF(led_pins))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return ((LED_PORT->ODR & (1<<led_pins[num])) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:45:13 +02:00
|
|
|
/**
|
|
|
|
* @brief Initialize the timer for the beeper to generate the output frequency
|
|
|
|
*
|
|
|
|
* TIM7 is used as the frequency generating timer. If @ref LOUDSPEAKER_MULTIFREQ
|
|
|
|
* is 0, the timer is unused and this function does nothing.
|
|
|
|
*/
|
2020-05-24 23:35:17 +02:00
|
|
|
static void loudspeaker_freq_timer_init(void)
|
|
|
|
{
|
|
|
|
#if LOUDSPEAKER_MULTIFREQ
|
|
|
|
rcc_manager_enable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(RCC_APB1ENR_TIM7EN));
|
|
|
|
TIM7->CR1 = 0UL;
|
|
|
|
TIM7->CR2 = 0UL;
|
|
|
|
TIM7->PSC = 1000;
|
|
|
|
TIM7->DIER = TIM_DIER_UIE;
|
|
|
|
NVIC_EnableIRQ(TIM7_IRQn);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-08-31 22:58:00 +02:00
|
|
|
void loudspeaker_setup(void)
|
2020-02-10 22:38:24 +01:00
|
|
|
{
|
|
|
|
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LOUDSPEAKER_RCC_MASK));
|
|
|
|
|
|
|
|
LOUDSPEAKER_PORT->MODER &= MODER_DELETE(LOUDSPEAKER_PIN);
|
|
|
|
LOUDSPEAKER_PORT->MODER |= OUTPUT(LOUDSPEAKER_PIN);
|
|
|
|
|
2020-05-24 23:35:17 +02:00
|
|
|
loudspeaker_freq_timer_init();
|
|
|
|
loudspeaker_set(0U);
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:45:13 +02:00
|
|
|
/**
|
|
|
|
* @brief Start the beeper
|
|
|
|
* @param val frequency value of the speaker in 'Timer relaod values'
|
|
|
|
* @note If @ref LOUDSPEAKER_MULTIFREQ isn't set,
|
|
|
|
* the speaker output will be set to high and no frequency is generated.
|
|
|
|
* The value of @p val is ignored in this case
|
|
|
|
*/
|
2020-05-24 23:35:17 +02:00
|
|
|
static void loudspeaker_start_beep(uint16_t val)
|
|
|
|
{
|
|
|
|
#if LOUDSPEAKER_MULTIFREQ
|
2021-01-01 19:48:31 +01:00
|
|
|
TIM7->ARR = (val == 1 ? LOUDSPEAKER_MULTIFREQ_DEFAULT : val);
|
2020-05-24 23:35:17 +02:00
|
|
|
TIM7->CNT = 0UL;
|
|
|
|
TIM7->CR1 |= TIM_CR1_CEN;
|
|
|
|
#else
|
|
|
|
(void)val;
|
|
|
|
LOUDSPEAKER_PORT->ODR |= (1<<LOUDSPEAKER_PIN);
|
|
|
|
#endif
|
2020-02-10 22:38:24 +01:00
|
|
|
}
|
2020-05-24 23:35:17 +02:00
|
|
|
|
2021-10-11 19:45:13 +02:00
|
|
|
/**
|
|
|
|
* @brief Stop the beeping of the loudspeaker
|
|
|
|
*/
|
2020-05-24 23:35:17 +02:00
|
|
|
static void loudspeaker_stop_beep(void)
|
2020-02-10 22:38:24 +01:00
|
|
|
{
|
2020-05-24 23:35:17 +02:00
|
|
|
#if LOUDSPEAKER_MULTIFREQ
|
|
|
|
TIM7->CR1 &= ~TIM_CR1_CEN;
|
|
|
|
__DSB();
|
|
|
|
TIM7->SR = 0UL;
|
|
|
|
__DSB();
|
|
|
|
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
|
|
|
|
#else
|
|
|
|
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
|
|
|
|
#endif
|
2020-02-10 22:38:24 +01:00
|
|
|
}
|
2020-05-24 23:35:17 +02:00
|
|
|
|
|
|
|
void loudspeaker_set(uint16_t val)
|
|
|
|
{
|
|
|
|
loudspeaker_val = val;
|
|
|
|
|
2021-10-10 20:40:40 +02:00
|
|
|
if (!val)
|
2020-05-24 23:35:17 +02:00
|
|
|
loudspeaker_stop_beep();
|
2021-10-10 20:40:40 +02:00
|
|
|
else
|
2020-05-24 23:35:17 +02:00
|
|
|
loudspeaker_start_beep(val);
|
|
|
|
}
|
|
|
|
|
2020-08-31 22:58:00 +02:00
|
|
|
uint16_t loudspeaker_get(void)
|
2020-05-24 23:35:17 +02:00
|
|
|
{
|
|
|
|
return loudspeaker_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LOUDSPEAKER_MULTIFREQ
|
2021-10-11 19:45:13 +02:00
|
|
|
/**
|
|
|
|
* @brief Timer7 IRQ Handler
|
|
|
|
*
|
|
|
|
* This IRQ Handler is used by the loudspeaker to synthesize the output frequency.
|
|
|
|
* If @ref LOUDSPEAKER_MULTIFREQ is 0, TIM7 and this interrupt will not be used.
|
|
|
|
*/
|
2020-05-24 23:35:17 +02:00
|
|
|
void TIM7_IRQHandler(void)
|
2020-02-10 22:38:24 +01:00
|
|
|
{
|
2020-05-24 23:35:17 +02:00
|
|
|
TIM7->SR = 0UL;
|
|
|
|
__DSB();
|
|
|
|
LOUDSPEAKER_PORT->ODR ^= (1<<LOUDSPEAKER_PIN);
|
2020-02-10 22:38:24 +01:00
|
|
|
}
|
2020-05-24 23:35:17 +02:00
|
|
|
#endif
|
2021-10-11 19:45:13 +02:00
|
|
|
|
|
|
|
/** @} */
|
2021-12-26 20:36:49 +01:00
|
|
|
|
|
|
|
void digio_set_default_values()
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < COUNT_OF(digio_pins); i++) {
|
|
|
|
digio_setup_pin_int(digio_pins[i], digio_default_io[i], digio_default_altfunc[i]);
|
|
|
|
if (digio_default_io[i] == 1)
|
|
|
|
digio_set(i, 0);
|
|
|
|
}
|
|
|
|
}
|