/*
 * Copyright (C) 2005-2020 Darron Broad
 * All rights reserved.
 *
 * This file is part of Pickle Microchip PIC ICSP.
 *
 * Pickle Microchip PIC ICSP is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 *
 * Pickle Microchip PIC ICSP 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 Pickle Microchip PIC ICSP. If not, see http://www.gnu.org/licenses/
 */

#ifndef _PICKLE_H
#define _PICKLE_H

#ifdef DEBUG
#define DPRINT(...) printf(__VA_ARGS__)
#else
#define DPRINT(...)
#endif

#include <errno.h>
#include <search.h>

const char VERSION[] = "5.01";

// ピンの割り当て設定
// 基板Ver2.4以降から変更したので注意すること
#define PIN_CLOCK 26
#define PIN_DATA 5
#define PIN_VPP 13
#define PIN_VDD 12
#define VPPLOW HIGH
#define VPPHIGH LOW
#define GPIO_0to31SET_REG   *((volatile unsigned long *)GPIO_OUT_W1TS_REG)
#define GPIO_0to31CLR_REG   *((volatile unsigned long *)GPIO_OUT_W1TC_REG)

// コマンドを表す定数
// プログラムを消す
#define COMMAND_ERASE 0x8000u
// プロテクトを解除する
#define COMMAND_BLANK 0x8001u
// プログラムを書き込む
#define COMMAND_WRITE_PROGRAM 0x8010u
// ファイルの中身を16進数で表示する
#define COMMAND_VIEW_FILE 0x8011u
// プログラムを比較する
#define COMMAND_VERIFY_PROGRAM 0x8012u
// プログラムを読み込む
#define COMMAND_READ_PROGRAM 0x8020u
// IDとconfigulation dataを読み込む
#define COMMAND_READ_ID_AND_CONFIG 0x8021u
// configulation dataを読み込む
#define COMMAND_READ_CONFIG 0x8022u
// 処理が完了した時に送る定数
#define COMMAND_RET_DONE 0xC000u
// 処理が失敗したと判定した時に送る定数
#define COMMAND_RET_FAILED 0xF000u
// データの終わりを示す定数
#define COMMAND_DATA_END 0xffffu

#ifndef FALSE
#define FALSE (LOW)
#endif

#ifndef TRUE
#define TRUE (HIGH)
#endif

#define ZERO (0)

#define STRLEN (512)
#define STRMAX (STRLEN - 1)

#define BUFLEN (512)
#define BUFMAX (BUFLEN - 1)

#define UL_ON ("\033[4m")
#define UL_OFF ("\033[0m")

#define ARCH12BIT (0x00000FFF)
#define ARCH14BIT (0x00003FFF)
#define ARCH16BIT (0x0000FFFF)
#define ARCH24BIT (0x00FFFFFF)
#define ARCH32BIT (0xFFFFFFFF)

#define NOKEY (0) /* NO KEY IN OPERATION */
#define HVPKEY (1) /* HVP KEY IN OPERATION */
#define LVPKEY (2) /* LVP KEY IN OPERATION */

/* Config mask rules (where supported & implemented) */
#define CONFIGVER (1)   /* VERIFY CONFIG BITS                     */
#define CONFIGAND (2)   /* AND CONFIG BITS AFTER READ             */
#define CONFIGSET (4)   /* SET UNUSED CONFIG BITS BEFORE WRITE    */

struct pickle;

#ifdef RPI
#include "raspi.h"
#endif
#ifdef ALLWINNER
#include "allwinner.h"
#endif
#ifdef MCP23016
#include "mcp23016.h"
#endif
#ifdef MCP230XX
#include "mcp230xx.h"
#endif
#ifdef MCP23SXX
#include "mcp23sxx.h"
#endif
#ifdef PCF8574
#include "pcf8574.h"
#endif
#ifdef BITBANG
#include "gpio-bb.h"
#endif
#ifdef FTDI
#include "ftdi-bb.h"
#endif
#ifdef MCP2221
#include "mcp2221.h"
#endif
#ifdef CP2104
#include "cp2104-bb.h"
#endif

#if defined(STK500) || defined(PLOAD)
#include "serial_posix.h"
#include "rw.h"
#include "ip.h"
#include "stk500v2.h"
#include "stk500v2_listen.h"
#include "stk500v2_load.h"
#endif

#if defined(PTEST) || defined(PIO)
#include "icspio.h"
#endif

#include "util.h"
#include "io.h"

#include "pic.h"
#include "pic12.h"
#include "pic14.h"
#include "pic14n.h"
#include "pic16.h"
#include "pic16n.h"
#include "pic24.h"
#include "pic32.h"

#include "inhx32.h"

typedef unsigned int speed_t;

/* SESSION */
struct pickle {
	/* Message output stream */
	FILE *f;

	/* Command line options */
	char devicename[STRLEN];/* PICMicro device name     */
	uint32_t key;		/* MCHP LVP key             */
	uint32_t partition;	/* dsPIC/PIC24 partition    */

	/* Dot file settings */
	char dotfile[STRLEN];	/* configuration file name  */
	char device[STRLEN];	/* I/O device type          */
	char etc[STRLEN];	/* user PE directory        */
	char serial[STRLEN];	/* USB serial               */
	char iface[STRLEN];	/* I/O interface            */
	uint32_t addr;		/* I2C/SPI address          */
	uint32_t bitrules;	/* I/O bit rules            */
	uint32_t busy;		/* I/O busy cursor speed    */
	uint32_t sleep_low;	/* I/O clock low duration   */
	uint32_t sleep_high;	/* I/O clock high duration  */
	uint32_t sleep_algo;	/* I/O sleep algorithm      */
	uint32_t fwsleep;	/* ICSPIO bit time          */
	uint32_t debug;		/* default 0 (no debugging) */
	uint32_t config;	/* default VERIFY + DUMP    */
	uint32_t baudrate;	/* STK500v2 baud rate       */
	uint16_t vpp;		/* TX/!MCLR/VPP             */
	uint16_t pgc;		/* RTS/PGC CLOCK            */
	uint16_t pgdo;		/* DTR/PGD DATA_OUT         */
	uint16_t pgdi;		/* CTS/PGD DATA_IN          */
	uint16_t pgm;		/* PGM OUT                  */
	uint32_t error;		/* Error rules              */

	/* Device configuration */
	uint8_t clock_falling;	/* Clock falling edge for shifting in bits        */
	uint8_t msb_first;	/* Shift direction is MSB>LSB rather than LSB>MSB */

	/* Hardware operations */
	struct pic_ops *pic;

	/* I/O operations */
	struct io_ops *io;

	/* I/O open */
	uint8_t iot;
};

extern char buf[200];
extern unsigned char p_f;

/* Error rules */
#define BADINPUT (1)
#define EX_BADINPUT (1)

/* prototypes */
void myprintf(char*);

#endif /* !_PICKLE_H */
