/**********************************************************************/
/*                                                                    */
/* File name: Com.h                                                   */
/*                                                                    */
/* Since:     2002/12/03                                              */
/*                                                                    */
/* Version:   1.02                                                    */
/*                                                                    */
/* Author:    MONTAGNE Xavier [XM] {link xavier.montagne@wanadoo.fr}  */
/*                                                                    */
/* Purpose: Offer high level interface for PIC programming operations:*/
/*          parsing or creat an HEX file, updating the PIC structures */
/*          after parsing, reading or programming the PIC,...         */
/*                                                                    */
/* Distribution: This file is part of PP18.                           */
/*               PP18 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; either version 2, or (at your option)    */
/*               any later version.                                   */
/*                                                                    */
/*               PP18 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 PP18; see the file         */
/*               COPYING.txt. If not, write to the Free Software      */
/*               Foundation, 59 Temple Place - Suite 330,             */
/*               Boston, MA 02111-1307, USA.                          */
/*                                                                    */
/* History:                                                           */
/*      2002/12/03  [XM] Create this file                             */
/*                                                                    */
/**********************************************************************/

#if !defined(__COM_H__)
#define __COM_H__

/***********************************************************************
 * INCLUDES
 **********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "driver.h"

/***********************************************************************
 * DEFINES
 **********************************************************************/
#if !defined NULL
#define NULL 0
#endif
#if !defined _MAX_PATH
#define _MAX_PATH	255
#endif

#define END_OF_FILE_RECORD               0x01

/******* Return value for internal fonctions ***************************/ 
#define KO                               0x00
#define OK                               0x01

/***********************************************************************
 * TYPE & ENUM DEFINITIONS
 **********************************************************************/

/**
 * Status fields return by the parser to inform the GUI
 * from the requiered process
 */
typedef enum _Status_t {
    ALL_RIGHT = 0,
    FILE_ERROR,      /* File can not be open or not intel hex format */
    NOT_BLANK,       /* The chip is not blank - all data = 0xFFFF */
    BLANK,           /* The chip is blank - all data = 0xFFFF */
    PROGRAMMED,      /* The chip is properly programmed */
    BAD_PROGRAMMED,  /* The program process didn't succeed */
    LOCKED,          /* The chip is code protected */
    PIC_DETECTED,    /* The chip REV-DEV is correct */
    HARDWARE_ERROR,  /* The hardware doesn't work properly */
    INTERNAL_ERROR,  /* Internal error return by a function */
    ABORTED          /* Aborted by USER in ProgressBar window */
} Status_t;

/**
 * Correspond to the different fields included in high adress block
 *  of every chip. Describe the configuration of the chip in term of
 *  frequency, watch dog, burn out reset, ... 
 */
typedef struct SConfigWord {
	unsigned char szCodeProtected;
	unsigned char szWriteProtected;
	unsigned char szTableReadProtected;
	unsigned char szOscsen;
	unsigned char szFosc;
	unsigned char szBorv;
	unsigned char szBoren;
	unsigned char szPwrten;
	unsigned char szWdtps;
	unsigned char szWdten;
	unsigned char szCcp2mx;
	unsigned char szStvren;
 	unsigned char szLVP;
  	unsigned char szDebug;   
	unsigned char szBw;
	unsigned char szRev;
	unsigned short usDev;	/*WARNING => Must be here for alignement*/
} *pConfigWord_t, ConfigWord_t;

/**
 * This structure contains some statistic informations about 
 * the programming process. Usefull to check hardware performance. 
 */
typedef struct SStatistic {
	unsigned int	uiNbWord2BeProgrammed;
	unsigned int	uiNbWordProgrammed;
	unsigned int	uiTotalTry;     
} **pStatisticPtr_t, *pStatistic_t, Statistic_t;

/**
 * Main structure to be passed to the Parse function. This one 
 * contains the filename of the HEX file and is filled by the parser
 * according to the programming process. 
 */
typedef struct SHexFile {
        unsigned char*  puszName;       /* Filename of the HEX file */
        Status_t        SChipStatus;    /* Enumerate variable */
        unsigned int    uiMemorySize;   /* Size of chip memory in term of WORD */
        unsigned short* pusMemory;      /* Pointer to buffer[memory_size] */
        pConfigWord_t   pConfig;        /* Pointer to the config structure */
        unsigned short  usChecksum;     /* 16 bits for checksum computation */
        unsigned char   szIdValue[8];   /* ID location id 8 bytes long*/
} **pHexFilePrt_t, *pHexFile_t, HexFile_t;


