//Ngo Hung Cuong - kidteam@gmail.com
#include <p18f2550.h>
#include <delays.h>
#include "KT_Proc/KT_Proc.h"
#include "usb.h"
// Note: there are timing related problems associated with GET_FEATURE
// when run at less than 48 MHz
#define CLK_48MHZ 1
#define led LATCbits.LATC6
/*
#pragma config USBDIV = ON, FOSC = HS, CPUDIV = CLKDIV3, PLLEN = ON, PCLKEN = OFF
#pragma config FCMEN = ON, IESO = ON, PWRTEN = ON, BOREN = OFF, BORV = 19
#pragma config WDTEN = OFF, WDTPS = 32768, HFOFST = OFF, MCLRE = OFF, STVREN = OFF
#pragma config LVP = OFF, BBSIZ = OFF, XINST = OFF, CP0 = ON, CP1 = ON,CPB = ON
#pragma config CPD = ON, WRT0 = OFF, WRT1 = OFF, WRTC = OFF, WRTB = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTRB = OFF
*/
//dau tien la sua lai config
#pragma config PLLDIV = 3, CPUDIV = OSC4_PLL6, USBDIV = 2, FOSC = HSPLL_HS
#pragma config FCMEN = ON, IESO = ON, PWRT = ON, BOR = OFF, BORV = 3, VREGEN = ON
#pragma config WDT = OFF, WDTPS = 32768, CCP2MX = OFF, PBADEN = OFF, LPT1OSC = OFF
#pragma config MCLRE = OFF, STVREN = ON, LVP = OFF, XINST = OFF, CP0 = ON
#pragma config CP1 = ON, CP2 = ON, CP3 = ON, CPB = ON, CPD = ON, WRT0 = OFF
#pragma config WRT1 = OFF, WRT2 = OFF, WRT3 = OFF, WRTC = ON, WRTB = OFF
#pragma config WRTD = OFF, EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
// HID feature buffer
//volatile unsigned char HIDFeatureBuffer[HID_FEATURE_REPORT_BYTES];
byte txBuffer[HID_INPUT_REPORT_BYTES]; //fixed by Ngo Hung Cuong
byte rxBuffer[HID_OUTPUT_REPORT_BYTES]; //fixed by Ngo Hung Cuong
extern byte transferType;
void ProcessIO(void);
/*===============================================================
chi can khoi tao 2 ham nay
===============================================================*/
void Kidteam_Init(void);
void Kidteam_Task(void);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//===============================================================
//===============================================================
/*
#pragma code low_vector=0x8
void low_interrupt (void)
{
}
#pragma code
*/
// Allocate buffers in RAM for storage of bytes that have either just
// come in from the SIE or are waiting to go out to the SIE.
// Regardless of what the USB is up to, we check the USART to see
// if there's something we should be doing.
// Entry point for user initialization
void main(void)
{
//kiem tra xem co vao mode bootload hay khong
byte led_cnt=0;
TRISB=0xff;
INTCON2bits.RBPU=1;
if(!PORTBbits.RB7) {
//vao mode bootloader
//while(1);//lap luon//chay chuong trinh cu
} else {
_asm
goto 0x1000//dau vao chuong trinh
_endasm
}
// Set all I/O pins to digital
//ANSEL=ANSELH=0;
// Initialize USB
UCFG = 0x14; // Enable pullup resistors; full speed mode
deviceState = DETACHED;
remoteWakeup = 0x00;
currentConfiguration = 0x00;
// Call user initialization function
Kidteam_Init();
while(1)
{
// Ensure USB module is available
EnableUSBModule();
// As long as we aren't in test mode (UTEYE), process
// USB transactions.
if(UCFGbits.UTEYE != 1)
ProcessUSBTransactions();
// Application specific tasks
ProcessIO();
//Blink LED2 @ 1Hz when configured
if(INTCONbits.TMR0IF){
INTCONbits.TMR0IF=0;
led_cnt++;
if (deviceState<CONFIGURED&&led_cnt>=40){
led = !led;
led_cnt=0;
}
else if (deviceState == CONFIGURED&&led_cnt>=5){
led = !led;
led_cnt=0;
}
}
}
}
//=======================================================================
void Kidteam_Init(void)
{
T0CON=0x80; // Timer0 period = T*2^16:2 = 5.46ms:2 = 10.93ms (PS=2)
//LATC=0;
TRISC=0;
//TRISB=0xff;//chu y
//INTCON2bits.RABPU=1;//loai bo tro treo noi
//T1CON=0x80;
//T1CON=0b10110100;
//PIR1bits.TMR1IF=0;
}
#define LOBYTE(x) (*((char *)&x))
#define HIBYTE(x) (*(((char *)&x)+1))
// If we got some bytes from the host, then echo them back.
void ProcessIO(void)
{
// User Application USB tasks
if ((deviceState < CONFIGURED) || (UCONbits.SUSPND==1))
return;
//them vao day
Kidteam_Task();
}
// Initialization for a SET_FEATURE request. This routine will be
// invoked during the setup stage and is used to set up the buffer
// for receiving data from the host
void SetupFeatureReport(byte reportID)
{
/*
if (reportID == 0)
{
// When the report arrives in the data stage, the data will be
// stored in HIDFeatureBuffer.
//inPtr = (byte*)&HIDFeatureBuffer;
}
*/
}
// Post processing for a SET_FEATURE request. After all the data has
// been delivered from host to device, this will be invoked to perform
// application specific processing.
void SetFeatureReport(byte reportID)
{
// Currently only handling report 0, ignore any others.
/*
if (reportID == 0)
{
// Set the state of PORTB based on the first byte
// of the feature report.
//LATB = HIDFeatureBuffer[0];
// x10 to signal feature transfer
//LATBbits.LATB1=1;
//LATBbits.LATB0=0;
}
*/
}
// Handle a feature report request on the control pipe
void GetFeatureReport(byte reportID)
{
if (reportID == 0)
{
wCount = HID_FEATURE_REPORT_BYTES;
transferType=0;
}
}
// Handle control out. This might be an alternate way of processing
// an output report, so all that's needed is to point the output
// pointer to the output buffer
// Initialization for a SET_REPORT request. This routine will be
// invoked during the setup stage and is used to set up the buffer
// for receiving data from the host
void SetupOutputReport(byte reportID)
{
if (reportID == 0)
{
// When the report arrives in the data stage, the data will be
// stored in HIDFeatureBuffer
inPtr = (byte*)&HIDRxBuffer;
}
}
// Post processing for a SET_REPORT request. After all the data has
// been delivered from host to device, this will be invoked to perform
// application specific processing.
void SetOutputReport(byte reportID)
{
/*
// Currently only handling report 0, ignore any others.
if (reportID != 0)
return;
*/
}
// Handle a control input report
void GetInputReport(byte reportID)
{
if (reportID == 0)
{
outPtr = (byte *)&HIDTxBuffer;
wCount = HID_INPUT_REPORT_BYTES;
transferType=0;
}
}
// Entry point of the firmware
void Kidteam_Task(void) {
byte rxCnt, cmd;
rxCnt = HIDRxReport(rxBuffer, HID_OUTPUT_REPORT_BYTES);
if (rxCnt == 0)
return;
cmd=rxBuffer[0];
switch(cmd) {
/*
case KT_ISP_CMD_GETVER:
KT_ISP_GetVer();
break;
*/
//======================================
//89S
/*
case KT_89S_INIT:
KT_89S_Init();
break;
case KT_89S_EXIT:
KT_89S_Exit();
break;
*/
case KT_ISP_CMD_READFL:
KT_ISP_ReadFlash();
break;
case KT_ISP_CMD_WRITEFL:
KT_ISP_WriteFlash();
break;
case KT_ISP_CMD_ERASEFL:
KT_ISP_EraseFlash();
break;
case KT_ISP_CMD_READEEP:
KT_ISP_ReadEEP();
break;
case KT_ISP_CMD_WRITEEEP:
KT_ISP_WriteEEP();
break;
case KT_ISP_CMD_RESET:
KT_ISP_Reset();
break;
}
}
void KT_ISP_ReadFlash(void) {
//thu doc flash tai dia chi 0x00 8 byte roi tra ve
char i;//,tmp;
EECON1bits.EEPGD=1;
EECON1bits.CFGS=0;
TBLPTRU=rxBuffer[1];
TBLPTRH=rxBuffer[2];
TBLPTRL=rxBuffer[3];
//n=rxBuffer[4];
for(i=0;i<32;++i) {
//TBLPTRL=i;
_asm
TBLRDPOSTINC
//BSR 0
//movlw tmp
_endasm
txBuffer[i]=TABLAT;
}
while (ep1Bi.Stat & UOWN)
ProcessUSBTransactions();
HIDTxReport(txBuffer, HID_INPUT_REPORT_BYTES);
}
//------------------------------------------------------------------------
void KT_ISP_WriteFlash(void) {
char i;//,tmp;
TBLPTRL=0;
for(i=0;i<32;++i) {
//TBLPTRL=i;
TABLAT=rxBuffer[4+i];
_asm
TBLWTPOSTINC
//BSR 0
//movlw tmp
_endasm
}
//xong 64 byte trong bo dem
EECON1bits.EEPGD=1;//truy cap flash khong phai eeprom
EECON1bits.CFGS=0;//flash khong phai config
//EECON1bits.FREE=1;//lenh xoa
EECON1bits.WREN=1;//cho phep
INTCONbits.GIE=0;
TBLPTRU=rxBuffer[1];
TBLPTRH=rxBuffer[2];
TBLPTRL=rxBuffer[3];
EECON2=0x55;
EECON2=0xaa;
EECON1bits.WR=1;
//doc nguoc lai
txBuffer[0]='W';
while (ep1Bi.Stat & UOWN)
ProcessUSBTransactions();
HIDTxReport(txBuffer, HID_INPUT_REPORT_BYTES);
}
//------------------------------------------------------------------------
void KT_ISP_EraseFlash(void) {
//xem no can xoa dia chi nao thi xoa o do
TBLPTRU=rxBuffer[1];
TBLPTRH=rxBuffer[2];
TBLPTRL=rxBuffer[3];
EECON1bits.EEPGD=1;
EECON1bits.CFGS=0;
EECON1bits.WREN=1;
EECON1bits.FREE=1;
INTCONbits.GIE=0;
EECON2=0x55;
EECON2=0xaa;
EECON1bits.WR=1;
txBuffer[0]='E';
while (ep1Bi.Stat & UOWN)
ProcessUSBTransactions();
HIDTxReport(txBuffer, HID_INPUT_REPORT_BYTES);
}
void KT_ISP_WriteEEP(void) {
//do some thing
txBuffer[0]=0x14;
while (ep1Bi.Stat & UOWN)
ProcessUSBTransactions();
HIDTxReport(txBuffer, HID_INPUT_REPORT_BYTES);
}
void KT_ISP_ReadEEP(void) {
EEADR=0;
//EEDATA=0x14;//full
EECON1bits.EEPGD=0;
EECON1bits.CFGS=0;
EECON1bits.RD=1;
txBuffer[0]=EEDATA;
while (ep1Bi.Stat & UOWN)
ProcessUSBTransactions();
HIDTxReport(txBuffer, HID_INPUT_REPORT_BYTES);
}
void KT_ISP_Reset(void) {
_asm
RESET
_endasm
}