/*
 * xp_write_xpwm_time.h
 *
 *  Created on: 03 апр. 2018 г.
 *      Author: stud
 */

#ifndef XP_WRITE_TIME_H_
#define XP_WRITE_TIME_H_


#include "word_structurs.h"



#define PWM_ERROR_LEVEL_INTERRUPT   0 // не определили где мы, ошибка!!!
#define PWM_LOW_LEVEL_INTERRUPT     1 // мы внизу пилы
#define PWM_HIGH_LEVEL_INTERRUPT    2 // мы вверху пилы


#define PWM_MODE_RELOAD_FORCE       0 // обновлять все значения
#define PWM_MODE_RELOAD_LEVEL_LOW   1 // обновлять значения для низа пилы, для saw_direct=1
#define PWM_MODE_RELOAD_LEVEL_HIGH  2 // обновлять значения для верха пилы, для saw_direct=0





#define PWM_KEY_NUMBER_A1_PLUS      0
#define PWM_KEY_NUMBER_A1_MINUS     1
#define PWM_KEY_NUMBER_B1_PLUS      2
#define PWM_KEY_NUMBER_B1_MINUS     3
#define PWM_KEY_NUMBER_C1_PLUS      4
#define PWM_KEY_NUMBER_C1_MINUS     5

#define PWM_KEY_NUMBER_A2_PLUS      6
#define PWM_KEY_NUMBER_A2_MINUS     7
#define PWM_KEY_NUMBER_B2_PLUS      8
#define PWM_KEY_NUMBER_B2_MINUS     9
#define PWM_KEY_NUMBER_C2_PLUS      10
#define PWM_KEY_NUMBER_C2_MINUS     11

#define PWM_KEY_NUMBER_BR1_PLUS     12
#define PWM_KEY_NUMBER_BR1_MINUS    13

#define PWM_KEY_NUMBER_BR2_PLUS     14
#define PWM_KEY_NUMBER_BR2_MINUS    15

//////////////////////////////////////////////////////////////////////
#define ENABLE_PWM_BREAK_ALL        0x0fff
#define ENABLE_PWM_BREAK_1          0xcfff
#define ENABLE_PWM_BREAK_2          0x3fff

#define ENABLE_PWM_1                0xffc0
#define ENABLE_PWM_2                0xf03f
#define ENABLE_PWM_1_2              0xf000

#define ENABLE_PWM_ALL              0x0000

//
#define DISABLE_PWM_BREAK_ALL        0xf000
#define DISABLE_PWM_BREAK_1          0x3000
#define DISABLE_PWM_BREAK_2          0xc000

#define DISABLE_PWM_1                0x003f
#define DISABLE_PWM_2                0x0fc0
#define DISABLE_PWM_1_2              0x0fff

#define DISABLE_PWM_ALL              0xffff

///
#define DISABLE_PWM_A1               0x0003
#define DISABLE_PWM_B1               0x000c
#define DISABLE_PWM_C1               0x0030

#define DISABLE_PWM_A2               0x00c0
#define DISABLE_PWM_B2               0x0300
#define DISABLE_PWM_C2               0x0c00
//
//////////////////////////////////////////////////////////////////////
/*
 * PWM - Start Stop
 * (15) - Soft start-stop m0de 1- soft mode enabled, 0 -disabled. Если выбран режим мЯгкой остановки, то  при подаче
 * сигнала стоп(0)-регистр = 0, выходы блокируютсЯ в момент перегиба пилы (после подачи сигнала остановки)., если нет- сразу.
 * Аналогично применЯютсЯ маски- при soft mode маски применЯютсЯ в момент перегиба.
 * ВАЖНО! ДлЯ моментальной остановки ШИМа данный регистр должен быть равен нулю.
 * (0) - 1 -start, 0 - stop
 */
#define PWM_START_SOFT  0x8001
#define PWM_START_HARD  0x0001

#define PWM_STOP_SOFT   0x8000
#define PWM_STOP_HARD   0x0000

/////////////////////////////////////




/////////////////////////////////////
typedef struct
{
	// Winding 1 times
	unsigned int Ta0_0;
	unsigned int Ta0_1;
	unsigned int Tb0_0;
	unsigned int Tb0_1;
	unsigned int Tc0_0;
	unsigned int Tc0_1;
	// Winding 2 times
	unsigned int Ta1_0;
	unsigned int Ta1_1;
	unsigned int Tb1_0;
	unsigned int Tb1_1;
	unsigned int Tc1_0;
	unsigned int Tc1_1;
	// Break transistors
	unsigned int Tbr0_0;
	unsigned int Tbr0_1;
	unsigned int Tbr1_0;
	unsigned int Tbr1_1;
	//Level transistors closed
	unsigned int Tclosed_0;
	unsigned int Tclosed_1;
    unsigned int Tclosed_high;
    unsigned int pwm_tics;
    unsigned int inited;
    unsigned int freq_pwm;
    unsigned int Tclosed_saw_direct_0;
    unsigned int Tclosed_saw_direct_1;
    unsigned int current_period;
    unsigned int where_interrupt;
    unsigned int mode_reload;
    unsigned int one_or_two_interrupts_run;
    WORD_UINT2BITS_STRUCT saw_direct;
	void (*write_1_2_winding_break_times)();
	void (*write_1_2_winding_break_times_split)();
} XPWM_TIME;

#define DEFAULT_XPWM_TIME {0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,0,0,0,0, {0}, \
                            xpwm_write_1_2_winding_break_times_16_lines, \
							xpwm_write_1_2_winding_break_times_16_lines_split_eages }

void xpwm_write_1_2_winding_break_times_16_lines();
void xpwm_write_1_2_winding_break_times_16_lines_split_eages(XPWM_TIME *p);
void xpwm_write_zero_winding_break_times_16_lines_split_eages();
void initXpwmTimeStructure(XPWM_TIME *p);

extern XPWM_TIME xpwm_time;

#define write_winding1_fase_a(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 0); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 1); WriteMemory(ADR_PWM_TIMING, T1);

#define write_winding1_fase_b(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 2); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 3); WriteMemory(ADR_PWM_TIMING, T1);

#define write_winding1_fase_c(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 4); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 5); WriteMemory(ADR_PWM_TIMING, T1);

#define write_winding2_fase_a(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 6); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 7); WriteMemory(ADR_PWM_TIMING, T1);

#define write_winding2_fase_b(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 8); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 9); WriteMemory(ADR_PWM_TIMING, T1);

#define write_winding2_fase_c(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 10); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 11); WriteMemory(ADR_PWM_TIMING, T1);
#define write_break_winding1(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 12); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 13); WriteMemory(ADR_PWM_TIMING, T1);

#define write_break_winding2(T0, T1) \
	WriteMemory(ADR_PWM_KEY_NUMBER, 14); WriteMemory(ADR_PWM_TIMING, T0);	\
	WriteMemory(ADR_PWM_KEY_NUMBER, 15); WriteMemory(ADR_PWM_TIMING, T1);


#endif /* XP_WRITE_TIME_H_ */