/***********************************************************************
 * FUNCTION DEFINITIONS
 **********************************************************************/
#if defined(__cplusplus)
extern "C"
{
#endif

/***********************************************************************
 * Parse an INTEL HEX file to fill a memory buffer.
 * Verify checksum for every line of the file. 
 *
 * @param  FILE *fd           IN  HEX filename to write in
 * @param  u_short *DataBytes IN  Reference to the memory buffer 
 * @param  u_int uiAddressMax IN  Number of addresses parsed
 * @return void               
 **********************************************************************/ 
int HexParserMemory(FILE *fd, unsigned short *Data_Bytes, unsigned int *uiAddressMax);

/***********************************************************************
 * Build a INTEL HEX file from memory buffer.
 * Compute checksum for every line of the file. 
 *
 * @param  FILE *fd           IN  HEX filename to write in
 * @param  u_short *DataBytes IN  Reference to the memory buffer 
 * @param  u_int uiAddressMax IN  Address Max = buffer_size x 2
 * @return void               
 **********************************************************************/  
void HexUnParserMemory(FILE *fd, unsigned short *DataBytes, unsigned int uiAddressMax);

/***********************************************************************
 * Parse a INTEL HEX file to extract the config values.
 *
 * @param  FILE *fd             IN  HEX filename to write in
 * @param  pConfigWord_t config IN  Config structure reference
 * @return int                      OK if sucessfull
 *                                  KO if checksum error occurs                
 **********************************************************************/  
int HexParserConfig(FILE *fd, pConfigWord_t config);

/***********************************************************************
 * Build a INTEL HEX file from config structure.
 * Compute checksum for only one line. 
 *
 * @param  FILE *fd           IN  HEX filename to write in
 * @param  pConfigWord_t cfg  IN  Reference to the config structure
 * @return void               
 **********************************************************************/  
void HexUnParserConfig(FILE *fd, pConfigWord_t cfg);

/***********************************************************************
 * Build a INTEL HEX file from ID structure.
 * Compute checksum for only one line. 
 *
 * @param  FILE *fd            IN  HEX filename to write in
 * @param  pHexFile_t p_hfFile IN  HEX structure reference
 * @return void               
 **********************************************************************/   
void HexUnParserID(FILE *fd, pHexFile_t p_hfFile);

/***********************************************************************
 * Parse a INTEL HEX file to extract the ID values.
 *
 * @param  FILE *fd            IN  HEX filename to write in
 * @param  pHexFile_t p_hfFile IN  HEX structure reference
 * @return int                     OK if sucessfull
 *                                 KO if checksum error occurs                
 **********************************************************************/  
int HexParserID(FILE *fd, pHexFile_t p_hfFile);

/***********************************************************************
 * Switch on an IO pin of the parallel port.
 *
 * @param  PIN_LABEL name      IN  Signal name
 * @return void
 **********************************************************************/
void TestIO_ON(PIN_LABEL name);

/***********************************************************************
 * Switch off an IO pin of the parallel port.
 *
 * @param  PIN_LABEL name      IN  Signal name
 * @return void
 **********************************************************************/
void TestIO_OFF(PIN_LABEL name);

/***********************************************************************
 * Assign an IO pin of the parallel port as an ICSP IO.
 *
 * @param  PIN_LABEL name      IN  Signal name
 * @param  u_char ucMask       IN  Signal mask 
 * @param  u_char ucStatus     IN  Signal status 
 * @return void
 **********************************************************************/  
void Settings(PIN_LABEL name, unsigned char mask, unsigned char status);

/***********************************************************************
 * Get the mask and status of a signal.
 *
 * @param  PIN_LABEL name      IN  Signal name
 * @param  u_char *ucMask      IN  Signal mask 
 * @param  u_char *ucStatus    IN  Signal status 
 * @return void  
 **********************************************************************/ 
void ReadSettings(PIN_LABEL name, unsigned char *ucMask, unsigned char *ucStatus);

/***********************************************************************
 * Read Data From Pic signal from parallel port.
 *
 * @param  u_char *p_puszValue IN  Reference to the checksum computed
 * @return void  
 **********************************************************************/  
void ReadDataFromPic(unsigned char *pucValue);

/***********************************************************************
 * Extract the checksum info from every line of an INTEL HEX file.
 * Compare it with the value computed by the HEX parser.
 *
 * @param  FILE *fd            IN  file descriptor
 * @return unsigned char           OK if sucessfull
 *                                 KO if checksum error occurs
 **********************************************************************/
unsigned char CompareChecksum(FILE *fd);

/***********************************************************************
 * Read the next short from an INTEL HEX file line.
 * Swap the low and hight bytes of the short. 
 *
 * @param  FILE *fd            IN  file descriptor
 * @return u_short value           Data read from the file 
 **********************************************************************/ 
unsigned short GetNextShort(FILE *fd);

/***********************************************************************
 * Read the next short from an INTEL HEX file line.
 * Do not swap the low and hight bytes of the short. 
 *
 * @param  FILE *fd            IN  file descriptor
 * @return u_short value           Data read from the file 
 **********************************************************************/
unsigned short GetNextShortUnSwap(FILE *fd);

/***********************************************************************
 * Write the next short into an INTEL HEX file.
 * Swap the low and hight bytes of the short. 
 *
 * @param  FILE *fd            IN  file descriptor
 * @param  u_short value       IN  Data to write into the file  
 * @return void 
 **********************************************************************/ 
void SetNextShort(FILE *fd, unsigned short value);

/***********************************************************************
 * Write the next short into an INTEL HEX file.
 * Do not swap the low and hight bytes of the short.
 *
 * @param  FILE *fd            IN  file descriptor
 * @param  u_short value       IN  Data to write into the file  
 * @return void 
 **********************************************************************/  
void SetNextShortUnSwap(FILE *fd, unsigned short value);

/***********************************************************************
 * Read the next char from an INTEL HEX file line.
 *
 * @param  FILE *fd            IN  file descriptor
 * @return u_char value            Data read from the file 
 **********************************************************************/   
unsigned char GetNextByte(FILE *fd);

/***********************************************************************
 * Write the next char into an INTEL HEX file.
 *
 * @param  FILE *fd            IN  file descriptor
 * @param  u_char value        IN  Data to write into the file  
 * @return void 
 **********************************************************************/  
void SetNextByte(FILE *fd, unsigned char value);

/***********************************************************************
 * Extract 8 bits data from a file.
 * Convert data from dec to hex.
 *
 * @param  FILE *fd            IN  file descriptor
 * @return u_char value            Data read from the file 
 **********************************************************************/   
unsigned char GetNextChar(FILE *fd);

/***********************************************************************
 * Write 8 bits data into a file.
 * Convert data from dec to hex.
 *
 * @param  FILE *fd            IN  file descriptor
 * @param  u_char value        IN  Data to write into the file 
 * @return void 
 **********************************************************************/  
void SetNextChar(FILE *fd, unsigned char value);

/***********************************************************************
 * Write a CR to the end of a file.
 *
 * @param  FILE *fd            IN  file descriptor
 * @return void                  
 **********************************************************************/  
void SetCR(FILE *fd);

/***********************************************************************
 * Compute a checksum of all the program memory.
 *
 * @param  pHexFile_t p_hfFile IN  HEX structure reference
 * @return unsigned short          Checksum computed (16 bits long)
 **********************************************************************/  
unsigned short ComputeMemChecksum(pHexFile_t p_hfFile);

/***********************************************************************
 * Compute a checksum of all the config memory.
 *
 * @param  pHexFile_t p_hfFile IN  HEX structure reference
 * @return unsigned short          Checksum computed (16 bits long)
 **********************************************************************/  
unsigned short ComputeConfigChecksum(pHexFile_t p_hfFile);

/***********************************************************************
 * Compute a checksum of 4 lower bits of the ID location.
 *
 * @param  pHexFile_t p_hfFile IN  HEX structure reference
 * @return unsigned short          Checksum computed (16 bits long)
 **********************************************************************/ 
unsigned short ComputeIDChecksum(pHexFile_t p_hfFile);

#if defined(__cplusplus)
}
#endif

#endif /* __COM_H__ */

