/*
 * $Id: protocol.h 198 2005-10-01 14:20:12Z anders $
 * 
 * Copyright (C) 2005 Anders Dubgaard <anders@dubgaard.net>
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _PROTOCOL_H
#define _PROTOCOL_H

#include "psx.h"
#include "crc.h"

/* The code assumes a 16bit SOF. It was difficult to make it neat
 * without being bothered by the LowByte-first-then-HighByte endian-ness... */
#define PSX_DATA_LEN (sizeof(psx_data)) /* 7B */
#define PREAMBLE     0x5555
#define PREAMBLE_LEN 2
#define SOF          0x33CC
#define SOF_LEN      2       
/* HEADER_LEN = 13 if 2B preamble and 2B SOF */
#define HEADER_LEN  (PREAMBLE_LEN + SOF_LEN)
#define PACKAGE_LEN (HEADER_LEN + PSX_DATA_LEN + CRC_LEN)
#define PAYLOAD_LEN (PACKAGE_LEN - HEADER_LEN)

volatile uint8_t package[PACKAGE_LEN]; /* package[x] == *(package + x) */
volatile crc pkg_crc;

void package_init(void);  /* For use when transmitting */
volatile uint8_t *update_package(volatile psx_data *);
int check_package_crc(volatile psx_data *);

#endif  // _PROTOCOL_H
