// ---------------------------------------------------------------------------

// STK500 programmer: main module header file

// ---------------------------------------------------------------------------

#ifndef MAIN_H
#define MAIN_H

// ---------------------------------------------------------------------------

#include <iom8.h>
#include <intrinsics.h>
#include <stdbool.h>

// ----------------------------- Constants: ----------------------------------

//#define	F_CLK 11.0592    // clock frequency, MHz
//#define	F_CLK 14.7456    // clock frequency, MHz
#define	F_CLK 18.432    // clock frequency, MHz

#define	T_SYS 1000.0     // system tick, us

// clock frequency of original device, MHz. Do not change!
#define	F_CLK_STK 7.3728
#define	F_CLK_ISP 3.6864

// ---------------------------------------------------------------------------
// ------------------------------- Ports: ------------------------------------
// ---------------------------------------------------------------------------

// I - input
// O - outbut
// B - bidirectional

// L - low active level
// H - high active level
// X - undefined active level
// A - analog signal

// ------------------------------ Port B: ------------------------------------

#define NC_PB0 (1 << PB0) // IL - not used
#define CLK    (1 << PB1) // OH - 1 MHz CLK out (OC1A)
#define LED    (1 << PB2) // OH - LED out
#define NC_PB3 (1 << PB3) // IL - not used
#define NC_PB4 (1 << PB4) // IL - not used
#define NC_PB5 (1 << PB5) // IL - not used
#define NC_PB6 (1 << PB6) // IL - not used
#define NC_PB7 (1 << PB7) // IL - not used

// Direction:
#define I_DDRB  (CLK | LED)
// Pull-ups (In) or initial state (Out):
#define I_PORTB (NC_PB0 | NC_PB3 | NC_PB4 | NC_PB5 | NC_PB6 | NC_PB7)
// Macros:
#define PORT_LED_0   (PORTB &= ~LED)
#define PORT_LED_1   (PORTB |= LED)

// ------------------------------ Port C: ------------------------------------

//#define NC_PC0 (1 << PC0) // IL - not used
//#define MOSI   (1 << PC1) // BH - target MOSI
//#define ADC_IN (1 << PC2) // AI - ADC input (ADC2)
//#define RESET  (1 << PC3) // BL - target RESET
//#define SCK    (1 << PC4) // BH - target SCK
//#define MISO   (1 << PC5) // BH - target MISO
//#define NC_PC6 (1 << PC6) // IL - not used

//для моей разводки
#define NC_PC0 (1 << PC0) // IL - not used
#define MOSI   (1 << PC5) // BH - target MOSI
#define ADC_IN (1 << PC4) // AI - ADC input (ADC4)
#define RESET  (1 << PC3) // BL - target RESET
#define SCK    (1 << PC1) // BH - target SCK
#define MISO   (1 << PC2) // BH - target MISO
#define NC_PC6 (1 << PC6) // IL - not used


// Direction:
#define I_DDRC  (0)
// Pull-ups (In) or initial state (Out):
#define I_PORTC (NC_PC0 | NC_PC6)
// ADC multiplexer (ADC2):
//#define ADC_MUX (1 << MUX1)

#define ADC_MUX (1 << MUX2)         //для моей разводки(ADC4) 

// Macros:
#define DIR_MOSI_IN   (DDRC &= ~MOSI)
#define DIR_MOSI_OUT  (DDRC |= MOSI)
#define PORT_MOSI_0   (PORTC &= ~MOSI)
#define PORT_MOSI_1   (PORTC |= MOSI)

#define DIR_RESET_IN  (DDRC &= ~RESET)
#define DIR_RESET_OUT (DDRC |= RESET)
#define PORT_RESET_0  (PORTC &= ~RESET)
#define PORT_RESET_1  (PORTC |= RESET)

#define DIR_SCK_IN    (DDRC &= ~SCK)
#define DIR_SCK_OUT   (DDRC |= SCK)
#define PORT_SCK_0    (PORTC &= ~SCK)
#define PORT_SCK_1    (PORTC |= SCK)

#define DIR_MISO_IN   (DDRC &= ~MISO)
#define DIR_MISO_OUT  (DDRC |= MISO)
#define PORT_MISO_0   (PORTC &= ~MISO)
#define PORT_MISO_1   (PORTC |= MISO)
#define PIN_MISO      (PINC & MISO)

// ------------------------------ Port D: ------------------------------------

#define RXD    (1 << PD0) // IL - UART RXD
#define TXD    (1 << PD1) // OL - UART TXD
#define NC_PD2 (1 << PD2) // IL - not used
#define NC_PD3 (1 << PD3) // IL - not used
#define NC_PD4 (1 << PD4) // IL - not used
#define NC_PD5 (1 << PD5) // IL - not used
#define NC_PD6 (1 << PD6) // IL - not used
#define NC_PD7 (1 << PD7) // IL - not used

// Direction:
#define I_DDRD  (TXD)
// Pull-ups (In) or initial state (Out):
#define I_PORTD (RXD | TXD | NC_PD2 | NC_PD3 | NC_PD4 | NC_PD5 | NC_PD6 | NC_PD7)

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

// ------------------------------ Macros: ------------------------------------

#define LO(x) (x & 0xFF)
#define HI(x) ((x >> 8) & 0xFF)
#define WORD(lo, hi) (((unsigned int)hi << 8) | lo)
#define Delay_us(x) __delay_cycles((int)(x * F_CLK + 0.5))

// ------------------------ Function prototypes: -----------------------------

void Main_Wdt_Reset(bool t); // watchdog reset
void Delay_ms(int d); // delay for mS range

// ---------------------------------------------------------------------------

#endif

// ---------------------------------------------------------------------------
