/* 
   USB led driver firmware
   Copyright 2008 Jacob Joseph <jacob@jjoseph.org>

   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 3 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, see <http://www.gnu.org/licenses/>.
*/

/*#define NO_BIT_DEFINES*/
#include "pic/pic16f887.h"

/* Configuration bits (p210-211) */
typedef unsigned int config1;
/*config1 at _CONFIG1 __CONFIG1 = _DEBUG_ON &
_LVP_OFF &
_FCMEN_OFF &
_IESO_OFF &
_BOR_OFF &
_CPD_OFF &
_CP_OFF &
_MCLRE_OFF &
_PWRTE_OFF &
_WDT_OFF &
_INTRC_OSC_CLKOUT;*/  /* 101: internal oscillator, clkout on RA6 */
config1 at _CONFIG1 __CONFIG1 = 0x20F5;


#define FT_TXE   RC6
#define FT_RXF   RC7
#define FT_RD    RB1
#define FT_WR    RB0
#define FT_BYTE  PORTD

#define TI_GSCLK RA6
#define TI_XLAT  RE0
#define TI_BLANK RE1
#define TI_SCLK  RC3
#define TI_SOUT  RC4
#define TI_SIN   RC5
/*#define TI_DCPRG RE1*/
#define TI_VPRG  RE2

#define xD0 PORTD ^= 0x01
#define xD1 PORTD ^= 0x02
#define xD2 PORTD ^= 0x04
#define xD3 PORTD ^= 0x08
#define xD4 PORTD ^= 0x10
#define xD5 PORTD ^= 0x20
#define xD6 PORTD ^= 0x40
#define xD7 PORTD ^= 0x80

struct led {
  int r;                /* 12 bits */
  int g;
  int b;
  unsigned char rdot;   /* 6 bits */
  unsigned char gdot;   
  unsigned char bdot;   
};

void init_pic(void);
unsigned char usb_read_byte(void);
void usb_write_byte(unsigned char c);
void usb_read(void);
void usb_write( unsigned char c);
void spi_send_byte( unsigned char b);
void ti_send_dot(void);

