/********************************MODBUS*************************************
Данный файл содержит объявления базовых функции и дефайны для реализации 
MODBUS.
Данный файл необходимо подключить в rs_message.h. После подключать rs_message.h
к основному проекту.
***************************************************************************/
#ifndef __PROJ_SETTINGS_H_
#define __PROJ_SETTINGS_H_

#include "stm32f4xx_hal.h"
//--------DEFINES FOR SETTING OF SETTINGS-----------
#define SETTINGS_FLASH_ADDRESS_SHIFT			(0x10000)
#define SETTINGS_FLASH_ADDRESS						(FLASH_BASE + SETTINGS_FLASH_ADDRESS_SHIFT)

#define EEPROM_BASE							
#define SETTINGS_EEPROM_ADDRESS_SHIFT
#define SETTINGS_EEPROM_ADDRESS						(EEPROM_BASE + SETTINGS_EEPROM_ADDRESS_SHIFT)				

#ifdef USE_EEPROM
#define SETTINGS_ADDRESS									SETTINGS_EEPROM_ADDRESS
#else // USE_EEPROM
#define SETTINGS_ADDRESS									(SETTINGS_FLASH_ADDRESS)
#endif // USE_EEPROM
//--------------------------------------------------
//------------DEFINES FOR PWM SETTING---------------
// settings defines
#define HZ_TIMER_CTRL	400
#define HZ_TIMER_PWM	1000


// TIM PWM1 SETTINGS
#define PWM_MASTER_TIM_NUMB				4
#define TIMER_PWM_TICKBASE				TIM_TickBase_1US
#define TIMER_PWM_AHB_FREQ				72

#define TIMER_PWM1_INSTANCE				TIM4
#define TIMER_PWM1_TIM_CHANNEL1		TIM_CHANNEL_1
#define TIMER_PWM1_TIM_CHANNEL2		TIM_CHANNEL_2
#define TIMER_PWM1_GPIOx					GPIOD
#define TIMER_PWM1_GPIO_PIN_X1		GPIO_PIN_12
#define TIMER_PWM1_GPIO_PIN_X2		GPIO_PIN_13



// TIM PWM2 SETTINGS
#define TIMER_PWM2_INSTANCE				TIM3
#define TIMER_PWM2_TIM_CHANNEL1		TIM_CHANNEL_3
#define TIMER_PWM2_TIM_CHANNEL2		TIM_CHANNEL_4
#define TIMER_PWM2_GPIOx					GPIOB
#define TIMER_PWM2_GPIO_PIN_X1		GPIO_PIN_0
#define TIMER_PWM2_GPIO_PIN_X2		GPIO_PIN_1



// TIM PWM3 SETTINGS
#define TIMER_PWM3_INSTANCE				TIM1
#define TIMER_PWM3_TIM_CHANNEL1		TIM_CHANNEL_1
#define TIMER_PWM3_TIM_CHANNEL2		TIM_CHANNEL_2
#define TIMER_PWM3_GPIOx					GPIOE
#define TIMER_PWM3_GPIO_PIN_X1		GPIO_PIN_9
#define TIMER_PWM3_GPIO_PIN_X2		GPIO_PIN_11

// TIM CTRL SETTINGS
#define TIMER_CTRL_TICKBASE				TIM_TickBase_1US
#define TIMER_CTRL_AHB_FREQ				72

// PWM SETTINGS
#define SIN_TABLE_ORIGIN 					sin_table
#define SIN_TABLE_SIZE_MAX 				1000
//--------------------------------------------------

//----------DEFINES FOR MODBUS SETTING--------------
#define MODBUS_UART_NUMB 					3			// number of used uart
#define MODBUS_SPEED							115200
#define MODBUS_GPIOX							GPIOB
#define MODBUS_GPIO_PIN_RX				GPIO_PIN_11
#define MODBUS_GPIO_PIN_TX				GPIO_PIN_10
/* accord to this define sets define USED_MB_UART = USARTx */
#define MODBUS_TIM_NUMB 					7			// number of used uart
#define MODBUS_TIM_AHB_FREQ				72
/* accord to this define sets define USED_MB_TIM = TIMx */

/* defines for modbus behaviour */
#define MODBUS_DEVICE_ID 					1			// number of used uart
#define MODBUS_MAX_TIMEOUT 				5000 	// is ms
// custom define for size of receive message 
//--------------------------------------------------

typedef struct
{
	// ctrl periph settings 
	uint64_t TIM_CTRL_TICKBASE;
	uint64_t TIM_CTRL_AHB_FREQ;
	// pwm peripth settings
	uint64_t TIM_PWM_TICKBASE;
	uint64_t TIM_PWM_AHB_FREQ;
	
//	uint64_t TIM_PWM1_INSTANCE;
	uint64_t TIM_PWM1_TIM_CHANNEL1;
	uint64_t TIM_PWM1_TIM_CHANNEL2;
	uint64_t TIM_PWM1_GPIOx;
	uint64_t TIM_PWM1_GPIO_PIN_X1;
	uint64_t TIM_PWM1_GPIO_PIN_X2;
	
	uint64_t TIM_PWM2_INSTANCE;
	uint64_t TIM_PWM2_TIM_CHANNEL1;
	uint64_t TIM_PWM2_TIM_CHANNEL2;
	uint64_t TIM_PWM2_GPIOx;
	uint64_t TIM_PWM2_GPIO_PIN_X1;
	uint64_t TIM_PWM2_GPIO_PIN_X2;
	
	uint64_t TIM_PWM3_INSTANCE;
	uint64_t TIM_PWM3_TIM_CHANNEL1;
	uint64_t TIM_PWM3_TIM_CHANNEL2;
	uint64_t TIM_PWM3_GPIOx;
	uint64_t TIM_PWM3_GPIO_PIN_X1;
	uint64_t TIM_PWM3_GPIO_PIN_X2;
	
	
	// modbus peripth settings
	uint64_t MB_DEVICE_ID;
	uint64_t MB_SPEED;
	uint64_t MB_GPIOX;
	uint64_t MB_GPIO_PIN_RX;
	uint64_t MB_GPIO_PIN_TX;
	uint64_t MB_MAX_TIMEOUT;	
	uint64_t MB_TIM_AHB_FREQ;
	
//	uint32_t MB_UART_NUMB;
//	uint32_t MB_TIM_NUMB;
}ProjectSettings_TypeDef;
extern ProjectSettings_TypeDef PROJSET;
	
//#define PROJSET_MEM		((ProjectSettings_TypeDef *)SETTINGS_ADDRESS)


//#define HAL_FLASH_GET_TYPEPROGRAM(_val_) 						(sizeof(PROJSET_MEM->MB_DEVICE_ID)/2 - 1)
#define HAL_FLASH_GET_TYPEPROGRAM(_val_) 						FLASH_TYPEPROGRAM_WORD
#define FLASH_WRITE_SETTING(_setting_, _val_)				HAL_FLASH_Program(HAL_FLASH_GET_TYPEPROGRAM(_setting_), (uint32_t)(&_setting_), (uint32_t)_val_);
#define STRUCT_WRITE_SETTING(_setting_, _val_)				(_setting_ = _val_)


void FillStructWithDefines(void);
void SetFlagUpdateSettingsInMem(void);
void UpdateSettingsInMem(void);
void WriteSettingsToMem(void);
void FillSettingsWithDefines(void);
void CheckSettingsInFLASH(void);
int CheckIsSettingsValid(ProjectSettings_TypeDef *set_struct);

#endif // __PROJ_SETTINGS_H_