//========================================================================================
//
// USB MASS STORAGE DRIVER FOR PIC18F452
// Version: 2.01
//
//========================================================================================
// Author : Guillaume Fondeville
// Email : g.fondeville@free.fr
// Site : http://g.fondeville.free.fr
//========================================================================================
// Micro : PIC18F452 16MHz
// SIE : SL811HS (Cypress)
// WatchDog disable, Oscillator HS
// UART : 115200, 8 data bits, no parity, 1 start, 1 stop
//
//--------- GNU GPL LICENCE --------------------------------------------------
// This file is subject to the terms of the GNU General Public License as
// published by the Free Software Foundation. A copy of this license is
// included with this software distribution in the file COPYING. If you
// do not have a copy, you may obtain a copy by writing to the Free
// Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// This software 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
//========================================================================================
#include <pic18fxx2.h>
#include <pic18.h>
//========================================================================================
// Functions
//========================================================================================
//UART
void init232(char speed);
void tx232(char val);
char rx232_wait(void);
void tx232_puts(const char * s);
void tx232_2ascii(unsigned char car);
void tx232_long(unsigned long car);
//USB
unsigned char sl811_init(void);
unsigned char sl811_usb_ready(void);
unsigned char sl811_write_sector(unsigned long sector, unsigned char *buf);
unsigned char sl811_read_sector(unsigned long sector, unsigned char *buf);
void display_sector(unsigned char * buf);
void sl811_write_next(unsigned char value);
unsigned char sl811_read_next(void);
void pause_ms(unsigned char tps);
//========================================================================================
// Global Variables
//========================================================================================
unsigned char buffer[512];
unsigned char data_toggle, adr, endp_in, endp_out;
//========================================================================================
// Constant definitions
//========================================================================================
#define INTRQ RB0
#define NRST RB1
#define LED_G RB2
#define LED_R RB3
#define BP_R RB4
#define BP_N RB5
#define NWR RC0
#define NCS RC1
#define NRD RC2
#define A0 RC3
#define SL811_DATA PORTD
#define READY 0
#define COMPLETED 0
#define NO_DEVICE 1
#define NOT_FULL_SPEED 2
#define NOT_MASS_STORAGE 3
#define ERROR 4
#define BUSY 5
#define STALL 6
//Macros
#define p_data_out(); {TRISD = 0x00;};
#define p_data_in(); {TRISD = 0xFF;};
#define USB_DEBUGMODE
//========================================================================================
// Interruption
//========================================================================================
static void interrupt
isr(void)
{;}
//========================================================================================
// Programme main
//========================================================================================
void main (void)
{
unsigned char tmp;
unsigned long sector;
// port configuration
TRISA = 0b11111111;
TRISB = 0b11110001;
TRISC = 0b11110000;
TRISD = 0b11111111;
TRISE = 0b00000111;
ADCON1 = 7; //enable digital I/O
//port initialisation
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
pause_ms(255);
init232(8); //Init UART 115200 8 for an oscillator 16 MHz
LED_G = 1;
tx232_puts("\n\n\rStart program\n\r");
sl811_init(); //SIE init & wait device attachment
LED_R = 1;
tx232_puts("\n\rPlease BP_G to read a sector\n\r");
while(BP_N == 1);
//read sector
sector = 0x00000000;
sl811_read_sector(sector, buffer);
//display sector
tx232_puts("\r\nSector 0x");
tx232_long(sector);
display_sector(buffer);
tx232_puts("\n\rPlease BP_O to write modified sector\n\r");
while(BP_R == 1);
//Modify sector
buffer[0x1FC] = buffer[0x1FC] + 1; //increment byte 508 of the sector
//Write sector
sl811_write_sector(sector, buffer);
//Check sector
sl811_read_sector(sector, buffer);
//Display sector
tx232_puts("\r\nSector 0x");
tx232_long(sector);
display_sector(buffer);
while(1); //end of program, reset PIC to restart
}
//************************************************************
// SL811 FUNCTIONS
//************************************************************
/****************************************************************************************/
//Write one byte in sl811 register
/****************************************************************************************/
void sl811_write(unsigned char adr, unsigned char value)
{
p_data_out();
NCS = 0;
A0 = 0;
SL811_DATA = adr;
NWR = 0;
asm("nop");
NWR = 1;
A0 = 1;
SL811_DATA = value;
NWR = 0;
asm("nop");
NWR = 1;
NCS = 1;
p_data_in();
}
/****************************************************************************************/
// Read one byte in sl811 register
/****************************************************************************************/
unsigned char sl811_read(unsigned char adr)
{
unsigned char value;
p_data_out();
NCS = 0;
A0 = 0;
SL811_DATA = adr;
NWR = 0;
asm("nop");
NWR = 1;
A0 = 1;
p_data_in();
NRD = 0;
asm("nop");
value = SL811_DATA;
NRD = 1;
NCS = 1;
p_data_in();
return value;
}
/****************************************************************************************/
//Write buffer
/****************************************************************************************/
void sl811_write_buf(unsigned char adr, unsigned char * buffer, unsigned char size)
{
unsigned char i;
sl811_write(adr, buffer[0]);
size--;
i = 1;
while(size != 0)
{
sl811_write_next(buffer[i]);
i++;
size--;
}
}
/****************************************************************************************/
//Read buffer
/****************************************************************************************/
void sl811_read_buf(unsigned char adr, unsigned char * buffer, unsigned char size)
{
unsigned char i;
buffer[0] = sl811_read(adr);
size --;
i = 1;
while(size != 0)
{
buffer[i] = sl811_read_next();
i++;
size--;
}
}
/****************************************************************************************/
//Write without specify address
/****************************************************************************************/
void sl811_write_next(unsigned char value)
{
A0 = 1;
NCS = 0;
p_data_out();
SL811_DATA = value;
NWR = 0;
asm("nop");
NWR = 1;
NCS = 1;
p_data_in();
}
/****************************************************************************************/
//Read without specify addre
评论0