/* 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 __SAFETY_ADC_HWCFG_H__
#define __SAFETY_ADC_HWCFG_H__

/**
 * @file safety-adc-hwcfg.h
 * @brief Safety ADC configuration settings
 */

#include <stm32/stm32f4xx.h>

/**
 * @brief Peripheral to use as safety ADC.
 * @note This must be ADC1, because it is the only ADC with access to the internal temperature sensor
 */
#define SAFETY_ADC_ADC_PERIPHERAL ADC1

/**
 * @brief Clock enable mask in RCC->APB2ENR to enable the #SAFETY_ADC_ADC_PERIPHERAL
 */
#define SAFETY_ADC_ADC_RCC_MASK RCC_APB2ENR_ADC1EN

/**
 * @brief ADC channel number of the internal temperature sensor
 */
#define TEMP_CHANNEL_NUM (16)

/**
 * @brief ADC channel number of the internal reference voltage
 */
#define INT_REF_CHANNEL_NUM (17)

#define SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_CHANNEL_NUM (15)
#define SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_PIN (5)
#define SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_PORT GPIOC
#define SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_PORT_RCC_MASK RCC_AHB1ENR_GPIOCEN


/**
 * @brief Nominal value of the internal reference voltage.
 *
 * This is used to compare to the external reference voltage.
 */
#define SAFETY_ADC_INT_REF_MV 1210.0f

/**
 * @brief Nominal temperature in degrees celsius of the internal temperature sensor at which it reads
 * #SAFETY_ADC_TEMP_NOM_MV millivolts.
 *
 */
#define SAFETY_ADC_TEMP_NOM 25.0f

/**
 * @brief Nominal internal temperature sensor voltage at temperature #SAFETY_ADC_TEMP_NOM
 */
#define SAFETY_ADC_TEMP_NOM_MV 760.0f

/**
 * @brief Sensitivity of the internal temperature sensor in millivolts per Kelvin
 */
#define SAFETY_ADC_TEMP_MV_SLOPE 2.5f

/**
 * @brief Number of channels to handle for the SAFETY ADC.
 * @note The maximum amount of channels is limited to 16
 */
#define SAFETY_ADC_NUM_OF_CHANNELS 12

/**
 * @brief Channel numbers to sample with the SAFETY ADC
 *
 * - The first 4 values are the internal temperature sensor
 * - The next 4 values are the internal reference voltage
 *
 * The values are samples 4 times each to allow averaging them to get
 * more precise readings. Check the maximum value of #SAFETY_ADC_NUM_OF_CHANNELS
 * when adding more channels.
 * @warning Safety controller expects the current measurment list. If you change it, check @ref safety_controller_handle_safety_adc
 */
#define SAFETY_ADC_CHANNELS TEMP_CHANNEL_NUM, TEMP_CHANNEL_NUM, TEMP_CHANNEL_NUM, TEMP_CHANNEL_NUM, \
				INT_REF_CHANNEL_NUM, INT_REF_CHANNEL_NUM, INT_REF_CHANNEL_NUM, INT_REF_CHANNEL_NUM, \
				SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_CHANNEL_NUM, SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_CHANNEL_NUM, \
				SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_CHANNEL_NUM, SAFETY_ADC_SUPPLY_VOLTAGE_MONITOR_CHANNEL_NUM


/* Check the channel count of the safety ADC */
#if SAFETY_ADC_NUM_OF_CHANNELS > 16 || SAFETY_ADC_NUM_OF_CHANNELS < 0
#error "Invlaid count of channels for safety ADC"
#endif

#endif /* __SAFETY_ADC_HWCFG_H__ */
